by default, it gives me the following build ``` ...
# kotlin-native
w
by default, it gives me the following build
Copy code
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"
            }
        }
    }
which works and generates a windows ".exe" file I can also change the first line to a literal
"Linux"
(to pretend I am on a linux) and it works and generates a linux ".kexe" file but I can't figure out how to make it generate both a linux ánd a windows executable
1
e
add all the targets regardless of the current platform, and don't give them the same name
Copy code
macosX64 {
    binaries {
        executable {
            entryPoint = "main"
        }
    }
}
linuxX64 {
    binaries {
        executable {
            entryPoint = "main"
        }
    }
}
mingwX64 {
    binaries {
        executable {
            entryPoint = "main"
        }
    }
}
(you can of course abstract out the common target configuration block as well)
but note that the macos target can only be built on macos (on other platforms it will be disabled by default)
w
but will the linux be built regardless of wether I am on linux, mac or windows?
e
I think so, although I only use Linux so I can't confirm
w
what do I do with the source sets?
e
https://kotlinlang.org/docs/multiplatform-share-on-platforms.html also hierarchy source sets are enabled by default since Kotlin 1.6.0 IIRC
w
it did mention something like that in the build output, but as I am completely new to gradle builds on kotlin native, I have absolutely no idea what it meant
e
you can set up your own hierarchy with
Copy code
kotlin {
    sourceSets{
        val commonMain by getting
        val nativeMain by creating {
            dependsOn(commonMain)
        }
        val macosX64Main by getting {
            dependsOn(nativeMain)
        }
        val linuxX64Main by getting {
            dependsOn(nativeMain)
        }
        val mingwX64Main by getting {
            dependsOn(nativeMain)
        }
    }
}
w
the source sets were defined as
Copy code
sourceSets {
    val nativeMain by getting
    val nativeTest by getting
}
changing that to
Copy code
sourceSets {
    val commonMain by getting
    val commonTest by getting
}
fixed it
(and renaming the source folders accordingly)
e
if you only have native targets, sure that works
if you have some non-native targets then you probably want a parent sourceset for all the native targets, which you can use my previous snippet to manage
w
for now, the code should all be the same on all targets and this project is indeed for windows/linux specific
if I need more, I will probably look at it again, but by then, perhaps I understand gradle and Kotlin MPP a bit more
thanks for now