[build][native support] intoduced new native support.

This commit is contained in:
Vasily Levchenko
2020-12-25 11:55:10 +01:00
parent e031da6e70
commit 65dd798818
20 changed files with 778 additions and 469 deletions
-175
View File
@@ -1,175 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
buildscript {
ext.rootBuildDirectory = file('../..')
apply from: "$rootBuildDirectory/gradle/kotlinGradlePlugin.gradle"
dependencies {
classpath "org.jetbrains.kotlin:kotlin-stdlib:${project.bootstrapKotlinVersion}"
}
}
apply plugin: 'kotlin'
apply plugin: org.jetbrains.kotlin.NativeInteropPlugin
apply plugin: 'c'
apply plugin: 'cpp'
import org.jetbrains.kotlin.konan.target.ClangArgs
final Project libclangextProject = project(":kotlin-native:libclangext")
final String libclangextTask = libclangextProject.path + ":build"
File libclangextDir = new File(libclangextProject.buildDir, "libs/clangext/static")
final boolean libclangextIsEnabled = libclangextProject.isEnabled
final String libclang
if (isWindows()) {
libclang = "bin/libclang.dll"
} else {
libclang = "lib/${System.mapLibraryName("clang")}"
}
List<String> cflags = [
"-I$llvmDir/include",
"-I${project(":kotlin-native:libclangext").projectDir.absolutePath}/src/main/include"
]*.toString()
List<String> ldflags = ["$llvmDir/$libclang", "-L$libclangextDir.absolutePath", "-lclangext"]*.toString()
if (libclangextIsEnabled) {
assert(isMac())
ldflags.addAll(['-Wl,--no-demangle', '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names', '-Wl,-U,_futimens',
'-Wl,-U,__ZN4llvm7remarks11parseFormatENS_9StringRefE',
'-Wl,-U,__ZN4llvm7remarks22createRemarkSerializerENS0_6FormatENS0_14SerializerModeERNS_11raw_ostreamE',
'-Wl,-U,__ZN4llvm7remarks14YAMLSerializerC1ERNS_11raw_ostreamENS0_14UseStringTableE'])
List<String> llvmLibs = [
"clangAST", "clangASTMatchers", "clangAnalysis", "clangBasic", "clangDriver", "clangEdit",
"clangFrontend", "clangFrontendTool", "clangLex", "clangParse", "clangSema", "clangEdit",
"clangRewrite", "clangRewriteFrontend", "clangStaticAnalyzerFrontend",
"clangStaticAnalyzerCheckers", "clangStaticAnalyzerCore", "clangSerialization",
"clangToolingCore",
"clangTooling", "clangFormat", "LLVMTarget", "LLVMMC", "LLVMLinker", "LLVMTransformUtils",
"LLVMBitWriter", "LLVMBitReader", "LLVMAnalysis", "LLVMProfileData", "LLVMCore",
"LLVMSupport", "LLVMBinaryFormat", "LLVMDemangle"
].collect { "$llvmDir/lib/lib${it}.a".toString() }
ldflags.addAll(llvmLibs)
ldflags.addAll(['-lpthread', '-lz', '-lm', '-lcurses'])
}
model {
tasks.compileClangstubsSharedLibraryClangstubsC{
dependsOn ":kotlin-native:dependencies:update"
}
tasks.compileClangstubsSharedLibraryClangstubsCpp {
dependsOn ":kotlin-native:dependencies:update"
}
components {
clangstubs(NativeLibrarySpec) {
sources {
c.source.srcDir 'prebuilt/nativeInteropStubs/c'
cpp.source.srcDir 'src/nativeInteropStubs/cpp'
}
binaries.all {
cCompiler.args hostPlatform.clang.hostCompilerArgsForJni
cCompiler.args.addAll(cflags)
cppCompiler.args.add("-std=c++11")
}
binaries.withType(SharedLibraryBinarySpec) {
linker.args.addAll(ldflags)
}
}
}
toolChains {
clang(Clang) {
eachPlatform {
cppCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags)
cCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags)
}
}
}
}
sourceSets {
main {
kotlin {
srcDirs 'prebuilt/nativeInteropStubs/kotlin'
}
}
}
dependencies {
compile project(":kotlin-stdlib")
compile project(':kotlin-native:Interop:Runtime')
}
task nativelibs(type: Copy) {
dependsOn 'clangstubsSharedLibrary'
from "$buildDir/libs/clangstubs/shared/"
into "$buildDir/nativelibs/"
}
classes.dependsOn nativelibs
kotlinNativeInterop {
clang {
defFile 'clang.def'
compilerOpts cflags
linkerOpts ldflags
genTask.dependsOn libclangextTask
genTask.inputs.dir libclangextDir
}
}
compileKotlin {
kotlinOptions {
allWarningsAsErrors=true
kotlinOptions.freeCompilerArgs = ["-Xskip-prerelease-check"]
}
}
tasks.matching { it.name == 'linkClangstubsSharedLibrary' }.all {
it.dependsOn libclangextTask
it.inputs.dir libclangextDir
}
task updatePrebuilt {
dependsOn genClangInteropStubs
doLast {
copy {
from("$buildDir/nativeInteropStubs/clang/kotlin") {
include 'clang/clang.kt'
}
into 'prebuilt/nativeInteropStubs/kotlin'
}
copy {
from("$buildDir/interopTemp") {
include 'clangstubs.c'
}
into 'prebuilt/nativeInteropStubs/c'
}
}
}
@@ -0,0 +1,194 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.gradle.plugins.tools.lib
import org.jetbrains.gradle.plugins.tools.solib
import org.jetbrains.kotlin.*
import org.jetbrains.kotlin.konan.target.*
import org.jetbrains.kotlin.konan.target.ClangArgs
import org.jetbrains.kotlin.konan.target.Family.*
import org.jetbrains.kotlin.konan.target.HostManager.Companion.hostIsMac
plugins {
`kotlin`
`native-interop-plugin`
`native`
}
val libclangextProject = project(":kotlin-native:libclangext")
val libclangextTask = libclangextProject.path + ":build"
val libclangextDir = libclangextProject.buildDir
val libclangextIsEnabled = libclangextProject.findProperty("isEnabled")!! as Boolean
val llvmDir = project.findProperty("llvmDir")
val libclang =
if (HostManager.hostIsMingw) {
"bin/libclang.dll"
} else {
"lib/${System.mapLibraryName("clang")}"
}
val cflags = mutableListOf( "-I$llvmDir/include",
"-I${project(":kotlin-native:libclangext").projectDir.absolutePath}/src/main/include",
*platformManager.hostPlatform.clang.hostCompilerArgsForJni)
if (!HostManager.hostIsMingw) {
cflags += "-fPIC"
}
val ldflags = mutableListOf("$llvmDir/$libclang", "-L${libclangextDir.absolutePath}", "-lclangext")
if (libclangextIsEnabled) {
assert(HostManager.hostIsMac)
ldflags.addAll(listOf("-Wl,--no-demangle", "-Wl,-search_paths_first", "-Wl,-headerpad_max_install_names", "-Wl,-U,_futimens",
"-Wl,-U,__ZN4llvm7remarks11parseFormatENS_9StringRefE",
"-Wl,-U,__ZN4llvm7remarks22createRemarkSerializerENS0_6FormatENS0_14SerializerModeERNS_11raw_ostreamE",
"-Wl,-U,__ZN4llvm7remarks14YAMLSerializerC1ERNS_11raw_ostreamENS0_14UseStringTableE"))
val llvmLibs = listOf(
"clangAST", "clangASTMatchers", "clangAnalysis", "clangBasic", "clangDriver", "clangEdit",
"clangFrontend", "clangFrontendTool", "clangLex", "clangParse", "clangSema", "clangEdit",
"clangRewrite", "clangRewriteFrontend", "clangStaticAnalyzerFrontend",
"clangStaticAnalyzerCheckers", "clangStaticAnalyzerCore", "clangSerialization",
"clangToolingCore",
"clangTooling", "clangFormat", "LLVMTarget", "LLVMMC", "LLVMLinker", "LLVMTransformUtils",
"LLVMBitWriter", "LLVMBitReader", "LLVMAnalysis", "LLVMProfileData", "LLVMCore",
"LLVMSupport", "LLVMBinaryFormat", "LLVMDemangle"
).map { "$llvmDir/lib/lib${it}.a" }
ldflags.addAll(llvmLibs)
ldflags.addAll(listOf("-lpthread", "-lz", "-lm", "-lcurses"))
}
val solib = when{
HostManager.hostIsMingw -> "dll"
HostManager.hostIsMac -> "dylib"
else -> "so"
}
val lib = if (HostManager.hostIsMingw) "lib" else "a"
native {
val obj = if (HostManager.hostIsMingw) "obj" else "o"
val host = rootProject.project(":kotlin-native").extra["hostName"]
val hostLibffiDir = rootProject.project(":kotlin-native").extra["${host}LibffiDir"]
val cxxflags = listOf("-std=c++11", *cflags.toTypedArray())
suffixes {
(".c" to ".$obj") {
tool(*platformManager.hostPlatform.clang.clangC("").toTypedArray())
flags(*cflags.toTypedArray(),
"-c", "-o", ruleOut(), ruleInFirst())
}
(".cpp" to ".$obj") {
tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray())
flags(*cxxflags.toTypedArray(), "-c", "-o", ruleOut(), ruleInFirst())
}
}
sourceSet {
"main-c" {
dir("prebuilt/nativeInteropStubs/c")
}
"main-cpp" {
dir("src/nativeInteropStubs/cpp")
}
}
val objSet = arrayOf(sourceSets["main-c"]!!.transform(".c" to ".$obj"),
sourceSets["main-cpp"]!!.transform(".cpp" to ".$obj"))
target(solib("clangstubs"), *objSet) {
tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray())
flags(
"-shared",
"-o", ruleOut(), *ruleInAll(),
*ldflags.toTypedArray())
}
}
tasks.named(solib("clangstubs")).configure {
dependsOn(":kotlin-native:libclangext:${lib("clangext")}")
}
sourceSets {
"main" {
java {
srcDirs("prebuilt/nativeInteropStubs/kotlin")
}
kotlin{
target {
}
}
}
}
dependencies {
compile(project(":kotlin-stdlib"))
compile(project(":kotlin-native:Interop:Runtime"))
}
val nativelibs = project.tasks.create<Copy>("nativelibs") {
dependsOn(solib("clangstubs"))
from("$buildDir/")
into("$buildDir/nativelibs/")
}
kotlinNativeInterop {
this.create("clang") {
defFile("clang.def")
compilerOpts(cflags)
linkerOpts = ldflags
genTask.dependsOn(libclangextTask)
genTask.inputs.dir(libclangextDir)
}
}
val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs = listOf("-Xskip-prerelease-check")
}
}
tasks.matching { it.name == "linkClangstubsSharedLibrary" }.all {
dependsOn(libclangextTask)
inputs.dir(libclangextDir)
}
tasks.create("updatePrebuilt") {
dependsOn("genClangInteropStubs")
doLast {
copy {
from("$buildDir/nativeInteropStubs/clang/kotlin") {
include("clang/clang.kt")
}
into("prebuilt/nativeInteropStubs/kotlin")
}
copy {
from("$buildDir/interopTemp") {
include("clangstubs.c")
}
into("prebuilt/nativeInteropStubs/c")
}
}
}
@@ -1,88 +0,0 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
apply plugin: 'kotlin'
apply plugin: 'c'
buildscript {
ext.rootBuildDirectory = file('../..')
apply from: "$rootBuildDirectory/gradle/kotlinGradlePlugin.gradle"
dependencies {
classpath "org.jetbrains.kotlin:kotlin-stdlib:${project.bootstrapKotlinVersion}"
}
}
import org.jetbrains.kotlin.konan.target.ClangArgs
model {
tasks.compileCallbacksSharedLibraryCallbacksC {
dependsOn ":kotlin-native:dependencies:update"
}
components {
callbacks(NativeLibrarySpec) {
sources.c.source {
srcDir 'src/callbacks/c'
include '**/*.c'
}
binaries.all {
def host = rootProject.project(":kotlin-native").ext.hostName
def hostLibffiDir = rootProject.project(":kotlin-native").ext.get("${host}LibffiDir")
cCompiler.args hostPlatform.clang.hostCompilerArgsForJni
cCompiler.args "-I$hostLibffiDir/include"
linker.args "$hostLibffiDir/lib/libffi.a"
}
}
}
toolChains {
clang(Clang) {
eachPlatform {
cCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags)
}
}
}
}
dependencies {
compile project(":kotlin-native:utilities:basic-utils")
compile project(":kotlin-stdlib")
compile project(":kotlin-reflect")
}
sourceSets.main.kotlin.srcDirs += "src/jvm/kotlin"
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xuse-experimental=kotlin.ExperimentalUnsignedTypes', '-Xuse-experimental=kotlin.Experimental',
'-Xopt-in=kotlin.RequiresOptIn', "-XXLanguage:+InlineClasses", "-Xskip-prerelease-check"]
allWarningsAsErrors=true
}
}
task nativelibs(type: Copy) {
dependsOn 'callbacksSharedLibrary'
from "$buildDir/libs/callbacks/shared/"
into "$buildDir/nativelibs/"
}
classes.dependsOn nativelibs
@@ -0,0 +1,93 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.jetbrains.gradle.plugins.tools.lib
import org.jetbrains.gradle.plugins.tools.solib
import org.jetbrains.kotlin.*
import org.jetbrains.kotlin.konan.target.HostManager
import java.io.ByteArrayOutputStream
val kotlinVersion = project.bootstrapKotlinVersion
plugins {
`native`
`kotlin`
}
//apply plugin: 'c'
native {
val isWindows = PlatformInfo.isWindows()
val obj = if (isWindows) "obj" else "o"
val host = rootProject.project(":kotlin-native").extra["hostName"]
val hostLibffiDir = rootProject.project(":kotlin-native").extra["${host}LibffiDir"]
val cflags = mutableListOf("-I$hostLibffiDir/include",
*platformManager.hostPlatform.clang.hostCompilerArgsForJni)
if (!HostManager.hostIsMingw) {
cflags += "-fPIC"
}
suffixes {
(".c" to ".$obj") {
tool(*platformManager.hostPlatform.clang.clangC("").toTypedArray())
flags( *cflags.toTypedArray(), "-c", "-o", ruleOut(), ruleInFirst())
}
}
sourceSet {
"callbacks" {
dir("src/callbacks/c")
}
}
val objSet = sourceSets["callbacks"]!!.transform(".c" to ".$obj")
target(solib("callbacks"), objSet) {
tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray())
flags("-shared",
"-o",ruleOut(), *ruleInAll(),
"-L${project(":kotlin-native:libclangext").buildDir}",
"$hostLibffiDir/lib/libffi.a",
"-lclangext")
}
tasks.named(solib("callbacks")).configure {
dependsOn(":kotlin-native:libclangext:${lib("clangext")}")
}
}
dependencies {
implementation(project(":kotlin-native:utilities:basic-utils"))
implementation(project(":kotlin-stdlib"))
implementation(project(":kotlin-reflect"))
}
sourceSets.main.get().java.srcDir("src/jvm/kotlin")
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs = listOf("-Xuse-experimental=kotlin.ExperimentalUnsignedTypes",
"-Xuse-experimental=kotlin.Experimental",
"-Xopt-in=kotlin.RequiresOptIn",
"-XXLanguage:+InlineClasses",
"-Xskip-prerelease-check")
allWarningsAsErrors = true
}
}
val nativelibs = project.tasks.create<Copy>("nativelibs") {
dependsOn(solib("callbacks"))
from("$buildDir/")
into("$buildDir/nativelibs/")
}