Skip to content

Comments

macOS: Support dSYM generation for cc_binary#25881

Closed
benjivos wants to merge 1 commit intobazelbuild:masterfrom
benjivos:feat/support_dsym_macos
Closed

macOS: Support dSYM generation for cc_binary#25881
benjivos wants to merge 1 commit intobazelbuild:masterfrom
benjivos:feat/support_dsym_macos

Conversation

@benjivos
Copy link
Contributor

Add .dSYM as additional_linker_outputs and expose a linker variable that can be used by apple_support and rules_swift.

Fixes issue #16893.

@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Apr 17, 2025
Copy link
Member

@keith keith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wondered if we should output a directory for this or a flat file, but directory seems preferred. AFAICT the reason that the apple binary logic doesn't do that predates good tree artifact support. but today the apple logic also lipos the dsym binary if you do a multi-arch built, so it has some post processing that this will not too.

I wondered if checking this feature was the right way to do this, and it seems fine, but it would also be equivalent to check the cpp fragment that the feature was based on

@iancha1992 iancha1992 added the team-Rules-CPP Issues for C++ rules label Apr 17, 2025
@benjivos
Copy link
Contributor Author

i wondered if we should output a directory for this or a flat file, but directory seems preferred. AFAICT the reason that the apple binary logic doesn't do that predates good tree artifact support. but today the apple logic also lipos the dsym binary if you do a multi-arch built, so it has some post processing that this will not too.

Directory is preferred as the .dSYM bundle can contain other resources too (like the UUID property list, see https://lldb.llvm.org/use/symbols.html).

I wondered if checking this feature was the right way to do this, and it seems fine, but it would also be equivalent to check the cpp fragment that the feature was based on

Would you prefer this instead (tested here and seems to work too)?

--- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
@@ -597,7 +597,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
     # On macOS, if GENERATE_DSYM_FILE feature is enabled
     # then a .dSYM file will be built along with the executable.
     dsym_file = None
-    if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_dsym_file"):
+    if cpp_config.apple_generate_dsym:
         dsym_file = ctx.actions.declare_directory(
             "{name}.dSYM".format(
                 name = target_name,

@keith
Copy link
Member

keith commented Apr 18, 2025

Would you prefer this instead (tested here and seems to work too)?

I'm not sure which of these would be preferred. The latter seems more resilient to me, but the googlers who have to review can weigh in

@benjivos
Copy link
Contributor Author

@allevato could you review this change please?

@allevato
Copy link
Member

C++ rules like cc_binary are @googlewalt 's domain, I'll defer to him.

@benjivos
Copy link
Contributor Author

@googlewalt could you please review these changes? Thank you!

# then a .dSYM file will be built along with the executable.
dsym_file = None
if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_dsym_file"):
dsym_file = ctx.actions.declare_directory(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be a file. Historically the toolchain calls dsymutil with --flat to generate a flat file, and that still seems to true for apple_support today. I suspect there might be a desire to support dsymutil generating a directory also, but let's not worry about that for this PR.

@googlewalt
Copy link
Contributor

googlewalt commented Apr 22, 2025

i wondered if we should output a directory for this or a flat file, but directory seems preferred. AFAICT the reason that the apple binary logic doesn't do that predates good tree artifact support. but today the apple logic also lipos the dsym binary if you do a multi-arch built, so it has some post processing that this will not too.

Directory is preferred as the .dSYM bundle can contain other resources too (like the UUID property list, see https://lldb.llvm.org/use/symbols.html).

I missed this discussion before I reviewed. Currently the default behavior of both the internal and OSS toolchains is to output a file, so for consistency it seems this should be a file. Internally we have a bazel flag to control what the toolchain should output, but unfortunately that's internal only.

@googlewalt
Copy link
Contributor

googlewalt commented Apr 22, 2025

I wondered if checking this feature was the right way to do this, and it seems fine, but it would also be equivalent to check the cpp fragment that the feature was based on

Would you prefer this instead (tested here and seems to work too)?

--- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
+++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl
@@ -597,7 +597,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
     # On macOS, if GENERATE_DSYM_FILE feature is enabled
     # then a .dSYM file will be built along with the executable.
     dsym_file = None
-    if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "generate_dsym_file"):
+    if cpp_config.apple_generate_dsym:
         dsym_file = ctx.actions.declare_directory(
             "{name}.dSYM".format(
                 name = target_name,

I'd prefer that since it's equivalent, shorter, and consistent with the objc linking code, but don't feel strongly.

@keith
Copy link
Member

keith commented Apr 23, 2025

I missed this discussion before I reviewed. Currently the default behavior of both the internal and OSS toolchains is to output a file, so for consistency it seems this should be a file. Internally we have a bazel flag to control what the toolchain should output, but unfortunately that's internal only.

I think the problem with this for cc_binary is that rules_apple does post processing https://github.com/bazelbuild/rules_apple/blob/14a8b6c05f71f3186d587c8b05c545a5d40ceae5/apple/internal/partials/debug_symbols.bzl#L172 to generate the Info.plist that ends up in the bundle, and with this approach no extra logic is required to generate the dsym with that structure (I don't actually know if that plist is required)

@googlewalt
Copy link
Contributor

I don't think we can hardwire this code to support a directory. The functionality would be broken internally; it would not work with the apple_support toolchain; and it is inconsistent with the analogous code in objc linking.

If we want to support directory, I think it has to be in addition to supporting a file. One solution would be to add an internal flag for this -- I don't love it but at least it would work.

For this PR can we implement a file? If we need directory support we can continue discussion outside of it.

Add .dSYM as additional_linker_outputs and expose a linker variable that can be used by apple_support and rules_swift.

Fixes issue bazelbuild#16893.

Signed-off-by: Benji Vos <[email protected]>
@benjivos benjivos force-pushed the feat/support_dsym_macos branch from aeca8ad to bcd58c5 Compare April 23, 2025 14:54
@benjivos
Copy link
Contributor Author

Thanks for the review, @googlewalt!

I don't think we can hardwire this code to support a directory. The functionality would be broken internally; it would not work with the apple_support toolchain; and it is inconsistent with the analogous code in objc linking.

I understand the concern. I'm currently working on adding proper support for dSYM bundles in apple_support here. The changes to wrapped_clang are backwards compatible with flat dSYMs. Since Bazel hasn’t previously supported dSYM bundles fully, could you clarify what internal functionality would break? I’d be happy to help coordinate changes in rules_apple and elsewhere to ensure consistent support.

For this PR can we implement a file? If we need directory support we can continue discussion outside of it.

I’d prefer to focus on proper support for dSYM bundles in this PR, rather than implementing flat dSYMs. Beyond embedding plists, we also rely on bundles to include Python scripts that LLDB loads automatically (for example https://dmaclach.medium.com/kernel-debugging-on-apple-silicon-ff5aa76c4429 and https://github.com/apple/darwin-xnu/blob/main/tools/lldbmacros/Makefile). These features and usecases don’t work with flat dSYM files.

I’d love to discuss how we can move forward with robust support for bundled dSYMs — happy to collaborate on that!

@benjivos benjivos requested a review from googlewalt April 23, 2025 16:04
@benjivos
Copy link
Contributor Author

@googlewalt any thoughts on this? What is your suggestion to move forward?

@googlewalt
Copy link
Contributor

I see. I need to make some changes so that this is compatible to our toolchain internally, but your proposal looks reasonable.

@benjivos
Copy link
Contributor Author

benjivos commented May 1, 2025

@iancha1992 would you be able to merge this PR? Thank you!

@googlewalt
Copy link
Contributor

I'm getting it merged; might take a day or two to find someone to review it.

@copybara-service copybara-service bot closed this in bfc4b20 May 2, 2025
@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label May 2, 2025
benjivos added a commit to benjivos/bazel that referenced this pull request May 9, 2025
Add .dSYM as additional_linker_outputs and expose a linker variable that can be used by apple_support and rules_swift.

Fixes issue bazelbuild#16893.

Closes bazelbuild#25881.

PiperOrigin-RevId: 754100310
Change-Id: I76016785b80540443383ab4be0a10655b41ce138
keith pushed a commit to bazelbuild/apple_support that referenced this pull request Aug 1, 2025
Add support for generating a .dSYM file if this is requested using
`--apple_generate_dsym`. This change contains the following patches:

* Add `DSYM_HINT` flags to be picked up by `wrapped_clang` to generate
.dSYM bundles on request.
* In `wrapped_clang.cc` make sure the linked binary is stripped after
generating the .dSYM bundle first to make the .dSYM bundle contain
useful information.

Adopts [changes](bazelbuild/bazel#25881) in
bazel to support .dSYM files (as part of fixing
bazelbuild/bazel#16893).
keith added a commit that referenced this pull request Sep 9, 2025
This was discussed in #25881
where we believed it was equivalent. This isn't equivalent if the
toolchain does not support dSYM generations but --apple_generate_dsym is
still passed (as is possible on Linux). In this case we let feature
configuration handle this instead, as this feature is requested when
--apple_generate_dsym is passed. In that case because of this mismatch
this directory will be created "successfully" but it will always be
empty (this would be a failure if it wasn't a tree artifact)
copybara-service bot pushed a commit to bazelbuild/rules_cc that referenced this pull request Dec 23, 2025
This was discussed in bazelbuild/bazel#25881
where we believed it was equivalent. This isn't equivalent if the
toolchain does not support dSYM generations but --apple_generate_dsym is
still passed (as is possible on Linux). In this case we let feature
configuration handle this instead, as this feature is requested when
--apple_generate_dsym is passed. In that case because of this mismatch
this directory will be created "successfully" but it will always be
empty (this would be a failure if it wasn't a tree artifact)

Closes #26925.

PiperOrigin-RevId: 848240203
Change-Id: I7de21ba82fda794ffecb2ad86d2010e46732316f
copybara-service bot pushed a commit that referenced this pull request Dec 23, 2025
This was discussed in #25881
where we believed it was equivalent. This isn't equivalent if the
toolchain does not support dSYM generations but --apple_generate_dsym is
still passed (as is possible on Linux). In this case we let feature
configuration handle this instead, as this feature is requested when
--apple_generate_dsym is passed. In that case because of this mismatch
this directory will be created "successfully" but it will always be
empty (this would be a failure if it wasn't a tree artifact)

Closes #26925.

PiperOrigin-RevId: 848240203
Change-Id: I604bc97e44c195142b12222b73d7442a1115ccd1
fmeum pushed a commit to fmeum/bazel that referenced this pull request Jan 6, 2026
This was discussed in bazelbuild#25881
where we believed it was equivalent. This isn't equivalent if the
toolchain does not support dSYM generations but --apple_generate_dsym is
still passed (as is possible on Linux). In this case we let feature
configuration handle this instead, as this feature is requested when
--apple_generate_dsym is passed. In that case because of this mismatch
this directory will be created "successfully" but it will always be
empty (this would be a failure if it wasn't a tree artifact)

Closes bazelbuild#26925.

PiperOrigin-RevId: 848240203
Change-Id: I604bc97e44c195142b12222b73d7442a1115ccd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Rules-CPP Issues for C++ rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants