swift-server / swift-backtrace

💥 Backtraces for Swift on Linux and Windows
Apache License 2.0
296 stars 34 forks source link

should be able to be cross-compiled #16

Closed weissi closed 5 years ago

weissi commented 5 years ago

It's very important to not use any #if os(...)/#if arch(...) etc in Package.swift files. The problem with that is that Package.swift is evaluated on the compiling machine for the compiling machine. For cross-compilation however, the compiling machine and the target machine do not match.

https://github.com/ianpartridge/swift-backtrace/blob/f244ad10744fc8e1b9f012682df94a27672c7033/Package.swift#L20-L26

Instead of doing it in Package.swift, CBacktrace/Backtrace should check in the .swift/.c files what platform they're on because that will automatically evaluate to what the target machine is.

ianpartridge commented 5 years ago

I wasn't aware that SPM supports cross-compilation, how is it enabled?

weissi commented 5 years ago

@ianpartridge it's enabled using swift build --destination my-platform.json. The my-platform.json is a description of the target platform you'd like to compile for. There's some more description on the original PR which added that.

To be able to cross-compile, you obviously need an SDK for the target platform which you have to build which can be more or less work, depending on how much your target platform put into their cross-compilable SDK.

Non-embedded Linuxes usually put zero effort in a cross-compilable SDK so that's quite a bit of manual work. As an example, there's a script which builds an Ubuntu 16.04 cross-compilation SDK for macOS. It's a very slim SDK (it's more a demo, the actual implementation could be done a lot better) but it can easily be extended.

I know that the Swift on Raspberry Pi community also uses the cross-compilation support.

weissi commented 5 years ago

@ianpartridge here's the swift-mac2arm-x-compile project's home which I believe is used by RasPi folks.

weissi commented 5 years ago

ha, and a newer, more complete version: https://github.com/CSCIX65G/SwiftCrossCompilers

ianpartridge commented 5 years ago

Thanks @weissi that's really interesting! Do you have a specific my-platform.json that you're interested in having support for?

I'm afraid I won't be able to work in this in the short term (try! Swift NYC followed by vacation 🍹) as it needs some work in https://github.com/ianpartridge/swift-backtrace/blob/master/scripts/vendor-libbacktrace.sh to add the necessary conditional checks to all the .c/.h files.

weissi commented 5 years ago

Thanks @weissi that's really interesting! Do you have a specific my-platform.json that you're interested in having support for?

All of them. It's really straightforward, you just need to remove all #ifs from Package.swift and you're done. If there are no #ifs in the Package.swift and it compiles normally on Linux and Mac (even if it doesn't work), then we're good for now.

I'm afraid I won't be able to work in this in the short term (try! Swift NYC followed by vacation 🍹) as it needs some work in https://github.com/ianpartridge/swift-backtrace/blob/master/scripts/vendor-libbacktrace.sh to add the necessary conditional checks to all the .c/.h files.

Of course, no rush. Just wanted to capture this here. The #ifs for the .c/.h files should be really straightforward.

We could start with making the whole file #ifdef __linux__. An example is the CNIOLinux module in NIO which compiles everywhere but only works on Linux :).

ianpartridge commented 5 years ago

Felt guilty about leaving this for weeks so did it over lunch!

Please review #17. :)

weissi commented 5 years ago

You're awesome @ianpartridge , thanks so much!

ianpartridge commented 5 years ago

Released https://github.com/ianpartridge/swift-backtrace/releases/tag/1.1.1