org.jetbrains.kotlin.cli.metadata.FirKlibMetadataSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.cli.metadata
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.packageFqName
import org.jetbrains.kotlin.fir.pipeline.*
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.fir.serialization.FirKLibSerializerExtension
import org.jetbrains.kotlin.fir.serialization.serializeSingleFirFile
import org.jetbrains.kotlin.library.SerializedMetadata
import org.jetbrains.kotlin.library.metadata.KlibMetadataHeaderFlags
import org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf
import java.io.File
/**
* Produces metadata klib using K2 compiler
*/
internal open class FirKlibMetadataSerializer(
configuration: CompilerConfiguration,
environment: KotlinCoreEnvironment
) : AbstractFirMetadataSerializer(configuration, environment) {
override fun serialize(analysisResult: List, destDir: File): OutputInfo? {
val fragments = mutableMapOf>()
for (output in analysisResult) {
val (session, scopeSession, fir) = output
val languageVersionSettings = environment.configuration.languageVersionSettings
for (firFile in fir) {
val packageFragment = serializeSingleFirFile(
firFile,
session,
scopeSession,
actualizedExpectDeclarations = null,
FirKLibSerializerExtension(
session, scopeSession, session.firProvider, metadataVersion, constValueProvider = null,
allowErrorTypes = false, exportKDoc = false,
additionalMetadataProvider = null
),
languageVersionSettings,
)
fragments.getOrPut(firFile.packageFqName.asString()) { mutableListOf() }.add(packageFragment.toByteArray())
}
}
val header = KlibMetadataProtoBuf.Header.newBuilder()
header.moduleName = analysisResult.last().session.moduleData.name.asString()
if (configuration.languageVersionSettings.isPreRelease()) {
header.flags = KlibMetadataHeaderFlags.PRE_RELEASE
}
val fragmentNames = mutableListOf()
val fragmentParts = mutableListOf>()
for ((fqName, fragment) in fragments.entries.sortedBy { it.key }) {
fragmentNames += fqName
fragmentParts += fragment
header.addPackageFragmentName(fqName)
}
val module = header.build().toByteArray()
val serializedMetadata = SerializedMetadata(module, fragmentParts, fragmentNames)
buildKotlinMetadataLibrary(configuration, serializedMetadata, destDir)
return null
}
}