Skip to content

[DNM][ClangImporter] Define _GNU_SOURCE - #91465

Draft
glessard wants to merge 6 commits into
swiftlang:mainfrom
glessard:define-gnu-source
Draft

[DNM][ClangImporter] Define _GNU_SOURCE#91465
glessard wants to merge 6 commits into
swiftlang:mainfrom
glessard:define-gnu-source

Conversation

@glessard

@glessard glessard commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

On Linux and other glibc-based platforms, the Glibc module is now imported with
_GNU_SOURCE defined, as it already was when C++ interop is enabled.

Addresses SR-328, SR-360, rdar://162215932, and apple/swift-system#229.

@glessard

Copy link
Copy Markdown
Contributor Author

@swift-ci please test Linux platform

@glessard

Copy link
Copy Markdown
Contributor Author

@swift-ci please build toolchain Linux platform

On Linux and other glibc-based platforms, the `Glibc` module is now imported with
`_GNU_SOURCE` defined, as it already was when C++ interop is enabled. This makes
the following GNU extensions available without workarounds: `dladdr`, `Dl_info`,
`pthread_setname_np`, `sched_getaffinity`, `cpu_set_t`, and `process_vm_readv`.

This is source-breaking for the few functions that have both GNU and XSI variants,
though enabling C++ interop already provoked that source break. Most notably,
`strerror_r` now has the GNU flavor, returning the message rather than an error code:

```swift
var buf = [CChar](repeating: 0, count: 128)
let message: UnsafeMutablePointer<CChar> = strerror_r(errno, &buf, buf.count)
print(String(cString: message))
```

Addresses [SR-328](swiftlang#42950),
[SR-360](swiftlang#42980), rdar://162215932,
and apple/swift-system#229.
@glessard

Copy link
Copy Markdown
Contributor Author

@swift-ci please test Linux platform

@glessard

Copy link
Copy Markdown
Contributor Author

@swift-ci build toolchain Linux platform

@finagolfin

Copy link
Copy Markdown
Member

Interesting, we considered going the other way, to stop defining this for Android 6-7 years ago, but decided it wasn't worth it even that early, as some Swift code on Android probably relied on it by then.

I wonder if the converse isn't true here, ie defining this with glibc too now would screw up too many Swift packages on linux, as can be seen with the failing linux CI builds here. Maybe the @swiftlang/server-workgroup has some input on how hard this glibc change is to pull off.

@glessard

Copy link
Copy Markdown
Contributor Author

We'll see. I didn't find an indication of why it was never done, so I figured I'd find out by trying it. Several projects jump through hoops to define _GNU_SOURCE locally, then import specific headers… it's just not good. Too much of Linux wants this define to exist.
If it requires an upcoming feature flag along with a future language mode, so be it.

@finagolfin

Copy link
Copy Markdown
Member

Not arguing against this, as I honestly don't know what APIs exactly are gated by this define, just thought the server people would have some feedback.

When `_XOPEN_SOURCE` is set, the stored property of `fd_set` is renamed
to `fds_bits` from `__fds_bits`. Add a computed property under the old name,
forwarding to the new name for compatibility.
Adds API notes for socket address parameters.
@glessard

Copy link
Copy Markdown
Contributor Author

swiftlang/swift-testing#1835
@swift-ci Please test Linux platform

@glessard

Copy link
Copy Markdown
Contributor Author

swiftlang/swift-testing#1835
@swift-ci build toolchain Linux platform

@FranzBusch

Copy link
Copy Markdown
Member

I am not opposed to this since we define _GNU_SOURCE in many packages but I think this is worth a thread on the forums.

@glessard

Copy link
Copy Markdown
Contributor Author

Absolutely! This is just a starting point. Note that I don't think that adding apinotes is the right way to do it; we probably need to add support for anonymous unions in ClangImporter.

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