[ObjCExport] AA: AnalysisApiHeaderGenerator: Implement support for klib dependencies

KT-65670
This commit is contained in:
Sebastian Sellmair
2024-02-23 11:45:20 +01:00
committed by Space Team
parent e377a98815
commit 64503d9217
2 changed files with 26 additions and 6 deletions
@@ -29,7 +29,11 @@ class AnalysisApiHeaderGeneratorExtension : ParameterResolver {
object AnalysisApiHeaderGenerator : HeaderGenerator {
override fun generateHeaders(root: File, configuration: HeaderGenerator.Configuration): ObjCHeader {
val session = createStandaloneAnalysisApiSession(root.listFiles().orEmpty().filter { it.extension == "kt" })
val session = createStandaloneAnalysisApiSession(
kotlinFiles = root.listFiles().orEmpty().filter { it.extension == "kt" },
dependencyKlibs = configuration.dependencies
)
val (module, files) = session.modulesWithFiles.entries.single()
return analyze(module) {
KtObjCExportSession(
@@ -16,7 +16,9 @@ import org.jetbrains.kotlin.backend.konan.testUtils.kotlinNativeStdlibPath
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.platform.konan.NativePlatforms
import java.io.File
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.nameWithoutExtension
/**
* Creates a standalone analysis session from Kotlin source code passed as [kotlinSources]
@@ -24,6 +26,7 @@ import kotlin.io.path.Path
fun createStandaloneAnalysisApiSession(
tempDir: File,
kotlinSources: Map</* File Name */ String, /* Source Code */ String>,
dependencyKlibs: List<Path> = emptyList(),
): StandaloneAnalysisAPISession {
val testModuleRoot = tempDir.resolve("testModule")
testModuleRoot.mkdirs()
@@ -33,14 +36,14 @@ fun createStandaloneAnalysisApiSession(
writeText(sourceCode)
}
}
return createStandaloneAnalysisApiSession(listOf(testModuleRoot))
return createStandaloneAnalysisApiSession(listOf(testModuleRoot), dependencyKlibs)
}
/**
* Creates a standalone analysis session from [kotlinFiles] on disk.
* The Kotlin/Native stdlib will be provided as dependency
*/
fun createStandaloneAnalysisApiSession(kotlinFiles: List<File>): StandaloneAnalysisAPISession {
fun createStandaloneAnalysisApiSession(kotlinFiles: List<File>, dependencyKlibs: List<Path> = emptyList()): StandaloneAnalysisAPISession {
val currentArchitectureTarget = HostManager.host
val nativePlatform = NativePlatforms.nativePlatformByTargets(listOf(currentArchitectureTarget))
return buildStandaloneAnalysisAPISession {
@@ -49,17 +52,30 @@ fun createStandaloneAnalysisApiSession(kotlinFiles: List<File>): StandaloneAnaly
buildKtModuleProvider {
platform = nativePlatform
val kLib = addModule(
val stdlibModule = addModule(
buildKtLibraryModule {
addBinaryRoot(Path(kotlinNativeStdlibPath))
platform = nativePlatform
libraryName = "klib"
libraryName = "stdlib"
}
)
val dependencyKlibModules = dependencyKlibs.map { klib ->
buildKtLibraryModule {
addBinaryRoot(klib)
platform = nativePlatform
libraryName = klib.nameWithoutExtension
addRegularDependency(stdlibModule)
}
}
addModule(
buildKtSourceModule {
addSourceRoots(kotlinFiles.map { it.toPath() })
addRegularDependency(kLib)
addRegularDependency(stdlibModule)
dependencyKlibModules.forEach { dependencyKlibModule ->
addRegularDependency(dependencyKlibModule)
}
platform = nativePlatform
moduleName = "source"
}