Skip to content

Commit

Permalink
Move CommonConfig to build-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Jan 17, 2025
1 parent 917a6d3 commit 170ba55
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 52 deletions.
28 changes: 24 additions & 4 deletions build-logic/src/main/kotlin/ktorbuild.kmp.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import ktorbuild.internal.gradle.*
import ktorbuild.internal.ktorBuild
import ktorbuild.maybeNamed
import ktorbuild.targets.*
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

Expand All @@ -26,12 +28,30 @@ kotlin {
}
}

if (ktorBuild.targets.hasJvm) configureJvm()
if (ktorBuild.targets.hasJs) configureJs()
if (ktorBuild.targets.hasWasmJs) configureWasmJs()
val targets = ktorBuild.targets

if (ktorBuild.targets.hasJsOrWasmJs) {
configureCommon()
if (targets.hasJvm) configureJvm()
if (targets.hasJs) configureJs()
if (targets.hasWasmJs) configureWasmJs()

if (targets.hasJsOrWasmJs) {
tasks.configureEach {
if (name == "compileJsAndWasmSharedMainKotlinMetadata") enabled = false
}
}

// Run native tests only on matching host.
// There is no need to configure `onlyIf` for Darwin targets as they're configured by KGP.
@Suppress("UnstableApiUsage")
if (targets.hasNative) {
tasks.maybeNamed("linkDebugTestLinuxX64") {
onlyIf("run only on Linux") { ktorBuild.os.get().isLinux() }
}
tasks.maybeNamed("linkDebugTestLinuxArm64") {
onlyIf("run only on Linux") { ktorBuild.os.get().isLinux() }
}
tasks.maybeNamed("linkDebugTestMingwX64") {
onlyIf("run only on Windows") { ktorBuild.os.get().isWindows() }
}
}
12 changes: 10 additions & 2 deletions build-logic/src/main/kotlin/ktorbuild/KtorBuildExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ import org.gradle.api.provider.ProviderFactory
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import org.gradle.platform.BuildPlatform
import org.gradle.platform.OperatingSystem
import javax.inject.Inject

@Suppress("UnstableApiUsage")
abstract class KtorBuildExtension(
objects: ObjectFactory,
providers: ProviderFactory,
val targets: KtorTargets
buildPlatform: BuildPlatform,
val targets: KtorTargets,
) {

@Inject
constructor(
objects: ObjectFactory,
providers: ProviderFactory,
) : this(objects, providers, targets = objects.newInstance())
buildPlatform: BuildPlatform,
) : this(objects, providers, buildPlatform, targets = objects.newInstance())

/**
* The JDK version to be used to build the project.
Expand Down Expand Up @@ -55,6 +60,9 @@ abstract class KtorBuildExtension(
.orElse(providers.provider { JavaVersion.current().majorVersion })
.map(JavaLanguageVersion::of)

/** Host operating system. */
val os: Provider<OperatingSystem> = providers.provider { buildPlatform.operatingSystem }

companion object {
const val NAME = "ktorBuild"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("UnstableApiUsage")

package ktorbuild.internal.gradle

import org.gradle.platform.OperatingSystem

internal fun OperatingSystem.isLinux() = this == OperatingSystem.LINUX
internal fun OperatingSystem.isWindows() = this == OperatingSystem.WINDOWS
24 changes: 24 additions & 0 deletions build-logic/src/main/kotlin/ktorbuild/targets/CommonConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package ktorbuild.targets

import ktorbuild.internal.kotlin
import ktorbuild.internal.libs
import org.gradle.api.Project
import org.gradle.kotlin.dsl.invoke

internal fun Project.configureCommon() {
kotlin {
sourceSets {
commonMain.dependencies {
api(libs.kotlinx.coroutines.core)
}

commonTest.dependencies {
implementation(libs.kotlin.test)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ abstract class KtorTargets internal constructor(
val hasWasmJs: Boolean get() = isEnabled("wasmJs")

val hasJsOrWasmJs: Boolean get() = hasJs || hasWasmJs
val hasNative: Boolean get() = resolveTargets("posix").any(::isEnabled)

/**
* Determines if the specified [target] is enabled.
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ subprojects {
apply(plugin = "ktorbuild.kmp")
apply(plugin = "atomicfu-conventions")

configureTargets()
if (CI) configureTestTasksOnCi()

kotlin {
Expand Down
25 changes: 0 additions & 25 deletions buildSrc/src/main/kotlin/CommonConfig.kt

This file was deleted.

2 changes: 0 additions & 2 deletions buildSrc/src/main/kotlin/KtorBuildProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

val IDEA_ACTIVE: Boolean = System.getProperty("idea.active") == "true"

val OS_NAME = System.getProperty("os.name").lowercase()

val HOST_NAME = when {
Expand Down
18 changes: 0 additions & 18 deletions buildSrc/src/main/kotlin/TargetsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ val Project.hasCommon: Boolean get() = files.any { it.name == "common" }
val Project.hasNonJvm: Boolean get() = files.any { it.name == "nonJvm" }
val Project.hasJvmAndPosix: Boolean get() = hasCommon || files.any { it.name == "jvmAndPosix" }
val Project.hasPosix: Boolean get() = hasCommon || hasNonJvm || hasJvmAndPosix || files.any { it.name == "posix" }
val Project.hasDesktop: Boolean get() = hasPosix || files.any { it.name == "desktop" }
val Project.hasNix: Boolean get() = hasPosix || files.any { it.name == "nix" }
val Project.hasLinux: Boolean get() = hasNix || files.any { it.name == "linux" }
val Project.hasDarwin: Boolean get() = hasNix || files.any { it.name == "darwin" }
Expand All @@ -23,20 +22,3 @@ val Project.hasJsAndWasmShared: Boolean get() = hasCommon || hasNonJvm || files.
val Project.hasJs: Boolean get() = hasJsAndWasmShared || files.any { it.name == "js" }
val Project.hasWasmJs: Boolean get() = hasJsAndWasmShared || files.any { it.name == "wasmJs" }
val Project.hasJvm: Boolean get() = hasCommon || hasJvmAndPosix || files.any { it.name == "jvm" }

val Project.hasExplicitNative: Boolean
get() = hasNix || hasPosix || hasLinux || hasAndroidNative || hasDarwin || hasDesktop || hasWindows
val Project.hasNative: Boolean
get() = hasCommon || hasExplicitNative

fun Project.configureTargets() {
kotlin {
configureCommon()
}

if (hasNative) {
tasks.maybeNamed("linkDebugTestLinuxX64") { onlyIf { HOST_NAME == "linux" } }
tasks.maybeNamed("linkDebugTestLinuxArm64") { onlyIf { HOST_NAME == "linux" } }
tasks.maybeNamed("linkDebugTestMingwX64") { onlyIf { HOST_NAME == "windows" } }
}
}

0 comments on commit 170ba55

Please sign in to comment.