diff options
author | 2018-01-10 06:39:48 -0800 | |
---|---|---|
committer | 2018-01-10 06:41:45 -0800 | |
commit | c166cd99ce9f72eed522e78d63c93ff410b6dc18 (patch) | |
tree | 1a416ea9df2fb3cc427554fdcb345d4ebffdb61b /src/main/java | |
parent | 8b447ed0888ee3ba09f2357d5588e7c1e118e763 (diff) |
Automated rollback of commit 67330ad52391ad6562d439f77cc5133a0ea4247d.
*** Reason for rollback ***
Breaks nightly: b/71790513
*** Original change description ***
C++ refactoring: Separate compilation and linking calls to CcLibraryHelper
RELNOTES:none
PiperOrigin-RevId: 181457811
Diffstat (limited to 'src/main/java')
8 files changed, 137 insertions, 194 deletions
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 d5fb13f241..69bd049dad 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 @@ -93,8 +93,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { RuleContext context, CcToolchainProvider toolchain, CcLinkingOutputs linkingOutputs, - CcLinkingOutputs ccLibraryLinkingOutputs, - CppCompilationContext cppCompilationContext, + CcLibraryHelper.Info info, LinkStaticness linkStaticness, NestedSet<Artifact> filesToBuild, Iterable<Artifact> fakeLinkerInputs, @@ -117,7 +116,8 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { } if (linkCompileOutputSeparately) { builder.addArtifacts( - LinkerInputs.toLibraryArtifacts(ccLibraryLinkingOutputs.getExecutionDynamicLibraries())); + LinkerInputs.toLibraryArtifacts( + info.getCcLinkingOutputs().getExecutionDynamicLibraries())); } // For cc_binary and cc_test rules, there is an implicit dependency on // the malloc library package, which is specified by the "malloc" attribute. @@ -148,6 +148,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { sourcesBuilder.add(cppSource.getSource()); } builder.addSymlinksToArtifacts(sourcesBuilder.build()); + CppCompilationContext cppCompilationContext = info.getCppCompilationContext(); builder.addSymlinksToArtifacts(cppCompilationContext.getDeclaredIncludeSrcs()); // Add additional files that are referenced from the compile command, like module maps // or header modules. @@ -194,49 +195,30 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { if (ruleContext.hasErrors()) { return null; } - - // if cc_binary includes "linkshared=1", then gcc will be invoked with - // linkopt "-shared", which causes the result of linking to be a shared - // library. In this case, the name of the executable target should end - // in ".so" or "dylib" or ".dll". - PathFragment binaryPath = PathFragment.create(ruleContext.getTarget().getName()); - if (!isLinkShared(ruleContext)) { - binaryPath = PathFragment.create(binaryPath.getPathString() + OsUtils.executableExtension()); - } - - Artifact binary = ruleContext.getBinArtifact(binaryPath); - if (isLinkShared(ruleContext) - && !CppFileTypes.SHARED_LIBRARY.matches(binary.getFilename()) - && !CppFileTypes.VERSIONED_SHARED_LIBRARY.matches(binary.getFilename())) { - ruleContext.attributeError("linkshared", "'linkshared' used in non-shared library"); - return null; - } - - CcLibraryHelper compilationHelper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) - .fromCommon(common) - .addSources(common.getSources()) - .addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext))) - .setFake(fake) - .addPrecompiledFiles(precompiledFiles); - Info.CompilationInfo compilationInfo = compilationHelper.compile(); - CppCompilationContext cppCompilationContext = compilationInfo.getCppCompilationContext(); - CcCompilationOutputs ccCompilationOutputs = compilationInfo.getCcCompilationOutputs(); - - CcLibraryHelper linkingHelper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) - .fromCommon(common) - .addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext))) - .setFake(fake) - .enableInterfaceSharedObjects(); + List<String> linkopts = common.getLinkopts(); LinkStaticness linkStaticness = getLinkStaticness(ruleContext, linkopts, cppConfiguration, ccToolchain); + // We currently only want link the dynamic library generated for test code separately. boolean linkCompileOutputSeparately = ruleContext.isTestTarget() && cppConfiguration.getLinkCompileOutputSeparately() && linkStaticness == LinkStaticness.DYNAMIC; + + CcLibraryHelper helper = + new CcLibraryHelper( + ruleContext, + semantics, + featureConfiguration, + ccToolchain, + fdoSupport) + .fromCommon(common) + .addSources(common.getSources()) + .addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext))) + .setFake(fake) + .addPrecompiledFiles(precompiledFiles) + .enableInterfaceSharedObjects(); // When linking the object files directly into the resulting binary, we do not need // library-level link outputs; thus, we do not let CcLibraryHelper produce link outputs // (either shared object files or archives) for a non-library link type [*], and add @@ -249,9 +231,28 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { // [*] The only library link type is STATIC_LIBRARY. EXECUTABLE specifies a normal // cc_binary output, while DYNAMIC_LIBRARY is a cc_binary rules that produces an // output matching a shared object, for example cc_binary(name="foo.so", ...) on linux. - linkingHelper.setLinkType( - linkCompileOutputSeparately ? LinkTargetType.STATIC_LIBRARY : linkType); - Info.LinkingInfo linkingInfo = linkingHelper.link(ccCompilationOutputs, cppCompilationContext); + helper.setLinkType(linkCompileOutputSeparately ? LinkTargetType.STATIC_LIBRARY : linkType); + + CcLibraryHelper.Info info = helper.build(); + CppCompilationContext cppCompilationContext = info.getCppCompilationContext(); + CcCompilationOutputs ccCompilationOutputs = info.getCcCompilationOutputs(); + + // if cc_binary includes "linkshared=1", then gcc will be invoked with + // linkopt "-shared", which causes the result of linking to be a shared + // library. In this case, the name of the executable target should end + // in ".so" or "dylib" or ".dll". + PathFragment binaryPath = PathFragment.create(ruleContext.getTarget().getName()); + if (!isLinkShared(ruleContext)) { + binaryPath = PathFragment.create(binaryPath.getPathString() + OsUtils.executableExtension()); + } + + Artifact binary = ruleContext.getBinArtifact(binaryPath); + if (isLinkShared(ruleContext) + && !CppFileTypes.SHARED_LIBRARY.matches(binary.getFilename()) + && !CppFileTypes.VERSIONED_SHARED_LIBRARY.matches(binary.getFilename())) { + ruleContext.attributeError("linkshared", "'linkshared' used in non-shared library"); + return null; + } CcLinkParams linkParams = collectCcLinkParams( @@ -259,6 +260,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { linkStaticness != LinkStaticness.DYNAMIC, isLinkShared(ruleContext), linkopts); + CppLinkActionBuilder linkActionBuilder = determineLinkerArguments( ruleContext, @@ -267,8 +269,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { fdoSupport, common, precompiledFiles, - ccCompilationOutputs, - linkingInfo.getCcLinkingOutputs(), + info, cppCompilationContext.getTransitiveCompilationPrerequisites(), fake, binary, @@ -448,13 +449,12 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { ruleContext, ccToolchain, linkingOutputs, - linkingInfo.getCcLinkingOutputs(), - cppCompilationContext, + info, linkStaticness, filesToBuild, fakeLinkerInputs, fake, - compilationHelper.getCompilationUnitSources(), + helper.getCompilationUnitSources(), linkCompileOutputSeparately); RunfilesSupport runfilesSupport = RunfilesSupport.withExecutable( ruleContext, runfiles, executable); @@ -543,8 +543,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { FdoSupportProvider fdoSupport, CcCommon common, PrecompiledFiles precompiledFiles, - CcCompilationOutputs compilationOutputs, - CcLinkingOutputs linkingOutputs, + Info info, ImmutableSet<Artifact> compilationPrerequisites, boolean fake, Artifact binary, @@ -561,12 +560,12 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { // Either link in the .o files generated for the sources of this target or link in the // generated dynamic library they are compiled into. if (linkCompileOutputSeparately) { - for (LibraryToLink library : linkingOutputs.getDynamicLibraries()) { + for (LibraryToLink library : info.getCcLinkingOutputs().getDynamicLibraries()) { builder.addLibrary(library); } } else { boolean usePic = CppHelper.usePic(context, toolchain, !isLinkShared(context)); - Iterable<Artifact> objectFiles = compilationOutputs.getObjectFiles(usePic); + Iterable<Artifact> objectFiles = info.getCcCompilationOutputs().getObjectFiles(usePic); if (fake) { builder.addFakeObjectFiles(objectFiles); @@ -575,7 +574,7 @@ public abstract class CcBinary implements RuleConfiguredTargetFactory { } } - builder.addLtoBitcodeFiles(compilationOutputs.getLtoBitcodeFiles()); + builder.addLtoBitcodeFiles(info.getCcCompilationOutputs().getLtoBitcodeFiles()); builder.addNonCodeInputs(common.getLinkerScripts()); // Determine the libraries to link in. diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java index f5e9f9dea5..b9173e71b9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java @@ -23,7 +23,6 @@ import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.Runfiles; import com.google.devtools.build.lib.analysis.RunfilesProvider; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; -import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.Info; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.CppConfiguration.HeadersCheckingMode; import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; @@ -69,14 +68,13 @@ public abstract class CcImport implements RuleConfiguredTargetFactory { CcCommon.configureFeatures(ruleContext, ccToolchain); FdoSupportProvider fdoSupport = CppHelper.getFdoSupportUsingDefaultCcToolchainAttribute(ruleContext); + CcLibraryHelper helper = + new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport); - // Add headers to compilation step. + // Add headers final CcCommon common = new CcCommon(ruleContext); - Info.CompilationInfo compilationInfo = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) - .addPublicHeaders(common.getHeaders()) - .setHeadersCheckingMode(HeadersCheckingMode.STRICT) - .compile(); + helper.addPublicHeaders(common.getHeaders()); + helper.setHeadersCheckingMode(HeadersCheckingMode.STRICT); // Get alwayslink attribute boolean alwayslink = ruleContext.attributes().get("alwayslink", Type.BOOLEAN); @@ -90,17 +88,14 @@ public abstract class CcImport implements RuleConfiguredTargetFactory { .getRelative(labelName.replaceName("lib" + labelName.getBaseName())) .getPathString(); - CcLibraryHelper linkingHelper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport); - if (staticLibrary != null) { if (CppFileTypes.PIC_ARCHIVE.matches(staticLibrary.getPath())) { - linkingHelper.addPicStaticLibraries( + helper.addPicStaticLibraries( ImmutableList.of( LinkerInputs.opaqueLibraryToLink( staticLibrary, staticLibraryCategory, libraryIdentifier, alwayslink))); } else { - linkingHelper.addStaticLibraries( + helper.addStaticLibraries( ImmutableList.of( LinkerInputs.opaqueLibraryToLink( staticLibrary, staticLibraryCategory, libraryIdentifier, alwayslink))); @@ -126,7 +121,7 @@ public abstract class CcImport implements RuleConfiguredTargetFactory { sharedLibrary, libraryIdentifier)); } - linkingHelper.addExecutionDynamicLibraries(executionDynamicLibraryList); + helper.addExecutionDynamicLibraries(executionDynamicLibraryList); } if (interfaceLibrary != null) { @@ -156,20 +151,15 @@ public abstract class CcImport implements RuleConfiguredTargetFactory { } if (dynamicLibraryList != null) { - linkingHelper.addDynamicLibraries(dynamicLibraryList); + helper.addDynamicLibraries(dynamicLibraryList); } - Info.LinkingInfo linkingInfo = - linkingHelper.link( - compilationInfo.getCcCompilationOutputs(), compilationInfo.getCppCompilationContext()); + CcLibraryHelper.Info info = helper.build(); return new RuleConfiguredTargetBuilder(ruleContext) - .addProviders(compilationInfo.getProviders()) - .addProviders(linkingInfo.getProviders()) + .addProviders(info.getProviders()) .addSkylarkTransitiveInfo(CcSkylarkApiProvider.NAME, new CcSkylarkApiProvider()) - .addOutputGroups( - Info.mergeOutputGroups( - compilationInfo.getOutputGroups(), linkingInfo.getOutputGroups())) + .addOutputGroups(info.getOutputGroups()) .addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) .build(); } 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 c1a40fc579..260a7c655c 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 @@ -26,7 +26,6 @@ import com.google.devtools.build.lib.analysis.Runfiles; import com.google.devtools.build.lib.analysis.RunfilesProvider; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.test.InstrumentedFilesProvider; -import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.Info; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.vfs.Path; @@ -130,18 +129,12 @@ public abstract class CcIncLibrary implements RuleConfiguredTargetFactory { new CreateIncSymlinkAction(ruleContext.getActionOwner(), virtualArtifactMap, includeRoot)); FdoSupportProvider fdoSupport = CppHelper.getFdoSupportUsingDefaultCcToolchainAttribute(ruleContext); - Info.CompilationInfo compilationInfo = + CcLibraryHelper.Info info = new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) .addIncludeDirs(Arrays.asList(includePath)) .addPublicHeaders(virtualArtifactMap.keySet()) .addDeps(ruleContext.getPrerequisites("deps", Mode.TARGET)) - .compile(); - Info.LinkingInfo linkingInfo = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) - .addDeps(ruleContext.getPrerequisites("deps", Mode.TARGET)) - .link( - compilationInfo.getCcCompilationOutputs(), - compilationInfo.getCppCompilationContext()); + .build(); // cc_inc_library doesn't compile any file - no compilation outputs available. InstrumentedFilesProvider instrumentedFilesProvider = @@ -150,12 +143,9 @@ public abstract class CcIncLibrary implements RuleConfiguredTargetFactory { /*withBaselineCoverage=*/true); return new RuleConfiguredTargetBuilder(ruleContext) - .addProviders(compilationInfo.getProviders()) - .addProviders(linkingInfo.getProviders()) + .addProviders(info.getProviders()) .addSkylarkTransitiveInfo(CcSkylarkApiProvider.NAME, new CcSkylarkApiProvider()) - .addOutputGroups( - Info.mergeOutputGroups( - compilationInfo.getOutputGroups(), linkingInfo.getOutputGroups())) + .addOutputGroups(info.getOutputGroups()) .add(InstrumentedFilesProvider.class, instrumentedFilesProvider) .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) .build(); 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 3457fedfd0..9777523b65 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 @@ -37,7 +37,6 @@ import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction; import com.google.devtools.build.lib.packages.RawAttributeMapper; import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier; -import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper.Info; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.Link.LinkTargetType; import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; @@ -132,19 +131,14 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { return; } - CcLibraryHelper compilationHelper = + CcLibraryHelper helper = new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) .fromCommon(common) + .addLinkopts(common.getLinkopts()) .addSources(common.getSources()) .addPublicHeaders(common.getHeaders()) - .enableCompileProviders() - .addPrecompiledFiles(precompiledFiles); - - CcLibraryHelper linkingHelper = - new CcLibraryHelper(ruleContext, semantics, featureConfiguration, ccToolchain, fdoSupport) - .fromCommon(common) - .addLinkopts(common.getLinkopts()) .enableCcNativeLibrariesProvider() + .enableCompileProviders() .enableInterfaceSharedObjects() // Generate .a and .so outputs even without object files to fulfill the rule class // contract wrt. implicit output files, if the contract says so. Behavior here differs @@ -153,6 +147,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { ruleContext.getRule().getImplicitOutputsFunction() != ImplicitOutputsFunction.NONE) .setLinkType(staticLinkType) .setNeverLink(neverLink) + .addPrecompiledFiles(precompiledFiles) .addLinkstamps(ruleContext.getPrerequisites("linkstamp", Mode.TARGET)); Artifact soImplArtifact = null; @@ -185,7 +180,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { ruleContext.checkSrcsSamePackage(true); } if (ruleContext.getRule().isAttrDefined("textual_hdrs", BuildType.LABEL_LIST)) { - compilationHelper.addPublicTextualHeaders( + helper.addPublicTextualHeaders( ruleContext.getPrerequisiteArtifacts("textual_hdrs", Mode.TARGET).list()); } @@ -194,8 +189,8 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { + "Did you mean to use 'linkstatic=1' instead?"); } - linkingHelper.setCreateDynamicLibrary(createDynamicLibrary); - linkingHelper.setDynamicLibrary(soImplArtifact); + helper.setCreateDynamicLibrary(createDynamicLibrary); + helper.setDynamicLibrary(soImplArtifact); // If the reason we're not creating a dynamic library is that the toolchain // doesn't support it, then register an action which complains when triggered, @@ -257,10 +252,10 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { Iterable<LibraryToLink> picAlwayslinkLibrariesFromSrcs = LinkerInputs.opaqueLibrariesToLink( ArtifactCategory.ALWAYSLINK_STATIC_LIBRARY, precompiledFiles.getPicAlwayslinkLibraries()); - linkingHelper.addStaticLibraries(staticLibrariesFromSrcs); - linkingHelper.addStaticLibraries(alwayslinkLibrariesFromSrcs); - linkingHelper.addPicStaticLibraries(picStaticLibrariesFromSrcs); - linkingHelper.addPicStaticLibraries(picAlwayslinkLibrariesFromSrcs); + helper.addStaticLibraries(staticLibrariesFromSrcs); + helper.addStaticLibraries(alwayslinkLibrariesFromSrcs); + helper.addPicStaticLibraries(picStaticLibrariesFromSrcs); + helper.addPicStaticLibraries(picAlwayslinkLibrariesFromSrcs); Iterable<LibraryToLink> dynamicLibraries = Iterables.transform( precompiledFiles.getSharedLibraries(), @@ -269,13 +264,9 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { common.getDynamicLibrarySymlink(library, true), library, CcLinkingOutputs.libraryIdentifierOf(library))); - linkingHelper.addDynamicLibraries(dynamicLibraries); - linkingHelper.addExecutionDynamicLibraries(dynamicLibraries); - - Info.CompilationInfo compilationInfo = compilationHelper.compile(); - Info.LinkingInfo linkingInfo = - linkingHelper.link( - compilationInfo.getCcCompilationOutputs(), compilationInfo.getCppCompilationContext()); + helper.addDynamicLibraries(dynamicLibraries); + helper.addExecutionDynamicLibraries(dynamicLibraries); + CcLibraryHelper.Info info = helper.build(); /* * We always generate a static library, even if there aren't any source files. @@ -285,8 +276,7 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { * However, we only generate a dynamic library if there are source files. */ // For now, we don't add the precompiled libraries to the files to build. - CcLinkingOutputs linkedLibraries = - linkingInfo.getCcLinkingOutputsExcludingPrecompiledLibraries(); + CcLinkingOutputs linkedLibraries = info.getCcLinkingOutputsExcludingPrecompiledLibraries(); NestedSetBuilder<Artifact> filesBuilder = NestedSetBuilder.stableOrder(); filesBuilder.addAll(LinkerInputs.toLibraryArtifacts(linkedLibraries.getStaticLibraries())); @@ -298,9 +288,10 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { LinkerInputs.toNonSolibArtifacts(linkedLibraries.getExecutionDynamicLibraries())); } - CcLinkingOutputs linkingOutputs = linkingInfo.getCcLinkingOutputs(); + CcLinkingOutputs linkingOutputs = info.getCcLinkingOutputs(); if (!featureConfiguration.isEnabled(CppRuleClasses.HEADER_MODULE_CODEGEN)) { - warnAboutEmptyLibraries(ruleContext, compilationInfo.getCcCompilationOutputs(), linkStatic); + warnAboutEmptyLibraries( + ruleContext, info.getCcCompilationOutputs(), linkStatic); } NestedSet<Artifact> filesToBuild = filesBuilder.build(); @@ -310,20 +301,16 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { neverLink, addDynamicRuntimeInputArtifactsToRunfiles, false); List<Artifact> instrumentedObjectFiles = new ArrayList<>(); - instrumentedObjectFiles.addAll(compilationInfo.getCcCompilationOutputs().getObjectFiles(false)); - instrumentedObjectFiles.addAll(compilationInfo.getCcCompilationOutputs().getObjectFiles(true)); + instrumentedObjectFiles.addAll(info.getCcCompilationOutputs().getObjectFiles(false)); + instrumentedObjectFiles.addAll(info.getCcCompilationOutputs().getObjectFiles(true)); InstrumentedFilesProvider instrumentedFilesProvider = common.getInstrumentedFilesProvider(instrumentedObjectFiles, /*withBaselineCoverage=*/true); CppHelper.maybeAddStaticLinkMarkerProvider(targetBuilder, ruleContext); - targetBuilder .setFilesToBuild(filesToBuild) - .addProviders(compilationInfo.getProviders()) - .addProviders(linkingInfo.getProviders()) + .addProviders(info.getProviders()) .addSkylarkTransitiveInfo(CcSkylarkApiProvider.NAME, new CcSkylarkApiProvider()) - .addOutputGroups( - Info.mergeOutputGroups( - compilationInfo.getOutputGroups(), linkingInfo.getOutputGroups())) + .addOutputGroups(info.getOutputGroups()) .addProvider(InstrumentedFilesProvider.class, instrumentedFilesProvider) .addProvider( RunfilesProvider.class, RunfilesProvider.withData(staticRunfiles, sharedRunfiles)) @@ -333,11 +320,10 @@ public abstract class CcLibrary implements RuleConfiguredTargetFactory { .addOutputGroup( OutputGroupInfo.HIDDEN_TOP_LEVEL, collectHiddenTopLevelArtifacts( - ruleContext, ccToolchain, compilationInfo.getCcCompilationOutputs())) + ruleContext, ccToolchain, info.getCcCompilationOutputs())) .addOutputGroup( CcLibraryHelper.HIDDEN_HEADER_TOKENS, - CcLibraryHelper.collectHeaderTokens( - ruleContext, compilationInfo.getCcCompilationOutputs())); + CcLibraryHelper.collectHeaderTokens(ruleContext, info.getCcCompilationOutputs())); } private static NestedSet<Artifact> collectHiddenTopLevelArtifacts( 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 b67b6e1330..83a36f74a3 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 @@ -224,27 +224,6 @@ public final class CcLibraryHelper { public CcLinkingOutputs getCcLinkingOutputsExcludingPrecompiledLibraries() { return linkingOutputsExcludingPrecompiledLibraries; } - - /** - * Adds the static, pic-static libraries to the given builder. If addDynamicLibraries - * parameter is true, it also adds dynamic(both compile-time and execution-time) libraries. - */ - public void addLinkingOutputsTo( - NestedSetBuilder<Artifact> filesBuilder, boolean addDynamicLibraries) { - filesBuilder - .addAll(LinkerInputs.toLibraryArtifacts(linkingOutputs.getStaticLibraries())) - .addAll(LinkerInputs.toLibraryArtifacts(linkingOutputs.getPicStaticLibraries())); - if (addDynamicLibraries) { - filesBuilder - .addAll(LinkerInputs.toNonSolibArtifacts(linkingOutputs.getDynamicLibraries())) - .addAll( - LinkerInputs.toNonSolibArtifacts(linkingOutputs.getExecutionDynamicLibraries())); - } - } - - public void addLinkingOutputsTo(NestedSetBuilder<Artifact> filesBuilder) { - addLinkingOutputsTo(filesBuilder, true); - } } public Info(CompilationInfo compilationInfo, LinkingInfo linkingInfo) { @@ -294,13 +273,35 @@ public final class CcLibraryHelper { } /** + * Adds the static, pic-static libraries to the given builder. + * If addDynamicLibraries parameter is true, it also adds dynamic(both compile-time and + * execution-time) libraries. + */ + public void addLinkingOutputsTo( + NestedSetBuilder<Artifact> filesBuilder, boolean addDynamicLibraries) { + filesBuilder + .addAll(LinkerInputs.toLibraryArtifacts(linkingOutputs.getStaticLibraries())) + .addAll(LinkerInputs.toLibraryArtifacts(linkingOutputs.getPicStaticLibraries())); + if (addDynamicLibraries) { + filesBuilder + .addAll(LinkerInputs.toNonSolibArtifacts(linkingOutputs.getDynamicLibraries())) + .addAll( + LinkerInputs.toNonSolibArtifacts(linkingOutputs.getExecutionDynamicLibraries())); + } + } + + public void addLinkingOutputsTo(NestedSetBuilder<Artifact> filesBuilder) { + addLinkingOutputsTo(filesBuilder, true); + } + + /** * Merges a list of output groups into one. The sets for each entry with a given key are merged. */ public static Map<String, NestedSet<Artifact>> mergeOutputGroups( - Map<String, NestedSet<Artifact>>... outputGroups) { + List<Map<String, NestedSet<Artifact>>> outputGroupsList) { Map<String, NestedSetBuilder<Artifact>> mergedOutputGroupsBuilder = new TreeMap<>(); - for (Map<String, NestedSet<Artifact>> outputGroup : outputGroups) { + for (Map<String, NestedSet<Artifact>> outputGroup : outputGroupsList) { for (Map.Entry<String, NestedSet<Artifact>> entryOutputGroup : outputGroup.entrySet()) { String key = entryOutputGroup.getKey(); mergedOutputGroupsBuilder.computeIfAbsent( 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 234b8fdf6f..ebc2df87f5 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 @@ -35,6 +35,7 @@ import com.google.devtools.build.lib.analysis.test.InstrumentedFilesCollector; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.packages.BuildType; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; +import com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs.Builder; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ExpansionException; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables.StringSequenceBuilder; @@ -811,7 +812,7 @@ public final class CppModel { private void createHeaderAction( Label sourceLabel, String outputName, - CcCompilationOutputs.Builder result, + Builder result, AnalysisEnvironment env, CppCompileActionBuilder builder, boolean generateDotd) @@ -932,7 +933,7 @@ public final class CppModel { private void createClifMatchAction( Label sourceLabel, String outputName, - CcCompilationOutputs.Builder result, + Builder result, AnalysisEnvironment env, CppCompileActionBuilder builder) throws RuleErrorException { 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 aa98d37cac..8931024b93 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 @@ -31,7 +31,6 @@ import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; import com.google.devtools.build.lib.analysis.TransitiveInfoProvider; import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMap; -import com.google.devtools.build.lib.analysis.TransitiveInfoProviderMapBuilder; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; @@ -42,7 +41,6 @@ import com.google.devtools.build.lib.packages.NativeAspectClass; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; 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.CcLibraryHelper.Info; import com.google.devtools.build.lib.rules.cpp.CcToolchain; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider; @@ -146,12 +144,13 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect FeatureConfiguration featureConfiguration = getFeatureConfiguration(supportData); ProtoConfiguration protoConfiguration = ruleContext.getFragment(ProtoConfiguration.class); - CcLibraryHelper compilationHelper = initializeCompilationHelper(featureConfiguration); + CcLibraryHelper helper = initializeCcLibraryHelper(featureConfiguration); + helper.addDeps(ruleContext.getPrerequisites("deps", TARGET)); // Compute and register files generated by this proto library. Collection<Artifact> outputs = new ArrayList<>(); if (areSrcsBlacklisted()) { - registerBlacklistedSrcs(supportData, compilationHelper); + registerBlacklistedSrcs(supportData, helper); headerProvider = null; } else if (supportData.hasProtoSources()) { Collection<Artifact> headers = @@ -161,8 +160,8 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect outputs.addAll(headers); outputs.addAll(sources); - compilationHelper.addSources(sources); - compilationHelper.addPublicHeaders(headers); + helper.addSources(sources); + helper.addPublicHeaders(headers); NestedSetBuilder<Artifact> publicHeaderPaths = NestedSetBuilder.stableOrder(); publicHeaderPaths.addAll(headers); @@ -177,7 +176,7 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect NestedSetBuilder<Artifact> transitiveHeaders = NestedSetBuilder.stableOrder(); for (ProtoCcHeaderProvider provider : ruleContext.getPrerequisites("deps", TARGET, ProtoCcHeaderProvider.class)) { - compilationHelper.addPublicTextualHeaders(provider.getHeaders()); + helper.addPublicTextualHeaders(provider.getHeaders()); transitiveHeaders.addTransitive(provider.getHeaders()); } headerProvider = new ProtoCcHeaderProvider(transitiveHeaders.build()); @@ -187,24 +186,11 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect filesBuilder.addAll(outputs); createProtoCompileAction(supportData, outputs); - Info.CompilationInfo compilationInfo = compilationHelper.compile(); - Info.LinkingInfo linkingInfo = - initializeLinkingHelper(featureConfiguration) - .link( - compilationInfo.getCcCompilationOutputs(), - compilationInfo.getCppCompilationContext()); - - ccLibraryProviders = - new TransitiveInfoProviderMapBuilder() - .addAll(compilationInfo.getProviders()) - .addAll(linkingInfo.getProviders()) - .build(); - outputGroups = - ImmutableMap.copyOf( - Info.mergeOutputGroups( - compilationInfo.getOutputGroups(), linkingInfo.getOutputGroups())); + CcLibraryHelper.Info info = helper.build(); + ccLibraryProviders = info.getProviders(); + outputGroups = info.getOutputGroups(); // On Windows, dynamic library is not built by default, so don't add them to filesToBuild. - linkingInfo.addLinkingOutputsTo( + info.addLinkingOutputsTo( filesBuilder, !featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)); } @@ -241,29 +227,17 @@ public class CcProtoAspect extends NativeAspectClass implements ConfiguredAspect featureConfiguration, ccToolchain(ruleContext), CppHelper.getFdoSupportUsingDefaultCcToolchainAttribute(ruleContext)); - TransitiveInfoCollection runtime = getProtoToolchainProvider().runtime(); - if (runtime != null) { - helper.addDeps(ImmutableList.of(runtime)); - } - - helper.addDeps(ruleContext.getPrerequisites("deps", TARGET)); - return helper; - } - - private CcLibraryHelper initializeCompilationHelper(FeatureConfiguration featureConfiguration) { - return initializeCcLibraryHelper(featureConfiguration); - } - - private CcLibraryHelper initializeLinkingHelper(FeatureConfiguration featureConfiguration) { - CcLibraryHelper helper = - initializeCcLibraryHelper(featureConfiguration) - .enableCcSpecificLinkParamsProvider() - .enableCcNativeLibrariesProvider(); + helper.enableCcSpecificLinkParamsProvider(); + helper.enableCcNativeLibrariesProvider(); // TODO(dougk): Configure output artifact with action_config // once proto compile action is configurable from the crosstool. if (!ccToolchain(ruleContext).supportsDynamicLinker()) { helper.setCreateDynamicLibrary(false); } + TransitiveInfoCollection runtime = getProtoToolchainProvider().runtime(); + if (runtime != null) { + helper.addDeps(ImmutableList.of(runtime)); + } return helper; } diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java index 80c8ddce93..7e4fa5ad73 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java @@ -479,12 +479,14 @@ public class CompilationSupport { resultLink.link( compilationOutputsBuilder.build(), compilationContextBuilder.build()); - Map<String, NestedSet<Artifact>> mergedOutputGroups = - Info.mergeOutputGroups( + List<Map<String, NestedSet<Artifact>>> outputGroupsList = + Arrays.asList( objcArcCompilationInfo.getOutputGroups(), nonObjcArcCompilationInfo.getOutputGroups(), linkingInfo.getOutputGroups()); + Map<String, NestedSet<Artifact>> mergedOutputGroups = Info.mergeOutputGroups(outputGroupsList); + return new Pair<>(compilationOutputsBuilder.build(), ImmutableMap.copyOf(mergedOutputGroups)); } |