Cross-Compiling to Linux from macOS

Hello fellow Swift users,

I’m encountering some problems and I wish to know if anyone here has ever come across something similar.

In essence, I want to cross-compile a Swift Project on macOS to run it on Linux. I’m using the following command to (try) to do so:

swift build -c release --build-path ./.build/linux -Xswiftc -static-stdlib -Xswiftc -target -Xswiftc x86_64-unknown-linux-gnu

The “target” parameter is passing to the “swiftc” program just fine. The problem is that it is overwritten by a “target” parameter added automatically by swift-build (that sets the target as the same arch/OS as the host machine)

How can I prevent that? Is it possible to cross-compile for Linux on macOS?

Thanks in advance,
Rafael Costa
rafael@rafaelcosta.me

1 Like

It is possible, but I don’t know if swift-build supports it. We don’t really have much comprehensive support for cross-compiling right now.

To cross-compile, you need to set the target, sysroot and tools-directory (the latter only required if compiling an executable, not for swiftmodules). You might also need to set the resource-dir to the lib/swift folder where the target’s standard library is.

···

On 21 Sep 2016, at 19:14, Rafael Costa via swift-users <swift-users@swift.org> wrote:

Hello fellow Swift users,

I’m encountering some problems and I wish to know if anyone here has ever come across something similar.

In essence, I want to cross-compile a Swift Project on macOS to run it on Linux. I’m using the following command to (try) to do so:

swift build -c release --build-path ./.build/linux -Xswiftc -static-stdlib -Xswiftc -target -Xswiftc x86_64-unknown-linux-gnu

The “target” parameter is passing to the “swiftc” program just fine. The problem is that it is overwritten by a “target” parameter added automatically by swift-build (that sets the target as the same arch/OS as the host machine)

How can I prevent that? Is it possible to cross-compile for Linux on macOS?

Thanks in advance,
Rafael Costa
rafael@rafaelcosta.me <mailto:rafael@rafaelcosta.me>

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

For example, this is what we do in LLDB to cross-compile the REPL:

···

On 21 Sep 2016, at 19:14, Rafael Costa via swift-users <swift-users@swift.org> wrote:

Hello fellow Swift users,

I’m encountering some problems and I wish to know if anyone here has ever come across something similar.

In essence, I want to cross-compile a Swift Project on macOS to run it on Linux. I’m using the following command to (try) to do so:

swift build -c release --build-path ./.build/linux -Xswiftc -static-stdlib -Xswiftc -target -Xswiftc x86_64-unknown-linux-gnu

The “target” parameter is passing to the “swiftc” program just fine. The problem is that it is overwritten by a “target” parameter added automatically by swift-build (that sets the target as the same arch/OS as the host machine)

How can I prevent that? Is it possible to cross-compile for Linux on macOS?

Thanks in advance,
Rafael Costa
rafael@rafaelcosta.me <mailto:rafael@rafaelcosta.me>

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Thank you Karl

···

Enviado do meu iPhone

Em 21 de set de 2016, às 18:45, Karl <razielim@gmail.com> escreveu:

On 21 Sep 2016, at 19:14, Rafael Costa via swift-users <swift-users@swift.org> wrote:

Hello fellow Swift users,

I’m encountering some problems and I wish to know if anyone here has ever come across something similar.

In essence, I want to cross-compile a Swift Project on macOS to run it on Linux. I’m using the following command to (try) to do so:

swift build -c release --build-path ./.build/linux -Xswiftc -static-stdlib -Xswiftc -target -Xswiftc x86_64-unknown-linux-gnu

The “target” parameter is passing to the “swiftc” program just fine. The problem is that it is overwritten by a “target” parameter added automatically by swift-build (that sets the target as the same arch/OS as the host machine)

How can I prevent that? Is it possible to cross-compile for Linux on macOS?

Thanks in advance,
Rafael Costa
rafael@rafaelcosta.me

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

For example, this is what we do in LLDB to cross-compile the REPL:

https://github.com/apple/swift-lldb/blob/587e781a6fab4db0338a6e71bbc7fc3475f9d43c/tools/repl/swift/CMakeLists.txt#L11