@@ -286,6 +286,24 @@ def _feature_enabled(ctx, feature_name, default = False):
286286
287287 return default
288288
289+ def _resolve_tristate (attr_value , default_flag_target ):
290+ """Resolve a tri-state `int` attribute (`-1`/`0`/`1`) to a `bool`.
291+
292+ `-1` defers to the `BuildSettingInfo` on `default_flag_target`; any other
293+ value is treated as truthy/falsy directly.
294+
295+ Args:
296+ attr_value (int): The tri-state attribute value (`-1`, `0`, or `1`).
297+ default_flag_target (Target): The `bool_flag` target providing the
298+ default when `attr_value` is `-1`.
299+
300+ Returns:
301+ bool: The resolved value.
302+ """
303+ if attr_value == - 1 :
304+ return default_flag_target [BuildSettingInfo ].value
305+ return bool (attr_value )
306+
289307def _rlocationpath (file , workspace_name ):
290308 if file .short_path .startswith ("../" ):
291309 return file .short_path [len ("../" ):]
@@ -390,12 +408,10 @@ def _cargo_build_script_impl(ctx):
390408
391409 env = {}
392410
393- if ctx .attr .use_default_shell_env == - 1 :
394- use_default_shell_env = ctx .attr ._default_use_default_shell_env [BuildSettingInfo ].value
395- elif ctx .attr .use_default_shell_env == 0 :
396- use_default_shell_env = False
397- else :
398- use_default_shell_env = True
411+ use_default_shell_env = _resolve_tristate (
412+ ctx .attr .use_default_shell_env ,
413+ ctx .attr ._default_use_default_shell_env ,
414+ )
399415
400416 # If enabled, start with the default shell env, which contains any --action_env
401417 # settings passed in on the command line and defaults like $PATH.
@@ -433,9 +449,17 @@ def _cargo_build_script_impl(ctx):
433449 env ["CARGO_PKG_VERSION_PRE" ] = patch [1 ] if len (patch ) > 1 else ""
434450 env ["CARGO_PKG_VERSION" ] = ctx .attr .version
435451
452+ use_cc_toolchain = _resolve_tristate (
453+ ctx .attr .use_cc_toolchain ,
454+ ctx .attr ._default_use_cc_toolchain ,
455+ )
456+
436457 # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
437458 # We hope that the linker env is sufficient for the whole cc_toolchain.
438- cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
459+ if use_cc_toolchain :
460+ cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
461+ else :
462+ cc_toolchain , feature_configuration = None , None
439463 linker , _ , link_args , linker_env = get_linker_and_args (ctx , "bin" , toolchain , cc_toolchain , feature_configuration , None )
440464 env .update (** linker_env )
441465 env ["LD" ] = linker
@@ -784,6 +808,31 @@ cargo_build_script = rule(
784808 allow_files = True ,
785809 cfg = "exec" ,
786810 ),
811+ "use_cc_toolchain" : attr .int (
812+ doc = dedent ("""\
813+ Whether or not to pull in the resolved `cc_toolchain` when
814+ running the build script.
815+
816+ When enabled, the resolved `cc_toolchain`'s `all_files` are
817+ added to the action inputs and the `CC`, `CXX`, `AR`,
818+ `CFLAGS`, `CXXFLAGS`, `LDFLAGS`, and `INCLUDE` environment
819+ variables are populated from that toolchain (matching Cargo's
820+ normal behavior).
821+
822+ When disabled, the `cc_toolchain` is not requested for the
823+ build script action. This can significantly shrink the input
824+ trees of `cargo_build_script` actions (particularly with
825+ hermetic sysroots) but breaks any build script that needs to
826+ compile C/C++ code.
827+
828+ Unset (`-1`, the default) defers to the
829+ `@rules_rust//cargo/settings:use_cc_toolchain` build setting
830+ which itself defaults to enabled. Set to `1` to force enable
831+ or `0` to force disable for a specific target.
832+ """ ),
833+ default = - 1 ,
834+ values = [- 1 , 0 , 1 ],
835+ ),
787836 "use_default_shell_env" : attr .int (
788837 doc = dedent ("""\
789838 Whether or not to include the default shell environment for the build
@@ -808,6 +857,9 @@ cargo_build_script = rule(
808857 "_debug_std_streams_output_group" : attr .label (
809858 default = Label ("//cargo/settings:debug_std_streams_output_group" ),
810859 ),
860+ "_default_use_cc_toolchain" : attr .label (
861+ default = Label ("//cargo/settings:use_cc_toolchain" ),
862+ ),
811863 "_default_use_default_shell_env" : attr .label (
812864 default = Label ("//cargo/settings:use_default_shell_env" ),
813865 ),
0 commit comments