Skip to content

Commit 5356fed

Browse files
authored
Cherrypicks for experimental cc_shared_library (bazelbuild#14773)
* Cherrypicks for experimental cc_shared_library * Adds cc_shared_library builtin test sources to //:srcs
1 parent 58ecec3 commit 5356fed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1940
-79
lines changed

.bazelci/presubmit.yml

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tasks:
2626
test_targets:
2727
- "//scripts/..."
2828
- "//src/java_tools/..."
29+
- "//src/main/starlark/tests/builtins_bzl/..."
2930
- "//src/test/..."
3031
- "//src/tools/execlog/..."
3132
- "//src/tools/singlejar/..."
@@ -68,6 +69,7 @@ tasks:
6869
test_targets:
6970
- "//scripts/..."
7071
- "//src/java_tools/..."
72+
- "//src/main/starlark/tests/builtins_bzl/..."
7173
- "//src/test/..."
7274
- "//src/tools/execlog/..."
7375
- "//src/tools/singlejar/..."
@@ -130,6 +132,7 @@ tasks:
130132
test_targets:
131133
- "//scripts/..."
132134
- "//src/java_tools/..."
135+
- "//src/main/starlark/tests/builtins_bzl/..."
133136
- "//src/test/..."
134137
- "//src/tools/execlog/..."
135138
- "//src/tools/singlejar/..."
@@ -166,6 +169,7 @@ tasks:
166169
- "--test_env=REMOTE_NETWORK_ADDRESS=bazel.build:80"
167170
test_targets:
168171
- "//scripts/..."
172+
- "//src/main/starlark/tests/builtins_bzl/..."
169173
- "//src/test/..."
170174
- "//src/tools/execlog/..."
171175
- "//src/tools/singlejar/..."
@@ -203,6 +207,7 @@ tasks:
203207
- "--test_env=TEST_REPOSITORY_HOME=$OUTPUT_BASE/external"
204208
test_targets:
205209
- "//src:embedded_tools_size_test"
210+
- "//src/main/starlark/tests/builtins_bzl/..."
206211
- "//src/test/cpp/..."
207212
- "//src/test/java/com/google/devtools/build/android/..."
208213
- "//src/test/java/com/google/devtools/build/lib/..."
@@ -269,6 +274,7 @@ tasks:
269274
test_targets:
270275
- "//scripts/..."
271276
- "//src/java_tools/..."
277+
- "//src/main/starlark/tests/builtins_bzl/..."
272278
- "//src/test/..."
273279
- "//src/tools/execlog/..."
274280
- "//src/tools/singlejar/..."

BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ filegroup(
2727
"//src:srcs",
2828
"//tools:srcs",
2929
"//third_party:srcs",
30+
"//src/main/starlark/tests/builtins_bzl:srcs",
3031
] + glob([".bazelci/*"]) + [".bazelrc"],
3132
visibility = ["//src/test/shell/bazel:__pkg__"],
3233
)

src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
6363
attr("implementation_deps", LABEL_LIST)
6464
.allowedFileTypes(FileTypeSet.NO_FILE)
6565
.mandatoryProviders(CcInfo.PROVIDER.id()))
66+
.advertiseStarlarkProvider(CcInfo.PROVIDER.id())
6667
.build();
6768
}
6869

src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java

+4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public CcLinkingOutputs link(
182182
Object wholeArchive,
183183
Object additionalLinkstampDefines,
184184
Object onlyForDynamicLibs,
185+
Object mainOutput,
185186
Object linkerOutputs,
187+
Object winDefFile,
186188
StarlarkThread thread)
187189
throws InterruptedException, EvalException {
188190
return super.link(
@@ -208,7 +210,9 @@ public CcLinkingOutputs link(
208210
wholeArchive,
209211
additionalLinkstampDefines,
210212
onlyForDynamicLibs,
213+
mainOutput,
211214
linkerOutputs,
215+
winDefFile,
212216
thread);
213217
}
214218

src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java

+47-5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ private static RuntimeFiles collectRunfiles(
259259
.getAllArtifacts()
260260
.toList());
261261
}
262+
for (TransitiveInfoCollection transitiveInfoCollection :
263+
ruleContext.getPrerequisites("dynamic_deps")) {
264+
builder.merge(
265+
transitiveInfoCollection.getProvider(RunfilesProvider.class).getDefaultRunfiles());
266+
}
262267
// Add the C++ runtime libraries if linking them dynamically.
263268
if (linkingMode == Link.LinkingMode.DYNAMIC) {
264269
try {
@@ -557,6 +562,7 @@ public static void init(
557562
Pair<CcLinkingOutputs, CcLauncherInfo> ccLinkingOutputsAndCcLinkingInfo =
558563
createTransitiveLinkingActions(
559564
ruleContext,
565+
ruleBuilder,
560566
ccToolchain,
561567
featureConfiguration,
562568
fdoContext,
@@ -741,8 +747,9 @@ public static void init(
741747
.addNativeDeclaredProvider(ccLauncherInfo);
742748
}
743749

744-
public static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActions(
750+
private static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActions(
745751
RuleContext ruleContext,
752+
RuleConfiguredTargetBuilder ruleBuilder,
746753
CcToolchainProvider ccToolchain,
747754
FeatureConfiguration featureConfiguration,
748755
FdoContext fdoContext,
@@ -869,7 +876,7 @@ public static Pair<CcLinkingOutputs, CcLauncherInfo> createTransitiveLinkingActi
869876
CcLinkingContext ccLinkingContext =
870877
ruleContext.attributes().isAttributeValueExplicitlySpecified("dynamic_deps")
871878
? filterLibrariesThatAreLinkedDynamically(
872-
ruleContext, ccInfo.getCcLinkingContext(), cppSemantics)
879+
ruleContext, ruleBuilder, ccInfo.getCcLinkingContext(), cppSemantics)
873880
: ccInfo.getCcLinkingContext();
874881
if (ruleContext.hasErrors()) {
875882
return null;
@@ -1426,8 +1433,11 @@ private static ImmutableList<CcLinkingContext.LinkerInput> filterInputs(
14261433
graphStructureAspectNodes.add(nodeInfo);
14271434
}
14281435
}
1429-
graphStructureAspectNodes.add(
1430-
CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class));
1436+
GraphNodeInfo mallocNodeInfo =
1437+
CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class);
1438+
if (mallocNodeInfo != null) {
1439+
graphStructureAspectNodes.add(mallocNodeInfo);
1440+
}
14311441

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

15351545
private static CcLinkingContext filterLibrariesThatAreLinkedDynamically(
1536-
RuleContext ruleContext, CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) {
1546+
RuleContext ruleContext, RuleConfiguredTargetBuilder ruleBuilder,
1547+
CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) {
15371548
ImmutableList<CcSharedLibraryInfo> mergedCcSharedLibraryInfos =
15381549
mergeCcSharedLibraryInfos(ruleContext, cppSemantics);
15391550
ImmutableList<CcLinkingContext.LinkerInput> preloadedDeps =
@@ -1548,6 +1559,37 @@ private static CcLinkingContext filterLibrariesThatAreLinkedDynamically(
15481559
buildExportsMapFromOnlyDynamicDeps(ruleContext, mergedCcSharedLibraryInfos);
15491560
ImmutableList<CcLinkingContext.LinkerInput> staticLinkerInputs =
15501561
filterInputs(ruleContext, ccLinkingContext, exportsMap, linkOnceStaticLibsMap);
1562+
if (ruleContext
1563+
.getConfiguration()
1564+
.getFragment(CppConfiguration.class)
1565+
.experimentalCcSharedLibraryDebug()) {
1566+
ImmutableList.Builder<String> debugLinkerInputsFile = ImmutableList.builder();
1567+
debugLinkerInputsFile.add("Owner: " + ruleContext.getLabel());
1568+
for (CcLinkingContext.LinkerInput linkerInput :
1569+
Iterables.concat(staticLinkerInputs, preloadedDeps)) {
1570+
debugLinkerInputsFile.add(linkerInput.getOwner().toString());
1571+
}
1572+
Artifact linkOnceStaticLibsDebugFile =
1573+
ruleContext.getBinArtifact(
1574+
ruleContext.getLabel().getName() + "_link_once_static_libs.txt");
1575+
ruleContext.registerAction(
1576+
FileWriteAction.create(
1577+
ruleContext,
1578+
linkOnceStaticLibsDebugFile,
1579+
Joiner.on("\n").join(debugLinkerInputsFile.build()),
1580+
false));
1581+
NestedSetBuilder<Artifact> transitiveDebugFiles = NestedSetBuilder.stableOrder();
1582+
for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("dynamic_deps")) {
1583+
transitiveDebugFiles.addTransitive(
1584+
dep.get(OutputGroupInfo.STARLARK_CONSTRUCTOR).getOutputGroup("rule_impl_debug_files"));
1585+
}
1586+
ruleBuilder.addOutputGroup(
1587+
"rule_impl_debug_files",
1588+
NestedSetBuilder.<Artifact>stableOrder()
1589+
.add(linkOnceStaticLibsDebugFile)
1590+
.addTransitive(transitiveDebugFiles.build())
1591+
.build());
1592+
}
15511593

15521594
return createLinkingContextWithDynamicDependencies(
15531595
staticLinkerInputs, preloadedDeps, exportsMap.values());

src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java

+10
Original file line numberDiff line numberDiff line change
@@ -2300,19 +2300,26 @@ protected CcLinkingOutputs link(
23002300
Object wholeArchiveObject,
23012301
Object additionalLinkstampDefines,
23022302
Object onlyForDynamicLibsObject,
2303+
Object mainOutputObject,
23032304
Object linkerOutputsObject,
2305+
Object winDefFile,
23042306
StarlarkThread thread)
23052307
throws InterruptedException, EvalException {
23062308
// TODO(bazel-team): Rename always_link to alwayslink before delisting. Also it looks like the
23072309
// suffix parameter can be removed since we can use `name` for the same thing.
23082310
if (checkObjectsBound(
2311+
// TODO(b/205690414): Keep linkedArtifactNameSuffixObject protected. Use cases that are
2312+
// passing the suffix should be migrated to using mainOutput instead where the suffix is
2313+
// taken into account. Then this parameter should be removed.
23092314
linkedArtifactNameSuffixObject,
23102315
neverLinkObject,
23112316
alwaysLinkObject,
23122317
testOnlyTargetObject,
23132318
nativeDepsObject,
23142319
wholeArchiveObject,
23152320
additionalLinkstampDefines,
2321+
mainOutputObject,
2322+
winDefFile,
23162323
onlyForDynamicLibsObject)) {
23172324
checkPrivateStarlarkificationAllowlist(thread);
23182325
}
@@ -2327,6 +2334,7 @@ protected CcLinkingOutputs link(
23272334
convertFromNoneable(starlarkCcToolchainProvider, null);
23282335
FeatureConfigurationForStarlark featureConfiguration =
23292336
convertFromNoneable(starlarkFeatureConfiguration, null);
2337+
Artifact mainOutput = convertFromNoneable(mainOutputObject, null);
23302338
Label label = getCallerLabel(actions, name);
23312339
FdoContext fdoContext = ccToolchainProvider.getFdoContext();
23322340
LinkTargetType dynamicLinkTargetType = null;
@@ -2406,6 +2414,8 @@ protected CcLinkingOutputs link(
24062414
&& actualFeatureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS)
24072415
&& CppHelper.useInterfaceSharedLibraries(
24082416
cppConfiguration, ccToolchainProvider, actualFeatureConfiguration))
2417+
.setDefFile(convertFromNoneable(winDefFile, null))
2418+
.setLinkerOutputArtifact(convertFromNoneable(mainOutput, null))
24092419
.addLinkerOutputs(linkerOutputs);
24102420
if (staticLinkTargetType != null) {
24112421
helper.setShouldCreateDynamicLibrary(false).setStaticLinkType(staticLinkTargetType);

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public interface BazelCcModuleApi<
243243
name = "name",
244244
doc =
245245
"This is used for naming the output artifacts of actions created by this "
246-
+ "method.",
246+
+ "method. See also the `main_output` arg.",
247247
positional = false,
248248
named = true),
249249
@Param(
@@ -546,13 +546,34 @@ Tuple compile(
546546
documented = false,
547547
allowedTypes = {@ParamType(type = Boolean.class)},
548548
defaultValue = "unbound"),
549+
@Param(
550+
name = "main_output",
551+
doc =
552+
"Name of the main output artifact that will be produced by the linker. "
553+
+ "Only set this if the default name generation does not match you needs "
554+
+ "For output_type=executable, this is the final executable filename. "
555+
+ "For output_type=dynamic_library, this is the shared library filename. "
556+
+ "If not specified, then one will be computed based on `name` and "
557+
+ "`output_type`",
558+
positional = false,
559+
named = true,
560+
documented = false,
561+
defaultValue = "unbound",
562+
allowedTypes = {@ParamType(type = FileApi.class), @ParamType(type = NoneType.class)}),
549563
@Param(
550564
name = "additional_outputs",
551565
doc = "For additional outputs to the linking action, e.g.: map files.",
552566
positional = false,
553567
named = true,
554568
allowedTypes = {@ParamType(type = Sequence.class)},
555-
defaultValue = "unbound")
569+
defaultValue = "unbound"),
570+
@Param(
571+
name = "win_def_file",
572+
documented = false,
573+
positional = false,
574+
named = true,
575+
defaultValue = "unbound"),
576+
556577
})
557578
LinkingOutputsT link(
558579
StarlarkActionFactoryT starlarkActionFactoryApi,
@@ -577,7 +598,9 @@ LinkingOutputsT link(
577598
Object wholeArchive,
578599
Object additionalLinkstampDefines,
579600
Object onlyForDynamicLibs,
601+
Object mainOutput,
580602
Object linkerOutputs,
603+
Object winDefFile,
581604
StarlarkThread thread)
582605
throws InterruptedException, EvalException;
583606

0 commit comments

Comments
 (0)