@@ -125,6 +125,8 @@ def create_lto_backends(
125125 if file .is_directory :
126126 fail ("Thinlto with tree artifacts requires feature use_lto_native_object_directory." )
127127
128+ build_variables , additional_inputs = setup_common_lto_variables (cc_toolchain , feature_configuration )
129+
128130 # Make this a NestedSet to return from LtoBackendAction.getAllowedDerivedInputs. For M binaries
129131 # and N .o files, this is O(M*N). If we had nested sets of bitcode files, it would be O(M + N).
130132 all_bitcode_depset = depset (all_bitcode )
@@ -153,6 +155,8 @@ def create_lto_backends(
153155 cc_toolchain = cc_toolchain ,
154156 use_pic = use_pic ,
155157 should_create_per_object_debug_info = debug ,
158+ build_variables = build_variables ,
159+ additional_inputs = additional_inputs ,
156160 argv = backend_user_compile_flags ,
157161 ))
158162 else :
@@ -181,6 +185,8 @@ def create_lto_backends(
181185 cc_toolchain = cc_toolchain ,
182186 use_pic = use_pic ,
183187 should_create_per_object_debug_info = debug ,
188+ build_variables = build_variables ,
189+ additional_inputs = additional_inputs ,
184190 argv = backend_user_compile_flags ,
185191 ))
186192 return lto_outputs
@@ -228,6 +234,8 @@ def create_shared_non_lto_artifacts(
228234 cpp_config = cc_toolchain ._cpp_configuration
229235 debug = should_create_per_object_debug_info (feature_configuration , cpp_config )
230236
237+ build_variables , additional_inputs = setup_common_lto_variables (cc_toolchain , feature_configuration )
238+
231239 shared_non_lto_backends = {}
232240 for obj in object_file_inputs :
233241 if obj not in lto_compilation_context .lto_bitcode_inputs :
@@ -244,10 +252,48 @@ def create_shared_non_lto_artifacts(
244252 cc_toolchain = cc_toolchain ,
245253 use_pic = use_pic ,
246254 should_create_per_object_debug_info = debug ,
255+ build_variables = build_variables ,
256+ additional_inputs = additional_inputs ,
247257 argv = backend_user_compile_flags ,
248258 )
249259 return shared_non_lto_backends
250260
261+ def setup_common_lto_variables (
262+ cc_toolchain ,
263+ feature_configuration ,
264+ ):
265+ """
266+ Populates build_variables and additional_inputs with data that is independent of what file is the input to the action.
267+
268+ Args:
269+ cc_toolchain: (CcToolchainInfo) The C++ toolchain.
270+ feature_configuration: (feature_configuration) The feature configuration.
271+
272+ Returns:
273+ A CcToolchainVariables provider and a list[File] of additional inputs.
274+ """
275+
276+ build_variables = {}
277+ additional_inputs = []
278+
279+ _add_profile_for_lto_backend (
280+ additional_inputs ,
281+ cc_toolchain ._fdo_context ,
282+ feature_configuration ,
283+ build_variables ,
284+ )
285+
286+ # Add the context sensitive instrument path to the backend.
287+ if feature_configuration .is_enabled ("cs_fdo_instrument" ):
288+ build_variables ["cs_fdo_instrument_path" ] = cc_toolchain ._cpp_configuration .cs_fdo_instrument ()
289+
290+ build_variables = _cc_internal .combine_cc_toolchain_variables (
291+ cc_toolchain ._build_variables ,
292+ _cc_internal .cc_toolchain_variables (vars = build_variables ),
293+ )
294+
295+ return build_variables , additional_inputs
296+
251297def create_lto_backend_artifacts (
252298 * ,
253299 actions ,
@@ -259,6 +305,8 @@ def create_lto_backend_artifacts(
259305 cc_toolchain ,
260306 use_pic ,
261307 should_create_per_object_debug_info ,
308+ build_variables ,
309+ additional_inputs ,
262310 argv ):
263311 """Create an LTO backend.
264312
@@ -281,26 +329,26 @@ def create_lto_backend_artifacts(
281329 cc_toolchain: (CcToolchainInfo) The C++ toolchain.
282330 use_pic: (bool) Whether to use PIC.
283331 should_create_per_object_debug_info: (bool) Whether to create per-object debug info.
332+ build_variables (CcToolchainVariables):
333+ additional_inputs: list[File]:
284334 argv: (list[str]) The command line arguments to pass to the LTO backend.
285335
286336 Returns:
287337 An LtoBackendArtifactsInfo provider.
288338 """
289339
340+ if not _cc_common_internal .action_is_enabled (feature_configuration = feature_configuration , action_name = "lto-backend" ):
341+ fail ("Thinlto build is requested, but the C++ toolchain doesn't define an action_config for 'lto-backend' action." )
342+
290343 create_shared_non_lto = all_bitcode_files == None
291344
292- build_variables = {}
293- additional_inputs = []
294- _initialize_build_variables (
345+ build_variables = _cc_internal .combine_cc_toolchain_variables (
295346 build_variables ,
296- additional_inputs ,
297- cc_toolchain ,
298- feature_configuration ,
299- argv ,
347+ _cc_internal .cc_toolchain_variables (vars = {
348+ "user_compile_flags" : _cc_internal .intern_string_sequence_variable_value (argv ),
349+ }),
300350 )
301351
302- build_variables = _cc_internal .cc_toolchain_variables (vars = build_variables )
303- build_variables = _cc_internal .combine_cc_toolchain_variables (cc_toolchain ._build_variables , build_variables )
304352 env = _cc_common_internal .get_environment_variables (
305353 feature_configuration = feature_configuration ,
306354 action_name = "lto-backend" ,
@@ -372,26 +420,6 @@ def create_lto_backend_artifacts(
372420 _dwo_file = dwo_file ,
373421 )
374422
375- def _initialize_build_variables (build_variables , additional_inputs , cc_toolchain , feature_configuration , user_compile_flags ):
376- """
377- Populates build_variables and additional_inputs with data that is independent of what file is the input to the action.
378- """
379- _add_profile_for_lto_backend (
380- additional_inputs ,
381- cc_toolchain ._fdo_context ,
382- feature_configuration ,
383- build_variables ,
384- )
385-
386- # Add the context sensitive instrument path to the backend.
387- if feature_configuration .is_enabled ("cs_fdo_instrument" ):
388- build_variables ["cs_fdo_instrument_path" ] = cc_toolchain ._cpp_configuration .cs_fdo_instrument ()
389-
390- build_variables ["user_compile_flags" ] = _cc_internal .intern_string_sequence_variable_value (user_compile_flags )
391-
392- if not _cc_common_internal .action_is_enabled (feature_configuration = feature_configuration , action_name = "lto-backend" ):
393- fail ("Thinlto build is requested, but the C++ toolchain doesn't define an action_config for 'lto-backend' action." )
394-
395423def _add_profile_for_lto_backend (additional_inputs , fdo_context , feature_configuration , build_variables ):
396424 prefetch = getattr (fdo_context , "prefetch_hints_artifact" , None )
397425 if prefetch != None :
0 commit comments