Skip to content

Commit

Permalink
C++: Remove precompiled files from CcCompilationHelper.
Browse files Browse the repository at this point in the history
Working towards bazelbuild#4570
RELNOTES:none
PiperOrigin-RevId: 230526082
  • Loading branch information
oquenchil authored and weixiao-huang committed Jan 31, 2019
1 parent c0319b4 commit 397096c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ public CcCompilationContext getCcCompilationContext() {
private final List<PathFragment> additionalExportedHeaders = new ArrayList<>();
private final List<CppModuleMap> additionalCppModuleMaps = new ArrayList<>();
private final LinkedHashMap<Artifact, CppSource> compilationUnitSources = new LinkedHashMap<>();
private final List<Artifact> objectFiles = new ArrayList<>();
private final List<Artifact> picObjectFiles = new ArrayList<>();
private ImmutableList<String> copts = ImmutableList.of();
private CoptsFilter coptsFilter = CoptsFilter.alwaysPasses();
private final Set<String> defines = new LinkedHashSet<>();
Expand Down Expand Up @@ -573,34 +571,6 @@ public ImmutableSet<CppSource> 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<Artifact> 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<Artifact> picObjectFiles) {
for (Artifact objectFile : objectFiles) {
Preconditions.checkArgument(Link.OBJECT_FILETYPES.matches(objectFile.getFilename()));
}
Iterables.addAll(this.picObjectFiles, picObjectFiles);
return this;
}

public CcCompilationHelper setCopts(Iterable<String> copts) {
this.copts = ImmutableList.copyOf(copts);
return this;
Expand Down Expand Up @@ -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}.
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ private CompilationInfo compile(
CcToolchainProvider ccToolchain,
FdoContext fdoContext,
Iterable<PathFragment> priorityHeaders,
PrecompiledFiles precompiledFiles,
Collection<Artifact> sources,
Collection<Artifact> privateHdrs,
Collection<Artifact> publicHdrs,
Expand All @@ -304,7 +303,7 @@ private CompilationInfo compile(
Iterable<? extends TransitiveInfoCollection> deps,
ObjcCppSemantics semantics,
String purpose)
throws RuleErrorException, InterruptedException {
throws RuleErrorException {
CcCompilationHelper result =
new CcCompilationHelper(
ruleContext,
Expand All @@ -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
Expand Down Expand Up @@ -392,7 +390,6 @@ private Pair<CcCompilationOutputs, ImmutableMap<String, NestedSet<Artifact>>> cc
ccToolchain,
fdoContext,
priorityHeaders,
precompiledFiles,
arcSources,
privateHdrs,
publicHdrs,
Expand All @@ -411,7 +408,6 @@ private Pair<CcCompilationOutputs, ImmutableMap<String, NestedSet<Artifact>>> cc
ccToolchain,
fdoContext,
priorityHeaders,
precompiledFiles,
nonArcSources,
privateHdrs,
publicHdrs,
Expand Down Expand Up @@ -452,7 +448,17 @@ private Pair<CcCompilationOutputs, ImmutableMap<String, NestedSet<Artifact>>> 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();
Expand Down

0 comments on commit 397096c

Please sign in to comment.