Initial implementation of klib components
This commit is contained in:
committed by
alexander-gorshenev
parent
f7626d6474
commit
7390e74bbd
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
import org.jetbrains.kotlin.library.KotlinLibraryVersioning
|
||||
import org.jetbrains.kotlin.library.SerializedMetadata
|
||||
import org.jetbrains.kotlin.library.impl.buildKoltinLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibraryComponents
|
||||
import org.jetbrains.kotlin.library.metadata.parseModuleHeader
|
||||
import org.jetbrains.kotlin.metadata.builtins.BuiltInsBinaryVersion
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
@@ -110,7 +110,9 @@ private class KlibMetadataDependencyContainer(
|
||||
val klibFiles = classpathFiles
|
||||
.filter { it.extension == "klib" || it.isDirectory }
|
||||
|
||||
klibFiles.map { createKotlinLibrary(org.jetbrains.kotlin.konan.file.File(it.absolutePath)) }
|
||||
// TODO: need to move K2Metadata to SearchPathResolver.
|
||||
// Otherewise there's no good way to choose between the components.
|
||||
klibFiles.map { createKotlinLibraryComponents(org.jetbrains.kotlin.konan.file.File(it.absolutePath)).single() }
|
||||
}
|
||||
|
||||
private val builtIns
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.library.KotlinLibrary
|
||||
import org.jetbrains.kotlin.library.KotlinLibraryProperResolverWithAttributes
|
||||
import org.jetbrains.kotlin.library.UnresolvedLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibraryComponents
|
||||
import org.jetbrains.kotlin.library.resolver.KotlinLibraryResolveResult
|
||||
import org.jetbrains.kotlin.library.resolver.impl.libraryResolver
|
||||
import org.jetbrains.kotlin.util.Logger
|
||||
@@ -37,7 +38,7 @@ class JsLibraryResolver(
|
||||
emptyList()
|
||||
) {
|
||||
// Stick with the default KotlinLibrary for now.
|
||||
override fun libraryBuilder(file: File, isDefault: Boolean) = createKotlinLibrary(file, isDefault)
|
||||
override fun libraryComponentBuilder(file: File, isDefault: Boolean) = createKotlinLibraryComponents(file, isDefault)
|
||||
}
|
||||
|
||||
// TODO: This is a temporary set of library resolver policies for js compiler.
|
||||
|
||||
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
|
||||
import org.jetbrains.kotlin.library.*
|
||||
import org.jetbrains.kotlin.library.impl.buildKoltinLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibrary
|
||||
import org.jetbrains.kotlin.library.impl.createKotlinLibraryComponents
|
||||
import org.jetbrains.kotlin.library.resolver.KotlinLibraryResolveResult
|
||||
import org.jetbrains.kotlin.library.resolver.TopologicalLibraryOrder
|
||||
import org.jetbrains.kotlin.metadata.ProtoBuf
|
||||
@@ -73,8 +74,11 @@ val KotlinLibrary.isBuiltIns: Boolean
|
||||
.propertyList(KLIB_PROPERTY_DEPENDS, escapeInQuotes = true)
|
||||
.isEmpty()
|
||||
|
||||
// TODO: The only place loadKlib() is used now is Wasm backend.
|
||||
// Need to move it to SearchPathResolver too.
|
||||
// Otherwise there's no good way to choose between the components.
|
||||
fun loadKlib(klibPath: String) =
|
||||
createKotlinLibrary(KFile(KFile(klibPath).absolutePath))
|
||||
createKotlinLibraryComponents(KFile(KFile(klibPath).absolutePath)).single()
|
||||
|
||||
val emptyLoggingContext = object : LoggingContext {
|
||||
override var inVerbosePhase = false
|
||||
|
||||
@@ -23,10 +23,12 @@ const val KLIB_PROPERTY_EXPORT_FORWARD_DECLARATIONS = "exportForwardDeclarations
|
||||
interface BaseKotlinLibrary {
|
||||
val libraryName: String
|
||||
val libraryFile: File
|
||||
val componentList: List<String>
|
||||
val versions: KotlinLibraryVersioning
|
||||
// Whether this library is default (provided by distribution)?
|
||||
val isDefault: Boolean
|
||||
val manifestProperties: Properties
|
||||
val has_pre_1_4_manifest: Boolean
|
||||
}
|
||||
|
||||
interface MetadataLibrary {
|
||||
|
||||
@@ -30,15 +30,20 @@ interface KotlinLibraryLayout {
|
||||
val libDir: File
|
||||
val libraryName: String
|
||||
get() = libDir.path
|
||||
val component: String?
|
||||
val componentDir: File
|
||||
get() = File(libDir, component!!)
|
||||
val manifestFile
|
||||
get() = File(libDir, KLIB_MANIFEST_FILE_NAME)
|
||||
get() = File(componentDir, KLIB_MANIFEST_FILE_NAME)
|
||||
val resourcesDir
|
||||
get() = File(libDir, "resources")
|
||||
get() = File(componentDir, "resources")
|
||||
val pre_1_4_manifest: File
|
||||
get() = File(libDir, KLIB_MANIFEST_FILE_NAME)
|
||||
}
|
||||
|
||||
interface MetadataKotlinLibraryLayout : KotlinLibraryLayout {
|
||||
val metadataDir
|
||||
get() = File(libDir, "linkdata")
|
||||
get() = File(componentDir, "linkdata")
|
||||
val moduleHeaderFile
|
||||
get() = File(metadataDir, KLIB_MODULE_METADATA_FILE_NAME)
|
||||
|
||||
@@ -51,7 +56,7 @@ interface MetadataKotlinLibraryLayout : KotlinLibraryLayout {
|
||||
|
||||
interface IrKotlinLibraryLayout : KotlinLibraryLayout {
|
||||
val irDir
|
||||
get() = File(libDir, KLIB_IR_FOLDER_NAME)
|
||||
get() = File(componentDir, KLIB_IR_FOLDER_NAME)
|
||||
val irDeclarations
|
||||
get() = File(irDir, "irDeclarations.knd")
|
||||
val irSymbols
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.jetbrains.kotlin.library
|
||||
|
||||
import org.jetbrains.kotlin.konan.CompilerVersion
|
||||
import org.jetbrains.kotlin.konan.file.File
|
||||
import org.jetbrains.kotlin.library.impl.isPre_1_4_Library
|
||||
import org.jetbrains.kotlin.util.*
|
||||
|
||||
const val KOTLIN_STDLIB_NAME = "stdlib"
|
||||
@@ -21,7 +22,7 @@ interface SearchPathResolverWithAttributes<L: KotlinLibrary>: SearchPathResolver
|
||||
}
|
||||
|
||||
// This is a simple library resolver that only cares for file names.
|
||||
abstract class KotlinLibrarySearchPathResolver<L: KotlinLibrary>(
|
||||
abstract class KotlinLibrarySearchPathResolver<L: KotlinLibrary> (
|
||||
repositories: List<String>,
|
||||
directLibs: List<String>,
|
||||
val distributionKlib: String?,
|
||||
@@ -43,10 +44,10 @@ abstract class KotlinLibrarySearchPathResolver<L: KotlinLibrary>(
|
||||
|
||||
private val repoRoots: List<File> by lazy { repositories.map { File(it) } }
|
||||
|
||||
abstract fun libraryBuilder(file: File, isDefault: Boolean): L
|
||||
abstract fun libraryComponentBuilder(file: File, isDefault: Boolean): List<L>
|
||||
|
||||
private val directLibraries: List<KotlinLibrary> by lazy {
|
||||
directLibs.mapNotNull { found(File(it)) }.map { libraryBuilder(it, false) }
|
||||
directLibs.mapNotNull { found(File(it)) }.flatMap { libraryComponentBuilder(it, false) }
|
||||
}
|
||||
|
||||
// This is the place where we specify the order of library search.
|
||||
@@ -55,8 +56,7 @@ abstract class KotlinLibrarySearchPathResolver<L: KotlinLibrary>(
|
||||
}
|
||||
|
||||
private fun found(candidate: File): File? {
|
||||
fun check(file: File): Boolean =
|
||||
file.exists && (file.isFile || File(file, "manifest").exists)
|
||||
fun check(file: File): Boolean = file.exists
|
||||
|
||||
val noSuffix = File(candidate.path.removeSuffixIfPresent(KLIB_FILE_EXTENSION_WITH_DOT))
|
||||
val withSuffix = File(candidate.path.suffixIfNot(KLIB_FILE_EXTENSION_WITH_DOT))
|
||||
@@ -94,11 +94,22 @@ abstract class KotlinLibrarySearchPathResolver<L: KotlinLibrary>(
|
||||
return sequence.filterNotNull()
|
||||
}
|
||||
|
||||
private fun Sequence<File>.filterOutPre_1_4_libraries(): Sequence<File> = this.filter{
|
||||
if (it.isPre_1_4_Library) {
|
||||
logger.warning("Skipping \"$it\" as it is a pre 1.4 library")
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
override fun resolve(unresolved: UnresolvedLibrary, isDefaultLink: Boolean): L {
|
||||
val givenPath = unresolved.path
|
||||
try {
|
||||
val fileSequence = resolutionSequence(givenPath)
|
||||
val matching = fileSequence.map { libraryBuilder(it, isDefaultLink) }
|
||||
val matching = fileSequence
|
||||
.filterOutPre_1_4_libraries()
|
||||
.flatMap { libraryComponentBuilder(it, isDefaultLink).asSequence() }
|
||||
.map { it.takeIf { libraryMatch(it, unresolved) } }
|
||||
.filterNotNull()
|
||||
|
||||
|
||||
@@ -28,8 +28,21 @@ open class BaseKotlinLibraryImpl(
|
||||
override val libraryFile get() = access.klib
|
||||
override val libraryName: String by lazy { access.inPlace { it.libraryName } }
|
||||
|
||||
override val componentList: List<String> by lazy {
|
||||
access.inPlace {
|
||||
it.libDir.listFiles
|
||||
.filter { it.isDirectory }
|
||||
.filter { it.listFiles.map { it.name }.contains(KLIB_MANIFEST_FILE_NAME) }
|
||||
.map { it.name }
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "$libraryName[default=$isDefault]"
|
||||
|
||||
override val has_pre_1_4_manifest: Boolean by lazy {
|
||||
access.inPlace { it.pre_1_4_manifest.exists }
|
||||
}
|
||||
|
||||
override val manifestProperties: Properties by lazy {
|
||||
access.inPlace { it.manifestFile.loadProperties() }
|
||||
}
|
||||
@@ -221,11 +234,12 @@ open class KotlinLibraryImpl(
|
||||
|
||||
fun createKotlinLibrary(
|
||||
libraryFile: File,
|
||||
component: String,
|
||||
isDefault: Boolean = false
|
||||
): KotlinLibrary {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile)
|
||||
val metadataAccess = MetadataLibraryAccess<MetadataKotlinLibraryLayout>(libraryFile)
|
||||
val irAccess = IrLibraryAccess<IrKotlinLibraryLayout>(libraryFile)
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, component)
|
||||
val metadataAccess = MetadataLibraryAccess<MetadataKotlinLibraryLayout>(libraryFile, component)
|
||||
val irAccess = IrLibraryAccess<IrKotlinLibraryLayout>(libraryFile, component)
|
||||
|
||||
val base = BaseKotlinLibraryImpl(baseAccess, isDefault)
|
||||
val metadata = MetadataLibraryImpl(metadataAccess)
|
||||
@@ -233,4 +247,22 @@ fun createKotlinLibrary(
|
||||
// val ir = IrPerFileLibraryImpl(irAccess)
|
||||
|
||||
return KotlinLibraryImpl(base, metadata, ir)
|
||||
}
|
||||
}
|
||||
|
||||
fun createKotlinLibraryComponents(
|
||||
libraryFile: File,
|
||||
isDefault: Boolean = true
|
||||
) : List<KotlinLibrary> {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, null)
|
||||
val base = BaseKotlinLibraryImpl(baseAccess, isDefault)
|
||||
return base.componentList.map {
|
||||
createKotlinLibrary(libraryFile, it, isDefault)
|
||||
}
|
||||
}
|
||||
|
||||
val File.isPre_1_4_Library: Boolean
|
||||
get() {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(this, null)
|
||||
val base = BaseKotlinLibraryImpl(baseAccess, false)
|
||||
return base.has_pre_1_4_manifest
|
||||
}
|
||||
+11
-9
@@ -7,7 +7,7 @@ import org.jetbrains.kotlin.library.*
|
||||
import org.jetbrains.kotlin.util.removeSuffixIfPresent
|
||||
import java.nio.file.FileSystem
|
||||
|
||||
open class KotlinLibraryLayoutImpl(val klib: File) : KotlinLibraryLayout {
|
||||
open class KotlinLibraryLayoutImpl(val klib: File, override val component: String?) : KotlinLibraryLayout {
|
||||
val isZipped = klib.isFile
|
||||
|
||||
init {
|
||||
@@ -32,7 +32,7 @@ open class KotlinLibraryLayoutImpl(val klib: File) : KotlinLibraryLayout {
|
||||
|
||||
}
|
||||
|
||||
class MetadataLibraryLayoutImpl(klib: File) : KotlinLibraryLayoutImpl(klib), MetadataKotlinLibraryLayout {
|
||||
class MetadataLibraryLayoutImpl(klib: File, component: String) : KotlinLibraryLayoutImpl(klib, component), MetadataKotlinLibraryLayout {
|
||||
|
||||
override val extractingToTemp: MetadataKotlinLibraryLayout by lazy {
|
||||
ExtractingMetadataLibraryImpl(this)
|
||||
@@ -42,7 +42,7 @@ class MetadataLibraryLayoutImpl(klib: File) : KotlinLibraryLayoutImpl(klib), Met
|
||||
FromZipMetadataLibraryImpl(this, zipFileSystem)
|
||||
}
|
||||
|
||||
class IrLibraryLayoutImpl(klib: File) : KotlinLibraryLayoutImpl(klib), IrKotlinLibraryLayout {
|
||||
class IrLibraryLayoutImpl(klib: File, component: String) : KotlinLibraryLayoutImpl(klib, component), IrKotlinLibraryLayout {
|
||||
|
||||
override val extractingToTemp: IrKotlinLibraryLayout by lazy {
|
||||
ExtractingIrLibraryImpl(this)
|
||||
@@ -52,8 +52,8 @@ class IrLibraryLayoutImpl(klib: File) : KotlinLibraryLayoutImpl(klib), IrKotlinL
|
||||
FromZipIrLibraryImpl(this, zipFileSystem)
|
||||
}
|
||||
|
||||
open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File) {
|
||||
open val layout = KotlinLibraryLayoutImpl(klib)
|
||||
open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File, component: String?) {
|
||||
open val layout = KotlinLibraryLayoutImpl(klib, component)
|
||||
|
||||
fun <T> realFiles(action: (L) -> T): T =
|
||||
if (layout.isZipped)
|
||||
@@ -71,12 +71,12 @@ open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File) {
|
||||
}
|
||||
|
||||
|
||||
open class MetadataLibraryAccess<L : KotlinLibraryLayout>(klib: File) : BaseLibraryAccess<L>(klib) {
|
||||
override val layout = MetadataLibraryLayoutImpl(klib)
|
||||
open class MetadataLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String) : BaseLibraryAccess<L>(klib, component) {
|
||||
override val layout = MetadataLibraryLayoutImpl(klib, component)
|
||||
}
|
||||
|
||||
open class IrLibraryAccess<L : KotlinLibraryLayout>(klib: File) : BaseLibraryAccess<L>(klib) {
|
||||
override val layout = IrLibraryLayoutImpl(klib)
|
||||
open class IrLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String) : BaseLibraryAccess<L>(klib, component) {
|
||||
override val layout = IrLibraryLayoutImpl(klib, component)
|
||||
}
|
||||
|
||||
open class FromZipBaseLibraryImpl(zipped: KotlinLibraryLayoutImpl, zipFileSystem: FileSystem) :
|
||||
@@ -84,6 +84,7 @@ open class FromZipBaseLibraryImpl(zipped: KotlinLibraryLayoutImpl, zipFileSystem
|
||||
|
||||
override val libraryName = zipped.libraryName
|
||||
override val libDir = zipFileSystem.file(zipped.libDir)
|
||||
override val component = zipped.component
|
||||
}
|
||||
|
||||
class FromZipMetadataLibraryImpl(zipped: MetadataLibraryLayoutImpl, zipFileSystem: FileSystem) :
|
||||
@@ -113,6 +114,7 @@ fun KotlinLibraryLayoutImpl.extractDir(directory: File): File = this.klib.withZi
|
||||
open class ExtractingKotlinLibraryLayout(zipped: KotlinLibraryLayoutImpl) : KotlinLibraryLayout {
|
||||
override val libDir: File get() = error("Extracting layout doesn't extract its own root")
|
||||
override val libraryName = zipped.libraryName
|
||||
override val component = zipped.component
|
||||
}
|
||||
|
||||
open class ExtractingBaseLibraryImpl(zipped: KotlinLibraryLayoutImpl) :
|
||||
|
||||
@@ -11,8 +11,11 @@ import org.jetbrains.kotlin.konan.properties.Properties
|
||||
import org.jetbrains.kotlin.konan.properties.saveToFile
|
||||
import org.jetbrains.kotlin.library.*
|
||||
|
||||
const val KLIB_DEFAULT_COMPONENT_NAME = "default"
|
||||
|
||||
open class KotlinLibraryLayoutForWriter(
|
||||
override val libDir: File
|
||||
override val libDir: File,
|
||||
override val component: String = KLIB_DEFAULT_COMPONENT_NAME
|
||||
) : KotlinLibraryLayout, MetadataKotlinLibraryLayout, IrKotlinLibraryLayout
|
||||
|
||||
open class BaseWriterImpl(
|
||||
|
||||
@@ -68,10 +68,11 @@ class Distribution(
|
||||
|
||||
val klib = "$konanHome/klib"
|
||||
val stdlib = "$klib/common/stdlib"
|
||||
val stdlibDefaultComponent = "$stdlib/default"
|
||||
|
||||
fun defaultNatives(target: KonanTarget) = "$konanHome/konan/targets/${target.visibleName}/native"
|
||||
|
||||
fun runtime(target: KonanTarget) = runtimeFileOverride ?: "$stdlib/targets/${target.visibleName}/native/runtime.bc"
|
||||
fun runtime(target: KonanTarget) = runtimeFileOverride ?: "$stdlibDefaultComponent/targets/${target.visibleName}/native/runtime.bc"
|
||||
|
||||
val launcherFiles = listOf("launcher.bc")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user