I've been working to get Kotlin/Native to produce ...
# kotlin-native
v
I've been working to get Kotlin/Native to produce a Linux binary (from the curl example project), to run on WSL (Windows Subsystem for Linux). I've finally managed to compile my project, but it's created a KLib file, not a Kexe file. Any ideas?
b
What gradle task did you run?
v
I created a "wsl" target and ran `gradle wslBinaries`:
Copy code
linuxX64("wsl") {
        compilations { "main" {
            cinterops {
                val libCurl by cinterops.creating {
                    defFile(project.file("src/nativeInterop/cinterop/libcurl.def"))
                    includeDirs("src/nativeInterop/cinterop/")
                }
            }
        }}
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }
b
You need to run something like linkWslReleaseExecutable
v
I didn't need to do that with the "hello world" version though.
b
What platform was your hello world for?
Also link tasks are executed as part of build task so might've invoked them unknowingly
Finally, just to be sure, are you building linux target on linux(wsl)?
v
I am trying to cross-compile, from Windows to WSL, as a first step to working out how to cross-compile from Windows to Raspberry Pi Linux.
b
You cannot cross compile native targets
☝️ 1
So to get linux binary you have to build it on wsl
v
What does "native" mean then? I did manage to build Hello World for the Pi.
from Windows.
b
Did you build it on wsl or Windows?
Pi might be an exception then
But sure as hell you need to build on wsl to get linux64 binary
There should be a warning in your gradle output when you try to do that on windows
v
Isn't the whole point of this to build an iOS executable from another platform? So cross compiling?
b
Nope
2
You can ever build ios on ios, regardless of technology
Kotlin mpp is like c. Write once, compile everywhere
v
So to realistically write Kotlin Native code for the Pi, I need to code and build it on the Pi.
b
You might be able to build pi executable on wsl since they're both linux
Not sure why/how you've managed to build hello word for pi on windows
v
Copy code
plugins {
    kotlin("multiplatform") version "1.4.10"
}
group = "org.liamjd.pi"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}
kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64("native")
        hostOs == "Linux" -> linuxX64("native")
        isMingwX64 -> mingwX64("native")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    nativeTarget.apply {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }

    linuxArm32Hfp("Pi") {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }

    sourceSets {
        val nativeMain by getting
        val nativeTest by getting
    }
}
gradle PiBinaries
with main function in src\PiMain\kotlin\main,kt
b
Ok, lots of thinks wrong here
First off, your sourceSet is nativeMain, not PiMain. So the main.kt file is ignored
Secondly, with your target setup, you'll only get .exe when building on windows
Oh wait. Ignore sourceSet comment
v
I've actually got the same code in src/nativeMain and src/PiMain as I experiment. It definitely compiled, and build\bin\Pi\releaseExecutable\KNWorld.exe definitely executes on the Pi.
b
You need to remove nativeMain target as it's jyst confusing you now and has nothing to do with pi target in your setup
Then just run the build on wsl
v
I think I'll focus on the Pi, as that's my real target. Was just using wsl as I thought it would be simpler. A pity IntelliJ doesn't run on arm.
And here's an example of cross-compilation from MacOs to Pi https://github.com/jinqian/kotlin-native-chifoumi

https://www.youtube.com/watch?v=KfdNiMP0emE

b
I see looks like you can compile to pi from any *nix host (this includes wsl)
v
I shall leave you in peace, but here is my "Hello World" repository, cross compiled from Windows (not WSL) to Raspberry Pi. https://github.com/v79/KNWorld
b
Interesting. Still not sure why you keep nativeTarget
v
every example I've found has a nativeTarget 🙂 , I only delete things when I'm sure I don't need them, but I'm still learning what I do and don't need.
sourceSets
doesn't seem necessary; I've commented them out and it works.
b
Got it. So you've managed to produce pi executable on windows? (Non .exe)
v
A Pi executable from windows, yes. It's an elf binary file.
b
Congratz!
v
But next step is to do cinterop, and I thought targeting WSL would make that easier to learn because I could just point my header definitions to files on WSL. And that's where we started; I had a KLib but not a WSL Kexe binary.
b
There's no kexe for pi
Kexe is windows only
As for cinterop, you can install msys2 on windows to have unix env
v
Copy code
pi@raspberrypi:~/PiShare $ uname -a
Linux raspberrypi 5.4.72-v7l+ #1356 SMP Thu Oct 22 13:57:51 BST 2020 armv7l GNU/Linux
pi@raspberrypi:~/PiShare $
pi@raspberrypi:~/PiShare $ file KNWorld.kexe
KNWorld.kexe: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[xxHash]=a487af0917a9a720, not stripped
pi@raspberrypi:~/PiShare $ ./KNWorld.kexe
PiMain: Hello, enter your name:
Liam Davison
Your name contains 11 letters
Your name contains 9 unique letters
pi@raspberrypi:~/PiShare $
Definitely an ELF binary .Kexe file!
b
mind blown i give up 😀
v
You have been very patient with me, thank you!
b
In any case, if you can run that on pi it's fine (learned something new myself)
As for cinterop, install msys2 and required c libs to allow you to work on windows
You can use this for reference
Have a look at the readme as well
Then make sure the libs are installed on both, wsl & msys2. Develop and test on windows, compile the final executable on wsl (even though you can do that on windows, it's better to compile the final binary on wsl to make sure that your cinterop libraries are linked properly for pi)
v
I shall bookmark and read. But WSL will be x64 linux, not ARM won't it?
b
Correct, but it'll still be linux, so lib conventions and paths will be identical. I've found out that unix and windows (via msys2) cinterop is very different when doing that game of life example I've sent you
v
Definitely a project for after lunch. Thanks again for your help and your patience...
b
It might work for you on windows or it might not, so just to save yourself some ambiguous errors I strongly recommend compiling final binaries on linux
No worries, needed just as much (if not more) help myself when I first bashed my head at Kotlin Native, so just giving back to the community :)