Skip to content

Commit f32be28

Browse files
ddolovovSpace Team
authored andcommitted
Klib API: Drop KotlinLibrary internal implementation classes
^KT-81411
1 parent 2310d2a commit f32be28

File tree

6 files changed

+26
-245
lines changed

6 files changed

+26
-245
lines changed

compiler/util-klib/src/org/jetbrains/kotlin/library/KotlinLibraryUtils.kt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
package org.jetbrains.kotlin.library
22

3-
import org.jetbrains.kotlin.konan.file.File
4-
import org.jetbrains.kotlin.konan.file.unzipTo
5-
import org.jetbrains.kotlin.library.impl.zippedKotlinLibraryChecks
6-
73
const val KLIB_FILE_EXTENSION = "klib"
84
const val KLIB_FILE_EXTENSION_WITH_DOT = ".$KLIB_FILE_EXTENSION"
95

10-
fun File.unpackZippedKonanLibraryTo(newDir: File) {
11-
12-
// First, run validity checks for the given KLIB file.
13-
zippedKotlinLibraryChecks(this)
14-
15-
if (newDir.exists) {
16-
if (newDir.isDirectory)
17-
newDir.deleteRecursively()
18-
else
19-
newDir.delete()
20-
}
21-
22-
this.unzipTo(newDir)
23-
check(newDir.exists) { "Could not unpack $this as $newDir." }
24-
}
25-
266
val List<String>.toUnresolvedLibraries
277
get() = this.map {
288
RequiredUnresolvedLibrary(it)

compiler/util-klib/src/org/jetbrains/kotlin/library/SearchPathResolver.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.jetbrains.kotlin.konan.file.File
44
import org.jetbrains.kotlin.konan.library.isFromKotlinNativeDistribution
55
import org.jetbrains.kotlin.library.SearchPathResolver.LookupResult
66
import org.jetbrains.kotlin.library.SearchPathResolver.SearchRoot
7-
import org.jetbrains.kotlin.library.impl.createKotlinLibraryComponents
7+
import org.jetbrains.kotlin.library.loader.KlibLoader
88
import org.jetbrains.kotlin.util.Logger
99
import org.jetbrains.kotlin.util.WithLogger
1010
import org.jetbrains.kotlin.util.removeSuffixIfPresent
@@ -327,7 +327,8 @@ class SingleKlibComponentResolver(
327327
logger,
328328
knownIrProviders = knownIrProviders
329329
) {
330-
override fun libraryComponentBuilder(file: File, /* ignored */ isDefault: Boolean) = createKotlinLibraryComponents(file)
330+
override fun libraryComponentBuilder(file: File, /* ignored */ isDefault: Boolean) =
331+
KlibLoader { libraryPaths(file.path) }.load().librariesStdlibFirst
331332
}
332333

333334
/**

compiler/util-klib/src/org/jetbrains/kotlin/library/ToolingResolve.kt

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
package org.jetbrains.kotlin.library
77

88
import org.jetbrains.kotlin.konan.file.File
9-
import org.jetbrains.kotlin.konan.file.file
10-
import org.jetbrains.kotlin.konan.file.withZipFileSystem
11-
import org.jetbrains.kotlin.library.impl.createKotlinLibrary
9+
import org.jetbrains.kotlin.konan.properties.Properties
10+
import org.jetbrains.kotlin.library.ToolingSingleFileKlibResolveStrategy.resolve
11+
import org.jetbrains.kotlin.library.impl.KLIB_DEFAULT_COMPONENT_NAME
12+
import org.jetbrains.kotlin.library.loader.KlibLoader
1213
import org.jetbrains.kotlin.util.Logger
13-
import java.io.IOException
1414

1515
/**
1616
* Resolves KLIB library by making sure that the library has 1.4+ layout with exactly one component.
@@ -30,56 +30,21 @@ object ToolingSingleFileKlibResolveStrategy : SingleFileKlibResolveStrategy {
3030
tryResolve(libraryFile, logger)
3131
?: fakeLibrary(libraryFile)
3232

33-
fun tryResolve(libraryFile: File, logger: Logger): KotlinLibrary? =
34-
withSafeAccess(libraryFile) { localRoot ->
35-
if (localRoot.looksLikeKlibComponent) {
36-
// old style library
37-
null
38-
} else {
39-
val components = localRoot.listFiles.filter { it.looksLikeKlibComponent }
40-
when (components.size) {
41-
0 -> null
42-
1 -> {
43-
// single component library
44-
createKotlinLibrary(libraryFile, components.single().name)
45-
}
46-
else -> { // TODO: choose the best fit among all available candidates
47-
// mimic as old style library and warn
48-
logger.strongWarning(
49-
"KLIB resolver: Library '$libraryFile' can not be read." +
50-
" Multiple components found: ${components.map { it.path.substringAfter(localRoot.path) }}"
51-
)
52-
53-
null
54-
}
55-
}
56-
}
57-
}
58-
59-
private const val NONEXISTENT_COMPONENT_NAME = "__nonexistent_component_name__"
60-
61-
private fun fakeLibrary(libraryFile: File): KotlinLibrary = createKotlinLibrary(libraryFile, NONEXISTENT_COMPONENT_NAME)
33+
fun tryResolve(libraryFile: File, /* unused */ logger: Logger): KotlinLibrary? {
34+
repeat(0) { logger.log("Trying to resolve KLIB: $libraryFile") }
35+
return KlibLoader { libraryPaths(libraryFile.path) }.load().librariesStdlibFirst.singleOrNull()
36+
}
6237

63-
private fun <T : Any> withSafeAccess(libraryFile: File, action: (localRoot: File) -> T?): T? {
64-
val extension = libraryFile.extension
38+
private fun fakeLibrary(libraryFile: File): KotlinLibrary = object : KotlinLibrary {
39+
override val location = libraryFile
40+
override val attributes = KlibAttributes()
41+
override val versions = KotlinLibraryVersioning(null, null, null)
42+
override val manifestProperties = Properties()
6543

66-
val wrappedAction: () -> T? = when {
67-
libraryFile.isDirectory -> {
68-
{ action(libraryFile) }
69-
}
70-
libraryFile.isFile && extension == KLIB_FILE_EXTENSION -> {
71-
{ libraryFile.withZipFileSystem { fs -> action(fs.file("/")) } }
72-
}
73-
else -> return null
74-
}
44+
override fun <KC : KlibComponent> getComponent(kind: KlibComponent.Kind<KC, *>): KC? = null
7545

76-
return try {
77-
wrappedAction()
78-
} catch (_: IOException) {
79-
null
80-
}
46+
override val libraryName get() = location.path
47+
override val libraryFile get() = location
48+
override val componentList get() = listOf(KLIB_DEFAULT_COMPONENT_NAME)
8149
}
82-
83-
private val File.looksLikeKlibComponent: Boolean
84-
get() = child(KLIB_MANIFEST_FILE_NAME).isFile
8550
}

compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryImpl.kt

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,8 @@ package org.jetbrains.kotlin.library.impl
1818

1919
import org.jetbrains.kotlin.konan.file.File
2020
import org.jetbrains.kotlin.konan.file.ZipFileSystemAccessor
21-
import org.jetbrains.kotlin.konan.file.ZipFileSystemInPlaceAccessor
22-
import org.jetbrains.kotlin.konan.properties.Properties
23-
import org.jetbrains.kotlin.konan.properties.loadProperties
24-
import org.jetbrains.kotlin.library.*
25-
26-
private class BaseKotlinLibraryImpl(
27-
val access: BaseLibraryAccess<KotlinLibraryLayout>,
28-
) : BaseKotlinLibrary {
29-
override val libraryFile get() = access.klib
30-
override val libraryName: String by lazy { access.inPlace { it.libraryName } }
31-
32-
override val componentList: List<String> by lazy {
33-
access.inPlace { layout ->
34-
val listFiles = layout.libFile.listFiles
35-
listFiles
36-
.filter { it.isDirectory }
37-
.filter { it.listFiles.map { it.name }.contains(KLIB_MANIFEST_FILE_NAME) }
38-
.map { it.name }
39-
}
40-
}
41-
42-
override fun toString() = libraryName
43-
44-
override val manifestProperties: Properties by lazy {
45-
access.inPlace { it.manifestFile.loadProperties() }
46-
}
47-
48-
override val versions: KotlinLibraryVersioning by lazy {
49-
manifestProperties.readKonanLibraryVersioning()
50-
}
51-
}
52-
53-
private class KotlinLibraryImpl(
54-
override val location: File,
55-
zipFileSystemAccessor: ZipFileSystemAccessor,
56-
val base: BaseKotlinLibraryImpl,
57-
) : KotlinLibrary,
58-
BaseKotlinLibrary by base {
59-
60-
private val components = KlibComponentsCache(
61-
layoutReaderFactory = KlibLayoutReaderFactory(
62-
klibFile = location,
63-
zipFileSystemAccessor = zipFileSystemAccessor
64-
)
65-
)
66-
67-
override fun <KC : KlibComponent> getComponent(kind: KlibComponent.Kind<KC, *>) = components.getComponent(kind)
68-
69-
override val attributes = KlibAttributes()
70-
71-
override fun toString(): String = buildString {
72-
append("name ")
73-
append(base.libraryName)
74-
append(", ")
75-
append("file: ")
76-
append(base.libraryFile.path)
77-
append(", ")
78-
append("version: ")
79-
append(base.versions)
80-
interopFlag?.let { append(", interop: $it") }
81-
irProviderName?.let { append(", IR provider: $it") }
82-
nativeTargets.takeIf { it.isNotEmpty() }?.joinTo(this, ", ", ", native targets: {", "}")
83-
append(')')
84-
}
85-
}
86-
87-
fun createKotlinLibrary(
88-
libraryFile: File,
89-
component: String,
90-
zipAccessor: ZipFileSystemAccessor? = null,
91-
): KotlinLibrary {
92-
val nonNullZipFileSystemAccessor = zipAccessor ?: ZipFileSystemInPlaceAccessor
93-
94-
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, component, nonNullZipFileSystemAccessor)
95-
val base = BaseKotlinLibraryImpl(baseAccess)
96-
97-
return KotlinLibraryImpl(libraryFile, nonNullZipFileSystemAccessor, base)
98-
}
99-
100-
fun createKotlinLibraryComponents(
101-
libraryFile: File,
102-
zipAccessor: ZipFileSystemAccessor? = null,
103-
): List<KotlinLibrary> {
104-
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, null, zipAccessor)
105-
val base = BaseKotlinLibraryImpl(baseAccess)
106-
return base.componentList.map {
107-
createKotlinLibrary(libraryFile, it, zipAccessor = zipAccessor)
108-
}
109-
}
21+
import org.jetbrains.kotlin.library.KotlinLibrary
22+
import org.jetbrains.kotlin.library.loader.KlibLoader
11023

11124
@Deprecated(
11225
"Preserved for binary compatibility. Use createKotlinLibraryComponents(libraryFile, zipAccessor = ...) instead.",
@@ -118,5 +31,8 @@ fun createKotlinLibraryComponents(
11831
zipAccessor: ZipFileSystemAccessor? = null,
11932
): List<KotlinLibrary> {
12033
if (isDefault) repeat(0) { Unit }
121-
return createKotlinLibraryComponents(libraryFile, zipAccessor)
34+
return KlibLoader {
35+
libraryPaths(libraryFile.path)
36+
zipAccessor?.let(::zipFileSystemAccessor)
37+
}.load().librariesStdlibFirst
12238
}

compiler/util-klib/src/org/jetbrains/kotlin/library/impl/KotlinLibraryLayoutImpl.kt

Lines changed: 0 additions & 56 deletions
This file was deleted.

compiler/util-klib/src/org/jetbrains/kotlin/library/impl/lowLevelReaders.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.library.impl
88
import org.jetbrains.kotlin.konan.file.File
99
import org.jetbrains.kotlin.library.KlibComponentLayout
1010
import org.jetbrains.kotlin.library.KlibLayoutReader
11-
import org.jetbrains.kotlin.library.KotlinLibraryLayout
1211
import java.nio.ByteBuffer
1312

1413
/******************************************************************************/
@@ -21,12 +20,6 @@ fun IrArrayReader(bytes: ByteArray): IrArrayReader = IrArrayReader(ReadBuffer.Me
2120
/** On-demand read from a byte array that will be loaded on the first access. */
2221
fun IrArrayReader(loadBytes: () -> ByteArray): IrArrayReader = IrArrayReader(ReadBuffer.OnDemandMemoryBuffer(loadBytes))
2322

24-
/** On-demand read from a file (potentially inside a KLIB archive file). */
25-
fun <L : KotlinLibraryLayout> IrArrayReader(
26-
access: BaseLibraryAccess<L>,
27-
getFile: L.() -> File
28-
): IrArrayReader = IrArrayReader { access.inPlace { it.getFile().readBytes() } }
29-
3023
/** On-demand read from a file (potentially inside a KLIB archive file). */
3124
inline fun <KCL : KlibComponentLayout> IrArrayReader(
3225
layoutReader: KlibLayoutReader<KCL>,
@@ -46,12 +39,6 @@ fun IrMultiArrayReader(bytes: ByteArray): IrMultiArrayReader = IrMultiArrayReade
4639
/** On-demand read from a byte array that will be loaded on the first access. */
4740
fun IrMultiArrayReader(loadBytes: () -> ByteArray): IrMultiArrayReader = IrMultiArrayReader(ReadBuffer.OnDemandMemoryBuffer(loadBytes))
4841

49-
/** On-demand read from a file (potentially inside a KLIB archive file). */
50-
fun <L : KotlinLibraryLayout> IrMultiArrayReader(
51-
access: BaseLibraryAccess<L>,
52-
getFile: L.() -> File
53-
): IrMultiArrayReader = IrMultiArrayReader { access.inPlace { it.getFile().readBytes() } }
54-
5542
/** On-demand read from a file (potentially inside a KLIB archive file). */
5643
inline fun <KCL : KlibComponentLayout> IrMultiArrayReader(
5744
layoutReader: KlibLayoutReader<KCL>,
@@ -82,12 +69,6 @@ fun DeclarationIdTableReader(bytes: ByteArray): DeclarationIdTableReader =
8269
fun DeclarationIdTableReader(loadBytes: () -> ByteArray): DeclarationIdTableReader =
8370
DeclarationIdTableReader(ReadBuffer.OnDemandMemoryBuffer(loadBytes))
8471

85-
/** On-demand read from a file (potentially inside a KLIB archive file). */
86-
fun <L : KotlinLibraryLayout> DeclarationIdTableReader(
87-
access: BaseLibraryAccess<L>,
88-
getFile: L.() -> File
89-
): DeclarationIdTableReader = DeclarationIdTableReader { access.inPlace { it.getFile().readBytes() } }
90-
9172
class DeclarationIdTableReader(private val buffer: ReadBuffer) {
9273
private val declarationIdToCoordinates: DeclarationIdToCoordinates = buffer.readDeclarationIdToCoordinates(0)
9374

@@ -103,12 +84,6 @@ fun DeclarationIdMultiTableReader(bytes: ByteArray): DeclarationIdMultiTableRead
10384
fun DeclarationIdMultiTableReader(loadBytes: () -> ByteArray): DeclarationIdMultiTableReader =
10485
DeclarationIdMultiTableReader(ReadBuffer.OnDemandMemoryBuffer(loadBytes))
10586

106-
/** On-demand read from a file (potentially inside a KLIB archive file). */
107-
fun <L : KotlinLibraryLayout> DeclarationIdMultiTableReader(
108-
access: BaseLibraryAccess<L>,
109-
getFile: L.() -> File
110-
): DeclarationIdMultiTableReader = DeclarationIdMultiTableReader { access.inPlace { it.getFile().readBytes() } }
111-
11287
/** On-demand read from a file (potentially inside a KLIB archive file). */
11388
inline fun <KCL : KlibComponentLayout> DeclarationIdMultiTableReader(
11489
layoutReader: KlibLayoutReader<KCL>,

0 commit comments

Comments
 (0)