aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
diff options
context:
space:
mode:
authorGravatar plf <plf@google.com>2018-01-10 06:39:48 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-10 06:41:45 -0800
commitc166cd99ce9f72eed522e78d63c93ff410b6dc18 (patch)
tree1a416ea9df2fb3cc427554fdcb345d4ebffdb61b /src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
parent8b447ed0888ee3ba09f2357d5588e7c1e118e763 (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/com/google/devtools/build/lib/rules/cpp/CcBinary.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java101
1 files changed, 50 insertions, 51 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.