Skip to content

cc_binary: support .dSYM generation #402

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 1 commit into
base: master
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
7 changes: 4 additions & 3 deletions crosstool/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
actions = _DYNAMIC_LINK_ACTIONS,
flag_groups = [
flag_group(
flags = ["-Wl,-S"],
flags = ["STRIP_DEBUG_SYMBOLS"],
expand_if_available = "strip_debug_symbols",
),
],
Expand Down Expand Up @@ -2059,13 +2059,14 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
flag_groups = [flag_group(flags = ["-g"])],
),
flag_set(
actions = ["objc-executable"],
actions = _DYNAMIC_LINK_ACTIONS,
flag_groups = [
flag_group(
flags = [
"DSYM_HINT_LINKED_BINARY=%{linked_binary}",
"DSYM_HINT_LINKED_BINARY=%{output_execpath}",
"DSYM_HINT_DSYM_PATH=%{dsym_path}",
],
expand_if_available = "output_execpath",
),
],
),
Expand Down
50 changes: 36 additions & 14 deletions crosstool/wrapped_clang.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,15 @@ void ProcessArgument(const std::string arg, const std::string developer_dir,
const std::string sdk_root, const std::string cwd,
const std::string nosandbox_root, bool relative_ast_path,
std::string &linked_binary, std::string &dsym_path,
std::string toolchain_path,
bool &strip_debug_symbols, std::string toolchain_path,
std::function<void(const std::string &)> consumer);

bool ProcessResponseFile(const std::string arg, const std::string developer_dir,
const std::string sdk_root, const std::string cwd,
const std::string nosandbox_root,
bool relative_ast_path, std::string &linked_binary,
std::string &dsym_path, std::string toolchain_path,
std::string &dsym_path, bool &strip_debug_symbols,
std::string toolchain_path,
std::function<void(const std::string &)> consumer) {
auto path = arg.substr(1);
std::ifstream original_file(path);
Expand All @@ -283,7 +284,7 @@ bool ProcessResponseFile(const std::string arg, const std::string developer_dir,
// unescape them ourselves.
ProcessArgument(Unescape(arg_from_file), developer_dir, sdk_root, cwd,
nosandbox_root, relative_ast_path, linked_binary, dsym_path,
toolchain_path, consumer);
strip_debug_symbols, toolchain_path, consumer);
}

return true;
Expand Down Expand Up @@ -341,13 +342,13 @@ void ProcessArgument(const std::string arg, const std::string developer_dir,
const std::string sdk_root, const std::string cwd,
const std::string nosandbox_root, bool relative_ast_path,
std::string &linked_binary, std::string &dsym_path,
std::string toolchain_path,
bool &strip_debug_symbols, std::string toolchain_path,
std::function<void(const std::string &)> consumer) {
auto new_arg = arg;
if (arg[0] == '@') {
if (ProcessResponseFile(arg, developer_dir, sdk_root, cwd, nosandbox_root,
relative_ast_path, linked_binary, dsym_path,
toolchain_path, consumer)) {
strip_debug_symbols, toolchain_path, consumer)) {
return;
}
}
Expand All @@ -358,6 +359,10 @@ void ProcessArgument(const std::string arg, const std::string developer_dir,
if (SetArgIfFlagPresent(arg, "DSYM_HINT_DSYM_PATH", &dsym_path)) {
return;
}
if (arg == "STRIP_DEBUG_SYMBOLS") {
strip_debug_symbols = true;
return;
}

FindAndReplace("__BAZEL_EXECUTION_ROOT__", cwd, &new_arg);
FindAndReplace("__BAZEL_EXECUTION_ROOT_NO_SANDBOX__", nosandbox_root,
Expand Down Expand Up @@ -427,6 +432,7 @@ int main(int argc, char *argv[]) {
std::string developer_dir = GetMandatoryEnvVar("DEVELOPER_DIR");
std::string sdk_root = GetMandatoryEnvVar("SDKROOT");
std::string linked_binary, dsym_path;
bool strip_debug_symbols = false;

const std::string cwd = GetCurrentDirectory();
std::vector<std::string> invocation_args = {"/usr/bin/xcrun", tool_name};
Expand All @@ -453,8 +459,8 @@ int main(int argc, char *argv[]) {
std::string arg(argv[i]);

ProcessArgument(arg, developer_dir, sdk_root, cwd, nosandbox_root,
relative_ast_path, linked_binary, dsym_path, toolchain_path,
consumer);
relative_ast_path, linked_binary, dsym_path,
strip_debug_symbols, toolchain_path, consumer);
}

char *modulemap = getenv("APPLE_SUPPORT_MODULEMAP");
Expand Down Expand Up @@ -509,16 +515,32 @@ int main(int argc, char *argv[]) {
return 0;
}

std::vector<std::string> dsymutil_args = {"/usr/bin/xcrun",
"dsymutil",
linked_binary,
"-o",
dsym_path,
"--flat",
"--no-swiftmodule-timestamp"};
const std::string bundle_suffix = ".dSYM";
bool is_bundle = dsym_path.rfind(bundle_suffix) ==
dsym_path.length() - bundle_suffix.length();

std::vector<std::string> dsymutil_args = {
"/usr/bin/xcrun", "dsymutil",
linked_binary, "-o",
dsym_path, "--no-swiftmodule-timestamp"};
if (!is_bundle) {
// We should generate a .dSYM bundle only when a path is passed to a .dSYM
// directory for backwards compatibility
dsymutil_args.push_back("--flat");
}
if (!RunSubProcess(dsymutil_args)) {
return 1;
}

// When stripping is requested, we should still strip the binary
// before returning
if (strip_debug_symbols) {
std::vector<std::string> strip_args = {"/usr/bin/xcrun", "strip", "-S",
linked_binary};
if (!RunSubProcess(strip_args)) {
return 2;
}
}

return 0;
}
13 changes: 13 additions & 0 deletions test/linking_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,16 @@ def linking_test_suite(name):
mnemonic = "ObjcLink",
target_under_test = "//test/test_data:macos_binary",
)

dsym_test(
name = "{}_generate_cpp_dsym_test".format(name),
tags = [name] +
# Can't be enabled until https://github.com/bazelbuild/bazel/pull/25881 lands
["manual"],
expected_argv = [
"DSYM_HINT_LINKED_BINARY",
"DSYM_HINT_DSYM_PATH",
],
mnemonic = "CppLink",
target_under_test = "//test/test_data:cc_test_binary",
)