Demote fatal error to warning on unresolved libraries mentioned in Klib's manifest 'depends' for metadata compilation

^KT-46107
This commit is contained in:
sebastian.sellmair
2021-04-15 13:23:59 +02:00
committed by Space
parent 95a1a4e66a
commit 22ea5c7de4
6 changed files with 84 additions and 37 deletions
@@ -74,9 +74,12 @@ val BaseKotlinLibrary.uniqueName: String
val BaseKotlinLibrary.shortName: String?
get() = manifestProperties.getProperty(KLIB_PROPERTY_SHORT_NAME)
val BaseKotlinLibrary.unresolvedDependencies: List<UnresolvedLibrary>
get() = manifestProperties.propertyList(KLIB_PROPERTY_DEPENDS, escapeInQuotes = true)
.map { UnresolvedLibrary(it, manifestProperties.getProperty("dependency_version_$it")) }
val BaseKotlinLibrary.unresolvedDependencies: List<RequiredUnresolvedLibrary>
get() = unresolvedDependencies(lenient = false).map { it as RequiredUnresolvedLibrary }
fun BaseKotlinLibrary.unresolvedDependencies(lenient: Boolean = false): List<UnresolvedLibrary> =
manifestProperties.propertyList(KLIB_PROPERTY_DEPENDS, escapeInQuotes = true)
.map { UnresolvedLibrary(it, manifestProperties.getProperty("dependency_version_$it"), lenient = lenient) }
interface KotlinLibrary : BaseKotlinLibrary, MetadataLibrary, IrLibrary
@@ -16,13 +16,19 @@ const val KOTLIN_STDLIB_NAME = "stdlib"
interface SearchPathResolver<L : KotlinLibrary> : WithLogger {
val searchRoots: List<File>
fun resolutionSequence(givenPath: String): Sequence<File>
fun resolve(unresolved: UnresolvedLibrary, isDefaultLink: Boolean = false): L
fun resolve(unresolved: LenientUnresolvedLibrary, isDefaultLink: Boolean = false): L?
fun resolve(unresolved: RequiredUnresolvedLibrary, isDefaultLink: Boolean = false): L
fun resolve(givenPath: String): L
fun defaultLinks(noStdLib: Boolean, noDefaultLibs: Boolean, noEndorsedLibs: Boolean): List<L>
fun libraryMatch(candidate: L, unresolved: UnresolvedLibrary): Boolean
fun isProvidedByDefault(unresolved: UnresolvedLibrary): Boolean = false
}
fun <L : KotlinLibrary> SearchPathResolver<L>.resolve(unresolved: UnresolvedLibrary): L? = when (unresolved) {
is LenientUnresolvedLibrary -> resolve(unresolved)
is RequiredUnresolvedLibrary -> resolve(unresolved)
}
// This is a simple library resolver that only cares for file names.
abstract class KotlinLibrarySearchPathResolver<L : KotlinLibrary>(
repositories: List<String>,
@@ -134,29 +140,42 @@ abstract class KotlinLibrarySearchPathResolver<L : KotlinLibrary>(
// Default libraries could be resolved several times during findLibraries and resolveDependencies.
// Store already resolved libraries.
private val resolvedLibraries = HashMap<UnresolvedLibrary, L>()
private inner class ResolvedLibrary(val library: L?)
override fun resolve(unresolved: UnresolvedLibrary, isDefaultLink: Boolean): L {
private val resolvedLibraries = HashMap<UnresolvedLibrary, ResolvedLibrary>()
private fun resolveOrNull(unresolved: UnresolvedLibrary, isDefaultLink: Boolean): L? {
return resolvedLibraries.getOrPut(unresolved) {
val givenPath = unresolved.path
try {
val fileSequence = resolutionSequence(givenPath)
val matching = fileSequence
.filterOutPre_1_4_libraries()
.flatMap { libraryComponentBuilder(it, isDefaultLink).asSequence() }
.map { it.takeIf { libraryMatch(it, unresolved) } }
.filterNotNull()
matching.firstOrNull() ?: run {
logger.fatal("Could not find \"$givenPath\" in ${searchRoots.map { it.absolutePath }}")
}
resolutionSequence(givenPath)
.filterOutPre_1_4_libraries()
.flatMap { libraryComponentBuilder(it, isDefaultLink).asSequence() }
.map { it.takeIf { libraryMatch(it, unresolved) } }
.filterNotNull()
.firstOrNull()
.let(::ResolvedLibrary)
} catch (e: Throwable) {
logger.error("Failed to resolve Kotlin library: $givenPath")
throw e
}
}.library
}
override fun resolve(unresolved: LenientUnresolvedLibrary, isDefaultLink: Boolean): L? {
return resolveOrNull(unresolved, isDefaultLink).also { resolvedLibrary ->
if (resolvedLibrary == null) {
logger.warning("Could not find \"${unresolved.path}\" in ${searchRoots.map { it.absolutePath }}")
}
}
}
override fun resolve(unresolved: RequiredUnresolvedLibrary, isDefaultLink: Boolean): L {
return resolveOrNull(unresolved, isDefaultLink)
?: logger.fatal("Could not find \"${unresolved.path}\" in ${searchRoots.map { it.absolutePath }}")
}
override fun libraryMatch(candidate: L, unresolved: UnresolvedLibrary): Boolean = true
override fun resolve(givenPath: String) = resolve(UnresolvedLibrary(givenPath, null), false)
@@ -1,10 +1,33 @@
@file:Suppress("FunctionName")
package org.jetbrains.kotlin.library
data class UnresolvedLibrary(
val path: String,
val libraryVersion: String?) {
fun UnresolvedLibrary(path: String, libraryVersion: String?): RequiredUnresolvedLibrary =
RequiredUnresolvedLibrary(path, libraryVersion)
fun substitutePath(newPath: String): UnresolvedLibrary {
return UnresolvedLibrary(newPath, libraryVersion)
fun UnresolvedLibrary(path: String, libraryVersion: String?, lenient: Boolean): UnresolvedLibrary =
if (lenient) LenientUnresolvedLibrary(path, libraryVersion) else RequiredUnresolvedLibrary(path, libraryVersion)
sealed class UnresolvedLibrary {
abstract val path: String
abstract val libraryVersion: String?
abstract fun substitutePath(newPath: String): UnresolvedLibrary
}
data class RequiredUnresolvedLibrary(
override val path: String,
override val libraryVersion: String?
) : UnresolvedLibrary() {
override fun substitutePath(newPath: String): RequiredUnresolvedLibrary {
return copy(path = newPath)
}
}
}
data class LenientUnresolvedLibrary(
override val path: String,
override val libraryVersion: String?
) : UnresolvedLibrary() {
override fun substitutePath(newPath: String): LenientUnresolvedLibrary {
return copy(path = newPath)
}
}