@@ -478,9 +478,9 @@ def _process_build_scripts(
478478 - (File): An optional path to a generated environment file from a `cargo_build_script` target
479479 - (depset[File]): All direct and transitive build flags from the current build info.
480480 """
481- extra_inputs , out_dir , build_env_file , build_flags_files = _create_extra_input_args (build_info , dep_info )
481+ extra_inputs , out_dir , build_env_file , flags_files , link_flags_files = _create_extra_input_args (build_info , dep_info )
482482 compile_inputs = depset (transitive = [extra_inputs , compile_inputs ])
483- return compile_inputs , out_dir , build_env_file , build_flags_files
483+ return compile_inputs , out_dir , build_env_file , flags_files , link_flags_files
484484
485485def _symlink_for_ambiguous_lib (actions , toolchain , crate_info , lib ):
486486 """Constructs a disambiguating symlink for a library dependency.
@@ -764,12 +764,11 @@ def collect_inputs(
764764 # For backwards compatibility, we also check the value of the `rustc_env_files` attribute when
765765 # `crate_info.rustc_env_files` is not populated.
766766 build_env_files = crate_info .rustc_env_files if crate_info .rustc_env_files else getattr (files , "rustc_env_files" , [])
767- compile_inputs , out_dir , build_env_file , build_flags_files = _process_build_scripts (build_info , dep_info , compile_inputs )
767+ compile_inputs , out_dir , build_env_file , flags_files , link_flag_files = _process_build_scripts (build_info , dep_info , compile_inputs )
768768 if build_env_file :
769769 build_env_files = [f for f in build_env_files ] + [build_env_file ]
770770 compile_inputs = depset (build_env_files , transitive = [compile_inputs ])
771-
772- return compile_inputs , out_dir , build_env_files , build_flags_files , linkstamp_outs , ambiguous_libs
771+ return compile_inputs , out_dir , build_env_files , flags_files , link_flag_files , linkstamp_outs , ambiguous_libs
773772
774773def construct_arguments (
775774 ctx ,
@@ -787,7 +786,8 @@ def construct_arguments(
787786 rust_flags ,
788787 out_dir ,
789788 build_env_files ,
790- build_flags_files ,
789+ flags_files ,
790+ link_flags_files ,
791791 emit = ["dep-info" , "link" ],
792792 force_all_deps_direct = False ,
793793 rustdoc = False ,
@@ -852,7 +852,7 @@ def construct_arguments(
852852 for build_env_file in build_env_files :
853853 process_wrapper_flags .add ("--env-file" , build_env_file )
854854
855- process_wrapper_flags .add_all (build_flags_files , before_each = "--arg-file" )
855+ process_wrapper_flags .add_all (flags_files , before_each = "--arg-file" )
856856
857857 # Certain rust build processes expect to find files from the environment
858858 # variable `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera,
@@ -1011,6 +1011,8 @@ def construct_arguments(
10111011 env .update (link_env )
10121012 rustc_flags .add (ld , format = "--codegen=linker=%s" )
10131013 rustc_flags .add_joined ("--codegen" , link_args , join_with = " " , format_joined = "link-args=%s" )
1014+ # TODO pass linkopts (now in link_flags_files) from cc deps to linker
1015+ # This can be done by propagating linkopts from cc deps to downstream via CcInfo
10141016
10151017 _add_native_link_flags (rustc_flags , dep_info , linkstamp_outs , ambiguous_libs , crate_info .type , toolchain , cc_toolchain , feature_configuration , compilation_mode )
10161018
@@ -1156,7 +1158,7 @@ def rustc_compile_action(
11561158 # Determine if the build is currently running with --stamp
11571159 stamp = is_stamping_enabled (attr )
11581160
1159- compile_inputs , out_dir , build_env_files , build_flags_files , linkstamp_outs , ambiguous_libs = collect_inputs (
1161+ compile_inputs , out_dir , build_env_files , flags_files , flags_files , linkstamp_outs , ambiguous_libs = collect_inputs (
11601162 ctx = ctx ,
11611163 file = ctx .file ,
11621164 files = ctx .files ,
@@ -1202,7 +1204,8 @@ def rustc_compile_action(
12021204 rust_flags = rust_flags ,
12031205 out_dir = out_dir ,
12041206 build_env_files = build_env_files ,
1205- build_flags_files = build_flags_files ,
1207+ flags_files = flags_files ,
1208+ link_flags_files = link_flags_files ,
12061209 force_all_deps_direct = force_all_deps_direct ,
12071210 stamp = stamp ,
12081211 use_json_output = bool (build_metadata ) or bool (rustc_output ) or bool (rustc_rmeta_output ),
@@ -1227,8 +1230,8 @@ def rustc_compile_action(
12271230 output_hash = output_hash ,
12281231 rust_flags = rust_flags ,
12291232 out_dir = out_dir ,
1230- build_env_files = build_env_files ,
1231- build_flags_files = build_flags_files ,
1233+ flags_files = flags_files ,
1234+ link_flag_files = link_flags_files ,
12321235 force_all_deps_direct = force_all_deps_direct ,
12331236 stamp = stamp ,
12341237 use_json_output = True ,
@@ -1318,8 +1321,8 @@ def rustc_compile_action(
13181321 )
13191322 elif hasattr (ctx .executable , "_bootstrap_process_wrapper" ):
13201323 # Run without process_wrapper
1321- if build_env_files or build_flags_files or stamp or build_metadata :
1322- fail ("build_env_files, build_flags_files , stamp, build_metadata are not supported when building without process_wrapper" )
1324+ if build_env_files or flags_files or link_flags_files or stamp or build_metadata :
1325+ fail ("build_env_files, flags_files, link_flags_files , stamp, build_metadata are not supported when building without process_wrapper" )
13231326 ctx .actions .run (
13241327 executable = ctx .executable ._bootstrap_process_wrapper ,
13251328 inputs = compile_inputs ,
@@ -1650,7 +1653,8 @@ def _create_extra_input_args(build_info, dep_info):
16501653 - (depset[File]): A list of all build info `OUT_DIR` File objects
16511654 - (str): The `OUT_DIR` of the current build info
16521655 - (File): An optional generated environment file from a `cargo_build_script` target
1653- - (depset[File]): All direct and transitive build flag files from the current build info.
1656+ - (depset[File]): All direct and transitive build flag files for compiling from the current build info.
1657+ - (depset[File]): All direct and transitive build flag files for linking from the current build info.
16541658 """
16551659 input_files = []
16561660 input_depsets = []
@@ -1659,25 +1663,33 @@ def _create_extra_input_args(build_info, dep_info):
16591663 # to create the final command line
16601664 out_dir = None
16611665 build_env_file = None
1662- build_flags_files = []
1666+
1667+ # We need to make a distinction for flags files here to decide whether to
1668+ # pass them to rustc based on the output crate type
1669+ # linker_flags_files are not needed for bin, dylib, and cdylib crate types
1670+ # because a linker is not invoked to produce such crates
1671+ flags_files = []
1672+ link_flag_files = []
16631673
16641674 if build_info :
16651675 if build_info .out_dir :
16661676 out_dir = build_info .out_dir .path
16671677 input_files .append (build_info .out_dir )
16681678 build_env_file = build_info .rustc_env
16691679 if build_info .flags :
1670- build_flags_files .append (build_info .flags )
1671- if build_info .link_flags :
1672- build_flags_files .append (build_info .link_flags )
1673- input_files .append (build_info .link_flags )
1680+ flags_files .append (build_info .flags )
1681+ if build_info .linker_flags :
1682+ link_flag_files .append (build_info .linker_flags )
1683+ input_files .append (build_info .linker_flags )
16741684 input_depsets .append (build_info .compile_data )
16751685
16761686 return (
1687+ # TODO: Split input_files into compile_input_files and link_input_files
16771688 depset (input_files , transitive = [dep_info .link_search_path_files ] + input_depsets ),
16781689 out_dir ,
16791690 build_env_file ,
1680- depset (build_flags_files , transitive = [dep_info .link_search_path_files ]),
1691+ depset (flags_files , transitive = [dep_info .link_search_path_files ]),
1692+ depset (link_flag_files , transitive = [dep_info .link_search_path_files ]),
16811693 )
16821694
16831695def _compute_rpaths (toolchain , output_dir , dep_info , use_pic ):
0 commit comments