From f93d5604aa9f093a21142bb48ba5ae8f7785d2e5 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Mon, 20 Sep 2021 20:27:48 +0700 Subject: [PATCH] [K/N] Warn if the inferred main package of a framework is root --- .../backend/konan/objcexport/ObjCExport.kt | 41 +++++++++++++++---- .../backend.native/tests/build.gradle | 14 +------ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExport.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExport.kt index 9d466c6441c..b7a0e5de66b 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExport.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExport.kt @@ -14,8 +14,10 @@ import org.jetbrains.kotlin.backend.konan.getExportedDependencies import org.jetbrains.kotlin.backend.konan.llvm.CodeGenerator import org.jetbrains.kotlin.backend.konan.llvm.objcexport.ObjCExportBlockCodeGenerator import org.jetbrains.kotlin.backend.konan.llvm.objcexport.ObjCExportCodeGenerator +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.descriptors.SourceFile import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.konan.exec.Command @@ -158,10 +160,7 @@ internal class ObjCExport(val context: Context, symbolTable: SymbolTable) { } val file = directory.child("Info.plist") - // TODO: consider showing warning if the main package is root. - val bundleId = context.configuration[BUNDLE_ID] - ?: guessMainPackage().child(Name.identifier(name)).asString() - + val bundleId = guessBundleID(name) val platform = properties.platformName() val minimumOsVersion = properties.osVersionMin @@ -293,8 +292,12 @@ internal class ObjCExport(val context: Context, symbolTable: SymbolTable) { } } - private fun guessMainPackage(): FqName { - val allPackages = (context.getIncludedLibraryDescriptors() + context.getExportedDependencies() + context.moduleDescriptor).flatMap { + private fun guessMainPackage(modules: List): FqName? { + if (modules.isEmpty()) { + return null + } + + val allPackages = modules.flatMap { it.getPackageFragments() // Includes also all parent packages, e.g. the root one. } @@ -305,6 +308,30 @@ internal class ObjCExport(val context: Context, symbolTable: SymbolTable) { return allPackages.map { it.fqName }.distinct() .filter { candidate -> nonEmptyPackages.all { it.isSubpackageOf(candidate) } } // Now there are all common ancestors of non-empty packages. Longest of them is the least common accessor: - .maxByOrNull { it.asString().length }!! + .maxByOrNull { it.asString().length } + } + + private fun guessBundleID(bundleName: String): String { + val configuration = context.configuration + configuration[BUNDLE_ID]?.let { + return it + } + + // Consider exported libraries only if we cannot infer the package from sources or included libs. + val mainPackage = guessMainPackage(context.getIncludedLibraryDescriptors() + context.moduleDescriptor) + ?: guessMainPackage(context.getExportedDependencies()) + ?: FqName.ROOT + + val bundleID = mainPackage.child(Name.identifier(bundleName)).asString() + + if (mainPackage.isRoot) { + configuration.report( + CompilerMessageSeverity.STRONG_WARNING, + "Cannot infer a bundle ID from packages of source files and exported dependencies, " + + "use the bundle name instead: $bundleName. " + + "Please specify the bundle ID explicitly using the -Xbundle-id compiler flag." + ) + } + return bundleID } } diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 0b2f82e9470..0c139ad57fc 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -5486,20 +5486,8 @@ if (isAppleTarget(project)) { def currentTarget = project.target.name enabled = currentTarget.startsWith("mac") - konanArtifacts { - library("frameworkBundleIdLib", targets: [target.name]) { - srcFiles("framework/bundle_id/lib.kt") - delegate.getByTarget(target.name).configure{ - UtilsKt.dependsOnDist(it) - } - } - } - framework("Foo") { - sources = ["framework/bundle_id/main.kt"] - library = "frameworkBundleIdLib" - def exportedLib = konanArtifacts[library].getArtifactByTarget(target.name) - opts = ["-Xexport-library=$exportedLib"] + sources = ["framework/bundle_id/main.kt", "framework/bundle_id/lib.kt"] } swiftSources = []