diff options
author | 2015-09-01 08:36:29 +0000 | |
---|---|---|
committer | 2015-09-01 10:43:46 +0000 | |
commit | fdfdd92b44bf0bfeb3a1e98b9ce9321941a39baa (patch) | |
tree | f06157ef9343e865deb023b662dfafc69769cd40 | |
parent | 5c54bdcc5b6d686687120b19be710d08e5186da8 (diff) |
Inject the prelude path through the rule class provider.
--
MOS_MIGRATED_REVID=102020499
6 files changed, 40 insertions, 13 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/Constants.java b/src/main/java/com/google/devtools/build/lib/Constants.java index 9c0bce686f..1925876000 100644 --- a/src/main/java/com/google/devtools/build/lib/Constants.java +++ b/src/main/java/com/google/devtools/build/lib/Constants.java @@ -35,8 +35,6 @@ public class Constants { public static final String NATIVE_DEPS_LIB_SUFFIX = "_nativedeps"; - public static final String PRELUDE_FILE_DEPOT_RELATIVE_PATH = "tools/build_rules/prelude_bazel"; - public static final String ANDROID_DEFAULT_SDK = "//external:android/sdk".toString(); public static final String ANDROID_DEFAULT_CROSSTOOL = "//external:android/crosstool".toString(); public static final ImmutableList<String> ANDROID_DEFAULT_FAT_APK_CPUS = diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java index 3bea243bc7..8f048f7bf1 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType.ABSTRACT; import static com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType.TEST; +import com.google.common.base.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -41,6 +42,7 @@ import com.google.devtools.build.lib.syntax.Label; import com.google.devtools.build.lib.syntax.SkylarkEnvironment; import com.google.devtools.build.lib.syntax.SkylarkType; import com.google.devtools.build.lib.syntax.ValidationEnvironment; +import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.common.options.OptionsClassProvider; import java.lang.reflect.Constructor; @@ -77,6 +79,7 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { */ public static class Builder implements RuleDefinitionEnvironment { private final StringBuilder defaultWorkspaceFile = new StringBuilder(); + private PathFragment preludePath; private final List<ConfigurationFragmentFactory> configurationFragments = new ArrayList<>(); private final List<BuildInfoFactory> buildInfoFactories = new ArrayList<>(); private final List<Class<? extends FragmentOptions>> configurationOptions = new ArrayList<>(); @@ -99,6 +102,14 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { defaultWorkspaceFile.append(contents); } + public Builder setPrelude(String workspaceRelativePath) { + PathFragment preludePathFragment = new PathFragment(workspaceRelativePath); + Preconditions.checkArgument(!preludePathFragment.isAbsolute()); + Preconditions.checkArgument(preludePathFragment.isNormalized()); + this.preludePath = preludePathFragment; + return this; + } + public Builder setPrerequisiteValidator(PrerequisiteValidator prerequisiteValidator) { this.prerequisiteValidator = prerequisiteValidator; return this; @@ -207,6 +218,7 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { } return new ConfiguredRuleClassProvider( + preludePath, ImmutableMap.copyOf(ruleClassMap), ImmutableMap.copyOf(ruleDefinitionMap), ImmutableMap.copyOf(aspectFactoryMap), @@ -248,6 +260,11 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { String defaultWorkspaceFile; /** + * Workspace-relative path to the prelude. + */ + private final PathFragment preludePath; + + /** * Maps rule class name to the metaclass instance for that rule. */ private final ImmutableMap<String, RuleClass> ruleClassMap; @@ -286,6 +303,7 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { private final ValidationEnvironment skylarkValidationEnvironment; public ConfiguredRuleClassProvider( + PathFragment preludePath, ImmutableMap<String, RuleClass> ruleClassMap, ImmutableMap<String, Class<? extends RuleDefinition>> ruleDefinitionMap, ImmutableMap<String, Class<? extends AspectFactory<?, ?, ?>>> aspectFactoryMap, @@ -296,7 +314,7 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { ConfigurationCollectionFactory configurationCollectionFactory, PrerequisiteValidator prerequisiteValidator, ImmutableMap<String, SkylarkType> skylarkAccessibleJavaClasses) { - + this.preludePath = preludePath; this.ruleClassMap = ruleClassMap; this.ruleDefinitionMap = ruleDefinitionMap; this.aspectFactoryMap = aspectFactoryMap; @@ -317,6 +335,11 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { } @Override + public PathFragment getPreludePath() { + return preludePath; + } + + @Override public Map<String, RuleClass> getRuleClassMap() { return ruleClassMap; } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java index 7cb8012848..f337c257ba 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java @@ -219,6 +219,7 @@ public class BazelRuleClassProvider { .addBuildInfoFactory(new CppBuildInfo()) .addBuildInfoFactory(new ObjcBuildInfoFactory()) .setConfigurationCollectionFactory(new BazelConfigurationCollection()) + .setPrelude("tools/build_rules/prelude_bazel") .setPrerequisiteValidator(new BazelPrerequisiteValidator()); for (Class<? extends FragmentOptions> fragmentOptions : BUILD_OPTIONS) { diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/packages/RuleClassProvider.java index 3f24efb208..d153fa1b1a 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/RuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/packages/RuleClassProvider.java @@ -17,6 +17,7 @@ package com.google.devtools.build.lib.packages; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.syntax.SkylarkEnvironment; import com.google.devtools.build.lib.syntax.ValidationEnvironment; +import com.google.devtools.build.lib.vfs.PathFragment; import java.util.Map; @@ -24,6 +25,12 @@ import java.util.Map; * The collection of the supported build rules. Provides an Environment for Skylark rule creation. */ public interface RuleClassProvider { + + /** + * Workspace relative path to the prelude file. + */ + PathFragment getPreludePath(); + /** * Returns a map from rule names to rule class objects. */ diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java index 38b4ac4851..c8fad5061c 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java @@ -21,7 +21,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; -import com.google.devtools.build.lib.Constants; import com.google.devtools.build.lib.cmdline.PackageIdentifier; import com.google.devtools.build.lib.cmdline.PackageIdentifier.RepositoryName; import com.google.devtools.build.lib.events.Event; @@ -89,22 +88,21 @@ public class PackageFunction implements SkyFunction { private final AtomicInteger numPackagesLoaded; private final Profiler profiler = Profiler.instance(); - static final PathFragment PRELUDE_FILE_FRAGMENT = - new PathFragment(Constants.PRELUDE_FILE_DEPOT_RELATIVE_PATH); + private final PathFragment preludePath; static final String DEFAULTS_PACKAGE_NAME = "tools/defaults"; public static final String EXTERNAL_PACKAGE_NAME = "external"; - static { - Preconditions.checkArgument(!PRELUDE_FILE_FRAGMENT.isAbsolute()); - } - public PackageFunction(Reporter reporter, PackageFactory packageFactory, CachingPackageLocator pkgLocator, AtomicBoolean showLoadingProgress, Cache<PackageIdentifier, Package.LegacyBuilder> packageFunctionCache, AtomicInteger numPackagesLoaded) { this.reporter = reporter; + // Can be null in tests. + this.preludePath = packageFactory == null + ? null + : packageFactory.getRuleClassProvider().getPreludePath(); this.packageFactory = packageFactory; this.packageLocator = pkgLocator; this.showLoadingProgress = showLoadingProgress; @@ -423,7 +421,7 @@ public class PackageFunction implements SkyFunction { SkyKey astLookupKey = null; try { astLookupKey = ASTFileLookupValue.key( - PackageIdentifier.createInDefaultRepo(PRELUDE_FILE_FRAGMENT)); + PackageIdentifier.createInDefaultRepo(preludePath)); } catch (ASTLookupInputException e) { // There's a static check ensuring that PRELUDE_FILE_FRAGMENT is relative. throw new IllegalStateException(e); @@ -597,7 +595,7 @@ public class PackageFunction implements SkyFunction { // a load() statement comes from the prelude, since we just prepend those statements before // the actual BUILD file. So we use this evil .endsWith() statement to figure it out. RepositoryName repository = - entry.getKey().getPath().endsWith(PRELUDE_FILE_FRAGMENT) + entry.getKey().getPath().endsWith(preludePath) ? PackageIdentifier.DEFAULT_REPOSITORY_NAME : packageId.getRepository(); SkyKey importsLookupKey = SkylarkImportLookupValue.key( repository, buildFileFragment, importFile); diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java index 1eb86cde0a..5096ba8d2e 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java @@ -91,7 +91,7 @@ public class SkylarkImportLookupFunction implements SkyFunction { // a load() statement comes from the prelude, since we just prepend those statements before // the actual BUILD file. So we use this evil .endsWith() statement to figure it out. RepositoryName repository = - entry.getKey().getPath().endsWith(PackageFunction.PRELUDE_FILE_FRAGMENT) + entry.getKey().getPath().endsWith(ruleClassProvider.getPreludePath()) ? PackageIdentifier.DEFAULT_REPOSITORY_NAME : arg.getRepository(); SkyKey importsLookupKey = SkylarkImportLookupValue.key(repository, file, importFile); SkylarkImportLookupValue importsLookupValue; |