gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 19 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
| 20 | import com.google.devtools.build.lib.analysis.config.ConfigurationEnvironment; |
| 21 | import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; |
| 22 | import com.google.devtools.build.lib.analysis.config.FragmentOptions; |
| 23 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
gregce | b099292 | 2018-01-04 14:04:13 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition; |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.packages.Target; |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
mjhalupka | 860e103 | 2018-02-27 14:15:45 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Grab bag file for test configuration fragments and fragment factories. |
| 31 | */ |
| 32 | public class TestConfigFragments { |
| 33 | /** |
tomlu | 6520469 | 2017-08-04 17:41:54 +0200 | [diff] [blame] | 34 | * A {@link PatchTransition} that appends a given value to {@link |
| 35 | * BuildConfiguration.Options#hostCpu}. |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 36 | */ |
mjhalupka | 860e103 | 2018-02-27 14:15:45 -0800 | [diff] [blame] | 37 | @AutoCodec |
| 38 | @VisibleForSerialization |
| 39 | static class HostCpuTransition implements PatchTransition { |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 40 | |
| 41 | private final String patchMessage; |
| 42 | |
tomlu | 6520469 | 2017-08-04 17:41:54 +0200 | [diff] [blame] | 43 | HostCpuTransition(String patchMessage) { |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 44 | this.patchMessage = patchMessage; |
| 45 | } |
| 46 | |
| 47 | @Override |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 48 | public BuildOptions apply(BuildOptions options) { |
| 49 | BuildOptions toOptions = options.clone(); |
| 50 | BuildConfiguration.Options coreOptions = |
| 51 | toOptions.get(BuildConfiguration.Options.class); |
tomlu | 6520469 | 2017-08-04 17:41:54 +0200 | [diff] [blame] | 52 | String prefix = coreOptions.hostCpu.startsWith("$") ? coreOptions.hostCpu : ""; |
| 53 | coreOptions.hostCpu = prefix + "$" + patchMessage; |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 54 | 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 |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 69 | public BuildConfiguration.Fragment create( |
| 70 | ConfigurationEnvironment env, BuildOptions buildOptions) |
| 71 | throws InvalidConfigurationException { |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 72 | 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 | |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 86 | @AutoCodec |
| 87 | static class Hook1Fragment extends BuildConfiguration.Fragment { |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 88 | @Override |
| 89 | public PatchTransition topLevelConfigurationHook(Target toTarget) { |
| 90 | return new HostCpuTransition("CONFIG HOOK 1"); |
| 91 | } |
| 92 | } |
| 93 | |
tomlu | 6520469 | 2017-08-04 17:41:54 +0200 | [diff] [blame] | 94 | /** Factory for a test fragment with a top-level configuration hook. */ |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 95 | public static SimpleFragmentFactory FragmentWithTopLevelConfigHook1Factory = |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 96 | 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 { |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 105 | @Override |
| 106 | public PatchTransition topLevelConfigurationHook(Target toTarget) { |
| 107 | return new HostCpuTransition("CONFIG HOOK 2"); |
| 108 | } |
| 109 | } |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 110 | |
tomlu | 6520469 | 2017-08-04 17:41:54 +0200 | [diff] [blame] | 111 | /** Factory for a test fragment with a top-level configuration hook. */ |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 112 | public static SimpleFragmentFactory FragmentWithTopLevelConfigHook2Factory = |
janakr | 146c245 | 2018-01-24 12:54:15 -0800 | [diff] [blame] | 113 | new SimpleFragmentFactory(new Hook2Fragment()); |
gregce | 3b8ffd1 | 2017-05-05 20:53:32 +0200 | [diff] [blame] | 114 | } |