I'm making another attempt at having Kotlin-to-Pyt...
# compiler
p
I'm making another attempt at having Kotlin-to-Python conversion. For context, the first attempt was at https://github.com/krzema12/kotlin-python where I forked the Kotlin repo and based on the JS-IR backend. I abandoned it because the iteration cycle in the Kotlin monorepo was pretty slow, and the stuff from the JS backend started to confuse me more than help, so I just gave up. The second attempt I have here: https://github.com/krzema12/ko2py, and it's a
IrGenerationExtension
-based compiler plugin. Starting with a clean slate, hoping to push things forward. I have a question regarding producing a Python-specific stdlib. In the first approach, I could process the stdlib and include it in a bundle (like it's done for JS, the stdlib is embedded in the bundle - see here for an example output for this input). Now that I don't have access to the whole internal compiler infra that could include the stdlib in the compilation, I'm wondering what's the best way to go. In particular, is there a way for a compiler plugin to access the IR of the stdlib (or basically any other module/library), so that the visitor can process it? When my visitor visits e.g. an
IrCall
and I'd like to access the IR of the function being called, I'm checking with a debugger that
expression.symbol
is like
FUN IR_EXTERNAL_DECLARATION_STUB name:println visibility:public modality:FINAL <> (message:kotlin.Int) returnType:kotlin.Unit [inline]
with no access to the body, and
expression.target
is an instance of
Fir2IrLazySimpleFunction
. I presume it's all the compiler has available when compiling the module that uses the stdlib. Technically it makes sense to me because why would the compiler have to compile the stdlib again, but still maybe it's possible to access the stdlib's IR somehow from this perspective? If the above doesn't lead to anything, my idea is to get back to the Kotlin monorepo, and apply my Kotlin-to-Python compiler plugin. This way the general Kotlin-to-Python conversion would stay separated in a form of a compiler plugin (much easier to iterate on vs. the Kotlin monorepo), and the stdlib would have be produced from within the Kotlin monorepo. It's just I'm a bit lost in the plethora of modules related to the stdlib, and confused by the custom Gradle utils. Is it realistic to hook up my compiler plugin somewhere? Any thoughts appreciated! 🙏
K 2
gave it a try by just adding my compiler plugin from Maven Local to
libraries/stdlib/build.gradle.kts
(), and I'm getting:
Copy code
Circular dependency between the following tasks:
:kotlin-stdlib:compileCompileOnlyDeclarationsJava
\--- :kotlin-stdlib:compileCompileOnlyDeclarationsKotlinJvm
     \--- :kotlin-stdlib:jvmJar
...
maybe it would be easier to copy over stdlib's code to a fresh project, and compile it there with the plugin... not as a long-term approach, but to get started with something
ok, I want to bite the bullet and build it from within the Kotlin monorepo, but I'm failing to build the repo locally. I tried on GitHub Actions to hopefully have something reproducible, but it's failing: https://kotlinlang.slack.com/archives/C0BUHC9HD/p1758635623977979