Question about cinterop. Can this be used to produ...
# kotlin-native
w
Question about cinterop. Can this be used to produce bindings in a shared module across many native targets? I have made a little example where I compiled a static library test.a in mingwx64 and then used cinterop in gradle to link the static file and create bindings from the header. That all works. Now let's say I have a sourceSet named nativeMain that other sourceSets like mingwX64Main depend on. How do I reference the generated bindings in that shared sourceSet? Currently I have a kotlin block in my build.gradle.kts configured like so:
Copy code
mingwX64 {
    this.compilations["main"].cinterops {
        create("test") {
            packageName = "github.fatalcatharsis.test"
            includeDirs.allHeaders(project.rootDir.resolve("headers"))
            extraOpts("-libraryPath", "${project.rootDir}/src/mingwX64Main/resources/", "-staticLibrary", "test.a")
        }
    }
}

sourceSets {
    val commonMain by getting {
        dependencies {
            implementation(kotlin("stdlib-common"))
        }
    }
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
        }
    }

    val nativeMain by creating {
        dependsOn(commonMain)
    }

    val mingwX64Main by getting {
        dependsOn(nativeMain)
    }
}
Code that I put in src/mingwX64Main/kotlin gets highlighted properly and builds using the definitions in my c header. But nativeMain obviously does not. Is there any way to have the header bindings available to a shared source set?
m
Should work, if you have in
gradle.properties
Copy code
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.enableHierarchicalCommonization=true
w
Nope no dice. I've added those to the gradle.properties, but any code in src/nativeMain/kotlin referencing kotlinx.cinterop.* and anything from my static lib is highlighted in red and results in unresolved reference compile errors.
Oh interesting. I caught this detail at the end of the documentation on shared code in mpp and when I set this property kotlin.native.enableDependencyPropagation=false, it then allows me to use kotlinx.cinterop.* objects and functions, but content from my test.a library is still not available.
I was then able to remove that property and I was still able to use kotlinx.cinterop.*. Not sure what's up with that. Still cannot figure out how to make calls in test.a available.
m
What Kotlin version? It works (almost) here: https://github.com/msink/kotlin-libui-hmpp-bug except for bug tracked in https://youtrack.jetbrains.com/issue/KT-51377
w
currently using kotlin("multiplatform") version 1.6.10, I see that that project is using a development 1.7.0, I'll give that a shot and see if it's just a bug that was fixed
Man I am very confused. Switching to that 1.7.0 version didn't work either, but when I switched back to 1.6.10, neither mingwX64Main and nativeMain could see my library bindings. Did full invalidate caches and restart, no dice. So I just reinstalled intellij. Now not only do my bindings work in the mingwX64Main source set, they now work in the nativeMain source set even with kotlin version 1.6.10. So I'm just gonna chalk this up to intellij weirdness. Thanks for your help!