Using Gradle, what would be a good way to pass a f...
# compiler
l
Using Gradle, what would be a good way to pass a function to a compiler plugin from the consumer project? Suppose I have a compiler plugin that does logging. Often you would have some sort of a logging configuration, e.g. whether to include thread names in the logging statements, and it may come as some code in the consumer project. But what if I want to have this configuration at the IR transforming stage? (Code, specifically)
y
Compiler plugin options is the best way probably
KotlinCompilerPluginSupportPlugin
lets you pass some `SubpluginOption`s. You can then read those in your
CommandLineProcessor
, placing them in the
CompilerConfiguration
, and finally you should pass the
CompilerConfiguration
to your IR extension
l
I have some options already, but I'm not sure how to pass code specifically. I think it would make a lot of sense to use code here for flexibility.
y
It's not really possible, at least not in a simple way. Kotlin compilation usually runs in its own daemon, separate from the Gradle build itself. What kind of code are you wanting to pass through anyway? Or do you mean code that'll ultimately be in the binary?
l
I made a compiler plugin that does logging without logger objects (allowing one to just say
trace {}
anywhere) that has nearly no overhead. Which means that in the IR stage the plugin must know how to construct the tag, the logging message, etc. For some things I can use plugin options, or with Java I can use agressive inlining for static things with R8 if I really want no overhead. But the latter is tricky and rather fragile. I'm just thinking, it would really make sense to have this configuration as function defined somewhere in Gradle. For example, I could say something like this
Copy code
myCoolLogging {
    tagSelector = { packageName ->
        when {
            packageName.startsWith(...) -> "App/Service"
            else -> "App"
        }
    }
}
y
What you could do is have the user pass in the fqn of a utility function:
Copy code
myCoolLogging {
  tagSelector = "my.package.myTagSelector"
}
Then in user code:
Copy code
package my.package

fun myTagSelector(packageName: String) = when {
  packageName.startsWith(...) -> "App/Service"
  else -> "App"
}
And have your IR wire a call to it
Or if it's just about package names, you can do a Gradle-side solution Simply find all the packages the user defines (e.g. by reading
package
statements from their files), run the lambda they provide on it, and pass that map as a compiler option
l
I want to select the tag while transforming. Or, do you mean that I can actually call this code from
IrTransformer
?
y
So the first solution would select the tag at runtime, which maybe isn't what you want The second would select it at build time, before the compiler is even called. There's another alternative, which is to basically have a plugin API for your plugin itself. Metro has an example of this, although it's more complicated than what you're looking for here
l
Aha yes I thought that something like this alternative would be one way to do this. Passing a kts for evaluation might be a viable option as well. Thanks!