Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Apr 15, 2022
1 parent eb848d7 commit cd7b3cf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 57 deletions.
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.analysis.resolve.*
import org.jetbrains.kotlin.analyzer.*
import org.jetbrains.kotlin.analyzer.common.CommonAnalysisParameters
import org.jetbrains.kotlin.analyzer.common.CommonDependenciesContainer
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
import org.jetbrains.kotlin.analyzer.common.CommonResolverForModuleFactory
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
Expand Down Expand Up @@ -50,6 +51,7 @@ import org.jetbrains.kotlin.idea.klib.getCompatibilityInfo
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.library.KLIB_FILE_EXTENSION
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.ToolingSingleFileKlibResolveStrategy
import org.jetbrains.kotlin.library.resolveSingleFileKlib
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
Expand Down Expand Up @@ -184,7 +186,15 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
Platform.jvm -> JvmPlatforms.defaultJvmPlatform
}

val nativeLibraries: Map<AbsolutePathString, LibraryModuleInfo> = loadNativeLibraries()
val kotlinLibraries: Map<AbsolutePathString, KotlinLibrary> = resolveKotlinLibraries()

val commonDependencyContainer = if (analysisPlatform == Platform.common) DokkaKlibMetadataCommonDependencyContainer(
kotlinLibraries.values.toList(),
environment.configuration,
projectContext.storageManager
) else null

val extraModuleDependencies = kotlinLibraries.values.registerLibraries() + commonDependencyContainer?.moduleInfos.orEmpty()

val library = object : LibraryModuleInfo {
override val analyzerServices: PlatformDependentAnalyzerServices =
Expand All @@ -194,15 +204,16 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
override fun dependencies(): List<ModuleInfo> = listOf(this)
override fun getLibraryRoots(): Collection<String> = classpath
.map { libraryFile -> libraryFile.absolutePath }
.filter { path -> path !in nativeLibraries }
.filter { path -> path !in kotlinLibraries }
}

val module = object : ModuleInfo {
override val analyzerServices: PlatformDependentAnalyzerServices =
analysisPlatform.analyzerServices()
override val name: Name = Name.special("<module>")
override val platform: TargetPlatform = targetPlatform
override fun dependencies(): List<ModuleInfo> = listOf(this, library) + nativeLibraries.values
override fun dependencies(): List<ModuleInfo> =
listOf(this, library) + extraModuleDependencies
}

val sourcesScope = createSourceModuleSearchScope(environment.project, sourceFiles)
Expand All @@ -211,10 +222,11 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
library -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope))
module -> ModuleContent(it, emptyList(), GlobalSearchScope.allScope(environment.project))
is DokkaKlibLibraryInfo -> {
if (it.libraryRoot in nativeLibraries)
if (it.libraryRoot in kotlinLibraries)
ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope))
else null
}
is CommonKlibModuleInfo -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope))
else -> null
} ?: throw IllegalArgumentException("Unexpected module info")
}
Expand All @@ -240,7 +252,8 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
projectContext,
module,
modulesContent,
environment
environment,
commonDependencyContainer
)
Platform.js -> createJsResolverForProject(projectContext, module, modulesContent)
Platform.native -> createNativeResolverForProject(projectContext, module, modulesContent)
Expand Down Expand Up @@ -282,13 +295,23 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
Platform.jvm -> JvmPlatformAnalyzerServices
}

@OptIn(ExperimentalStdlibApi::class)
private fun loadNativeLibraries(): Map<AbsolutePathString, LibraryModuleInfo> {
if (analysisPlatform != Platform.native && analysisPlatform != Platform.js) return emptyMap()

fun Collection<KotlinLibrary>.registerLibraries(): List<DokkaKlibLibraryInfo> {
if (analysisPlatform != Platform.native && analysisPlatform != Platform.js) return emptyList()
val dependencyResolver = DokkaKlibLibraryDependencyResolver()
val analyzerServices = analysisPlatform.analyzerServices()

return map { kotlinLibrary ->
if (analysisPlatform == org.jetbrains.dokka.Platform.native) DokkaNativeKlibLibraryInfo(
kotlinLibrary,
analyzerServices,
dependencyResolver
)
else DokkaJsKlibLibraryInfo(kotlinLibrary, analyzerServices, dependencyResolver)
}
}

@OptIn(ExperimentalStdlibApi::class)
private fun resolveKotlinLibraries(): Map<AbsolutePathString, KotlinLibrary> {
return buildMap {
classpath
.filter { it.isDirectory || (it.extension == "jar" || it.extension == KLIB_FILE_EXTENSION) }
Expand All @@ -303,12 +326,7 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
// exists, is KLIB, has compatible format
put(
libraryFile.absolutePath,
if (analysisPlatform == Platform.native) DokkaNativeKlibLibraryInfo(
kotlinLibrary,
analyzerServices,
dependencyResolver
)
else DokkaJsKlibLibraryInfo(kotlinLibrary, analyzerServices, dependencyResolver)
kotlinLibrary
)
}
} catch (e: Throwable) {
Expand All @@ -323,21 +341,14 @@ class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPl
projectContext: ProjectContext,
module: ModuleInfo,
modulesContent: (ModuleInfo) -> ModuleContent<ModuleInfo>,
environment: KotlinCoreEnvironment
environment: KotlinCoreEnvironment,
dependencyContainer: CommonDependenciesContainer?
): ResolverForProject<ModuleInfo> {
return object : AbstractResolverForProject<ModuleInfo>(
"Dokka",
projectContext,
modules = module.dependencies()
) {
val dependencyContainer by lazy {
DokkaKlibMetadataDependencyContainer(
classpath.filter { it.isDirectory || it.extension == KLIB_FILE_EXTENSION },
environment.configuration,
projectContext.storageManager
)
}

override fun modulesContent(module: ModuleInfo): ModuleContent<ModuleInfo> = modulesContent(module)

override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance
Expand Down
@@ -0,0 +1,25 @@
package org.jetbrains.dokka.analysis.resolve

import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.CommonPlatforms
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices

internal class CommonKlibModuleInfo(
override val name: Name,
val kotlinLibrary: KotlinLibrary,
private val dependOnModules: List<ModuleInfo>
) : ModuleInfo {
override fun dependencies(): List<ModuleInfo> = dependOnModules

override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns = ModuleInfo.DependencyOnBuiltIns.LAST

override val platform: TargetPlatform
get() = CommonPlatforms.defaultCommonPlatform

override val analyzerServices: PlatformDependentAnalyzerServices
get() = CommonPlatformAnalyzerServices
}
Expand Up @@ -2,7 +2,6 @@ package org.jetbrains.dokka.analysis.resolve

import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.analyzer.common.CommonDependenciesContainer
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
Expand All @@ -11,18 +10,13 @@ import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.konan.DeserializedKlibModuleOrigin
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.metadata.NativeTypeTransformer
import org.jetbrains.kotlin.library.metadata.NullFlexibleTypeDeserializer
import org.jetbrains.kotlin.library.metadata.parseModuleHeader
import org.jetbrains.kotlin.library.resolveSingleFileKlib
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.CommonPlatforms
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
import org.jetbrains.kotlin.serialization.konan.impl.KlibMetadataModuleDescriptorFactoryImpl
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager
Expand All @@ -31,35 +25,15 @@ import org.jetbrains.kotlin.utils.keysToMap
/**
* Adapted from org.jetbrains.kotlin.cli.metadata.KlibMetadataDependencyContainer
*/
class DokkaKlibMetadataDependencyContainer(
private val klibFiles: List<java.io.File>,
class DokkaKlibMetadataCommonDependencyContainer(
kotlinLibraries: List<KotlinLibrary>,
private val configuration: CompilerConfiguration,
private val storageManager: StorageManager
) : CommonDependenciesContainer {

private val kotlinLibraries = run {
klibFiles.map { resolveSingleFileKlib(File(it.absolutePath)) }
}

private val builtIns
get() = DefaultBuiltIns.Instance

private class KlibModuleInfo(
override val name: Name,
val kotlinLibrary: KotlinLibrary,
private val dependOnModules: List<ModuleInfo>
) : ModuleInfo {
override fun dependencies(): List<ModuleInfo> = dependOnModules

override fun dependencyOnBuiltIns(): ModuleInfo.DependencyOnBuiltIns = ModuleInfo.DependencyOnBuiltIns.LAST

override val platform: TargetPlatform
get() = CommonPlatforms.defaultCommonPlatform

override val analyzerServices: PlatformDependentAnalyzerServices
get() = CommonPlatformAnalyzerServices
}

private val mutableDependenciesForAllModuleDescriptors = mutableListOf<ModuleDescriptorImpl>().apply {
add(builtIns.builtInsModule)
}
Expand All @@ -82,10 +56,10 @@ class DokkaKlibMetadataDependencyContainer(
mutableDependenciesForAllModuleDescriptors.addAll(resultValues)
}

private val moduleInfosImpl: List<KlibModuleInfo> = mutableListOf<KlibModuleInfo>().apply {
private val moduleInfosImpl: List<CommonKlibModuleInfo> = mutableListOf<CommonKlibModuleInfo>().apply {
addAll(
moduleDescriptorsForKotlinLibraries.map { (kotlinLibrary, moduleDescriptor) ->
KlibModuleInfo(moduleDescriptor.name, kotlinLibrary, mutableDependenciesForAllModules)
CommonKlibModuleInfo(moduleDescriptor.name, kotlinLibrary, mutableDependenciesForAllModules)
}
)
mutableDependenciesForAllModules.addAll(this@apply)
Expand All @@ -107,7 +81,7 @@ class DokkaKlibMetadataDependencyContainer(
// initialized with the package fragment provider:
packageFragmentProviderForModuleInfo(moduleInfo)

return moduleDescriptorsForKotlinLibraries.getValue((moduleInfo as KlibModuleInfo).kotlinLibrary)
return moduleDescriptorsForKotlinLibraries.getValue((moduleInfo as CommonKlibModuleInfo).kotlinLibrary)
}

override fun registerDependencyForAllModules(
Expand All @@ -123,7 +97,7 @@ class DokkaKlibMetadataDependencyContainer(
): PackageFragmentProvider? {
if (moduleInfo !in moduleInfos)
return null
return packageFragmentProviderForKotlinLibrary((moduleInfo as KlibModuleInfo).kotlinLibrary)
return packageFragmentProviderForKotlinLibrary((moduleInfo as CommonKlibModuleInfo).kotlinLibrary)
}

private val klibMetadataModuleDescriptorFactory by lazy {
Expand Down Expand Up @@ -156,7 +130,6 @@ class DokkaKlibMetadataDependencyContainer(
libraryModuleDescriptor.initialize(it)
}
}

}

private val MetadataFactories =
Expand Down

0 comments on commit cd7b3cf

Please sign in to comment.