Properly check platform of klib by reading manifest

This commit is contained in:
Dmitry Savvinov
2020-03-03 15:24:52 +03:00
committed by Dmitriy Dolovov
parent 4742057b51
commit 6ccb8cd0a3
3 changed files with 31 additions and 12 deletions
@@ -90,21 +90,30 @@ internal val VirtualFile.isKonanLibraryRoot: Boolean
fun checkComponent(componentFile: VirtualFile): Boolean {
val manifestFile = componentFile.findChild(KLIB_MANIFEST_FILE_NAME)?.takeIf { !it.isDirectory } ?: return false
// this is a hacky way to determine whether this is a Kotlin/Native .klib or common .klib (common .klibs don't have ir)
// TODO(dsavvinov): introduce more robust way to detect library platform
val irFolder = componentFile.findChild(KLIB_IR_FOLDER_NAME)
if (irFolder == null || irFolder.children.isEmpty()) return false
val manifestProperties = try {
manifestFile.inputStream.use { Properties().apply { load(it) } }
} catch (_: IOException) {
return false
}
return manifestProperties.getProperty(KLIB_PROPERTY_BUILTINS_PLATFORM) == BuiltInsPlatform.NATIVE.name
if (!manifestProperties.containsKey(KLIB_PROPERTY_UNIQUE_NAME)) return false
// No builtins_platform property => either a new common klib (we don't write builtins_platform for common) or old Native klib
val builtInsPlatformProperty = manifestProperties.getProperty(KLIB_PROPERTY_BUILTINS_PLATFORM)
?: return componentFile.isLegacyNativeKlibComponent // TODO(dsavvinov): drop additional legacy check after 1.4
val builtInsPlatform = BuiltInsPlatform.parseFromString(builtInsPlatformProperty) ?: return false
return builtInsPlatform == BuiltInsPlatform.NATIVE
}
// run check for library root too
// this is necessary to recognize old style KLIBs that do not have components, and report tem to user appropriately
return checkComponent(this) || children?.any(::checkComponent) == true
}
private val VirtualFile.isLegacyNativeKlibComponent: Boolean
get() {
val irFolder = findChild(KLIB_IR_FOLDER_NAME)
return irFolder != null && irFolder.children.isNotEmpty()
}