Oleg Yukhnevich
08/21/2023, 3:55 PMdependsOn
of commonMain/commonTest
to some other source set (as from what Iâve found in current master branch on GitHub)
What would be a suggestion on how to provide autogenerated code which should be accessible from commonMain
?
The question is for ffi-kotlin
where I would want to generate some code with bindings (for different platforms) and then attach it to specific source sets/compilations
F.e. for project with 2 targets (like jvm and macosArm64) I would want:
1. generate common
code with expect
functions/classes and make it accessible from commonMain
2. generate jvm
specific code which will actualise those expects
and maybe add some new declarations - should be accessible from jvmMain
3. generate macosArm64
specific code - same as for jvm
In comparison to how cinterop
do it, I would want not to declare dependency on some generated klib, but link directly sources, at least to be able to mark some of those declarations internal
in generated code, so they will be accessible inside sourceSets in module, but not outside.
I found, that itâs possible to create separate compilation in platform targets, and associate main
compilation with created one - it works, and I can access external internal
declarations inside jvmMain
(IDEA donât like such code, but compilation works) - but itâs not possible to add additional metadata
compilation, so in the end it doesnât work for my use-case
Of course itâs possible to just directly add those sources to commonMain
sourseSet, like sourceSets.commonMain.get().kotlin.srcDir("PATH")
- But Im not sure itâs a good idea, as those sources are mutable by anyone, and could be easily replaced by some other plugin or even by user, when she prefer to use other convention to sources folders
Note: those declarations of course should be accessible after publication to library consumersOleg Yukhnevich
08/22/2023, 7:47 AMkotlin {
// applied by default via KGP from 1.9.20 (with different API)
targetHierarchy.default()
// will be applied in foreign plugin, could be not default, but here it's ok
targetHierarchy.default {
sourceSetTrees(KotlinTargetHierarchy.SourceSetTree("libcrypto"))
}
jvm {
compilations {
val main by getting
val libcrypto by creating
main.associateWith(libcrypto)
}
}
macosArm64 {
compilations {
val main by getting
val libcrypto by creating
main.associateWith(libcrypto)
}
}
macosX64 {
compilations {
val main by getting
val libcrypto by creating
main.associateWith(libcrypto)
}
}
linuxX64 {
compilations {
val main by getting
val libcrypto by creating
main.associateWith(libcrypto)
}
}
metadata {
compilations.all {
println("${target.name} -> ${name} -> ${defaultSourceSet.name} -> ${defaultSourceSet.dependsOn.map { it.name }}")
}
}
}
Oleg Yukhnevich
08/22/2023, 7:48 AMmetadata
compilations were created and there is no association in intermediate sourcesOleg Yukhnevich
08/22/2023, 7:49 AMMargarita Bobova
08/22/2023, 11:49 AMa-dd
08/22/2023, 12:39 PMSebastian Sellmair [JB]
08/22/2023, 12:48 PMOleg Yukhnevich
08/22/2023, 12:54 PMplugin
could easily just replace srcDir
with setSrcDirs
- and generated bindings will be lost.
Use case for replacing default srcDirs
which I know is to f.e. change directory layout from src/commonMain/kotlin
to f.e. common/src
(like in kotlinx.* repositories) or because any other different from default convention for sources. And AFAIK there is no really strait forward way to know when this replacement will be called (in other gradle plugin or in user script or in some convention plugin)Sebastian Sellmair [JB]
08/22/2023, 2:26 PMSebastian Sellmair [JB]
08/22/2023, 2:26 PMOleg Yukhnevich
08/22/2023, 2:37 PMSebastian Sellmair [JB]
08/22/2023, 2:38 PMOleg Yukhnevich
08/22/2023, 2:41 PM