Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ build:unpretty --output_groups=+rust_unpretty
# https://github.com/rust-lang/rust/issues/43364
build:unpretty --config=nightly

# Disable cc toolchains to test rust targets can be built without one.
build:no_cc_toolchain --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
build:no_cc_toolchain --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1

###############################################################################
## Incompatibility flags
###############################################################################
Expand Down
30 changes: 30 additions & 0 deletions cargo/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ rust_binary(
visibility = ["//visibility:public"],
)

rust_binary(
name = "no_ar",
srcs = ["no_binary.rs"],
edition = "2021",
rustc_env = {
"BINARY_ENV": "AR",
},
visibility = ["//visibility:public"],
)

rust_binary(
name = "no_cc",
srcs = ["no_binary.rs"],
edition = "2021",
rustc_env = {
"BINARY_ENV": "CC",
},
visibility = ["//visibility:public"],
)

rust_binary(
name = "no_cxx",
srcs = ["no_binary.rs"],
edition = "2021",
rustc_env = {
"BINARY_ENV": "CXX",
},
visibility = ["//visibility:public"],
)

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
Expand Down
42 changes: 32 additions & 10 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
load("@rules_cc//cc:find_cc_toolchain.bzl", find_cpp_toolchain = "find_cc_toolchain")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("//rust:defs.bzl", "rust_common")
load("//rust:rust_common.bzl", "BuildInfo", "CrateGroupInfo", "DepInfo")
Expand Down Expand Up @@ -367,8 +366,6 @@ def _cargo_build_script_impl(ctx):

toolchain_tools = [toolchain.all_files]

cc_toolchain = find_cpp_toolchain(ctx)

env = {}

if ctx.attr.use_default_shell_env == -1:
Expand Down Expand Up @@ -417,18 +414,25 @@ def _cargo_build_script_impl(ctx):
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
# We hope that the linker env is sufficient for the whole cc_toolchain.
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
linker, link_args, linker_env = get_linker_and_args(ctx, "bin", cc_toolchain, feature_configuration, None)
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None)
env.update(**linker_env)
env["LD"] = linker
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))

# MSVC requires INCLUDE to be set
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
include = cc_env.get("INCLUDE")
if include:
env["INCLUDE"] = include
# Defaults for cxx flags.
env["CC"] = "${{pwd}}/{}".format(ctx.executable._fallback_cc.path)
env["CXX"] = "${{pwd}}/{}".format(ctx.executable._fallback_cxx.path)
env["AR"] = "${{pwd}}/{}".format(ctx.executable._fallback_ar.path)
env["CFLAGS"] = ""
env["CXXFLAGS"] = ""

if cc_toolchain:
# MSVC requires INCLUDE to be set
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
include = cc_env.get("INCLUDE")
if include:
env["INCLUDE"] = include

toolchain_tools.append(cc_toolchain.all_files)

env["CC"] = cc_common.get_tool_for_action(
Expand Down Expand Up @@ -503,6 +507,9 @@ def _cargo_build_script_impl(ctx):
direct = [
script,
ctx.executable._cargo_build_script_runner,
ctx.executable._fallback_cc,
ctx.executable._fallback_cxx,
ctx.executable._fallback_ar,
] + ([toolchain.target_json] if toolchain.target_json else []),
transitive = script_data + script_tools + toolchain_tools,
)
Expand Down Expand Up @@ -725,14 +732,29 @@ cargo_build_script = rule(
"_experimental_symlink_execroot": attr.label(
default = Label("//cargo/settings:experimental_symlink_execroot"),
),
"_fallback_ar": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private:no_ar"),
),
"_fallback_cc": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private:no_cc"),
),
"_fallback_cxx": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private:no_cxx"),
),
"_incompatible_runfiles_cargo_manifest_dir": attr.label(
default = Label("//cargo/settings:incompatible_runfiles_cargo_manifest_dir"),
),
},
fragments = ["cpp"],
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
)

Expand Down
6 changes: 6 additions & 0 deletions cargo/private/no_binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! A cross platform implementation of `/bin/false`

fn main() {
eprintln!(concat!("No binary provided for ", env!("BINARY_ENV")));
std::process::exit(1);
}
2 changes: 1 addition & 1 deletion extensions/bindgen/private/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def _rust_bindgen_impl(ctx):
for define in ctx.attr.cc_lib[CcInfo].compilation_context.defines.to_list():
args.add("-D" + define)

_, _, linker_env = get_linker_and_args(ctx, "bin", cc_toolchain, feature_configuration, None)
_, _, _, linker_env = get_linker_and_args(ctx, "bin", rust_toolchain, cc_toolchain, feature_configuration, None)
env.update(**linker_env)

# Set the dynamic linker search path so that clang uses the libstdcxx from the toolchain.
Expand Down
2 changes: 1 addition & 1 deletion extensions/prost/private/prost.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ rust_prost_aspect = aspect(
fragments = ["cpp"],
toolchains = [
TOOLCHAIN_TYPE,
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
"@rules_rust//rust:toolchain_type",
"@rules_rust//rust/rustfmt:toolchain_type",
],
Expand Down
4 changes: 2 additions & 2 deletions extensions/protobuf/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ rust_proto_library = rule(
toolchains = [
str(Label("//:toolchain_type")),
str(Label("@rules_rust//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
doc = """\
Builds a Rust library crate from a set of `proto_library`s.
Expand Down Expand Up @@ -435,7 +435,7 @@ rust_grpc_library = rule(
toolchains = [
str(Label("//:toolchain_type")),
str(Label("@rules_rust//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
doc = """\
Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.
Expand Down
5 changes: 1 addition & 4 deletions extensions/wasm_bindgen/private/wasm_bindgen_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ load("@rules_rust//rust/private:rust.bzl", "RUSTC_ATTRS", "get_rust_test_flags")
# buildifier: disable=bzl-visibility
load("@rules_rust//rust/private:rustc.bzl", "rustc_compile_action")

# buildifier: disable=bzl-visibility
# load("@rules_rust//rust/private:toolchain_utils.bzl", "get_coverage_env")

# buildifier: disable=bzl-visibility
load(
"@rules_rust//rust/private:utils.bzl",
Expand Down Expand Up @@ -334,7 +331,7 @@ rust_wasm_bindgen_test = rule(
toolchains = [
str(Label("//:toolchain_type")),
"@rules_rust//rust:toolchain_type",
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
test = True,
)
2 changes: 1 addition & 1 deletion ffi/rs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")

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

rust_allocator_libraries(
name = "allocator_libraries_with_mangling_support",
Expand Down
16 changes: 16 additions & 0 deletions rust/platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ constraint_value(
constraint_setting = ":wasi_version",
)

# ABI constraint settings
constraint_setting(
name = "abi",
default_constraint_value = ":gnu",
)

constraint_value(
name = "gnu",
constraint_setting = ":abi",
)

constraint_value(
name = "musl",
constraint_setting = ":abi",
)

package_group(
name = "function_transition_allowlist",
packages = [
Expand Down
1 change: 1 addition & 0 deletions rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = {
"x86_64-linux-android": _support(std = True, host_tools = False),
"x86_64-unknown-freebsd": _support(std = True, host_tools = True),
"x86_64-unknown-fuchsia": _support(std = True, host_tools = False),
"x86_64-unknown-linux-musl": _support(std = True, host_tools = True),
"x86_64-unknown-none": _support(std = True, host_tools = False),
"x86_64-unknown-uefi": _support(std = True, host_tools = False),
}
Expand Down
2 changes: 1 addition & 1 deletion rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ rust_clippy_aspect = aspect(
],
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
],
implementation = _clippy_aspect_impl,
doc = """\
Expand Down
Loading