Where can I find examples of compiling Kotlin code...
# k2-adopters
g
Where can I find examples of compiling Kotlin code along with its dependency classpath using K2 + FIR?
d
Starting from
2.0.0-Beta1
K2 compiler is used by default So all you need is just use the compiler from the eap channel and ensure that you didn't manually set
languageVersion
to
1.9
anywhere in your configuration
BTW
2.0.0-Beta2
was released today
g
Ah sorry, I meant programmatically -- as in invoking
IncrementalFirJvmCompilerRunner
I am interested in building some tooling using the K2/FIR compiler and analysis API's 😃
I am having trouble finding documentation/tests that show taking Kotlin source files and creating an analysis + compilation context/execution environment and then compiling the source
d
The easiest way is to check
K2JVMCompiler
class, it's the main entrypoint for CLI version of
kotlinc
Using
IncrementalFirJvmCompilerRunner
is much harder and I suspect that the only user of this entrypoint is Kotlin Gradle Plugin So you can check its sources
g
Ahh okay, that sounds good -- thank you! I was hoping to do some integration between K2 and Gradle, to detect the classpath from Gradle dependencies. I'm trying to investigate the difficulty of writing a basic LSP server for Kotlin.
I think the Gradle Tooling API should make the Gradle part simpler, hopefully?
d
Such a good intent As an example of setup of Analysis API I can recommend to check sources of KSP, as they do exactly this -- set up AA and refer to it during building of KSP symbols
🙏 1
I think the Gradle Tooling API should make the Gradle part simpler, hopefully?
Sorry, don't know about it
👍 1
g
It looks like there is an embeddable jar for KSP as well, that might be a great place to get an analysis foundation -- thanks for the tip! https://github.com/google/ksp/blob/main/symbol-processing-aa-embeddable/build.gradle.kts
👍 1
Thanks again, making some progress now!
Copy code
Project: {ALL_SCOPE_KEY=com.intellij.psi.search.EverythingGlobalScope@3}
   Analyzing module: example, files are:
       [KtFile: example.kt]
Copy code
fun compileAndAnalyzeKotlinFiles() {
    val standaloneAnalysisAPISession = buildStandaloneAnalysisAPISession {
        buildKtModuleProvider {
            val jvmTarget = JvmTarget.DEFAULT
            this.platform = JvmPlatforms.jvmPlatformByTargetVersion(jvmTarget)
            addModule(
                buildKtSourceModule {
                    this.moduleName = "example"
                    this.platform = JvmPlatforms.jvmPlatformByTargetVersion(jvmTarget)
                    val projectPath = "kotlinprojects/example/src/main/kotlin"
                    val examplePath = ClassLoader.getSystemClassLoader().getResource(projectPath)
                        ?: error("Could not find resource $projectPath")
                    this.addSourceRoot(Path.of(examplePath.toURI()))
                }
            )
        }
    }

    println("Project: ${standaloneAnalysisAPISession.project}")
    standaloneAnalysisAPISession.modulesWithFiles.forEach { (ktSourceModule: KtSourceModule, psiFiles: List<PsiFile>) ->
        println("   Analyzing module: ${ktSourceModule.moduleName}, files are:")
        println("       $psiFiles")
    }
}
👍 1