@@ -1115,39 +1115,16 @@ def construct_arguments(
1115
1115
if toolchain ._rename_first_party_crates :
1116
1116
env ["RULES_RUST_THIRD_PARTY_DIR" ] = toolchain ._third_party_dir
1117
1117
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
-
1126
1118
# extra_rustc_env applies to the target configuration, not the exec configuration.
1127
1119
if hasattr (ctx .attr , "_extra_rustc_env" ) and not is_exec_configuration (ctx ):
1128
1120
env .update (ctx .attr ._extra_rustc_env [ExtraRustcEnvInfo ].extra_rustc_env )
1129
1121
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
-
1141
1122
if hasattr (ctx .attr , "_extra_exec_rustc_env" ) and is_exec_configuration (ctx ):
1142
1123
env .update (ctx .attr ._extra_exec_rustc_env [ExtraExecRustcEnvInfo ].extra_exec_rustc_env )
1143
1124
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 )
1149
1126
1150
- if _is_no_std (ctx , toolchain , crate_info ):
1127
+ if is_no_std (ctx , toolchain , crate_info . is_test ):
1151
1128
rustc_flags .add ('--cfg=feature="no_std"' )
1152
1129
1153
1130
# Add target specific flags last, so they can override previous flags
@@ -1175,6 +1152,45 @@ def construct_arguments(
1175
1152
1176
1153
return args , env
1177
1154
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
+
1178
1194
def rustc_compile_action (
1179
1195
* ,
1180
1196
ctx ,
@@ -1635,12 +1651,8 @@ def rustc_compile_action(
1635
1651
1636
1652
return providers
1637
1653
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" )
1644
1656
1645
1657
def _should_use_rustc_allocator_libraries (toolchain ):
1646
1658
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):
1675
1687
return libs .libstd_and_allocator_ccinfo
1676
1688
return toolchain .libstd_and_allocator_ccinfo
1677
1689
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 ):
1679
1691
if attr_global_allocator_library :
1680
1692
return libs .nostd_and_global_allocator_ccinfo
1681
1693
return toolchain .nostd_and_global_allocator_ccinfo
@@ -2285,16 +2297,20 @@ def _get_dirname(file):
2285
2297
"""
2286
2298
return file .dirname
2287
2299
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.
2290
2302
2291
2303
Args:
2292
2304
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
2296
2306
per_crate_rustc_flags (list): A list of per_crate_rustc_flag values
2307
+
2308
+ Returns:
2309
+ List[str]: matching per-crate rustc flags
2297
2310
"""
2311
+
2312
+ flags = []
2313
+
2298
2314
for per_crate_rustc_flag in per_crate_rustc_flags :
2299
2315
at_index = per_crate_rustc_flag .find ("@" )
2300
2316
if at_index == - 1 :
@@ -2312,13 +2328,12 @@ def _add_per_crate_rustc_flags(ctx, args, map_flag, crate_info, per_crate_rustc_
2312
2328
label = label_string [2 :]
2313
2329
else :
2314
2330
label = label_string
2315
- execution_path = crate_info . root .path
2331
+ execution_path = crate_root .path
2316
2332
2317
2333
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
2322
2337
2323
2338
def _error_format_impl (ctx ):
2324
2339
"""Implementation of the `error_format` rule
0 commit comments