From 397096cd0eae05236b5a5467e980a6214a364175 Mon Sep 17 00:00:00 2001 From: plf Date: Wed, 23 Jan 2019 07:29:48 -0800 Subject: [PATCH] C++: Remove precompiled files from CcCompilationHelper. Working towards #4570 RELNOTES:none PiperOrigin-RevId: 230526082 --- .../build/lib/rules/cpp/CcBinary.java | 14 ++++-- .../lib/rules/cpp/CcCompilationHelper.java | 50 ------------------- .../lib/rules/cpp/CcCompilationOutputs.java | 2 + .../build/lib/rules/cpp/CcLibrary.java | 15 ++++-- .../lib/rules/objc/CompilationSupport.java | 18 ++++--- 5 files changed, 37 insertions(+), 62 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 597999088b8ee5..72306014cbf960 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 @@ -312,11 +312,19 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont .addPrivateHeaders(common.getPrivateHeaders()) .addSources(common.getSources()) .addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext))) - .setFake(fake) - .addPrecompiledFiles(precompiledFiles); + .setFake(fake); CompilationInfo compilationInfo = compilationHelper.compile(); CcCompilationContext ccCompilationContext = compilationInfo.getCcCompilationContext(); - CcCompilationOutputs ccCompilationOutputs = compilationInfo.getCcCompilationOutputs(); + CcCompilationOutputs precompiledFileObjects = + new CcCompilationOutputs.Builder() + .addObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ false)) + .addPicObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true)) + .build(); + CcCompilationOutputs ccCompilationOutputs = + new CcCompilationOutputs.Builder() + .merge(precompiledFileObjects) + .merge(compilationInfo.getCcCompilationOutputs()) + .build(); // Allows the dynamic library generated for code of default dynamic mode targets to be linked // separately. The main use case for default dynamic mode is the cc_test rule. The same behavior diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java index 42eb06b403386d..84b80383b4e9e2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java @@ -260,8 +260,6 @@ public CcCompilationContext getCcCompilationContext() { private final List additionalExportedHeaders = new ArrayList<>(); private final List additionalCppModuleMaps = new ArrayList<>(); private final LinkedHashMap compilationUnitSources = new LinkedHashMap<>(); - private final List objectFiles = new ArrayList<>(); - private final List picObjectFiles = new ArrayList<>(); private ImmutableList copts = ImmutableList.of(); private CoptsFilter coptsFilter = CoptsFilter.alwaysPasses(); private final Set defines = new LinkedHashSet<>(); @@ -573,34 +571,6 @@ public ImmutableSet getCompilationUnitSources() { return ImmutableSet.copyOf(this.compilationUnitSources.values()); } - /** - * Add the corresponding files as linker inputs for no-PIC links. If the corresponding files are - * compiled with PIC, the final link may or may not fail. Note that the final link may not happen - * here, if {@code --start_end_lib} is enabled, but instead at any binary that transitively - * depends on the current rule. - */ - public CcCompilationHelper addObjectFiles(Iterable objectFiles) { - for (Artifact objectFile : objectFiles) { - Preconditions.checkArgument(Link.OBJECT_FILETYPES.matches(objectFile.getFilename())); - } - Iterables.addAll(this.objectFiles, objectFiles); - return this; - } - - /** - * Add the corresponding files as linker inputs for PIC links. If the corresponding files are not - * compiled with PIC, the final link may or may not fail. Note that the final link may not happen - * here, if {@code --start_end_lib} is enabled, but instead at any binary that transitively - * depends on the current rule. - */ - public CcCompilationHelper addPicObjectFiles(Iterable picObjectFiles) { - for (Artifact objectFile : objectFiles) { - Preconditions.checkArgument(Link.OBJECT_FILETYPES.matches(objectFile.getFilename())); - } - Iterables.addAll(this.picObjectFiles, picObjectFiles); - return this; - } - public CcCompilationHelper setCopts(Iterable copts) { this.copts = ImmutableList.copyOf(copts); return this; @@ -637,17 +607,6 @@ public CcCompilationHelper addCcCompilationContexts( return this; } - /** - * Adds the given precompiled files to this helper. Shared and static libraries are added as - * compilation prerequisites, and object files are added as pic or no-PIC object files - * respectively. - */ - public CcCompilationHelper addPrecompiledFiles(PrecompiledFiles precompiledFiles) { - addObjectFiles(precompiledFiles.getObjectFiles(false)); - addPicObjectFiles(precompiledFiles.getObjectFiles(true)); - return this; - } - /** * Sets the given directories to by loose include directories that are only allowed to be * referenced when headers checking is {@link HeadersCheckingMode#LOOSE}. @@ -805,15 +764,6 @@ public CompilationInfo compile() throws RuleErrorException { // Create compile actions (both PIC and no-PIC). CcCompilationOutputs ccOutputs = createCcCompileActions(); - if (!objectFiles.isEmpty() || !picObjectFiles.isEmpty()) { - // Merge the pre-compiled object files into the compiler outputs. - ccOutputs = - new CcCompilationOutputs.Builder() - .merge(ccOutputs) - .addObjectFiles(objectFiles) - .addPicObjectFiles(picObjectFiles) - .build(); - } DwoArtifactsCollector dwoArtifacts = DwoArtifactsCollector.transitiveCollector( diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java index e0d88788cbbbcb..80a53ff98f8a76 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java @@ -27,6 +27,8 @@ /** A structured representation of the compilation outputs of a C++ rule. */ public class CcCompilationOutputs implements CcCompilationOutputsApi { + public static final CcCompilationOutputs EMPTY = new CcCompilationOutputs.Builder().build(); + /** * All .o files built by the 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 9ce22cc2225a33..4e6c4ac1dd2c5c 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 @@ -144,8 +144,7 @@ public static void init( .addSources(common.getSources()) .addPrivateHeaders(common.getPrivateHeaders()) .addPublicHeaders(common.getHeaders()) - .enableCompileProviders() - .addPrecompiledFiles(precompiledFiles); + .enableCompileProviders(); CcLinkingHelper linkingHelper = new CcLinkingHelper( @@ -257,7 +256,17 @@ public static void init( } CompilationInfo compilationInfo = compilationHelper.compile(); - CcCompilationOutputs ccCompilationOutputs = compilationInfo.getCcCompilationOutputs(); + CcCompilationOutputs precompiledFilesObjects = + new CcCompilationOutputs.Builder() + .addObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true)) + .addPicObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true)) + .build(); + CcCompilationOutputs ccCompilationOutputs = + new CcCompilationOutputs.Builder() + .merge(precompiledFilesObjects) + .merge(compilationInfo.getCcCompilationOutputs()) + .build(); + // 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 // between Bazel and Blaze. 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 cb16df2c1dbb56..fd307ff6be6676 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 @@ -295,7 +295,6 @@ private CompilationInfo compile( CcToolchainProvider ccToolchain, FdoContext fdoContext, Iterable priorityHeaders, - PrecompiledFiles precompiledFiles, Collection sources, Collection privateHdrs, Collection publicHdrs, @@ -304,7 +303,7 @@ private CompilationInfo compile( Iterable deps, ObjcCppSemantics semantics, String purpose) - throws RuleErrorException, InterruptedException { + throws RuleErrorException { CcCompilationHelper result = new CcCompilationHelper( ruleContext, @@ -319,7 +318,6 @@ private CompilationInfo compile( .addDefines(objcProvider.get(DEFINE)) .enableCompileProviders() .addPublicHeaders(publicHdrs) - .addPrecompiledFiles(precompiledFiles) .addDeps(deps) // Not all our dependencies need to export cpp information. // For example, objc_proto_library can depend on a proto_library rule that does not @@ -392,7 +390,6 @@ private Pair>> cc ccToolchain, fdoContext, priorityHeaders, - precompiledFiles, arcSources, privateHdrs, publicHdrs, @@ -411,7 +408,6 @@ private Pair>> cc ccToolchain, fdoContext, priorityHeaders, - precompiledFiles, nonArcSources, privateHdrs, publicHdrs, @@ -452,7 +448,17 @@ private Pair>> cc String.format("%s_merged_arc_non_arc_objc", semantics.getPurpose())); semantics.setupCcCompilationContext(ruleContext, ccCompilationContextBuilder); - CcCompilationOutputs.Builder compilationOutputsBuilder = new CcCompilationOutputs.Builder(); + CcCompilationOutputs precompiledFilesObjects = + new CcCompilationOutputs.Builder() + .addObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ false)) + .addPicObjectFiles(precompiledFiles.getObjectFiles(/* usePic= */ true)) + .build(); + + CcCompilationOutputs.Builder compilationOutputsBuilder = + new CcCompilationOutputs.Builder() + .merge(objcArcCompilationInfo.getCcCompilationOutputs()) + .merge(nonObjcArcCompilationInfo.getCcCompilationOutputs()) + .merge(precompiledFilesObjects); compilationOutputsBuilder.merge(objcArcCompilationInfo.getCcCompilationOutputs()); compilationOutputsBuilder.merge(nonObjcArcCompilationInfo.getCcCompilationOutputs()); CcCompilationOutputs compilationOutputs = compilationOutputsBuilder.build();