How should I go about publishing a multiplatform n...
# multiplatform
m
How should I go about publishing a multiplatform native library? Github actions is building them, but since they all have the name "native" it seems to cause some conflicts when publishing to maven, any ideas? I tried changing it to build all 3 but then it refuses to commonize properly and I'm afraid of that causing issues when publishing
a
What native target are you trying to publish?
m
linuxX64, mingwX64 and macosX64, all with the same native code base
The source is at https://github.com/Martmists-GH/kpy-plugin if you want to see the project structure and how cinterops are generated
a
m
correct
a
So the first thing is that:
Copy code
val nativeTarget = when {
        hostOs == "Mac OS X" -> macosX64()
        hostOs == "Linux" -> linuxX64()
        isMingwX64 -> mingwX64()
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }
it is just a trick used in samples to have the right target selected for users on different hosts. It shouldn't be used in libraries that are meant to be published. You need a proper layout with all targets you want to support. Like this one:
Copy code
kotlin {
  macosX64() // what about arm64?
  linuxX64() // what about arm64?
  mingwX64()

  sourceSets {
    val commonMain by getting
    val macosX64Main by getting
    val linuxX64Main by getting
    val mingwX64Main by getting

    val nativeMain by creating {
       this.dependsOn(commonMain)
       macosX64Main.dependsOn(this)
       linuxX64Main.dependsOn(this)
       mingwX64Main.dependsOn(this)
    }
  }   
}
Then upon publishing, you will publish libraries for all targets that can be used by consumers. The only thing is that you have to be aware of host-specific targets such as macosX64. They have to be published separately. So please read https://kotlinlang.org/docs/multiplatform-publish-lib.html It should make it clearer for you.
You can also debug it locally by publishing it to custom directory.
Copy code
publishing {
   repositories {
      maven("/home/username/my-test-repo")
   }
}
m
I had some issues with this setup where the cinterops wouldn't work properly since it would only build for one, and additionally when looking at the json and module metadata generated only ever referred to a single target (like only macos)
a
Can you elaborate a bit more?
m
For example, here's the tooling-metadata.json and as you can see it only mentions the existance of the macos_x64 target since that's the only one github actions was able to build. Would this not create a problem when using this library on linux_x64?
Copy code
{
  "schemaVersion": "1.0.0",
  "buildSystem": "Gradle",
  "buildSystemVersion": "7.4.2",
  "buildPlugin": "org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper",
  "buildPluginVersion": "1.6.20",
  "projectSettings": {
    "isHmppEnabled": true,
    "isCompatibilityMetadataVariantEnabled": false
  },
  "projectTargets": [
    {
      "target": "org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests",
      "platformType": "native",
      "extras": {
        "native": {
          "konanTarget": "macos_x64",
          "konanVersion": "1.6.20",
          "konanAbiVersion": "1.6.0"
        }
      }
    },
    {
      "target": "org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget",
      "platformType": "common"
    }
  ]
}
(this behavior also exists in the .module file)
Also, when attempting to handle cinterop, I get errors when not compiling on the correct platform, hence why I was using the separate setups previously (building on Linux here):
Copy code
> Task :kpy-library:cinteropPythonMingwX64 FAILED
Exception in thread "main" java.lang.Error: /usr/include/python3.10/pyport.h:235:10: fatal error: 'sys/select.h' file not found
	at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:273)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.indexDeclarations(Indexer.kt:1189)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1178)
	at org.jetbrains.kotlin.native.interop.indexer.IndexerKt.buildNativeIndexImpl(Indexer.kt:1174)
	at org.jetbrains.kotlin.native.interop.gen.jvm.DefaultPlugin.buildNativeIndex(Plugins.kt:33)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:272)
	at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.interop(main.kt:76)
	at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:45)
	at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:38)
	at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:60)