Hello! I have a Kotlin compiler plugin in my MPP p...
# gradle
n
Hello! I have a Kotlin compiler plugin in my MPP project that behaves as Annotation Processor (because Annotation Processors doesn't work on MPP according to

https://youtu.be/w-GMlaziIyo?t=118

). I also have a Gradle Plugin that wraps my plugin, which eventually is used in my main module. It all works nice and smoothly, BUT: I need the plugin to be run before compiling the main project. I was wondering whether I can have something like
compile.dependsOn(something)
, but I don't know what task should I put in the parenthesis (I don't create explicitly any tasks)
f
compileKotlin
n
@Fleshgrinder is it the dependency or depender?
f
Depender:
tasks.compileKotlin.get().dependsOn(yourTask)
You can always run
./gradlew [whatever it is you want to hook into, e.g. publish] --console=plain
and it shows you every task it runs and in which order.
n
Thanks @Fleshgrinder, but I don't know what should it be dependent on. I suppose it should be some default plugin task, like
applyMyPlugin
, but there is no such task
I didn't create any tasks in that plugin, manually at least
f
I missed the last part in your message. Usually there is nothing you need to do here because your plugin is part of the Kotlin compilation. It runs with it.
compileKotlin
is basically the same (not exactly) as
kotlinc File.kt
and with your plugin that would be
kotlinc -Xplugin=path/to/your/plugin.jar File.kt
.
Your Gradle plugin is simply providing that
-Xplugin=
part to the
compileKotlin
task.
n
But is it possible to change the compilation order?
So that the plugin would be run (and generate the classes) before the rest of the project is compiled?
f
Not really because your plugin is executed as part of the compilation. In other words, as part of the class generation (class as in
.class
file, so byte code). You need to add your additional definitions to the AST or directly produce byte code. You cannot produce Kotlin files and feed them in because then it is not a compiler plugin.
The compiler calls your plugin for everything it parses. You subscribed to something and probably filter it. Like Kevin in the video who subscribed to every function that is being parsed. You can now decide when to run (AST, before optimizations, after optimizations, …) and change how the compiler works.
n
Thanks @Fleshgrinder, I'll dig into that direction
My status update is that I was able to force it run a bit earlier than before by registering as a ton of extensions like this:
Copy code
ClassBuilderInterceptorExtension.registerExtension(project, extension)
        ExpressionCodegenExtension.registerExtension(project, extension)
        SyntheticResolveExtension.registerExtension(project, extension)
        StorageComponentContainerContributor.registerExtension(project, extension)
        CompilerConfigurationExtension.registerExtension(project, extension)
        PreprocessedVirtualFileFactoryExtension.registerExtension(project, extension)
Now the build is coming through, BUT in 30% only. It seems like the compiler and my plugin are racing each other, and sometimes, when my plugin wins, the generated files are on their places when the compiler is looking for them. Is there anything I can do about flakiness of the builds?
I solved a problem with a dirty little hack: I moved the generated files and the annotated files to another module and made the main project depend on this new module