Skip to content

Commit 1cb1779

Browse files
Googlercopybara-github
authored andcommitted
Add new API to Module classes, necessary for starlark cc_binary.
PiperOrigin-RevId: 409139665
1 parent a45db60 commit 1cb1779

File tree

5 files changed

+419
-41
lines changed

5 files changed

+419
-41
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public CcLinkingOutputs link(
184184
Object onlyForDynamicLibs,
185185
Object mainOutput,
186186
Object linkerOutputs,
187+
Object useTestOnlyFlags,
188+
Object pdbFile,
189+
Object winDefFile,
187190
StarlarkThread thread)
188191
throws InterruptedException, EvalException {
189192
return super.link(
@@ -211,13 +214,21 @@ public CcLinkingOutputs link(
211214
onlyForDynamicLibs,
212215
mainOutput,
213216
linkerOutputs,
217+
useTestOnlyFlags,
218+
pdbFile,
219+
winDefFile,
214220
thread);
215221
}
216222

217223
@Override
218224
public CcCompilationOutputs createCompilationOutputsFromStarlark(
219-
Object objectsObject, Object picObjectsObject) throws EvalException {
220-
return super.createCompilationOutputsFromStarlark(objectsObject, picObjectsObject);
225+
Object objectsObject,
226+
Object picObjectsObject,
227+
Object ltoCopmilationContextObject,
228+
StarlarkThread thread)
229+
throws EvalException {
230+
return super.createCompilationOutputsFromStarlark(
231+
objectsObject, picObjectsObject, ltoCopmilationContextObject, thread);
221232
}
222233

223234
@Override

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

Lines changed: 97 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ public String getToolForAction(
217217
return featureConfiguration.getFeatureConfiguration().getToolPathForAction(actionName);
218218
}
219219

220+
@Override
221+
public Sequence<String> getToolRequirementForAction(
222+
FeatureConfigurationForStarlark featureConfiguration,
223+
String actionName,
224+
StarlarkThread thread)
225+
throws EvalException {
226+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
227+
return StarlarkList.immutableCopyOf(
228+
featureConfiguration.getFeatureConfiguration().getToolRequirementsForAction(actionName));
229+
}
230+
220231
@Override
221232
public Sequence<String> getExecutionRequirements(
222233
FeatureConfigurationForStarlark featureConfiguration, String actionName) {
@@ -275,43 +286,61 @@ public CcToolchainVariables getCompileBuildVariables(
275286
Object thinLtoOutputObjectFile,
276287
boolean usePic,
277288
boolean addLegacyCxxOptions,
278-
Object variablesExtension)
289+
Object variablesExtension,
290+
Object stripOpts,
291+
Object inputFile,
292+
StarlarkThread thread)
279293
throws EvalException {
294+
if (checkObjectsBound(stripOpts, inputFile)) {
295+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
296+
}
280297
ImmutableList<VariablesExtension> variablesExtensions =
281298
asDict(variablesExtension).isEmpty()
282299
? ImmutableList.of()
283300
: ImmutableList.of(new UserVariablesExtension(asDict(variablesExtension)));
284-
return CompileBuildVariables.setupVariablesOrThrowEvalException(
285-
featureConfiguration.getFeatureConfiguration(),
286-
ccToolchainProvider,
287-
featureConfiguration
288-
.getBuildOptionsFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
289-
featureConfiguration
290-
.getCppConfigurationFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
291-
convertFromNoneable(sourceFile, /* defaultValue= */ null),
292-
convertFromNoneable(outputFile, /* defaultValue= */ null),
293-
/* gcnoFile= */ null,
294-
/* isUsingFission= */ false,
295-
/* dwoFile= */ null,
296-
/* ltoIndexingFile= */ null,
297-
convertFromNoneable(thinLtoIndex, /* defaultValue= */ null),
298-
convertFromNoneable(thinLtoInputBitcodeFile, /* defaultValue=*/ null),
299-
convertFromNoneable(thinLtoOutputObjectFile, /* defaultValue=*/ null),
300-
/* includes= */ ImmutableList.of(),
301-
userFlagsToIterable(userCompileFlags),
302-
/* cppModuleMap= */ null,
303-
usePic,
304-
/* fdoStamp= */ null,
305-
/* dotdFileExecPath= */ null,
306-
variablesExtensions,
307-
/* additionalBuildVariables= */ ImmutableMap.of(),
308-
/* directModuleMaps= */ ImmutableList.of(),
309-
Depset.noneableCast(includeDirs, String.class, "framework_include_directories"),
310-
Depset.noneableCast(quoteIncludeDirs, String.class, "quote_include_directories"),
311-
Depset.noneableCast(systemIncludeDirs, String.class, "system_include_directories"),
312-
Depset.noneableCast(frameworkIncludeDirs, String.class, "framework_include_directories"),
313-
Depset.noneableCast(defines, String.class, "preprocessor_defines").toList(),
314-
ImmutableList.of());
301+
CcToolchainVariables.Builder variables =
302+
CcToolchainVariables.builder(
303+
CompileBuildVariables.setupVariablesOrThrowEvalException(
304+
featureConfiguration.getFeatureConfiguration(),
305+
ccToolchainProvider,
306+
featureConfiguration
307+
.getBuildOptionsFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
308+
featureConfiguration
309+
.getCppConfigurationFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
310+
convertFromNoneable(sourceFile, /* defaultValue= */ null),
311+
convertFromNoneable(outputFile, /* defaultValue= */ null),
312+
/* gcnoFile= */ null,
313+
/* isUsingFission= */ false,
314+
/* dwoFile= */ null,
315+
/* ltoIndexingFile= */ null,
316+
convertFromNoneable(thinLtoIndex, /* defaultValue= */ null),
317+
convertFromNoneable(thinLtoInputBitcodeFile, /* defaultValue=*/ null),
318+
convertFromNoneable(thinLtoOutputObjectFile, /* defaultValue=*/ null),
319+
/* includes= */ ImmutableList.of(),
320+
userFlagsToIterable(userCompileFlags),
321+
/* cppModuleMap= */ null,
322+
usePic,
323+
/* fdoStamp= */ null,
324+
/* dotdFileExecPath= */ null,
325+
variablesExtensions,
326+
/* additionalBuildVariables= */ ImmutableMap.of(),
327+
/* directModuleMaps= */ ImmutableList.of(),
328+
Depset.noneableCast(includeDirs, String.class, "framework_include_directories"),
329+
Depset.noneableCast(
330+
quoteIncludeDirs, String.class, "quote_include_directories"),
331+
Depset.noneableCast(
332+
systemIncludeDirs, String.class, "system_include_directories"),
333+
Depset.noneableCast(
334+
frameworkIncludeDirs, String.class, "framework_include_directories"),
335+
Depset.noneableCast(defines, String.class, "preprocessor_defines").toList(),
336+
ImmutableList.of()))
337+
.addStringSequenceVariable("stripopts", asClassImmutableList(stripOpts));
338+
339+
String inputFileString = convertFromNoneable(inputFile, null);
340+
if (inputFileString != null) {
341+
variables.addStringVariable("input_file", inputFileString);
342+
}
343+
return variables.build();
315344
}
316345

317346
@Override
@@ -1804,16 +1833,26 @@ public Tuple createLinkingContextFromCompilationOutputs(
18041833
Sequence<?> linkingContexts, // <CcLinkingContext> expected
18051834
String name,
18061835
String languageString,
1807-
boolean alwayslink, // <Artifact> expected
1808-
Sequence<?> additionalInputs,
1836+
boolean alwayslink,
1837+
Sequence<?> additionalInputs, // <Artifact> expected
18091838
boolean disallowStaticLibraries,
18101839
boolean disallowDynamicLibraries,
18111840
Object grepIncludes,
18121841
Object variablesExtension,
1842+
Object stamp,
18131843
StarlarkThread thread)
18141844
throws InterruptedException, EvalException {
1845+
if (checkObjectsBound(stamp)) {
1846+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
1847+
}
18151848
Language language = parseLanguage(languageString);
18161849
StarlarkActionFactory actions = starlarkActionFactoryApi;
1850+
int stampInt = 0;
1851+
if (stamp != Starlark.UNBOUND) {
1852+
stampInt = (int) stamp;
1853+
}
1854+
boolean isStampingEnabled =
1855+
isStampingEnabled(stampInt, actions.getRuleContext().getConfiguration());
18171856
CcToolchainProvider ccToolchainProvider =
18181857
convertFromNoneable(starlarkCcToolchainProvider, null);
18191858
FeatureConfigurationForStarlark featureConfiguration =
@@ -1863,6 +1902,7 @@ public Tuple createLinkingContextFromCompilationOutputs(
18631902
.setStaticLinkType(staticLinkTargetType)
18641903
.setDynamicLinkType(LinkTargetType.NODEPS_DYNAMIC_LIBRARY)
18651904
.emitInterfaceSharedLibraries(true)
1905+
.setIsStampingEnabled(isStampingEnabled)
18661906
.addLinkopts(Sequence.cast(userLinkFlags, String.class, "user_link_flags"));
18671907
if (!asDict(variablesExtension).isEmpty()) {
18681908
helper.addVariableExtension(new UserVariablesExtension(asDict(variablesExtension)));
@@ -2268,6 +2308,9 @@ protected CcLinkingOutputs link(
22682308
Object onlyForDynamicLibsObject,
22692309
Object mainOutputObject,
22702310
Object linkerOutputsObject,
2311+
Object useTestOnlyFlags,
2312+
Object pdbFile,
2313+
Object winDefFile,
22712314
StarlarkThread thread)
22722315
throws InterruptedException, EvalException {
22732316
// TODO(bazel-team): Rename always_link to alwayslink before delisting. Also it looks like the
@@ -2284,7 +2327,10 @@ protected CcLinkingOutputs link(
22842327
wholeArchiveObject,
22852328
additionalLinkstampDefines,
22862329
mainOutputObject,
2287-
onlyForDynamicLibsObject)) {
2330+
onlyForDynamicLibsObject,
2331+
useTestOnlyFlags,
2332+
pdbFile,
2333+
winDefFile)) {
22882334
checkPrivateStarlarkificationAllowlist(thread);
22892335
}
22902336
Language language = parseLanguage(languageString);
@@ -2379,6 +2425,9 @@ protected CcLinkingOutputs link(
23792425
&& CppHelper.useInterfaceSharedLibraries(
23802426
cppConfiguration, ccToolchainProvider, actualFeatureConfiguration))
23812427
.setLinkerOutputArtifact(convertFromNoneable(mainOutput, null))
2428+
.setUseTestOnlyFlags(convertFromNoneable(useTestOnlyFlags, false))
2429+
.setPdbFile(convertFromNoneable(pdbFile, null))
2430+
.setDefFile(convertFromNoneable(winDefFile, null))
23822431
.addLinkerOutputs(linkerOutputs);
23832432
if (staticLinkTargetType != null) {
23842433
helper.setShouldCreateDynamicLibrary(false).setStaticLinkType(staticLinkTargetType);
@@ -2397,7 +2446,14 @@ protected CcLinkingOutputs link(
23972446
}
23982447

23992448
protected CcCompilationOutputs createCompilationOutputsFromStarlark(
2400-
Object objectsObject, Object picObjectsObject) throws EvalException {
2449+
Object objectsObject,
2450+
Object picObjectsObject,
2451+
Object ltoCompilationContextObject,
2452+
StarlarkThread thread)
2453+
throws EvalException {
2454+
if (checkObjectsBound(ltoCompilationContextObject)) {
2455+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
2456+
}
24012457
CcCompilationOutputs.Builder ccCompilationOutputsBuilder = CcCompilationOutputs.builder();
24022458
NestedSet<Artifact> objects = convertToNestedSet(objectsObject, Artifact.class, "objects");
24032459
validateExtensions(
@@ -2406,6 +2462,8 @@ protected CcCompilationOutputs createCompilationOutputsFromStarlark(
24062462
Link.OBJECT_FILETYPES,
24072463
Link.OBJECT_FILETYPES,
24082464
/* allowAnyTreeArtifacts= */ false);
2465+
LtoCompilationContext ltoCompilationContext =
2466+
convertFromNoneable(ltoCompilationContextObject, null);
24092467
NestedSet<Artifact> picObjects =
24102468
convertToNestedSet(picObjectsObject, Artifact.class, "pic_objects");
24112469
validateExtensions(
@@ -2416,6 +2474,9 @@ protected CcCompilationOutputs createCompilationOutputsFromStarlark(
24162474
/* allowAnyTreeArtifacts= */ false);
24172475
ccCompilationOutputsBuilder.addObjectFiles(objects.toList());
24182476
ccCompilationOutputsBuilder.addPicObjectFiles(picObjects.toList());
2477+
if (ltoCompilationContext != null) {
2478+
ccCompilationOutputsBuilder.addLtoCompilationContext(ltoCompilationContext);
2479+
}
24192480
return ccCompilationOutputsBuilder.build();
24202481
}
24212482

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,26 @@ Tuple compile(
566566
positional = false,
567567
named = true,
568568
allowedTypes = {@ParamType(type = Sequence.class)},
569-
defaultValue = "unbound")
569+
defaultValue = "unbound"),
570+
@Param(
571+
name = "use_test_only_flags",
572+
documented = false,
573+
positional = false,
574+
named = true,
575+
allowedTypes = {@ParamType(type = Boolean.class)},
576+
defaultValue = "unbound"),
577+
@Param(
578+
name = "pdb_file",
579+
documented = false,
580+
positional = false,
581+
named = true,
582+
defaultValue = "unbound"),
583+
@Param(
584+
name = "win_def_file",
585+
documented = false,
586+
positional = false,
587+
named = true,
588+
defaultValue = "unbound"),
570589
})
571590
LinkingOutputsT link(
572591
StarlarkActionFactoryT starlarkActionFactoryApi,
@@ -593,12 +612,16 @@ LinkingOutputsT link(
593612
Object onlyForDynamicLibs,
594613
Object mainOutput,
595614
Object linkerOutputs,
615+
Object useTestOnlyFlags,
616+
Object pdbFile,
617+
Object winDefFile,
596618
StarlarkThread thread)
597619
throws InterruptedException, EvalException;
598620

599621
@StarlarkMethod(
600622
name = "create_compilation_outputs",
601623
doc = "Create compilation outputs object.",
624+
useStarlarkThread = true,
602625
parameters = {
603626
@Param(
604627
name = "objects",
@@ -620,9 +643,19 @@ LinkingOutputsT link(
620643
@ParamType(type = Depset.class),
621644
@ParamType(type = NoneType.class),
622645
}),
646+
@Param(
647+
name = "lto_compilation_context",
648+
documented = false,
649+
positional = false,
650+
named = true,
651+
defaultValue = "unbound"),
623652
})
624653
CompilationOutputsT createCompilationOutputsFromStarlark(
625-
Object objectsObject, Object picObjectsObject) throws EvalException;
654+
Object objectsObject,
655+
Object picObjectsObject,
656+
Object ltoCopmilationContextObject,
657+
StarlarkThread thread)
658+
throws EvalException;
626659

627660
@StarlarkMethod(
628661
name = "merge_compilation_outputs",

0 commit comments

Comments
 (0)