Skip to content

Commit

Permalink
Cherrypicks for experimental cc_shared_library (bazelbuild#14773)
Browse files Browse the repository at this point in the history
* Cherrypicks for experimental cc_shared_library

* Adds cc_shared_library builtin test sources to //:srcs
  • Loading branch information
oquenchil committed Feb 10, 2022
1 parent 58ecec3 commit 5356fed
Show file tree
Hide file tree
Showing 60 changed files with 1,940 additions and 79 deletions.
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tasks:
test_targets:
- "//scripts/..."
- "//src/java_tools/..."
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/..."
- "//src/tools/execlog/..."
- "//src/tools/singlejar/..."
Expand Down Expand Up @@ -68,6 +69,7 @@ tasks:
test_targets:
- "//scripts/..."
- "//src/java_tools/..."
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/..."
- "//src/tools/execlog/..."
- "//src/tools/singlejar/..."
Expand Down Expand Up @@ -130,6 +132,7 @@ tasks:
test_targets:
- "//scripts/..."
- "//src/java_tools/..."
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/..."
- "//src/tools/execlog/..."
- "//src/tools/singlejar/..."
Expand Down Expand Up @@ -166,6 +169,7 @@ tasks:
- "--test_env=REMOTE_NETWORK_ADDRESS=bazel.build:80"
test_targets:
- "//scripts/..."
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/..."
- "//src/tools/execlog/..."
- "//src/tools/singlejar/..."
Expand Down Expand Up @@ -203,6 +207,7 @@ tasks:
- "--test_env=TEST_REPOSITORY_HOME=$OUTPUT_BASE/external"
test_targets:
- "//src:embedded_tools_size_test"
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/cpp/..."
- "//src/test/java/com/google/devtools/build/android/..."
- "//src/test/java/com/google/devtools/build/lib/..."
Expand Down Expand Up @@ -269,6 +274,7 @@ tasks:
test_targets:
- "//scripts/..."
- "//src/java_tools/..."
- "//src/main/starlark/tests/builtins_bzl/..."
- "//src/test/..."
- "//src/tools/execlog/..."
- "//src/tools/singlejar/..."
Expand Down
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ filegroup(
"//src:srcs",
"//tools:srcs",
"//third_party:srcs",
"//src/main/starlark/tests/builtins_bzl:srcs",
] + glob([".bazelci/*"]) + [".bazelrc"],
visibility = ["//src/test/shell/bazel:__pkg__"],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
attr("implementation_deps", LABEL_LIST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.mandatoryProviders(CcInfo.PROVIDER.id()))
.advertiseStarlarkProvider(CcInfo.PROVIDER.id())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public CcLinkingOutputs link(
Object wholeArchive,
Object additionalLinkstampDefines,
Object onlyForDynamicLibs,
Object mainOutput,
Object linkerOutputs,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException {
return super.link(
Expand All @@ -208,7 +210,9 @@ public CcLinkingOutputs link(
wholeArchive,
additionalLinkstampDefines,
onlyForDynamicLibs,
mainOutput,
linkerOutputs,
winDefFile,
thread);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ private static RuntimeFiles collectRunfiles(
.getAllArtifacts()
.toList());
}
for (TransitiveInfoCollection transitiveInfoCollection :
ruleContext.getPrerequisites("dynamic_deps")) {
builder.merge(
transitiveInfoCollection.getProvider(RunfilesProvider.class).getDefaultRunfiles());
}
// Add the C++ runtime libraries if linking them dynamically.
if (linkingMode == Link.LinkingMode.DYNAMIC) {
try {
Expand Down Expand Up @@ -557,6 +562,7 @@ public static void init(
Pair<CcLinkingOutputs, CcLauncherInfo> ccLinkingOutputsAndCcLinkingInfo =
createTransitiveLinkingActions(
ruleContext,
ruleBuilder,
ccToolchain,
featureConfiguration,
fdoContext,
Expand Down Expand Up @@ -741,8 +747,9 @@ public static void init(
.addNativeDeclaredProvider(ccLauncherInfo);
}

public static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActions(
private static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActions(
RuleContext ruleContext,
RuleConfiguredTargetBuilder ruleBuilder,
CcToolchainProvider ccToolchain,
FeatureConfiguration featureConfiguration,
FdoContext fdoContext,
Expand Down Expand Up @@ -869,7 +876,7 @@ public static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActi
CcLinkingContext ccLinkingContext =
ruleContext.attributes().isAttributeValueExplicitlySpecified("dynamic_deps")
? filterLibrariesThatAreLinkedDynamically(
ruleContext, ccInfo.getCcLinkingContext(), cppSemantics)
ruleContext, ruleBuilder, ccInfo.getCcLinkingContext(), cppSemantics)
: ccInfo.getCcLinkingContext();
if (ruleContext.hasErrors()) {
return null;
Expand Down Expand Up @@ -1426,8 +1433,11 @@ private static ImmutableList<CcLinkingContext.LinkerInput> filterInputs(
graphStructureAspectNodes.add(nodeInfo);
}
}
graphStructureAspectNodes.add(
CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class));
GraphNodeInfo mallocNodeInfo =
CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class);
if (mallocNodeInfo != null) {
graphStructureAspectNodes.add(mallocNodeInfo);
}

Set<String> canBeLinkedDynamically = new HashSet<>();
for (CcLinkingContext.LinkerInput linkerInput : linkerInputs) {
Expand Down Expand Up @@ -1533,7 +1543,8 @@ public static ImmutableMap<String, String> buildLinkOnceStaticLibsMap(
}

private static CcLinkingContext filterLibrariesThatAreLinkedDynamically(
RuleContext ruleContext, CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) {
RuleContext ruleContext, RuleConfiguredTargetBuilder ruleBuilder,
CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) {
ImmutableList<CcSharedLibraryInfo> mergedCcSharedLibraryInfos =
mergeCcSharedLibraryInfos(ruleContext, cppSemantics);
ImmutableList<CcLinkingContext.LinkerInput> preloadedDeps =
Expand All @@ -1548,6 +1559,37 @@ private static CcLinkingContext filterLibrariesThatAreLinkedDynamically(
buildExportsMapFromOnlyDynamicDeps(ruleContext, mergedCcSharedLibraryInfos);
ImmutableList<CcLinkingContext.LinkerInput> staticLinkerInputs =
filterInputs(ruleContext, ccLinkingContext, exportsMap, linkOnceStaticLibsMap);
if (ruleContext
.getConfiguration()
.getFragment(CppConfiguration.class)
.experimentalCcSharedLibraryDebug()) {
ImmutableList.Builder<String> debugLinkerInputsFile = ImmutableList.builder();
debugLinkerInputsFile.add("Owner: " + ruleContext.getLabel());
for (CcLinkingContext.LinkerInput linkerInput :
Iterables.concat(staticLinkerInputs, preloadedDeps)) {
debugLinkerInputsFile.add(linkerInput.getOwner().toString());
}
Artifact linkOnceStaticLibsDebugFile =
ruleContext.getBinArtifact(
ruleContext.getLabel().getName() + "_link_once_static_libs.txt");
ruleContext.registerAction(
FileWriteAction.create(
ruleContext,
linkOnceStaticLibsDebugFile,
Joiner.on("\n").join(debugLinkerInputsFile.build()),
false));
NestedSetBuilder<Artifact> transitiveDebugFiles = NestedSetBuilder.stableOrder();
for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("dynamic_deps")) {
transitiveDebugFiles.addTransitive(
dep.get(OutputGroupInfo.STARLARK_CONSTRUCTOR).getOutputGroup("rule_impl_debug_files"));
}
ruleBuilder.addOutputGroup(
"rule_impl_debug_files",
NestedSetBuilder.<Artifact>stableOrder()
.add(linkOnceStaticLibsDebugFile)
.addTransitive(transitiveDebugFiles.build())
.build());
}

return createLinkingContextWithDynamicDependencies(
staticLinkerInputs, preloadedDeps, exportsMap.values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2300,19 +2300,26 @@ protected CcLinkingOutputs link(
Object wholeArchiveObject,
Object additionalLinkstampDefines,
Object onlyForDynamicLibsObject,
Object mainOutputObject,
Object linkerOutputsObject,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException {
// TODO(bazel-team): Rename always_link to alwayslink before delisting. Also it looks like the
// suffix parameter can be removed since we can use `name` for the same thing.
if (checkObjectsBound(
// TODO(b/205690414): Keep linkedArtifactNameSuffixObject protected. Use cases that are
// passing the suffix should be migrated to using mainOutput instead where the suffix is
// taken into account. Then this parameter should be removed.
linkedArtifactNameSuffixObject,
neverLinkObject,
alwaysLinkObject,
testOnlyTargetObject,
nativeDepsObject,
wholeArchiveObject,
additionalLinkstampDefines,
mainOutputObject,
winDefFile,
onlyForDynamicLibsObject)) {
checkPrivateStarlarkificationAllowlist(thread);
}
Expand All @@ -2327,6 +2334,7 @@ protected CcLinkingOutputs link(
convertFromNoneable(starlarkCcToolchainProvider, null);
FeatureConfigurationForStarlark featureConfiguration =
convertFromNoneable(starlarkFeatureConfiguration, null);
Artifact mainOutput = convertFromNoneable(mainOutputObject, null);
Label label = getCallerLabel(actions, name);
FdoContext fdoContext = ccToolchainProvider.getFdoContext();
LinkTargetType dynamicLinkTargetType = null;
Expand Down Expand Up @@ -2406,6 +2414,8 @@ protected CcLinkingOutputs link(
&& actualFeatureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
&& CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchainProvider, actualFeatureConfiguration))
.setDefFile(convertFromNoneable(winDefFile, null))
.setLinkerOutputArtifact(convertFromNoneable(mainOutput, null))
.addLinkerOutputs(linkerOutputs);
if (staticLinkTargetType != null) {
helper.setShouldCreateDynamicLibrary(false).setStaticLinkType(staticLinkTargetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public interface BazelCcModuleApi<
name = "name",
doc =
"This is used for naming the output artifacts of actions created by this "
+ "method.",
+ "method. See also the `main_output` arg.",
positional = false,
named = true),
@Param(
Expand Down Expand Up @@ -546,13 +546,34 @@ Tuple compile(
documented = false,
allowedTypes = {@ParamType(type = Boolean.class)},
defaultValue = "unbound"),
@Param(
name = "main_output",
doc =
"Name of the main output artifact that will be produced by the linker. "
+ "Only set this if the default name generation does not match you needs "
+ "For output_type=executable, this is the final executable filename. "
+ "For output_type=dynamic_library, this is the shared library filename. "
+ "If not specified, then one will be computed based on `name` and "
+ "`output_type`",
positional = false,
named = true,
documented = false,
defaultValue = "unbound",
allowedTypes = {@ParamType(type = FileApi.class), @ParamType(type = NoneType.class)}),
@Param(
name = "additional_outputs",
doc = "For additional outputs to the linking action, e.g.: map files.",
positional = false,
named = true,
allowedTypes = {@ParamType(type = Sequence.class)},
defaultValue = "unbound")
defaultValue = "unbound"),
@Param(
name = "win_def_file",
documented = false,
positional = false,
named = true,
defaultValue = "unbound"),

})
LinkingOutputsT link(
StarlarkActionFactoryT starlarkActionFactoryApi,
Expand All @@ -577,7 +598,9 @@ LinkingOutputsT link(
Object wholeArchive,
Object additionalLinkstampDefines,
Object onlyForDynamicLibs,
Object mainOutput,
Object linkerOutputs,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException;

Expand Down
Loading

0 comments on commit 5356fed

Please sign in to comment.