Using the standard library with kotlin-js -Xir

I’m trying to compile a Kotlin program into an IR file serialized with protobuf. I tried using the kotlinc-js compiler for that (with <kotlin-home>/bin/kotlinc-js -Xir -Xir-produce-only=klib main.kt -output test -kotlin-home <kotlin-home> -verbose). The compiler always complains about not finding types from the standard library though:

exception: java.lang.AssertionError: Built-in class kotlin.Unit is not found
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns$3.invoke(KotlinBuiltIns.java:113)
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns$3.invoke(KotlinBuiltIns.java:108)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$MapBasedMemoizedFunction.invoke(LockBasedStorageManager.java:440)
        at org.jetbrains.kotlin.storage.LockBasedStorageManager$MapBasedMemoizedFunctionToNotNull.invoke(LockBasedStorageManager.java:515)
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns.getBuiltInClassByName(KotlinBuiltIns.java:362)
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns.getUnit(KotlinBuiltIns.java:437)
        at org.jetbrains.kotlin.builtins.KotlinBuiltIns.getUnitType(KotlinBuiltIns.java:707)

When the -Xir-related flags are omitted, it compiles to js without any problem, but I only need the intermediate representation.

Am I passing some wrong argument here? If kotlinc-js is not suitable to generate IR files, is there another tool I could use?

Such compiler mode requires the standard library in IR format either. We do not ship this library in that format with the compiler yet, so you may need to build it manually. Or you can wait for Kotlin 1.3.60 in which we hope to provide a suitable dual-format standard library for Kotlin/JS.

Thanks for the reply - I ended up passing the source for the standard library as inputs with Xallow-kotlin-package, but I couldn’t quite get it to compile yet.

In the end, I’m trying to compile Kotlin to another target by generating Kotlin interfaces like these, compiling Kotlin using those interfaces to IR and then translate that to the target.
I probably won’t need the whole js library, I mainly looked at kotlinc-js -Xir because it promises to output IR files. I’m wondering if there are other options available to compile Kotlin source code to a (preferably lowered) intermediate representation?

You don’t need to compile the standard library with your code, you need to compile it separately and pass it as a dependency. You can do it currently by checking out kotlin repository (better at the version matching the version of the compiler, supposedly in 1.3.50 branch) and then running gradlew :kotlin-stdlib-js-ir:assemble task. The resulting artifact will be produced in the libraries/stdlib/js-ir/build/libs directory.