Skip to content

Commit

Permalink
Allow setting supports_interface_shared_objects crosstool field using…
Browse files Browse the repository at this point in the history
… feature

This cl allows toolchain owners to express that toolchain supports creating interface shared libraries.

This cl is a step towards #5883. Also
see the rollout doc here:
https://docs.google.com/document/d/1uv4c1zag6KvdI31qdx8C6jiTognXPQrxgsUpVefm9fM/edit#.

Flag removing legacy behavior is #6861

RELNOTES: None.
PiperOrigin-RevId: 226302378
  • Loading branch information
hlopko authored and Copybara-Service committed Dec 20, 2018
1 parent 7779617 commit a5ba344
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 56 deletions.
9 changes: 9 additions & 0 deletions site/docs/crosstool-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,13 @@ conditions.
static libraries.
</td>
</tr>
<tr>
<td><strong><code>supports_interface_shared_libraries</code></strong>
</td>
<td>If enabled (and the option <code>--interface_shared_objects</code> is
set), Bazel will link targets that have <code>linkstatic</code> set to
False (<code>cc_test</code>s by default) against interface shared
libraries. This makes incremental relinking faster.
</td>
</tr>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
ruleContext.getConfiguration())
.fromCommon(common)
.addDeps(ImmutableList.of(CppHelper.mallocForTarget(ruleContext)))
.emitInterfaceSharedObjects(true)
.emitInterfaceSharedLibraries(true)
.setAlwayslink(false);
ccLinkingOutputs = linkingHelper.link(ccCompilationOutputs);
}
Expand Down Expand Up @@ -700,10 +700,11 @@ public static ConfiguredTarget init(CppSemantics semantics, RuleContext ruleCont
.setDynamicLinkType(linkType)
.setLinkerOutputArtifact(binary)
.setNeverLink(true)
.emitInterfaceSharedObjects(
.emitInterfaceSharedLibraries(
isLinkShared(ruleContext)
&& featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
&& CppHelper.useInterfaceSharedObjects(cppConfiguration, ccToolchain))
&& CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchain, featureConfiguration))
.setPdbFile(pdbFile)
.setFake(fake);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void init(
ruleContext.getConfiguration())
.fromCommon(common)
.addLinkopts(common.getLinkopts())
.emitInterfaceSharedObjects(true)
.emitInterfaceSharedLibraries(true)
.setAlwayslink(alwaysLink)
.setNeverLink(neverLink)
.addLinkstamps(ruleContext.getPrerequisites("linkstamp", Mode.TARGET));
Expand Down Expand Up @@ -217,7 +217,8 @@ public static void init(
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.NODEPS_DYNAMIC_LIBRARY));
if (CppHelper.useInterfaceSharedObjects(ccToolchain.getCppConfiguration(), ccToolchain)) {
if (CppHelper.useInterfaceSharedLibraries(
ccToolchain.getCppConfiguration(), ccToolchain, featureConfiguration)) {
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
Expand All @@ -241,7 +242,8 @@ public static void init(
ccToolchain,
ruleContext.getConfiguration(),
LinkTargetType.NODEPS_DYNAMIC_LIBRARY));
if (CppHelper.useInterfaceSharedObjects(ccToolchain.getCppConfiguration(), ccToolchain)) {
if (CppHelper.useInterfaceSharedLibraries(
ccToolchain.getCppConfiguration(), ccToolchain, featureConfiguration)) {
dynamicLibraries.add(
CppHelper.getLinkedArtifact(
ruleContext,
Expand Down Expand Up @@ -347,7 +349,11 @@ public static void init(
if (!ccLinkingOutputs.isEmpty()) {
outputGroups.putAll(
addLinkerOutputArtifacts(
ruleContext, ccToolchain, ruleContext.getConfiguration(), ccCompilationOutputs));
ruleContext,
ccToolchain,
ruleContext.getConfiguration(),
ccCompilationOutputs,
featureConfiguration));
}
CcLinkingOutputs ccLinkingOutputsWithPrecompiledLibraries =
addPrecompiledLibrariesToLinkingOutputs(
Expand Down Expand Up @@ -558,7 +564,8 @@ private static Map<String, NestedSet<Artifact>> addLinkerOutputArtifacts(
RuleContext ruleContext,
CcToolchainProvider ccToolchain,
BuildConfiguration configuration,
CcCompilationOutputs ccCompilationOutputs)
CcCompilationOutputs ccCompilationOutputs,
FeatureConfiguration featureConfiguration)
throws RuleErrorException {

NestedSetBuilder<Artifact> archiveFile = new NestedSetBuilder<>(Order.STABLE_ORDER);
Expand Down Expand Up @@ -599,7 +606,8 @@ private static Map<String, NestedSet<Artifact>> addLinkerOutputArtifacts(
Link.LinkTargetType.NODEPS_DYNAMIC_LIBRARY,
/* linkedArtifactNameSuffix= */ ""));

if (CppHelper.useInterfaceSharedObjects(ccToolchain.getCppConfiguration(), ccToolchain)) {
if (CppHelper.useInterfaceSharedLibraries(
ccToolchain.getCppConfiguration(), ccToolchain, featureConfiguration)) {
dynamicLibrary.add(
CppHelper.getLinkedArtifact(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public CcLinkingOutputs getCcLinkingOutputs() {
private boolean neverlink;

private boolean checkDepsGenerateCpp = true;
private boolean emitInterfaceSharedObjects;
private boolean emitInterfaceSharedLibraries;
private boolean shouldCreateDynamicLibrary = true;
private boolean shouldCreateStaticLibraries = true;
private boolean willOnlyBeLinkedIntoDynamicLibraries;
Expand Down Expand Up @@ -307,8 +307,8 @@ public CcLinkingHelper setLinkedArtifactNameSuffix(String suffix) {
* linker generates a dynamic library, and only if the crosstool supports it. The default is not
* to generate interface dynamic libraries.
*/
public CcLinkingHelper emitInterfaceSharedObjects(boolean emitInterfaceSharedObjects) {
this.emitInterfaceSharedObjects = emitInterfaceSharedObjects;
public CcLinkingHelper emitInterfaceSharedLibraries(boolean emitInterfaceSharedLibraries) {
this.emitInterfaceSharedLibraries = emitInterfaceSharedLibraries;
return this;
}

Expand Down Expand Up @@ -394,7 +394,7 @@ public CcLinkingInfo buildCcLinkingInfoFromLibraryToLinkWrappers(
*
* <p>For dynamic libraries, this method can additionally create an interface shared library that
* can be used for linking, but doesn't contain any executable code. This increases the number of
* cache hits for link actions. Call {@link #emitInterfaceSharedObjects(boolean)} to enable this
* cache hits for link actions. Call {@link #emitInterfaceSharedLibraries(boolean)} to enable this
* behavior.
*
* @throws RuleErrorException
Expand Down Expand Up @@ -603,8 +603,8 @@ private void createDynamicLibrary(

List<String> sonameLinkopts = ImmutableList.of();
Artifact soInterface = null;
if (CppHelper.useInterfaceSharedObjects(cppConfiguration, ccToolchain)
&& emitInterfaceSharedObjects) {
if (CppHelper.useInterfaceSharedLibraries(cppConfiguration, ccToolchain, featureConfiguration)
&& emitInterfaceSharedLibraries) {
soInterface = getLinkedArtifact(LinkTargetType.INTERFACE_DYNAMIC_LIBRARY);
// TODO(b/28946988): Remove this hard-coded flag.
if (!featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,8 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(

boolean supportsGoldLinker = featureNames.contains("supports_gold_linker");
boolean supportsStartEndLib = featureNames.contains("supports_start_end_lib");
boolean supportsInterfaceSharedObjects =
featureNames.contains("supports_interface_shared_objects");
boolean supportsInterfaceSharedLibraries =
featureNames.contains("supports_interface_shared_libraries");
boolean supportsEmbeddedRuntimes = featureNames.contains("supports_embedded_runtimes");
boolean supportsFission = featureNames.contains("supports_fission");
boolean dynamicLinkingMode = featureNames.contains("dynamic_linking_mode");
Expand Down Expand Up @@ -1175,7 +1175,7 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
// This should be toolchain-based, rather than feature based, because
// it controls whether or not to declare the feature at all.
supportsEmbeddedRuntimes,
supportsInterfaceSharedObjects)) {
supportsInterfaceSharedLibraries)) {
legacyFeaturesBuilder.add(new Feature(feature));
}
legacyFeaturesBuilder.addAll(
Expand Down Expand Up @@ -1244,7 +1244,7 @@ public CcToolchainConfigInfo ccToolchainConfigInfoFromSkylark(
abiLibcVersion,
supportsGoldLinker,
supportsStartEndLib,
supportsInterfaceSharedObjects,
supportsInterfaceSharedLibraries,
supportsEmbeddedRuntimes,
/* staticRuntimesFilegroup= */ "",
/* dynamicRuntimesFilegroup= */ "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class CcToolchainConfigInfo extends NativeInfo implements CcToolchainConf
private final String abiLibcVersion;
private final boolean supportsGoldLinker;
private final boolean supportsStartEndLib;
private final boolean supportsInterfaceSharedObjects;
private final boolean supportsInterfaceSharedLibraries;
private final boolean supportsEmbeddedRuntimes;
private final String staticRuntimesFilegroup;
private final String dynamicRuntimesFilegroup;
Expand Down Expand Up @@ -109,7 +109,7 @@ public CcToolchainConfigInfo(
String abiLibcVersion,
boolean supportsGoldLinker,
boolean supportsStartEndLib,
boolean supportsInterfaceSharedObjects,
boolean supportsInterfaceSharedLibraries,
boolean supportsEmbeddedRuntimes,
String staticRuntimesFilegroup,
String dynamicRuntimesFilegroup,
Expand Down Expand Up @@ -153,7 +153,7 @@ public CcToolchainConfigInfo(
this.abiLibcVersion = abiLibcVersion;
this.supportsGoldLinker = supportsGoldLinker;
this.supportsStartEndLib = supportsStartEndLib;
this.supportsInterfaceSharedObjects = supportsInterfaceSharedObjects;
this.supportsInterfaceSharedLibraries = supportsInterfaceSharedLibraries;
this.supportsEmbeddedRuntimes = supportsEmbeddedRuntimes;
this.staticRuntimesFilegroup = staticRuntimesFilegroup;
this.dynamicRuntimesFilegroup = dynamicRuntimesFilegroup;
Expand Down Expand Up @@ -407,8 +407,8 @@ public boolean supportsStartEndLib() {

// TODO(b/65151735): Remove once this field is migrated to features.
@Deprecated
public boolean supportsInterfaceSharedObjects() {
return supportsInterfaceSharedObjects;
public boolean supportsInterfaceSharedLibraries() {
return supportsInterfaceSharedLibraries;
}

// TODO(b/65151735): Remove once this field is migrated to features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,10 @@ public boolean supportsStartEndLib(FeatureConfiguration featureConfiguration) {
|| featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_START_END_LIB);
}

/**
* Returns whether this toolchain supports interface shared objects.
*
* <p>Should be true if this toolchain generates ELF objects.
*/
public boolean supportsInterfaceSharedObjects() {
return toolchainInfo.supportsInterfaceSharedObjects();
/** Returns whether this toolchain supports interface shared libraries. */
public boolean supportsInterfaceSharedLibraries(FeatureConfiguration featureConfiguration) {
return toolchainInfo.supportsInterfaceSharedLibraries()
|| featureConfiguration.isEnabled(CppRuleClasses.SUPPORTS_INTERFACE_SHARED_LIBRARIES);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ static CcToolchainProvider getCcToolchainProvider(
attributes.getAdditionalBuildVariables()),
getBuiltinIncludes(attributes.getLibc()),
coverageEnvironment.build(),
toolchainInfo.supportsInterfaceSharedObjects()
toolchainInfo.supportsInterfaceSharedLibraries()
? attributes.getLinkDynamicLibraryTool()
: null,
builtInIncludeDirectories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public boolean getParseHeadersVerifiesModules() {
return cppOptions.parseHeadersVerifiesModules;
}

public boolean getUseInterfaceSharedObjects() {
public boolean getUseInterfaceSharedLibraries() {
return cppOptions.useInterfaceSharedObjects;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,12 @@ public static Link.ArchiveType getArchiveType(
* Returns true if interface shared objects should be used in the build implied by the given
* cppConfiguration and toolchain.
*/
public static boolean useInterfaceSharedObjects(
CppConfiguration config, CcToolchainProvider toolchain) {
return toolchain.supportsInterfaceSharedObjects() && config.getUseInterfaceSharedObjects();
public static boolean useInterfaceSharedLibraries(
CppConfiguration cppConfiguration,
CcToolchainProvider toolchain,
FeatureConfiguration featureConfiguration) {
return toolchain.supportsInterfaceSharedLibraries(featureConfiguration)
&& cppConfiguration.getUseInterfaceSharedLibraries();
}

public static CcNativeLibraryProvider collectNativeCcLibraries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ private ImmutableSet<Artifact> getArtifactsPossiblyLtoMapped(

private boolean shouldUseLinkDynamicLibraryTool() {
return linkType.isDynamicLibrary()
&& toolchain.supportsInterfaceSharedObjects()
&& toolchain.supportsInterfaceSharedLibraries(featureConfiguration)
&& !featureConfiguration.hasConfiguredLinkerPathInActionConfig();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ public static Label ccToolchainTypeAttribute(RuleDefinitionEnvironment env) {
*/
public static final String TARGETS_WINDOWS = "targets_windows";

/**
* A string constant for a feature that indicates we are using a toolchain building for Windows.
*/
public static final String SUPPORTS_INTERFACE_SHARED_LIBRARIES =
"supports_interface_shared_libraries";

/**
* A string constant for no_stripping feature, if it's specified, then no strip action config is
* needed, instead the stripped binary will simply be a symlink (or a copy on Windows) of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public final class CppToolchainInfo {
private final boolean supportsStartEndLib;
private final boolean supportsEmbeddedRuntimes;
private final boolean supportsDynamicLinker;
private final boolean supportsInterfaceSharedObjects;
private final boolean supportsInterfaceSharedLibraries;
private final boolean supportsGoldLinker;
private final boolean toolchainNeedsPic;

Expand Down Expand Up @@ -244,7 +244,7 @@ public static CppToolchainInfo create(
ccToolchainConfigInfo.supportsStartEndLib(),
ccToolchainConfigInfo.supportsEmbeddedRuntimes(),
haveDynamicMode || !ccToolchainConfigInfo.getDynamicLibraryLinkerFlags().isEmpty(),
ccToolchainConfigInfo.supportsInterfaceSharedObjects(),
ccToolchainConfigInfo.supportsInterfaceSharedLibraries(),
ccToolchainConfigInfo.supportsGoldLinker(),
ccToolchainConfigInfo.needsPic());
} catch (LabelSyntaxException e) {
Expand Down Expand Up @@ -292,7 +292,7 @@ public static CppToolchainInfo create(
boolean supportsStartEndLib,
boolean supportsEmbeddedRuntimes,
boolean supportsDynamicLinker,
boolean supportsInterfaceSharedObjects,
boolean supportsInterfaceSharedLibraries,
boolean supportsGoldLinker,
boolean toolchainNeedsPic)
throws EvalException {
Expand Down Expand Up @@ -337,7 +337,7 @@ public static CppToolchainInfo create(
|| toolchainFeatures
.getActivatableNames()
.contains(CppRuleClasses.DYNAMIC_LINKING_MODE);
this.supportsInterfaceSharedObjects = supportsInterfaceSharedObjects;
this.supportsInterfaceSharedLibraries = supportsInterfaceSharedLibraries;
this.supportsGoldLinker = supportsGoldLinker;
this.toolchainNeedsPic = toolchainNeedsPic;
}
Expand Down Expand Up @@ -600,8 +600,8 @@ public boolean supportsDynamicLinker() {
*
* <p>Should be true if this toolchain generates ELF objects.
*/
public boolean supportsInterfaceSharedObjects() {
return supportsInterfaceSharedObjects;
public boolean supportsInterfaceSharedLibraries() {
return supportsInterfaceSharedLibraries;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private void addDynamicInputLinkOptions(
input,
CppHelper.getArchiveType(cppConfiguration, ccToolchainProvider, featureConfiguration)));
if (featureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
&& ccToolchainProvider.supportsInterfaceSharedObjects()) {
&& ccToolchainProvider.supportsInterfaceSharedLibraries(featureConfiguration)) {
// On Windows, dynamic library (dll) cannot be linked directly when using toolchains that
// support interface library (eg. MSVC).
Preconditions.checkState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ private static class Impl {
depsBuilder.addAll(ruleContext.getPrerequisites("deps", TARGET));
ImmutableList<TransitiveInfoCollection> deps = depsBuilder.build();
CcLinkingHelper ccLinkingHelper = initializeLinkingHelper(featureConfiguration, deps);
if (ccToolchain(ruleContext).supportsInterfaceSharedObjects()) {
ccLinkingHelper.emitInterfaceSharedObjects(true);
if (ccToolchain(ruleContext).supportsInterfaceSharedLibraries(featureConfiguration)) {
ccLinkingHelper.emitInterfaceSharedLibraries(true);
}
CcLinkingOutputs ccLinkingOutputs = CcLinkingOutputs.EMPTY;
ImmutableList.Builder<LibraryToLinkWrapper> libraryToLinkWrapperBuilder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,21 @@ public void testInterfaceSharedObjects() throws Exception {
CcToolchainProvider toolchainProvider =
(CcToolchainProvider) target.get(ToolchainInfo.PROVIDER);
assertThat(
CppHelper.useInterfaceSharedObjects(
getConfiguration(target).getFragment(CppConfiguration.class), toolchainProvider))
CppHelper.useInterfaceSharedLibraries(
getConfiguration(target).getFragment(CppConfiguration.class),
toolchainProvider,
FeatureConfiguration.EMPTY))
.isFalse();

useConfiguration("--interface_shared_objects");
invalidatePackages();
target = getConfiguredTarget("//a:b");
toolchainProvider = (CcToolchainProvider) target.get(ToolchainInfo.PROVIDER);
assertThat(
CppHelper.useInterfaceSharedObjects(
getConfiguration(target).getFragment(CppConfiguration.class), toolchainProvider))
CppHelper.useInterfaceSharedLibraries(
getConfiguration(target).getFragment(CppConfiguration.class),
toolchainProvider,
FeatureConfiguration.EMPTY))
.isFalse();

getAnalysisMock()
Expand All @@ -100,17 +104,21 @@ public void testInterfaceSharedObjects() throws Exception {
target = getConfiguredTarget("//a:b");
toolchainProvider = (CcToolchainProvider) target.get(ToolchainInfo.PROVIDER);
assertThat(
CppHelper.useInterfaceSharedObjects(
getConfiguration(target).getFragment(CppConfiguration.class), toolchainProvider))
CppHelper.useInterfaceSharedLibraries(
getConfiguration(target).getFragment(CppConfiguration.class),
toolchainProvider,
FeatureConfiguration.EMPTY))
.isTrue();

useConfiguration("--nointerface_shared_objects");
invalidatePackages();
target = getConfiguredTarget("//a:b");
toolchainProvider = (CcToolchainProvider) target.get(ToolchainInfo.PROVIDER);
assertThat(
CppHelper.useInterfaceSharedObjects(
getConfiguration(target).getFragment(CppConfiguration.class), toolchainProvider))
CppHelper.useInterfaceSharedLibraries(
getConfiguration(target).getFragment(CppConfiguration.class),
toolchainProvider,
FeatureConfiguration.EMPTY))
.isFalse();
}

Expand Down
Loading

0 comments on commit a5ba344

Please sign in to comment.