I want to create a kotlin native project to genera...
# kotlin-native
y
I want to create a kotlin native project to generate a static library. I would like the project to have a structure like this:
Copy code
src/nativeMain // contains "generic" code that can be used in other dir
src/macosMain // contains "specific" code for macOS
src/mingwMain // contains "specific" code for Windows
From my understanding, the
build.gradle
needs to look like this:
Copy code
kotlin {
      macosX64("macos") {
        binaries {
            staticLib {
            }
        }
    }

      mingwX64("mingw") {
        binaries {
            staticLib {
            }
        }
    }
}
But I am unclear how I include
nativeMain
which should be part of the static library as well as how I declare a dependency (
macosMain
depends on
nativeMain
) Any help appreciated.
d
There's isn't a trivial way to do this yet.
For now, you have to create a new sourceSet called
nativeMain
.
Then do the
dependsOn
thing.
The IDE might complain a bit but that's the first step.
l
For the IDE, there's a possible trick that I use: detecting when Gradle is run from the IDE and use symlinks in that case so you see
nativeMain
from every platform main source set. I'm setting this is into the `buildSrc`: https://github.com/LouisCAD/Splitties/blob/557fc72733b49bc9c013c3a34a35cb0b7bc87d07/buildSrc/src/main/kotlin/config/KotlinSourceSetsConfig.kt#L35 If you're interested, I also made a script to create the directories structure and the corresponding symlinks as needed.
y
Wow. Can't believe this is so complicated! It seems weird to me that this is not "standard" since that seems like it should be such a common use case... Thanks for sharing. This will definitely help!
1
m
Alternatively it can be added as `srcDir`:
Copy code
targets.withType(KotlinNativeTarget::class) {
        sourceSets["${targetName}Main"].apply {
            kotlin.srcDir("src/nativeMain/kotlin")
        }
    }
l
Intermediate source sets are perfectly supported by Gradle and the compiler. The tooling (IDE integration) is lagging behind. Note that it doesn't like when you add one directory to multiple source sets: it warns and ignores some of them (which is why I use the symlinks trick, which requires me to manually save the files before commit). Don't you have this issue with the snippet you showed @msink?
m
It works for me, but maybe because in my use case it is always single target on every platform, and no cross-compiling.
l
It must be this. For a project, I had to have 32 bit and 64 bit targets.
y
Since I am only planning to have a single target on every platform then the
srcDir
trick should work for me. I will give this a try. Thanks!
l
You mean a single sourceSet for all targets?
y
Reading your response and Mike's it seems (from Mike) that I should be able to use
srcDir
to accomplish what I want. To wish you responded that there was a problem and to wish Mike responded it worked because in his case "it is always single target on every platform". It is the same use case for me. On each platform (macOs or Windows in my case), I only create 1 library. So for each platform I only need the common code + the "platform" specific code. I thought that was what Mike described. I have not had the chance to try his proposal but doesn't look like I need to create symlink like you do.