@@ -6,16 +6,16 @@ import org.gradle.api.tasks.*
6
6
import org.gradle.workers.*
7
7
import org.jetbrains.kotlin.config.*
8
8
import org.jetbrains.kotlin.descriptors.*
9
- import org.jetbrains.kotlin.konan.library.*
10
- import org.jetbrains.kotlin.konan.library.impl.*
11
- import org.jetbrains.kotlin.konan.properties.*
12
- import org.jetbrains.kotlin.konan.target.*
13
9
import org.jetbrains.kotlin.konan.util.*
14
- import org.jetbrains.kotlin.storage.*
10
+ import org.jetbrains.kotlin.builtins.*
11
+ import org.jetbrains.kotlin.serialization.konan.*
12
+ import org.jetbrains.kotlin.storage.LockBasedStorageManager
13
+ import org.jetbrains.kotlin.util.Logger
14
+ import org.jetbrains.kotlin.library.*
15
+ import org.jetbrains.kotlin.library.impl.*
16
+ import org.jetbrains.kotlin.library.resolver.impl.*
15
17
import java.io.*
16
- import java.nio.file.*
17
18
import javax.inject.*
18
- import kotlin.reflect.jvm.*
19
19
20
20
@Suppress(" UnstableApiUsage" )
21
21
@CacheableTask
@@ -59,6 +59,9 @@ open class NativeSourceGeneratorTask
59
59
}
60
60
}
61
61
62
+ private val Builtins = DefaultBuiltIns .Instance
63
+ private val NativeFactories = KlibMetadataFactories ( { Builtins }, NullFlexibleTypeDeserializer )
64
+
62
65
class NativeSourceGeneratorWorker
63
66
@Inject constructor (
64
67
private val title: String ,
@@ -91,32 +94,30 @@ class NativeSourceGeneratorWorker
91
94
if (nativeTarget.isEmpty())
92
95
throw Exception (" nativeTarget should be specified for API generator for native targets" )
93
96
94
- val hostManager = HostManager ()
95
- val konanTarget = hostManager.targetByName(nativeTarget)
96
- val versionSpec = LanguageVersionSettingsImpl (
97
- LanguageVersion .LATEST_STABLE ,
98
- ApiVersion .LATEST_STABLE
99
- )
100
- val ABI_VERSION = 8
101
-
102
- val pathResolver = ProvidedPathResolver (dependencyPaths, konanTarget)
103
- val libraryResolver = pathResolver.libraryResolver(ABI_VERSION )
97
+ val logger = object : Logger {
98
+ override fun log (message : String ) {}
99
+ override fun error (message : String ) = kotlin.error(" e: $message " )
100
+ override fun warning (message : String ) {}
101
+ override fun fatal (message : String ) = kotlin.error(" e: $message " )
102
+ }
103
+ val pathResolver = SeveralKlibComponentResolver (dependencyPaths.map { it.canonicalPath }, listOf (KotlinAbiVersion .CURRENT ), logger)
104
+ val libraryResolver = pathResolver.libraryResolver()
104
105
105
- val factory = KonanFactories .DefaultDeserializedDescriptorFactory
106
+ val factory = NativeFactories .DefaultDeserializedDescriptorFactory
106
107
107
108
val konanFile = org.jetbrains.kotlin.konan.file.File (lib.canonicalPath)
109
+ val library = createKotlinLibrary(konanFile)
108
110
109
- val library = reflectCreateKonanLibrary(konanFile, ABI_VERSION , konanTarget)
110
- val unresolvedDependencies = library.manifestProperties.propertyList(KLIB_PROPERTY_DEPENDS )
111
+ val versionSpec = LanguageVersionSettingsImpl (LanguageVersion .LATEST_STABLE , ApiVersion .LATEST_STABLE )
111
112
val storageManager = LockBasedStorageManager (" Inspect" )
112
113
113
- val module = factory.createDescriptorAndNewBuiltIns (library, versionSpec, storageManager)
114
+ val module = factory.createDescriptorOptionalBuiltIns (library, versionSpec, storageManager, Builtins , null )
114
115
115
- val dependencies = libraryResolver.resolveWithDependencies(unresolvedDependencies)
116
- val dependenciesResolved = KonanFactories .DefaultResolvedDescriptorsFactory .createResolved(
116
+ val dependencies = libraryResolver.resolveWithDependencies(library. unresolvedDependencies)
117
+ val dependenciesResolved = NativeFactories .DefaultResolvedDescriptorsFactory .createResolved(
117
118
dependencies,
118
119
storageManager,
119
- null ,
120
+ Builtins ,
120
121
versionSpec
121
122
)
122
123
@@ -127,61 +128,14 @@ class NativeSourceGeneratorWorker
127
128
return module
128
129
}
129
130
130
- companion object {
131
- private fun reflectCreateKonanLibrary (
132
- libraryFile : org.jetbrains.kotlin.konan.file.File ,
133
- abi : Int ,
134
- konanTarget : KonanTarget
135
- ): KonanLibrary {
136
- val clazz = Class .forName(" org.jetbrains.kotlin.konan.library.KonanLibraryUtilsKt" )
137
- val method = clazz.methods.single { it.name == " createKonanLibrary" }.kotlinFunction
138
- val params = method!! .parameters
139
- if (params[1 ].name == " currentAbiVersion" ) {
140
- // new mode
141
- return createKonanLibrary(libraryFile, abi, konanTarget, false )
142
- } else {
143
- return method.call(libraryFile, konanTarget, false , DefaultMetadataReaderImpl ) as KonanLibrary
144
- }
145
- }
146
- }
147
-
148
-
149
- private class ProvidedPathResolver (private val dependencies : Set <File >, override val target : KonanTarget ) :
150
- SearchPathResolverWithTarget {
151
-
152
- override val searchRoots: List < org.jetbrains.kotlin.konan.file.File > get() = emptyList()
153
-
154
- private val nameMap = dependencies
155
- // TODO: what's wrong with JARs? They seem common libs, how does native ignores them?
156
- .filter { it.extension != " jar" }
157
- .map {
158
- val file = org.jetbrains.kotlin.konan.file.File (it.absolutePath)
159
- // Need to load library to know its uniqueName, some libs like atomicfu has it different from klib file name
160
- reflectCreateKonanLibrary(file, 1 , target)
161
- }
162
- .associateBy { it.uniqueName }
163
- .mapValues { it.value.libraryFile }
164
-
165
- override fun resolve (givenPath : String ): org.jetbrains.kotlin.konan.file.File {
166
- val path = Paths .get(givenPath)
167
- return when {
168
- path.isAbsolute -> org.jetbrains.kotlin.konan.file.File (path)
169
- else -> {
170
- val file = nameMap[givenPath]
171
- if (file != null )
172
- return file
173
-
174
- println (" Cannot resolve library $givenPath with the following dependencies:" )
175
- println (dependencies.joinToString(prefix = " " , separator = " \n " ))
176
- throw Exception (" Cannot resolve library '$givenPath ' with $nameMap " )
177
- }
178
- }
179
- }
180
-
181
- override fun defaultLinks (
182
- noStdLib : Boolean ,
183
- noDefaultLibs : Boolean
184
- ): List <org.jetbrains.kotlin.konan.file.File > =
185
- emptyList()
131
+ private class SeveralKlibComponentResolver (
132
+ klibFiles : List <String >,
133
+ knownAbiVersions : List <KotlinAbiVersion >? ,
134
+ logger : Logger
135
+ ) : KotlinLibraryProperResolverWithAttributes<KotlinLibrary>(
136
+ emptyList(), klibFiles, knownAbiVersions, emptyList(),
137
+ null , null , false , logger, emptyList()
138
+ ) {
139
+ override fun libraryBuilder (file : org.jetbrains.kotlin.konan.file.File , isDefault : Boolean ) = createKotlinLibrary(file, isDefault)
186
140
}
187
141
}
0 commit comments