From 65dd79881884b2eb9fbfd06fa5b048239a4ec2ee Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Fri, 25 Dec 2020 11:55:10 +0100 Subject: [PATCH] [build][native support] intoduced new native support. --- buildSrc/build.gradle.kts | 10 + .../gradle/plugins/tools/NativePlugin.kt | 228 ++++++++++++++++++ kotlin-native/Interop/Indexer/build.gradle | 175 -------------- .../Interop/Indexer/build.gradle.kts | 194 +++++++++++++++ kotlin-native/Interop/Runtime/build.gradle | 88 ------- .../Interop/Runtime/build.gradle.kts | 93 +++++++ kotlin-native/backend.native/build.gradle | 7 +- .../kotlin/NativeInteropPlugin.groovy | 3 + .../main/kotlin/org/jetbrains/kotlin/Utils.kt | 32 +++ kotlin-native/dependencies/build.gradle | 17 +- kotlin-native/libclangext/build.gradle | 65 ----- kotlin-native/libclangext/build.gradle.kts | 56 +++++ .../llvmCoverageMappingC/build.gradle | 67 ----- .../llvmCoverageMappingC/build.gradle.kts | 69 ++++++ kotlin-native/llvmDebugInfoC/build.gradle | 58 ----- kotlin-native/llvmDebugInfoC/build.gradle.kts | 59 +++++ .../kotlin/konan/target/ClangArgs.kt | 6 + .../kotlin/konan/target/KonanProperties.kt | 4 +- .../kotlin/konan/util/DependencyDownloader.kt | 4 +- .../kotlin/konan/util/DependencyProcessor.kt | 12 +- 20 files changed, 778 insertions(+), 469 deletions(-) create mode 100644 buildSrc/src/main/kotlin/org/jetbrains/gradle/plugins/tools/NativePlugin.kt delete mode 100644 kotlin-native/Interop/Indexer/build.gradle create mode 100644 kotlin-native/Interop/Indexer/build.gradle.kts delete mode 100644 kotlin-native/Interop/Runtime/build.gradle create mode 100644 kotlin-native/Interop/Runtime/build.gradle.kts delete mode 100644 kotlin-native/libclangext/build.gradle create mode 100644 kotlin-native/libclangext/build.gradle.kts delete mode 100644 kotlin-native/llvmCoverageMappingC/build.gradle create mode 100644 kotlin-native/llvmCoverageMappingC/build.gradle.kts delete mode 100644 kotlin-native/llvmDebugInfoC/build.gradle create mode 100644 kotlin-native/llvmDebugInfoC/build.gradle.kts diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1092c65a35f..64b327da56f 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -247,5 +247,15 @@ gradlePlugin { id = "org.jetbrains.kotlin.konan" implementationClass = "org.jetbrains.kotlin.gradle.plugin.konan.KonanPlugin" } + + create("native") { + id = "native" + implementationClass = "org.jetbrains.gradle.plugins.tools.NativePlugin" + } + + create("native-interop-plugin") { + id = "native-interop-plugin" + implementationClass = "org.jetbrains.kotlin.NativeInteropPlugin" + } } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/org/jetbrains/gradle/plugins/tools/NativePlugin.kt b/buildSrc/src/main/kotlin/org/jetbrains/gradle/plugins/tools/NativePlugin.kt new file mode 100644 index 00000000000..3a2ea182e72 --- /dev/null +++ b/buildSrc/src/main/kotlin/org/jetbrains/gradle/plugins/tools/NativePlugin.kt @@ -0,0 +1,228 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.gradle.plugins.tools + +import org.gradle.api.DefaultTask +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.file.FileCollection +import org.gradle.api.plugins.BasePlugin +import org.gradle.api.tasks.* +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.withType +import org.gradle.language.base.plugins.LifecycleBasePlugin +import org.jetbrains.kotlin.konan.target.HostManager.Companion.hostIsMac +import org.jetbrains.kotlin.konan.target.HostManager.Companion.hostIsMingw +import java.io.File +import kotlin.collections.List +import kotlin.collections.MutableMap +import kotlin.collections.addAll +import kotlin.collections.drop +import kotlin.collections.first +import kotlin.collections.flatMap +import kotlin.collections.forEach +import kotlin.collections.listOf +import kotlin.collections.map +import kotlin.collections.mutableListOf +import kotlin.collections.mutableMapOf +import kotlin.collections.plusAssign +import kotlin.collections.set +import kotlin.collections.toTypedArray + +open class NativePlugin : Plugin { + override fun apply(project: Project) { + project.apply() + project.extensions.create("native", NativeToolsExtension::class.java, project) + } +} + +abstract class ToolExecutionTask : DefaultTask() { + @get:OutputFile + abstract var output: File + + @get:InputFiles + abstract var input: List + + @get:Input + abstract var cmd: String + + @get:Input + abstract var args: List + + @TaskAction + fun action() { + project.exec { + executable(cmd) + args(*this@ToolExecutionTask.args.toTypedArray()) + } + } +} + +class ToolPatternImpl(val extension: NativeToolsExtension, val output:String, vararg val input: String):ToolPattern { + val tool = mutableListOf() + val args = mutableListOf() + override fun ruleOut(): String = output + override fun ruleInFirst(): String = input.first() + override fun ruleInAll(): Array = arrayOf(*input) + + override fun flags(vararg args: String) { + this.args.addAll(args) + } + + override fun tool(vararg arg: String) { + tool.addAll(arg) + } + + override fun env(name: String) = emptyArray() + + fun configure(task: ToolExecutionTask, configureDepencies:Boolean) { + extension.cleanupfiles += output + task.input = input.map { + extension.project.file(it) + } + task.dependsOn(":kotlin-native:dependencies:update") + if (configureDepencies) + task.input.forEach { task.dependsOn(it.name) } + val file = extension.project.file(output) + file.parentFile.mkdirs() + task.output = file + task.cmd = tool.first() + task.args = listOf(*tool.drop(1).toTypedArray(), *args.toTypedArray()) + } +} + +open class SourceSet( + val sourceSets: SourceSets, + val name: String, + val initialDirectory: File = sourceSets.project.projectDir, + val initialSourceSet: SourceSet? = null, + val rule: Pair? = null +) { + var collection = sourceSets.project.objects.fileCollection() as FileCollection + fun file(path: String) { + collection = collection.plus(sourceSets.project.files("${initialDirectory.absolutePath}/$path")) + } + + fun dir(path: String) { + sourceSets.project.fileTree("${initialDirectory.absolutePath}/$path").files.forEach { + collection = collection.plus(sourceSets.project.files(it)) + } + } + + fun transform(suffixes: Pair): SourceSet { + return SourceSet( + sourceSets, + name, + sourceSets.project.file("${sourceSets.project.buildDir}/$name/${suffixes.first}_${suffixes.second}/"), + this, + suffixes + ) + } + + fun implicitTasks(): Array> { + rule ?: return emptyArray() + initialSourceSet?.implicitTasks() + return initialSourceSet!!.collection + .filter { !it.isDirectory() } + .filter { it.name.endsWith(rule.first) } + .map { it.relativeTo(initialSourceSet.initialDirectory) } + .map { it.path } + .map { it to (it.substring(0, it.lastIndexOf(rule.first)) + rule.second) } + .map { + file(it.second) + sourceSets.project.file("${initialSourceSet.initialDirectory.path}/${it.first}") to sourceSets.project.file("${initialDirectory.path}/${it.second}") + }.map { + sourceSets.project.tasks.register(it.second.name, ToolExecutionTask::class.java) { + val toolConfiguration = ToolPatternImpl(sourceSets.extension, it.second.path, it.first.path) + sourceSets.extension.toolPatterns[rule]!!.invoke(toolConfiguration) + toolConfiguration.configure(this, initialSourceSet.rule != null) + } + }.toTypedArray() + } +} + +class SourceSets(val project: Project, val extension: NativeToolsExtension, val sources: MutableMap) : + MutableMap by sources { + operator fun String.invoke(initialDirectory: File = project.projectDir, configuration: SourceSet.() -> Unit) { + sources[this] = SourceSet(this@SourceSets, this, initialDirectory).also { + configuration(it) + } + } +} + + +interface Environment { + operator fun String.invoke(vararg values: String) +} + +interface ToolPattern { + fun ruleOut(): String + fun ruleInFirst(): String + fun ruleInAll(): Array + fun flags(vararg args: String): Unit + fun tool(vararg arg: String): Unit + fun env(name: String): Array +} + + +typealias ToolPatternConfiguration = ToolPattern.() -> Unit +typealias EnvironmentConfiguration = Environment.() -> Unit + +class ToolConfigurationPatterns( + val extension: NativeToolsExtension, + val patterns: MutableMap, ToolPatternConfiguration> +) : MutableMap, ToolPatternConfiguration> by patterns { + operator fun Pair.invoke(configuration: ToolPatternConfiguration) { + patterns[this] = configuration + } +} + + +open class NativeToolsExtension(val project: Project) { + val sourceSets = SourceSets(project, this, mutableMapOf()) + val toolPatterns = ToolConfigurationPatterns(this, mutableMapOf, ToolPatternConfiguration>()) + val cleanupfiles = mutableListOf() + fun sourceSet(configuration: SourceSets.() -> Unit) { + sourceSets.configuration() + } + + var environmentConfiguration: EnvironmentConfiguration? = null + fun environment(configuration: EnvironmentConfiguration) { + environmentConfiguration = configuration + } + + fun suffixes(configuration: ToolConfigurationPatterns.() -> Unit) = toolPatterns.configuration() + + fun target(name: String, vararg objSet: SourceSet, configuration: ToolPatternConfiguration) { + project.tasks.withType().named(LifecycleBasePlugin.CLEAN_TASK_NAME).configure { + doLast { + delete(*this@NativeToolsExtension.cleanupfiles.toTypedArray()) + } + } + + sourceSets.project.tasks.create(name, ToolExecutionTask::class.java) { + objSet.forEach { + dependsOn(it.implicitTasks()) + } + val deps = objSet.flatMap { it.collection.files }.map { it.path } + val toolConfiguration = ToolPatternImpl(sourceSets.extension, "${project.buildDir.path}/$name", *deps.toTypedArray()) + toolConfiguration.configuration() + toolConfiguration.configure(this, false ) + } + } +} + + +fun solib(name: String) = when { + hostIsMingw -> "$name.dll" + hostIsMac -> "lib$name.dylib" + else -> "lib$name.so" +} + +fun lib(name:String) = when { + hostIsMingw -> "$name.lib" + else -> "lib$name.a" +} \ No newline at end of file diff --git a/kotlin-native/Interop/Indexer/build.gradle b/kotlin-native/Interop/Indexer/build.gradle deleted file mode 100644 index 97f0d7f8514..00000000000 --- a/kotlin-native/Interop/Indexer/build.gradle +++ /dev/null @@ -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 cflags = [ - "-I$llvmDir/include", - "-I${project(":kotlin-native:libclangext").projectDir.absolutePath}/src/main/include" -]*.toString() - -List 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 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' - } - } -} diff --git a/kotlin-native/Interop/Indexer/build.gradle.kts b/kotlin-native/Interop/Indexer/build.gradle.kts new file mode 100644 index 00000000000..8c84f87a824 --- /dev/null +++ b/kotlin-native/Interop/Indexer/build.gradle.kts @@ -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("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().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") + } + } +} diff --git a/kotlin-native/Interop/Runtime/build.gradle b/kotlin-native/Interop/Runtime/build.gradle deleted file mode 100644 index b9b3059d86c..00000000000 --- a/kotlin-native/Interop/Runtime/build.gradle +++ /dev/null @@ -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 diff --git a/kotlin-native/Interop/Runtime/build.gradle.kts b/kotlin-native/Interop/Runtime/build.gradle.kts new file mode 100644 index 00000000000..6132d96490a --- /dev/null +++ b/kotlin-native/Interop/Runtime/build.gradle.kts @@ -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().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("nativelibs") { + dependsOn(solib("callbacks")) + + from("$buildDir/") + into("$buildDir/nativelibs/") +} diff --git a/kotlin-native/backend.native/build.gradle b/kotlin-native/backend.native/build.gradle index 9c7c91f9f84..d639512d533 100644 --- a/kotlin-native/backend.native/build.gradle +++ b/kotlin-native/backend.native/build.gradle @@ -1,6 +1,7 @@ import org.jetbrains.kotlin.CopyCommonSources import org.jetbrains.kotlin.konan.target.HostManager import org.jetbrains.kotlin.* +import org.jetbrains.gradle.plugins.tools.* /* * Copyright 2010-2018 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. @@ -86,13 +87,13 @@ task renamePackage { kotlinNativeInterop { llvm { - dependsOn ":kotlin-native:llvmDebugInfoC:debugInfoStaticLibrary" - dependsOn ":kotlin-native:llvmCoverageMappingC:coverageMappingStaticLibrary" + dependsOn ":kotlin-native:llvmDebugInfoC:${NativePluginKt.lib("debugInfo")}" + dependsOn ":kotlin-native:llvmCoverageMappingC:${NativePluginKt.lib("coverageMapping")}" defFile 'llvm.def' if (!project.parent.convention.plugins.platformInfo.isWindows()) compilerOpts "-fPIC" compilerOpts "-I$llvmDir/include", "-I${rootProject.project(':kotlin-native:llvmDebugInfoC').projectDir}/src/main/include", "-I${rootProject.project(':kotlin-native:llvmCoverageMappingC').projectDir}/src/main/include" - linkerOpts "-L$llvmDir/lib", "-L${rootProject.project(':kotlin-native:llvmDebugInfoC').buildDir}/libs/debugInfo/static", "-L${rootProject.project(':kotlin-native:llvmCoverageMappingC').buildDir}/libs/coverageMapping/static" + linkerOpts "-L$llvmDir/lib", "-L${rootProject.project(':kotlin-native:llvmDebugInfoC').buildDir}", "-L${rootProject.project(':kotlin-native:llvmCoverageMappingC').buildDir}" } hash { // TODO: copy-pasted from ':common:compileHash' diff --git a/kotlin-native/build-tools/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy b/kotlin-native/build-tools/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy index 1ef8220f8d0..7b780e40184 100644 --- a/kotlin-native/build-tools/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy +++ b/kotlin-native/build-tools/src/main/groovy/org/jetbrains/kotlin/NativeInteropPlugin.groovy @@ -190,6 +190,7 @@ class NamedNativeInteropConfig implements Named { project.dependencies { add interopStubs.getCompileConfigurationName(), project(path: ':kotlin-native:Interop:Runtime') + add interopStubs.getCompileConfigurationName(), "org.jetbrains.kotlin:kotlin-stdlib:${project.bootstrapKotlinVersion}" } this.configuration.extendsFrom project.configurations[interopStubs.runtimeConfigurationName] @@ -198,6 +199,8 @@ class NamedNativeInteropConfig implements Named { genTask.configure { dependsOn ":kotlin-native:dependencies:update" + dependsOn ":kotlin-native:Interop:Indexer:nativelibs" + dependsOn ":kotlin-native:Interop:Runtime:nativelibs" classpath = project.configurations.interopStubGenerator main = "org.jetbrains.kotlin.native.interop.gen.jvm.MainKt" jvmArgs '-ea' diff --git a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt index 3995e9b83f4..882430430d8 100644 --- a/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt +++ b/kotlin-native/build-tools/src/main/kotlin/org/jetbrains/kotlin/Utils.kt @@ -7,6 +7,9 @@ package org.jetbrains.kotlin import org.gradle.api.Project import org.gradle.api.Task +import org.gradle.api.tasks.TaskState +import org.gradle.api.logging.LogLevel +import org.gradle.api.tasks.TaskCollection import org.jetbrains.kotlin.konan.properties.loadProperties import org.jetbrains.kotlin.konan.properties.propertyList import org.jetbrains.kotlin.konan.properties.saveProperties @@ -20,6 +23,35 @@ import java.util.Base64 import org.jetbrains.report.json.* import java.nio.file.Path import org.jetbrains.kotlin.konan.file.File as KFile +import org.gradle.nativeplatform.toolchain.internal.* +import org.gradle.nativeplatform.toolchain.plugins.ClangCompilerPlugin +import org.gradle.api.Incubating +import org.gradle.api.NamedDomainObjectFactory +import org.gradle.api.NonNullApi +import org.gradle.api.Plugin +import org.gradle.api.internal.file.FileResolver +import org.gradle.api.internal.plugins.PotentialPlugin +import org.gradle.internal.operations.BuildOperationExecutor +import org.gradle.internal.os.OperatingSystem +import org.gradle.internal.reflect.Instantiator +import org.gradle.internal.service.ServiceRegistry +import org.gradle.internal.work.WorkerLeaseService +import org.gradle.model.Defaults +import org.gradle.model.RuleSource +import org.gradle.nativeplatform.internal.CompilerOutputFileNamingSchemeFactory +import org.gradle.nativeplatform.platform.internal.NativePlatformInternal +import org.gradle.nativeplatform.plugins.NativeComponentPlugin +import org.gradle.nativeplatform.toolchain.Clang +import org.gradle.nativeplatform.toolchain.internal.clang.ClangToolChain +import org.gradle.nativeplatform.toolchain.internal.gcc.AbstractGccCompatibleToolChain +import org.gradle.nativeplatform.toolchain.internal.gcc.DefaultGccPlatformToolChain +import org.gradle.nativeplatform.toolchain.internal.gcc.metadata.SystemLibraryDiscovery +import org.gradle.nativeplatform.toolchain.internal.metadata.CompilerMetaDataProviderFactory +import org.gradle.nativeplatform.toolchain.internal.tools.CommandLineToolSearchResult +import org.gradle.nativeplatform.toolchain.internal.tools.GccCommandLineToolConfigurationInternal +import org.gradle.nativeplatform.toolchain.internal.tools.ToolSearchPath +import org.gradle.process.internal.ExecActionFactory +import java.io.ByteArrayOutputStream //region Project properties. diff --git a/kotlin-native/dependencies/build.gradle b/kotlin-native/dependencies/build.gradle index 55d22b9d92c..3d15eb2155e 100644 --- a/kotlin-native/dependencies/build.gradle +++ b/kotlin-native/dependencies/build.gradle @@ -40,8 +40,10 @@ class NativeDep extends DefaultTask { @TaskAction void downloadAndExtract() { - def downloader = new DependencyProcessor(baseOutDir, konanPropertiesLoader, baseUrl, false, ArchiveType.systemDefault) - downloader.showInfo = false + def downloader = new DependencyProcessor(baseOutDir, konanPropertiesLoader, baseUrl, false, ArchiveType.systemDefault, { url, currentBytes, totalBytes -> + print("\n$name Downloading dependency: $url (${currentBytes}/${totalBytes}). ") + }) + downloader.showInfo = project.logger.isEnabled(LogLevel.INFO) downloader.run() } } @@ -77,7 +79,6 @@ enum DependencyKind { def platformManager = rootProject.project(":kotlin-native").ext.platformManager - platformManager.enabled.each { target -> def loader = platformManager.loader(target) @@ -92,8 +93,10 @@ platformManager.enabled.each { target -> loader.dependencies, NativeDep.baseUrl, false, - ArchiveType.systemDefault - ) + ArchiveType.systemDefault, + { url, currentBytes, totalBytes -> + print("\n(no-task) Downloading dependency: $url (${currentBytes}/${totalBytes}). ") + }) DependencyKind.values().each { kind -> def dir = kind.getDirectory(loader) @@ -104,9 +107,11 @@ platformManager.enabled.each { target -> } } -task update(type: Copy) { +task update { dependsOn tasks.withType(NativeDep) + mustRunAfter(tasks.withType(NativeDep)) } +rootProject.project(":kotlin-native").ext.nativeDependencies = tasks.withType(NativeDep) task rmDotKonan(type: Delete) { def dir = System.getenv("KONAN_DATA_DIR") ?: "${System.getProperty("user.home")}/.konan" diff --git a/kotlin-native/libclangext/build.gradle b/kotlin-native/libclangext/build.gradle deleted file mode 100644 index ec786ff57e2..00000000000 --- a/kotlin-native/libclangext/build.gradle +++ /dev/null @@ -1,65 +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: "cpp" -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 -import org.jetbrains.kotlin.PlatformInfo -ext.isEnabled = PlatformInfo.isMac() - -model { - tasks.compileClangextStaticLibraryClangextCpp { - dependsOn ":kotlin-native:dependencies:update" - } - components { - clangext(NativeLibrarySpec) { - sources { - cpp { - source.srcDirs "src/main/cpp" - exportedHeaders.srcDirs "src/main/include" - } - } - binaries.withType(StaticLibraryBinarySpec) { binary -> - if (!project.parent.convention.plugins.platformInfo.isWindows()) - cppCompiler.args "-fPIC" - cppCompiler.args "--std=c++17", "-g", "-I${llvmDir}/include" - if (isEnabled) { - cppCompiler.args '-DLIBCLANGEXT_ENABLE=1' - } - } - binaries.withType(SharedLibraryBinarySpec) { binary -> - buildable = false - } - } - } - - toolChains { - clang(Clang) { - eachPlatform { - cppCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags) - } - } - } -} diff --git a/kotlin-native/libclangext/build.gradle.kts b/kotlin-native/libclangext/build.gradle.kts new file mode 100644 index 00000000000..f22ca577ddd --- /dev/null +++ b/kotlin-native/libclangext/build.gradle.kts @@ -0,0 +1,56 @@ +/* + * 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.kotlin.* +import org.jetbrains.kotlin.konan.target.Family.* +import java.io.ByteArrayOutputStream + +plugins{ + `native` +} +val libclangextEnabled = org.jetbrains.kotlin.konan.target.HostManager.hostIsMac +extra["isEnabled"] = libclangextEnabled + +native { + val isWindows = PlatformInfo.isWindows() + val obj = if (isWindows) "obj" else "o" + val cxxflags = mutableListOf("--std=c++17", "-g", + "-Isrc/main/include", + "-I${project.findProperty("llvmDir")}/include", + "-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1") + if (!org.jetbrains.kotlin.konan.target.HostManager.hostIsMingw) { + cxxflags += "-fPIC" + } + if (libclangextEnabled) { + cxxflags += "-DLIBCLANGEXT_ENABLE=1" + } + suffixes { + (".cpp" to ".$obj") { + tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray()) + flags(*cxxflags.toTypedArray(), "-c", "-o", ruleOut(), ruleInFirst()) + } + } + sourceSet { + "main" { + dir("src/main/cpp") + } + } + val objSet = sourceSets["main"]!!.transform(".cpp" to ".$obj") + target(lib("clangext"), objSet) { + tool(*platformManager.hostPlatform.clang.llvmAr("").toTypedArray()) + flags("-qv", ruleOut(), *ruleInAll()) + } +} \ No newline at end of file diff --git a/kotlin-native/llvmCoverageMappingC/build.gradle b/kotlin-native/llvmCoverageMappingC/build.gradle deleted file mode 100644 index 3a1d665461b..00000000000 --- a/kotlin-native/llvmCoverageMappingC/build.gradle +++ /dev/null @@ -1,67 +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: "cpp" -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.compileCoverageMappingStaticLibraryCoverageMappingCpp { - dependsOn ":kotlin-native:dependencies:update" - } - components { - coverageMapping(NativeLibrarySpec) { - sources.cpp.source.srcDirs "src/main/cpp" - - binaries.withType(StaticLibraryBinarySpec) { binary -> - if (!project.parent.convention.plugins.platformInfo.isWindows()) - cppCompiler.args "-fPIC" - cppCompiler.args "--std=c++17", "-I${llvmDir}/include", "-I${projectDir}/src/main/include" - if (isMac()) { - cppCompiler.args "-DKONAN_MACOS=1" - } else if (isWindows()) { - cppCompiler.args "-DKONAN_WINDOWS=1" - } else if (isLinux()) { - cppCompiler.args "-DKONAN_LINUX=1", "-D_GLIBCXX_USE_CXX11_ABI=0" - } else { - throw new Error("Unsupported host OS.") - } - linker.args "-L${llvmDir}/lib", "-lLLVMCore", "-lLLVMSupport" - } - binaries.withType(SharedLibraryBinarySpec) { binary -> - buildable = false - } - } - } - - toolChains { - clang(Clang) { - eachPlatform { - cppCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags) - } - } - } -} diff --git a/kotlin-native/llvmCoverageMappingC/build.gradle.kts b/kotlin-native/llvmCoverageMappingC/build.gradle.kts new file mode 100644 index 00000000000..25c2ecfcb2e --- /dev/null +++ b/kotlin-native/llvmCoverageMappingC/build.gradle.kts @@ -0,0 +1,69 @@ +/* + * 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.kotlin.* +import org.jetbrains.kotlin.konan.target.ClangArgs +import org.jetbrains.kotlin.konan.target.Family.* +import org.jetbrains.kotlin.konan.target.HostManager + +plugins { + `native` +} + +native { + val obj = if (HostManager.hostIsMingw) "obj" else "o" + val llvmDir = project.findProperty("llvmDir") + val host = rootProject.project(":kotlin-native").extra["hostName"] + val hostLibffiDir = rootProject.project(":kotlin-native").extra["${host}LibffiDir"] + val cxxflags = mutableListOf( + "--std=c++17", + "-I${llvmDir}/include", + "-Isrc/main/include" + ) + when (org.jetbrains.kotlin.konan.target.HostManager.host.family) { + LINUX -> { + cxxflags.addAll(listOf("-DKONAN_LINUX=1", "-D_GLIBCXX_USE_CXX11_ABI=0")) + } + MINGW -> { + cxxflags += "-DKONAN_WINDOWS=1" + } + OSX -> { + cxxflags += "-DKONAN_MACOS=1" + } + } + if (!HostManager.hostIsMingw) { + cxxflags += "-fPIC" + } + suffixes { + (".cpp" to ".$obj") { + tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray()) + flags(*cxxflags.toTypedArray(), "-c", "-o", ruleOut(), ruleInFirst()) + } + + } + sourceSet { + "main" { + dir("src/main/cpp") + } + } + val objSet = sourceSets["main"]!!.transform(".cpp" to ".$obj") + + target(lib("coverageMapping"), objSet) { + tool(*platformManager.hostPlatform.clang.llvmAr("").toTypedArray()) + flags("-qv", ruleOut(), *ruleInAll()) + } +} diff --git a/kotlin-native/llvmDebugInfoC/build.gradle b/kotlin-native/llvmDebugInfoC/build.gradle deleted file mode 100644 index bc9e1cf211d..00000000000 --- a/kotlin-native/llvmDebugInfoC/build.gradle +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2010-2019 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: "cpp" -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.compileDebugInfoStaticLibraryDebugInfoCpp { - dependsOn ":kotlin-native:dependencies:update" - } - components { - debugInfo(NativeLibrarySpec) { - sources.cpp.source.srcDirs "src/main/cpp" - - binaries.withType(StaticLibraryBinarySpec) { binary -> - if (!project.parent.convention.plugins.platformInfo.isWindows()) - cppCompiler.args "-fPIC" - cppCompiler.args "--std=c++17", "-I${llvmDir}/include", "-I${projectDir}/src/main/include" - linker.args "-L${llvmDir}/lib", "-lLLVMCore", "-lLLVMSupport" - } - binaries.withType(SharedLibraryBinarySpec) { binary -> - buildable = false - } - } - } - - toolChains { - clang(Clang) { - eachPlatform { - cppCompiler.withArguments(ClangArgs.&filterGradleNativeSoftwareFlags) - } - } - } -} diff --git a/kotlin-native/llvmDebugInfoC/build.gradle.kts b/kotlin-native/llvmDebugInfoC/build.gradle.kts new file mode 100644 index 00000000000..d6e6ff40894 --- /dev/null +++ b/kotlin-native/llvmDebugInfoC/build.gradle.kts @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2019 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.kotlin.* +import org.jetbrains.kotlin.* +import org.jetbrains.kotlin.konan.target.ClangArgs +import org.jetbrains.kotlin.konan.target.HostManager + +plugins { + `native` +} + +val llvmDir = project.findProperty("llvmDir") + +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 = mutableListOf( + "--std=c++17", + "-I${llvmDir}/include", + "-I${projectDir}/src/main/include" + ) + if (!HostManager.hostIsMingw) { + cxxflags += "-fPIC" + } + suffixes { + (".cpp" to ".$obj") { + tool(*platformManager.hostPlatform.clang.clangCXX("").toTypedArray()) + flags(*cxxflags.toTypedArray(), "-c", "-o", ruleOut(), ruleInFirst()) + } + + } + sourceSet { + "main" { + dir("src/main/cpp") + } + } + val objSet = sourceSets["main"]!!.transform(".cpp" to ".$obj") + + target(lib("debugInfo"), objSet) { + tool(*platformManager.hostPlatform.clang.llvmAr("").toTypedArray()) + flags("-qv", ruleOut(), *ruleInAll()) + } +} diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt index 6c161e8c0c6..b6ba0275c8e 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt @@ -226,10 +226,16 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con private val targetClangXXCmd = listOf("${absoluteLlvmHome}/bin/clang++") + clangArgs + private val targetArCmd + = listOf("${absoluteLlvmHome}/bin/llvm-ar") + + fun clangC(vararg userArgs: String) = targetClangCmd + userArgs.asList() fun clangCXX(vararg userArgs: String) = targetClangXXCmd + userArgs.asList() + fun llvmAr(vararg userArgs: String) = targetArCmd + userArgs.asList() + companion object { @JvmStatic fun filterGradleNativeSoftwareFlags(args: MutableList) { diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/KonanProperties.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/KonanProperties.kt index 00cc0f2d22c..2008075ba5b 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/KonanProperties.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/KonanProperties.kt @@ -80,7 +80,9 @@ abstract class KonanPropertiesLoader(override val target: KonanTarget, dependenciesRoot = File(baseDir), properties = this, archiveType = defaultArchiveTypeByHost(host) - ) + ){ url, currentBytes, totalBytes -> + print("\n(KonanProperies) Downloading dependency: $url (${currentBytes}/${totalBytes}). ") + } } } } diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt index 18cbb770a1e..524a1e0ab9c 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyDownloader.kt @@ -32,9 +32,9 @@ class DependencyDownloader( customProgressCallback: ProgressCallback? = null ) { - private val progressCallback = customProgressCallback ?: { url, currentBytes, totalBytes -> + private val progressCallback = customProgressCallback ?: TODO()/* { url, currentBytes, totalBytes -> print("\nDownloading dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ") - } + }*/ val executor = ExecutorCompletionService(Executors.newSingleThreadExecutor(object : ThreadFactory { override fun newThread(r: Runnable?): Thread { diff --git a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt index 93a566d9809..7ad40cd88cc 100644 --- a/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt +++ b/kotlin-native/shared/src/main/kotlin/org/jetbrains/kotlin/konan/util/DependencyProcessor.kt @@ -111,20 +111,23 @@ class DependencyProcessor(dependenciesRoot: File, properties: KonanPropertiesLoader, dependenciesUrl: String = properties.dependenciesUrl, keepUnstable:Boolean = true, - archiveType: ArchiveType = ArchiveType.systemDefault) : this( + archiveType: ArchiveType = ArchiveType.systemDefault, + customProgressCallback: ProgressCallback? = null) : this( dependenciesRoot, properties.properties, properties.dependencies, dependenciesUrl, keepUnstable = keepUnstable, - archiveType = archiveType) + archiveType = archiveType, + customProgressCallback = customProgressCallback) constructor(dependenciesRoot: File, properties: Properties, dependencies: List, dependenciesUrl: String = properties.dependenciesUrl, keepUnstable:Boolean = true, - archiveType: ArchiveType = ArchiveType.systemDefault) : this( + archiveType: ArchiveType = ArchiveType.systemDefault, + customProgressCallback: ProgressCallback? = null ) : this( dependenciesRoot, dependenciesUrl, dependencyToCandidates = properties.findCandidates(dependencies), @@ -132,7 +135,8 @@ class DependencyProcessor(dependenciesRoot: File, maxAttempts = properties.downloadingAttempts, attemptIntervalMs = properties.downloadingAttemptIntervalMs, keepUnstable = keepUnstable, - archiveType = archiveType) + archiveType = archiveType, + customProgressCallback = customProgressCallback) class DependencyFile(directory: File, fileName: String) {