Skip to content

Commit 59583b3

Browse files
pzembrodcopybara-github
authored andcommitted
Migrate CppLinkstampCompileHelper.createLinkstampCompileAction() to Starlark.
PiperOrigin-RevId: 811716060 Change-Id: Ia1f908ee36983129b7f2b3549f902eb3760cc243
1 parent a9a3ea1 commit 59583b3

File tree

4 files changed

+83
-184
lines changed

4 files changed

+83
-184
lines changed

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

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.devtools.build.lib.actions.Artifact;
2222
import com.google.devtools.build.lib.actions.PathMapper;
23-
import com.google.devtools.build.lib.analysis.AnalysisUtils;
24-
import com.google.devtools.build.lib.analysis.RuleContext;
2523
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
2624
import com.google.devtools.build.lib.analysis.starlark.StarlarkActionFactory;
2725
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
2826
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
2927
import com.google.devtools.build.lib.cmdline.Label;
3028
import com.google.devtools.build.lib.collect.nestedset.Depset;
31-
import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException;
3229
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
3330
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
3431
import com.google.devtools.build.lib.collect.nestedset.Order;
@@ -92,7 +89,6 @@ public Provider getCcToolchainProvider() {
9289
return CcToolchainProvider.BUILTINS_PROVIDER;
9390
}
9491

95-
9692
@Override
9793
public String getToolForAction(
9894
FeatureConfigurationForStarlark featureConfiguration,
@@ -524,111 +520,6 @@ protected Language parseLanguage(String string) throws EvalException {
524520
}
525521
}
526522

527-
@StarlarkMethod(
528-
name = "register_linkstamp_compile_action_internal",
529-
documented = false,
530-
useStarlarkThread = true,
531-
parameters = {
532-
@Param(
533-
name = "actions",
534-
positional = false,
535-
named = true,
536-
doc = "<code>actions</code> object."),
537-
@Param(
538-
name = "cc_toolchain",
539-
doc = "<code>CcToolchainInfo</code> provider to be used.",
540-
positional = false,
541-
named = true),
542-
@Param(
543-
name = "feature_configuration",
544-
doc = "<code>feature_configuration</code> to be queried.",
545-
positional = false,
546-
named = true),
547-
@Param(name = "source_file", documented = false, positional = false, named = true),
548-
@Param(name = "output_file", documented = false, positional = false, named = true),
549-
@Param(name = "compilation_inputs", documented = false, positional = false, named = true),
550-
@Param(
551-
name = "inputs_for_validation",
552-
documented = false,
553-
positional = false,
554-
named = true),
555-
@Param(name = "label_replacement", documented = false, positional = false, named = true),
556-
@Param(name = "output_replacement", documented = false, positional = false, named = true),
557-
@Param(
558-
name = "needs_pic",
559-
documented = false,
560-
positional = false,
561-
named = true,
562-
defaultValue = "False"),
563-
@Param(
564-
name = "stamping",
565-
documented = false,
566-
positional = false,
567-
named = true,
568-
defaultValue = "None"),
569-
@Param(
570-
name = "compile_build_variables",
571-
positional = false,
572-
named = true,
573-
documented = false),
574-
})
575-
public void registerLinkstampCompileAction(
576-
StarlarkActionFactory starlarkActionFactoryApi,
577-
Info ccToolchainInfo,
578-
FeatureConfigurationForStarlark featureConfigurationForStarlark,
579-
Artifact sourceFile,
580-
Artifact outputFile,
581-
Depset compilationInputs,
582-
Depset inputsForValidation,
583-
String labelReplacement,
584-
String outputReplacement,
585-
boolean needsPic,
586-
Object stampingObject,
587-
CcToolchainVariables compileBuildVariables,
588-
StarlarkThread thread)
589-
throws EvalException, InterruptedException, TypeException, RuleErrorException {
590-
isCalledFromStarlarkCcCommon(thread);
591-
RuleContext ruleContext = starlarkActionFactoryApi.getRuleContext();
592-
boolean stamping =
593-
stampingObject instanceof Boolean b
594-
? b
595-
: AnalysisUtils.isStampingEnabled(ruleContext, ruleContext.getConfiguration());
596-
CcToolchainProvider ccToolchain = CcToolchainProvider.wrapOrThrowEvalException(ccToolchainInfo);
597-
if (AnalysisUtils.isStampingEnabled(ruleContext, ruleContext.getConfiguration())) {
598-
// Makes the target depend on BUILD_INFO_KEY, which helps to discover stamped targets
599-
// See b/326620485 for more details.
600-
var unused =
601-
starlarkActionFactoryApi
602-
.getRuleContext()
603-
.getAnalysisEnvironment()
604-
.getVolatileWorkspaceStatusArtifact();
605-
}
606-
CppSemantics semantics = getSemantics();
607-
ImmutableList<Artifact> buildInfoHeaderArtifacts =
608-
stamping
609-
? ccToolchain
610-
.getCcBuildInfoTranslator()
611-
.getOutputGroup("non_redacted_build_info_files")
612-
.toList()
613-
: ccToolchain
614-
.getCcBuildInfoTranslator()
615-
.getOutputGroup("redacted_build_info_files")
616-
.toList();
617-
starlarkActionFactoryApi.registerAction(
618-
CppLinkstampCompileHelper.createLinkstampCompileAction(
619-
CppLinkActionBuilder.newActionConstruction(ruleContext),
620-
sourceFile,
621-
outputFile,
622-
compilationInputs.getSet(Artifact.class),
623-
/* nonCodeInputs= */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
624-
inputsForValidation.getSet(Artifact.class),
625-
buildInfoHeaderArtifacts,
626-
ccToolchain,
627-
featureConfigurationForStarlark.getFeatureConfiguration(),
628-
semantics,
629-
compileBuildVariables));
630-
}
631-
632523
@StarlarkMethod(
633524
name = "create_extra_link_time_library",
634525
documented = false,

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.google.devtools.build.lib.analysis.starlark.StarlarkActionFactory.StarlarkActionContext;
3939
import com.google.devtools.build.lib.analysis.starlark.StarlarkRuleContext;
4040
import com.google.devtools.build.lib.cmdline.Label;
41+
import com.google.devtools.build.lib.collect.nestedset.Depset;
4142
import com.google.devtools.build.lib.concurrent.BlazeInterners;
4243
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
4344
import com.google.devtools.build.lib.packages.AttributeMap;
@@ -611,6 +612,42 @@ public String computeOutputNamePrefixDir(BuildConfigurationValue configuration,
611612
@Param(name = "lto_indexing_file", positional = false, named = true, defaultValue = "None"),
612613
@Param(name = "use_pic", positional = false, named = true, defaultValue = "False"),
613614
@Param(name = "compile_build_variables", positional = false, named = true),
615+
@Param(
616+
name = "cache_key_inputs",
617+
positional = false,
618+
named = true,
619+
allowedTypes = {
620+
@ParamType(type = Depset.class, generic1 = Artifact.class),
621+
@ParamType(type = NoneType.class)
622+
},
623+
defaultValue = "None"),
624+
@Param(
625+
name = "build_info_header_files",
626+
positional = false,
627+
named = true,
628+
allowedTypes = {
629+
@ParamType(type = Sequence.class, generic1 = Artifact.class),
630+
@ParamType(type = NoneType.class)
631+
},
632+
defaultValue = "None"),
633+
@Param(
634+
name = "action_name",
635+
positional = false,
636+
named = true,
637+
allowedTypes = {@ParamType(type = String.class), @ParamType(type = NoneType.class)},
638+
defaultValue = "None"),
639+
@Param(
640+
name = "should_scan_includes",
641+
positional = false,
642+
named = true,
643+
allowedTypes = {@ParamType(type = Boolean.class), @ParamType(type = NoneType.class)},
644+
defaultValue = "None"),
645+
@Param(
646+
name = "shareable",
647+
positional = false,
648+
named = true,
649+
allowedTypes = {@ParamType(type = Boolean.class), @ParamType(type = NoneType.class)},
650+
defaultValue = "None")
614651
})
615652
public void createCppCompileAction(
616653
StarlarkRuleContext starlarkRuleContext,
@@ -630,7 +667,12 @@ public void createCppCompileAction(
630667
Object dwoFile,
631668
Object ltoIndexingFile,
632669
boolean usePic,
633-
CcToolchainVariables compileBuildVariables)
670+
CcToolchainVariables compileBuildVariables,
671+
Object cacheKeyInputs,
672+
Object buildInfoHeaderArtifacts,
673+
Object actionName,
674+
Object shouldScanIncludes,
675+
Object shareable)
634676
throws EvalException {
635677
CppCompileActionBuilder builder =
636678
createCppCompileActionBuilder(
@@ -652,6 +694,23 @@ public void createCppCompileAction(
652694
ltoIndexingFile,
653695
usePic);
654696
builder.setVariables(compileBuildVariables);
697+
if (cacheKeyInputs != Starlark.NONE) {
698+
builder.setCacheKeyInputs(Depset.cast(cacheKeyInputs, Artifact.class, "cache_key_inputs"));
699+
}
700+
if (buildInfoHeaderArtifacts != Starlark.NONE) {
701+
builder.setBuildInfoHeaderArtifacts(
702+
Sequence.cast(buildInfoHeaderArtifacts, Artifact.class, "builtin_header_files")
703+
.getImmutableList());
704+
}
705+
if (actionName instanceof String actionNameString) {
706+
builder.setActionName(actionNameString);
707+
}
708+
if (shouldScanIncludes instanceof Boolean bool) {
709+
builder.setShouldScanIncludes(bool);
710+
}
711+
if (shareable instanceof Boolean bool) {
712+
builder.setShareable(bool);
713+
}
655714
semantics.finalizeCompileActionBuilder(
656715
configuration, featureConfigurationForStarlark.getFeatureConfiguration(), builder);
657716
try {

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/main/starlark/builtins_bzl/common/cc/compile/linkstamp_compile.bzl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The cc_common.register_linkstamp_compile_action function.
1717
Used for C++ linkstamp compiling.
1818
"""
1919

20+
load(":common/cc/action_names.bzl", "LINKSTAMP_COMPILE_ACTION_NAME")
2021
load(
2122
":common/cc/cc_helper_internal.bzl",
2223
"is_stamping_enabled",
@@ -84,17 +85,30 @@ def register_linkstamp_compile_action(
8485
cc_toolchain = cc_toolchain,
8586
needs_pic = needs_pic,
8687
)
87-
cc_common_internal.register_linkstamp_compile_action_internal(
88-
actions = actions,
88+
89+
if stamping:
90+
# Makes the target depend on BUILD_INFO_KEY, which helps to discover stamped targets
91+
# See b/326620485 for more details.
92+
unused = ctx.version_file # @unused
93+
94+
# TODO(b/447325425): Consider if inputs_for_validation could (and should?) be passed in via
95+
# cc_compilation_context instead of via cache_key_inputs - a param that is used only here.
96+
cc_internal.create_cpp_compile_action(
97+
action_construction_context = ctx,
98+
cc_compilation_context = cc_internal.empty_compilation_context(),
8999
cc_toolchain = cc_toolchain,
100+
configuration = ctx.configuration,
101+
copts_filter = cc_internal.create_copts_filter(),
90102
feature_configuration = feature_configuration,
91-
source_file = source_file,
103+
cpp_semantics = cc_common_internal.get_cpp_semantics(language = "c++"),
104+
source_artifact = source_file,
105+
additional_compilation_inputs = compilation_inputs.to_list(),
92106
output_file = output_file,
93-
compilation_inputs = compilation_inputs,
94-
inputs_for_validation = inputs_for_validation,
95-
label_replacement = label_replacement,
96-
output_replacement = output_replacement,
97-
needs_pic = needs_pic,
98-
stamping = stamping,
107+
use_pic = needs_pic,
99108
compile_build_variables = compile_build_variables,
109+
cache_key_inputs = inputs_for_validation,
110+
build_info_header_files = build_info_files,
111+
action_name = LINKSTAMP_COMPILE_ACTION_NAME,
112+
should_scan_includes = True,
113+
shareable = True,
100114
)

0 commit comments

Comments
 (0)