https://kotlinlang.org logo
Title
m

martmists

04/20/2022, 5:48 PM
How would I use KSP in a gradle plugin to generate additional code based on annotations? I was working with an IR plugin before, but generating code instead of IR seemed better suited for my use case.
b

Big Chungus

04/20/2022, 5:55 PM
1. Implement ksp processor as a separate module 2. Apply ksp plugin 3. Add your processor to the relevant ksp configuration
Or just implement the processor and explain your users how to add it to their ksp projects
t

Ting-Yuan Huang

04/20/2022, 5:56 PM
If OP means applying KSP in another Gradle plugin programmatically, I think it should work.
m

martmists

04/20/2022, 6:33 PM
Yeah it's supposed to add some functions to a K/N project, to be specific I'm aiming to make a plugin that just allows marking a class with @PyExport and it'll generate the staticCFunctions and such for use with the Python C API.
t

Ting-Yuan Huang

04/20/2022, 6:56 PM
KSP allows processors to visit all files / classes / etc. It doesn't require them to be marked with any annotation to be processed. So in your use case it might be possible to just use KSP without a customized plugin.
m

martmists

04/20/2022, 7:21 PM
I mean I still need it to be bundled in the plugin, how else would you apply it? It also should only export things marked by the annotation so I'll need to figure out some way to check for them anyway
b

Big Chungus

04/20/2022, 7:33 PM
You don't, you publish both, plugin and processor as separate artefacts. Gradle will then download both when applied
m

martmists

04/20/2022, 7:42 PM
Do you have an example project so I can see how it's made and used?
b

Big Chungus

04/20/2022, 7:44 PM
Not ksp, but the idea is the same github.com/mpetuska/klip
Gradle plugin applies kotlin plugin and then adds compiler plugin (published separately)
m

martmists

04/20/2022, 7:50 PM
And do you have an example KSP project to see how I'd go about generating large amounts of source code based on annotations? And would I still need to write a kotlin compiler plugin or does KSP work differently?
b

Big Chungus

04/20/2022, 7:51 PM
Ksp processor kinda IS a compiler plugin. Just with stable api and read-only source access
You'd then just apply processor artefact exactly like you would in a consumer project (read the docs), just from your own plugin.