From 721a84ea441a9f3bb0f9011d686493beba503143 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Tue, 31 Mar 2020 13:33:01 +0700 Subject: [PATCH] [CLI] Allow using -Xshort-module-name for klib compilation only --- .../org/jetbrains/kotlin/cli/bc/K2Native.kt | 25 ++++++++++++++++++- .../cli/bc/K2NativeCompilerArguments.kt | 5 ++-- .../kotlin/cli/utilities/InteropCompiler.kt | 3 ++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index 5f17204b51f..92bc57bb478 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -124,7 +124,6 @@ class K2Native : CLICompiler() { put(LINKER_ARGS, arguments.linkerArguments.toNonNullList() + arguments.singleLinkerArguments.toNonNullList()) arguments.moduleName?.let{ put(MODULE_NAME, it) } - arguments.shortModuleName?.let { put(SHORT_MODULE_NAME, it) } arguments.target?.let{ put(TARGET, it) } put(INCLUDED_BINARY_FILES, @@ -229,6 +228,10 @@ class K2Native : CLICompiler() { libraryToAddToCache?.let { put(LIBRARY_TO_ADD_TO_CACHE, it) } put(CACHE_DIRECTORIES, cacheDirectories) put(CACHED_LIBRARIES, parseCachedLibraries(arguments, configuration)) + + parseShortModuleName(arguments, configuration, outputKind)?.let { + put(SHORT_MODULE_NAME, it) + } } } } @@ -374,5 +377,25 @@ private fun parseLibraryToAddToCache( } } +// TODO: Support short names for current module in ObjC export and lift this limitation. +private fun parseShortModuleName( + arguments: K2NativeCompilerArguments, + configuration: CompilerConfiguration, + outputKind: CompilerOutputKind +): String? { + val input = arguments.shortModuleName + + return if (input != null && outputKind != CompilerOutputKind.LIBRARY) { + configuration.report( + STRONG_WARNING, + "$SHORT_MODULE_NAME_ARG is only supported when producing a Kotlin library, " + + "but the compiler is producing ${outputKind.name.toLowerCase()}" + ) + null + } else { + input + } +} + fun main(args: Array) = K2Native.main(args) fun mainNoExit(args: Array) = K2Native.mainNoExit(args) 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 2416f3a21ff..3bf62fe3c97 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 @@ -200,7 +200,7 @@ class K2NativeCompilerArguments : CommonCompilerArguments() { var includes: Array? = null @Argument( - value = "-Xshort-module-name", + value = SHORT_MODULE_NAME_ARG, valueDescription = "", description = "A short name used to denote this library in the IDE and in a generated Objective-C header" ) @@ -281,4 +281,5 @@ const val STATIC_FRAMEWORK_FLAG = "-Xstatic-framework" const val INCLUDE_ARG = "-Xinclude" const val CACHED_LIBRARY = "-Xcached-library" const val MAKE_CACHE = "-Xmake-cache" -const val ADD_CACHE = "-Xadd-cache" \ No newline at end of file +const val ADD_CACHE = "-Xadd-cache" +const val SHORT_MODULE_NAME_ARG = "-Xshort-module-name" \ No newline at end of file diff --git a/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/InteropCompiler.kt b/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/InteropCompiler.kt index f68a87daba0..6cc4cb7246f 100644 --- a/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/InteropCompiler.kt +++ b/utilities/src/main/kotlin/org/jetbrains/kotlin/cli/utilities/InteropCompiler.kt @@ -4,6 +4,7 @@ */ package org.jetbrains.kotlin.cli.utilities +import org.jetbrains.kotlin.cli.bc.SHORT_MODULE_NAME_ARG import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.konan.target.PlatformManager import org.jetbrains.kotlin.native.interop.gen.jvm.InternalInteropOptions @@ -69,7 +70,7 @@ fun invokeInterop(flavor: String, args: Array): Array? { (if (purgeUserLibs) arrayOf("-$PURGE_USER_LIBS") else emptyArray()) + (if (nopack) arrayOf("-$NOPACK") else emptyArray()) + moduleName?.let { arrayOf("-module-name", it) }.orEmpty() + - shortModuleName?.let { arrayOf("-Xshort-module-name=$it") }.orEmpty() + + shortModuleName?.let { arrayOf("$SHORT_MODULE_NAME_ARG=$it") }.orEmpty() + arguments.kotlincOption }