How do I generate entry point for Kotlin/JS progra...
# compiler
g
How do I generate entry point for Kotlin/JS program from IR? I wrote a plugin that generates
main
function (tried generating both
main()
and
main(args: Array<out String>)
), but it seems that the function is ignored (and maybe, it is even swallowed by DCE). What should I do? I don’t want to expose the function in FIR, but if it’s a must then OK, I’ll expose it.
Stumbled upon the same problem for Kotlin/Native (
mingwX64
): for some reason my
main
function cannot be found by the FQ name.
p
Can you try calling pluginContext.registerAaMetadataVisible (the name is from memory, maybe slightly different) on this function? I'm not sure why exactly this would be important, but can imagine it would be (especially before 2.4).
g
OK, I’ll try in a minute then. P.S. I am using Kotlin 2.5.0-Beta2 right now.
I stumbled upon a problem that I generate additional
IrFile
which has to have metadata before the entry point is registered to metadata. But I don’t see a way to add the file to metadata. I am finding a workaround for now…
I fixed the problem by manually inserting the metadata. (Also, I had to make
Fir2IrIrGeneratedDeclarationsRegistrar.session
accessible via reflection. Saying a fact. Nothing more.) But adding the metadata didn’t help 😞
p
Hm. In which package is your main? At least for native, explicit argument for link task is required for non-root package main (e.g. this used to call tests entry point for test task). I think js have something similar.
g
I generate (only in IR) a file with package FQ name
dev.lounres.kone.plugin.benchmarks.kotlinxBenchmarkSupport.generated
and name
run.kt
. In it, I generate a function with name
main
. Then I configure executable entry point via
Copy code
mingwX64 {
    binaries {
        executable {
            entryPoint = "dev.lounres.kone.plugin.benchmarks.kotlinxBenchmarkSupport.generated.main"
        }
    }
}
I hope, I didn’t mess up in the configuration.
p
Yes, sounds legitimate. Some ideas of troubleshooting: 1. Try to write the code you trying to generate manually, see if it works that way (that would help to understand if this is plugin or configuration issue) 2. Check which actual arguments are passed to the link task 3. If 1 works, you can compare generated IR in both cases with klib utility from k/n distribution.
g
Thanks! Then I’ll go through the plan and leave a comment here about my results later.
g
Just confirmed that it’s not configuration problem. It seems compiler ignores the newly generated file. Here is a description of what I’ve done and my conclusions. 1. I have a not-that-small (Gradle) project called
Kone
where there are several modules with one compiler plugin each. (Not all modules contain compiler plugins but there are 5 of them that contain.) One of the modules is called
:plugins:suppliedTypes
and another one (the one that I discussed earlier here) is
:plugins:benchmarksKotlinxBenchmarkSupport
. The project is accessible on GitHub (I push all commits to the
main
branch for now, so there are no other branches to check out): github.com/lounres/kone 2. To test functionality of the project in place, I created another (Gradle) project
Kone-sandbox
in a sibling directory. The project consists of only one module. The module declares dependencies on modules of the
Kone
project. 3. Usual KMP dependencies are declared using the standard way, via
kotlin.sourceSets.<source set name>.dependencies
DSL. Also, the plugin modules are applied as well using the following DSL:
Copy code
dependencies {
    add(PLUGIN_CLASSPATH_CONFIGURATION_NAME, <plugin module dependecy notation>)
    add(NATIVE_COMPILER_PLUGIN_CLASSPATH_CONFIGURATION_NAME, <plugin module dependecy notation>)
}
That’s the setup. Now I move to the experiment itself. 1. In
suppliedTypes
module, plugin modifies only existing files. Specifically, it changes (and copies in IR) functions and classes marked with
@Suppliable
annotation and replaces behaviour of calls to the functions. Yeah, it’s not a simple plugin, but it is tested more or less. And in the following text, it will be used just in one case as a litmus test that the plugins are actually consumed by the compiler. 2. In
benchmarksKotlinxBenchmarkSupport
module, plugin only creates a new file with a new simple content in it. It’s not tested well. There is exactly one test in it. But it’s obvious to me that the plugin adds a new file. (It’s very bad argumentation. But I hope you believe me here.) 3. Now, I add some code to the
Kone-sandbox
module and run
mingwX64Klib
task. After which, I use
klib dump-ir
from the
klib
distribution (that is found by path in
Kone-sandbox/build/konan.txt
) to review the generated bytecode. 4. In the IR I find traces of
suppliedTypes
compiler plugin. But no traces of a new file that should have been added by the
benchmarksKotlinxBenchmarkSupport
compiler plugin. (Even the specific package
dev.lounres.kone.plugin.benchmarks.kotlinxBenchmarkSupport.generated
where the new file should be placed does not appear anywhere.) That’s why I think that the compiler just ignores the file. So it looks like a compiler bug to me. I can create minimized reproducible example of
Kone-snadbox
if you (or someone else) need. Should I create a ticket on this in YouTrack? I can also try to create a plugin that adds a new file and changes an existing one to reveal the bug even more so that the example might be more trustworthy.
Found a small bug that prevented the
benchmarksKotlinxBenchmarkSupport
compiler to do anything on Native platform. This is very stupid of me… But now I have a similar plugin in the same source set that uses FIR for generation of the entry point and shares most of its IR logic with a IR-only version. And this version works perfectly! So I still think it’s an unusual compiler bug.