[gradle] Added a bunch of Gradle IT on K/N incremental compilation

This commit is contained in:
Igor Chevdar
2023-07-07 14:01:31 +03:00
committed by Space Team
parent 4e66881549
commit 40e8552a8d
15 changed files with 287 additions and 1 deletions
@@ -0,0 +1,141 @@
/*
* Copyright 2010-2023 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.gradle.native
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.NativeCacheKind
import org.jetbrains.kotlin.gradle.testbase.*
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.junit.jupiter.api.DisplayName
import java.nio.file.Path
import kotlin.io.path.appendText
import kotlin.io.path.writeText
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
@DisplayName("Tests for K/N incremental compilation")
@NativeGradlePluginTests
class NativeIncrementalCompilationIT : KGPBaseTest() {
override val defaultBuildOptions = super.defaultBuildOptions.copy(
nativeOptions = BuildOptions.NativeOptions(
cacheKind = NativeCacheKind.STATIC,
incremental = true
)
)
@DisplayName("Smoke test")
@GradleTest
fun checkIncrementalCacheIsCreated(gradleVersion: GradleVersion) {
nativeProject("native-incremental-simple", gradleVersion) {
build("linkDebugExecutableHost") {
assertDirectoryExists(
getFileCache("native-incremental-simple", "src/hostMain/kotlin/main.kt", "")
)
}
}
}
@DisplayName("IC works after compilation error (test 1)")
@GradleTest
fun compilationError1(gradleVersion: GradleVersion) {
nativeProject("native-incremental-multifile", gradleVersion) {
var mainKtCacheModified = 0L
var fooKtCacheModified = 0L
val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt", "")
val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt", "")
build("linkDebugExecutableHost") {
assertDirectoryExists(mainKtCache)
assertDirectoryExists(fooKtCache)
mainKtCacheModified = mainKtCache.toFile().lastModified()
fooKtCacheModified = fooKtCache.toFile().lastModified()
}
val fooKt = kotlinSourcesDir("hostMain").resolve("foo.kt")
fooKt.writeText("fun foo(): Int = \"zzz\"")
buildAndFail("linkDebugExecutableHost") {
assertTasksFailed(":compileKotlinHost")
}
fooKt.writeText("fun foo(): Int = 42")
build("linkDebugExecutableHost") {
assertDirectoryExists(mainKtCache)
assertDirectoryExists(fooKtCache)
assertEquals(mainKtCacheModified, mainKtCache.toFile().lastModified())
assertEquals(fooKtCacheModified, fooKtCache.toFile().lastModified())
}
}
}
@DisplayName("IC works after compilation error (test 2)")
@GradleTest
fun compilationError2(gradleVersion: GradleVersion) {
nativeProject("native-incremental-multifile", gradleVersion) {
var mainKtCacheModified = 0L
var fooKtCacheModified = 0L
val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt", "")
val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt", "")
build("linkDebugExecutableHost") {
assertDirectoryExists(mainKtCache)
assertDirectoryExists(fooKtCache)
mainKtCacheModified = mainKtCache.toFile().lastModified()
fooKtCacheModified = fooKtCache.toFile().lastModified()
}
val fooKt = kotlinSourcesDir("hostMain").resolve("foo.kt")
fooKt.writeText("fun foo(): Int = \"zzz\"")
buildAndFail("linkDebugExecutableHost") {
assertTasksFailed(":compileKotlinHost")
}
fooKt.writeText("fun foo(): String = \"zzz\"")
build("linkDebugExecutableHost") {
assertDirectoryExists(mainKtCache)
assertDirectoryExists(fooKtCache)
assertNotEquals(mainKtCacheModified, mainKtCache.toFile().lastModified())
assertNotEquals(fooKtCacheModified, fooKtCache.toFile().lastModified())
}
}
}
@DisplayName("Check dependencies on project level")
@GradleTest
fun inProjectDependencies(gradleVersion: GradleVersion) {
nativeProject("native-incremental-multi-project", gradleVersion, configureSubProjects = true) {
var fooKtCacheModified = 0L
var barKtCacheModified = 0L
var mainKtCacheModified = 0L
val fooKtCache = getFileCache("program", "MultiProject:library", "library/src/hostMain/kotlin/foo.kt", "")
val barKtCache = getFileCache("program", "MultiProject:program", "program/src/hostMain/kotlin/bar.kt", "")
val mainKtCache = getFileCache("program", "MultiProject:program", "program/src/hostMain/kotlin/main.kt", "")
build("linkDebugExecutableHost") {
assertDirectoryExists(fooKtCache)
assertDirectoryExists(barKtCache)
assertDirectoryExists(mainKtCache)
fooKtCacheModified = fooKtCache.toFile().lastModified()
barKtCacheModified = barKtCache.toFile().lastModified()
mainKtCacheModified = mainKtCache.toFile().lastModified()
}
val fooKt = projectPath.resolve("library/src/hostMain/kotlin").resolve("foo.kt")
fooKt.writeText("fun foo(): Int = 41")
build("linkDebugExecutableHost") {
assertDirectoryExists(fooKtCache)
assertDirectoryExists(barKtCache)
assertDirectoryExists(mainKtCache)
assertNotEquals(fooKtCacheModified, fooKtCache.toFile().lastModified())
assertEquals(barKtCacheModified, barKtCache.toFile().lastModified())
assertNotEquals(mainKtCacheModified, mainKtCache.toFile().lastModified())
}
}
}
}
@@ -93,6 +93,7 @@ data class BuildOptions(
val useXcodeMessageStyle: Boolean? = null,
val version: String? = null,
val cacheOrchestration: String? = null,
val incremental: Boolean? = null,
)
fun toArguments(
@@ -263,6 +264,9 @@ data class BuildOptions(
nativeOptions.cacheOrchestration?.let {
arguments.add("-Pkotlin.native.cacheOrchestration=${it}")
}
nativeOptions.incremental?.let {
arguments.add("-Pkotlin.incremental.native=${it}")
}
}
}
@@ -5,9 +5,11 @@
package org.jetbrains.kotlin.gradle.testbase
import org.jetbrains.kotlin.gradle.dsl.NativeCacheKind
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.presetName
import java.nio.file.Path
import java.util.*
val DEFAULT_CURRENT_PLATFORM_TARGET_NAME_POSTFIX = HostManager.host.presetName.lowercase(Locale.getDefault())
@@ -88,4 +90,33 @@ internal object MPPNativeTargets {
}
val supported = listOf("linux64", "macos64", "mingw64").filter { !unsupported.contains(it) }
}
}
fun computeCacheDirName(
testTarget: KonanTarget,
cacheKind: String,
debuggable: Boolean,
partialLinkageEnabled: Boolean
) = "$testTarget${if (debuggable) "-g" else ""}$cacheKind${if (partialLinkageEnabled) "-pl" else ""}"
fun TestProject.getFileCache(fileProjectName: String, fileRelativePath: String, fqName: String) =
getFileCache("", fileProjectName, fileRelativePath, fqName)
fun TestProject.getFileCache(
executableProjectName: String,
fileProjectName: String,
fileRelativePath: String,
fqName: String,
): Path {
val cacheFlavor = computeCacheDirName(HostManager.host, NativeCacheKind.STATIC.name, true, true)
val libCacheDir = getICCacheDir(executableProjectName).resolve(cacheFlavor).resolve("$fileProjectName-per-file-cache")
val fileId = cacheFileId(fqName, projectPath.resolve(fileRelativePath).toFile().canonicalPath)
return libCacheDir.resolve(fileId)
}
private fun TestProject.getICCacheDir(projectName: String = "") =
(if (projectName == "") projectPath else projectPath.resolve(projectName))
.resolve("build/kotlin-native-ic-cache")
private fun cacheFileId(fqName: String, filePath: String) =
"${if (fqName == "") "ROOT" else fqName}.${filePath.hashCode().toString(Character.MAX_RADIX)}"
@@ -0,0 +1,14 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
allprojects {
repositories {
mavenCentral()
mavenLocal()
}
}
kotlin {
<SingleNativeTarget>("host")
}
@@ -0,0 +1,12 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
<SingleNativeTarget>("host")
}
@@ -0,0 +1,26 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
<SingleNativeTarget>("host") {
binaries {
executable {
entryPoint = "main"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":library"))
}
}
}
}
@@ -0,0 +1,3 @@
rootProject.name = "MultiProject"
include(":library")
include(":program")
@@ -0,0 +1,18 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
<SingleNativeTarget>("host") {
binaries {
executable {
entryPoint = "main"
}
}
}
}
@@ -0,0 +1,18 @@
plugins {
id("org.jetbrains.kotlin.multiplatform")
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
<SingleNativeTarget>("host") {
binaries {
executable {
entryPoint = "main"
}
}
}
}
@@ -0,0 +1,8 @@
/*
* Copyright 2010-2022 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.
*/
fun main() {
println("Hello, Kotlin/Native!")
}