Skip to content

Handle strerror_r return type of UnsafeMutablePointer<CChar> for Android #499

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

Closed
wants to merge 1 commit into from

Conversation

marcprux
Copy link
Contributor

This fixes the Android build error:

/opt/src/github/swift-everywhere/swift-tools-support-core/Sources/TSCBasic/misc.swift:331:24: error: binary operator '==' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int32'
329 |                 var buf = [Int8](repeating: 0, count: cap)
330 |                 let err = TSCLibc.strerror_r(errno, &buf, buf.count)
331 |                 if err == EINVAL {
    |                        |- error: binary operator '==' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int32'
    |                        `- note: overloads for '==' exist with these partially matching parameter lists: (Int32, Int32)
332 |                     return "Unknown error \(errno)"
333 |                 }

/opt/src/github/swift-everywhere/swift-tools-support-core/Sources/TSCBasic/misc.swift:334:24: error: binary operator '==' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int32'
332 |                     return "Unknown error \(errno)"
333 |                 }
334 |                 if err == ERANGE {
    |                        |- error: binary operator '==' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int32'
    |                        `- note: overloads for '==' exist with these partially matching parameter lists: (Int32, Int32)
335 |                     cap *= 2
336 |                     continue

/opt/src/github/swift-everywhere/swift-tools-support-core/Sources/TSCBasic/misc.swift:338:24: error: binary operator '!=' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int'
336 |                     continue
337 |                 }
338 |                 if err != 0 {
    |                        |- error: binary operator '!=' cannot be applied to operands of type 'UnsafeMutablePointer<CChar>' (aka 'UnsafeMutablePointer<Int8>') and 'Int'
    |                        `- note: overloads for '!=' exist with these partially matching parameter lists: (Int, Int)
339 |                     fatalError("strerror_r error: \(err)")
340 |                 }

@marcprux
Copy link
Contributor Author

@finagolfin does this seem right to you? You mention avoiding using the GNU strerror_r by specifying -Xlinker -landroid-spawn at https://github.com/finagolfin/swift-android-sdk/blob/53495a4adf9695395a65cec2dc2faa75962a4583/swift-android-ci-release.patch#L163C22-L163C32 , but that didn't change anything when I tried it manually.

@marcprux marcprux marked this pull request as draft March 19, 2025 14:33
marcprux added a commit to swift-everywhere/swift-package-builds that referenced this pull request Mar 19, 2025
@@ -327,7 +327,12 @@ extension SystemError: CustomStringConvertible {
var cap = 64
while cap <= 16 * 1024 {
var buf = [Int8](repeating: 0, count: cap)
#if os(Android)
let errptr: UnsafeMutablePointer<CChar> = TSCLibc.strerror_r(errno, &buf, buf.count)
let err = Int(errptr.pointee)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right. I think that this is missing a wrapper (is strerror_r implemented as a macro?

@finagolfin
Copy link
Member

You mention avoiding using the GNU strerror_r by specifying -Xlinker -landroid-spawn

No, that flag has nothing to do with strerror_r, either I miswrote or you are misremembering.

@jakepetroules fixed this in #497 last month, but it was reverted later because of an issue on Amazon Linux. If you want to fix this, I suggest you bring back that commit and modify it so it works on that linux distro too.

@marcprux
Copy link
Contributor Author

It looks like the reversion was #498. @rintaro, did this fix the Amazon Linux linker error?

@rintaro
Copy link
Member

rintaro commented Mar 19, 2025

did this fix the Amazon Linux linker error?

Yes.

@finagolfin
Copy link
Member

Why are you seeing this error on Android in the first place, building this package as a dependency of something else? As that linked pull notes, passing -Xswiftc -Xcc -Xswiftc -U_GNU_SOURCE to SwiftPM should work around that.

marcprux added a commit to skiptools/swift-android-action that referenced this pull request Mar 19, 2025
@marcprux
Copy link
Contributor Author

As that linked pull notes, passing -Xswiftc -Xcc -Xswiftc -U_GNU_SOURCE to SwiftPM should work around that.

Yes, -Xswiftc -Xcc -Xswiftc -U_GNU_SOURCE does fix it. I had confused the comment in swift-android-ci-release.patch to think that it was the intention of the -Xlinker -landroid-spawn flag that I mentioned earlier. But it turns out that the linker flag is needed too, of else we hit a bunch of errors like ld.lld: error: undefined symbol: posix_spawnattr_init when linking the test runner executable.

I was hitting this error with my periodic CI for swift-everywhere.org at https://github.com/swift-everywhere/swift-package-builds/actions for swift-package-manager. I've added that flag to the swift-android-action, and it seems to work now, so this can probably be closed unless anyone thinks it is worthwhile to try to fix it in code…

@finagolfin
Copy link
Member

I plan to resurrect #497 later, feel free to beat me to it. 😉

@finagolfin finagolfin closed this Mar 19, 2025
@finagolfin
Copy link
Member

Looking into it in #500. Btw @marcprux, if you're adding that -U_GNU_SOURCE for all packages, that could break some other package instead (no problem with -landroid-spawn though).

@marcprux
Copy link
Contributor Author

Looking into it in #500. Btw @marcprux, if you're adding that -U_GNU_SOURCE for all packages, that could break some other package instead

Yeah, I just noticed that swift-nio fails to build with it.

(no problem with -landroid-spawn though).

Actually, I'm seeing that it is failing with ld: library 'android-spawn' not found with the 6.1 nightly SDK but working in 6.0 for some reason.

I'll just remove all the flags and pin my hopes on #500 🥹

@finagolfin
Copy link
Member

Actually, I'm seeing that it is failing with ld: library 'android-spawn' not found with the 6.1 nightly SDK but working in 6.0 for some reason.

Huh, that appears to be this bug with macros when cross-compiling and applying command-line flags, perhaps getting worse in 6.1. Can't you just apply some per-package fixes on your CI for now, instead of applying these CLI flags universally?

I'll just remove all the flags and pin my hopes on #500 🥹

That will only fix this repo and those that depend on it not having to -U_GNU_SOURCE, it does nothing for the occasional -landroid-spawn dependency.

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.

4 participants