[build][kotlin-native] native compiler embeddable

This commit is contained in:
Vasily Levchenko
2021-07-02 06:36:23 +02:00
committed by Space
parent fa2e787f76
commit 1eb951749d
6 changed files with 206 additions and 0 deletions
@@ -0,0 +1,89 @@
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget.MACOS_ARM64
import org.jetbrains.kotlin.kotlinNativeDist
plugins {
kotlin("jvm")
}
val testCompilationClasspath by configurations.creating
val testCompilerClasspath by configurations.creating {
isCanBeConsumed = false
extendsFrom(configurations["runtimeElements"])
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
}
}
repositories{
mavenCentral()
}
val kotlinNativeEmbedded by configurations.creating
val testPlugin by configurations.creating
val testPluginRuntime by configurations.creating
fun DependencyHandlerScope.testPluginRuntime(any: Any) {
val notation = any as? String ?: return add(testPluginRuntime.name, any){}
val (group, artifact, version) = notation.split(":")
val platformName = HostManager.host.name
val gradlePlatformName = platformName.replace("_", "")
return add(testPluginRuntime.name, "$group:$artifact-$gradlePlatformName:$version") {
isTransitive = false
attributes {
attribute(Attribute.of("artifactType", String::class.java), "org.jetbrains.kotlin.klib")
attribute(Attribute.of("org.gradle.status", String::class.java), "release")
attribute(Attribute.of("org.jetbrains.kotlin.native.target", String::class.java), platformName)
attribute(Attribute.of("org.jetbrains.kotlin.platform.type", String::class.java), "native")
attribute(Usage.USAGE_ATTRIBUTE, objects.named("kotlin-api"))
}
}
}
dependencies {
kotlinNativeEmbedded(project(":kotlin-native:Interop:Runtime"))
kotlinNativeEmbedded(project(":kotlin-native:Interop:Indexer"))
kotlinNativeEmbedded(project(":kotlin-native:Interop:StubGenerator"))
kotlinNativeEmbedded(project(":kotlin-native:Interop:Skia"))
kotlinNativeEmbedded(project(":kotlin-native:backend.native"))
kotlinNativeEmbedded(project(":kotlin-native:utilities:cli-runner"))
kotlinNativeEmbedded(project(":kotlin-native:utilities:basic-utils"))
kotlinNativeEmbedded(project(":kotlin-native:klib"))
kotlinNativeEmbedded(project(":kotlin-native:endorsedLibraries:kotlinx.cli", "jvmRuntimeElements"))
kotlinNativeEmbedded(project(":kotlin-compiler", configuration = "runtimeJar"))
testImplementation(commonDep("junit:junit"))
testImplementation(project(":kotlin-test:kotlin-test-junit"))
}
val compiler = embeddableCompiler("kotlin-native-compiler-embeddable") {
from(kotlinNativeEmbedded)
/**
* this jar distributed through kotlin-native distribution, but not with maven.
*/
archiveVersion.set("")
mergeServiceFiles()
}
val runtimeJar = runtimeJar(compiler) {
exclude("com/sun/jna/**")
mergeServiceFiles()
}
kotlin.sourceSets["test"].kotlin.srcDir("tests/kotlin")
projectTest {
/**
* It's expected that test should be executed on CI, but currently this project under `kotlin.native.enabled`
*/
dependsOn(runtimeJar)
val runtimeJarPathProvider = project.provider { runtimeJar.get().outputs.files.asPath }
doFirst {
systemProperty("compilerClasspath", runtimeJarPathProvider.get())
systemProperty("kotlin.native.home", kotlinNativeDist)
}
}
@@ -0,0 +1,14 @@
package serialization
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
data class Data(val number: Int, val english: String, val germany:String)
fun main() {
Data(42, "forty two", "zweiundvierzig").let {
println(Json.encodeToString(it))
}
}
@@ -0,0 +1,21 @@
/*
* Copyright 2010-2015 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.
*/
package smoke
fun main(args: Array<String>) {
println(args)
}
@@ -0,0 +1,79 @@
/*
* 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.kotlin.native.compiler.embeddable
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import kotlin.test.assertEquals
private val COMPILER_CLASS_FQN = "org.jetbrains.kotlin.cli.bc.K2Native"
class CompilerSmokeTest {
private val _workingDir: TemporaryFolder = TemporaryFolder()
@Rule
fun getWorkingDir(): TemporaryFolder = _workingDir
private val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java")
private val compilerClasspath: List<File> by lazy {
filesFromProp("compilerClasspath", "kotlin-native-compiler-embeddable.jar")
}
private fun filesFromProp(propName: String, vararg defaultPaths: String): List<File> =
(System.getProperty(propName)?.split(File.pathSeparator) ?: defaultPaths.asList()).map {
File(it).takeIf(File::exists)
?: throw FileNotFoundException("cannot find ($it)")
}
@Test
fun testSmoke() {
val (out, code) = runCompiler("-e", "smoke.main", File("testData/projects/smoke/Smoke.kt").absolutePath)
assertEquals(0, code, "compilation failed: $out\n")
}
private fun createProcess(vararg cmd: String, projectDir: File): Process {
val builder = ProcessBuilder(*cmd)
builder.directory(projectDir)
builder.redirectErrorStream(true)
return builder.start()
}
private fun runCompiler(vararg arguments: String): Pair<String, Int> {
val cmd = listOf(
javaExecutable.absolutePath,
"-Djava.awt.headless=true",
"-Dkotlin.native.home=${System.getProperty("kotlin.native.home")}",
"-cp",
compilerClasspath.joinToString(File.pathSeparator),
COMPILER_CLASS_FQN
) + arguments
val proc = createProcess(*cmd.toTypedArray(), projectDir = _workingDir.root)
return readOutput(proc)
}
private fun readOutput(process: Process): Pair<String, Int> {
fun InputStream.readFully(): String {
val text = reader().readText()
close()
return text
}
val stdout = process.inputStream!!.readFully()
System.out.println(stdout)
val stderr = process.errorStream!!.readFully()
System.err.println(stderr)
val result = process.waitFor()
return stdout to result
}
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.isAtLeast
import org.jetbrains.kotlin.gradle.tasks.CacheBuilder
import org.jetbrains.kotlin.gradle.utils.NativeCompilerDownloader
import org.jetbrains.kotlin.konan.CompilerVersion
import org.jetbrains.kotlin.konan.file.File
import org.jetbrains.kotlin.konan.properties.resolvablePropertyString
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
+2
View File
@@ -641,4 +641,6 @@ if (buildProperties.isKotlinNativeEnabled) {
include ':kotlin-native:platformLibs'
include ':kotlin-native:libclangext'
include ':kotlin-native:backend.native:tests'
include ":kotlin-native-compiler-embeddable"
project(":kotlin-native-compiler-embeddable").projectDir = "$rootDir/kotlin-native/prepare/kotlin-native-embeddable-compiler" as File
}