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