diff options
Diffstat (limited to 'src')
20 files changed, 197 insertions, 136 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppSemantics.java index e770d0625a..e7b9830928 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppSemantics.java @@ -24,7 +24,6 @@ import com.google.devtools.build.lib.rules.cpp.CppCompileActionBuilder; import com.google.devtools.build.lib.rules.cpp.CppCompileActionContext; import com.google.devtools.build.lib.rules.cpp.CppConfiguration; import com.google.devtools.build.lib.rules.cpp.CppConfiguration.HeadersCheckingMode; -import com.google.devtools.build.lib.rules.cpp.CppHelper; import com.google.devtools.build.lib.rules.cpp.CppSemantics; import com.google.devtools.build.lib.rules.cpp.IncludeProcessing; import com.google.devtools.build.lib.rules.cpp.NoProcessing; @@ -54,7 +53,7 @@ public class BazelCppSemantics implements CppSemantics { actionBuilder.setActionContext(CppCompileActionContext.class); // Because Bazel does not support include scanning, we need the entire crosstool filegroup, // including header files, as opposed to just the "compile" filegroup. - actionBuilder.addTransitiveMandatoryInputs(CppHelper.getToolchain(ruleContext).getCrosstool()); + actionBuilder.addTransitiveMandatoryInputs(actionBuilder.getToolchain().getCrosstool()); actionBuilder.setShouldScanIncludes(false); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java index 6d8f41ce88..d0b29958b5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java @@ -83,6 +83,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { private static Runfiles collectRunfiles( RuleContext context, + CcToolchainProvider toolchain, CcLinkingOutputs linkingOutputs, CcLibraryHelper.Info info, LinkStaticness linkStaticness, @@ -101,7 +102,6 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { builder.addArtifacts(linkingOutputs.getLibrariesForRunfiles(true)); builder.addRunfiles(context, RunfilesProvider.DEFAULT_RUNFILES); builder.add(context, runfilesMapping); - CcToolchainProvider toolchain = CppHelper.getToolchain(context); // Add the C++ runtime libraries if linking them dynamically. if (linkStaticness == LinkStaticness.DYNAMIC) { builder.addTransitiveArtifacts(toolchain.getDynamicRuntimeLinkInputs()); @@ -161,8 +161,10 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleContext, boolean fake) throws InterruptedException, RuleErrorException { ruleContext.checkSrcsSamePackage(true); - FeatureConfiguration featureConfiguration = CcCommon.configureFeatures(ruleContext); CcCommon common = new CcCommon(ruleContext); + CcToolchainProvider ccToolchain = common.getToolchain(); + FeatureConfiguration featureConfiguration = + CcCommon.configureFeatures(ruleContext, ccToolchain); CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class); PrecompiledFiles precompiledFiles = new PrecompiledFiles(ruleContext); LinkTargetType linkType = @@ -183,7 +185,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { && linkStaticness == LinkStaticness.DYNAMIC; CcLibraryHelper helper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration) + new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain) .fromCommon(common) .addSources(common.getSources()) .addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext))) @@ -224,10 +226,10 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { ruleContext.attributeError("linkshared", "'linkshared' used in non-shared library"); return null; } - CppLinkActionBuilder linkActionBuilder = determineLinkerArguments( ruleContext, + ccToolchain, common, precompiledFiles, info, @@ -238,8 +240,6 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { linkopts, linkCompileOutputSeparately); linkActionBuilder.setUseTestOnlyFlags(ruleContext.isTestTarget()); - - CcToolchainProvider ccToolchain = CppHelper.getToolchain(ruleContext); if (linkStaticness == LinkStaticness.DYNAMIC) { linkActionBuilder.setRuntimeInputs( ArtifactCategory.DYNAMIC_LIBRARY, @@ -288,6 +288,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { ltoArtifacts.scheduleLTOBackendAction( ruleContext, featureConfiguration, + ccToolchain, usePic, /*generateDwo=*/ cppConfiguration.useFission()); } @@ -320,7 +321,8 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { // Create the stripped binary, but don't add it to filesToBuild; it's only built when requested. Artifact strippedFile = ruleContext.getImplicitOutputArtifact( CppRuleClasses.CC_BINARY_STRIPPED); - CppHelper.createStripAction(ruleContext, cppConfiguration, executable, strippedFile); + CppHelper.createStripAction( + ruleContext, ccToolchain, cppConfiguration, executable, strippedFile); DwoArtifactsCollector dwoArtifacts = collectTransitiveDwoArtifacts( @@ -332,7 +334,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { ltoBackendArtifacts); Artifact dwpFile = ruleContext.getImplicitOutputArtifact(CppRuleClasses.CC_BINARY_DEBUG_PACKAGE); - createDebugPackagerActions(ruleContext, cppConfiguration, dwpFile, dwoArtifacts); + createDebugPackagerActions(ruleContext, ccToolchain, cppConfiguration, dwpFile, dwoArtifacts); // The debug package should include the dwp file only if it was explicitly requested. Artifact explicitDwpFile = dwpFile; @@ -356,6 +358,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { Runfiles runfiles = collectRunfiles( ruleContext, + ccToolchain, linkingOutputs, info, linkStaticness, @@ -432,6 +435,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { */ private static CppLinkActionBuilder determineLinkerArguments( RuleContext context, + CcToolchainProvider toolchain, CcCommon common, PrecompiledFiles precompiledFiles, CcLibraryHelper.Info info, @@ -443,8 +447,8 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { boolean linkCompileOutputSeparately) throws InterruptedException { CppLinkActionBuilder builder = - new CppLinkActionBuilder(context, binary) - .setCrosstoolInputs(CppHelper.getToolchain(context).getLink()) + new CppLinkActionBuilder(context, binary, toolchain) + .setCrosstoolInputs(toolchain.getLink()) .addNonCodeInputs(compilationPrerequisites); // Either link in the .o files generated for the sources of this target or link in the @@ -556,7 +560,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { * Creates the actions needed to generate this target's "debug info package" * (i.e. its .dwp file). */ - private static void createDebugPackagerActions(RuleContext context, + private static void createDebugPackagerActions(RuleContext context, CcToolchainProvider toolchain, CppConfiguration cppConfiguration, Artifact dwpOutput, DwoArtifactsCollector dwoArtifactsCollector) { Iterable<Artifact> allInputs = getDwpInputs(context, @@ -575,7 +579,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { } // Get the tool inputs necessary to run the dwp command. - NestedSet<Artifact> dwpTools = CppHelper.getToolchain(context).getDwp(); + NestedSet<Artifact> dwpTools = toolchain.getDwp(); Preconditions.checkState(!dwpTools.isEmpty()); List<SpawnAction.Builder> packagers = createIntermediateDwpPackagers( diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java index 8e58a103fc..5989d605b8 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java @@ -95,9 +95,13 @@ public final class CcCommon { private final RuleContext ruleContext; + private final CcToolchainProvider ccToolchain; + public CcCommon(RuleContext ruleContext) { this.ruleContext = ruleContext; this.cppConfiguration = ruleContext.getFragment(CppConfiguration.class); + this.ccToolchain = + Preconditions.checkNotNull(CppHelper.getToolchain(ruleContext, ":cc_toolchain")); } /** @@ -272,6 +276,13 @@ public final class CcCommon { } /** + * Returns the C++ toolchain provider. + */ + public CcToolchainProvider getToolchain() { + return ccToolchain; + } + + /** * Returns the files from headers and does some sanity checks. Note that this method reports * warnings to the {@link RuleContext} as a side effect, and so should only be called once for any * given rule. @@ -491,8 +502,8 @@ public final class CcCommon { ? InstrumentedFilesProviderImpl.EMPTY : InstrumentedFilesCollector.collect( ruleContext, CppRuleClasses.INSTRUMENTATION_SPEC, CC_METADATA_COLLECTOR, files, - CppHelper.getGcovFilesIfNeeded(ruleContext), - CppHelper.getCoverageEnvironmentIfNeeded(ruleContext), + CppHelper.getGcovFilesIfNeeded(ruleContext, ccToolchain), + CppHelper.getCoverageEnvironmentIfNeeded(ruleContext, ccToolchain), withBaselineCoverage); } @@ -581,24 +592,14 @@ public final class CcCommon { } /** - * Creates a feature configuration for a given rule. - * - * @param ruleContext the context of the rule we want the feature configuraiton for. - * @param sourceCategory the category of sources to be used in this build. - * @return the feature configuration for the given {@code ruleContext}. - */ - public static FeatureConfiguration configureFeatures( - RuleContext ruleContext, SourceCategory sourceCategory) { - return configureFeatures(ruleContext, CppHelper.getToolchain(ruleContext), sourceCategory); - } - - /** * Creates a feature configuration for a given rule. Assumes strictly cc sources. * * @param ruleContext the context of the rule we want the feature configuration for. + * @param toolchain C++ toolchain provider. * @return the feature configuration for the given {@code ruleContext}. */ - public static FeatureConfiguration configureFeatures(RuleContext ruleContext) { - return configureFeatures(ruleContext, SourceCategory.CC); + public static FeatureConfiguration configureFeatures( + RuleContext ruleContext, CcToolchainProvider toolchain) { + return configureFeatures(ruleContext, toolchain, SourceCategory.CC); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcIncLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcIncLibrary.java index b4ffc91cbb..4ca632a37e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcIncLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcIncLibrary.java @@ -59,7 +59,9 @@ public abstract class CcIncLibrary implements RuleConfiguredTargetFactory { @Override public ConfiguredTarget create(final RuleContext ruleContext) throws RuleErrorException, InterruptedException { - FeatureConfiguration featureConfiguration = CcCommon.configureFeatures(ruleContext); + CcToolchainProvider ccToolchain = CppHelper.getToolchain(ruleContext, ":cc_toolchain"); + FeatureConfiguration featureConfiguration = + CcCommon.configureFeatures(ruleContext, ccToolchain); PathFragment packageFragment = ruleContext.getPackageDirectory(); // The rule needs a unique location for the include directory, which doesn't conflict with any @@ -126,7 +128,7 @@ public abstract class CcIncLibrary implements RuleConfiguredTargetFactory { new CreateIncSymlinkAction(ruleContext.getActionOwner(), virtualArtifactMap, includeRoot)); CcLibraryHelper.Info info = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration) + new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain) .addIncludeDirs(Arrays.asList(includePath)) .addPublicHeaders(virtualArtifactMap.keySet()) .addDeps(ruleContext.getPrerequisites("deps", Mode.TARGET)) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java index d7d2e7a08a..e99c12e243 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java @@ -63,6 +63,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { private static Runfiles collectRunfiles(RuleContext context, CcLinkingOutputs ccLinkingOutputs, + CcToolchainProvider ccToolchain, boolean neverLink, boolean addDynamicRuntimeInputArtifactsToRunfiles, boolean linkingStatically) { Runfiles.Builder builder = new Runfiles.Builder( @@ -77,7 +78,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { builder.addDataDeps(context); if (addDynamicRuntimeInputArtifactsToRunfiles) { - builder.addTransitiveArtifacts(CppHelper.getToolchain(context).getDynamicRuntimeLinkInputs()); + builder.addTransitiveArtifacts(ccToolchain.getDynamicRuntimeLinkInputs()); } return builder.build(); } @@ -106,8 +107,10 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { boolean collectLinkstamp, boolean addDynamicRuntimeInputArtifactsToRunfiles) throws RuleErrorException, InterruptedException { - FeatureConfiguration featureConfiguration = CcCommon.configureFeatures(ruleContext); final CcCommon common = new CcCommon(ruleContext); + CcToolchainProvider ccToolchain = common.getToolchain(); + FeatureConfiguration featureConfiguration = + CcCommon.configureFeatures(ruleContext, ccToolchain); PrecompiledFiles precompiledFiles = new PrecompiledFiles(ruleContext); semantics.validateAttributes(ruleContext); @@ -116,7 +119,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { } CcLibraryHelper helper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration) + new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain) .fromCommon(common) .addLinkopts(common.getLinkopts()) .addSources(common.getSources()) @@ -258,10 +261,10 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { ruleContext, info.getCcCompilationOutputs(), linkStatic); NestedSet<Artifact> filesToBuild = filesBuilder.build(); - Runfiles staticRunfiles = collectRunfiles(ruleContext, - linkingOutputs, neverLink, addDynamicRuntimeInputArtifactsToRunfiles, true); - Runfiles sharedRunfiles = collectRunfiles(ruleContext, - linkingOutputs, neverLink, addDynamicRuntimeInputArtifactsToRunfiles, false); + Runfiles staticRunfiles = collectRunfiles(ruleContext, linkingOutputs, ccToolchain, + neverLink, addDynamicRuntimeInputArtifactsToRunfiles, true); + Runfiles sharedRunfiles = collectRunfiles(ruleContext, linkingOutputs, ccToolchain, + neverLink, addDynamicRuntimeInputArtifactsToRunfiles, false); List<Artifact> instrumentedObjectFiles = new ArrayList<>(); instrumentedObjectFiles.addAll(info.getCcCompilationOutputs().getObjectFiles(false)); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java index 7b62f1230f..e6972100c4 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java @@ -293,6 +293,7 @@ public final class CcLibraryHelper { private boolean propagateModuleMapToCompileAction = true; private final FeatureConfiguration featureConfiguration; + private CcToolchainProvider ccToolchain; /** * Creates a CcLibraryHelper. @@ -301,25 +302,19 @@ public final class CcLibraryHelper { * @param semantics CppSemantics for the build * @param featureConfiguration activated features and action configs for the build * @param sourceCatagory the candidate source types for the build + * @param ccToolchain the C++ toolchain provider for the build */ public CcLibraryHelper( RuleContext ruleContext, CppSemantics semantics, FeatureConfiguration featureConfiguration, - SourceCategory sourceCatagory) { + SourceCategory sourceCatagory, + CcToolchainProvider ccToolchain) { this.ruleContext = Preconditions.checkNotNull(ruleContext); this.semantics = Preconditions.checkNotNull(semantics); this.featureConfiguration = Preconditions.checkNotNull(featureConfiguration); this.sourceCategory = Preconditions.checkNotNull(sourceCatagory); - } - - public CcLibraryHelper( - RuleContext ruleContext, CppSemantics semantics, SourceCategory sourceCategory) { - this( - ruleContext, - semantics, - CcCommon.configureFeatures(ruleContext, sourceCategory), - sourceCategory); + this.ccToolchain = Preconditions.checkNotNull(ccToolchain); } /** @@ -328,10 +323,12 @@ public final class CcLibraryHelper { * @param ruleContext the RuleContext for the rule being built * @param semantics CppSemantics for the build * @param featureConfiguration activated features and action configs for the build + * @param ccToolchain the C++ toolchain provider for the build */ public CcLibraryHelper( - RuleContext ruleContext, CppSemantics semantics, FeatureConfiguration featureConfiguration) { - this(ruleContext, semantics, featureConfiguration, SourceCategory.CC); + RuleContext ruleContext, CppSemantics semantics, FeatureConfiguration featureConfiguration, + CcToolchainProvider ccToolchain) { + this(ruleContext, semantics, featureConfiguration, SourceCategory.CC, ccToolchain); } /** Sets fields that overlap for cc_library and cc_binary rules. */ @@ -1055,7 +1052,7 @@ public final class CcLibraryHelper { * Creates the C/C++ compilation action creator. */ private CppModel initializeCppModel() { - return new CppModel(ruleContext, semantics) + return new CppModel(ruleContext, semantics, ccToolchain) .addCompilationUnitSources(compilationUnitSources) .addCopts(copts) .setLinkTargetType(linkType) @@ -1230,7 +1227,7 @@ public final class CcLibraryHelper { contextBuilder.mergeDependentContexts( AnalysisUtils.getProviders(deps, CppCompilationContext.class)); contextBuilder.mergeDependentContexts(depContexts); - CppHelper.mergeToolchainDependentContext(ruleContext, contextBuilder); + CppHelper.mergeToolchainDependentContext(ruleContext, ccToolchain, contextBuilder); // But defines come after those inherited from deps. contextBuilder.addDefines(defines); @@ -1346,9 +1343,8 @@ public final class CcLibraryHelper { } } - CcToolchainProvider toolchain = CppHelper.getToolchain(ruleContext); - if (toolchain != null) { - result.add(toolchain.getCppCompilationContext().getCppModuleMap()); + if (ccToolchain != null) { + result.add(ccToolchain.getCppCompilationContext().getCppModuleMap()); } for (CppModuleMap additionalCppModuleMap : additionalCppModuleMaps) { result.add(additionalCppModuleMap); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java index 5027335be1..2e4b52f5be 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java @@ -269,6 +269,8 @@ public class CppCompileAction extends AbstractAction * @param actionName a string giving the name of this action for the purpose of toolchain * evaluation * @param ruleContext The rule-context that produced this action + * @param cppSemantics C++ compilation semantics + * @param ccToolchain C++ toolchain provider */ protected CppCompileAction( ActionOwner owner, @@ -305,11 +307,13 @@ public class CppCompileAction extends AbstractAction ImmutableMap<String, String> environment, String actionName, RuleContext ruleContext, - CppSemantics cppSemantics) { + CppSemantics cppSemantics, + CcToolchainProvider ccToolchain) { super( owner, createInputs( ruleContext, + ccToolchain, mandatoryInputs, context.getTransitiveCompilationPrerequisites(), optionalSourceFile, @@ -347,7 +351,7 @@ public class CppCompileAction extends AbstractAction // artifact and will definitely exist prior to this action execution. this.mandatoryInputs = mandatoryInputs; this.prunableInputs = prunableInputs; - this.builtinIncludeFiles = CppHelper.getToolchain(ruleContext).getBuiltinIncludeFiles(); + this.builtinIncludeFiles = ccToolchain.getBuiltinIncludeFiles(); this.cppSemantics = cppSemantics; if (cppSemantics.needsIncludeValidation()) { verifyIncludePaths(ruleContext); @@ -392,6 +396,7 @@ public class CppCompileAction extends AbstractAction private static NestedSet<Artifact> createInputs( RuleContext ruleContext, + CcToolchainProvider ccToolchain, NestedSet<Artifact> mandatoryInputs, Set<Artifact> prerequisites, Artifact optionalSourceFile, @@ -401,7 +406,7 @@ public class CppCompileAction extends AbstractAction builder.add(optionalSourceFile); } builder.addAll(prerequisites); - builder.addAll(CppHelper.getToolchain(ruleContext).getBuiltinIncludeFiles()); + builder.addAll(ccToolchain.getBuiltinIncludeFiles()); builder.addTransitive(mandatoryInputs); if (lipoScannables != null && lipoScannables.iterator().hasNext()) { // We need to add "legal generated scanner files" coming through LIPO scannables here. These diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java index 1ab770de45..decb598117 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileActionBuilder.java @@ -83,13 +83,15 @@ public class CppCompileActionBuilder { private Boolean shouldScanIncludes; private Map<String, String> environment = new LinkedHashMap<>(); private CppSemantics cppSemantics; + private CcToolchainProvider ccToolchain; // New fields need to be added to the copy constructor. /** * Creates a builder from a rule. This also uses the configuration and * artifact factory from the rule. */ - public CppCompileActionBuilder(RuleContext ruleContext, Artifact sourceFile, Label sourceLabel) { + public CppCompileActionBuilder(RuleContext ruleContext, Artifact sourceFile, Label sourceLabel, + CcToolchainProvider ccToolchain) { this.owner = ruleContext.getActionOwner(); this.actionContext = CppCompileActionContext.class; this.cppConfiguration = ruleContext.getFragment(CppConfiguration.class); @@ -101,6 +103,7 @@ public class CppCompileActionBuilder { this.lipoScannableMap = getLipoScannableMap(ruleContext); this.ruleContext = ruleContext; this.allowUsingHeaderModules = true; + this.ccToolchain = ccToolchain; features.addAll(ruleContext.getFeatures()); } @@ -154,6 +157,7 @@ public class CppCompileActionBuilder { this.shouldScanIncludes = other.shouldScanIncludes; this.environment = new LinkedHashMap<>(other.environment); this.cppSemantics = other.cppSemantics; + this.ccToolchain = other.ccToolchain; } public PathFragment getTempOutputFile() { @@ -312,7 +316,8 @@ public class CppCompileActionBuilder { ImmutableList.copyOf(copts), getNocoptPredicate(nocopts), ruleContext, - cppSemantics); + cppSemantics, + ccToolchain); } else { return new CppCompileAction( owner, @@ -347,7 +352,8 @@ public class CppCompileActionBuilder { ImmutableMap.copyOf(environment), getActionName(), ruleContext, - cppSemantics); + cppSemantics, + ccToolchain); } } @@ -430,9 +436,11 @@ public class CppCompileActionBuilder { public CppCompileActionBuilder setOutputs( ArtifactCategory outputCategory, String outputName, boolean generateDotd) { this.outputFile = CppHelper.getCompileOutputArtifact( - ruleContext, CppHelper.getArtifactNameForCategory(ruleContext, outputCategory, outputName)); + ruleContext, + CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, outputCategory, outputName)); if (generateDotd) { - String dotdFileName = CppHelper.getDotdFileName(ruleContext, outputCategory, outputName); + String dotdFileName = + CppHelper.getDotdFileName(ruleContext, ccToolchain, outputCategory, outputName); if (configuration.getFragment(CppConfiguration.class).getInmemoryDotdFiles()) { // Just set the path, no artifact is constructed dotdFile = new DotdFile( @@ -530,4 +538,8 @@ public class CppCompileActionBuilder { public boolean getShouldScanIncludes() { return shouldScanIncludes; } + + public CcToolchainProvider getToolchain() { + return ccToolchain; + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java index ed1dc14219..e4b19dcb2b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppHelper.java @@ -83,7 +83,7 @@ public class CppHelper { * using the ":stl" attribute. */ public static void mergeToolchainDependentContext(RuleContext ruleContext, - Builder contextBuilder) { + CcToolchainProvider toolchain, Builder contextBuilder) { if (ruleContext.getRule().getAttributeDefinition(":stl") != null) { TransitiveInfoCollection stl = ruleContext.getPrerequisite(":stl", Mode.TARGET); if (stl != null) { @@ -93,7 +93,6 @@ public class CppHelper { contextBuilder.mergeDependentContext(stl.getProvider(CppCompilationContext.class)); } } - CcToolchainProvider toolchain = getToolchain(ruleContext); if (toolchain != null) { contextBuilder.mergeDependentContext(toolchain.getCppCompilationContext()); } @@ -225,36 +224,39 @@ public class CppHelper { } public static NestedSet<Pair<String, String>> getCoverageEnvironmentIfNeeded( - RuleContext ruleContext) { + RuleContext ruleContext, CcToolchainProvider toolchain) { if (ruleContext.getConfiguration().isCodeCoverageEnabled()) { - return CppHelper.getToolchain(ruleContext).getCoverageEnvironment(); + return toolchain.getCoverageEnvironment(); } else { return NestedSetBuilder.emptySet(Order.COMPILE_ORDER); } } - public static NestedSet<Artifact> getGcovFilesIfNeeded(RuleContext ruleContext) { + public static NestedSet<Artifact> getGcovFilesIfNeeded( + RuleContext ruleContext, CcToolchainProvider toolchain) { if (ruleContext.getConfiguration().isCodeCoverageEnabled()) { - return CppHelper.getToolchain(ruleContext).getCrosstool(); + return toolchain.getCrosstool(); } else { return NestedSetBuilder.emptySet(Order.STABLE_ORDER); } } /** - * This almost trivial method looks up the :cc_toolchain attribute on the rule context, makes sure - * that it refers to a rule that has a {@link CcToolchainProvider} (gives an error otherwise), and - * returns a reference to that {@link CcToolchainProvider}. The method only returns {@code null} - * if there is no such attribute (this is currently not an error). + * This almost trivial method looks up the given cc toolchain attribute on the rule context, makes + * sure that it refers to a rule that has a {@link CcToolchainProvider} + * (gives an error otherwise), and returns a reference to that {@link CcToolchainProvider}. + * The method only returns {@code null} if there is no such attribute + * (this is currently not an error). */ - @Nullable public static CcToolchainProvider getToolchain(RuleContext ruleContext) { - if (!ruleContext.isAttrDefined(":cc_toolchain", LABEL)) { - // TODO(bazel-team): Report an error or throw an exception in this case. - return null; - } - TransitiveInfoCollection dep = ruleContext.getPrerequisite(":cc_toolchain", Mode.TARGET); - return getToolchain(ruleContext, dep); - } + @Nullable public static CcToolchainProvider getToolchain(RuleContext ruleContext, + String toolchainAttribute) { + if (!ruleContext.isAttrDefined(toolchainAttribute, LABEL)) { + // TODO(bazel-team): Report an error or throw an exception in this case. + return null; + } + TransitiveInfoCollection dep = ruleContext.getPrerequisite(toolchainAttribute, Mode.TARGET); + return getToolchain(ruleContext, dep); + } /** * This almost trivial method makes sure that the given info collection has a {@link @@ -537,11 +539,11 @@ public class CppHelper { /** * Creates an action to strip an executable. */ - public static void createStripAction(RuleContext context, + public static void createStripAction(RuleContext context, CcToolchainProvider toolchain, CppConfiguration cppConfiguration, Artifact input, Artifact output) { context.registerAction(new SpawnAction.Builder() .addInput(input) - .addTransitiveInputs(CppHelper.getToolchain(context).getStrip()) + .addTransitiveInputs(toolchain.getStrip()) .addOutput(output) .useDefaultShellEnvironment() .setExecutable(cppConfiguration.getStripExecutable()) @@ -582,18 +584,21 @@ public class CppHelper { ruleContext.getConfiguration().getBinDirectory(ruleContext.getRule().getRepository())); } - static String getArtifactNameForCategory(RuleContext ruleContext, ArtifactCategory category, + static String getArtifactNameForCategory( + RuleContext ruleContext, CcToolchainProvider toolchain, ArtifactCategory category, String outputName) { - return getToolchain(ruleContext).getFeatures().getArtifactNameForCategory(category, outputName); + return toolchain.getFeatures().getArtifactNameForCategory(category, outputName); } - static String getDotdFileName(RuleContext ruleContext, ArtifactCategory outputCategory, + static String getDotdFileName( + RuleContext ruleContext, CcToolchainProvider toolchain, ArtifactCategory outputCategory, String outputName) { String baseName = outputCategory == ArtifactCategory.OBJECT_FILE || outputCategory == ArtifactCategory.PROCESSED_HEADER ? outputName - : getArtifactNameForCategory(ruleContext, outputCategory, outputName); + : getArtifactNameForCategory(ruleContext, toolchain, outputCategory, outputName); - return getArtifactNameForCategory(ruleContext, ArtifactCategory.INCLUDED_FILE_LIST, baseName); + return getArtifactNameForCategory( + ruleContext, toolchain, ArtifactCategory.INCLUDED_FILE_LIST, baseName); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java index f99191da6b..b22e989734 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionBuilder.java @@ -185,14 +185,16 @@ public class CppLinkActionBuilder { * * @param ruleContext the rule that owns the action * @param output the output artifact + * @param toolchain the C++ toolchain provider */ - public CppLinkActionBuilder(RuleContext ruleContext, Artifact output) { + public CppLinkActionBuilder(RuleContext ruleContext, Artifact output, + CcToolchainProvider toolchain) { this( ruleContext, output, ruleContext.getConfiguration(), ruleContext.getAnalysisEnvironment(), - CppHelper.getToolchain(ruleContext)); + toolchain); } /** @@ -200,6 +202,8 @@ public class CppLinkActionBuilder { * * @param ruleContext the rule that owns the action * @param output the output artifact + * @param configuration build configuration + * @param toolchain C++ toolchain provider */ public CppLinkActionBuilder( RuleContext ruleContext, @@ -241,12 +245,15 @@ public class CppLinkActionBuilder { * @param ruleContext the rule that owns the action * @param output the output artifact * @param linkContext an immutable CppLinkAction.Context from the original builder + * @param configuration build configuration + * @param toolchain the C++ toolchain provider */ public CppLinkActionBuilder( RuleContext ruleContext, Artifact output, Context linkContext, - BuildConfiguration configuration) { + BuildConfiguration configuration, + CcToolchainProvider toolchain) { // These Builder-only fields get set in the constructor: // ruleContext, analysisEnvironment, outputPath, configuration, runtimeSolibDir this( @@ -254,7 +261,7 @@ public class CppLinkActionBuilder { output, configuration, ruleContext.getAnalysisEnvironment(), - CppHelper.getToolchain(ruleContext)); + toolchain); Preconditions.checkNotNull(linkContext); // All linkContext fields should be transferred to this Builder. @@ -504,7 +511,7 @@ public class CppLinkActionBuilder { featureConfiguration = CcCommon.configureFeatures(ruleContext, toolchain, CcLibraryHelper.SourceCategory.CC); } else { - featureConfiguration = CcCommon.configureFeatures(ruleContext); + featureConfiguration = CcCommon.configureFeatures(ruleContext, toolchain); } } @@ -1393,7 +1400,7 @@ public class CppLinkActionBuilder { // Variables arising from the toolchain buildVariables - .addAllStringVariables(CppHelper.getToolchain(ruleContext).getBuildVariables()) + .addAllStringVariables(toolchain.getBuildVariables()) .build(); CppHelper.getFdoSupport(ruleContext).getLinkOptions(featureConfiguration, buildVariables); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java index 1a2e4d95b2..1e186294aa 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppModel.java @@ -87,10 +87,13 @@ public final class CppModel { private Artifact soImplArtifact; private FeatureConfiguration featureConfiguration; private List<VariablesExtension> variablesExtensions = new ArrayList<>(); + private CcToolchainProvider ccToolchain; - public CppModel(RuleContext ruleContext, CppSemantics semantics) { + public CppModel(RuleContext ruleContext, CppSemantics semantics, + CcToolchainProvider ccToolchain) { this.ruleContext = Preconditions.checkNotNull(ruleContext); this.semantics = semantics; + this.ccToolchain = Preconditions.checkNotNull(ccToolchain); configuration = ruleContext.getConfiguration(); cppConfiguration = ruleContext.getFragment(CppConfiguration.class); } @@ -252,7 +255,7 @@ public final class CppModel { this.featureConfiguration = featureConfiguration; return this; } - + /** * @returns whether we want to provide header modules for the current target. */ @@ -446,7 +449,7 @@ public final class CppModel { buildVariables.addStringVariable("per_object_debug_info_file", dwoFile.getExecPathString()); } - buildVariables.addAllStringVariables(CppHelper.getToolchain(ruleContext).getBuildVariables()); + buildVariables.addAllStringVariables(ccToolchain.getBuildVariables()); buildVariables.addAllStringVariables(sourceSpecificBuildVariables); @@ -518,8 +521,8 @@ public final class CppModel { private void createHeaderAction(String outputName, Builder result, AnalysisEnvironment env, CppCompileActionBuilder builder, boolean generateDotd) { - String outputNameBase = CppHelper.getArtifactNameForCategory(ruleContext, - ArtifactCategory.GENERATED_HEADER, outputName); + String outputNameBase = CppHelper.getArtifactNameForCategory( + ruleContext, ccToolchain, ArtifactCategory.GENERATED_HEADER, outputName); builder .setOutputs(ArtifactCategory.PROCESSED_HEADER, outputNameBase, generateDotd) @@ -630,11 +633,11 @@ public final class CppModel { // generate .pic.o, .pic.d, .pic.gcno instead of .o, .d, .gcno.) if (generatePicAction) { String picOutputBase = CppHelper.getArtifactNameForCategory(ruleContext, - ArtifactCategory.PIC_FILE, outputName); + ccToolchain, ArtifactCategory.PIC_FILE, outputName); CppCompileActionBuilder picBuilder = copyAsPicBuilder( builder, picOutputBase, outputCategory, generateDotd); String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext, - ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase); + ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, picOutputBase); Artifact gcnoFile = enableCoverage ? CppHelper.getCompileOutputArtifact(ruleContext, gcnoFileName) : null; @@ -683,11 +686,13 @@ public final class CppModel { } if (generateNoPicAction) { - Artifact noPicOutputFile = CppHelper.getCompileOutputArtifact(ruleContext, - CppHelper.getArtifactNameForCategory(ruleContext, outputCategory, outputName)); + Artifact noPicOutputFile = CppHelper.getCompileOutputArtifact( + ruleContext, + CppHelper.getArtifactNameForCategory( + ruleContext, ccToolchain, outputCategory, outputName)); builder.setOutputs(outputCategory, outputName, generateDotd); String gcnoFileName = CppHelper.getArtifactNameForCategory(ruleContext, - ArtifactCategory.COVERAGE_DATA_FILE, outputName); + ccToolchain, ArtifactCategory.COVERAGE_DATA_FILE, outputName); // Create non-PIC compile actions Artifact gcnoFile = @@ -748,7 +753,8 @@ public final class CppModel { String getOutputNameBaseWith(String base, boolean usePic) { return usePic - ? CppHelper.getArtifactNameForCategory(ruleContext, ArtifactCategory.PIC_FILE, base) + ? CppHelper.getArtifactNameForCategory( + ruleContext, ccToolchain, ArtifactCategory.PIC_FILE, base) : base; } @@ -759,8 +765,9 @@ public final class CppModel { String outputNameBase = getOutputNameBaseWith(outputName, usePic); String tempOutputName = ruleContext.getConfiguration().getBinFragment() .getRelative(CppHelper.getObjDirectory(ruleContext.getLabel())) - .getRelative(CppHelper.getArtifactNameForCategory(ruleContext, outputCategory, - getOutputNameBaseWith(outputName + ".temp", usePic))) + .getRelative( + CppHelper.getArtifactNameForCategory(ruleContext, ccToolchain, outputCategory, + getOutputNameBaseWith(outputName + ".temp", usePic))) .getPathString(); builder .setPicMode(usePic) @@ -809,11 +816,11 @@ public final class CppModel { String maybePicName = ruleContext.getLabel().getName(); if (linkTargetType.picness() == Picness.PIC) { maybePicName = CppHelper.getArtifactNameForCategory( - ruleContext, ArtifactCategory.PIC_FILE, maybePicName); + ruleContext, ccToolchain, ArtifactCategory.PIC_FILE, maybePicName); } String linkedName = CppHelper.getArtifactNameForCategory( - ruleContext, linkTargetType.getLinkerOutput(), maybePicName); + ruleContext, ccToolchain, linkTargetType.getLinkerOutput(), maybePicName); PathFragment artifactFragment = new PathFragment(ruleContext.getLabel().getName()) .getParentDirectory().getRelative(linkedName); result = ruleContext.getBinArtifact(artifactFragment); @@ -982,8 +989,8 @@ public final class CppModel { .addLinkopts(sonameLinkopts) .setRuntimeInputs( ArtifactCategory.DYNAMIC_LIBRARY, - CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkMiddleman(), - CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkInputs()) + ccToolchain.getDynamicRuntimeLinkMiddleman(), + ccToolchain.getDynamicRuntimeLinkInputs()) .setFeatureConfiguration(featureConfiguration) .addVariablesExtensions(variablesExtensions); @@ -997,6 +1004,7 @@ public final class CppModel { ltoArtifacts.scheduleLTOBackendAction( ruleContext, featureConfiguration, + ccToolchain, usePicForSharedLibs, // If support is ever added for generating a dwp file for shared // library targets (e.g. when linkstatic=0), then this should change @@ -1044,8 +1052,8 @@ public final class CppModel { } private CppLinkActionBuilder newLinkActionBuilder(Artifact outputArtifact) { - return new CppLinkActionBuilder(ruleContext, outputArtifact) - .setCrosstoolInputs(CppHelper.getToolchain(ruleContext).getLink()) + return new CppLinkActionBuilder(ruleContext, outputArtifact, ccToolchain) + .setCrosstoolInputs(ccToolchain.getLink()) .addNonCodeInputs(context.getTransitiveCompilationPrerequisites()); } @@ -1055,10 +1063,10 @@ public final class CppModel { */ private CppCompileActionBuilder createCompileActionBuilder(Artifact source, Label label) { CppCompileActionBuilder builder = new CppCompileActionBuilder( - ruleContext, source, label); + ruleContext, source, label, ccToolchain); builder.setContext(context).addCopts(copts); - builder.addEnvironment(CppHelper.getToolchain(ruleContext).getEnvironment()); + builder.addEnvironment(ccToolchain.getEnvironment()); return builder; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java index c5d1118ee0..1e589edd80 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/FakeCppCompileAction.java @@ -83,7 +83,8 @@ public class FakeCppCompileAction extends CppCompileAction { ImmutableList<String> copts, Predicate<String> nocopts, RuleContext ruleContext, - CppSemantics cppSemantics) { + CppSemantics cppSemantics, + CcToolchainProvider ccToolchain) { super( owner, features, @@ -124,7 +125,8 @@ public class FakeCppCompileAction extends CppCompileAction { ImmutableMap.<String, String>of(), CppCompileAction.CPP_COMPILE, ruleContext, - cppSemantics); + cppSemantics, + ccToolchain); this.tempOutputFile = Preconditions.checkNotNull(tempOutputFile); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LTOBackendArtifacts.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LTOBackendArtifacts.java index b509bf9007..8b333b543c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LTOBackendArtifacts.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LTOBackendArtifacts.java @@ -108,6 +108,7 @@ public final class LTOBackendArtifacts { public void scheduleLTOBackendAction( RuleContext ruleContext, FeatureConfiguration featureConfiguration, + CcToolchainProvider ccToolchain, boolean usePic, boolean generateDwo) { LTOBackendAction.Builder builder = new LTOBackendAction.Builder(); @@ -115,7 +116,7 @@ public final class LTOBackendArtifacts { builder.addInput(bitcodeFile); builder.addInput(index); - builder.addTransitiveInputs(CppHelper.getToolchain(ruleContext).getCompile()); + builder.addTransitiveInputs(ccToolchain.getCompile()); builder.addOutput(objectFile); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkCommandLine.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkCommandLine.java index 67cb8a3e8c..69b48731f3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkCommandLine.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/LinkCommandLine.java @@ -701,7 +701,8 @@ public final class LinkCommandLine extends CommandLine { CcCommon.configureFeatures( ruleContext, toolchain, CcLibraryHelper.SourceCategory.CC); } else { - featureConfiguration = CcCommon.configureFeatures(ruleContext); + CcToolchainProvider ccToolchain = CppHelper.getToolchain(ruleContext, ":cc_toolchain"); + featureConfiguration = CcCommon.configureFeatures(ruleContext, ccToolchain); } } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java index 7e2b8e6fbb..8efef155b9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoAspect.java @@ -45,6 +45,7 @@ import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory. import com.google.devtools.build.lib.rules.cpp.CcCommon; import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; +import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider; import com.google.devtools.build.lib.rules.cpp.CppConfiguration; import com.google.devtools.build.lib.rules.cpp.CppHelper; import com.google.devtools.build.lib.rules.cpp.CppRuleClasses; @@ -213,13 +214,14 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect requestedFeatures.build(), unsupportedFeatures.build(), CcLibraryHelper.SourceCategory.CC, - CppHelper.getToolchain( - ruleContext, ruleContext.getPrerequisite(":cc_toolchain", TARGET))); + ccToolchain(ruleContext)); return featureConfiguration; } private CcLibraryHelper initializeCcLibraryHelper(FeatureConfiguration featureConfiguration) { - CcLibraryHelper helper = new CcLibraryHelper(ruleContext, cppSemantics, featureConfiguration); + CcLibraryHelper helper = + new CcLibraryHelper(ruleContext, cppSemantics, featureConfiguration, + ccToolchain(ruleContext)); helper.enableCcSpecificLinkParamsProvider(); helper.enableCcNativeLibrariesProvider(); // TODO(dougk): Configure output artifact with action_config @@ -234,6 +236,11 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect return helper; } + private static CcToolchainProvider ccToolchain(RuleContext ruleContext) { + return CppHelper.getToolchain( + ruleContext, ruleContext.getPrerequisite(":cc_toolchain", TARGET)); + } + private Collection<Artifact> getHeaders(SupportData supportData) { return ProtoCommon.getGeneratedOutputs( ruleContext, supportData.getDirectProtoSources(), ".pb.h"); diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java index 988321c6fd..a81a38ff1b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java @@ -191,7 +191,8 @@ public abstract class GenRuleBase implements RuleConfiguredTargetFactory { // TODO(bazel-team): Make the make variable expander pass back a list of these. if (requiresCrosstool(baseCommand)) { // If cc is used, silently throw in the crosstool filegroup as a dependency. - inputs.addTransitive(CppHelper.getToolchain(ruleContext).getCrosstoolMiddleman()); + inputs.addTransitive( + CppHelper.getToolchain(ruleContext, ":cc_toolchain").getCrosstoolMiddleman()); } if (requiresJdk(baseCommand)) { // If javac is used, silently throw in the jdk filegroup as a dependency. diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java index e40d8f7936..c4f9c39a2a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java @@ -223,7 +223,7 @@ public class JavaBinary implements RuleConfiguredTargetFactory { // Collect the action inputs for the runfiles collector here because we need to access the // analysis environment, and that may no longer be safe when the runfiles collector runs. Iterable<Artifact> dynamicRuntimeActionInputs = - CppHelper.getToolchain(ruleContext).getDynamicRuntimeLinkInputs(); + CppHelper.getToolchain(ruleContext, ":cc_toolchain").getDynamicRuntimeLinkInputs(); Iterables.addAll(jvmFlags, diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CrosstoolCompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CrosstoolCompilationSupport.java index cc20a9512a..60b1155d55 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/CrosstoolCompilationSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CrosstoolCompilationSupport.java @@ -77,6 +77,7 @@ public class CrosstoolCompilationSupport extends CompilationSupport { "preprocess-assemble", "c-compile", "c++-compile"); + private final CcToolchainProvider ccToolchain; /** * Creates a new CompilationSupport instance that uses the c++ rule backend @@ -104,6 +105,7 @@ public class CrosstoolCompilationSupport extends CompilationSupport { IntermediateArtifacts intermediateArtifacts, CompilationAttributes compilationAttributes) { super(ruleContext, buildConfiguration, intermediateArtifacts, compilationAttributes); + this.ccToolchain = CppHelper.getToolchain(ruleContext, ":cc_toolchain"); } @Override @@ -159,11 +161,11 @@ public class CrosstoolCompilationSupport extends CompilationSupport { .build(); CppLinkAction fullyLinkAction = - new CppLinkActionBuilder(ruleContext, outputArchive) + new CppLinkActionBuilder(ruleContext, outputArchive, ccToolchain) .addActionInputs(objcProvider.getObjcLibraries()) .addActionInputs(objcProvider.getCcLibraries()) .addActionInputs(objcProvider.get(IMPORTED_LIBRARY).toSet()) - .setCrosstoolInputs(CppHelper.getToolchain(ruleContext).getLink()) + .setCrosstoolInputs(ccToolchain.getLink()) .setLinkType(LinkTargetType.OBJC_FULLY_LINKED_ARCHIVE) .setLinkStaticness(LinkStaticness.FULLY_STATIC) .setLibraryIdentifier(libraryIdentifier) @@ -221,14 +223,14 @@ public class CrosstoolCompilationSupport extends CompilationSupport { Artifact binaryToLink = getBinaryToLink(); CppLinkAction executableLinkAction = - new CppLinkActionBuilder(ruleContext, binaryToLink) + new CppLinkActionBuilder(ruleContext, binaryToLink, ccToolchain) .setMnemonic("ObjcLink") .addActionInputs(bazelBuiltLibraries) .addActionInputs(objcProvider.getCcLibraries()) .addTransitiveActionInputs(objcProvider.get(IMPORTED_LIBRARY)) .addTransitiveActionInputs(objcProvider.get(STATIC_FRAMEWORK_FILE)) .addTransitiveActionInputs(objcProvider.get(DYNAMIC_FRAMEWORK_FILE)) - .setCrosstoolInputs(CppHelper.getToolchain(ruleContext).getLink()) + .setCrosstoolInputs(ccToolchain.getLink()) .addActionInputs(prunedJ2ObjcArchives) .addActionInput(inputFileList) .setLinkType(linkType) @@ -258,7 +260,8 @@ public class CrosstoolCompilationSupport extends CompilationSupport { includeProcessing, ruleContext.getFragment(ObjcConfiguration.class)), getFeatureConfiguration(ruleContext), - CcLibraryHelper.SourceCategory.CC_AND_OBJC) + CcLibraryHelper.SourceCategory.CC_AND_OBJC, + ccToolchain) .addSources(arcSources, ImmutableMap.of("objc_arc", "")) .addSources(nonArcSources, ImmutableMap.of("no_objc_arc", "")) .addSources(privateHdrs) diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCppSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCppSemantics.java index bb3cd93c0d..e2d49af3e9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCppSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCppSemantics.java @@ -26,7 +26,6 @@ import com.google.devtools.build.lib.rules.cpp.CppCompileActionBuilder; import com.google.devtools.build.lib.rules.cpp.CppCompileActionContext; import com.google.devtools.build.lib.rules.cpp.CppConfiguration; import com.google.devtools.build.lib.rules.cpp.CppConfiguration.HeadersCheckingMode; -import com.google.devtools.build.lib.rules.cpp.CppHelper; import com.google.devtools.build.lib.rules.cpp.CppSemantics; import com.google.devtools.build.lib.rules.cpp.HeaderDiscovery.DotdPruningMode; import com.google.devtools.build.lib.rules.cpp.IncludeProcessing; @@ -69,7 +68,7 @@ public class ObjcCppSemantics implements CppSemantics { actionBuilder.setActionContext(CppCompileActionContext.class); // Because Bazel does not support include scanning, we need the entire crosstool filegroup, // including header files, as opposed to just the "compile" filegroup. - actionBuilder.addTransitiveMandatoryInputs(CppHelper.getToolchain(ruleContext).getCrosstool()); + actionBuilder.addTransitiveMandatoryInputs(actionBuilder.getToolchain().getCrosstool()); actionBuilder.setShouldScanIncludes(false); actionBuilder.addTransitiveMandatoryInputs(objcProvider.get(STATIC_FRAMEWORK_FILE)); diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java index 75e7cd912b..2157e8673a 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppLinkActionTest.java @@ -242,7 +242,9 @@ public class CppLinkActionTest extends BuildViewTestCase { public Action generate(int i) throws InterruptedException { CppLinkActionBuilder builder = new CppLinkActionBuilder( - ruleContext, (i & 2) == 0 ? dynamicOutputFile : staticOutputFile) { + ruleContext, + (i & 2) == 0 ? dynamicOutputFile : staticOutputFile, + CppHelper.getToolchain(ruleContext, ":cc_toolchain")) { @Override protected Artifact getInterfaceSoBuilder() { return interfaceSoBuilder; @@ -292,7 +294,9 @@ public class CppLinkActionTest extends BuildViewTestCase { public Action generate(int i) throws InterruptedException { CppLinkActionBuilder builder = new CppLinkActionBuilder( - ruleContext, (i & 2) == 0 ? staticOutputFile : dynamicOutputFile) { + ruleContext, + (i & 2) == 0 ? staticOutputFile : dynamicOutputFile, + CppHelper.getToolchain(ruleContext, ":cc_toolchain")) { @Override protected Artifact getInterfaceSoBuilder() { return interfaceSoBuilder; @@ -320,7 +324,8 @@ public class CppLinkActionTest extends BuildViewTestCase { new PathFragment("output/path.ifso"), getTargetConfiguration().getBinDirectory( RepositoryName.MAIN), ActionsTestUtil.NULL_ARTIFACT_OWNER); - CppLinkActionBuilder builder = new CppLinkActionBuilder(ruleContext, output); + CppLinkActionBuilder builder = new CppLinkActionBuilder( + ruleContext, output, CppHelper.getToolchain(ruleContext, ":cc_toolchain")); builder.setLinkType(LinkTargetType.STATIC_LIBRARY); assertTrue(builder.canSplitCommandLine()); @@ -408,7 +413,7 @@ public class CppLinkActionTest extends BuildViewTestCase { getTargetConfiguration() .getBinDirectory(ruleContext.getRule().getRepository())), ruleContext.getConfiguration(), - CppHelper.getToolchain(ruleContext)) + CppHelper.getToolchain(ruleContext, ":cc_toolchain")) .addObjectFiles(nonLibraryInputs) .addLibraries(NestedSetBuilder.wrap(Order.LINK_ORDER, libraryInputs)) .setLinkType(type) |