Skip to content

Commit 09e1a1d

Browse files
committed
Add support for optional cc_toolchain
1 parent aa6e74e commit 09e1a1d

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
@@ -49,6 +49,10 @@ build:unpretty --output_groups=+rust_unpretty
4949
# https://github.com/rust-lang/rust/issues/43364
5050
build:unpretty --config=nightly
5151

52+
# Disable cc toolchains to test rust targets can be built without one.
53+
build:no_cc_toolchain --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
54+
build:no_cc_toolchain --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
55+
5256
###############################################################################
5357
## Incompatibility flags
5458
###############################################################################

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")
@@ -373,8 +372,6 @@ def _cargo_build_script_impl(ctx):
373372

374373
toolchain_tools = [toolchain.all_files]
375374

376-
cc_toolchain = find_cpp_toolchain(ctx)
377-
378375
env = {}
379376

380377
if ctx.attr.use_default_shell_env == -1:
@@ -423,18 +420,25 @@ def _cargo_build_script_impl(ctx):
423420
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
424421
# We hope that the linker env is sufficient for the whole cc_toolchain.
425422
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
426-
linker, link_args, linker_env = get_linker_and_args(ctx, "bin", cc_toolchain, feature_configuration, None)
423+
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None)
427424
env.update(**linker_env)
428425
env["LD"] = linker
429426
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
430427

431-
# MSVC requires INCLUDE to be set
432-
cc_c_args, cc_cxx_args, cc_ar_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
433-
include = cc_env.get("INCLUDE")
434-
if include:
435-
env["INCLUDE"] = include
428+
# Defaults for cxx flags.
429+
env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path)
430+
env["CXX"] = "${{pwd}}/{}".format(ctx.executable._fallback_cxx.path)
431+
env["AR"] = "${{pwd}}/{}".format(ctx.executable._fallback_ar.path)
432+
env["CFLAGS"] = ""
433+
env["CXXFLAGS"] = ""
436434

437435
if cc_toolchain:
436+
# MSVC requires INCLUDE to be set
437+
cc_c_args, cc_cxx_args, cc_ar_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
438+
include = cc_env.get("INCLUDE")
439+
if include:
440+
env["INCLUDE"] = include
441+
438442
toolchain_tools.append(cc_toolchain.all_files)
439443

440444
env["CC"] = cc_common.get_tool_for_action(
@@ -515,6 +519,9 @@ def _cargo_build_script_impl(ctx):
515519
direct = [
516520
script,
517521
ctx.executable._cargo_build_script_runner,
522+
ctx.executable._fallback_cc,
523+
ctx.executable._fallback_cxx,
524+
ctx.executable._fallback_ar,
518525
] + ([toolchain.target_json] if toolchain.target_json else []),
519526
transitive = script_data + script_tools + toolchain_tools,
520527
)
@@ -737,14 +744,29 @@ cargo_build_script = rule(
737744
"_experimental_symlink_execroot": attr.label(
738745
default = Label("//cargo/settings:experimental_symlink_execroot"),
739746
),
747+
"_fallback_ar": attr.label(
748+
cfg = "exec",
749+
executable = True,
750+
default = Label("//cargo/private:no_ar"),
751+
),
752+
"_fallback_cc": attr.label(
753+
cfg = "exec",
754+
executable = True,
755+
default = Label("//cargo/private:no_cc"),
756+
),
757+
"_fallback_cxx": attr.label(
758+
cfg = "exec",
759+
executable = True,
760+
default = Label("//cargo/private:no_cxx"),
761+
),
740762
"_incompatible_runfiles_cargo_manifest_dir": attr.label(
741763
default = Label("//cargo/settings:incompatible_runfiles_cargo_manifest_dir"),
742764
),
743765
},
744766
fragments = ["cpp"],
745767
toolchains = [
746768
str(Label("//rust:toolchain_type")),
747-
"@bazel_tools//tools/cpp:toolchain_type",
769+
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
748770
],
749771
)
750772

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)