diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt index 1961280cde3..fe56513d669 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt @@ -112,7 +112,7 @@ open class CInteropArguments(argParser: ArgParser = val shortModuleName by argParser.option(ArgType.String, fullName = SHORT_MODULE_NAME, - description = "A short name used to denote this library in the IDE and during Objective-C export" + description = "A short name used to denote this library in the IDE" ) val moduleName by argParser.option(ArgType.String, diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt index 6c80b64cd2c..2416f3a21ff 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt @@ -202,7 +202,7 @@ class K2NativeCompilerArguments : CommonCompilerArguments() { @Argument( value = "-Xshort-module-name", valueDescription = "", - description = "A short name used to denote this library in the IDE and during Objective-C export" + description = "A short name used to denote this library in the IDE and in a generated Objective-C header" ) var shortModuleName: String? = null diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt index 09a94a6f21e..c55c799e2a1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportNamer.kt @@ -11,7 +11,10 @@ import org.jetbrains.kotlin.backend.konan.descriptors.isInterface import org.jetbrains.kotlin.descriptors.konan.isNativeStdlib import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.konan.* import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.library.shortName +import org.jetbrains.kotlin.library.uniqueName import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.Name @@ -857,7 +860,14 @@ private fun ObjCExportMapper.canHaveSameName(first: PropertyDescriptor, second: internal val ModuleDescriptor.namePrefix: String get() { if (this.isNativeStdlib()) return "Kotlin" - return abbreviate(this.name.asString().let { it.substring(1, it.lastIndex) }) + val fullPrefix = when(val module = this.klibModuleOrigin) { + CurrentKlibModuleOrigin, SyntheticModulesOrigin -> + this.name.asString().let { it.substring(1, it.lastIndex) } + is DeserializedKlibModuleOrigin -> + module.library.let { it.shortName ?: it.uniqueName } + } + + return abbreviate(fullPrefix) } internal fun abbreviate(name: String): String { diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 7e300e1570a..9e02c83ad92 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3894,14 +3894,29 @@ if (isAppleTarget(project)) { } konanArtifacts { + def libraryName = frameworkName + "Library" + library(libraryName, targets: [target.name]) { + srcDir "objcexport/library" + artifactName "test-library" + + extraOpts "-Xshort-module-name=MyLibrary" + extraOpts "-module-name", "org.jetbrains.kotlin.native.test-library" + } + framework(frameworkName, targets: [target.name]) { - srcDir 'objcexport' + srcFiles fileTree("objcexport") { + include "*.kt" + } baseDir dir if (!useCustomDist) { dependsOn ":${target.name}CrossDistRuntime", ':distCompiler' } + libraries { + artifact libraryName + } + extraOpts "-Xembed-bitcode-marker" extraOpts "-Xemit-lazy-objc-header=$lazyHeader" extraOpts project.globalTestArgs @@ -3924,14 +3939,29 @@ if (isAppleTarget(project)) { final String dir = "$testOutputFramework/$testName" konanArtifacts { + def libraryName = frameworkName + "Library" + library(libraryName, targets: [target.name]) { + srcDir "objcexport/library" + artifactName "test-library" + + extraOpts "-Xshort-module-name=MyLibrary" + extraOpts "-module-name", "org.jetbrains.kotlin.native.test-library" + } + framework(frameworkName, targets: [target.name]) { - srcDir 'objcexport' + srcFiles fileTree("objcexport") { + include "*.kt" + } baseDir dir if (!useCustomDist) { dependsOn ":${target.name}CrossDistRuntime", ':distCompiler' } + libraries { + artifact libraryName + } + extraOpts "-Xembed-bitcode-marker" extraOpts "-Xstatic-framework" extraOpts project.globalTestArgs diff --git a/backend.native/tests/objcexport/coroutines.kt b/backend.native/tests/objcexport/coroutines.kt index 204ec020945..854f2d9101a 100644 --- a/backend.native/tests/objcexport/coroutines.kt +++ b/backend.native/tests/objcexport/coroutines.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + package coroutines import kotlin.coroutines.* diff --git a/backend.native/tests/objcexport/coroutines.swift b/backend.native/tests/objcexport/coroutines.swift index 3cb587d7a21..41762f677b7 100644 --- a/backend.native/tests/objcexport/coroutines.swift +++ b/backend.native/tests/objcexport/coroutines.swift @@ -1,3 +1,8 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + import Kt private func testCallSimple() throws { diff --git a/backend.native/tests/objcexport/expectedLazy.h b/backend.native/tests/objcexport/expectedLazy.h index dd2bc9965f0..a90ae1b33ce 100644 --- a/backend.native/tests/objcexport/expectedLazy.h +++ b/backend.native/tests/objcexport/expectedLazy.h @@ -204,6 +204,14 @@ __attribute__((swift_name("GH4002Argument"))) + (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); @end; +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("LibraryKt"))) +@interface KtLibraryKt : KtBase ++ (NSString *)readDataFromLibraryClassInput:(KtA *)input __attribute__((swift_name("readDataFromLibraryClass(input:)"))); ++ (NSString *)readDataFromLibraryInterfaceInput:(id)input __attribute__((swift_name("readDataFromLibraryInterface(input:)"))); ++ (NSString *)readDataFromLibraryEnumInput:(KtE *)input __attribute__((swift_name("readDataFromLibraryEnum(input:)"))); +@end; + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("DelegateClass"))) @interface KtDelegateClass : KtBase diff --git a/backend.native/tests/objcexport/gh4002.kt b/backend.native/tests/objcexport/gh4002.kt index ed11b94e8f6..b162084fc2b 100644 --- a/backend.native/tests/objcexport/gh4002.kt +++ b/backend.native/tests/objcexport/gh4002.kt @@ -1,3 +1,8 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + package gh4002 open class GH4002ArgumentBase diff --git a/backend.native/tests/objcexport/gh4002.swift b/backend.native/tests/objcexport/gh4002.swift index d962f27bf20..d0bfbd19aa1 100644 --- a/backend.native/tests/objcexport/gh4002.swift +++ b/backend.native/tests/objcexport/gh4002.swift @@ -1,3 +1,8 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + import Kt // See https://github.com/JetBrains/kotlin-native/issues/4002 diff --git a/backend.native/tests/objcexport/library.kt b/backend.native/tests/objcexport/library.kt new file mode 100644 index 00000000000..d0f6f142bb0 --- /dev/null +++ b/backend.native/tests/objcexport/library.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package library + +fun readDataFromLibraryClass(input: A): String { + return input.data +} + +fun readDataFromLibraryInterface(input: I): String { + return input.data +} + +fun readDataFromLibraryEnum(input: E): String { + return input.data +} \ No newline at end of file diff --git a/backend.native/tests/objcexport/library.swift b/backend.native/tests/objcexport/library.swift new file mode 100644 index 00000000000..f3c3dcd5d19 --- /dev/null +++ b/backend.native/tests/objcexport/library.swift @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +import Kt + +func testAccessClassFromLibraryWithShortName() throws { + + let object: MyLibraryA = MyLibraryA(data: "Data from Class") + let interface: MyLibraryI = MyLibraryA(data: "Data from Interface") + let enumObject: MyLibraryE = MyLibraryE.b + + + let dataFromClass = LibraryKt.readDataFromLibraryClass(input: object) + let dataFromInterface = LibraryKt.readDataFromLibraryInterface(input: interface) + let dataFromEnum = LibraryKt.readDataFromLibraryEnum(input: enumObject) + + try assertEquals(actual: dataFromClass, expected: "Data from Class") + try assertEquals(actual: dataFromInterface, expected: "Data from Interface") + try assertEquals(actual: dataFromEnum, expected: "Enum entry B") +} + +class LibraryTests : SimpleTestProvider { + override init() { + super.init() + + test("testAccessClassFromLibraryWithShortName", testAccessClassFromLibraryWithShortName) + } +} \ No newline at end of file diff --git a/backend.native/tests/objcexport/library/library.kt b/backend.native/tests/objcexport/library/library.kt new file mode 100644 index 00000000000..e53e436eb86 --- /dev/null +++ b/backend.native/tests/objcexport/library/library.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package library + +interface I { + val data: String +} + +class A(override val data: String): I + +enum class E(val data: String) { + A("Enum entry A"), + B("Enum entry B"), + C("Enum entry C") +} \ No newline at end of file diff --git a/backend.native/tests/objcexport/values.swift b/backend.native/tests/objcexport/values.swift index 8abf98741ff..594affd173e 100644 --- a/backend.native/tests/objcexport/values.swift +++ b/backend.native/tests/objcexport/values.swift @@ -1,17 +1,6 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ import Foundation