From f29b52764a3e77295f6f6d11abf64e24c3522a32 Mon Sep 17 00:00:00 2001 From: LepilkinaElena Date: Thu, 19 Mar 2020 15:56:50 +0300 Subject: [PATCH] Made cinterop command line options consistent (#3972) --- .../native/interop/gen/jvm/CommandLine.kt | 8 +--- .../kotlin/native/interop/gen/jvm/main.kt | 42 ++++++++++++++----- .../native/interop/gen/wasm/StubGenerator.kt | 10 ++--- .../kotlin/cli/utilities/InteropCompiler.kt | 18 ++++---- 4 files changed, 44 insertions(+), 34 deletions(-) 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 5343dc62bbe..eb4a3ad1dad 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 @@ -33,8 +33,6 @@ const val COMPILE_SOURCES = "Xcompile-source" // Possible solution is to accept both cases open class CommonInteropArguments(val argParser: ArgParser) { val verbose by argParser.option(ArgType.Boolean, description = "Enable verbose logging output").default(false) - val flavor by argParser.option(ArgType.Choice(listOf("jvm", "native", "wasm")), description = "Interop target") - .default("jvm") val pkg by argParser.option(ArgType.String, description = "place generated bindings to the package") val output by argParser.option(ArgType.String, shortName = "o", description = "specifies the resulting library file") .default("nativelib") @@ -42,10 +40,6 @@ open class CommonInteropArguments(val argParser: ArgParser) { .multiple().delimiter(",") val staticLibrary by argParser.option(ArgType.String, description = "embed static library to the result") .multiple().delimiter(",") - val generated by argParser.option(ArgType.String, description = "place generated bindings to the directory") - .default(System.getProperty("user.dir")) - val natives by argParser.option(ArgType.String, description = "where to put the built native files") - .default(System.getProperty("user.dir")) val library by argParser.option(ArgType.String, shortName = "l", description = "library to use for building") .multiple() val repo by argParser.option(ArgType.String, shortName = "r", description = "repository to resolve dependencies") @@ -76,7 +70,7 @@ open class CommonInteropArguments(val argParser: ArgParser) { } } -class CInteropArguments(argParser: ArgParser = +open class CInteropArguments(argParser: ArgParser = ArgParser("cinterop", prefixStyle = ArgParser.OptionPrefixStyle.JVM)): CommonInteropArguments(argParser) { val target by argParser.option(ArgType.String, description = "native target to compile to").default("host") diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index 3680535f575..c1d7c9e65b8 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -24,6 +24,9 @@ import org.jetbrains.kotlin.native.interop.gen.wasm.processIdlLib import org.jetbrains.kotlin.native.interop.indexer.* import org.jetbrains.kotlin.native.interop.tool.* import kotlinx.cli.ArgParser +import kotlinx.cli.ArgType +import kotlinx.cli.default +import kotlinx.cli.required import org.jetbrains.kotlin.konan.library.* import org.jetbrains.kotlin.konan.target.CompilerOutputKind import org.jetbrains.kotlin.konan.target.Distribution @@ -38,15 +41,34 @@ import java.lang.IllegalArgumentException import java.nio.file.* import java.util.* +data class InternalInteropOptions(val generated: String, val natives: String, val manifest: String? = null, + val cstubsName: String? = null) + fun main(args: Array) { - processCLib(args) + // Adding flavor option for interop plugin. + class FullCInteropArguments: CInteropArguments() { + val flavor by argParser.option(ArgType.Choice(listOf("jvm", "native", "wasm")), description = "Interop target") + .default("jvm") + val generated by argParser.option(ArgType.String, description = "place generated bindings to the directory") + .required() + val natives by argParser.option(ArgType.String, description = "where to put the built native files") + .required() + } + val arguments = FullCInteropArguments() + arguments.argParser.parse(args) + val flavorName = arguments.flavor + processCLib(flavorName, arguments, InternalInteropOptions(arguments.generated, arguments.natives)) } fun interop( flavor: String, args: Array, - additionalArgs: Map = mapOf() + additionalArgs: InternalInteropOptions ): Array? = when(flavor) { - "jvm", "native" -> processCLib(args, additionalArgs) + "jvm", "native" -> { + val cinteropArguments = CInteropArguments() + cinteropArguments.argParser.parse(args) + processCLib(flavor, cinteropArguments, additionalArgs) + } "wasm" -> processIdlLib(args, additionalArgs) else -> error("Unexpected flavor") } @@ -167,15 +189,13 @@ private fun findFilesByGlobs(roots: List, globs: List): Map, additionalArgs: Map = mapOf()): Array? { - val cinteropArguments = CInteropArguments() - cinteropArguments.argParser.parse(args) - val ktGenRoot = cinteropArguments.generated - val nativeLibsDir = cinteropArguments.natives - val flavorName = cinteropArguments.flavor +private fun processCLib(flavorName: String, cinteropArguments: CInteropArguments, + additionalArgs: InternalInteropOptions): Array? { + val ktGenRoot = additionalArgs.generated + val nativeLibsDir = additionalArgs.natives val flavor = KotlinPlatform.values().single { it.name.equals(flavorName, ignoreCase = true) } val defFile = cinteropArguments.def?.let { File(it) } - val manifestAddend = (additionalArgs["manifest"] as? String)?.let { File(it) } + val manifestAddend = additionalArgs.manifest?.let { File(it) } if (defFile == null && cinteropArguments.pkg == null) { cinteropArguments.argParser.printError("-def or -pkg should provided!") @@ -233,7 +253,7 @@ private fun processCLib(args: Array, additionalArgs: Map = else -> listOf() } - val libName = additionalArgs["cstubsName"] as? String ?: fqParts.joinToString("") + "stubs" + val libName = additionalArgs.cstubsName ?: fqParts.joinToString("") + "stubs" val tempFiles = TempFiles(libName, cinteropArguments.tempDir) diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/wasm/StubGenerator.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/wasm/StubGenerator.kt index 138dd8652f3..acff143f1c8 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/wasm/StubGenerator.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/wasm/StubGenerator.kt @@ -3,7 +3,7 @@ package org.jetbrains.kotlin.native.interop.gen.wasm import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.native.interop.gen.argsToCompiler import org.jetbrains.kotlin.native.interop.gen.wasm.idl.* -import kotlinx.cli.ArgParser +import org.jetbrains.kotlin.native.interop.gen.jvm.InternalInteropOptions import org.jetbrains.kotlin.native.interop.tool.JSInteropArguments fun kotlinHeader(packageName: String): String { @@ -391,12 +391,12 @@ fun generateJs(interfaces: List): String = const val idlMathPackage = "kotlinx.interop.wasm.math" const val idlDomPackage = "kotlinx.interop.wasm.dom" -fun processIdlLib(args: Array, additionalArgs: Map = mapOf()): Array { +fun processIdlLib(args: Array, additionalArgs: InternalInteropOptions): Array { val jsInteropArguments = JSInteropArguments() jsInteropArguments.argParser.parse(args) // TODO: Refactor me. - val ktGenRoot = File(jsInteropArguments.generated).mkdirs() - val nativeLibsDir = File(jsInteropArguments.natives).mkdirs() + val ktGenRoot = File(additionalArgs.generated).mkdirs() + val nativeLibsDir = File(additionalArgs.natives).mkdirs() val idl = when (jsInteropArguments.pkg) { idlMathPackage -> idlMath @@ -405,6 +405,6 @@ fun processIdlLib(args: Array, additionalArgs: Map = mapOf( } File(ktGenRoot, "kotlin_stubs.kt").writeText(generateKotlin(jsInteropArguments.pkg!!, idl)) File(nativeLibsDir, "js_stubs.js").writeText(generateJs(idl)) - File((additionalArgs["manifest"] as? String)!!).writeText("") // The manifest is currently unused for wasm. + File((additionalArgs.manifest)!!).writeText("") // The manifest is currently unused for wasm. return argsToCompiler(jsInteropArguments.staticLibrary.toTypedArray(), jsInteropArguments.libraryPath.toTypedArray()) } 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 6e46ab7e5b2..9a47514d97a 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 @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.cli.utilities import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.konan.target.PlatformManager +import org.jetbrains.kotlin.native.interop.gen.jvm.InternalInteropOptions import org.jetbrains.kotlin.native.interop.gen.jvm.interop import org.jetbrains.kotlin.native.interop.tool.* @@ -28,13 +29,8 @@ fun invokeInterop(flavor: String, args: Array): Array? { val buildDir = File("$outputFileName-build") val generatedDir = File(buildDir, "kotlin") - val nativesDir = File(buildDir, "natives") + val nativesDir = File(buildDir,"natives") val manifest = File(buildDir, "manifest.properties") - val additionalArgs = listOf( - "-generated", generatedDir.path, - "-natives", nativesDir.path, - "-flavor", flavor - ) val cstubsName ="cstubs" val libraries = arguments.library val repos = arguments.repo @@ -42,11 +38,11 @@ fun invokeInterop(flavor: String, args: Array): Array? { else (arguments as JSInteropArguments).target val target = PlatformManager().targetManager(targetRequest).target - val cinteropArgsToCompiler = interop(flavor, args + additionalArgs, - listOfNotNull( - "manifest" to manifest.path, - ("cstubsName" to cstubsName).takeIf { flavor == "native" } - ).toMap() + val cinteropArgsToCompiler = interop(flavor, args, + InternalInteropOptions(generatedDir.absolutePath, + nativesDir.absolutePath,manifest.path, + cstubsName.takeIf { flavor == "native" } + ) ) ?: return null // There is no need in compiler invocation if we're generating only metadata. val nativeStubs =