Skip to content

Commit 0850152

Browse files
bigelephant29copybara-github
authored andcommitted
Centralize stamp injection between the cc toolchain and PostMark.
PiperOrigin-RevId: 903199755 Change-Id: I56023825cfdb0ece1f99ad0c7e83ff6789fe9c16
1 parent 8956a5b commit 0850152

4 files changed

Lines changed: 110 additions & 53 deletions

File tree

cc/common/cc_helper_internal.bzl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,87 @@ CPP_SOURCE_TYPE_HEADER = "HEADER"
4444
CPP_SOURCE_TYPE_SOURCE = "SOURCE"
4545
CPP_SOURCE_TYPE_CLIF_INPUT_PROTO = "CLIF_INPUT_PROTO"
4646

47+
def get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration):
48+
"""Returns the FDO build stamp.
49+
50+
Args:
51+
cpp_configuration: The C++ configuration.
52+
fdo_context: The FDO context.
53+
feature_configuration: The feature configuration.
54+
55+
Returns:
56+
The FDO build stamp string, or None if FDO is not enabled.
57+
"""
58+
branch_fdo_profile = getattr(fdo_context, "branch_fdo_profile", None)
59+
if branch_fdo_profile:
60+
branch_fdo_mode = branch_fdo_profile.branch_fdo_mode
61+
if branch_fdo_mode == "auto_fdo":
62+
return "AFDO" if feature_configuration.is_enabled("autofdo") else None
63+
if branch_fdo_mode == "xbinary_fdo":
64+
return "XFDO" if feature_configuration.is_enabled("xbinaryfdo") else None
65+
if branch_fdo_mode == "llvm_cs_fdo" or cpp_configuration.cs_fdo_instrument():
66+
return "CSFDO"
67+
if branch_fdo_profile or cpp_configuration.fdo_instrument():
68+
return "FDO"
69+
return None
70+
71+
def get_linkstamp_stamps(
72+
cc_toolchain,
73+
feature_configuration,
74+
label_replacement,
75+
output_replacement,
76+
additional_linkstamp_defines):
77+
"""Returns a dict of stamps for linkstamp compilation/PostMark.
78+
79+
Args:
80+
cc_toolchain: The C++ toolchain provider.
81+
feature_configuration: The feature configuration.
82+
label_replacement: String to replace ${LABEL} in linkstamp defines.
83+
output_replacement: String to replace ${OUTPUT_PATH} in linkstamp defines.
84+
additional_linkstamp_defines: A list of additional defines for linkstamp compilation.
85+
86+
Returns:
87+
A dictionary of linkstamp defines.
88+
"""
89+
fdo_build_stamp = get_fdo_build_stamp(
90+
cc_toolchain._cpp_configuration,
91+
cc_toolchain._fdo_context,
92+
feature_configuration,
93+
)
94+
stamps = {
95+
"GPLATFORM": cc_toolchain.toolchain_id,
96+
"BUILD_COVERAGE_ENABLED": "1" if feature_configuration.is_enabled("coverage") else "0",
97+
# G3_TARGET_NAME is a C string literal that normally contain the label of the target
98+
# being linked. However, they are set differently when using shared native deps. In
99+
# that case, a single .so file is shared by multiple targets, and its contents cannot
100+
# depend on which target(s) were specified on the command line. So in that case we
101+
# have to use the (obscure) name of the .so file instead, or more precisely the path of
102+
# the .so file relative to the workspace root.
103+
"G3_TARGET_NAME": label_replacement,
104+
# G3_BUILD_TARGET is a C string literal containing the output of this
105+
# link. (An undocumented and untested invariant is that G3_BUILD_TARGET is the
106+
# location of the executable, either absolutely, or relative to the directory part of
107+
# BUILD_INFO.)
108+
"G3_BUILD_TARGET": output_replacement,
109+
}
110+
if fdo_build_stamp:
111+
stamps["BUILD_FDO_TYPE"] = fdo_build_stamp
112+
113+
if feature_configuration.is_enabled("thin_lto"):
114+
stamps["BUILD_LTO_TYPE"] = "thin"
115+
116+
if additional_linkstamp_defines:
117+
for d in additional_linkstamp_defines:
118+
# TODO: b/503100490 - This replacement looks redundant, but we should remove this in a separate CL.
119+
d = d.replace("${LABEL}", label_replacement).replace("${OUTPUT_PATH}", output_replacement)
120+
if "=" in d:
121+
k, v = d.split("=", 1)
122+
stamps[k] = v
123+
else:
124+
stamps[d] = "1"
125+
126+
return stamps
127+
47128
# LINT.IfChange(forked_exports)
48129

49130
CREATE_COMPILE_ACTION_API_ALLOWLISTED_PACKAGES = [("", "devtools/rust/cc_interop"), ("", "third_party/crubit"), ("", "tools/build_defs/clif")]

cc/private/compile/compile_build_variables.bzl

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
All build variables we create for various `CppCompileAction`s
1616
"""
1717

18-
load("//cc/common:cc_helper_internal.bzl", "extensions", _PRIVATE_STARLARKIFICATION_ALLOWLIST = "PRIVATE_STARLARKIFICATION_ALLOWLIST")
18+
load("//cc/common:cc_helper_internal.bzl", "extensions", "get_fdo_build_stamp", "get_linkstamp_stamps", _PRIVATE_STARLARKIFICATION_ALLOWLIST = "PRIVATE_STARLARKIFICATION_ALLOWLIST")
1919
load("//cc/private:cc_internal.bzl", _cc_internal = "cc_internal")
2020
load("//cc/private/rules_impl:native_cc_common.bzl", _cc_common_internal = "native_cc_common")
2121

@@ -154,7 +154,7 @@ def create_compile_variables(
154154
common_vars = _setup_common_compile_build_variables_internal(
155155
feature_configuration = feature_configuration,
156156
is_using_memprof = getattr(fdo_context, "memprof_profile_artifact", None) != None,
157-
fdo_build_stamp = _get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration),
157+
fdo_build_stamp = get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration),
158158
variables_extension = variables_extension,
159159
includes = includes or [],
160160
include_dirs = include_directories or depset(),
@@ -194,7 +194,7 @@ def setup_common_compile_build_variables(
194194
common_vars = _setup_common_compile_build_variables_internal(
195195
feature_configuration = feature_configuration,
196196
is_using_memprof = getattr(fdo_context, "memprof_profile_artifact", None) != None,
197-
fdo_build_stamp = _get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration),
197+
fdo_build_stamp = get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration),
198198
variables_extension = variables_extension,
199199
include_dirs = cc_compilation_context.includes,
200200
quote_include_dirs = cc_compilation_context.quote_includes,
@@ -259,20 +259,6 @@ def _setup_common_compile_build_variables_internal(
259259
result[_VARS.EXTERNAL_INCLUDE_PATHS] = external_include_dirs
260260
return _cc_internal.cc_toolchain_variables(vars = result)
261261

262-
def _get_fdo_build_stamp(cpp_configuration, fdo_context, feature_configuration):
263-
branch_fdo_profile = getattr(fdo_context, "branch_fdo_profile", None)
264-
if branch_fdo_profile:
265-
branch_fdo_mode = branch_fdo_profile.branch_fdo_mode
266-
if branch_fdo_mode == "auto_fdo":
267-
return "AFDO" if feature_configuration.is_enabled("autofdo") else None
268-
if branch_fdo_mode == "xbinary_fdo":
269-
return "XFDO" if feature_configuration.is_enabled("xbinaryfdo") else None
270-
if branch_fdo_mode == "llvm_cs_fdo" or cpp_configuration.cs_fdo_instrument():
271-
return "CSFDO"
272-
if branch_fdo_profile or cpp_configuration.fdo_instrument():
273-
return "FDO"
274-
return None
275-
276262
# Note: this method is side-effect free, callers should add fdo inputs to
277263
# cc_compile_action_builder themselves
278264
def get_specific_compile_build_variables(
@@ -573,12 +559,6 @@ def get_linkstamp_compile_variables(
573559
action_name = "linkstamp-compile",
574560
):
575561
fail("Action 'linkstamp-compile' is not configured.")
576-
fdo_build_stamp = _get_fdo_build_stamp(
577-
cc_toolchain._cpp_configuration,
578-
cc_toolchain._fdo_context,
579-
feature_configuration,
580-
)
581-
code_coverage_enabled = feature_configuration.is_enabled("coverage")
582562
copts = get_copts(
583563
language = "c++", # The only language that receives special treatment is "objc".
584564
cpp_configuration = cc_toolchain._cpp_configuration,
@@ -593,8 +573,7 @@ def get_linkstamp_compile_variables(
593573
output_replacement = output_replacement,
594574
additional_linkstamp_defines = additional_linkstamp_defines,
595575
cc_toolchain = cc_toolchain,
596-
fdo_build_stamp = fdo_build_stamp,
597-
code_coverage_enabled = code_coverage_enabled,
576+
feature_configuration = feature_configuration,
598577
)
599578
return create_compile_variables(
600579
feature_configuration = feature_configuration,
@@ -608,37 +587,31 @@ def get_linkstamp_compile_variables(
608587
user_compile_flags = copts,
609588
)
610589

590+
def _stamps_to_defines(stamps):
591+
"""Converts a dictionary of stamps to a list of C preprocessor defines."""
592+
defines = []
593+
for k, v in stamps.items():
594+
if v.isdigit() or k == "BUILD_LTO_TYPE":
595+
# BUILD_LTO_TYPE is a special case because of the AS_STRING(x) call in "builddata_globals.cc".
596+
# Normally, a string set as -DKEY_NAME="VALUE" is interpreted as KEY_NAME="VALUE",
597+
# but for this specific case, it's KEY_NAME="\"VALUE\"" because of AS_STRING(x).
598+
defines.append("{}={}".format(k, v))
599+
else:
600+
defines.append('{}="{}"'.format(k, v))
601+
return defines
602+
611603
def _compute_all_linkstamp_defines(
612604
label_replacement,
613605
output_replacement,
614606
additional_linkstamp_defines,
615607
cc_toolchain,
616-
fdo_build_stamp,
617-
code_coverage_enabled):
608+
feature_configuration):
618609
"""Computes defines for linkstamp compilation."""
619-
defines = [
620-
'GPLATFORM="' + cc_toolchain.toolchain_id + '"',
621-
"BUILD_COVERAGE_ENABLED=" + ("1" if code_coverage_enabled else "0"),
622-
# G3_TARGET_NAME is a C string literal that normally contain the label of the target
623-
# being linked. However, they are set differently when using shared native deps. In
624-
# that case, a single .so file is shared by multiple targets, and its contents cannot
625-
# depend on which target(s) were specified on the command line. So in that case we
626-
# have to use the (obscure) name of the .so file instead, or more precisely the path of
627-
# the .so file relative to the workspace root.
628-
'G3_TARGET_NAME="${LABEL}"',
629-
# G3_BUILD_TARGET is a C string literal containing the output of this
630-
# link. (An undocumented and untested invariant is that G3_BUILD_TARGET is the
631-
# location of the executable, either absolutely, or relative to the directory part of
632-
# BUILD_INFO.)
633-
'G3_BUILD_TARGET="${OUTPUT_PATH}"',
634-
]
635-
if additional_linkstamp_defines:
636-
defines.extend(additional_linkstamp_defines)
637-
638-
if fdo_build_stamp:
639-
defines.append('BUILD_FDO_TYPE="' + fdo_build_stamp + '"')
640-
641-
return [
642-
define.replace("${LABEL}", label_replacement).replace("${OUTPUT_PATH}", output_replacement)
643-
for define in defines
644-
]
610+
stamps = get_linkstamp_stamps(
611+
cc_toolchain = cc_toolchain,
612+
feature_configuration = feature_configuration,
613+
label_replacement = label_replacement,
614+
output_replacement = output_replacement,
615+
additional_linkstamp_defines = additional_linkstamp_defines,
616+
)
617+
return _stamps_to_defines(stamps)

cc/private/rules_impl/cc_binary_impl.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,9 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
679679
cc_toolchain,
680680
binary,
681681
output_binary_for_linking,
682+
feature_configuration = feature_configuration,
682683
additional_stamp_infos = additional_stamp_infos,
684+
additional_linkstamp_defines = [],
683685
)
684686

685687
cc_linking_outputs_binary_library = cc_linking_outputs_binary.library_to_link

cc/private/toolchain/unix_cc_toolchain_config.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,7 @@ def _impl(ctx):
16411641
),
16421642
],
16431643
),
1644+
# TODO: b/503100490 - We should centralize the stamp injection and remove this.
16441645
flag_set(
16451646
actions = [ACTION_NAMES.linkstamp_compile],
16461647
flag_groups = [flag_group(flags = ["-DBUILD_LTO_TYPE=thin"])],

0 commit comments

Comments
 (0)