Skip to content

Commit 7b8cc91

Browse files
author
Ivan Kalchev
committed
Forward CXX env and arguments from cargo_build_script
We need to forward the C++ command line arguments and env to ``bin.rs`` to make sure cargo_build libraries are compiled with the same options as `cc_*` targets. Fixes bazelbuild#1003
1 parent 3cc41db commit 7b8cc91

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
Google Inc.
1010
Spotify AB
11+
VMware Inc.
1112
Damien Martin-Guillerez <[email protected]>
1213
David Chen <[email protected]>
1314
Florian Weikert <[email protected]>

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Philipp Wollermann <[email protected]>
2020
Ulf Adams <[email protected]>
2121
Justine Alexandra Roberts Tunney <[email protected]>
2222
John Edmonds <[email protected]>
23+
Ivan Kalchev <[email protected]>

cargo/cargo_build_script.bzl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# buildifier: disable=module-docstring
2-
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
2+
load(
3+
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
4+
"C_COMPILE_ACTION_NAME",
5+
"CPP_COMPILE_ACTION_NAME"
6+
)
37
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
48
load("//rust:defs.bzl", "rust_binary", "rust_common")
59

@@ -29,6 +33,37 @@ def get_cc_compile_env(cc_toolchain, feature_configuration):
2933
variables = compile_variables,
3034
)
3135

36+
def get_cxx_compile_opts(cc_toolchain, feature_configuration):
37+
"""Gather command line arguments and env for the ``CPP_COMPILE_ACTION_NAME``
38+
action from the given cc_toolchain.
39+
40+
Args:
41+
cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`.
42+
feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features.
43+
44+
Returns:
45+
tuple: Returns the command line arguments as the first element and the
46+
env as the second element.
47+
"""
48+
compile_variables = cc_common.create_compile_variables(
49+
cc_toolchain = cc_toolchain,
50+
feature_configuration = feature_configuration,
51+
)
52+
53+
compile_args_from_toolchain = cc_common.get_memory_inefficient_command_line(
54+
feature_configuration = feature_configuration,
55+
action_name = CPP_COMPILE_ACTION_NAME,
56+
variables = compile_variables,
57+
)
58+
59+
cc_compile_env = cc_common.get_environment_variables(
60+
feature_configuration = feature_configuration,
61+
action_name = CPP_COMPILE_ACTION_NAME,
62+
variables = compile_variables,
63+
)
64+
65+
return (compile_args_from_toolchain, cc_compile_env)
66+
3267
def _build_script_impl(ctx):
3368
"""The implementation for the `_build_script_run` rule.
3469
@@ -101,6 +136,17 @@ def _build_script_impl(ctx):
101136
if include:
102137
env["INCLUDE"] = include
103138

139+
compile_args_from_toolchain, _ = get_cxx_compile_opts(cc_toolchain, feature_configuration)
140+
# XXX: Skip adding the CXX env since we already added the C env. Should we stick to just CXX?
141+
# Remove --sysroot since we pass it in the SYSROOT env var (we need to
142+
# use the absolute path for it). See ``cargo_build_script_runner/bin.rs``
143+
# for how this is handled.
144+
compile_args_from_toolchain = [
145+
arg for arg in compile_args_from_toolchain
146+
if not arg.startswith("--sysroot")
147+
]
148+
env["CXXFLAGS"] = " ".join(compile_args_from_toolchain)
149+
104150
if cc_toolchain:
105151
toolchain_tools.append(cc_toolchain.all_files)
106152

test/cargo_build_script/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ load("//rust:defs.bzl", "rust_test")
77
cargo_build_script(
88
name = "tools_exec_build_rs",
99
srcs = ["build.rs"],
10-
build_script_env = {"TOOL": "$(execpath :tool)"},
10+
build_script_env = {
11+
"TOOL": "$(execpath :tool)",
12+
"CXXFLAGS": "-DMY_DEFINE"
13+
},
1114
tools = [":tool"],
1215
)
1316

test/cargo_build_script/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ fn main() {
44
"cargo:rustc-env=TOOL_PATH={}",
55
std::env::var("TOOL").unwrap()
66
);
7+
println!(
8+
"cargo:rustc-env=CXXFLAGS={}",
9+
std::env::var("CXXFLAGS").unwrap()
10+
);
711

812
// Assert that the CC and CXX env vars existed and were executable.
913
// We don't assert what happens when they're executed (in particular, we don't check for a

test/cargo_build_script/tools_exec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ pub fn test_tool_exec() {
77
tool_path
88
);
99
}
10+
11+
#[test]
12+
pub fn test_cxxflags() {
13+
let cxxflags = env!("CXXFLAGS");
14+
assert!(
15+
cxxflags.contains("-DMY_DEFINE"),
16+
"CXXFLAGS did not contain '-DMY_DEFINE', {}",
17+
cxxflags
18+
);
19+
}

0 commit comments

Comments
 (0)