Skip to content

[embedded] Implicitly define __APPLE__ and __MACH__ when on -apple-none triples #71278

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

Merged
merged 3 commits into from
Feb 3, 2024

Conversation

kubamracek
Copy link
Contributor

This removes the need for the stdlib and other modules to -Xcc those defines manually, and also drops this requirement from users of the -apple-none triples (see the lit test changes).

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek added the embedded Embedded Swift label Jan 31, 2024
// a few things that the Apple SDKs expect.
if (triple.getVendor() == llvm::Triple::VendorType::Apple) {
invocationArgStrs.insert(invocationArgStrs.end(),
{"-D__APPLE__", "-D__MACH__"});
Copy link
Contributor

Choose a reason for hiding this comment

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

mach should only be defined when the triple contains mach:
technically someone could write xxx-apple-none-elf (granted that would be rather strange...)

Copy link
Member

Choose a reason for hiding this comment

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

+1 this should be conditional object file format == MachO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -631,6 +631,17 @@ importer::getNormalInvocationArguments(
});
}

// To support -apple-none triples, which are non-Darwin, we need to #define
// a few things that the Apple SDKs expect.
if (triple.getVendor() == llvm::Triple::VendorType::Apple) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be done by the driver and not the clang importer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, that would be against the established pattern -- look at what the rest of this (pretty large) function is doing. It's setting up a ton of things, essentially "construct reasonable defaults for a Clang instance given the current Swift compilation", which my addition seems to match.

I'd say this addition shouldn't go to the driver, on the grounds that that would create an inconsistency. Maybe this whole function should go to the driver? (Even that I would probably argue against.)

Copy link
Member

Choose a reason for hiding this comment

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

I'll defer to you, its just a bit odd to splat out the platform knowledge in a bunch of places, cc @artemcm for thoughts (not blocking)

Copy link
Member

Choose a reason for hiding this comment

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

I believe that @rauhul is correct here. __APPLE__ is a vendor definition, and should always be set by clang for *-apple-* triples. I could see the argument for __MACH__ being the clang importer as technically it is the kernel identifier and not the object file format specifier, but it is used for the object file format as well. I think that sinking this into upstream clang is the right approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I misunderstood then. I thought that @rauhul was talking about the Swift driver and that it should receive this logic instead (and pass it as -Xcc flags down to the compiler).

@kubamracek
Copy link
Contributor Author

@swift-ci please test

invocationArgStrs.insert(invocationArgStrs.end(), {"-D__MACH__"});
}
if (triple.isOSBinFormatWasm()) {
invocationArgStrs.insert(invocationArgStrs.end(), {"-D__wasi__"});
Copy link
Member

@compnerd compnerd Jan 31, 2024

Choose a reason for hiding this comment

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

The macro here should be __wasm__. WASI is not related to the object file format but the environment (WASI uses WASM and provides a system interface). This would already be set by clang. This likely should be that the condition is isOSWASI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha, so the reason why I seemingly needed to add -D__wasi__ is that we have SwiftShims code that conditionalizes based on it, and we fail to build the embedded stdlib for wasm-unknown-none-wasm without this -D__wasi__. The concrete piece of code is here: https://github.com/apple/swift/blob/main/stdlib/public/SwiftShims/swift/shims/Visibility.h#L137

Sounds like you're saying we should fix Visibility.h instead?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that definitely looks like a bug. That should be __wasm__.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek merged commit 21eddf0 into swiftlang:main Feb 3, 2024
kubamracek added a commit to kubamracek/swift that referenced this pull request Feb 5, 2024
…o-mach-defines"

This reverts commit 21eddf0, reversing
changes made to e2d516f.
kubamracek added a commit that referenced this pull request Feb 6, 2024
Revert "Merge pull request #71278 from kubamracek/embedded-no-mach-defines"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
embedded Embedded Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants