aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2015-09-03 14:04:19 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2015-09-03 14:42:52 +0000
commitd0496cc5f64a06e8d9456d66d5595369616a6b68 (patch)
tree6539c9a1b4f2be78535a659a7590453dd23b583e /src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
parent54733f93471af0cbe63e9f32a522971dbd193a3e (diff)
Link Android native code with libstdc++, statically.
- Refactor the ndk toolchain generation to properly set the dynamic and static runtime lib filegroups. - Enable the runtime filegroups. - Change the NativeDepsHelper to work as documented - mostly static means statically linking the runtime filegroups; but only for Android to remain backwards compatible with other users of NativeDepsHelper. - This is safe as we don't use runtime filegroups internally for Android... yet. - Clean up the NativeDepsHelper a bit. Fixes #392. Next steps are to make this configurable at the android_binary level. We should also disable whole-archive in this case. -- Change-Id: I95b0ce45d8b3adcf5424544c92ec30102b7fac6b Reviewed-on: https://bazel-review.googlesource.com/1879 MOS_MIGRATED_REVID=102239337
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java102
1 files changed, 67 insertions, 35 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java b/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
index 5cd41f3913..496ada0f09 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/nativedeps/NativeDepsHelper.java
@@ -115,9 +115,15 @@ public abstract class NativeDepsHelper {
Artifact nativeDeps = ruleContext.getPackageRelativeArtifact(nativeDepsPath,
configuration.getBinDirectory());
- return createNativeDepsAction(ruleContext, linkParams, extraLinkOpts, configuration,
- CppHelper.getToolchain(ruleContext), nativeDeps,
- ruleContext.getConfiguration().getBinDirectory());
+ return createNativeDepsAction(
+ ruleContext,
+ linkParams,
+ extraLinkOpts,
+ configuration,
+ CppHelper.getToolchain(ruleContext),
+ nativeDeps,
+ ruleContext.getConfiguration().getBinDirectory(), /*useDynamicRuntime*/
+ true);
}
private static final String ANDROID_UNIQUE_DIR = "nativedeps";
@@ -153,15 +159,30 @@ public abstract class NativeDepsHelper {
configuration.getBinDirectory());
return createNativeDepsAction(
- ruleContext, linkParams, /** extraLinkOpts */ImmutableList.<String>of(),
- configuration, toolchain, nativeDeps, configuration.getBinDirectory()).getLibrary();
+ ruleContext,
+ linkParams, /** extraLinkOpts */
+ ImmutableList.<String>of(),
+ configuration,
+ toolchain,
+ nativeDeps,
+ configuration.getBinDirectory(),
+ /*useDynamicRuntime*/ false)
+ .getLibrary();
}
- private static NativeDepsRunfiles createNativeDepsAction(final RuleContext ruleContext,
- CcLinkParams linkParams, Collection<String> extraLinkOpts, BuildConfiguration configuration,
- CcToolchainProvider toolchain, Artifact nativeDeps, Root bindirIfShared) {
- Preconditions.checkState(ruleContext.isLegalFragment(CppConfiguration.class),
- "%s does not have access to CppConfiguration", ruleContext.getRule().getRuleClass());
+ private static NativeDepsRunfiles createNativeDepsAction(
+ final RuleContext ruleContext,
+ CcLinkParams linkParams,
+ Collection<String> extraLinkOpts,
+ BuildConfiguration configuration,
+ CcToolchainProvider toolchain,
+ Artifact nativeDeps,
+ Root bindirIfShared,
+ boolean useDynamicRuntime) {
+ Preconditions.checkState(
+ ruleContext.isLegalFragment(CppConfiguration.class),
+ "%s does not have access to CppConfiguration",
+ ruleContext.getRule().getRuleClass());
List<String> linkopts = new ArrayList<>(extraLinkOpts);
linkopts.addAll(linkParams.flattenedLinkopts());
@@ -182,38 +203,49 @@ public abstract class NativeDepsHelper {
: nativeDeps;
CppLinkAction.Builder builder = new CppLinkAction.Builder(
ruleContext, sharedLibrary, configuration, toolchain);
- builder.setLinkArtifactFactory(SHAREABLE_LINK_ARTIFACT_FACTORY);
- CppLinkAction linkAction = builder
- .setCrosstoolInputs(toolchain.getLink())
- .addLibraries(linkerInputs)
- .setLinkType(LinkTargetType.DYNAMIC_LIBRARY)
- .setLinkStaticness(LinkStaticness.MOSTLY_STATIC)
- .addLinkopts(linkopts)
- .setNativeDeps(true)
- .setRuntimeInputs(
- toolchain.getDynamicRuntimeLinkMiddleman(), toolchain.getDynamicRuntimeLinkInputs())
- .addLinkstamps(linkstamps)
- .build();
+ if (useDynamicRuntime) {
+ builder.setRuntimeInputs(
+ toolchain.getDynamicRuntimeLinkMiddleman(), toolchain.getDynamicRuntimeLinkInputs());
+ } else {
+ builder.setRuntimeInputs(
+ toolchain.getStaticRuntimeLinkMiddleman(), toolchain.getStaticRuntimeLinkInputs());
+ }
+ CppLinkAction linkAction =
+ builder
+ .setLinkArtifactFactory(SHAREABLE_LINK_ARTIFACT_FACTORY)
+ .setCrosstoolInputs(toolchain.getLink())
+ .addLibraries(linkerInputs)
+ .setLinkType(LinkTargetType.DYNAMIC_LIBRARY)
+ .setLinkStaticness(LinkStaticness.MOSTLY_STATIC)
+ .addLinkopts(linkopts)
+ .setNativeDeps(true)
+ .addLinkstamps(linkstamps)
+ .build();
ruleContext.registerAction(linkAction);
- final Artifact linkerOutput = linkAction.getPrimaryOutput();
-
- List<Artifact> runtimeSymlinks = new LinkedList<>();
+ Artifact linkerOutput = linkAction.getPrimaryOutput();
if (shareNativeDeps) {
// Collect dynamic-linker-resolvable symlinks for C++ runtime library dependencies.
// Note we only need these symlinks when --share_native_deps is on, as shared native deps
// mangle path names such that the library's conventional _solib RPATH entry
// no longer resolves (because the target directory's relative depth gets lost).
- for (final Artifact runtimeInput : toolchain.getDynamicRuntimeLinkInputs()) {
- final Artifact runtimeSymlink = ruleContext.getPackageRelativeArtifact(
- getRuntimeLibraryPath(ruleContext, runtimeInput), bindirIfShared);
- // Since runtime library symlinks are underneath the target's output directory and
- // multiple targets may share the same output directory, we need to make sure this
- // symlink's generating action is only set once.
- ruleContext.registerAction(
- new SymlinkAction(ruleContext.getActionOwner(), runtimeInput, runtimeSymlink, null));
- runtimeSymlinks.add(runtimeSymlink);
+ List<Artifact> runtimeSymlinks;
+ if (useDynamicRuntime) {
+ runtimeSymlinks = new LinkedList<>();
+ for (final Artifact runtimeInput : toolchain.getDynamicRuntimeLinkInputs()) {
+ final Artifact runtimeSymlink =
+ ruleContext.getPackageRelativeArtifact(
+ getRuntimeLibraryPath(ruleContext, runtimeInput), bindirIfShared);
+ // Since runtime library symlinks are underneath the target's output directory and
+ // multiple targets may share the same output directory, we need to make sure this
+ // symlink's generating action is only set once.
+ ruleContext.registerAction(
+ new SymlinkAction(ruleContext.getActionOwner(), runtimeInput, runtimeSymlink, null));
+ runtimeSymlinks.add(runtimeSymlink);
+ }
+ } else {
+ runtimeSymlinks = ImmutableList.of();
}
ruleContext.registerAction(
@@ -221,7 +253,7 @@ public abstract class NativeDepsHelper {
return new NativeDepsRunfiles(nativeDeps, runtimeSymlinks);
}
- return new NativeDepsRunfiles(linkerOutput, runtimeSymlinks);
+ return new NativeDepsRunfiles(linkerOutput, ImmutableList.<Artifact>of());
}
/**