Skip to content

Commit 97fd329

Browse files
authored
Populate CFLAGS and CXXFLAGS when invoking build script. (#1081)
Downstream tools (in particular cc-rs) depend on them being set. The particular use case that I noticed was broken was the macos deployment target (specified via the --macos_minimun_os Bazel option) wasn't being applied on binaries being built via cc-rs in a build.rs script. This was causing all sort of bugs such as symbols not being weakly linked since it was using the SDK version (the default) for the deployment target and bazel-built Rust binaries wouldn't load on older OS versions due to failures to resolve symbols at runtime, even though the deployment target was correctly set in Bazel.
1 parent bf59038 commit 97fd329

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

cargo/cargo_build_script.bzl

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# buildifier: disable=module-docstring
2-
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
2+
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "CPP_COMPILE_ACTION_NAME", "C_COMPILE_ACTION_NAME")
33
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
44
load("//rust:defs.bzl", "rust_binary", "rust_common")
55

@@ -9,25 +9,39 @@ load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_
99
# buildifier: disable=bzl-visibility
1010
load("//rust/private:utils.bzl", "dedent", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")
1111

12-
def get_cc_compile_env(cc_toolchain, feature_configuration):
12+
def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
1313
"""Gather cc environment variables from the given `cc_toolchain`
1414
1515
Args:
1616
cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`.
1717
feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features.
1818
1919
Returns:
20-
dict: Returns environment variables to be set for given action.
20+
tuple: A tuple of the following items:
21+
- (sequence): A flattened C command line flags for given action.
22+
- (sequence): A flattened CXX command line flags for given action.
23+
- (dict): C environment variables to be set for given action.
2124
"""
2225
compile_variables = cc_common.create_compile_variables(
2326
feature_configuration = feature_configuration,
2427
cc_toolchain = cc_toolchain,
2528
)
26-
return cc_common.get_environment_variables(
29+
cc_c_args = cc_common.get_memory_inefficient_command_line(
2730
feature_configuration = feature_configuration,
2831
action_name = C_COMPILE_ACTION_NAME,
2932
variables = compile_variables,
3033
)
34+
cc_cxx_args = cc_common.get_memory_inefficient_command_line(
35+
feature_configuration = feature_configuration,
36+
action_name = CPP_COMPILE_ACTION_NAME,
37+
variables = compile_variables,
38+
)
39+
cc_env = cc_common.get_environment_variables(
40+
feature_configuration = feature_configuration,
41+
action_name = C_COMPILE_ACTION_NAME,
42+
variables = compile_variables,
43+
)
44+
return cc_c_args, cc_cxx_args, cc_env
3145

3246
def _build_script_impl(ctx):
3347
"""The implementation for the `_build_script_run` rule.
@@ -99,7 +113,7 @@ def _build_script_impl(ctx):
99113
env["LDFLAGS"] = " ".join(link_args)
100114

101115
# MSVC requires INCLUDE to be set
102-
cc_env = get_cc_compile_env(cc_toolchain, feature_configuration)
116+
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
103117
include = cc_env.get("INCLUDE")
104118
if include:
105119
env["INCLUDE"] = include
@@ -117,6 +131,12 @@ def _build_script_impl(ctx):
117131
if cc_toolchain.sysroot:
118132
env["SYSROOT"] = cc_toolchain.sysroot
119133

134+
# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
135+
# to determine the deployment target when building for apple platforms (`macosx-version-min`
136+
# for example, itself derived from the `macos_minimum_os` Bazel argument).
137+
env["CFLAGS"] = " ".join(cc_c_args)
138+
env["CXXFLAGS"] = " ".join(cc_cxx_args)
139+
120140
# Inform build scripts of rustc flags
121141
# https://github.com/rust-lang/cargo/issues/9600
122142
env["CARGO_ENCODED_RUSTFLAGS"] = "\\x1f".join([

test/cargo_build_script/build.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ fn main() {
2828
std::env::var("TOOL").unwrap()
2929
);
3030

31-
// Assert that the CC and CXX env vars existed and were executable.
31+
// Assert that the CC, CXX and LD env vars existed and were executable.
3232
// We don't assert what happens when they're executed (in particular, we don't check for a
3333
// non-zero exit code), but this asserts that it's an existing file which is executable.
3434
//
3535
// Unfortunately we need to shlex the path, because we add a `--sysroot=...` arg to the env var.
36-
for env_var in &["CC", "CXX"] {
36+
for env_var in &["CC", "CXX", "LD"] {
3737
let v = std::env::var(env_var)
3838
.unwrap_or_else(|err| panic!("Error getting {}: {}", env_var, err));
3939
let (path, args) = if let Some(index) = v.find("--sysroot") {
@@ -47,4 +47,9 @@ fn main() {
4747
.status()
4848
.unwrap();
4949
}
50+
51+
// Assert that some env variables are set.
52+
for env_var in &["CFLAGS", "CXXFLAGS", "LDFLAGS"] {
53+
assert!(std::env::var(env_var).is_ok());
54+
}
5055
}

0 commit comments

Comments
 (0)