@@ -329,7 +329,6 @@ def construct_arguments(
329
329
out_dir ,
330
330
build_env_files ,
331
331
build_flags_files ,
332
- maker_path = None ,
333
332
emit = ["dep-info" , "link" ]):
334
333
"""Builds an Args object containing common rustc flags
335
334
@@ -349,12 +348,16 @@ def construct_arguments(
349
348
out_dir (str): The path to the output directory for the target Crate.
350
349
build_env_files (list): Files containing rustc environment variables, for instance from `cargo_build_script` actions.
351
350
build_flags_files (list): The output files of a `cargo_build_script` actions containing rustc build flags
352
- maker_path (File): An optional clippy marker file
353
351
emit (list): Values for the --emit flag to rustc.
354
352
355
353
Returns:
356
354
tuple: A tuple of the following items
357
- - (Args): An Args object with common Rust flags
355
+ - (struct): A struct of arguments used to run the `Rustc` action
356
+ - process_wrapper_flags (Args): Arguments for the process wrapper
357
+ - rustc_path (Args): Arguments for invoking rustc via the process wrapper
358
+ - rustc_flags (Args): Rust flags for the Rust compiler
359
+ - all (list): A list of all `Args` objects in the order listed above.
360
+ This is to be passed to the `arguments` parameter of actions
358
361
- (dict): Common rustc environment variables
359
362
"""
360
363
output_dir = getattr (crate_info .output , "dirname" ) if hasattr (crate_info .output , "dirname" ) else None
@@ -364,12 +367,12 @@ def construct_arguments(
364
367
env = _get_rustc_env (attr , toolchain )
365
368
366
369
# Wrapper args first
367
- args = ctx .actions .args ()
370
+ process_wrapper_flags = ctx .actions .args ()
368
371
369
372
for build_env_file in build_env_files :
370
- args .add ("--env-file" , build_env_file )
373
+ process_wrapper_flags .add ("--env-file" , build_env_file )
371
374
372
- args .add_all (build_flags_files , before_each = "--arg-file" )
375
+ process_wrapper_flags .add_all (build_flags_files , before_each = "--arg-file" )
373
376
374
377
# Certain rust build processes expect to find files from the environment
375
378
# variable `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera,
@@ -383,7 +386,7 @@ def construct_arguments(
383
386
#
384
387
# Since we cannot get the `exec_root` from starlark, we cheat a little and
385
388
# use `${pwd}` which resolves the `exec_root` at action execution time.
386
- args .add ("--subst" , "pwd=${pwd}" )
389
+ process_wrapper_flags .add ("--subst" , "pwd=${pwd}" )
387
390
388
391
# Both ctx.label.workspace_root and ctx.label.package are relative paths
389
392
# and either can be empty strings. Avoid trailing/double slashes in the path.
@@ -410,57 +413,57 @@ def construct_arguments(
410
413
if src != dst :
411
414
emit_with_paths = [("link=" + dst if val == "link" else val ) for val in emit ]
412
415
413
- if maker_path != None :
414
- args .add ("--touch-file" , maker_path )
415
-
416
- args .add ("--" )
417
- args .add (tool_path )
416
+ # Arguments for launching rustc from the process wrapper
417
+ rustc_path = ctx .actions .args ()
418
+ rustc_path .add ("--" )
419
+ rustc_path .add (tool_path )
418
420
419
421
# Rustc arguments
420
- args .add (crate_info .root )
421
- args .add ("--crate-name=" + crate_info .name )
422
- args .add ("--crate-type=" + crate_info .type )
422
+ rustc_flags = ctx .actions .args ()
423
+ rustc_flags .add (crate_info .root )
424
+ rustc_flags .add ("--crate-name=" + crate_info .name )
425
+ rustc_flags .add ("--crate-type=" + crate_info .type )
423
426
if hasattr (attr , "_error_format" ):
424
- args .add ("--error-format=" + attr ._error_format [ErrorFormatInfo ].error_format )
427
+ rustc_flags .add ("--error-format=" + attr ._error_format [ErrorFormatInfo ].error_format )
425
428
426
429
# Mangle symbols to disambiguate crates with the same name
427
430
extra_filename = "-" + output_hash if output_hash else ""
428
- args .add ("--codegen=metadata=" + extra_filename )
431
+ rustc_flags .add ("--codegen=metadata=" + extra_filename )
429
432
if output_dir :
430
- args .add ("--out-dir=" + output_dir )
431
- args .add ("--codegen=extra-filename=" + extra_filename )
433
+ rustc_flags .add ("--out-dir=" + output_dir )
434
+ rustc_flags .add ("--codegen=extra-filename=" + extra_filename )
432
435
433
436
compilation_mode = get_compilation_mode_opts (ctx , toolchain )
434
- args .add ("--codegen=opt-level=" + compilation_mode .opt_level )
435
- args .add ("--codegen=debuginfo=" + compilation_mode .debug_info )
437
+ rustc_flags .add ("--codegen=opt-level=" + compilation_mode .opt_level )
438
+ rustc_flags .add ("--codegen=debuginfo=" + compilation_mode .debug_info )
436
439
437
440
# For determinism to help with build distribution and such
438
- args .add ("--remap-path-prefix=${pwd}=." )
441
+ rustc_flags .add ("--remap-path-prefix=${pwd}=." )
439
442
440
- args .add ("--emit=" + "," .join (emit_with_paths ))
441
- args .add ("--color=always" )
442
- args .add ("--target=" + toolchain .target_flag_value )
443
+ rustc_flags .add ("--emit=" + "," .join (emit_with_paths ))
444
+ rustc_flags .add ("--color=always" )
445
+ rustc_flags .add ("--target=" + toolchain .target_flag_value )
443
446
if hasattr (attr , "crate_features" ):
444
- args .add_all (getattr (attr , "crate_features" ), before_each = "--cfg" , format_each = 'feature="%s"' )
447
+ rustc_flags .add_all (getattr (attr , "crate_features" ), before_each = "--cfg" , format_each = 'feature="%s"' )
445
448
if linker_script :
446
- args .add (linker_script .path , format = "--codegen=link-arg=-T%s" )
449
+ rustc_flags .add (linker_script .path , format = "--codegen=link-arg=-T%s" )
447
450
448
451
# Gets the paths to the folders containing the standard library (or libcore)
449
452
rust_lib_paths = depset ([file .dirname for file in toolchain .rust_lib .files .to_list ()]).to_list ()
450
453
451
454
# Tell Rustc where to find the standard library
452
- args .add_all (rust_lib_paths , before_each = "-L" , format_each = "%s" )
453
- args .add_all (rust_flags )
455
+ rustc_flags .add_all (rust_lib_paths , before_each = "-L" , format_each = "%s" )
456
+ rustc_flags .add_all (rust_flags )
454
457
455
458
data_paths = getattr (attr , "data" , []) + getattr (attr , "compile_data" , [])
456
- args .add_all (
459
+ rustc_flags .add_all (
457
460
expand_list_element_locations (
458
461
ctx ,
459
462
getattr (attr , "rustc_flags" , []),
460
463
data_paths ,
461
464
),
462
465
)
463
- add_edition_flags (args , crate_info )
466
+ add_edition_flags (rustc_flags , crate_info )
464
467
465
468
# Link!
466
469
if "link" in emit :
@@ -470,19 +473,19 @@ def construct_arguments(
470
473
rpaths = _compute_rpaths (toolchain , output_dir , dep_info )
471
474
ld , link_args , link_env = get_linker_and_args (ctx , cc_toolchain , feature_configuration , rpaths )
472
475
env .update (link_env )
473
- args .add ("--codegen=linker=" + ld )
474
- args .add_joined ("--codegen" , link_args , join_with = " " , format_joined = "link-args=%s" )
476
+ rustc_flags .add ("--codegen=linker=" + ld )
477
+ rustc_flags .add_joined ("--codegen" , link_args , join_with = " " , format_joined = "link-args=%s" )
475
478
476
- _add_native_link_flags (args , dep_info , crate_type , toolchain , cc_toolchain , feature_configuration )
479
+ _add_native_link_flags (rustc_flags , dep_info , crate_type , toolchain , cc_toolchain , feature_configuration )
477
480
478
481
# These always need to be added, even if not linking this crate.
479
- add_crate_link_flags (args , dep_info )
482
+ add_crate_link_flags (rustc_flags , dep_info )
480
483
481
484
needs_extern_proc_macro_flag = "proc-macro" in [crate_info .type , crate_info .wrapped_crate_type ] and \
482
485
crate_info .edition != "2015"
483
486
if needs_extern_proc_macro_flag :
484
- args .add ("--extern" )
485
- args .add ("proc_macro" )
487
+ rustc_flags .add ("--extern" )
488
+ rustc_flags .add ("proc_macro" )
486
489
487
490
# Make bin crate data deps available to tests.
488
491
for data in getattr (attr , "data" , []):
@@ -501,6 +504,15 @@ def construct_arguments(
501
504
# Set the SYSROOT to the directory of the rust_lib files passed to the toolchain
502
505
env ["SYSROOT" ] = paths .dirname (toolchain .rust_lib .files .to_list ()[0 ].short_path )
503
506
507
+ # Create a struct which keeps the arguments separate so each may be tuned or
508
+ # replaced where necessary
509
+ args = struct (
510
+ process_wrapper_flags = process_wrapper_flags ,
511
+ rustc_path = rustc_path ,
512
+ rustc_flags = rustc_flags ,
513
+ all = [process_wrapper_flags , rustc_path , rustc_flags ],
514
+ )
515
+
504
516
return args , env
505
517
506
518
def rustc_compile_action (
@@ -576,7 +588,7 @@ def rustc_compile_action(
576
588
inputs = compile_inputs ,
577
589
outputs = [crate_info .output ],
578
590
env = env ,
579
- arguments = [ args ] ,
591
+ arguments = args . all ,
580
592
mnemonic = "Rustc" ,
581
593
progress_message = "Compiling Rust {} {}{} ({} files)" .format (
582
594
crate_info .type ,
0 commit comments