Skip to content

Commit af15db4

Browse files
authored
Migrated shared build logic from buildSrc to composite builds (#186)
The sharing code is rewritten as convention plugins and placed in the separate build. Two plugins are allocated - for setting up the publication and for setting up the Kotlin multiplatform project. Resolves #142
1 parent 16d56ef commit af15db4

File tree

9 files changed

+199
-238
lines changed

9 files changed

+199
-238
lines changed

buildSrc/build.gradle.kts renamed to build-logic/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
*/
55

66
plugins {
7-
`kotlin-dsl`
7+
`kotlin-dsl`
88
}
99

1010
repositories {
11-
mavenCentral()
11+
mavenCentral()
1212
}
1313

1414
dependencies {
15-
implementation(libs.kotlin.gradle.plugin)
15+
implementation(libs.kotlin.gradle.plugin)
1616
}
File renamed without changes.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
6+
import org.gradle.kotlin.dsl.kotlin
7+
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
8+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
9+
import kotlin.jvm.optionals.getOrNull
10+
11+
plugins {
12+
kotlin("multiplatform")
13+
}
14+
15+
kotlin {
16+
17+
val versionCatalog: VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
18+
jvmToolchain {
19+
val javaVersion = versionCatalog.findVersion("java").getOrNull()?.requiredVersion
20+
?: throw GradleException("Version 'java' is not specified in the version catalog")
21+
languageVersion.set(JavaLanguageVersion.of(javaVersion))
22+
}
23+
24+
jvm {
25+
withJava()
26+
testRuns["test"].executionTask.configure {
27+
useJUnitPlatform()
28+
}
29+
}
30+
31+
js(IR) {
32+
browser {
33+
testTask {
34+
filter.setExcludePatterns("*SmokeFileTest*")
35+
}
36+
}
37+
}
38+
39+
sourceSets {
40+
commonTest {
41+
dependencies {
42+
implementation(kotlin("test"))
43+
}
44+
}
45+
}
46+
47+
explicitApi()
48+
sourceSets.configureEach {
49+
configureSourceSet()
50+
}
51+
52+
configureNativePlatforms()
53+
54+
val nativeTargets = nativeTargets()
55+
sourceSets {
56+
createSourceSet("nativeMain", parent = commonMain.get(), children = nativeTargets)
57+
createSourceSet("nativeTest", parent = commonTest.get(), children = nativeTargets)
58+
}
59+
}
60+
61+
fun KotlinSourceSet.configureSourceSet() {
62+
val srcDir = if (name.endsWith("Main")) "src" else "test"
63+
val platform = name.dropLast(4)
64+
kotlin.srcDir("$platform/$srcDir")
65+
if (name == "jvmMain") {
66+
resources.srcDir("$platform/resources")
67+
} else if (name == "jvmTest") {
68+
resources.srcDir("$platform/test-resources")
69+
}
70+
languageSettings {
71+
progressiveMode = true
72+
}
73+
}
74+
75+
/**
76+
* Creates a source set for a directory that isn't already a built-in platform. Use this to create
77+
* custom shared directories like `nonJvmMain` or `unixMain`.
78+
*/
79+
fun NamedDomainObjectContainer<KotlinSourceSet>.createSourceSet(
80+
name: String,
81+
parent: KotlinSourceSet? = null,
82+
children: List<String> = listOf()
83+
): KotlinSourceSet {
84+
val result = create(name)
85+
86+
if (parent != null) {
87+
result.dependsOn(parent)
88+
}
89+
90+
val suffix = when {
91+
name.endsWith("Main") -> "Main"
92+
name.endsWith("Test") -> "Test"
93+
else -> error("unexpected source set name: ${name}")
94+
}
95+
96+
for (childTarget in children) {
97+
val childSourceSet = get("${childTarget}$suffix")
98+
childSourceSet.dependsOn(result)
99+
}
100+
101+
return result
102+
}
103+
104+
fun KotlinMultiplatformExtension.configureNativePlatforms() {
105+
iosX64()
106+
iosArm64()
107+
iosSimulatorArm64()
108+
tvosX64()
109+
tvosArm64()
110+
tvosSimulatorArm64()
111+
watchosArm32()
112+
watchosArm64()
113+
watchosX64()
114+
watchosSimulatorArm64()
115+
watchosDeviceArm64()
116+
linuxArm64()
117+
androidNativeArm32()
118+
androidNativeArm64()
119+
androidNativeX64()
120+
androidNativeX86()
121+
// Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
122+
linuxX64()
123+
macosX64()
124+
macosArm64()
125+
mingwX64()
126+
}
127+
128+
fun nativeTargets(): List<String> {
129+
return appleTargets() + linuxTargets() + mingwTargets() + androidTargets()
130+
}
131+
132+
fun appleTargets() = listOf(
133+
"iosArm64",
134+
"iosX64",
135+
"iosSimulatorArm64",
136+
"macosX64",
137+
"macosArm64",
138+
"tvosArm64",
139+
"tvosX64",
140+
"tvosSimulatorArm64",
141+
"watchosArm32",
142+
"watchosArm64",
143+
"watchosX64",
144+
"watchosSimulatorArm64",
145+
"watchosDeviceArm64"
146+
)
147+
148+
fun mingwTargets() = listOf(
149+
"mingwX64"
150+
)
151+
152+
fun linuxTargets() = listOf(
153+
"linuxX64",
154+
"linuxArm64"
155+
)
156+
157+
fun androidTargets() = listOf(
158+
"androidNativeArm32",
159+
"androidNativeArm64",
160+
"androidNativeX64",
161+
"androidNativeX86"
162+
)

buildSrc/src/main/kotlin/Publication.kt renamed to build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-publish.gradle.kts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
44
*/
55

6-
import org.gradle.api.Project
7-
import org.gradle.api.artifacts.dsl.*
8-
import org.gradle.api.file.*
9-
import org.gradle.api.provider.*
10-
import org.gradle.api.publish.maven.*
11-
import org.gradle.jvm.tasks.*
12-
import org.gradle.kotlin.dsl.*
13-
import org.gradle.plugins.signing.*
14-
import java.net.*
6+
import org.gradle.jvm.tasks.Jar
7+
import java.net.URI
8+
9+
plugins {
10+
`maven-publish`
11+
signing
12+
}
13+
14+
publishing {
15+
repositories {
16+
configureMavenPublication(project)
17+
}
18+
19+
val javadocJar = project.configureEmptyJavadocArtifact()
20+
publications.withType(MavenPublication::class).all {
21+
pom.configureMavenCentralMetadata(project)
22+
signPublicationIfKeyPresent(project, this)
23+
artifact(javadocJar)
24+
}
25+
26+
tasks.withType<PublishToMavenRepository>().configureEach {
27+
dependsOn(tasks.withType<Sign>())
28+
}
29+
}
1530

1631
// Pom configuration
1732
infix fun <T> Property<T>.by(value: T) {
@@ -103,6 +118,6 @@ fun signPublicationIfKeyPresent(project: Project, publication: MavenPublication)
103118
}
104119
}
105120

106-
private fun Project.getSensitiveProperty(name: String): String? {
121+
fun Project.getSensitiveProperty(name: String): String? {
107122
return project.findProperty(name) as? String ?: System.getenv(name)
108123
}

build.gradle.kts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,10 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
77
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
88

99
plugins {
10+
id("kotlinx-io-publish") apply false
11+
1012
alias(libs.plugins.bcv)
1113
alias(libs.plugins.dokka)
12-
`maven-publish`
13-
signing
14-
}
15-
16-
buildscript {
17-
dependencies {
18-
classpath(libs.kotlin.gradle.plugin)
19-
}
20-
21-
repositories {
22-
mavenCentral()
23-
gradlePluginPortal()
24-
}
2514
}
2615

2716
allprojects {
@@ -31,35 +20,6 @@ allprojects {
3120
}
3221
}
3322

34-
apply(plugin = "maven-publish")
35-
apply(plugin = "signing")
36-
37-
subprojects {
38-
if (name.contains("benchmark")) {
39-
return@subprojects
40-
}
41-
42-
repositories {
43-
mavenCentral()
44-
}
45-
46-
apply(plugin = "maven-publish")
47-
apply(plugin = "signing")
48-
49-
publishing {
50-
repositories {
51-
configureMavenPublication(project)
52-
}
53-
54-
val javadocJar = project.configureEmptyJavadocArtifact()
55-
publications.withType(MavenPublication::class).all {
56-
pom.configureMavenCentralMetadata(project)
57-
signPublicationIfKeyPresent(project, this)
58-
artifact(javadocJar)
59-
}
60-
}
61-
}
62-
6323
subprojects {
6424
tasks.withType<KotlinCompile>().configureEach {
6525
kotlinOptions {

buildSrc/src/main/kotlin/Platforms.kt

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)