Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import com.google.common.base.Preconditions; |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ListMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.CachingAnalysisEnvironment; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.ConfiguredAspect; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.ConfiguredAspectFactory; |
| 23 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 24 | import com.google.devtools.build.lib.analysis.RuleConfiguredTarget; |
| 25 | import com.google.devtools.build.lib.analysis.TargetAndConfiguration; |
| 26 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 27 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.Label; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.events.StoredEventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.Attribute; |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.Package; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.Rule; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.Target; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.rules.SkylarkRuleClassFunctions.SkylarkAspect; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.rules.SkylarkRuleClassFunctions.SkylarkAspectClass; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import com.google.devtools.build.lib.skyframe.AspectValue.AspectKey; |
| 42 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction.DependencyEvaluationException; |
| 43 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor.BuildViewProvider; |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.syntax.Type.ConversionException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.skyframe.SkyFunction; |
| 46 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 47 | import com.google.devtools.build.skyframe.SkyKey; |
| 48 | import com.google.devtools.build.skyframe.SkyValue; |
| 49 | |
| 50 | import java.util.Set; |
| 51 | |
| 52 | import javax.annotation.Nullable; |
| 53 | |
| 54 | /** |
| 55 | * The Skyframe function that generates aspects. |
| 56 | */ |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 57 | public final class AspectFunction implements SkyFunction { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | private final BuildViewProvider buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 59 | private final RuleClassProvider ruleClassProvider; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 61 | public AspectFunction(BuildViewProvider buildViewProvider, RuleClassProvider ruleClassProvider) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | this.buildViewProvider = buildViewProvider; |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 63 | this.ruleClassProvider = ruleClassProvider; |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Load Skylark aspect from an extension file. Is to be called from a SkyFunction. |
| 68 | * |
| 69 | * @return {@code null} if dependencies cannot be satisfied. |
| 70 | */ |
| 71 | @Nullable |
| 72 | public static SkylarkAspect loadSkylarkAspect( |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 73 | Environment env, Label extensionLabel, String skylarkValueName) |
| 74 | throws ConversionException { |
| 75 | |
| 76 | SkyKey importFileKey = SkylarkImportLookupValue.key(extensionLabel); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 77 | SkylarkImportLookupValue skylarkImportLookupValue = |
| 78 | (SkylarkImportLookupValue) env.getValue(importFileKey); |
| 79 | if (skylarkImportLookupValue == null) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | Object skylarkValue = skylarkImportLookupValue.getEnvironmentExtension().get(skylarkValueName); |
| 84 | if (!(skylarkValue instanceof SkylarkAspect)) { |
| 85 | throw new ConversionException( |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 86 | skylarkValueName + " from " + extensionLabel.toString() + " is not an aspect"); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 87 | } |
| 88 | return (SkylarkAspect) skylarkValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Nullable |
| 92 | @Override |
| 93 | public SkyValue compute(SkyKey skyKey, Environment env) |
Florian Weikert | 4b67d4f | 2015-09-14 13:35:34 +0000 | [diff] [blame] | 94 | throws AspectFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 96 | NestedSetBuilder<Package> transitivePackages = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | AspectKey key = (AspectKey) skyKey.argument(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 98 | ConfiguredAspectFactory aspectFactory; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 99 | if (key.getAspectClass() instanceof NativeAspectClass<?>) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 100 | aspectFactory = |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 101 | (ConfiguredAspectFactory) ((NativeAspectClass<?>) key.getAspectClass()).newInstance(); |
| 102 | } else if (key.getAspectClass() instanceof SkylarkAspectClass) { |
| 103 | SkylarkAspectClass skylarkAspectClass = (SkylarkAspectClass) key.getAspectClass(); |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 104 | SkylarkAspect skylarkAspect; |
| 105 | try { |
| 106 | skylarkAspect = |
| 107 | loadSkylarkAspect( |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 108 | env, skylarkAspectClass.getExtensionLabel(), skylarkAspectClass.getExportedName()); |
| 109 | } catch (ConversionException e) { |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 110 | throw new AspectFunctionException(skyKey, e); |
| 111 | } |
| 112 | if (skylarkAspect == null) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | aspectFactory = new SkylarkAspectFactory(skylarkAspect.getName(), skylarkAspect); |
| 117 | } else { |
| 118 | throw new IllegalStateException(); |
| 119 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | |
| 121 | PackageValue packageValue = |
| 122 | (PackageValue) env.getValue(PackageValue.key(key.getLabel().getPackageIdentifier())); |
| 123 | if (packageValue == null) { |
| 124 | return null; |
| 125 | } |
| 126 | |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 127 | Package pkg = packageValue.getPackage(); |
| 128 | if (pkg.containsErrors()) { |
| 129 | throw new AspectFunctionException( |
| 130 | skyKey, new BuildFileContainsErrorsException(key.getLabel().getPackageIdentifier())); |
| 131 | } |
| 132 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 133 | Target target; |
| 134 | try { |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 135 | target = pkg.getTarget(key.getLabel().getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 136 | } catch (NoSuchTargetException e) { |
| 137 | throw new AspectFunctionException(skyKey, e); |
| 138 | } |
| 139 | |
| 140 | if (!(target instanceof Rule)) { |
| 141 | throw new AspectFunctionException(new AspectCreationException( |
| 142 | "aspects must be attached to rules")); |
| 143 | } |
| 144 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 145 | final ConfiguredTargetValue configuredTargetValue = |
| 146 | (ConfiguredTargetValue) |
| 147 | env.getValue(ConfiguredTargetValue.key(key.getLabel(), key.getConfiguration())); |
| 148 | if (configuredTargetValue == null) { |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 149 | // TODO(bazel-team): remove this check when top-level targets also use dynamic configurations. |
| 150 | // Right now the key configuration may be dynamic while the original target's configuration |
| 151 | // is static, resulting in a Skyframe cache miss even though the original target is, in fact, |
| 152 | // precomputed. |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 153 | return null; |
| 154 | } |
| 155 | RuleConfiguredTarget associatedTarget = |
| 156 | (RuleConfiguredTarget) configuredTargetValue.getConfiguredTarget(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 157 | if (associatedTarget == null) { |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | SkyframeDependencyResolver resolver = view.createDependencyResolver(env); |
| 162 | if (resolver == null) { |
| 163 | return null; |
| 164 | } |
| 165 | |
| 166 | TargetAndConfiguration ctgValue = |
| 167 | new TargetAndConfiguration(target, key.getConfiguration()); |
| 168 | |
| 169 | try { |
| 170 | // Get the configuration targets that trigger this rule's configurable attributes. |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 171 | Set<ConfigMatchingProvider> configConditions = ConfiguredTargetFunction.getConfigConditions( |
| 172 | target, env, resolver, ctgValue, transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | if (configConditions == null) { |
| 174 | // Those targets haven't yet been resolved. |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | ListMultimap<Attribute, ConfiguredTarget> depValueMap = |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 179 | ConfiguredTargetFunction.computeDependencies( |
| 180 | env, |
| 181 | resolver, |
| 182 | ctgValue, |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 183 | key.getAspect(), |
Dmitry Lomov | c15ba2e | 2015-10-30 15:50:01 +0000 | [diff] [blame] | 184 | configConditions, |
| 185 | ruleClassProvider, |
| 186 | view.getHostConfiguration(ctgValue.getConfiguration()), |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 187 | transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 188 | |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 189 | return createAspect( |
| 190 | env, |
| 191 | key, |
| 192 | aspectFactory, |
| 193 | associatedTarget, |
| 194 | configConditions, |
| 195 | depValueMap, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 196 | transitivePackages); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 197 | } catch (DependencyEvaluationException e) { |
| 198 | throw new AspectFunctionException(e.getRootCauseSkyKey(), e.getCause()); |
Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 199 | } catch (AspectCreationException e) { |
| 200 | throw new AspectFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | @Nullable |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 205 | private AspectValue createAspect( |
| 206 | Environment env, |
| 207 | AspectKey key, |
| 208 | ConfiguredAspectFactory aspectFactory, |
| 209 | RuleConfiguredTarget associatedTarget, |
| 210 | Set<ConfigMatchingProvider> configConditions, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 211 | ListMultimap<Attribute, ConfiguredTarget> directDeps, |
| 212 | NestedSetBuilder<Package> transitivePackages) |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 213 | throws AspectFunctionException, InterruptedException { |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 214 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | SkyframeBuildView view = buildViewProvider.getSkyframeBuildView(); |
| 216 | BuildConfiguration configuration = associatedTarget.getConfiguration(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 217 | |
| 218 | StoredEventHandler events = new StoredEventHandler(); |
| 219 | CachingAnalysisEnvironment analysisEnvironment = view.createAnalysisEnvironment( |
Ulf Adams | bfb76a9 | 2015-04-22 20:44:35 +0000 | [diff] [blame] | 220 | key, false, events, env, configuration); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 221 | if (env.valuesMissing()) { |
| 222 | return null; |
| 223 | } |
| 224 | |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 225 | ConfiguredAspect configuredAspect = |
Carmi Grushko | 2c0c458 | 2015-11-12 16:55:37 +0000 | [diff] [blame] | 226 | view.getConfiguredTargetFactory().createAspect( |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 227 | analysisEnvironment, |
| 228 | associatedTarget, |
| 229 | aspectFactory, |
Carmi Grushko | 2c0c458 | 2015-11-12 16:55:37 +0000 | [diff] [blame] | 230 | key.getAspect(), |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 231 | directDeps, |
| 232 | configConditions, |
Carmi Grushko | 2c0c458 | 2015-11-12 16:55:37 +0000 | [diff] [blame] | 233 | view.getHostConfiguration(associatedTarget.getConfiguration())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 234 | |
| 235 | events.replayOn(env.getListener()); |
| 236 | if (events.hasErrors()) { |
| 237 | analysisEnvironment.disable(associatedTarget.getTarget()); |
| 238 | throw new AspectFunctionException(new AspectCreationException( |
| 239 | "Analysis of target '" + associatedTarget.getLabel() + "' failed; build aborted")); |
| 240 | } |
| 241 | Preconditions.checkState(!analysisEnvironment.hasErrors(), |
| 242 | "Analysis environment hasError() but no errors reported"); |
| 243 | |
| 244 | if (env.valuesMissing()) { |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | analysisEnvironment.disable(associatedTarget.getTarget()); |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 249 | Preconditions.checkNotNull(configuredAspect); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 250 | |
| 251 | return new AspectValue( |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 252 | key, |
| 253 | associatedTarget.getLabel(), |
| 254 | associatedTarget.getTarget().getLocation(), |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 255 | configuredAspect, |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 256 | ImmutableList.copyOf(analysisEnvironment.getRegisteredActions()), |
| 257 | transitivePackages.build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Nullable |
| 261 | @Override |
| 262 | public String extractTag(SkyKey skyKey) { |
| 263 | return null; |
| 264 | } |
Marian Lobur | c62faba | 2015-09-09 10:08:06 +0000 | [diff] [blame] | 265 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 266 | /** |
| 267 | * An exception indicating that there was a problem creating an aspect. |
| 268 | */ |
| 269 | public static final class AspectCreationException extends Exception { |
| 270 | public AspectCreationException(String message) { |
| 271 | super(message); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Used to indicate errors during the computation of an {@link AspectValue}. |
| 277 | */ |
| 278 | private static final class AspectFunctionException extends SkyFunctionException { |
| 279 | public AspectFunctionException(Exception e) { |
| 280 | super(e, Transience.PERSISTENT); |
| 281 | } |
| 282 | |
| 283 | /** Used to rethrow a child error that we cannot handle. */ |
| 284 | public AspectFunctionException(SkyKey childKey, Exception transitiveError) { |
| 285 | super(transitiveError, childKey); |
| 286 | } |
| 287 | } |
Dmitry Lomov | 0b832ce | 2015-10-20 10:03:14 +0000 | [diff] [blame] | 288 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 289 | } |