https://kotlinlang.org logo
#compiler
Title
# compiler
x

xoangon

09/23/2023, 8:33 AM
Basic questions here. My understanding so far is that when we talk about compiler plugins, we’re acting at the compiler backend level. • Are there frontend plugins or plugins that act at both levels? • Am I wrong in my understanding so far? • Is any of this configurable in the
build.gradle.kts
file of the plugin project?
a

apatrida

09/23/2023, 11:14 AM
A smaller sample that reads but does not manipulate IR. This isn't the best Gradle structure though. https://github.com/jisungbin/KotlinCompilerPluginSample?source=post_page-----f4e857be9a1--------------------------------
Arrow project has both Meta and Reflection (FIR) plugins that are aiming at making some things easier such as quick macros in FIR. But these were stalled when the compiler extension API's were changing rapidly, and will likely be picked up and continued soon. They do show some use of various extension points. Although this is obfuscated a bit by using their meta library to abstract the extensiono points, you can see them working on FIR with code generation and swapping code (not all implemented but some is): https://github.com/arrow-kt/arrow-reflection/tree/main/arrow-reflect-compiler-plugin/src/main/kotlin/arrow/reflect/compiler/plugin
x

xoangon

09/23/2023, 11:25 AM
Super useful links there! Those on your first answer go straight to bookmarks 👏🏻
👍 1
a

apatrida

09/23/2023, 11:25 AM
Now, there are notes above if you want to test your FIR plugin in Intellij, you'll need to publish it to local maven repo and open another project in a custom build of intellij-community as mentioned above. And there is a custom setting to change in intellij. I'm looking to create a sample with more notes soon as I start a new project. But for now, most the info is in the links above.
The Gradle setup is a little sensitive for making things work, I'll know more about it soon as I go through that myself.
x

xoangon

09/23/2023, 11:28 AM
I’d like to elaborate on what defines the lever at which an extension is acting, though. Is this something declared in the
build.gradle.kts
file?
a

apatrida

09/23/2023, 11:28 AM
Other notes: Writing your first compiler plugin (backend, but covers other topics) https://resources.jetbrains.com/storage/products/kotlinconf2018/slides/5_Writing%20Your%20First%20Kotlin%20Compiler%20Plugin.pdf This series covers starting of IR extension (backend?): https://blog.bnorm.dev/writing-your-second-compiler-plugin-part-1 (read all parts linked from there) another article: https://betterprogramming.pub/say-hello-to-kotlin-compiler-plugin-f4e857be9a1
I’d like to elaborate on what defines the lever at which an extension is acting, though. Is this something declared in the
build.gradle.kts
file?
no
the registrar decides the extension points
👍🏻 1
x

xoangon

09/23/2023, 11:29 AM
I can see in the sam-with-receiver that these dependencies are explicitly specified but that’s not the case in the Arrow FIR extension that you shared above
a

apatrida

09/23/2023, 11:31 AM
You will want to have a Gradle plugin to load your compiler plugin.
And most of the repos linked above have simple plugins for Gradle. • compiler plugin • gradle compiler plugin (using the helpers given by Kotlin team) • use your compiler plugin in another project via Gradle and for FIR • test in Intellij by running custom build, with the property set allowing loading non bundled (official) plugins
The Gradle part you need to care about is how to make sure you can do integration tests, and what is required to tie the module builds together to do that.
Integration testing is mentioned in the links above (the 6 part series, ending here: https://blog.bnorm.dev/writing-your-second-compiler-plugin-part-6) and this glosses over the topic of
includeBuild
but you can see that in other plugin examples, including some linked above, or checking ones like Cite (J Wharton)
I think the "Redacted" plugin above is the best example for integration testing and
includeBuild
but I haven't dug in a lot to it, It just has a nice package of things you probably need to see.
It likely covers everything you need as an example
Screenshot 2023-09-23 at 5.37.16 AM.png
x

xoangon

09/23/2023, 11:39 AM
As a beginner in this whole compiler thing, there’s a lot here for me to digest 🤓! With all this content I definitely have more questions now than before, but I’m sure there’s a lot of answers in those links
This last screenshot seems to be the short answer my first question, it makes it clear that there are extensions at both levels and how to declare them
a

apatrida

09/23/2023, 11:39 AM
I suggest clone the redacted plugin and learn what it does.
it is medium scope, covers the bases.
not too big, not too small.
Arrow has good information in there, but it is abstracted away from the compiler. It would be a good place for mini-extensions, but not when it is stalled and not being worked on for the moment (they mentioned it "might be a good time to pick it up again", but here we are....)
I'm newer too, so mostly I've just collected the information above and checked out what works and doesn't. So we will both be here asking questions!
🆒 1
🙌🏻 1
x

xoangon

09/23/2023, 11:43 AM
I got first interested into the compiler after some talks in this last KotlinConf. There’s a lot I want to learn before the next one, hopefully by that time I can manage to have some more specific questions for the speakers haha
a

apatrida

09/23/2023, 11:44 AM
One other note, if you are going to do multiplatform support, you need to find examples that show how to test and work with the variations (Cite plugin does this I believe). And if you do Kotlin Native you'll have another set of problems in dependencies, so hard to do Kotlin + Native from the same code base due to one using embedded compiler and the other not (native). So focus on non-native first and then figure out how to abstract to work in native with an adapter or something.
KotlinConf
I hope to make it this year!
🐕 1
K 1
Oh, this template is pointed to from some threads here: https://github.com/demiurg906/kotlin-compiler-plugin-template/tree/master But, I don't think it has the full Gradle setup for integration testing. It does show FIR vs. backend registration as well.
(that Gradle setup is not going to serve you well, but the other information in this template might be useful)
summary: • Redacted plugin is a good Gradle setup, does FIR • Cite is a good plugin to see someone testing Kotlin KMP with a plugin (non-FIR) • Arrow Reflection / Meta is a good sample of more advanced things, but the abstractions might slow you down • The other templates and other samples usually only have part of the puzzle and not really the "full gradle" setup
❤️ 1
x

xoangon

09/23/2023, 11:48 AM
I knew there were a lot of gotchas in the Native side, but I don’t know the specifics yet. I remember Tadeas Kriz telling that at Touchlab they’ve been having a hard time in developing plugins that target native in

this talk

a

apatrida

09/23/2023, 11:49 AM
Native has different dependencies (for now) so causes problems with using things like VirtualFile for example, has one import in non-native and another in native. I'm sure there are things deeper down that are similar.
There are threads about it here you can search for.
I'm going to go now and actually start pulling together a base plugin sample that does very little FIR but works in all the cases mentioned above, and can load into intellij-community custom build and show that FIR is working in the IDE as well.
x

xoangon

09/23/2023, 11:51 AM
Great, thanks a lot mate 🙌🏻! Really hoping to catch up in the next KotlinConf 😎
a

apatrida

09/23/2023, 11:52 AM