Skip to content

Commit 953cce3

Browse files
committed
Populate CFLAGS and CXXFLAGS when invoking build script.
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 f569827 commit 953cce3

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", "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.
@@ -102,7 +116,7 @@ def _build_script_impl(ctx):
102116
env["LDFLAGS"] = " ".join(link_args)
103117

104118
# MSVC requires INCLUDE to be set
105-
cc_env = get_cc_compile_env(cc_toolchain, feature_configuration)
119+
cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
106120
include = cc_env.get("INCLUDE")
107121
if include:
108122
env["INCLUDE"] = include
@@ -120,6 +134,12 @@ def _build_script_impl(ctx):
120134
if cc_toolchain.sysroot:
121135
env["SYSROOT"] = cc_toolchain.sysroot
122136

137+
# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
138+
# to determine the deployment target when building for apple platforms (`macosx-version-min`
139+
# for example, itself derived from the `macos_minimum_os` Bazel argument).
140+
env["CFLAGS"] = " ".join(cc_c_args)
141+
env["CXXFLAGS"] = " ".join(cc_cxx_args)
142+
123143
for f in ctx.attr.crate_features:
124144
env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"
125145

test/cargo_build_script/build.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ fn main() {
55
std::env::var("TOOL").unwrap()
66
);
77

8-
// Assert that the CC and CXX env vars existed and were executable.
8+
// Assert that the CC, CXX and LD env vars existed and were executable.
99
// We don't assert what happens when they're executed (in particular, we don't check for a
1010
// non-zero exit code), but this asserts that it's an existing file which is executable.
1111
//
1212
// Unfortunately we need to shlex the path, because we add a `--sysroot=...` arg to the env var.
13-
for env_var in &["CC", "CXX"] {
13+
for env_var in &["CC", "CXX", "LD"] {
1414
let v = std::env::var(env_var)
1515
.unwrap_or_else(|err| panic!("Error getting {}: {}", env_var, err));
1616
let (path, args) = if let Some(index) = v.find("--sysroot") {
@@ -24,4 +24,9 @@ fn main() {
2424
.status()
2525
.unwrap();
2626
}
27+
28+
// Assert that some env variables are set.
29+
for env_var in &["CFLAGS", "CXXFLAGS", "LDFLAGS"] {
30+
assert!(std::env::var(env_var).is_ok());
31+
}
2732
}

0 commit comments

Comments
 (0)