Skip to content

Commit 009148a

Browse files
committed
Move magic compiler wrapper args to env vars
This lets us avoid trying to parse these values out of various arguments and instead we just get them through the environment.
1 parent 1f942ef commit 009148a

5 files changed

Lines changed: 80 additions & 56 deletions

File tree

crosstool/cc_toolchain_config.bzl

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -863,12 +863,13 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
863863

864864
strip_debug_symbols_feature = feature(
865865
name = "strip_debug_symbols",
866-
flag_sets = [
867-
flag_set(
866+
env_sets = [
867+
env_set(
868868
actions = _DYNAMIC_LINK_ACTIONS,
869-
flag_groups = [
870-
flag_group(
871-
flags = ["STRIP_DEBUG_SYMBOLS"],
869+
env_entries = [
870+
env_entry(
871+
key = "STRIP_DEBUG_SYMBOLS",
872+
value = "true",
872873
expand_if_available = "strip_debug_symbols",
873874
),
874875
],
@@ -2093,14 +2094,20 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
20932094
],
20942095
flag_groups = [flag_group(flags = ["-g"])],
20952096
),
2096-
flag_set(
2097+
],
2098+
env_sets = [
2099+
env_set(
20972100
actions = _DYNAMIC_LINK_ACTIONS,
2098-
flag_groups = [
2099-
flag_group(
2100-
flags = [
2101-
"DSYM_HINT_LINKED_BINARY=%{output_execpath}",
2102-
"DSYM_HINT_DSYM_PATH=%{dsym_path}",
2103-
],
2101+
env_entries = [
2102+
env_entry(
2103+
key = "DSYM_HINT_LINKED_BINARY",
2104+
value = "%{output_execpath}",
2105+
# We need to check this for backwards compatibility with bazel 7
2106+
expand_if_available = "dsym_path",
2107+
),
2108+
env_entry(
2109+
key = "DSYM_HINT_DSYM_PATH",
2110+
value = "%{dsym_path}",
21042111
# We need to check this for backwards compatibility with bazel 7
21052112
expand_if_available = "dsym_path",
21062113
),

crosstool/universal_exec_tool.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ env -i \
3939
-arch arm64 \
4040
-arch x86_64 \
4141
-O3 \
42+
-g \
4243
-o $@ \
4344
$(SRCS)
4445
""",

crosstool/wrapped_clang.cc

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
//
15-
// wrapped_clang.cc: Pass args to 'xcrun clang' and zip dsym files.
15+
// wrapped_clang.cc: Pass args to 'xcrun clang' and optionally produce dSYM
16+
// files.
1617
//
17-
// wrapped_clang passes its args to clang, but also supports a separate set of
18-
// invocations to generate dSYM files. If "DSYM_HINT" flags are passed in, they
19-
// are used to construct that separate set of invocations (instead of being
20-
// passed to clang).
21-
// The following "DSYM_HINT" flags control dsym generation. If any one if these
22-
// are passed in, then they all must be passed in.
23-
// "DSYM_HINT_LINKED_BINARY": Workspace-relative path to binary output of the
24-
// link action generating the dsym file.
25-
// "DSYM_HINT_DSYM_PATH": Workspace-relative path to dSYM dwarf file.
2618

2719
#include <libgen.h>
2820
#include <spawn.h>
@@ -259,16 +251,12 @@ static std::unique_ptr<TempFile> WriteResponseFile(
259251

260252
void ProcessArgument(const std::string arg, const std::string developer_dir,
261253
const std::string sdk_root, const std::string cwd,
262-
bool relative_ast_path, std::string &linked_binary,
263-
std::string &dsym_path, bool &strip_debug_symbols,
264-
std::string toolchain_path,
254+
bool relative_ast_path, std::string toolchain_path,
265255
std::function<void(const std::string &)> consumer);
266256

267257
bool ProcessResponseFile(const std::string arg, const std::string developer_dir,
268258
const std::string sdk_root, const std::string cwd,
269-
bool relative_ast_path, std::string &linked_binary,
270-
std::string &dsym_path, bool &strip_debug_symbols,
271-
std::string toolchain_path,
259+
bool relative_ast_path, std::string toolchain_path,
272260
std::function<void(const std::string &)> consumer) {
273261
auto path = arg.substr(1);
274262
std::ifstream original_file(path);
@@ -282,8 +270,7 @@ bool ProcessResponseFile(const std::string arg, const std::string developer_dir,
282270
// Arguments in response files might be quoted/escaped, so we need to
283271
// unescape them ourselves.
284272
ProcessArgument(Unescape(arg_from_file), developer_dir, sdk_root, cwd,
285-
relative_ast_path, linked_binary, dsym_path,
286-
strip_debug_symbols, toolchain_path, consumer);
273+
relative_ast_path, toolchain_path, consumer);
287274
}
288275

289276
return true;
@@ -339,30 +326,16 @@ std::string GetToolchainPath(const std::string &toolchain_id) {
339326

340327
void ProcessArgument(const std::string arg, const std::string developer_dir,
341328
const std::string sdk_root, const std::string cwd,
342-
bool relative_ast_path, std::string &linked_binary,
343-
std::string &dsym_path, bool &strip_debug_symbols,
344-
std::string toolchain_path,
329+
bool relative_ast_path, std::string toolchain_path,
345330
std::function<void(const std::string &)> consumer) {
346331
auto new_arg = arg;
347332
if (arg[0] == '@') {
348333
if (ProcessResponseFile(arg, developer_dir, sdk_root, cwd,
349-
relative_ast_path, linked_binary, dsym_path,
350-
strip_debug_symbols, toolchain_path, consumer)) {
334+
relative_ast_path, toolchain_path, consumer)) {
351335
return;
352336
}
353337
}
354338

355-
if (SetArgIfFlagPresent(arg, "DSYM_HINT_LINKED_BINARY", &linked_binary)) {
356-
return;
357-
}
358-
if (SetArgIfFlagPresent(arg, "DSYM_HINT_DSYM_PATH", &dsym_path)) {
359-
return;
360-
}
361-
if (arg == "STRIP_DEBUG_SYMBOLS") {
362-
strip_debug_symbols = true;
363-
return;
364-
}
365-
366339
FindAndReplace("__BAZEL_EXECUTION_ROOT__", cwd, &new_arg);
367340
FindAndReplace("__BAZEL_XCODE_DEVELOPER_DIR__", developer_dir, &new_arg);
368341
FindAndReplace("__BAZEL_XCODE_SDKROOT__", sdk_root, &new_arg);
@@ -428,8 +401,6 @@ int main(int argc, char *argv[]) {
428401

429402
std::string developer_dir = GetMandatoryEnvVar("DEVELOPER_DIR");
430403
std::string sdk_root = GetMandatoryEnvVar("SDKROOT");
431-
std::string linked_binary, dsym_path;
432-
bool strip_debug_symbols = false;
433404

434405
const std::string cwd = GetCurrentDirectory();
435406
std::vector<std::string> invocation_args = {"/usr/bin/xcrun", tool_name};
@@ -443,7 +414,6 @@ int main(int argc, char *argv[]) {
443414
std::string arg(argv[i]);
444415

445416
ProcessArgument(arg, developer_dir, sdk_root, cwd, relative_ast_path,
446-
linked_binary, dsym_path, strip_debug_symbols,
447417
toolchain_path, consumer);
448418
}
449419

@@ -468,10 +438,12 @@ int main(int argc, char *argv[]) {
468438

469439
// Check to see if we should postprocess with dsymutil.
470440
bool postprocess = false;
471-
if ((!linked_binary.empty()) || (!dsym_path.empty())) {
472-
if ((linked_binary.empty()) || (dsym_path.empty())) {
441+
const char *linked_binary = getenv("DSYM_HINT_LINKED_BINARY");
442+
const char *dsym_path_raw_value = getenv("DSYM_HINT_DSYM_PATH");
443+
if (linked_binary != nullptr || dsym_path_raw_value != nullptr) {
444+
if (linked_binary == nullptr || dsym_path_raw_value == nullptr) {
473445
const char *missing_dsym_flag;
474-
if (linked_binary.empty()) {
446+
if (linked_binary == nullptr) {
475447
missing_dsym_flag = "DSYM_HINT_LINKED_BINARY";
476448
} else {
477449
missing_dsym_flag = "DSYM_HINT_DSYM_PATH";
@@ -499,6 +471,7 @@ int main(int argc, char *argv[]) {
499471
return 0;
500472
}
501473

474+
std::string dsym_path = dsym_path_raw_value;
502475
const std::string bundle_suffix = ".dSYM";
503476
bool is_bundle = dsym_path.rfind(bundle_suffix) ==
504477
dsym_path.length() - bundle_suffix.length();
@@ -518,7 +491,8 @@ int main(int argc, char *argv[]) {
518491

519492
// When stripping is requested, we should still strip the binary
520493
// before returning
521-
if (strip_debug_symbols) {
494+
const char *strip_debug_symbols = getenv("STRIP_DEBUG_SYMBOLS");
495+
if (strip_debug_symbols != nullptr) {
522496
std::vector<std::string> strip_args = {"/usr/bin/xcrun", "strip", "-S",
523497
linked_binary};
524498
if (!RunSubProcess(strip_args)) {

test/linking_tests.bzl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ def linking_test_suite(name):
6464
],
6565
not_expected_argv = [
6666
"-g",
67-
"DSYM_HINT_LINKED_BINARY",
6867
"-dead_strip",
6968
],
69+
not_expected_env_keys = [
70+
"DSYM_HINT_LINKED_BINARY",
71+
],
7072
mnemonic = "ObjcLink",
7173
target_under_test = "//test/test_data:macos_binary",
7274
)
@@ -86,9 +88,11 @@ def linking_test_suite(name):
8688
],
8789
not_expected_argv = [
8890
"-g",
89-
"DSYM_HINT_LINKED_BINARY",
9091
"-dead_strip",
9192
],
93+
not_expected_env_keys = [
94+
"DSYM_HINT_LINKED_BINARY",
95+
],
9296
mnemonic = "ObjcLink",
9397
target_under_test = "//test/test_data:ios_binary",
9498
)
@@ -182,6 +186,8 @@ def linking_test_suite(name):
182186
tags = [name],
183187
expected_argv = [
184188
"-g",
189+
],
190+
expected_env_keys = [
185191
"DSYM_HINT_LINKED_BINARY",
186192
],
187193
mnemonic = "ObjcLink",
@@ -191,7 +197,7 @@ def linking_test_suite(name):
191197
dsym_test(
192198
name = "{}_generate_cpp_dsym_test".format(name),
193199
tags = [name],
194-
expected_argv = [
200+
expected_env_keys = [
195201
"DSYM_HINT_LINKED_BINARY",
196202
"DSYM_HINT_DSYM_PATH",
197203
],

test/rules/action_command_line_test.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ def _action_command_line_test_impl(ctx):
102102
),
103103
)
104104

105+
for expected_key in ctx.attr.expected_env_keys:
106+
if expected_key not in action.env:
107+
unittest.fail(
108+
env,
109+
"{}expected env to contain key '{}', but it did not: {}".format(
110+
message_prefix,
111+
expected_key,
112+
action.env,
113+
),
114+
)
115+
116+
for not_expected_key in ctx.attr.not_expected_env_keys:
117+
if not_expected_key in action.env:
118+
unittest.fail(
119+
env,
120+
"{}expected env to not contain key '{}', but it did: {}".format(
121+
message_prefix,
122+
not_expected_key,
123+
action.env,
124+
),
125+
)
126+
105127
return analysistest.end(env)
106128

107129
def make_action_command_line_test_rule(config_settings = {}):
@@ -132,6 +154,20 @@ space-delimited string.
132154
A list of strings representing substrings expected not to appear in the action
133155
command line, after concatenating all command line arguments into a single
134156
space-delimited string.
157+
""",
158+
),
159+
"expected_env_keys": attr.string_list(
160+
mandatory = False,
161+
doc = """\
162+
A list of strings representing environment variable keys expected to be set
163+
in the action environment.
164+
""",
165+
),
166+
"not_expected_env_keys": attr.string_list(
167+
mandatory = False,
168+
doc = """\
169+
A list of strings representing environment variable keys expected not to be
170+
set in the action environment.
135171
""",
136172
),
137173
"mnemonic": attr.string(

0 commit comments

Comments
 (0)