Closed weissi closed 5 years ago
I wasn't aware that SPM supports cross-compilation, how is it enabled?
@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.
@ianpartridge here's the swift-mac2arm-x-compile project's home which I believe is used by RasPi folks.
ha, and a newer, more complete version: https://github.com/CSCIX65G/SwiftCrossCompilers
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.
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 #if
s from Package.swift
and you're done. If there are no #if
s 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 #if
s 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 :).
Felt guilty about leaving this for weeks so did it over lunch!
Please review #17. :)
You're awesome @ianpartridge , thanks so much!
It's very important to not use any
#if os(...)
/#if arch(...)
etc inPackage.swift
files. The problem with that is thatPackage.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.