Implement llvm and clang CLI helpers

This commit is contained in:
SvyatoslavScherbina
2020-09-22 11:40:22 +03:00
committed by GitHub
parent b4cd621ab1
commit 14eb397cf5
3 changed files with 66 additions and 0 deletions
+19
View File
@@ -267,3 +267,22 @@ konanc main.kt -Xtemporary-files-dir=<PATH> -o <OUTPUT_NAME>
```shell script
konanc main.kt -Xdisable-phases=BitcodeOptimization -Xoverride-clang-options=-c,-O2
```
## Running Clang the same way Kotlin/Native compiler does
Kotlin/Native compiler (including `cinterop` tool) has machinery that manages LLVM, Clang and native SDKs for supported targets
and runs bundled Clang with proper arguments.
To utilize this machinery, use `$dist/bin/run_konan clang $tool $target $arguments`, e.g.
```
$dist/bin/run_konan clang clang ios_arm64 1.c
```
will print and run the following command:
```
~/.konan/dependencies/clang-llvm-apple-8.0.0-darwin-macos/bin/clang \
-B/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin \
-fno-stack-protector -stdlib=libc++ -arch arm64 \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.5.sdk \
-miphoneos-version-min=9.0 1.c
```
The similar helper is available for LLVM tools, `$dist/bin/run_konan llvm $tool $arguments`.
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package org.jetbrains.kotlin.cli.utilities
import org.jetbrains.kotlin.konan.exec.Command
import org.jetbrains.kotlin.konan.target.PlatformManager
import org.jetbrains.kotlin.konan.util.KonanHomeProvider
fun runLlvmTool(args: Array<String>) {
val toolName = args[0]
val toolArguments = args.drop(1)
val platform = platformManager().hostPlatform
val llvmHome = platform.configurables.absoluteLlvmHome
val toolPath = "$llvmHome/bin/$toolName"
runCommand(toolPath, *toolArguments.toTypedArray())
}
fun runLlvmClangToolWithTarget(args: Array<String>) {
val toolName = args[0]
val targetName = args[1]
val toolArguments = args.drop(2)
val platformManager = platformManager()
val platform = platformManager.platform(platformManager.targetByName(targetName))
val llvmHome = platform.configurables.absoluteLlvmHome
val toolPath = "$llvmHome/bin/$toolName"
runCommand(toolPath, *platform.clang.clangArgs, *toolArguments.toTypedArray())
}
private fun platformManager() = PlatformManager(KonanHomeProvider.determineKonanHome())
private fun runCommand(vararg args: String) {
Command(*args)
.logWith { println(it()) }
.execute()
}
@@ -29,6 +29,10 @@ private fun mainImpl(args: Array<String>, konancMain: (Array<String>) -> Unit) {
defFileDependencies(utilityArgs)
"generatePlatformLibraries" ->
generatePlatformLibraries(utilityArgs)
"llvm" -> runLlvmTool(utilityArgs)
"clang" -> runLlvmClangToolWithTarget(utilityArgs)
else ->
error("Unexpected utility name")
}