Kotlin-native: cross-compilation

Created on 16 Nov 2017  路  7Comments  路  Source: JetBrains/kotlin-native

I really like how support for cross-compilation inplemented in meson-build

For new target I just create plain text file and pass in to meson:
meson ... --cross-file osx-clang-64.def ...

osx-clang-64.def:

[host_machine]
system     = 'darwin'
cpu_family = 'x86_64'
cpu        = 'x86_64'
endian     = 'little'

[binaries]
c          = 'c:\\cygwin-cross\\bin\\x86_64-apple-darwin14-clang'
cpp        = 'c:\\cygwin-cross\\bin\\x86_64-apple-darwin14-clang++'
ar         = 'c:\\cygwin-cross\\bin\\x86_64-apple-darwin14-ar'
strip      = 'c:\\cygwin-cross\\bin\\x86_64-apple-darwin14-strip'
pkgconfig  = 'c:\\cygwin-cross\\bin\\x86_64-apple-darwin14-pkg-config'

[properties]
root       = 'c:\\cygwin-cross\\usr\\SDK\\MacOSX10.10.sdk'

linux-gcc-64.def:

[host_machine]
system     = 'linux'
cpu_family = 'x86_64'
cpu        = 'x86_64'
endian     = 'little'

[binaries]
c          = 'c:\\cygwin-cross\\bin\\x86_64-pc-linux-gnu-gcc'
cpp        = 'c:\\cygwin-cross\\bin\\x86_64-pc-linux-gnu-g++'
ar         = 'c:\\cygwin-cross\\bin\\x86_64-pc-linux-gnu-ar'
strip      = 'c:\\cygwin-cross\\bin\\x86_64-pc-linux-gnu-strip'
pkgconfig  = 'c:\\cygwin-cross\\bin\\x86_64-pc-linux-gnu-pkg-config'

[properties]
root       = 'c:\\cygwin-cross\\usr\\x86_64-pc-linux-gnu\\sys-root'

linux-arm-gcc-64.def:

[host_machine]
system     = 'linux'
cpu_family = 'arm'
cpu        = 'armv7hl'
endian     = 'little'

[binaries]
c          = 'c:\\cygwin-cross\\bin\\armv7hl-unknown-linux-gnueabi-gcc'
cpp        = 'c:\\cygwin-cross\\bin\\armv7hl-unknown-linux-gnueabi-g++'
ar         = 'c:\\cygwin-cross\\bin\\armv7hl-unknown-linux-gnueabi-ar'
strip      = 'c:\\cygwin-cross\\bin\\armv7hl-unknown-linux-gnueabi-strip'
pkgconfig  = 'c:\\cygwin-cross\\bin\\armv7hl-unknown-linux-gnueabi-pkg-config'

[properties]
root       = 'c:\\cygwin-cross\\usr\\armv7hl-unknown-linux-gnueabi\\sys-root'

And so on, for any target.

Is it possible to implement something like this in Kotlin/Native?
Not exactly same, of course, but as general idea.

enhancement

Most helpful comment

It also would be very nice to have an ability to cross-compile from any supported os targeting any supported os, right now at least on Linux I can't target Windows and Mac OSX.

All 7 comments

Far step in this direction will be splittling monolitic file konan/konan.properties into multiple files konan/platforms/linux/x64, konan/platforms/linux/arm32_hfp, and so on.
Is it planned, am I understand correctly?

If yes, and if user can add anything here -- in distributed package, not in source tree -- and runtime, stdlib and platform libs will be automatically compiled when needed -- again, in distributed package! -- this will be almost same as in meson.

It also would be very nice to have an ability to cross-compile from any supported os targeting any supported os, right now at least on Linux I can't target Windows and Mac OSX.

With the Linux host it wouldn't be feasible to have cross compilation for Mac OSX due to licensing issues. Cross compilation for Windows on Linux would be feasible provided there is integration with multiple providers (Wine, Proton, VirtualBox, VMWare etc).

That sounds logical. I wonder how that is solved in Golang though, where I can build executables for macOS and Windows from my Linux PC, without the use of Wine or VMs.

@fleskesvor Go was using glibc mainly for usr and net packages and everything else is implemented in pure go without any dependency on glibc (pure assembly code that has access to syscalls through int 0x80). Recently they added pure go impl for net and usr packages. So if you don't use CGO or any package doesn't depend on cgo, you can cross-compile to any available platforms. That's not the case for Kotlin Native, which always depends on the libc.

Oh, I see. Thanks for the explanation. 馃檪 I do use net in one of my cross-platform projects, so I take it that wouldn't have worked until recently. I also use CGO in another project, but that's Linux-only, and I guess it will have to remain that.

Internally, we're moving a lot of stuff from source code to konan.properties. Since 1.4.30, it will be possible to override these values with -Xoverride-konan-properties compiler flag and tune target for your needs.
Proper support for cross-compilation is a much bigger and complicated story, and configurable properties files is the simplest part of the puzzle. It requires changes to all parts of compiler, including cinterop, Gradle plugin and klibs.
Nevertheless, this is a really important and interesting idea. We are moving to YouTrack, so I created a feature request there, where we can continue the discussion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonnyzzz picture jonnyzzz  路  4Comments

talanov picture talanov  路  3Comments

nvlizlo picture nvlizlo  路  4Comments

brettwillis picture brettwillis  路  4Comments

ghost picture ghost  路  4Comments