blob: dbede304268fbb1a16136707438e68b8f35eaaea [file] [log] [blame]
gregce3b8ffd12017-05-05 20:53:32 +02001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.analysis;
16
gregce3b8ffd12017-05-05 20:53:32 +020017import com.google.common.collect.ImmutableSet;
18import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
19import com.google.devtools.build.lib.analysis.config.BuildOptions;
20import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment;
21import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory;
22import com.google.devtools.build.lib.analysis.config.FragmentOptions;
23import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
gregceb0992922018-01-04 14:04:13 -080024import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
gregce3b8ffd12017-05-05 20:53:32 +020025import com.google.devtools.build.lib.packages.Target;
janakr146c2452018-01-24 12:54:15 -080026import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
mjhalupka860e1032018-02-27 14:15:45 -080027import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization;
gregce3b8ffd12017-05-05 20:53:32 +020028
29/**
30 * Grab bag file for test configuration fragments and fragment factories.
31 */
32public class TestConfigFragments {
33 /**
tomlu65204692017-08-04 17:41:54 +020034 * A {@link PatchTransition} that appends a given value to {@link
35 * BuildConfiguration.Options#hostCpu}.
gregce3b8ffd12017-05-05 20:53:32 +020036 */
mjhalupka860e1032018-02-27 14:15:45 -080037 @AutoCodec
38 @VisibleForSerialization
39 static class HostCpuTransition implements PatchTransition {
gregce3b8ffd12017-05-05 20:53:32 +020040
41 private final String patchMessage;
42
tomlu65204692017-08-04 17:41:54 +020043 HostCpuTransition(String patchMessage) {
gregce3b8ffd12017-05-05 20:53:32 +020044 this.patchMessage = patchMessage;
45 }
46
47 @Override
gregce3b8ffd12017-05-05 20:53:32 +020048 public BuildOptions apply(BuildOptions options) {
49 BuildOptions toOptions = options.clone();
50 BuildConfiguration.Options coreOptions =
51 toOptions.get(BuildConfiguration.Options.class);
tomlu65204692017-08-04 17:41:54 +020052 String prefix = coreOptions.hostCpu.startsWith("$") ? coreOptions.hostCpu : "";
53 coreOptions.hostCpu = prefix + "$" + patchMessage;
gregce3b8ffd12017-05-05 20:53:32 +020054 return toOptions;
55 }
56 }
57
58 /**
59 * A {@link ConfigurationFragmentFactory} that trivially returns a given fragment.
60 */
61 private static class SimpleFragmentFactory implements ConfigurationFragmentFactory {
62 private final BuildConfiguration.Fragment fragment;
63
64 public SimpleFragmentFactory(BuildConfiguration.Fragment fragment) {
65 this.fragment = fragment;
66 }
67
68 @Override
janakr146c2452018-01-24 12:54:15 -080069 public BuildConfiguration.Fragment create(
70 ConfigurationEnvironment env, BuildOptions buildOptions)
71 throws InvalidConfigurationException {
gregce3b8ffd12017-05-05 20:53:32 +020072 return fragment;
73 }
74
75 @Override
76 public Class<? extends BuildConfiguration.Fragment> creates() {
77 return fragment.getClass();
78 }
79
80 @Override
81 public ImmutableSet<Class<? extends FragmentOptions>> requiredOptions() {
82 return ImmutableSet.<Class<? extends FragmentOptions>>of();
83 }
84 }
85
janakr146c2452018-01-24 12:54:15 -080086 @AutoCodec
87 static class Hook1Fragment extends BuildConfiguration.Fragment {
janakr146c2452018-01-24 12:54:15 -080088 @Override
89 public PatchTransition topLevelConfigurationHook(Target toTarget) {
90 return new HostCpuTransition("CONFIG HOOK 1");
91 }
92 }
93
tomlu65204692017-08-04 17:41:54 +020094 /** Factory for a test fragment with a top-level configuration hook. */
gregce3b8ffd12017-05-05 20:53:32 +020095 public static SimpleFragmentFactory FragmentWithTopLevelConfigHook1Factory =
janakr146c2452018-01-24 12:54:15 -080096 new SimpleFragmentFactory(new Hook1Fragment());
97
98 /**
99 * The class definition for the BuildConfiguration.Fragment needs to be different than the one of
100 * its peer above. This is because Bazel indexes configuration fragments by class name. So we need
101 * to make sure all fragment definitions retain distinct class names.
102 */
103 @AutoCodec
104 static class Hook2Fragment extends BuildConfiguration.Fragment {
janakr146c2452018-01-24 12:54:15 -0800105 @Override
106 public PatchTransition topLevelConfigurationHook(Target toTarget) {
107 return new HostCpuTransition("CONFIG HOOK 2");
108 }
109 }
gregce3b8ffd12017-05-05 20:53:32 +0200110
tomlu65204692017-08-04 17:41:54 +0200111 /** Factory for a test fragment with a top-level configuration hook. */
gregce3b8ffd12017-05-05 20:53:32 +0200112 public static SimpleFragmentFactory FragmentWithTopLevelConfigHook2Factory =
janakr146c2452018-01-24 12:54:15 -0800113 new SimpleFragmentFactory(new Hook2Fragment());
gregce3b8ffd12017-05-05 20:53:32 +0200114}