Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import static com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment; |
| 17 | |
| 18 | import com.google.common.collect.ClassToInstanceMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.MutableClassToInstanceMap; |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionEnvironment; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.skyframe.SkyFunction; |
| 29 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 30 | import com.google.devtools.build.skyframe.SkyKey; |
| 31 | import com.google.devtools.build.skyframe.SkyValue; |
| 32 | import com.google.devtools.build.skyframe.ValueOrException; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 33 | import java.util.LinkedHashSet; |
| 34 | import java.util.Map; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
| 38 | * A builder for {@link BuildConfigurationValue} instances. |
| 39 | */ |
| 40 | public class BuildConfigurationFunction implements SkyFunction { |
| 41 | |
| 42 | private final BlazeDirectories directories; |
gregce | 490b095 | 2017-07-06 18:44:38 -0400 | [diff] [blame] | 43 | private final ConfiguredRuleClassProvider ruleClassProvider; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 44 | private final BuildOptions defaultBuildOptions; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 45 | |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 46 | public BuildConfigurationFunction( |
| 47 | BlazeDirectories directories, |
| 48 | RuleClassProvider ruleClassProvider, |
| 49 | BuildOptions defaultBuildOptions) { |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 50 | this.directories = directories; |
gregce | 490b095 | 2017-07-06 18:44:38 -0400 | [diff] [blame] | 51 | this.ruleClassProvider = (ConfiguredRuleClassProvider) ruleClassProvider; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 52 | this.defaultBuildOptions = defaultBuildOptions; |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 56 | public SkyValue compute(SkyKey skyKey, Environment env) |
| 57 | throws InterruptedException, BuildConfigurationFunctionException { |
kchodorow | 85ae190 | 2017-04-22 15:07:22 -0400 | [diff] [blame] | 58 | WorkspaceNameValue workspaceNameValue = (WorkspaceNameValue) env |
| 59 | .getValue(WorkspaceNameValue.key()); |
| 60 | if (workspaceNameValue == null) { |
| 61 | return null; |
| 62 | } |
| 63 | |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 64 | BuildConfigurationValue.Key key = (BuildConfigurationValue.Key) skyKey.argument(); |
| 65 | Set<Fragment> fragments; |
| 66 | try { |
| 67 | fragments = getConfigurationFragments(key, env); |
| 68 | } catch (InvalidConfigurationException e) { |
| 69 | throw new BuildConfigurationFunctionException(e); |
| 70 | } |
| 71 | if (fragments == null) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | ClassToInstanceMap<Fragment> fragmentsMap = MutableClassToInstanceMap.create(); |
| 76 | for (Fragment fragment : fragments) { |
| 77 | fragmentsMap.put(fragment.getClass(), fragment); |
| 78 | } |
| 79 | |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 80 | BuildOptions options = defaultBuildOptions.applyDiff(key.getOptionsDiff()); |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 81 | ActionEnvironment actionEnvironment = |
| 82 | ruleClassProvider.getActionEnvironmentProvider().getActionEnvironment(options); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 83 | |
gregce | 490b095 | 2017-07-06 18:44:38 -0400 | [diff] [blame] | 84 | BuildConfiguration config = |
| 85 | new BuildConfiguration( |
lberki | 406199f | 2018-04-09 03:16:19 -0700 | [diff] [blame] | 86 | directories, |
| 87 | fragmentsMap, |
| 88 | options, |
| 89 | key.getOptionsDiff(), |
| 90 | ruleClassProvider.getReservedActionMnemonics(), |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 91 | actionEnvironment, |
lberki | 406199f | 2018-04-09 03:16:19 -0700 | [diff] [blame] | 92 | workspaceNameValue.getName()); |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 93 | return new BuildConfigurationValue(config); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private Set<Fragment> getConfigurationFragments(BuildConfigurationValue.Key key, Environment env) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 97 | throws InvalidConfigurationException, InterruptedException { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 98 | |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 99 | // Get SkyKeys for the fragments we need to load. |
| 100 | Set<SkyKey> fragmentKeys = new LinkedHashSet<>(); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 101 | BuildOptions options = defaultBuildOptions.applyDiff(key.getOptionsDiff()); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 102 | for (Class<? extends BuildConfiguration.Fragment> fragmentClass : key.getFragments()) { |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 103 | fragmentKeys.add(ConfigurationFragmentValue.key(options, fragmentClass, ruleClassProvider)); |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Load them as Skyframe deps. |
| 107 | Map<SkyKey, ValueOrException<InvalidConfigurationException>> fragmentDeps = |
| 108 | env.getValuesOrThrow(fragmentKeys, InvalidConfigurationException.class); |
| 109 | if (env.valuesMissing()) { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | // Collect and return the results. |
| 114 | ImmutableSet.Builder<Fragment> fragments = ImmutableSet.builder(); |
| 115 | for (ValueOrException<InvalidConfigurationException> value : fragmentDeps.values()) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 116 | BuildConfiguration.Fragment fragment = |
| 117 | ((ConfigurationFragmentValue) value.get()).getFragment(); |
| 118 | if (fragment != null) { |
| 119 | fragments.add(fragment); |
| 120 | } |
Greg Estren | 531fc04 | 2015-05-26 22:37:44 +0000 | [diff] [blame] | 121 | } |
| 122 | return fragments.build(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public String extractTag(SkyKey skyKey) { |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | private static final class BuildConfigurationFunctionException extends SkyFunctionException { |
| 131 | public BuildConfigurationFunctionException(InvalidConfigurationException e) { |
| 132 | super(e, Transience.PERSISTENT); |
| 133 | } |
| 134 | } |
| 135 | } |