From 3f43f903811fad09ad308920c797ba8870bbfea3 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Thu, 26 Sep 2024 16:51:43 +0200 Subject: [PATCH] KTOR-7501 Use target hierarchy DSL --- build.gradle.kts | 6 +- buildSrc/src/main/kotlin/JsConfig.kt | 56 ++-- buildSrc/src/main/kotlin/TargetsConfig.kt | 362 ++++------------------ buildSrc/src/main/kotlin/WasmConfig.kt | 42 +-- ktor-network/build.gradle.kts | 21 +- 5 files changed, 101 insertions(+), 386 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 087d2b042f9..78e5bb59a79 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -108,7 +108,7 @@ doctor { enableTestCaching = false } -allprojects { +subprojects { group = "io.ktor" version = configuredVersion extra["hostManager"] = HostManager() @@ -116,7 +116,7 @@ allprojects { setupTrainForSubproject() val nonDefaultProjectStructure: List by rootProject.extra - if (nonDefaultProjectStructure.contains(project.name)) return@allprojects + if (nonDefaultProjectStructure.contains(project.name)) return@subprojects apply(plugin = "kotlin-multiplatform") apply(plugin = "atomicfu-conventions") @@ -138,9 +138,7 @@ allprojects { if (!skipPublish.contains(project.name)) { configurePublication() } -} -subprojects { configureCodestyle() } diff --git a/buildSrc/src/main/kotlin/JsConfig.kt b/buildSrc/src/main/kotlin/JsConfig.kt index 0784f8b33fa..0cf581fd03c 100644 --- a/buildSrc/src/main/kotlin/JsConfig.kt +++ b/buildSrc/src/main/kotlin/JsConfig.kt @@ -4,14 +4,21 @@ import internal.* import org.gradle.api.* +import org.gradle.internal.extensions.stdlib.* import org.gradle.kotlin.dsl.* -import org.jetbrains.kotlin.gradle.targets.js.ir.* +import org.jetbrains.kotlin.gradle.targets.js.dsl.* import java.io.* +import kotlin.toString fun Project.configureJs() { - configureJsTasks() - kotlin { + js { + if (project.targetIsEnabled("js.nodeJs")) nodejs { useMochaForTests() } + if (project.targetIsEnabled("js.browser")) browser { useKarmaForTests() } + + binaries.library() + } + sourceSets { jsTest { dependencies { @@ -21,40 +28,31 @@ fun Project.configureJs() { } } - configureJsTestTasks() + configureJsTestTasks(target = "js") } -private fun Project.configureJsTasks() { - kotlin { - js { - if (project.targetIsEnabled("js.nodeJs")) { - nodejs { - testTask { - useMocha { - timeout = "10000" - } - } - } - } - - (this as KotlinJsIrTarget).whenBrowserConfigured { - testTask { - useKarma { - useChromeHeadless() - useConfigDirectory(File(project.rootProject.projectDir, "karma")) - } - } - } +internal fun KotlinJsSubTargetDsl.useMochaForTests() { + testTask { + useMocha { + timeout = "10000" + } + } +} - binaries.library() +internal fun KotlinJsSubTargetDsl.useKarmaForTests() { + testTask { + useKarma { + useChromeHeadless() + useConfigDirectory(File(project.rootProject.projectDir, "karma")) } } } -private fun Project.configureJsTestTasks() { +internal fun Project.configureJsTestTasks(target: String) { val shouldRunJsBrowserTest = !hasProperty("teamcity") || hasProperty("enable-js-tests") if (shouldRunJsBrowserTest) return - tasks.maybeNamed("cleanJsBrowserTest") { onlyIf { false } } - tasks.maybeNamed("jsBrowserTest") { onlyIf { false } } + val capitalizedTarget = target.replaceFirstChar { it.titlecase() } + tasks.maybeNamed("clean${capitalizedTarget}BrowserTest") { onlyIf { false } } + tasks.maybeNamed("${target}BrowserTest") { onlyIf { false } } } diff --git a/buildSrc/src/main/kotlin/TargetsConfig.kt b/buildSrc/src/main/kotlin/TargetsConfig.kt index 03f82af0249..9921bbb07d5 100644 --- a/buildSrc/src/main/kotlin/TargetsConfig.kt +++ b/buildSrc/src/main/kotlin/TargetsConfig.kt @@ -8,7 +8,7 @@ import org.jetbrains.kotlin.gradle.* import org.jetbrains.kotlin.gradle.plugin.* import java.io.* -val Project.files: Array get() = project.projectDir.listFiles() ?: emptyArray() +private val Project.files: Array get() = project.projectDir.listFiles() ?: emptyArray() val Project.hasCommon: Boolean get() = files.any { it.name == "common" } val Project.hasJvmAndPosix: Boolean get() = hasCommon || files.any { it.name == "jvmAndPosix" } val Project.hasJvmAndNix: Boolean get() = hasCommon || files.any { it.name == "jvmAndNix" } @@ -22,329 +22,89 @@ val Project.hasJsAndWasmShared: Boolean get() = files.any { it.name == "jsAndWas val Project.hasJs: Boolean get() = hasCommon || files.any { it.name == "js" } || hasJsAndWasmShared val Project.hasWasm: Boolean get() = hasCommon || files.any { it.name == "wasmJs" } || hasJsAndWasmShared val Project.hasJvm: Boolean get() = hasCommon || hasJvmAndNix || hasJvmAndPosix || files.any { it.name == "jvm" } + +val Project.hasExplicitNative: Boolean + get() = hasNix || hasPosix || hasLinux || hasDarwin || hasDesktop || hasWindows val Project.hasNative: Boolean - get() = hasCommon || hasNix || hasPosix || hasLinux || hasDarwin || hasDesktop || hasWindows + get() = hasCommon || hasExplicitNative fun Project.configureTargets() { - configureCommon() - if (hasJvm) configureJvm() - kotlin { - if (hasJs) { - js { - if (project.targetIsEnabled("js.nodeJs")) nodejs() - if (project.targetIsEnabled("js.browser")) browser() - } - - configureJs() - } - - if (hasWasm) { - @OptIn(ExperimentalWasmDsl::class) - wasmJs { - nodejs() - if (project.targetIsEnabled("wasmJs.browser")) browser() - } - - configureWasm() - } - - if (hasPosix || hasLinux || hasDarwin || hasWindows) extra["hasNative"] = true - - sourceSets { - if (hasJsAndWasmShared) { - val commonMain by getting {} - val jsAndWasmSharedMain by creating { - dependsOn(commonMain) - } - val commonTest by getting {} - val jsAndWasmSharedTest by creating { - dependsOn(commonTest) - } - - jsMain { - dependsOn(jsAndWasmSharedMain) - } - jsTest { - dependsOn(jsAndWasmSharedTest) - } - wasmJsMain { - dependsOn(jsAndWasmSharedMain) - } - wasmJsTest { - dependsOn(jsAndWasmSharedTest) - } - } - - if (hasPosix) { - val posixMain by creating - val posixTest by creating - } - - if (hasNix) { - val nixMain by creating - val nixTest by creating - } + configureCommon() - if (hasDarwin) { - val darwinMain by creating { - val nixMain = findByName("nixMain") - nixMain?.let { dependsOn(it) } + if (hasJvm) configureJvm() - val posixMain = findByName("posixMain") - posixMain?.let { dependsOn(posixMain) } + if (hasJs) configureJs() + if (hasWasm) configureWasm() - val jvmAndNixMain = findByName("jvmAndNixMain") - jvmAndNixMain?.let { dependsOn(jvmAndNixMain) } - - val commonMain = findByName("commonMain") - commonMain?.let { dependsOn(commonMain) } - } - val darwinTest by creating { - dependencies { - implementation(kotlin("test")) - } - - val nixTest = findByName("nixTest") - nixTest?.let { dependsOn(nixTest) } - - val posixTest = findByName("posixTest") - posixTest?.let { dependsOn(posixTest) } - - val jvmAndNixTest = findByName("jvmAndNixTest") - jvmAndNixTest?.let { dependsOn(jvmAndNixTest) } - - val commonTest = findByName("commonTest") - commonTest?.let { dependsOn(commonTest) } - } + if (hasPosix) posixTargets() + if (hasNix) nixTargets() + if (hasDarwin) darwinTargets() + if (hasLinux) linuxTargets() + if (hasDesktop) desktopTargets() + if (hasWindows) windowsTargets() - val macosMain by creating - val macosTest by creating - - val watchosMain by creating - val watchosTest by creating - - val tvosMain by creating - val tvosTest by creating - - val iosMain by creating - val iosTest by creating - } - - if (hasDesktop) { - val desktopMain by creating { - val commonMain = findByName("commonMain") - commonMain?.let { dependsOn(commonMain) } - } - val desktopTest by creating { - val commonTest = findByName("commonTest") - commonTest?.let { dependsOn(commonTest) } - } - } - - if (hasLinux) { - val linuxMain by creating - val linuxTest by creating - } - - if (hasWindows) { - val windowsMain by creating - val windowsTest by creating - } - - if (hasJvmAndPosix) { - val jvmAndPosixMain by creating { - findByName("commonMain")?.let { dependsOn(it) } - } - - val jvmAndPosixTest by creating { - findByName("commonTest")?.let { dependsOn(it) } - } - } - - if (hasJvmAndNix) { - val jvmAndNixMain by creating { - findByName("commonMain")?.let { dependsOn(it) } - } - - val jvmAndNixTest by creating { - findByName("commonTest")?.let { dependsOn(it) } - } - } - - if (hasJvm) { - val jvmMain by getting { - findByName("jvmAndNixMain")?.let { dependsOn(it) } - findByName("jvmAndPosixMain")?.let { dependsOn(it) } - } - - val jvmTest by getting { - findByName("jvmAndNixTest")?.let { dependsOn(it) } - findByName("jvmAndPosixTest")?.let { dependsOn(it) } - } - } - - if (hasPosix) { - val posixMain by getting { - findByName("commonMain")?.let { dependsOn(it) } - findByName("jvmAndPosixMain")?.let { dependsOn(it) } - } - - val posixTest by getting { - findByName("commonTest")?.let { dependsOn(it) } - findByName("jvmAndPosixTest")?.let { dependsOn(it) } - - dependencies { - implementation(kotlin("test")) - } - } - - posixTargets().forEach { - getByName("${it}Main").dependsOn(posixMain) - getByName("${it}Test").dependsOn(posixTest) - } - } - - if (hasNix) { - val nixMain by getting { - findByName("posixMain")?.let { dependsOn(it) } - findByName("jvmAndNixMain")?.let { dependsOn(it) } - } - - val nixTest by getting { - findByName("posixTest")?.let { dependsOn(it) } - findByName("jvmAndNixTest")?.let { dependsOn(it) } - } - - nixTargets().forEach { - getByName("${it}Main").dependsOn(nixMain) - getByName("${it}Test").dependsOn(nixTest) - } - } - - if (hasDarwin) { - val nixMain: KotlinSourceSet? = findByName("nixMain") - val nixTest: KotlinSourceSet? = findByName("nixTest") - - val darwinMain by getting - val darwinTest by getting - val macosMain by getting - val macosTest by getting - val iosMain by getting - val iosTest by getting - val watchosMain by getting - val watchosTest by getting - val tvosMain by getting - val tvosTest by getting - - nixMain?.let { darwinMain.dependsOn(it) } - macosMain.dependsOn(darwinMain) - tvosMain.dependsOn(darwinMain) - iosMain.dependsOn(darwinMain) - watchosMain.dependsOn(darwinMain) - - nixTest?.let { darwinTest.dependsOn(it) } - macosTest.dependsOn(darwinTest) - tvosTest.dependsOn(darwinTest) - iosTest.dependsOn(darwinTest) - watchosTest.dependsOn(darwinTest) - - macosTargets().forEach { - getByName("${it}Main").dependsOn(macosMain) - getByName("${it}Test").dependsOn(macosTest) - } - - iosTargets().forEach { - getByName("${it}Main").dependsOn(iosMain) - getByName("${it}Test").dependsOn(iosTest) - } - - watchosTargets().forEach { - getByName("${it}Main").dependsOn(watchosMain) - getByName("${it}Test").dependsOn(watchosTest) - } + @OptIn(ExperimentalKotlinGradlePluginApi::class) + applyHierarchyTemplate(hierarchyTemplate) + } - tvosTargets().forEach { - getByName("${it}Main").dependsOn(tvosMain) - getByName("${it}Test").dependsOn(tvosTest) - } + if (hasExplicitNative) extra["hasNative"] = true + if (hasNative) { + tasks.maybeNamed("linkDebugTestLinuxX64") { onlyIf { HOST_NAME == "linux" } } + tasks.maybeNamed("linkDebugTestLinuxArm64") { onlyIf { HOST_NAME == "linux" } } + tasks.maybeNamed("linkDebugTestMingwX64") { onlyIf { HOST_NAME == "windows" } } + } - darwinTargets().forEach { - getByName("${it}Main").dependsOn(darwinMain) - getByName("${it}Test").dependsOn(darwinTest) - } + if (hasJsAndWasmShared) { + tasks.configureEach { + if (name == "compileJsAndWasmSharedMainKotlinMetadata") { + enabled = false } + } + } +} - if (hasLinux) { - val linuxMain by getting { - findByName("nixMain")?.let { dependsOn(it) } - } +@OptIn(ExperimentalKotlinGradlePluginApi::class) +private val hierarchyTemplate = KotlinHierarchyTemplate { + withSourceSetTree(KotlinSourceSetTree.main, KotlinSourceSetTree.test) - val linuxTest by getting { - findByName("nixTest")?.let { dependsOn(it) } + common { + group("posix") { + group("windows") { withMingw() } - dependencies { - implementation(kotlin("test")) - } - } + group("nix") { + group("linux") { withLinux() } - linuxTargets().forEach { - getByName("${it}Main").dependsOn(linuxMain) - getByName("${it}Test").dependsOn(linuxTest) - } - } - - if (hasDesktop) { - val desktopMain by getting { - findByName("posixMain")?.let { dependsOn(it) } - } + group("darwin") { + withApple() - val desktopTest by getting { - findByName("posixTest")?.let { dependsOn(it) } - } - - desktopTargets().forEach { - getByName("${it}Main").dependsOn(desktopMain) - getByName("${it}Test").dependsOn(desktopTest) + group("ios") { withIos() } + group("tvos") { withTvos() } + group("watchos") { withWatchos() } + group("macos") { withMacos() } } } + } - if (hasWindows) { - val windowsMain by getting { - findByName("posixMain")?.let { dependsOn(it) } - findByName("desktopMain")?.let { dependsOn(it) } - findByName("commonMain")?.let { dependsOn(it) } - } - - val windowsTest by getting { - findByName("posixTest")?.let { dependsOn(it) } - findByName("desktopTest")?.let { dependsOn(it) } - findByName("commonTest")?.let { dependsOn(it) } - - dependencies { - implementation(kotlin("test")) - } - } + group("jsAndWasmShared") { + withJs() + withWasmJs() + } - windowsTargets().forEach { - getByName("${it}Main").dependsOn(windowsMain) - getByName("${it}Test").dependsOn(windowsTest) - } - } + group("jvmAndPosix") { + withJvm() + group("posix") + } - if (hasNative) { - tasks.maybeNamed("linkDebugTestLinuxX64") { onlyIf { HOST_NAME == "linux" } } - tasks.maybeNamed("linkDebugTestLinuxArm64") { onlyIf { HOST_NAME == "linux" } } - tasks.maybeNamed("linkDebugTestMingwX64") { onlyIf { HOST_NAME == "windows" } } - } + group("jvmAndNix") { + withJvm() + group("nix") } - } - if (hasJsAndWasmShared) { - tasks.configureEach { - if (name == "compileJsAndWasmSharedMainKotlinMetadata") { - enabled = false - } + group("desktop") { + group("linux") + group("windows") + group("macos") } } } diff --git a/buildSrc/src/main/kotlin/WasmConfig.kt b/buildSrc/src/main/kotlin/WasmConfig.kt index 7aa60b563ae..fc9d23fcb1f 100644 --- a/buildSrc/src/main/kotlin/WasmConfig.kt +++ b/buildSrc/src/main/kotlin/WasmConfig.kt @@ -10,9 +10,13 @@ import org.jetbrains.kotlin.gradle.targets.js.ir.* import java.io.* fun Project.configureWasm() { - configureWasmTasks() - kotlin { + @OptIn(ExperimentalWasmDsl::class) + wasmJs { + nodejs { useMochaForTests() } + if (project.targetIsEnabled("wasmJs.browser")) browser { useKarmaForTests() } + } + sourceSets { wasmJsMain { dependencies { @@ -27,37 +31,5 @@ fun Project.configureWasm() { } } - configureWasmTestTasks() -} - -private fun Project.configureWasmTasks() { - kotlin { - @OptIn(ExperimentalWasmDsl::class) - wasmJs { - nodejs { - testTask { - useMocha { - timeout = "10000" - } - } - } - - (this as KotlinJsIrTarget).whenBrowserConfigured { - testTask { - useKarma { - useChromeHeadless() - useConfigDirectory(File(project.rootProject.projectDir, "karma")) - } - } - } - } - } -} - -private fun Project.configureWasmTestTasks() { - val shouldRunWasmBrowserTest = !hasProperty("teamcity") || hasProperty("enable-js-tests") - if (shouldRunWasmBrowserTest) return - - tasks.maybeNamed("cleanWasmJsBrowserTest") { onlyIf { false } } - tasks.maybeNamed("wasmJsBrowserTest") { onlyIf { false } } + configureJsTestTasks(target = "wasmJs") } diff --git a/ktor-network/build.gradle.kts b/ktor-network/build.gradle.kts index a0ca333b3d0..a3c871ddec5 100644 --- a/ktor-network/build.gradle.kts +++ b/ktor-network/build.gradle.kts @@ -1,3 +1,7 @@ +/* + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + description = "Ktor network utilities" kotlin { @@ -24,22 +28,5 @@ kotlin { implementation(libs.mockk) } } - - macosTest { - val nixTest = getByName("posixTest") - dependsOn(nixTest) - } - watchosTest { - val nixTest = getByName("posixTest") - dependsOn(nixTest) - } - tvosTest { - val nixTest = getByName("posixTest") - dependsOn(nixTest) - } - iosTest { - val nixTest = getByName("posixTest") - dependsOn(nixTest) - } } }