Building the different konan targets on GitHub Actions

I’m working on a Kotlin multiplatform project (GitHub - rhdunn/xqt-kotlinx-json-rpc: Kotlin multiplatform JSON-RPC 2.0 library) and am trying to figure out how to get the different native konan targets building on GitHub Actions.

In my build.gradle.kts script, I currently have:

val nativeTarget = when ( {
    KonanTarget.MACOS_ARM64 -> kotlin.macosArm64("native")
    KonanTarget.MACOS_X64 -> kotlin.macosX64("native")
    KonanTarget.LINUX_X64 -> kotlin.linuxX64("native")
    KonanTarget.MINGW_X64 -> kotlin.mingwX64("native")
    else -> throw GradleException("Kotlin/Native build target '${}' is not supported.")

// Fix the suffix for maven artifacts, so they are -[target] and not -native.
publishing.publications.getByName("native", MavenPublication::class) {
    artifactId = project.nativeArtifactId(nativeTarget.konanTarget)

I know that I need to have an option to pass the target to the build and modify to use that (or if it is not set). I then need to extend the when block above to list/map all native targets. – This I know how to do.

In my GitHub Actions I have the following matrix definition:

        konan-target: [
          # Tier 1 --
          # Tier 3 --
          - konan-target: linuxX64
            os: ubuntu-latest
          - konan-target: macosX64
            os: macos-latest
          - konan-target: mingwX64
            os: windows-latest

This is the bit I don’t know how to get working. Specifically, how to configure the GitHub Action for each of the tier 1-3 targets in Kotlin/Native target support | Kotlin Documentation.

At the moment, I’m just doing:

    runs-on: ${{ matrix.os }}

to run the build on the correct host OS.

Not an expert in KMP, but have you tried to add some code to your Gradle scripts to know which target to build in each environment? Gradle script files are just code, so you can do whatever you want there.

Projects like Ktor may inspire you, ktor/NativeUtils.kt at main · ktorio/ktor · GitHub and its usage in the Gradle scripts might be a good start.