1515All 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" )
1919load ("//cc/private:cc_internal.bzl" , _cc_internal = "cc_internal" )
2020load ("//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
278264def 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+
611603def _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 )
0 commit comments