1- load ("@io_bazel_rules_rust//rust:private/rustc.bzl" , "BuildInfo" , "DepInfo" , "get_compilation_mode_opts" )
1+ load ("@io_bazel_rules_rust//rust:private/rustc.bzl" , "BuildInfo" , "DepInfo" , "get_compilation_mode_opts" , "get_linker_and_args" )
22load ("@io_bazel_rules_rust//rust:private/utils.bzl" , "find_toolchain" )
33load ("@io_bazel_rules_rust//rust:rust.bzl" , "rust_binary" )
44load ("@bazel_tools//tools/cpp:toolchain_utils.bzl" , "find_cpp_toolchain" )
@@ -20,6 +20,14 @@ def _cargo_build_script_run(ctx, script):
2020 crate_name = crate_name .replace ("_build_script" , "" )
2121 crate_name = crate_name .replace ("_" , "-" )
2222
23+ toolchain_tools = [
24+ # Needed for rustc to function.
25+ toolchain .rustc_lib .files ,
26+ toolchain .rust_lib .files ,
27+ ]
28+
29+ cc_toolchain = find_cpp_toolchain (ctx )
30+
2331 env = {
2432 "CARGO_CFG_TARGET_ARCH" : toolchain .target_arch ,
2533 "CARGO_MANIFEST_DIR" : manifest_dir ,
@@ -31,10 +39,15 @@ def _cargo_build_script_run(ctx, script):
3139 "TARGET" : toolchain .target_triple ,
3240 }
3341
34- cc_toolchain = find_cpp_toolchain (ctx )
42+ # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
43+ # We hope that the linker env is sufficient for the whole cc_toolchain.
44+ _ , _ , linker_env = get_linker_and_args (ctx , None )
45+ env .update (** linker_env )
46+
3547 cc_executable = cc_toolchain and cc_toolchain .compiler_executable
3648 if cc_executable :
3749 env ["CC" ] = cc_executable
50+ toolchain_tools .append (cc_toolchain .all_files )
3851
3952 for f in ctx .attr .crate_features :
4053 env ["CARGO_FEATURE_" + f .upper ().replace ("-" , "_" )] = "1"
@@ -45,11 +58,7 @@ def _cargo_build_script_run(ctx, script):
4558 ctx .executable ._cargo_build_script_runner ,
4659 toolchain .rustc ,
4760 ],
48- transitive = [
49- # Needed for rustc to function.
50- toolchain .rustc_lib .files ,
51- toolchain .rust_lib .files ,
52- ],
61+ transitive = toolchain_tools ,
5362 )
5463
5564 # dep_env_file contains additional environment variables coming from
@@ -109,6 +118,7 @@ _build_script_run = rule(
109118 ),
110119 "deps" : attr .label_list (),
111120 },
121+ fragments = ["cpp" ],
112122 toolchains = [
113123 "@io_bazel_rules_rust//rust:toolchain" ,
114124 "@bazel_tools//tools/cpp:toolchain_type" ,
0 commit comments