Rewrite interop tools command line options to have help messages without crashes (#2672)
This commit is contained in:
@@ -6,106 +6,87 @@ package org.jetbrains.kotlin.cli.utilities
|
||||
|
||||
import org.jetbrains.kotlin.konan.file.File
|
||||
import org.jetbrains.kotlin.konan.target.PlatformManager
|
||||
import org.jetbrains.kotlin.konan.KonanAbiVersion
|
||||
import org.jetbrains.kotlin.konan.library.*
|
||||
import org.jetbrains.kotlin.konan.target.Distribution
|
||||
import org.jetbrains.kotlin.native.interop.gen.jvm.interop
|
||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
|
||||
private const val NODEFAULTLIBS = "-nodefaultlibs"
|
||||
private const val PURGE_USER_LIBS = "-Xpurge-user-libs"
|
||||
import org.jetbrains.kliopt.ArgParser
|
||||
import org.jetbrains.kotlin.native.interop.tool.*
|
||||
|
||||
// TODO: this function should eventually be eliminated from 'utilities'.
|
||||
// The interaction of interop and the compler should be streamlined.
|
||||
|
||||
fun invokeInterop(flavor: String, args: Array<String>): Array<String> {
|
||||
val cinteropArgFilter = listOf(NODEFAULTLIBS, PURGE_USER_LIBS)
|
||||
|
||||
var outputFileName = "nativelib"
|
||||
var targetRequest = "host"
|
||||
val libraries = mutableListOf<String>()
|
||||
val repos = mutableListOf<String>()
|
||||
var noDefaultLibs = false
|
||||
var purgeUserLibs = false
|
||||
var temporaryFilesDir = ""
|
||||
for (i in args.indices) {
|
||||
val arg = args[i]
|
||||
val nextArg = args.getOrNull(i + 1)
|
||||
if (arg.startsWith("-o"))
|
||||
outputFileName = nextArg ?: outputFileName
|
||||
if (arg == "-target")
|
||||
targetRequest = nextArg ?: targetRequest
|
||||
if (arg == "-library")
|
||||
libraries.addIfNotNull(nextArg)
|
||||
if (arg == "-r" || arg == "-repo")
|
||||
repos.addIfNotNull(nextArg)
|
||||
if (arg == NODEFAULTLIBS)
|
||||
noDefaultLibs = true
|
||||
if (arg == PURGE_USER_LIBS)
|
||||
purgeUserLibs = true
|
||||
if (arg == "-Xtemporary-files-dir")
|
||||
temporaryFilesDir = nextArg ?: ""
|
||||
}
|
||||
// The interaction of interop and the compler should be streamlined.
|
||||
|
||||
fun invokeInterop(flavor: String, args: Array<String>): Array<String>? {
|
||||
val argParser = ArgParser(if (flavor == "native") getCInteropArguments() else getCommonInteropArguments(),
|
||||
useDefaultHelpShortName = false)
|
||||
if (!argParser.parse(args))
|
||||
return null
|
||||
val outputFileName = argParser.get<String>("output")!!
|
||||
val noDefaultLibs = argParser.get<Boolean>(NODEFAULTLIBS)!!
|
||||
val purgeUserLibs = argParser.get<Boolean>(PURGE_USER_LIBS)!!
|
||||
val temporaryFilesDir = argParser.get<String>(TEMP_DIR)
|
||||
|
||||
val buildDir = File("$outputFileName-build")
|
||||
val generatedDir = File(buildDir, "kotlin")
|
||||
val nativesDir = File(buildDir, "natives")
|
||||
val cstubsName ="cstubs"
|
||||
val manifest = File(buildDir, "manifest.properties")
|
||||
val additionalArgs = listOf(
|
||||
"-generated", generatedDir.path,
|
||||
"-natives", nativesDir.path,
|
||||
"-flavor", flavor
|
||||
)
|
||||
val additionalProperties = mutableMapOf<String, Any>(
|
||||
"manifest" to manifest.path)
|
||||
val cstubsName ="cstubs"
|
||||
val libraries = argParser.getAll<String>("library") ?: listOf<String>()
|
||||
val repos = argParser.getAll<String>("repo") ?: listOf<String>()
|
||||
var targetName = "wasm32"
|
||||
|
||||
val target = PlatformManager().targetManager(targetRequest).target
|
||||
val resolver = defaultResolver(
|
||||
repos,
|
||||
libraries.filter { it.contains(File.separator) },
|
||||
target,
|
||||
Distribution()
|
||||
).libraryResolver()
|
||||
val allLibraries = resolver.resolveWithDependencies(
|
||||
libraries.toUnresolvedLibraries, noStdLib = true, noDefaultLibs = noDefaultLibs
|
||||
).getFullList()
|
||||
if (flavor == "native") {
|
||||
val targetRequest = argParser.get<String>("target")!!
|
||||
val target = PlatformManager().targetManager(targetRequest).target
|
||||
targetName = target.visibleName
|
||||
val resolver = defaultResolver(
|
||||
repos,
|
||||
libraries.filter { it.contains(File.separator) },
|
||||
target,
|
||||
Distribution()
|
||||
).libraryResolver()
|
||||
val allLibraries = resolver.resolveWithDependencies(
|
||||
libraries.toUnresolvedLibraries, noStdLib = true, noDefaultLibs = noDefaultLibs
|
||||
).getFullList()
|
||||
|
||||
val importArgs = allLibraries.flatMap { library ->
|
||||
// TODO: handle missing properties?
|
||||
library.packageFqName?.let { packageFqName ->
|
||||
val headerIds = library.includedHeaders
|
||||
val arg = "$packageFqName:${headerIds.joinToString(";")}"
|
||||
listOf("-import", arg)
|
||||
} ?: emptyList()
|
||||
val imports = allLibraries.map { library ->
|
||||
// TODO: handle missing properties?
|
||||
library.packageFqName?.let { packageFqName ->
|
||||
val headerIds = library.includedHeaders
|
||||
"$packageFqName:${headerIds.joinToString(";")}"
|
||||
}
|
||||
}.filterNotNull()
|
||||
additionalProperties.putAll(mapOf("cstubsname" to cstubsName, "import" to imports))
|
||||
}
|
||||
|
||||
val additionalArgs = listOf(
|
||||
"-generated", generatedDir.path,
|
||||
"-natives", nativesDir.path,
|
||||
"-cstubsname", cstubsName,
|
||||
"-manifest", manifest.path,
|
||||
"-flavor", flavor,
|
||||
"-temporaryFilesDir", temporaryFilesDir
|
||||
) + importArgs
|
||||
|
||||
val cinteropArgs = (additionalArgs + args.filter { it !in cinteropArgFilter }).toTypedArray()
|
||||
|
||||
val cinteropArgsToCompiler = interop(flavor, cinteropArgs) ?: emptyArray()
|
||||
val cinteropArgsToCompiler = interop(flavor, args + additionalArgs, additionalProperties) ?: return null
|
||||
|
||||
val nativeStubs =
|
||||
if (flavor == "wasm")
|
||||
arrayOf("-include-binary", File(nativesDir, "js_stubs.js").path)
|
||||
else
|
||||
arrayOf("-native-library",File(nativesDir, "$cstubsName.bc").path)
|
||||
arrayOf("-native-library", File(nativesDir, "$cstubsName.bc").path)
|
||||
|
||||
val konancArgs = arrayOf(
|
||||
generatedDir.path,
|
||||
"-produce", "library",
|
||||
"-o", outputFileName,
|
||||
"-target", target.visibleName,
|
||||
"-target", targetName,
|
||||
"-manifest", manifest.path,
|
||||
"-Xtemporary-files-dir=$temporaryFilesDir") +
|
||||
nativeStubs +
|
||||
cinteropArgsToCompiler +
|
||||
libraries.flatMap { listOf("-library", it) } +
|
||||
repos.flatMap { listOf("-repo", it) } +
|
||||
(if (noDefaultLibs) arrayOf(NODEFAULTLIBS) else emptyArray()) +
|
||||
(if (purgeUserLibs) arrayOf(PURGE_USER_LIBS) else emptyArray())
|
||||
(if (noDefaultLibs) arrayOf("-$NODEFAULTLIBS") else emptyArray()) +
|
||||
(if (purgeUserLibs) arrayOf("-$PURGE_USER_LIBS") else emptyArray())
|
||||
|
||||
return konancArgs
|
||||
}
|
||||
|
||||
@@ -16,11 +16,11 @@ fun main(args: Array<String>) {
|
||||
konancMain(utilityArgs)
|
||||
"cinterop" -> {
|
||||
val konancArgs = invokeInterop("native", utilityArgs)
|
||||
konancMain(konancArgs)
|
||||
konancArgs?.let { konancMain(it) }
|
||||
}
|
||||
"jsinterop" -> {
|
||||
val konancArgs = invokeInterop("wasm", utilityArgs)
|
||||
konancMain(konancArgs)
|
||||
konancArgs?.let { konancMain(it) }
|
||||
}
|
||||
"klib" ->
|
||||
klibMain(utilityArgs)
|
||||
|
||||
Reference in New Issue
Block a user