Posix platforms only, 100% shared code, cannot figure out dependencies and imports

Hi All,

I am trying learn Kotlin by developing a command line application that will be used exclusively on macos and linux, and it is comparatively primitive so 100% of source code will be shared. But I cannot figure out how to make commonMain depend on posix.

I’ve started with csv parser sample, made some changed, but IDEA keeps highlighting those imports with Unresolved reference message:
https://github.com/TheIndifferent/timestampname-ktn/blob/master/src/commonMain/kotlin/CsvParser.kt#L19-L20

And those same imports work without problems in the source root that was passed to fromPreset(presets.macosX64, ...):
https://github.com/TheIndifferent/timestampname-ktn/blob/master/src/timestampnameMain/kotlin/CsvParser.kt#L19-L20

So, is it possible to make commonMain dependant on posix? Is there any other (better?) way to share 100% of the source code?

Common can’t depend on posix since posix is actual only for kotlin-native. It does not make any sense for JVM and JS. What you are probably trying to do is not a multiplatform application, but native application with multiple targets.

Hm that makes sense, and it is true that I was going for kotlin-native first, but looks like it is not supported by IDEA, unlike kotlin-multiplatform, right?

    sourceSets {
        linuxMain { kotlin.srcDir('src/posixMain/kotlin') }
        macosxMain { kotlin.srcDir('src/posixMain/kotlin') }
    }

Or since 1.3.20:

    sourceSets {
        posixMain
        linuxMain { dependsOn posixMain }
        macosxMain { dependsOn posixMain }
    }

The former solution works really nice from the command line, thank you!

Unfortunately it is quite tricky in the IDEA, somehow it finds the single source folder as linux sources and marks imports as Unresolved reference, then if I comment linux section it becomes regognized again, then when I uncomment it everything becomes fine till the next restart.

Do you by any chance know if 1.3.20 way will be fully supported by IDEA plugin? Or if pure kotlin-native will be supported?

One more possible workaround, works both in Gradle and IDEA

final def os = org.gradle.internal.os.OperatingSystem.current()

kotlin {
    final def posixPreset = os.isLinux()   ? presets.linuxX64
                           : os.isMacOsX()  ? presets.macosX64
                           : /*unknown host*/ null
    targets {
        fromPreset(posixPreset, 'posix') {
            compilations.main {
                outputKinds 'EXECUTABLE'
            }
        }
    }
}

1.3.20 way is currently in EAP state, so don’t know.

1 Like

That worked like charm! Thank you!