Made cinterop command line options consistent (#3972)

This commit is contained in:
LepilkinaElena
2020-03-19 15:56:50 +03:00
committed by GitHub
parent 95daf2258c
commit f29b52764a
4 changed files with 44 additions and 34 deletions
@@ -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")
@@ -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<String>) {
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<String>,
additionalArgs: Map<String, Any> = mapOf()
additionalArgs: InternalInteropOptions
): Array<String>? = 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<Path>, globs: List<String>): Map<Path,
return relativeToRoot
}
private fun processCLib(args: Array<String>, additionalArgs: Map<String, Any> = mapOf()): Array<String>? {
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<String>? {
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<String>, additionalArgs: Map<String, Any> =
else -> listOf()
}
val libName = additionalArgs["cstubsName"] as? String ?: fqParts.joinToString("") + "stubs"
val libName = additionalArgs.cstubsName ?: fqParts.joinToString("") + "stubs"
val tempFiles = TempFiles(libName, cinteropArguments.tempDir)
@@ -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<Interface>): String =
const val idlMathPackage = "kotlinx.interop.wasm.math"
const val idlDomPackage = "kotlinx.interop.wasm.dom"
fun processIdlLib(args: Array<String>, additionalArgs: Map<String, Any> = mapOf()): Array<String> {
fun processIdlLib(args: Array<String>, additionalArgs: InternalInteropOptions): Array<String> {
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<String>, additionalArgs: Map<String, Any> = 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())
}
@@ -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<String>): Array<String>? {
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<String>): Array<String>? {
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 =