Skip to content

Commit ce56648

Browse files
committed
Allow opting out of cc toolchain for cargo_build_script
1 parent 8e38d48 commit ce56648

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
load("@bazel_skylib//lib:paths.bzl", "paths")
44
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
55
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
6-
load("@rules_cc//cc:find_cc_toolchain.bzl", find_cpp_toolchain = "find_cc_toolchain")
76
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
87
load("//rust:defs.bzl", "rust_common")
98
load("//rust:rust_common.bzl", "BuildInfo", "CrateGroupInfo", "DepInfo")
@@ -367,8 +366,6 @@ def _cargo_build_script_impl(ctx):
367366

368367
toolchain_tools = [toolchain.all_files]
369368

370-
cc_toolchain = find_cpp_toolchain(ctx)
371-
372369
env = {}
373370

374371
if ctx.attr.use_default_shell_env == -1:
@@ -414,41 +411,42 @@ def _cargo_build_script_impl(ctx):
414411
env["CARGO_PKG_VERSION_PRE"] = patch[1] if len(patch) > 1 else ""
415412
env["CARGO_PKG_VERSION"] = ctx.attr.version
416413

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))
414+
if ctx.attr.add_cc_toolchain:
415+
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
416+
# We hope that the linker env is sufficient for the whole cc_toolchain.
417+
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
418+
linker, link_args, linker_env = get_linker_and_args(ctx, "bin", cc_toolchain, feature_configuration, None)
419+
env.update(linker_env)
420+
env["LD"] = linker
421+
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
422+
423+
# MSVC requires INCLUDE to be set
424+
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
425+
include = cc_env.get("INCLUDE")
426+
if include:
427+
env["INCLUDE"] = include
428+
429+
if cc_toolchain:
430+
toolchain_tools.append(cc_toolchain.all_files)
431+
432+
env["CC"] = cc_common.get_tool_for_action(
433+
feature_configuration = feature_configuration,
434+
action_name = ACTION_NAMES.c_compile,
435+
)
436+
env["CXX"] = cc_common.get_tool_for_action(
437+
feature_configuration = feature_configuration,
438+
action_name = ACTION_NAMES.cpp_compile,
439+
)
440+
env["AR"] = cc_common.get_tool_for_action(
441+
feature_configuration = feature_configuration,
442+
action_name = ACTION_NAMES.cpp_link_static_library,
443+
)
444+
445+
# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
446+
# to determine the deployment target when building for apple platforms (`macosx-version-min`
447+
# for example, itself derived from the `macos_minimum_os` Bazel argument).
448+
env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args))
449+
env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args))
452450

453451
# Inform build scripts of rustc flags
454452
# https://github.com/rust-lang/cargo/issues/9600
@@ -617,6 +615,7 @@ cargo_build_script = rule(
617615
),
618616
implementation = _cargo_build_script_impl,
619617
attrs = {
618+
"add_cc_toolchain": attr.bool(default = True),
620619
"build_script_env": attr.string_dict(
621620
doc = "Environment variables for build scripts.",
622621
),

0 commit comments

Comments
 (0)