Skip to content

Commit 95a3d34

Browse files
committed
Add support for optional cc_toolchain
1 parent 7439f60 commit 95a3d34

File tree

29 files changed

+1453
-563
lines changed

29 files changed

+1453
-563
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ build:unpretty --output_groups=+rust_unpretty
4646
# https://github.com/rust-lang/rust/issues/43364
4747
build:unpretty --config=nightly
4848

49+
# Disable cc toolchains to test rust targets can be built without one.
50+
build:no_cc_toolchain --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
51+
build:no_cc_toolchain --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
52+
4953
###############################################################################
5054
## Incompatibility flags
5155
###############################################################################

cargo/private/BUILD.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@ rust_binary(
88
visibility = ["//visibility:public"],
99
)
1010

11+
rust_binary(
12+
name = "no_ar",
13+
srcs = ["no_binary.rs"],
14+
edition = "2021",
15+
rustc_env = {
16+
"BINARY_ENV": "AR",
17+
},
18+
visibility = ["//visibility:public"],
19+
)
20+
21+
rust_binary(
22+
name = "no_cc",
23+
srcs = ["no_binary.rs"],
24+
edition = "2021",
25+
rustc_env = {
26+
"BINARY_ENV": "CC",
27+
},
28+
visibility = ["//visibility:public"],
29+
)
30+
31+
rust_binary(
32+
name = "no_cxx",
33+
srcs = ["no_binary.rs"],
34+
edition = "2021",
35+
rustc_env = {
36+
"BINARY_ENV": "CXX",
37+
},
38+
visibility = ["//visibility:public"],
39+
)
40+
1141
bzl_library(
1242
name = "bzl_lib",
1343
srcs = glob(["**/*.bzl"]),

cargo/private/cargo_build_script.bzl

Lines changed: 32 additions & 10 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:
@@ -417,18 +414,25 @@ def _cargo_build_script_impl(ctx):
417414
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
418415
# We hope that the linker env is sufficient for the whole cc_toolchain.
419416
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)
417+
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None)
421418
env.update(**linker_env)
422419
env["LD"] = linker
423420
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
424421

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
422+
# Defaults for cxx flags.
423+
env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path)
424+
env["CXX"] = "${{pwd}}/{}".format(ctx.executable._fallback_cxx.path)
425+
env["AR"] = "${{pwd}}/{}".format(ctx.executable._fallback_ar.path)
426+
env["CFLAGS"] = ""
427+
env["CXXFLAGS"] = ""
430428

431429
if cc_toolchain:
430+
# MSVC requires INCLUDE to be set
431+
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
432+
include = cc_env.get("INCLUDE")
433+
if include:
434+
env["INCLUDE"] = include
435+
432436
toolchain_tools.append(cc_toolchain.all_files)
433437

434438
env["CC"] = cc_common.get_tool_for_action(
@@ -503,6 +507,9 @@ def _cargo_build_script_impl(ctx):
503507
direct = [
504508
script,
505509
ctx.executable._cargo_build_script_runner,
510+
ctx.executable._fallback_cc,
511+
ctx.executable._fallback_cxx,
512+
ctx.executable._fallback_ar,
506513
] + ([toolchain.target_json] if toolchain.target_json else []),
507514
transitive = script_data + script_tools + toolchain_tools,
508515
)
@@ -725,14 +732,29 @@ cargo_build_script = rule(
725732
"_experimental_symlink_execroot": attr.label(
726733
default = Label("//cargo/settings:experimental_symlink_execroot"),
727734
),
735+
"_fallback_ar": attr.label(
736+
cfg = "exec",
737+
executable = True,
738+
default = Label("//cargo/private:no_ar"),
739+
),
740+
"_fallback_cc": attr.label(
741+
cfg = "exec",
742+
executable = True,
743+
default = Label("//cargo/private:no_cc"),
744+
),
745+
"_fallback_cxx": attr.label(
746+
cfg = "exec",
747+
executable = True,
748+
default = Label("//cargo/private:no_cxx"),
749+
),
728750
"_incompatible_runfiles_cargo_manifest_dir": attr.label(
729751
default = Label("//cargo/settings:incompatible_runfiles_cargo_manifest_dir"),
730752
),
731753
},
732754
fragments = ["cpp"],
733755
toolchains = [
734756
str(Label("//rust:toolchain_type")),
735-
"@bazel_tools//tools/cpp:toolchain_type",
757+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
736758
],
737759
)
738760

cargo/private/no_binary.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! A cross platform implementation of `/bin/false`
2+
3+
fn main() {
4+
eprintln!(concat!("No binary provided for ", env!("BINARY_ENV")));
5+
std::process::exit(1);
6+
}

extensions/prost/private/prost.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ rust_prost_aspect = aspect(
392392
fragments = ["cpp"],
393393
toolchains = [
394394
TOOLCHAIN_TYPE,
395-
"@bazel_tools//tools/cpp:toolchain_type",
395+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
396396
"@rules_rust//rust:toolchain_type",
397397
"@rules_rust//rust/rustfmt:toolchain_type",
398398
],

extensions/protobuf/proto.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ rust_proto_library = rule(
343343
toolchains = [
344344
str(Label("//:toolchain_type")),
345345
str(Label("@rules_rust//rust:toolchain_type")),
346-
"@bazel_tools//tools/cpp:toolchain_type",
346+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
347347
],
348348
doc = """\
349349
Builds a Rust library crate from a set of `proto_library`s.
@@ -435,7 +435,7 @@ rust_grpc_library = rule(
435435
toolchains = [
436436
str(Label("//:toolchain_type")),
437437
str(Label("@rules_rust//rust:toolchain_type")),
438-
"@bazel_tools//tools/cpp:toolchain_type",
438+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
439439
],
440440
doc = """\
441441
Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.

extensions/wasm_bindgen/private/wasm_bindgen_test.bzl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ load("@rules_rust//rust/private:rust.bzl", "RUSTC_ATTRS", "get_rust_test_flags")
88
# buildifier: disable=bzl-visibility
99
load("@rules_rust//rust/private:rustc.bzl", "rustc_compile_action")
1010

11-
# buildifier: disable=bzl-visibility
12-
# load("@rules_rust//rust/private:toolchain_utils.bzl", "get_coverage_env")
13-
1411
# buildifier: disable=bzl-visibility
1512
load(
1613
"@rules_rust//rust/private:utils.bzl",
@@ -334,7 +331,7 @@ rust_wasm_bindgen_test = rule(
334331
toolchains = [
335332
str(Label("//:toolchain_type")),
336333
"@rules_rust//rust:toolchain_type",
337-
"@bazel_tools//tools/cpp:toolchain_type",
334+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
338335
],
339336
test = True,
340337
)

ffi/rs/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_cc//cc:cc_library.bzl", "cc_library")
22

33
# buildifier: disable=bzl-visibility
4-
load("@rules_rust//rust/private:rust.bzl", "rust_allocator_libraries")
4+
load("@rules_rust//rust/private:rust_allocator_libraries.bzl", "rust_allocator_libraries")
55

66
rust_allocator_libraries(
77
name = "allocator_libraries_with_mangling_support",

rust/platform/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ constraint_value(
2121
constraint_setting = ":wasi_version",
2222
)
2323

24+
# ABI constraint settings
25+
constraint_setting(
26+
name = "abi",
27+
default_constraint_value = ":gnu",
28+
)
29+
30+
constraint_value(
31+
name = "gnu",
32+
constraint_setting = ":abi",
33+
)
34+
35+
constraint_value(
36+
name = "musl",
37+
constraint_setting = ":abi",
38+
)
39+
2440
package_group(
2541
name = "function_transition_allowlist",
2642
packages = [

rust/platform/triple_mappings.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = {
6969
"x86_64-linux-android": _support(std = True, host_tools = False),
7070
"x86_64-unknown-freebsd": _support(std = True, host_tools = True),
7171
"x86_64-unknown-fuchsia": _support(std = True, host_tools = False),
72+
"x86_64-unknown-linux-musl": _support(std = True, host_tools = True),
7273
"x86_64-unknown-none": _support(std = True, host_tools = False),
7374
"x86_64-unknown-uefi": _support(std = True, host_tools = False),
7475
}

0 commit comments

Comments
 (0)