Skip to content

Commit

Permalink
Support short library names in compiler, interop and platform libs
Browse files Browse the repository at this point in the history
Fixes for KT-36720 and KT-36721 change a scheme that is used to generate
unique names for klibs: now we use "fully qualified" names that
include a group and a name of a library (e.g. org.jetbrains.kotlin.
native.posix).

But in some cases we still need short library names. For example
to display platform libs in the IDE or to export some symbols of
a library to an ObjC framework.

This patch adds support for such short names to:
 - klib generation in the compiler
 - cinterop tool (both sourcecode and metadata mode)
 - platform libraries (both prebuilt and built on the user side)

Support for C/ObjC export remains.
  • Loading branch information
ilmat192 committed Apr 9, 2020
1 parent 624328c commit ce1cb39
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const val PURGE_USER_LIBS = "Xpurge-user-libs"
const val TEMP_DIR = "Xtemporary-files-dir"
const val NOPACK = "nopack"
const val COMPILE_SOURCES = "Xcompile-source"
const val SHORT_MODULE_NAME = "Xshort-module-name"

// TODO: unify camel and snake cases.
// Possible solution is to accept both cases
Expand Down Expand Up @@ -108,6 +109,11 @@ open class CInteropArguments(argParser: ArgParser =
fullName = "Xsource-compiler-option",
description = "compiler options for sources provided via -$COMPILE_SOURCES"
).multiple()

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"
)
}

class JSInteropArguments(argParser: ArgParser = ArgParser("jsinterop",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fun createInteropLibrary(
target: KonanTarget,
manifest: Properties,
dependencies: List<KotlinLibrary>,
nopack: Boolean
nopack: Boolean,
shortName: String?
) {
val version = KotlinLibraryVersioning(
libraryVersion = null,
Expand All @@ -49,7 +50,8 @@ fun createInteropLibrary(
target,

BuiltInsPlatform.NATIVE,
nopack = nopack
nopack = nopack,
shortName = shortName
).apply {
val metadata = metadata.write(ChunkingWriteStrategy())
addMetadata(SerializedMetadata(metadata.header, metadata.fragments, metadata.fragmentNames))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ private fun processCLib(flavorName: String, cinteropArguments: CInteropArguments
outputPath = cinteropArguments.output,
manifest = def.manifestAddendProperties,
dependencies = stdlibDependency + imports.requiredLibraries.toList(),
nopack = cinteropArguments.nopack
nopack = cinteropArguments.nopack,
shortName = cinteropArguments.shortModuleName
)
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
arguments.libraries.toNonNullList())
put(LINKER_ARGS, arguments.linkerArguments.toNonNullList() +
arguments.singleLinkerArguments.toNonNullList())
arguments.moduleName ?. let{ put(MODULE_NAME, it) }
arguments.target ?.let{ put(TARGET, it) }
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,
arguments.includeBinaries.toNonNullList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,17 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(
value = INCLUDE_ARG,
valueDescription = "<path>",
description = "A path to an intermediate library that should be processed in the same manner as source files.\n"
description = "A path to an intermediate library that should be processed in the same manner as source files"
)
var includes: Array<String>? = null

@Argument(
value = "-Xshort-module-name",
valueDescription = "<name>",
description = "A short name used to denote this library in the IDE and during Objective-C export"
)
var shortModuleName: String? = null

@Argument(value = STATIC_FRAMEWORK_FLAG, description = "Create a framework with a static library instead of a dynamic one")
var staticFramework: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ internal fun produceOutput(context: Context) {
CompilerOutputKind.LIBRARY -> {
val output = context.config.outputFiles.outputName
val libraryName = context.config.moduleId
val shortLibraryName = context.config.shortModuleName
val neededLibraries = context.librariesWithDependencies
val abiVersion = KotlinAbiVersion.CURRENT
val compilerVersion = CompilerVersion.CURRENT.toString()
Expand Down Expand Up @@ -159,6 +160,7 @@ internal fun produceOutput(context: Context) {
output,
libraryName,
nopack,
shortLibraryName,
manifestProperties,
context.dataFlowGraph)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
val moduleId: String
get() = configuration.get(KonanConfigKeys.MODULE_NAME) ?: File(outputFiles.outputName).name

val shortModuleName: String?
get() = configuration.get(KonanConfigKeys.SHORT_MODULE_NAME)

val infoArgsOnly = configuration.kotlinSourceRoots.isEmpty()
&& configuration[KonanConfigKeys.INCLUDED_LIBRARIES].isNullOrEmpty()
&& librariesToCache.isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class KonanConfigKeys {
= CompilerConfigurationKey("klibs processed in the same manner as source files")
val SOURCE_MAP: CompilerConfigurationKey<List<String>>
= CompilerConfigurationKey.create("generate source map")
val SHORT_MODULE_NAME: CompilerConfigurationKey<String?>
= CompilerConfigurationKey("short module name for IDE and export")
val STATIC_FRAMEWORK: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("produce a static library for a framework")
val TARGET: CompilerConfigurationKey<String?>
Expand Down
2 changes: 1 addition & 1 deletion platformLibs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ project.rootProject.ext.platformManager.enabled.each { target ->
"${fileNamePrefix}${it}".toString()
}
}
extraOpts '-Xpurge-user-libs'
extraOpts '-Xpurge-user-libs', "-Xshort-module-name", df.name
compilerOpts "-fmodules-cache-path=${project.buildDir}/clangModulesCache"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ class KonanLibraryWriterImpl(
target: KonanTarget,
builtInsPlatform: BuiltInsPlatform,
nopack: Boolean = false,
shortName: String? = null,

val layout: KonanLibraryLayoutForWriter = KonanLibraryLayoutForWriter(libDir, target),

base: BaseWriter = BaseWriterImpl(layout, moduleName, versions, builtInsPlatform, nopack),
base: BaseWriter = BaseWriterImpl(layout, moduleName, versions, builtInsPlatform, nopack, shortName),
bitcode: BitcodeWriter = BitcodeWriterImpl(layout),
metadata: MetadataWriter = MetadataWriterImpl(layout),
ir: IrWriter = IrMonoliticWriterImpl(layout)
Expand All @@ -51,11 +52,20 @@ fun buildLibrary(
output: String,
moduleName: String,
nopack: Boolean,
shortName: String?,
manifestProperties: Properties?,
dataFlowGraph: ByteArray?
): KonanLibraryLayout {

val library = KonanLibraryWriterImpl(File(output), moduleName, versions, target, BuiltInsPlatform.NATIVE, nopack)
val library = KonanLibraryWriterImpl(
File(output),
moduleName,
versions,
target,
BuiltInsPlatform.NATIVE,
nopack,
shortName
)

library.addMetadata(metadata)
if (ir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.konan.util.visibleName
import org.jetbrains.kotlin.native.interop.tool.CommonInteropArguments.Companion.DEFAULT_MODE
import org.jetbrains.kotlin.native.interop.tool.CommonInteropArguments.Companion.MODE_METADATA
import org.jetbrains.kotlin.native.interop.tool.CommonInteropArguments.Companion.MODE_SOURCECODE
import org.jetbrains.kotlin.native.interop.tool.SHORT_MODULE_NAME
import java.io.PrintWriter
import java.io.StringWriter
import java.util.concurrent.atomic.AtomicInteger
Expand Down Expand Up @@ -167,6 +168,9 @@ private class DefFile(val name: String, val depends: MutableList<DefFile>) {

val libraryName: String
get() = "${PlatformLibsInfo.namePrefix}$name"

val shortLibraryName: String
get() = name
}

private fun createTempDir(prefix: String, parent: File): File =
Expand Down Expand Up @@ -236,6 +240,7 @@ private fun generateLibrary(
"-repo", outputDirectory.absolutePath,
"-no-default-libs", "-no-endorsed-libs", "-Xpurge-user-libs", "-nopack",
"-mode", mode,
"-$SHORT_MODULE_NAME", def.shortLibraryName,
*def.depends.flatMap { listOf("-l", "$outputDirectory/${it.libraryName}") }.toTypedArray()
)
logger.verbose("Run cinterop with args: ${cinteropArgs.joinToString(separator = " ")}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fun invokeInterop(flavor: String, args: Array<String>): Array<String>? {
val purgeUserLibs = arguments.purgeUserLibs
val nopack = arguments.nopack
val temporaryFilesDir = arguments.tempDir
val shortModuleName = (arguments as? CInteropArguments)?.shortModuleName

val buildDir = File("$outputFileName-build")
val generatedDir = File(buildDir, "kotlin")
Expand Down Expand Up @@ -66,6 +67,7 @@ fun invokeInterop(flavor: String, args: Array<String>): Array<String>? {
(if (noEndorsedLibs) arrayOf("-$NOENDORSEDLIBS") else emptyArray()) +
(if (purgeUserLibs) arrayOf("-$PURGE_USER_LIBS") else emptyArray()) +
(if (nopack) arrayOf("-$NOPACK") else emptyArray()) +
shortModuleName?.let { arrayOf("-Xshort-module-name=$it") }.orEmpty() +
arguments.kotlincOption
}

Expand Down

0 comments on commit ce1cb39

Please sign in to comment.