From 9cc97513839442ac13fe10ab84e103e127e36987 Mon Sep 17 00:00:00 2001 From: Googler Date: Sat, 21 Oct 2017 04:06:39 +0200 Subject: Add support for linker scripts in auto-linked Android native deps Android's mechanism for automatically linking native deps does not currently support linker scripts. A dependency cc_library can specify the linker script in the linkopts and include it in its deps, but since the artifact is not provided as an input to the generated link action, this results in an error. This change provides the missing inputs and makes this work. RELNOTES: Support for linker scripts in NativeDepsHelper (e.g., android_binary) PiperOrigin-RevId: 172963605 --- .../build/lib/rules/cpp/CcLibraryHelper.java | 1 + .../devtools/build/lib/rules/cpp/CcLinkParams.java | 56 ++++++++++++++++++---- .../lib/rules/nativedeps/NativeDepsHelper.java | 9 +++- 3 files changed, 55 insertions(+), 11 deletions(-) (limited to 'src/main/java/com/google/devtools') diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java index 846e68b19b..6a48bae198 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibraryHelper.java @@ -1563,6 +1563,7 @@ public final class CcLibraryHelper { LinkerInputs.toLibraryArtifacts(ccLinkingOutputs.getExecutionDynamicLibraries())); } builder.addLinkOpts(linkopts); + builder.addNonCodeInputs(nonCodeLinkerInputs); } } }; diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java index b9243fb1cb..731c1f6c99 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLinkParams.java @@ -29,6 +29,7 @@ import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; import com.google.devtools.build.lib.util.Preconditions; import java.util.Collection; import java.util.Objects; +import javax.annotation.Nullable; /** * Parameters to be passed to the linker. @@ -70,22 +71,25 @@ public final class CcLinkParams { private final NestedSet libraries; private final NestedSet executionDynamicLibraries; private final ExtraLinkTimeLibraries extraLinkTimeLibraries; + private final NestedSet nonCodeInputs; private CcLinkParams( NestedSet linkOpts, NestedSet linkstamps, NestedSet libraries, NestedSet executionDynamicLibraries, - ExtraLinkTimeLibraries extraLinkTimeLibraries) { + ExtraLinkTimeLibraries extraLinkTimeLibraries, + NestedSet nonCodeInputs) { this.linkOpts = linkOpts; this.linkstamps = linkstamps; this.libraries = libraries; this.executionDynamicLibraries = executionDynamicLibraries; this.extraLinkTimeLibraries = extraLinkTimeLibraries; + this.nonCodeInputs = nonCodeInputs; } /** - * @return the linkopts + * Returns the linkopts */ public NestedSet getLinkopts() { return linkOpts; @@ -96,20 +100,22 @@ public final class CcLinkParams { } /** - * @return the linkstamps + * Returns the linkstamps */ public NestedSet getLinkstamps() { return linkstamps; } /** - * @return the libraries + * Returns the libraries */ public NestedSet getLibraries() { return libraries; } - /** @return the executionDynamicLibraries */ + /** + * Returns the executionDynamicLibraries. + */ public NestedSet getExecutionDynamicLibraries() { return executionDynamicLibraries; } @@ -117,18 +123,23 @@ public final class CcLinkParams { /** * The extra link time libraries; will be null if there are no such libraries. */ - public ExtraLinkTimeLibraries getExtraLinkTimeLibraries() { + public @Nullable ExtraLinkTimeLibraries getExtraLinkTimeLibraries() { return extraLinkTimeLibraries; } + /** + * Returns the non-code inputs, e.g. linker scripts; will be null if none. + */ + public @Nullable NestedSet getNonCodeInputs() { + return nonCodeInputs; + } + public static final Builder builder(boolean linkingStatically, boolean linkShared) { return new Builder(linkingStatically, linkShared); } /** * Builder for {@link CcLinkParams}. - * - * */ public static final class Builder { @@ -164,6 +175,8 @@ public final class CcLinkParams { */ private ExtraLinkTimeLibraries.Builder extraLinkTimeLibrariesBuilder = null; + private NestedSetBuilder nonCodeInputsBuilder = null; + private boolean built = false; private Builder(boolean linkingStatically, boolean linkShared) { @@ -172,7 +185,7 @@ public final class CcLinkParams { } /** - * Build a {@link CcLinkParams} object. + * Builds a {@link CcLinkParams} object. */ public CcLinkParams build() { Preconditions.checkState(!built); @@ -186,12 +199,17 @@ public final class CcLinkParams { if (extraLinkTimeLibrariesBuilder != null) { extraLinkTimeLibraries = extraLinkTimeLibrariesBuilder.build(); } + NestedSet nonCodeInputs = null; + if (nonCodeInputsBuilder != null) { + nonCodeInputs = nonCodeInputsBuilder.build(); + } return new CcLinkParams( linkOptsBuilder.build(), linkstampsBuilder.build(), librariesBuilder.build(), executionDynamicLibrariesBuilder.build(), - extraLinkTimeLibraries); + extraLinkTimeLibraries, + nonCodeInputs); } public boolean add(CcLinkParamsStore store) { @@ -284,6 +302,12 @@ public final class CcLinkParams { } extraLinkTimeLibrariesBuilder.addTransitive(args.getExtraLinkTimeLibraries()); } + if (args.getNonCodeInputs() != null) { + if (nonCodeInputsBuilder == null) { + nonCodeInputsBuilder = NestedSetBuilder.linkOrder(); + } + nonCodeInputsBuilder.addTransitive(args.getNonCodeInputs()); + } return this; } @@ -339,6 +363,17 @@ public final class CcLinkParams { return this; } + /** + * Adds a collection of non-code inputs. + */ + public Builder addNonCodeInputs(Iterable nonCodeInputs) { + if (nonCodeInputsBuilder == null) { + nonCodeInputsBuilder = NestedSetBuilder.linkOrder(); + } + nonCodeInputsBuilder.addAll(nonCodeInputs); + return this; + } + /** Processes typical dependencies of a C/C++ library. */ public Builder addCcLibrary(RuleContext context) { addTransitiveTargets( @@ -404,5 +439,6 @@ public final class CcLinkParams { NestedSetBuilder.emptySet(Order.COMPILE_ORDER), NestedSetBuilder.emptySet(Order.LINK_ORDER), NestedSetBuilder.emptySet(Order.STABLE_ORDER), + null, null); } 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 2c8cf58886..83f1be8491 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 @@ -232,6 +232,12 @@ public abstract class NativeDepsHelper { ltoBitcodeFilesMap.putAll(lib.getLtoBitcodeFiles()); } } + + Iterable nonCodeInputs = linkParams.getNonCodeInputs(); + if (nonCodeInputs == null) { + nonCodeInputs = ImmutableList.of(); + } + builder .setLinkArtifactFactory(SHAREABLE_LINK_ARTIFACT_FACTORY) .setCrosstoolInputs(toolchain.getLink()) @@ -242,7 +248,8 @@ public abstract class NativeDepsHelper { .addLinkopts(linkopts) .setNativeDeps(true) .addLinkstamps(linkstamps) - .addLtoBitcodeFiles(ltoBitcodeFilesMap.build()); + .addLtoBitcodeFiles(ltoBitcodeFilesMap.build()) + .addNonCodeInputs(nonCodeInputs); if (!builder.getLtoBitcodeFiles().isEmpty() && featureConfiguration.isEnabled(CppRuleClasses.THIN_LTO)) { -- cgit v1.2.3