@@ -367,9 +367,7 @@ def _cargo_build_script_impl(ctx):
367367
368368 toolchain_tools = [toolchain .all_files ]
369369
370- cc_toolchain = find_cpp_toolchain (ctx )
371-
372- env = dict ({})
370+ env = {}
373371
374372 if ctx .attr .use_default_shell_env == - 1 :
375373 use_default_shell_env = ctx .attr ._default_use_default_shell_env [BuildSettingInfo ].value
@@ -415,41 +413,42 @@ def _cargo_build_script_impl(ctx):
415413 env ["CARGO_PKG_VERSION_PRE" ] = patch [1 ] if len (patch ) > 1 else ""
416414 env ["CARGO_PKG_VERSION" ] = ctx .attr .version
417415
418- # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
419- # We hope that the linker env is sufficient for the whole cc_toolchain.
420- cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
421- linker , link_args , linker_env = get_linker_and_args (ctx , "bin" , cc_toolchain , feature_configuration , None )
422- env .update (** linker_env )
423- env ["LD" ] = linker
424- env ["LDFLAGS" ] = " " .join (_pwd_flags (link_args ))
425-
426- # MSVC requires INCLUDE to be set
427- cc_c_args , cc_cxx_args , cc_env = get_cc_compile_args_and_env (cc_toolchain , feature_configuration )
428- include = cc_env .get ("INCLUDE" )
429- if include :
430- env ["INCLUDE" ] = include
431-
432- if cc_toolchain :
433- toolchain_tools .append (cc_toolchain .all_files )
434-
435- env ["CC" ] = cc_common .get_tool_for_action (
436- feature_configuration = feature_configuration ,
437- action_name = ACTION_NAMES .c_compile ,
438- )
439- env ["CXX" ] = cc_common .get_tool_for_action (
440- feature_configuration = feature_configuration ,
441- action_name = ACTION_NAMES .cpp_compile ,
442- )
443- env ["AR" ] = cc_common .get_tool_for_action (
444- feature_configuration = feature_configuration ,
445- action_name = ACTION_NAMES .cpp_link_static_library ,
446- )
447-
448- # Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
449- # to determine the deployment target when building for apple platforms (`macosx-version-min`
450- # for example, itself derived from the `macos_minimum_os` Bazel argument).
451- env ["CFLAGS" ] = " " .join (_pwd_flags (cc_c_args ))
452- env ["CXXFLAGS" ] = " " .join (_pwd_flags (cc_cxx_args ))
416+ if ctx .attr .add_cc_toolchain :
417+ # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
418+ # We hope that the linker env is sufficient for the whole cc_toolchain.
419+ cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
420+ linker , link_args , linker_env = get_linker_and_args (ctx , "bin" , cc_toolchain , feature_configuration , None )
421+ env .update (linker_env )
422+ env ["LD" ] = linker
423+ env ["LDFLAGS" ] = " " .join (_pwd_flags (link_args ))
424+
425+ # MSVC requires INCLUDE to be set
426+ cc_c_args , cc_cxx_args , cc_env = get_cc_compile_args_and_env (cc_toolchain , feature_configuration )
427+ include = cc_env .get ("INCLUDE" )
428+ if include :
429+ env ["INCLUDE" ] = include
430+
431+ if cc_toolchain :
432+ toolchain_tools .append (cc_toolchain .all_files )
433+
434+ env ["CC" ] = cc_common .get_tool_for_action (
435+ feature_configuration = feature_configuration ,
436+ action_name = ACTION_NAMES .c_compile ,
437+ )
438+ env ["CXX" ] = cc_common .get_tool_for_action (
439+ feature_configuration = feature_configuration ,
440+ action_name = ACTION_NAMES .cpp_compile ,
441+ )
442+ env ["AR" ] = cc_common .get_tool_for_action (
443+ feature_configuration = feature_configuration ,
444+ action_name = ACTION_NAMES .cpp_link_static_library ,
445+ )
446+
447+ # Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
448+ # to determine the deployment target when building for apple platforms (`macosx-version-min`
449+ # for example, itself derived from the `macos_minimum_os` Bazel argument).
450+ env ["CFLAGS" ] = " " .join (_pwd_flags (cc_c_args ))
451+ env ["CXXFLAGS" ] = " " .join (_pwd_flags (cc_cxx_args ))
453452
454453 # Inform build scripts of rustc flags
455454 # https://github.com/rust-lang/cargo/issues/9600
@@ -618,6 +617,7 @@ cargo_build_script = rule(
618617 ),
619618 implementation = _cargo_build_script_impl ,
620619 attrs = {
620+ "add_cc_toolchain" : attr .bool (default = True ),
621621 "build_script_env" : attr .string_dict (
622622 doc = "Environment variables for build scripts." ,
623623 ),
0 commit comments