https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
r

raulraja

03/21/2019, 12:56 PM
Anyone succeeding at meta-programming in MPP projects with kapt or other ways to codegen for all platforms? Kapt is JVM only and we rely on Arrow on annotation processing to generate code at the moment. Not being able to use kapt in MPP projects is currently not allowing to publish for JS and native. Any advice or recommendations are much appreciated. Thanks!
❤️ 2
p

pardom

03/21/2019, 1:03 PM
I don't know much about Kotlin compiler plugins, but I'm guessing you've already looked into them?
r

raulraja

03/21/2019, 1:06 PM
yes, undocumented and unclear how to get the most basic info out of types. Even for the current implementation we are having to use a combination of TypeElement from the Java apis, Kotlin Poet and @Eugenio meta data lib to be able to actually get the Kotlin info out of the types. Meta programming in Kotlin it's in a very sad state unless I'm missing some obvious lib. I don't want to go down the path of parsing sources and coming up with my own AST and pseudo type checker.
g

gildor

03/21/2019, 1:46 PM
What about generating common code as a separate step using Kapt?
it wouldn’t be integrated so seamlessly to build as it happening with JSR-269, but nothing prevent you from using common code add it as source set for annotation processing of JVM module, generate code (Kotlin only, common compatible) and use it as platform/common code
For end user it would be harder, but I think many users of Arrow actually do not use arrow-meta, all other modules may be generated for other platforms
r

raulraja

03/21/2019, 3:29 PM
The issue is that kapt actually has made the decission to not run on MPP projects it requires the projects to be JVM only
l

louiscad

03/21/2019, 5:46 PM
I think here's a great example of multiplatform Kotlin codegen that doesn't rely on annotations to generate other code: https://github.com/square/sqldelight
👏 1
r

raulraja

03/21/2019, 6:00 PM
thanks @louiscad
l

louiscad

03/21/2019, 6:19 PM
It looks so. The authors present on this Slack (
@jw
and
@alec
) may help you if you struggle understand the organization of SQLDelight codegen code. You can also reach them and possibly other maintainers in #squarelibraries and in GitHub issues if needed.
r

raulraja

03/21/2019, 6:22 PM
thanks Louis, much appreciated 👍
j

josephivie

03/28/2019, 3:16 AM
Well... if you're insane, you could always do what I did. Take a look at #mirror. I wrote a plugin that reads
.kotlin_metadata
and
.kt
files to generate code. I'm not sure I recommend it, but it's possible and I've done it. It relies on no PSI stuff.
g

gildor

03/28/2019, 3:20 AM
The issue is that kapt actually has made the decission to not run on MPP projects it requires the projects to be JVM only
This is not a “decision” , but Kapt just based on Java-only API. What I suggested is to use Kapt and JVM to generate common code, this is similar to what Louis also suggested
r

raulraja

03/28/2019, 8:15 AM
@gildor that is what we want to do but all of our attempts to have kapt run over an MPP layout have failed
thanks @josephivie I'll check it out
g

gildor

03/28/2019, 8:16 AM
what do you mean “MPP layout”?
r

raulraja

03/28/2019, 8:16 AM
Modules that are common on MPP projects. Kapt requires the module to be JVM only or we have not figured it out otherwise.
g

gildor

03/28/2019, 8:18 AM
Yes, what I propose, 1 Add common code source set to special JVM module with Kapt 2. generate code using Kapt using common-code 3. add generated code as source set to common module
r

raulraja

03/28/2019, 8:25 AM
thanks @gildor, the issue is that arrow usage of kapt is not limited to the library build but we also export annotations that we ask users to place in places if they want to take advantage of codegen. Is there a way in kapt or in gradle to bundle this behavior so you can just run
kapt
on an common module?
g

gildor

03/28/2019, 8:26 AM
No, you cannot run kapt on common module without writing some additional build step that would generate code first
r

raulraja

03/28/2019, 8:29 AM
ok, I'll look into how this is done in Gradle. Thanks
g

gildor

03/28/2019, 8:33 AM
In Gradle?
r

raulraja

03/28/2019, 8:34 AM
yes, the idea is to create a small plugin as part of arrow meta that when activated in an common module it would take the sources, create a jvm temp project layout, run kapt and copied the codegen over back to the common place.
g

gildor

03/28/2019, 8:55 AM
Yeah, this looks reasonable
j

Jonas Bark

09/02/2019, 7:02 AM
@raulraja have you been able to build your mentioned plugin?
r

raulraja

09/02/2019, 12:14 PM
@Jonas Bark almost there, currently.buikdimg IDEA support and generating synth resolution but met can already do compiler tree transformations in the analysis phase and ir manipulation pretty easily
It automatically subscribes to all the compiler phases that it needs to contribute to so plugin authors can focus just on the tree transformation. I'll announce it once I have a build for others to try
But it leaves kapt behind as this uses the Psi KTdescriptors which are not only easier to use than TypeElement but also contain more details and info about the Kotlin code. Not limited to stubs
Progress here https://github.com/47deg/arrow-meta-prototype but it's not usable yet. We hope to release in October
j

Jonas Bark

09/02/2019, 12:31 PM
okay, thanks for letting me know! I'm gonna look into this as I'm also looking for something like this
BTW, I just stumbled upon https://github.com/Foso/MpApt
r

raulraja

09/02/2019, 1:34 PM
yes, that is great
If I had that then I would not have taken in the venture of arrow meta but now arrow meta is a lot more than annotation processing
🙏 1
e

Eugenio

09/02/2019, 4:23 PM
@raulraja nice progress on arrow meta! does it work on sources only, or also klib?
r

raulraja

09/02/2019, 4:30 PM
Everything related to sources works in the analysys and sythtetic resolution phases as a Tree transformation that modified the document. That is for the CLI compiler, the IDEA implementation runs it inside the SynthResolver phase on demand as the user types providing synth descriptors automatically. Beside these source level transformations user can intercept IR or traditional codegen but that has no impact in IDEA since for that we would have to scan the .class files and generate synth descriptors for them even if they are for the same module
👍 1
We are currently working on the IDEA integration
2 Views