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