[KLIB Resolver] Add a heuristic to look up for KLIB file given only unique name

This fix adds a workaround to allow Kotlin/Native compiler to resolve
KLIB library if only library name was passed via `-l` CLI argument.

^KT-63931
This commit is contained in:
Dmitriy Dolovov
2023-12-11 18:01:44 +01:00
committed by Space Team
parent 2b1fc51f89
commit e9b4b8919d
3 changed files with 184 additions and 4 deletions
@@ -31,13 +31,28 @@ interface SearchPathResolver<L : KotlinLibrary> : WithLogger {
*/
class SearchRoot(val searchRootPath: File, val allowLookupByRelativePath: Boolean = false, val isDeprecated: Boolean = false) {
fun lookUp(libraryPath: File): LookupResult {
if (libraryPath.isAbsolute)
if (libraryPath.isAbsolute) {
// Look up by the absolute path if it is indeed an absolute path.
return LookupResult.Found(lookUpByAbsolutePath(libraryPath) ?: return LookupResult.NotFound)
}
if (!allowLookupByRelativePath && libraryPath.nameSegments.size > 1)
val isDefinitelyRelativePath = libraryPath.nameSegments.size > 1
if (isDefinitelyRelativePath && !allowLookupByRelativePath) {
// Lookup by the relative path is disallowed, but the path is definitely a relative path.
return LookupResult.NotFound
}
// First, try to resolve by the relative path.
val resolvedLibrary = lookUpByAbsolutePath(File(searchRootPath, libraryPath))
?: run {
if (!isDefinitelyRelativePath && libraryPath.extension.isEmpty()) {
// If the path actually looks like an unique name of the library, try to guess the name of the KLIB file.
// TODO: This logic is unreliable and needs to be replaced by the new KLIB resolver in the future.
lookUpByAbsolutePath(File(searchRootPath, "${libraryPath.path}.$KLIB_FILE_EXTENSION"))
} else null
}
?: return LookupResult.NotFound
val resolvedLibrary = lookUpByAbsolutePath(File(searchRootPath, libraryPath)) ?: return LookupResult.NotFound
return if (isDeprecated)
LookupResult.FoundWithWarning(
library = resolvedLibrary,
@@ -54,7 +69,7 @@ interface SearchPathResolver<L : KotlinLibrary> : WithLogger {
when {
absoluteLibraryPath.isFile -> {
// It's a really existing file.
when (absoluteLibraryPath.extension.toLowerCase()) {
when (absoluteLibraryPath.extension) {
KLIB_FILE_EXTENSION -> absoluteLibraryPath
"jar" -> {
// A special workaround for old JS stdlib, that was packed in a JAR file.
@@ -8,6 +8,15 @@ fun UnresolvedLibrary(path: String, libraryVersion: String?): RequiredUnresolved
fun UnresolvedLibrary(path: String, libraryVersion: String?, lenient: Boolean): UnresolvedLibrary =
if (lenient) LenientUnresolvedLibrary(path, libraryVersion) else RequiredUnresolvedLibrary(path, libraryVersion)
/**
* Representation of a Kotlin library that has not been yet resolved.
*
* TODO: This class has a major design flaw and needs to be replaced by the new KLIB resolver in the future.
* - In certain situations [path] represents a path to the library, would it be relative or absolute.
* - In certain situations [path] represents an `unique_name` of the library.
* - In general, `unique_name` needs not be equal to the file name of the library. And this adds some mess to the classes
* that implement the "resolver" logic, e.g. [SearchPathResolver].
*/
sealed class UnresolvedLibrary {
abstract val path: String
abstract val libraryVersion: String?