Skip to content

Commit 6d85884

Browse files
authored
Merge branch 'main' into bjhham/pinger-failure-fix
2 parents ead8b6f + 5bed42f commit 6d85884

File tree

42 files changed

+1028
-498
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1028
-498
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/*
2-
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
import ktorbuild.KtorBuildExtension
56
import ktorbuild.internal.resolveVersion
67

78
version = resolveVersion()
9+
10+
extensions.create<KtorBuildExtension>(KtorBuildExtension.NAME)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import ktorbuild.internal.gradle.*
6+
import ktorbuild.internal.ktorBuild
7+
import ktorbuild.maybeNamed
8+
import ktorbuild.targets.*
9+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
10+
11+
plugins {
12+
id("ktorbuild.base")
13+
kotlin("multiplatform")
14+
}
15+
16+
kotlin {
17+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
18+
applyHierarchyTemplate(KtorTargets.hierarchyTemplate)
19+
addTargets(ktorBuild.targets)
20+
21+
// Specify JVM toolchain later to prevent it from being evaluated before it was configured.
22+
// TODO: Remove `afterEvaluate` when the BCV issue triggering JVM toolchain evaluation is fixed
23+
// https://github.com/Kotlin/binary-compatibility-validator/issues/286
24+
afterEvaluate {
25+
jvmToolchain {
26+
languageVersion = ktorBuild.jvmToolchain
27+
}
28+
}
29+
}
30+
31+
val targets = ktorBuild.targets
32+
33+
configureCommon()
34+
if (targets.hasJvm) configureJvm()
35+
if (targets.hasJs) configureJs()
36+
if (targets.hasWasmJs) configureWasmJs()
37+
38+
if (targets.hasJsOrWasmJs) {
39+
tasks.configureEach {
40+
if (name == "compileJsAndWasmSharedMainKotlinMetadata") enabled = false
41+
}
42+
}
43+
44+
// Run native tests only on matching host.
45+
// There is no need to configure `onlyIf` for Darwin targets as they're configured by KGP.
46+
@Suppress("UnstableApiUsage")
47+
if (targets.hasNative) {
48+
tasks.maybeNamed("linkDebugTestLinuxX64") {
49+
onlyIf("run only on Linux") { ktorBuild.os.get().isLinux() }
50+
}
51+
tasks.maybeNamed("linkDebugTestLinuxArm64") {
52+
onlyIf("run only on Linux") { ktorBuild.os.get().isLinux() }
53+
}
54+
tasks.maybeNamed("linkDebugTestMingwX64") {
55+
onlyIf("run only on Windows") { ktorBuild.os.get().isWindows() }
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ktorbuild
6+
7+
import ktorbuild.targets.KtorTargets
8+
import org.gradle.kotlin.dsl.assign
9+
import org.gradle.kotlin.dsl.getValue
10+
import org.gradle.kotlin.dsl.provideDelegate
11+
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
12+
import org.jetbrains.kotlin.gradle.plugin.mpp.DefaultCInteropSettings
13+
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
14+
15+
/**
16+
* Creates a CInterop configuration for all Native targets using the given [sourceSet]
17+
* in a Kotlin Multiplatform project.
18+
*
19+
* The [name] defines the CInterop configuration name. Definition file is expected to be located
20+
* at `[sourceSet]/interop/[name].def` by default, but can be customized via [definitionFilePath].
21+
* Additional configuration can be provided through [configure] block.
22+
*
23+
* Simple usage:
24+
* ```
25+
* kotlin {
26+
* createCInterop("ssl", "posix")
27+
* }
28+
* ```
29+
*
30+
* Advanced usage with a separate definition for each target and additional configuration:
31+
* ```
32+
* kotlin {
33+
* createCInterop(
34+
* name = "ssl",
35+
* sourceSet = "posix",
36+
* definitionFilePath = { target -> "$target/interop/ssl.def" },
37+
* configure = { target ->
38+
* includeDirs("$target/interop/include")
39+
* compilerOpts("-DUSE_SSL")
40+
* }
41+
* )
42+
* }
43+
* ```
44+
*/
45+
@Suppress("UnstableApiUsage")
46+
fun KotlinMultiplatformExtension.createCInterop(
47+
name: String,
48+
sourceSet: String,
49+
definitionFilePath: (String) -> String = { "$sourceSet/interop/$name.def" },
50+
configure: DefaultCInteropSettings.(String) -> Unit = {}
51+
) {
52+
val cinteropTargets = KtorTargets.resolveTargets(sourceSet)
53+
val projectDirectory = project.isolated.projectDirectory
54+
55+
targets.named { it in cinteropTargets }
56+
.all {
57+
check(this is KotlinNativeTarget) { "Can't create cinterop for non-native target $targetName" }
58+
59+
val main by compilations
60+
main.cinterops.create(name) {
61+
definitionFile = projectDirectory.file(definitionFilePath(targetName))
62+
configure(targetName)
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ktorbuild
6+
7+
import ktorbuild.internal.gradle.finalizedOnRead
8+
import ktorbuild.targets.KtorTargets
9+
import org.gradle.api.JavaVersion
10+
import org.gradle.api.model.ObjectFactory
11+
import org.gradle.api.provider.Property
12+
import org.gradle.api.provider.Provider
13+
import org.gradle.api.provider.ProviderFactory
14+
import org.gradle.jvm.toolchain.JavaLanguageVersion
15+
import org.gradle.kotlin.dsl.newInstance
16+
import org.gradle.kotlin.dsl.property
17+
import org.gradle.platform.BuildPlatform
18+
import org.gradle.platform.OperatingSystem
19+
import javax.inject.Inject
20+
21+
@Suppress("UnstableApiUsage")
22+
abstract class KtorBuildExtension(
23+
objects: ObjectFactory,
24+
providers: ProviderFactory,
25+
buildPlatform: BuildPlatform,
26+
val targets: KtorTargets,
27+
) {
28+
29+
@Inject
30+
constructor(
31+
objects: ObjectFactory,
32+
providers: ProviderFactory,
33+
buildPlatform: BuildPlatform,
34+
) : this(objects, providers, buildPlatform, targets = objects.newInstance())
35+
36+
/**
37+
* The JDK version to be used to build the project.
38+
* By default, the minimal supported JDK version is used.
39+
*/
40+
val jvmToolchain: Property<JavaLanguageVersion> =
41+
objects.property<JavaLanguageVersion>()
42+
.convention(DEFAULT_JDK)
43+
.finalizedOnRead()
44+
45+
fun jvmToolchain(version: Int) {
46+
jvmToolchain.set(JavaLanguageVersion.of(version))
47+
}
48+
49+
/**
50+
* The JDK version to be used for testing.
51+
*
52+
* The value is determined from the Gradle property "test.jdk".
53+
* If the property is not specified, it defaults to the current JDK used by Gradle.
54+
*
55+
* For example, to run tests against JDK 8, run a test task with flag "-Ptest.jdk=8"
56+
* or put this property to `gradle.properties`.
57+
*/
58+
val jvmTestToolchain: Provider<JavaLanguageVersion> =
59+
providers.gradleProperty("test.jdk")
60+
.orElse(providers.provider { JavaVersion.current().majorVersion })
61+
.map(JavaLanguageVersion::of)
62+
63+
/** Host operating system. */
64+
val os: Provider<OperatingSystem> = providers.provider { buildPlatform.operatingSystem }
65+
66+
companion object {
67+
const val NAME = "ktorBuild"
68+
69+
/** The default (minimal) JDK version used for building the project. */
70+
private val DEFAULT_JDK = JavaLanguageVersion.of(8)
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ktorbuild
6+
7+
import org.gradle.api.NamedDomainObjectCollection
8+
9+
internal fun <T> NamedDomainObjectCollection<T>.maybeNamed(name: String, configure: T.() -> Unit) {
10+
if (name in names) named(name).configure(configure)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ktorbuild.internal
6+
7+
import ktorbuild.KtorBuildExtension
8+
import org.gradle.api.Project
9+
import org.gradle.kotlin.dsl.getByType
10+
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
11+
12+
/*
13+
* Gradle doesn't generate accessors for plugins defined in this module,
14+
* and we can't use generated accessors from .kt files, so we have to declare them manually.
15+
*/
16+
17+
internal val Project.ktorBuild: KtorBuildExtension get() = extensions.getByType()
18+
19+
internal fun Project.kotlin(configure: KotlinMultiplatformExtension.() -> Unit) =
20+
extensions.configure("kotlin", configure)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package ktorbuild.internal
6+
7+
internal fun String.capitalized() = replaceFirstChar { it.uppercase() }

0 commit comments

Comments
 (0)