Skip to content

Commit ce0291c

Browse files
authored
Internal macro construct_arguments now generates separate arg sets (#850)
1 parent 1d6bcbf commit ce0291c

File tree

2 files changed

+59
-46
lines changed

2 files changed

+59
-46
lines changed

rust/private/clippy.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def _clippy_aspect_impl(target, ctx):
9595
out_dir = out_dir,
9696
build_env_files = build_env_files,
9797
build_flags_files = build_flags_files,
98-
maker_path = clippy_marker.path,
9998
emit = ["dep-info", "metadata"],
10099
)
101100

@@ -105,16 +104,18 @@ def _clippy_aspect_impl(target, ctx):
105104
if "__bindgen" in ctx.rule.attr.tags:
106105
# bindgen-generated content is likely to trigger warnings, so
107106
# only fail on clippy warnings
108-
args.add("-Dclippy::style")
109-
args.add("-Dclippy::correctness")
110-
args.add("-Dclippy::complexity")
111-
args.add("-Dclippy::perf")
107+
args.rustc_flags.add("-Dclippy::style")
108+
args.rustc_flags.add("-Dclippy::correctness")
109+
args.rustc_flags.add("-Dclippy::complexity")
110+
args.rustc_flags.add("-Dclippy::perf")
112111
else:
113112
# fail on any warning
114-
args.add("-Dwarnings")
113+
args.rustc_flags.add("-Dwarnings")
115114

116115
if crate_info.is_test:
117-
args.add("--test")
116+
args.rustc_flags.add("--test")
117+
118+
args.process_wrapper_flags.add("--touch-file", clippy_marker.path)
118119

119120
# Upstream clippy requires one of these two filenames or it silently uses
120121
# the default config. Enforce the naming so users are not confused.
@@ -130,7 +131,7 @@ def _clippy_aspect_impl(target, ctx):
130131
outputs = [clippy_marker],
131132
env = env,
132133
tools = [toolchain.clippy_driver],
133-
arguments = [args],
134+
arguments = args.all,
134135
mnemonic = "Clippy",
135136
)
136137

rust/private/rustc.bzl

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ def construct_arguments(
329329
out_dir,
330330
build_env_files,
331331
build_flags_files,
332-
maker_path = None,
333332
emit = ["dep-info", "link"]):
334333
"""Builds an Args object containing common rustc flags
335334
@@ -349,12 +348,16 @@ def construct_arguments(
349348
out_dir (str): The path to the output directory for the target Crate.
350349
build_env_files (list): Files containing rustc environment variables, for instance from `cargo_build_script` actions.
351350
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
353351
emit (list): Values for the --emit flag to rustc.
354352
355353
Returns:
356354
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
358361
- (dict): Common rustc environment variables
359362
"""
360363
output_dir = getattr(crate_info.output, "dirname") if hasattr(crate_info.output, "dirname") else None
@@ -364,12 +367,12 @@ def construct_arguments(
364367
env = _get_rustc_env(attr, toolchain)
365368

366369
# Wrapper args first
367-
args = ctx.actions.args()
370+
process_wrapper_flags = ctx.actions.args()
368371

369372
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)
371374

372-
args.add_all(build_flags_files, before_each = "--arg-file")
375+
process_wrapper_flags.add_all(build_flags_files, before_each = "--arg-file")
373376

374377
# Certain rust build processes expect to find files from the environment
375378
# variable `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera,
@@ -383,7 +386,7 @@ def construct_arguments(
383386
#
384387
# Since we cannot get the `exec_root` from starlark, we cheat a little and
385388
# 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}")
387390

388391
# Both ctx.label.workspace_root and ctx.label.package are relative paths
389392
# and either can be empty strings. Avoid trailing/double slashes in the path.
@@ -410,57 +413,57 @@ def construct_arguments(
410413
if src != dst:
411414
emit_with_paths = [("link=" + dst if val == "link" else val) for val in emit]
412415

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)
418420

419421
# 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)
423426
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)
425428

426429
# Mangle symbols to disambiguate crates with the same name
427430
extra_filename = "-" + output_hash if output_hash else ""
428-
args.add("--codegen=metadata=" + extra_filename)
431+
rustc_flags.add("--codegen=metadata=" + extra_filename)
429432
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)
432435

433436
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)
436439

437440
# For determinism to help with build distribution and such
438-
args.add("--remap-path-prefix=${pwd}=.")
441+
rustc_flags.add("--remap-path-prefix=${pwd}=.")
439442

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)
443446
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"')
445448
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")
447450

448451
# Gets the paths to the folders containing the standard library (or libcore)
449452
rust_lib_paths = depset([file.dirname for file in toolchain.rust_lib.files.to_list()]).to_list()
450453

451454
# 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)
454457

455458
data_paths = getattr(attr, "data", []) + getattr(attr, "compile_data", [])
456-
args.add_all(
459+
rustc_flags.add_all(
457460
expand_list_element_locations(
458461
ctx,
459462
getattr(attr, "rustc_flags", []),
460463
data_paths,
461464
),
462465
)
463-
add_edition_flags(args, crate_info)
466+
add_edition_flags(rustc_flags, crate_info)
464467

465468
# Link!
466469
if "link" in emit:
@@ -470,19 +473,19 @@ def construct_arguments(
470473
rpaths = _compute_rpaths(toolchain, output_dir, dep_info)
471474
ld, link_args, link_env = get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths)
472475
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")
475478

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)
477480

478481
# 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)
480483

481484
needs_extern_proc_macro_flag = "proc-macro" in [crate_info.type, crate_info.wrapped_crate_type] and \
482485
crate_info.edition != "2015"
483486
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")
486489

487490
# Make bin crate data deps available to tests.
488491
for data in getattr(attr, "data", []):
@@ -501,6 +504,15 @@ def construct_arguments(
501504
# Set the SYSROOT to the directory of the rust_lib files passed to the toolchain
502505
env["SYSROOT"] = paths.dirname(toolchain.rust_lib.files.to_list()[0].short_path)
503506

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+
504516
return args, env
505517

506518
def rustc_compile_action(
@@ -576,7 +588,7 @@ def rustc_compile_action(
576588
inputs = compile_inputs,
577589
outputs = [crate_info.output],
578590
env = env,
579-
arguments = [args],
591+
arguments = args.all,
580592
mnemonic = "Rustc",
581593
progress_message = "Compiling Rust {} {}{} ({} files)".format(
582594
crate_info.type,

0 commit comments

Comments
 (0)