Skip to content

Forward CXX env and arguments from cargo_build_script #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

Google Inc.
Spotify AB
VMware Inc.
Damien Martin-Guillerez <[email protected]>
David Chen <[email protected]>
Florian Weikert <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Philipp Wollermann <[email protected]>
Ulf Adams <[email protected]>
Justine Alexandra Roberts Tunney <[email protected]>
John Edmonds <[email protected]>
Ivan Kalchev <[email protected]>
49 changes: 48 additions & 1 deletion cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# buildifier: disable=module-docstring
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"CPP_COMPILE_ACTION_NAME",
"C_COMPILE_ACTION_NAME",
)
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//rust:defs.bzl", "rust_binary", "rust_common")

Expand Down Expand Up @@ -29,6 +33,36 @@ def get_cc_compile_env(cc_toolchain, feature_configuration):
variables = compile_variables,
)

def get_cxx_compile_opts(cc_toolchain, feature_configuration):
"""Gather command line arguments and env from the given ``cc_toolchain``.

Args:
cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`.
feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features.

Returns:
tuple: Returns the command line arguments as the first element and the
env as the second element.
"""
compile_variables = cc_common.create_compile_variables(
cc_toolchain = cc_toolchain,
feature_configuration = feature_configuration,
)

compile_args_from_toolchain = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
variables = compile_variables,
)

cc_compile_env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = CPP_COMPILE_ACTION_NAME,
variables = compile_variables,
)

return (compile_args_from_toolchain, cc_compile_env)

def _build_script_impl(ctx):
"""The implementation for the `_build_script_run` rule.

Expand Down Expand Up @@ -101,6 +135,19 @@ def _build_script_impl(ctx):
if include:
env["INCLUDE"] = include

compile_args_from_toolchain, _ = get_cxx_compile_opts(cc_toolchain, feature_configuration)

# XXX: Skip adding the CXX env since we already added the C env. Should we stick to just CXX?
# Remove --sysroot since we pass it in the SYSROOT env var (we need to
# use the absolute path for it). See ``cargo_build_script_runner/bin.rs``
# for how this is handled.
compile_args_from_toolchain = [
arg
for arg in compile_args_from_toolchain
if not arg.startswith("--sysroot")
]
env["CXXFLAGS"] = " ".join(compile_args_from_toolchain)

if cc_toolchain:
toolchain_tools.append(cc_toolchain.all_files)

Expand Down
5 changes: 4 additions & 1 deletion test/cargo_build_script/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ load("//rust:defs.bzl", "rust_test")
cargo_build_script(
name = "tools_exec_build_rs",
srcs = ["build.rs"],
build_script_env = {"TOOL": "$(execpath :tool)"},
build_script_env = {
"CXXFLAGS": "-DMY_DEFINE",
"TOOL": "$(execpath :tool)",
},
tools = [":tool"],
)

Expand Down
4 changes: 4 additions & 0 deletions test/cargo_build_script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ fn main() {
"cargo:rustc-env=TOOL_PATH={}",
std::env::var("TOOL").unwrap()
);
println!(
"cargo:rustc-env=CXXFLAGS={}",
std::env::var("CXXFLAGS").unwrap()
);

// Assert that the CC and CXX env vars existed and were executable.
// We don't assert what happens when they're executed (in particular, we don't check for a
Expand Down
10 changes: 10 additions & 0 deletions test/cargo_build_script/tools_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ pub fn test_tool_exec() {
tool_path
);
}

#[test]
pub fn test_cxxflags() {
let cxxflags = env!("CXXFLAGS");
assert!(
cxxflags.contains("-DMY_DEFINE"),
"CXXFLAGS did not contain '-DMY_DEFINE', {}",
cxxflags
);
}