Skip to content

Commit

Permalink
C++: Makes Java native deps launcher use linking API.
Browse files Browse the repository at this point in the history
The CppLinkAction.Context provider from CcBinary is no longer needed. Only use remaining is the Python launcher.

RELNOTES:none
PiperOrigin-RevId: 214272741
  • Loading branch information
oquenchil authored and Copybara-Service committed Sep 24, 2018
1 parent 258222c commit 4cba428
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 31 deletions.
107 changes: 83 additions & 24 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.rules.apple.ApplePlatform;
import com.google.devtools.build.lib.rules.cpp.CcCommon.CcFlagsSupplier;
Expand All @@ -60,6 +63,7 @@
import com.google.devtools.build.lib.rules.cpp.Link.LinkingMode;
import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
Expand Down Expand Up @@ -90,6 +94,53 @@ protected CcBinary(CppSemantics semantics) {
@VisibleForTesting
public static final String INTERMEDIATE_DWP_DIR = "_dwps";

/** Provider for native deps launchers. DO NOT USE. */
@Deprecated
public static class CcLauncherInfo extends NativeInfo {
private static final String RESTRICTION_ERROR_MESSAGE =
"This provider is restricted to native.java_binary, native.py_binary and native.java_test. "
+ "This is a ";
public static final String PROVIDER_NAME = "CcLauncherInfo";
public static final Provider PROVIDER = new Provider();

private final CcCompilationOutputs ccCompilationOutputs;
private final CcLinkParams staticModeParamsForExecutable;

public CcLauncherInfo(
CcLinkParams staticModeParamsForExecutable, CcCompilationOutputs ccCompilationOutputs) {
super(PROVIDER);
this.staticModeParamsForExecutable = staticModeParamsForExecutable;
this.ccCompilationOutputs = ccCompilationOutputs;
}

public CcCompilationOutputs getCcCompilationOutputs(RuleContext ruleContext) {
checkRestrictedUsage(ruleContext);
return ccCompilationOutputs;
}

public CcLinkParams getStaticModeParamsForExecutable(RuleContext ruleContext) {
checkRestrictedUsage(ruleContext);
return staticModeParamsForExecutable;
}

private void checkRestrictedUsage(RuleContext ruleContext) {
Rule rule = ruleContext.getRule();
if (rule.getRuleClassObject().isSkylark()
|| (!rule.getRuleClass().equals("java_binary")
&& !rule.getRuleClass().equals("java_test")
&& !rule.getRuleClass().equals("py_binary"))) {
throw new IllegalStateException(RESTRICTION_ERROR_MESSAGE + rule.getRuleClass());
}
}

/** Provider class for {@link CcLauncherInfo} objects. */
public static class Provider extends BuiltinProvider<CcLauncherInfo> {
private Provider() {
super(PROVIDER_NAME, CcLauncherInfo.class);
}
}
}

private static Runfiles collectRunfiles(
RuleContext ruleContext,
FeatureConfiguration featureConfiguration,
Expand Down Expand Up @@ -327,7 +378,7 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
pdbFile = ruleContext.getRelatedArtifact(binary.getRootRelativePath(), ".pdb");
}

CcLinkingOutputs ccLinkingOutputsBinary =
Pair<CcLinkingOutputs, CcLinkingInfo> ccLinkingOutputsAndCcLinkingInfo =
createTransitiveLinkingActions(
ruleContext,
ccToolchain,
Expand All @@ -346,11 +397,17 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
linkingMode,
cppConfiguration,
linkType,
binaryPath,
pdbFile,
generatedDefFile,
customDefFile);

CcLinkingOutputs ccLinkingOutputsBinary = ccLinkingOutputsAndCcLinkingInfo.first;

CcLauncherInfo ccLauncherInfo =
new CcLauncherInfo(
ccLinkingOutputsAndCcLinkingInfo.second.getStaticModeParamsForExecutable(),
ccCompilationOutputs);

// Store immutable context for use in other *_binary rules that are implemented by
// linking the interpreter (Java, Python, etc.) together with native deps.
CppLinkAction.Context linkContext = ccLinkingOutputsBinary.getCppLinkActionContext();
Expand Down Expand Up @@ -490,11 +547,12 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
new DebugPackageProvider(ruleContext.getLabel(), strippedFile, binary, explicitDwpFile))
.setRunfilesSupport(runfilesSupport, binary)
.addProvider(CppLinkAction.Context.class, linkContext)
.addNativeDeclaredProvider(ccLauncherInfo)
.addSkylarkTransitiveInfo(CcSkylarkApiProvider.NAME, new CcSkylarkApiProvider())
.build();
}

public static CcLinkingOutputs createTransitiveLinkingActions(
public static Pair<CcLinkingOutputs, CcLinkingInfo> createTransitiveLinkingActions(
RuleContext ruleContext,
CcToolchainProvider ccToolchain,
FeatureConfiguration featureConfiguration,
Expand All @@ -512,21 +570,18 @@ public static CcLinkingOutputs createTransitiveLinkingActions(
LinkingMode linkingMode,
CppConfiguration cppConfiguration,
LinkTargetType linkType,
PathFragment binaryPath,
Artifact pdbFile,
Artifact generatedDefFile,
Artifact customDefFile)
throws InterruptedException, RuleErrorException {
CcLinkingHelper ccLinkingHelper =
new CcLinkingHelper(
ruleContext,
cppSemantics,
featureConfiguration,
ccToolchain,
fdoProvider,
ruleContext.getConfiguration())
.addNonCodeLinkerInputs(ccCompilationContext.getTransitiveCompilationPrerequisites())
.addNonCodeLinkerInputs(common.getLinkerScripts());
ruleContext,
cppSemantics,
featureConfiguration,
ccToolchain,
fdoProvider,
ruleContext.getConfiguration());

CcLinkParams.Builder ccLinkParamsBuilder = CcLinkParams.builder();
ccLinkParamsBuilder.addTransitiveArgs(linkParams);
Expand Down Expand Up @@ -561,6 +616,20 @@ public static CcLinkingOutputs createTransitiveLinkingActions(
}
}

if (linkingMode != Link.LinkingMode.DYNAMIC
&& !cppConfiguration.disableEmittingStaticLibgcc()) {
// Only force a static link of libgcc if static runtime linking is enabled (which
// can't be true if runtimeInputs is empty).
// TODO(bazel-team): Move this to CcToolchain.
if (!ccToolchain.getStaticRuntimeLinkInputs(featureConfiguration).isEmpty()) {
ccLinkParamsBuilder.addLinkOpts(ImmutableList.of("-static-libgcc"));
}
}

ccLinkParamsBuilder
.addNonCodeInputs(ccCompilationContext.getTransitiveCompilationPrerequisites())
.addNonCodeInputs(common.getLinkerScripts());

CcLinkParams ccLinkParams = ccLinkParamsBuilder.build();
CcLinkingInfo.Builder ccLinkingInfo = CcLinkingInfo.Builder.create();
ccLinkingInfo.setStaticModeParamsForDynamicLibrary(ccLinkParams);
Expand All @@ -574,7 +643,7 @@ public static CcLinkingOutputs createTransitiveLinkingActions(
.setShouldCreateStaticLibraries(false)
.setLinkingMode(linkingMode)
.setDynamicLinkType(linkType)
.setDynamicLibrary(binary)
.setLinkerOutputArtifact(binary)
.setNeverLink(true)
.emitInterfaceSharedObjects(
isLinkShared(ruleContext)
Expand All @@ -589,17 +658,7 @@ public static CcLinkingOutputs createTransitiveLinkingActions(
ccLinkingHelper.setDefFile(generatedDefFile);
}

if (linkingMode != Link.LinkingMode.DYNAMIC
&& !cppConfiguration.disableEmittingStaticLibgcc()) {
// Only force a static link of libgcc if static runtime linking is enabled (which
// can't be true if runtimeInputs is empty).
// TODO(bazel-team): Move this to CcToolchain.
if (!ccToolchain.getStaticRuntimeLinkInputs(featureConfiguration).isEmpty()) {
ccLinkingHelper.addLinkopts(ImmutableList.of("-static-libgcc"));
}
}

return ccLinkingHelper.link(/* ccOutputs */ ccCompilationOutputs);
return Pair.of(ccLinkingHelper.link(ccCompilationOutputs), ccLinkingInfo.build());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static void init(
}

linkingHelper.setShouldCreateDynamicLibrary(createDynamicLibrary);
linkingHelper.setDynamicLibrary(soImplArtifact);
linkingHelper.setLinkerOutputArtifact(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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public CcLinkingOutputs getCcLinkingOutputs() {
private final NestedSetBuilder<Artifact> linkstamps = NestedSetBuilder.stableOrder();
private final List<Artifact> linkActionInputs = new ArrayList<>();

@Nullable private Artifact dynamicLibrary;
@Nullable private Artifact linkerOutputArtifact;
private LinkTargetType staticLinkType = LinkTargetType.STATIC_LIBRARY;
private LinkTargetType dynamicLinkType = LinkTargetType.NODEPS_DYNAMIC_LIBRARY;
private boolean neverlink;
Expand Down Expand Up @@ -223,8 +223,8 @@ public CcLinkingHelper addVariableExtension(VariablesExtension variableExtension
* dynamic library is an implicit or explicit output of the rule, i.e., if it is accessible by
* name from other rules in the same package. Set to {@code null} to use the default computation.
*/
public CcLinkingHelper setDynamicLibrary(@Nullable Artifact dynamicLibrary) {
this.dynamicLibrary = dynamicLibrary;
public CcLinkingHelper setLinkerOutputArtifact(@Nullable Artifact linkerOutputArtifact) {
this.linkerOutputArtifact = linkerOutputArtifact;
return this;
}

Expand Down Expand Up @@ -598,7 +598,7 @@ private void createDynamicLibrary(
// Create dynamic library.
Artifact soImpl;
String mainLibraryIdentifier;
if (dynamicLibrary == null) {
if (linkerOutputArtifact == null) {
// If the crosstool is configured to select an output artifact, we use that selection.
// Otherwise, we use linux defaults.
soImpl = getLinkedArtifact(LinkTargetType.NODEPS_DYNAMIC_LIBRARY);
Expand All @@ -608,7 +608,7 @@ private void createDynamicLibrary(
// file is explicitly specified in the BUILD file and as such, is platform-dependent. Thus,
// we just hardcode some reasonable logic to compute the library identifier and hope that this
// will eventually go away.
soImpl = dynamicLibrary;
soImpl = linkerOutputArtifact;
mainLibraryIdentifier =
FileSystemUtils.removeExtension(soImpl.getRootRelativePath().getPathString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ protected static LinkingInfo link(
ruleContext.getConfiguration())
.addLinkopts(linkopts)
.setShouldCreateStaticLibraries(shouldCreateStaticLibraries)
.setDynamicLibrary(convertFromNoneable(dynamicLibrary, null))
.setLinkerOutputArtifact(convertFromNoneable(dynamicLibrary, null))
.addCcLinkingInfos(skylarkCcLinkingInfos)
.setNeverLink(neverLink);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,9 @@ public CppLinkActionBuilder addLinkParams(
throws InterruptedException, RuleErrorException {
addLinkopts(linkParams.flattenedLinkopts());
addLibraries(linkParams.getLibraries());
if (linkParams.getNonCodeInputs() != null) {
addNonCodeInputs(linkParams.getNonCodeInputs());
}
ExtraLinkTimeLibraries extraLinkTimeLibraries = linkParams.getExtraLinkTimeLibraries();
if (extraLinkTimeLibraries != null) {
for (ExtraLinkTimeLibrary extraLibrary : extraLinkTimeLibraries.getExtraLibraries()) {
Expand Down

0 comments on commit 4cba428

Please sign in to comment.