Skip to content

Commit

Permalink
Add new API to Module classes, necessary for starlark cc_binary.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 409139665
  • Loading branch information
Googler authored and copybara-github committed Nov 11, 2021
1 parent a45db60 commit 1cb1779
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public CcLinkingOutputs link(
Object onlyForDynamicLibs,
Object mainOutput,
Object linkerOutputs,
Object useTestOnlyFlags,
Object pdbFile,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException {
return super.link(
Expand Down Expand Up @@ -211,13 +214,21 @@ public CcLinkingOutputs link(
onlyForDynamicLibs,
mainOutput,
linkerOutputs,
useTestOnlyFlags,
pdbFile,
winDefFile,
thread);
}

@Override
public CcCompilationOutputs createCompilationOutputsFromStarlark(
Object objectsObject, Object picObjectsObject) throws EvalException {
return super.createCompilationOutputsFromStarlark(objectsObject, picObjectsObject);
Object objectsObject,
Object picObjectsObject,
Object ltoCopmilationContextObject,
StarlarkThread thread)
throws EvalException {
return super.createCompilationOutputsFromStarlark(
objectsObject, picObjectsObject, ltoCopmilationContextObject, thread);
}

@Override
Expand Down
133 changes: 97 additions & 36 deletions src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ public String getToolForAction(
return featureConfiguration.getFeatureConfiguration().getToolPathForAction(actionName);
}

@Override
public Sequence<String> getToolRequirementForAction(
FeatureConfigurationForStarlark featureConfiguration,
String actionName,
StarlarkThread thread)
throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return StarlarkList.immutableCopyOf(
featureConfiguration.getFeatureConfiguration().getToolRequirementsForAction(actionName));
}

@Override
public Sequence<String> getExecutionRequirements(
FeatureConfigurationForStarlark featureConfiguration, String actionName) {
Expand Down Expand Up @@ -275,43 +286,61 @@ public CcToolchainVariables getCompileBuildVariables(
Object thinLtoOutputObjectFile,
boolean usePic,
boolean addLegacyCxxOptions,
Object variablesExtension)
Object variablesExtension,
Object stripOpts,
Object inputFile,
StarlarkThread thread)
throws EvalException {
if (checkObjectsBound(stripOpts, inputFile)) {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
}
ImmutableList<VariablesExtension> variablesExtensions =
asDict(variablesExtension).isEmpty()
? ImmutableList.of()
: ImmutableList.of(new UserVariablesExtension(asDict(variablesExtension)));
return CompileBuildVariables.setupVariablesOrThrowEvalException(
featureConfiguration.getFeatureConfiguration(),
ccToolchainProvider,
featureConfiguration
.getBuildOptionsFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
featureConfiguration
.getCppConfigurationFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
convertFromNoneable(sourceFile, /* defaultValue= */ null),
convertFromNoneable(outputFile, /* defaultValue= */ null),
/* gcnoFile= */ null,
/* isUsingFission= */ false,
/* dwoFile= */ null,
/* ltoIndexingFile= */ null,
convertFromNoneable(thinLtoIndex, /* defaultValue= */ null),
convertFromNoneable(thinLtoInputBitcodeFile, /* defaultValue=*/ null),
convertFromNoneable(thinLtoOutputObjectFile, /* defaultValue=*/ null),
/* includes= */ ImmutableList.of(),
userFlagsToIterable(userCompileFlags),
/* cppModuleMap= */ null,
usePic,
/* fdoStamp= */ null,
/* dotdFileExecPath= */ null,
variablesExtensions,
/* additionalBuildVariables= */ ImmutableMap.of(),
/* directModuleMaps= */ ImmutableList.of(),
Depset.noneableCast(includeDirs, String.class, "framework_include_directories"),
Depset.noneableCast(quoteIncludeDirs, String.class, "quote_include_directories"),
Depset.noneableCast(systemIncludeDirs, String.class, "system_include_directories"),
Depset.noneableCast(frameworkIncludeDirs, String.class, "framework_include_directories"),
Depset.noneableCast(defines, String.class, "preprocessor_defines").toList(),
ImmutableList.of());
CcToolchainVariables.Builder variables =
CcToolchainVariables.builder(
CompileBuildVariables.setupVariablesOrThrowEvalException(
featureConfiguration.getFeatureConfiguration(),
ccToolchainProvider,
featureConfiguration
.getBuildOptionsFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
featureConfiguration
.getCppConfigurationFromFeatureConfigurationCreatedForStarlark_andIKnowWhatImDoing(),
convertFromNoneable(sourceFile, /* defaultValue= */ null),
convertFromNoneable(outputFile, /* defaultValue= */ null),
/* gcnoFile= */ null,
/* isUsingFission= */ false,
/* dwoFile= */ null,
/* ltoIndexingFile= */ null,
convertFromNoneable(thinLtoIndex, /* defaultValue= */ null),
convertFromNoneable(thinLtoInputBitcodeFile, /* defaultValue=*/ null),
convertFromNoneable(thinLtoOutputObjectFile, /* defaultValue=*/ null),
/* includes= */ ImmutableList.of(),
userFlagsToIterable(userCompileFlags),
/* cppModuleMap= */ null,
usePic,
/* fdoStamp= */ null,
/* dotdFileExecPath= */ null,
variablesExtensions,
/* additionalBuildVariables= */ ImmutableMap.of(),
/* directModuleMaps= */ ImmutableList.of(),
Depset.noneableCast(includeDirs, String.class, "framework_include_directories"),
Depset.noneableCast(
quoteIncludeDirs, String.class, "quote_include_directories"),
Depset.noneableCast(
systemIncludeDirs, String.class, "system_include_directories"),
Depset.noneableCast(
frameworkIncludeDirs, String.class, "framework_include_directories"),
Depset.noneableCast(defines, String.class, "preprocessor_defines").toList(),
ImmutableList.of()))
.addStringSequenceVariable("stripopts", asClassImmutableList(stripOpts));

String inputFileString = convertFromNoneable(inputFile, null);
if (inputFileString != null) {
variables.addStringVariable("input_file", inputFileString);
}
return variables.build();
}

@Override
Expand Down Expand Up @@ -1804,16 +1833,26 @@ public Tuple createLinkingContextFromCompilationOutputs(
Sequence<?> linkingContexts, // <CcLinkingContext> expected
String name,
String languageString,
boolean alwayslink, // <Artifact> expected
Sequence<?> additionalInputs,
boolean alwayslink,
Sequence<?> additionalInputs, // <Artifact> expected
boolean disallowStaticLibraries,
boolean disallowDynamicLibraries,
Object grepIncludes,
Object variablesExtension,
Object stamp,
StarlarkThread thread)
throws InterruptedException, EvalException {
if (checkObjectsBound(stamp)) {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
}
Language language = parseLanguage(languageString);
StarlarkActionFactory actions = starlarkActionFactoryApi;
int stampInt = 0;
if (stamp != Starlark.UNBOUND) {
stampInt = (int) stamp;
}
boolean isStampingEnabled =
isStampingEnabled(stampInt, actions.getRuleContext().getConfiguration());
CcToolchainProvider ccToolchainProvider =
convertFromNoneable(starlarkCcToolchainProvider, null);
FeatureConfigurationForStarlark featureConfiguration =
Expand Down Expand Up @@ -1863,6 +1902,7 @@ public Tuple createLinkingContextFromCompilationOutputs(
.setStaticLinkType(staticLinkTargetType)
.setDynamicLinkType(LinkTargetType.NODEPS_DYNAMIC_LIBRARY)
.emitInterfaceSharedLibraries(true)
.setIsStampingEnabled(isStampingEnabled)
.addLinkopts(Sequence.cast(userLinkFlags, String.class, "user_link_flags"));
if (!asDict(variablesExtension).isEmpty()) {
helper.addVariableExtension(new UserVariablesExtension(asDict(variablesExtension)));
Expand Down Expand Up @@ -2268,6 +2308,9 @@ protected CcLinkingOutputs link(
Object onlyForDynamicLibsObject,
Object mainOutputObject,
Object linkerOutputsObject,
Object useTestOnlyFlags,
Object pdbFile,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException {
// TODO(bazel-team): Rename always_link to alwayslink before delisting. Also it looks like the
Expand All @@ -2284,7 +2327,10 @@ protected CcLinkingOutputs link(
wholeArchiveObject,
additionalLinkstampDefines,
mainOutputObject,
onlyForDynamicLibsObject)) {
onlyForDynamicLibsObject,
useTestOnlyFlags,
pdbFile,
winDefFile)) {
checkPrivateStarlarkificationAllowlist(thread);
}
Language language = parseLanguage(languageString);
Expand Down Expand Up @@ -2379,6 +2425,9 @@ protected CcLinkingOutputs link(
&& CppHelper.useInterfaceSharedLibraries(
cppConfiguration, ccToolchainProvider, actualFeatureConfiguration))
.setLinkerOutputArtifact(convertFromNoneable(mainOutput, null))
.setUseTestOnlyFlags(convertFromNoneable(useTestOnlyFlags, false))
.setPdbFile(convertFromNoneable(pdbFile, null))
.setDefFile(convertFromNoneable(winDefFile, null))
.addLinkerOutputs(linkerOutputs);
if (staticLinkTargetType != null) {
helper.setShouldCreateDynamicLibrary(false).setStaticLinkType(staticLinkTargetType);
Expand All @@ -2397,7 +2446,14 @@ protected CcLinkingOutputs link(
}

protected CcCompilationOutputs createCompilationOutputsFromStarlark(
Object objectsObject, Object picObjectsObject) throws EvalException {
Object objectsObject,
Object picObjectsObject,
Object ltoCompilationContextObject,
StarlarkThread thread)
throws EvalException {
if (checkObjectsBound(ltoCompilationContextObject)) {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
}
CcCompilationOutputs.Builder ccCompilationOutputsBuilder = CcCompilationOutputs.builder();
NestedSet<Artifact> objects = convertToNestedSet(objectsObject, Artifact.class, "objects");
validateExtensions(
Expand All @@ -2406,6 +2462,8 @@ protected CcCompilationOutputs createCompilationOutputsFromStarlark(
Link.OBJECT_FILETYPES,
Link.OBJECT_FILETYPES,
/* allowAnyTreeArtifacts= */ false);
LtoCompilationContext ltoCompilationContext =
convertFromNoneable(ltoCompilationContextObject, null);
NestedSet<Artifact> picObjects =
convertToNestedSet(picObjectsObject, Artifact.class, "pic_objects");
validateExtensions(
Expand All @@ -2416,6 +2474,9 @@ protected CcCompilationOutputs createCompilationOutputsFromStarlark(
/* allowAnyTreeArtifacts= */ false);
ccCompilationOutputsBuilder.addObjectFiles(objects.toList());
ccCompilationOutputsBuilder.addPicObjectFiles(picObjects.toList());
if (ltoCompilationContext != null) {
ccCompilationOutputsBuilder.addLtoCompilationContext(ltoCompilationContext);
}
return ccCompilationOutputsBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,26 @@ Tuple compile(
positional = false,
named = true,
allowedTypes = {@ParamType(type = Sequence.class)},
defaultValue = "unbound")
defaultValue = "unbound"),
@Param(
name = "use_test_only_flags",
documented = false,
positional = false,
named = true,
allowedTypes = {@ParamType(type = Boolean.class)},
defaultValue = "unbound"),
@Param(
name = "pdb_file",
documented = false,
positional = false,
named = true,
defaultValue = "unbound"),
@Param(
name = "win_def_file",
documented = false,
positional = false,
named = true,
defaultValue = "unbound"),
})
LinkingOutputsT link(
StarlarkActionFactoryT starlarkActionFactoryApi,
Expand All @@ -593,12 +612,16 @@ LinkingOutputsT link(
Object onlyForDynamicLibs,
Object mainOutput,
Object linkerOutputs,
Object useTestOnlyFlags,
Object pdbFile,
Object winDefFile,
StarlarkThread thread)
throws InterruptedException, EvalException;

@StarlarkMethod(
name = "create_compilation_outputs",
doc = "Create compilation outputs object.",
useStarlarkThread = true,
parameters = {
@Param(
name = "objects",
Expand All @@ -620,9 +643,19 @@ LinkingOutputsT link(
@ParamType(type = Depset.class),
@ParamType(type = NoneType.class),
}),
@Param(
name = "lto_compilation_context",
documented = false,
positional = false,
named = true,
defaultValue = "unbound"),
})
CompilationOutputsT createCompilationOutputsFromStarlark(
Object objectsObject, Object picObjectsObject) throws EvalException;
Object objectsObject,
Object picObjectsObject,
Object ltoCopmilationContextObject,
StarlarkThread thread)
throws EvalException;

@StarlarkMethod(
name = "merge_compilation_outputs",
Expand Down
Loading

0 comments on commit 1cb1779

Please sign in to comment.