Skip to content

Use the new #available(Android <API>, *) instead to look for backtrace()#1546

Merged
finagolfin merged 1 commit intoswiftlang:mainfrom
finagolfin:droid
Apr 21, 2026
Merged

Use the new #available(Android <API>, *) instead to look for backtrace()#1546
finagolfin merged 1 commit intoswiftlang:mainfrom
finagolfin:droid

Conversation

@finagolfin
Copy link
Copy Markdown
Member

Mads added this compiler feature for Android in swiftlang/swift#84574 before the 6.3 branch, so update the Android triples and NDK version to test it, as it requires NDK 28 or later.

@grynspan, just a pure rebased copy of #1373, unfortunately, may be a while before we can merge it.

@finagolfin finagolfin added android 🤖 Android support cross-compilation Compiling tests for other platforms than the host tech-debt 💾 reduces technical debt labels Feb 12, 2026
@grynspan grynspan added this to the Swift 6.4.0 (main) milestone Feb 12, 2026
@grynspan
Copy link
Copy Markdown
Contributor

Converted to a draft since it's not possible to safely merge it yet.

@grynspan grynspan marked this pull request as draft February 12, 2026 14:19
@finagolfin finagolfin force-pushed the droid branch 2 times, most recently from bd60401 to f43b73d Compare February 20, 2026 03:19
@finagolfin
Copy link
Copy Markdown
Member Author

Rebased and changed official workflow version to get an NDK fix, swiftlang/github-workflows#235, so this pull's CI finally uses the NDK 28 it requires and passes.

This change requires Android NDK 28 or later, but the Swift SDK for Android is still stuck on LTS NDK 27, which doesn't have the header annotations to support this Android availability feature.

I just tried changing the availability condition to if __NDK_MAJOR__ > 27, #available(Android 33, *) {, using that NDK C define, which worked fine with NDK 29 but doesn't compile with NDK 27, as it still looks for backtrace without the required NDK header changes, /home/finagolfin/swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift:82:27: error: cannot find 'backtrace' in scope.

I think this would be fixed if we had compile-time NDK versioning, swiftlang/swift#81402, but for now, users will have to choose one NDK or the other and we cannot merge this till all Android CI switch to NDK 28 or later.

@grynspan
Copy link
Copy Markdown
Contributor

I just tried changing the availability condition to if __NDK_MAJOR__ > 27, #available(Android 33, *) {, using that NDK C define, which worked fine with NDK 29 but doesn't compile with NDK 27, as it still looks for backtrace without the required NDK header changes, /home/finagolfin/swift-testing/Sources/Testing/SourceAttribution/Backtrace.swift:82:27: error: cannot find 'backtrace' in scope.

Yeah that's not surprising; the __NDK_MAJOR__ test is a runtime test (optimized away obviously) and the symbol is still "needed" at least in debug builds.

I probably said "don't do this" previously, but to cut the knot: maybe add an swt_backtrace() thunk in Stubs.h that checks #if __NDK_MAJOR >= 33 and skip the runtime checks for now?

@finagolfin
Copy link
Copy Markdown
Member Author

Heh, nice idea but I'd rather not resort to C, will just wait till we update the NDK. 👍

@finagolfin
Copy link
Copy Markdown
Member Author

Rebased and updated the workflow version to build this against the 6.3 release, no problem, with the trunk snapshot failures a known issue for all SDKs- hence the trunk wasm failures too- that we're told will be fixed in the next trunk tag.

The plan is to update the trunk Android CI to a newer NDK soon, after which the only question is whether we should try to get this into 6.3 too, which will require updating the 6.3 CI to NDK 28+ also.

@finagolfin finagolfin marked this pull request as ready for review April 21, 2026 02:57
@finagolfin
Copy link
Copy Markdown
Member Author

finagolfin commented Apr 21, 2026

Alright, this is finally ready to go, now that both trunk CI that build Android SDKs have been switched to NDK 28c, the minimum NDK version that supports this availability feature, and I made sure this CI only testsbuilds this Android code with the trunk snapshot Android bundles and those newer NDKs.

Pinging @swiftlang/android-workgroup and the Testing reviewers for any final review.

Comment thread .github/workflows/pull_request.yml Outdated
enable_freebsd_checks: true
freebsd_swift_versions: '["nightly-main"]'
freebsd_os_versions: '["14.3"]'
android_sdk_versions: '["nightly-main"]'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are similar changes required in the other workflow files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll take a look.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see no other workflows in this repo building for Android, is there somewhere else you think we should enable Android also?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

main_using_main and main_using_release should probably be building for Android, but if they're not currently then we can add that in a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added those and removed this android_sdk_versions restriction, as I believe building with 6.3 will work, provided we only use NDK 28 or above. We just don't want this pull merged back to the release/6.3 branch, which still supports NDK 27.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Passed all Android CI, as expected. 👍

Comment thread Sources/Testing/SourceAttribution/Backtrace.swift Outdated
Comment thread Sources/Testing/SourceAttribution/Backtrace.swift Outdated
@finagolfin finagolfin force-pushed the droid branch 2 times, most recently from 26f0e0d to 05e9581 Compare April 21, 2026 17:35
@finagolfin finagolfin changed the title Use the new #available(Android <API>, *) instead from Swift 6.3 to look for backtrace() Use the new #available(Android <API>, *) instead to look for backtrace() Apr 21, 2026
…race()`

Mads added this compiler feature for Android in swiftlang/swift#84574 before the
6.3 branch, so update the Android triples and NDK versions to test it, as it requires
NDK 28 or later.
enable_wasm_sdk_build: true
wasm_sdk_versions: '["nightly-6.3"]'
enable_android_sdk_build: true
android_sdk_versions: '["6.3", "nightly-6.3"]'
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Would you like me to update all the others to "6.3" also, as the 6.3 snapshots won't be tagged much going forward?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can do that in a separate PR.

@finagolfin finagolfin merged commit ea09eda into swiftlang:main Apr 21, 2026
32 checks passed
@finagolfin finagolfin deleted the droid branch April 21, 2026 19:42
@hjyamauchi
Copy link
Copy Markdown
Contributor

It seems that the BCNY CI started to fail during the Android xctest build after this change. What'd be the best way to fix it?

D:\r\_work\swift-build\swift-build\SourceCache\swift-testing\Sources\Testing\SourceAttribution\Backtrace.swift:71:27: error: cannot find 'backtrace' in scope
 69 |       if #available(Android 33, *) {
 70 |         initializedCount = addresses.withMemoryRebound(to: UnsafeMutableRawPointer.self) { addresses in
 71 |           .init(clamping: backtrace(addresses.baseAddress!, .init(clamping: addresses.count)))
    |                           `- error: cannot find 'backtrace' in scope
 72 |         }
 73 |       }
D:\r\_work\swift-build\swift-build\SourceCache\swift-testing\Sources\Testing\SourceAttribution\Backtrace.swift:71:62: error: cannot infer contextual base in reference to member 'init'
 69 |       if #available(Android 33, *) {
 70 |         initializedCount = addresses.withMemoryRebound(to: UnsafeMutableRawPointer.self) { addresses in
 71 |           .init(clamping: backtrace(addresses.baseAddress!, .init(clamping: addresses.count)))
    |                                                              `- error: cannot infer contextual base in reference to member 'init'
 72 |         }
 73 |       }

https://github.com/thebrowsercompany/swift-build/actions/runs/24752103243/job/72427215969

@grynspan
Copy link
Copy Markdown
Contributor

What toolchain version are you using, and what NDK version?

@hjyamauchi
Copy link
Copy Markdown
Contributor

We were using NDK r27 and updating to the NDK r28 seems to resolve the issue. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

android 🤖 Android support cross-compilation Compiling tests for other platforms than the host tech-debt 💾 reduces technical debt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants