@@ -1115,39 +1115,16 @@ def construct_arguments(
11151115 if toolchain ._rename_first_party_crates :
11161116 env ["RULES_RUST_THIRD_PARTY_DIR" ] = toolchain ._third_party_dir
11171117
1118- if crate_info .type in toolchain .extra_rustc_flags_for_crate_types .keys ():
1119- rustc_flags .add_all (toolchain .extra_rustc_flags_for_crate_types [crate_info .type ], map_each = map_flag )
1120-
1121- if is_exec_configuration (ctx ):
1122- rustc_flags .add_all (toolchain .extra_exec_rustc_flags , map_each = map_flag )
1123- else :
1124- rustc_flags .add_all (toolchain .extra_rustc_flags , map_each = map_flag )
1125-
11261118 # extra_rustc_env applies to the target configuration, not the exec configuration.
11271119 if hasattr (ctx .attr , "_extra_rustc_env" ) and not is_exec_configuration (ctx ):
11281120 env .update (ctx .attr ._extra_rustc_env [ExtraRustcEnvInfo ].extra_rustc_env )
11291121
1130- # extra_rustc_flags apply to the target configuration, not the exec configuration.
1131- if hasattr (ctx .attr , "_extra_rustc_flags" ) and not is_exec_configuration (ctx ):
1132- rustc_flags .add_all (ctx .attr ._extra_rustc_flags [ExtraRustcFlagsInfo ].extra_rustc_flags , map_each = map_flag )
1133-
1134- if hasattr (ctx .attr , "_extra_rustc_flag" ) and not is_exec_configuration (ctx ):
1135- rustc_flags .add_all (ctx .attr ._extra_rustc_flag [ExtraRustcFlagsInfo ].extra_rustc_flags , map_each = map_flag )
1136-
1137- if hasattr (ctx .attr , "_per_crate_rustc_flag" ) and not is_exec_configuration (ctx ):
1138- per_crate_rustc_flags = ctx .attr ._per_crate_rustc_flag [PerCrateRustcFlagsInfo ].per_crate_rustc_flags
1139- _add_per_crate_rustc_flags (ctx , rustc_flags , map_flag , crate_info , per_crate_rustc_flags )
1140-
11411122 if hasattr (ctx .attr , "_extra_exec_rustc_env" ) and is_exec_configuration (ctx ):
11421123 env .update (ctx .attr ._extra_exec_rustc_env [ExtraExecRustcEnvInfo ].extra_exec_rustc_env )
11431124
1144- if hasattr (ctx .attr , "_extra_exec_rustc_flags" ) and is_exec_configuration (ctx ):
1145- rustc_flags .add_all (ctx .attr ._extra_exec_rustc_flags [ExtraExecRustcFlagsInfo ].extra_exec_rustc_flags , map_each = map_flag )
1146-
1147- if hasattr (ctx .attr , "_extra_exec_rustc_flag" ) and is_exec_configuration (ctx ):
1148- rustc_flags .add_all (ctx .attr ._extra_exec_rustc_flag [ExtraExecRustcFlagsInfo ].extra_exec_rustc_flags , map_each = map_flag )
1125+ rustc_flags .add_all (collect_extra_rustc_flags (ctx , toolchain , crate_info .root , crate_info .type ), map_each = map_flag )
11491126
1150- if _is_no_std (ctx , toolchain , crate_info ):
1127+ if is_no_std (ctx , toolchain , crate_info . is_test ):
11511128 rustc_flags .add ('--cfg=feature="no_std"' )
11521129
11531130 # Add target specific flags last, so they can override previous flags
@@ -1175,6 +1152,45 @@ def construct_arguments(
11751152
11761153 return args , env
11771154
1155+ def collect_extra_rustc_flags (ctx , toolchain , crate_root , crate_type ):
1156+ """Gather all 'extra' rustc flags from the target's attributes and toolchain.
1157+
1158+ Args:
1159+ ctx (ctx): The current rule's context object.
1160+ toolchain (rust_toolchain): The current Rust toolchain.
1161+ crate_root (File): The root file of the crate.
1162+ crate_type (str): The crate type.
1163+
1164+ Returns:
1165+ List[str]: Extra rustc flags.
1166+ """
1167+ flags = []
1168+
1169+ if crate_type in toolchain .extra_rustc_flags_for_crate_types .keys ():
1170+ flags .extend (toolchain .extra_rustc_flags_for_crate_types [crate_type ])
1171+
1172+ is_exec = is_exec_configuration (ctx )
1173+
1174+ flags .extend (toolchain .extra_exec_rustc_flags if is_exec else toolchain .extra_rustc_flags )
1175+
1176+ if hasattr (ctx .attr , "_extra_rustc_flags" ) and not is_exec :
1177+ flags .extend (ctx .attr ._extra_rustc_flags [ExtraRustcFlagsInfo ].extra_rustc_flags )
1178+
1179+ if hasattr (ctx .attr , "_extra_rustc_flag" ) and not is_exec :
1180+ flags .extend (ctx .attr ._extra_rustc_flag [ExtraRustcFlagsInfo ].extra_rustc_flags )
1181+
1182+ if hasattr (ctx .attr , "_per_crate_rustc_flag" ) and not is_exec :
1183+ per_crate_rustc_flags = ctx .attr ._per_crate_rustc_flag [PerCrateRustcFlagsInfo ].per_crate_rustc_flags
1184+ flags .extend (_collect_per_crate_rustc_flags (ctx , crate_root , per_crate_rustc_flags ))
1185+
1186+ if hasattr (ctx .attr , "_extra_exec_rustc_flags" ) and is_exec :
1187+ flags .extend (ctx .attr ._extra_exec_rustc_flags [ExtraExecRustcFlagsInfo ].extra_exec_rustc_flags )
1188+
1189+ if hasattr (ctx .attr , "_extra_exec_rustc_flag" ) and is_exec :
1190+ flags .extend (ctx .attr ._extra_exec_rustc_flag [ExtraExecRustcFlagsInfo ].extra_exec_rustc_flags )
1191+
1192+ return flags
1193+
11781194def rustc_compile_action (
11791195 * ,
11801196 ctx ,
@@ -1635,12 +1651,8 @@ def rustc_compile_action(
16351651
16361652 return providers
16371653
1638- def _is_no_std (ctx , toolchain , crate_info ):
1639- if is_exec_configuration (ctx ) or crate_info .is_test :
1640- return False
1641- if toolchain ._no_std == "off" :
1642- return False
1643- return True
1654+ def is_no_std (ctx , toolchain , crate_is_test ):
1655+ return not (is_exec_configuration (ctx ) or crate_is_test or toolchain ._no_std == "off" )
16441656
16451657def _should_use_rustc_allocator_libraries (toolchain ):
16461658 use_or_default = toolchain ._experimental_use_allocator_libraries_with_mangled_symbols
@@ -1675,7 +1687,7 @@ def _get_std_and_alloc_info(ctx, toolchain, crate_info):
16751687 return libs .libstd_and_allocator_ccinfo
16761688 return toolchain .libstd_and_allocator_ccinfo
16771689 if toolchain ._experimental_use_global_allocator :
1678- if _is_no_std (ctx , toolchain , crate_info ):
1690+ if is_no_std (ctx , toolchain , crate_info . is_test ):
16791691 if attr_global_allocator_library :
16801692 return libs .nostd_and_global_allocator_ccinfo
16811693 return toolchain .nostd_and_global_allocator_ccinfo
@@ -2285,16 +2297,20 @@ def _get_dirname(file):
22852297 """
22862298 return file .dirname
22872299
2288- def _add_per_crate_rustc_flags (ctx , args , map_flag , crate_info , per_crate_rustc_flags ):
2289- """Adds matching per-crate rustc flags to `args` .
2300+ def _collect_per_crate_rustc_flags (ctx , crate_root , per_crate_rustc_flags ):
2301+ """Return all matching per-crate rustc flags.
22902302
22912303 Args:
22922304 ctx (ctx): The source rule's context object
2293- args (Args): A reference to an Args object
2294- map_flag (function): An optional function to use to map added flags
2295- crate_info (CrateInfo): A CrateInfo provider
2305+ crate_root (File): The root file of the crate
22962306 per_crate_rustc_flags (list): A list of per_crate_rustc_flag values
2307+
2308+ Returns:
2309+ List[str]: matching per-crate rustc flags
22972310 """
2311+
2312+ flags = []
2313+
22982314 for per_crate_rustc_flag in per_crate_rustc_flags :
22992315 at_index = per_crate_rustc_flag .find ("@" )
23002316 if at_index == - 1 :
@@ -2312,13 +2328,12 @@ def _add_per_crate_rustc_flags(ctx, args, map_flag, crate_info, per_crate_rustc_
23122328 label = label_string [2 :]
23132329 else :
23142330 label = label_string
2315- execution_path = crate_info . root .path
2331+ execution_path = crate_root .path
23162332
23172333 if label .startswith (prefix_filter ) or execution_path .startswith (prefix_filter ):
2318- if map_flag :
2319- flag = map_flag (flag )
2320- if flag :
2321- args .add (flag )
2334+ flags .append (flag )
2335+
2336+ return flags
23222337
23232338def _error_format_impl (ctx ):
23242339 """Implementation of the `error_format` rule
0 commit comments