Skip to content

Conversation

@jpnurmi
Copy link
Collaborator

@jpnurmi jpnurmi commented Sep 2, 2025

  1. Lets Objective Sharpie generate bindings for everything in Sentry-Swift.h (Obj-C header auto-generated by the Swift compiler)
  2. Uses Roslyn to patch C# types in StructsAndEnums.cs and ApiDefinitions.cs (because doing the same with regexps in PWSH is extremely difficult to get right)

#skip-changelog

@jpnurmi jpnurmi changed the title Generate bindings from Sentry-Swift.h Generate Cocoa bindings from Sentry-Swift.h Sep 2, 2025
@jpnurmi jpnurmi changed the title Generate Cocoa bindings from Sentry-Swift.h build: Generate Cocoa bindings from Sentry-Swift.h Sep 2, 2025
jpnurmi added a commit that referenced this pull request Sep 3, 2025
@codecov
Copy link

codecov bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (version6@8bd302b). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             version6    #4488   +/-   ##
===========================================
  Coverage            ?   73.17%           
===========================================
  Files               ?      480           
  Lines               ?    17421           
  Branches            ?     3437           
===========================================
  Hits                ?    12748           
  Misses              ?     3822           
  Partials            ?      851           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jpnurmi jpnurmi changed the title build: Generate Cocoa bindings from Sentry-Swift.h build: Generate Swift types from Sentry-Swift.h Oct 7, 2025
@jpnurmi jpnurmi changed the base branch from main to version6 October 8, 2025 10:53
@jpnurmi
Copy link
Collaborator Author

jpnurmi commented Oct 23, 2025

@jamescrosswell @Flash0ver There haven't been any Swift binding updates that required manual intervention recently, so I forgot I had this PR lying here. This can generate C# bindings for Swift types, so we'd no longer need those error-prone, manually crafted bindings in SwiftApiDefinitions.cs and SwiftStructsAndEnumes.cs. How do you like the approach? I think this PR in its current state would be a reasonable first step. Later on, we could even move more of that regexp manipulation from PWSH to C# and Roslyn to make it subjectively easier to maintain...

@jamescrosswell
Copy link
Collaborator

jamescrosswell commented Oct 29, 2025

How do you like the approach? I think this PR in its current state would be a reasonable first step. Later on, we could even move more of that regexp manipulation from PWSH to C# and Roslyn to make it subjectively easier to maintain...

It looks like it's easier to surgically remove the stuff that's problematic with the Roslyn code. I guess we're placing a bet that there will be less work required to maintain the whitelist/blacklist here than the manual bindings. There's no way to be sure of that in advance really - just have to give it a crack.

I think it's worth trying 👍🏻

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice - our first c# script!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

😏

@jpnurmi
Copy link
Collaborator Author

jpnurmi commented Oct 31, 2025

The auto-generated Sentry-Swift.h is so noisy that it's quite hard to clean up the resulting ApiDefinitions.cs with regular expressions. I spent a few late evenings trying to remove unwanted types with regexps, but all my attempts eventually became awfully complicated and messy due to comments, attributes, macros, etc. they often have around.

$interfacesToKeep = @(...)
$pattern = $interfacesToKeep.Count -gt 0 ? ($interfacesToKeep -join '|') : '(?!)'
$content = $content -replace "(?ms)^(?:[ \t]*SWIFT_AVAILABILITY\([^)]+\)[^\r\n]*\r?\n)*[ \t]*@interface\s+(?!($pattern)\b)[A-Za-z_][A-Za-z0-9_]*(?:\s*\([^)]+\))?(?:\s*<[^>]*>)?[\s\S]*?\r?\n@end", ''

$protocolsToKeep = @(...)
$pattern = $protocolsToKeep.Count -gt 0 ? ($protocolsToKeep -join '|') : '(?!)'
$content = $content -replace "(?ms)^(?!#\s*define\s+SWIFT_PROTOCOL(_NAMED)?)[ \t]*SWIFT_PROTOCOL(_NAMED)?\([^)]+\)[^\r\n]*?(?:\s+SWIFT_AVAILABILITY\([^)]+\))*\s*\r?\n@protocol\s+(?!($pattern)\b)[A-Za-z_][A-Za-z0-9_]*\s*(?:<[^>]*>)?[\s\S]*?\r?\n@end", ''

That's one of my earlier attempts that I failed to make work correctly, as it swallowed some unrelated types. 😅 I thought there must be an easier way to filter types by name, which is how I ended up giving Roslyn a try...

P.S. I have also successfully used this together with sentry-cocoa#6193 after rebasing it on the latest main (v9) branch that has quite a few API changes compared to v8.

@jpnurmi jpnurmi marked this pull request as ready for review October 31, 2025 12:51
@jpnurmi jpnurmi requested a review from Flash0ver as a code owner October 31, 2025 12:51
cursor[bot]

This comment was marked as outdated.

Copy link
Collaborator

@jamescrosswell jamescrosswell left a comment

Choose a reason for hiding this comment

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

Awesome... just tested it out on #4664 and I think easier than hand coded bindings 👍🏻

@jamescrosswell jamescrosswell merged commit a739982 into version6 Nov 3, 2025
30 checks passed
@jamescrosswell jamescrosswell deleted the build/swift-gen branch November 3, 2025 00:15
@jpnurmi jpnurmi mentioned this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants