2121 "deduplicate" ,
2222 "expand_dict_value_locations" ,
2323 "find_cc_toolchain" ,
24+ "find_optional_toolchain" ,
2425 "find_toolchain" ,
2526 _name_to_crate_name = "name_to_crate_name" ,
2627)
@@ -315,14 +316,15 @@ def _cargo_build_script_impl(ctx):
315316 """
316317 script = ctx .executable .script
317318 script_info = ctx .attr .script [CargoBuildScriptRunfilesInfo ]
318- toolchain = find_toolchain (ctx )
319+ rustc_toolchain = find_toolchain (ctx )
320+ cargo_toolchain = find_optional_toolchain (ctx , "@rust_toolchains//cargo:toolchain_type" ) or rustc_toolchain
319321 out_dir = ctx .actions .declare_directory (ctx .label .name + ".out_dir" )
320322 env_out = ctx .actions .declare_file (ctx .label .name + ".env" )
321323 dep_env_out = ctx .actions .declare_file (ctx .label .name + ".depenv" )
322324 flags_out = ctx .actions .declare_file (ctx .label .name + ".flags" )
323325 link_flags = ctx .actions .declare_file (ctx .label .name + ".linkflags" )
324326 link_search_paths = ctx .actions .declare_file (ctx .label .name + ".linksearchpaths" ) # rustc-link-search, propagated from transitive dependencies
325- compilation_mode_opt_level = get_compilation_mode_opts (ctx , toolchain ).opt_level
327+ compilation_mode_opt_level = get_compilation_mode_opts (ctx , rustc_toolchain ).opt_level
326328
327329 script_tools = []
328330 script_data = []
@@ -364,7 +366,7 @@ def _cargo_build_script_impl(ctx):
364366 if pkg_name == "" :
365367 pkg_name = name_to_pkg_name (ctx .label .name )
366368
367- toolchain_tools = [toolchain .all_files ]
369+ toolchain_tools = [rustc_toolchain .all_files ]
368370
369371 env = {}
370372
@@ -380,19 +382,19 @@ def _cargo_build_script_impl(ctx):
380382 if use_default_shell_env :
381383 env .update (ctx .configuration .default_shell_env )
382384
383- if toolchain .cargo :
384- env ["CARGO" ] = "${pwd}/%s" % toolchain .cargo .path
385+ if cargo_toolchain .cargo :
386+ env ["CARGO" ] = "${pwd}/%s" % cargo_toolchain .cargo .path
385387
386388 env .update ({
387389 "CARGO_CRATE_NAME" : name_to_crate_name (pkg_name ),
388390 "CARGO_MANIFEST_DIR" : manifest_dir ,
389391 "CARGO_PKG_NAME" : pkg_name ,
390- "HOST" : toolchain .exec_triple .str ,
392+ "HOST" : rustc_toolchain .exec_triple .str ,
391393 "NUM_JOBS" : "1" ,
392394 "OPT_LEVEL" : compilation_mode_opt_level ,
393- "RUSTC" : toolchain .rustc .path ,
394- "RUSTDOC" : toolchain .rust_doc .path ,
395- "TARGET" : toolchain .target_flag_value ,
395+ "RUSTC" : rustc_toolchain .rustc .path ,
396+ "RUSTDOC" : rustc_toolchain .rust_doc .path ,
397+ "TARGET" : rustc_toolchain .target_flag_value ,
396398 # OUT_DIR is set by the runner itself, rather than on the action.
397399 })
398400
@@ -414,7 +416,7 @@ def _cargo_build_script_impl(ctx):
414416 # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
415417 # We hope that the linker env is sufficient for the whole cc_toolchain.
416418 cc_toolchain , feature_configuration = find_cc_toolchain (ctx )
417- linker , _ , link_args , linker_env = get_linker_and_args (ctx , "bin" , toolchain , cc_toolchain , feature_configuration , None )
419+ linker , _ , link_args , linker_env = get_linker_and_args (ctx , "bin" , rustc_toolchain , cc_toolchain , feature_configuration , None )
418420 env .update (** linker_env )
419421 env ["LD" ] = linker
420422 env ["LDFLAGS" ] = " " .join (_pwd_flags (link_args ))
@@ -477,7 +479,7 @@ def _cargo_build_script_impl(ctx):
477479 # https://github.com/rust-lang/cargo/issues/9600
478480 env ["CARGO_ENCODED_RUSTFLAGS" ] = "\\ x1f" .join ([
479481 # Allow build scripts to locate the generated sysroot
480- "--sysroot=${{pwd}}/{}" .format (toolchain .sysroot ),
482+ "--sysroot=${{pwd}}/{}" .format (rustc_toolchain .sysroot ),
481483 ] + ctx .attr .rustc_flags )
482484
483485 for f in ctx .attr .crate_features :
@@ -488,7 +490,7 @@ def _cargo_build_script_impl(ctx):
488490 env ["CARGO_MANIFEST_LINKS" ] = links
489491
490492 # Add environment variables from the Rust toolchain.
491- env .update (toolchain .env )
493+ env .update (rustc_toolchain .env )
492494
493495 known_variables = {}
494496
@@ -526,7 +528,7 @@ def _cargo_build_script_impl(ctx):
526528 direct = [
527529 script ,
528530 ctx .executable ._cargo_build_script_runner ,
529- ] + fallback_tools + ([toolchain .target_json ] if toolchain .target_json else []),
531+ ] + fallback_tools + ([rustc_toolchain .target_json ] if rustc_toolchain .target_json else []),
530532 transitive = script_data + script_tools + toolchain_tools ,
531533 )
532534
@@ -772,7 +774,8 @@ cargo_build_script = rule(
772774 },
773775 fragments = ["cpp" ],
774776 toolchains = [
775- str (Label ("//rust:toolchain_type" )),
777+ config_common .toolchain_type ("@rust_toolchains//cargo:toolchain_type" , mandatory = False ),
778+ str (Label ("@rust_toolchains//rustc:toolchain_type" )),
776779 config_common .toolchain_type ("@bazel_tools//tools/cpp:toolchain_type" , mandatory = False ),
777780 ],
778781)
0 commit comments