-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle.kts
377 lines (333 loc) · 15.1 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import kotlin.jvm.optionals.getOrNull
plugins {
alias(libs.plugins.kmp)
alias(libs.plugins.androidLibrary)
}
val libFinder = versionCatalogs.find("libs").get()
var REAL_VERSION = libs.versions.libraryVersion.get()
val JVM_TARGET = JvmTarget.JVM_17
val JDK_VERSION = JavaVersion.VERSION_17
val GROUP = "com.fleeksoft"
kotlin {
jvm()
androidTarget()
}
allprojects {
repositories {
mavenCentral()
mavenLocal()
google()
gradlePluginPortal()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
}
version = REAL_VERSION
group = GROUP
project.apply(plugin = "kotlin-multiplatform")
project.apply(plugin = "android-library")
java.toolchain.languageVersion = JavaLanguageVersion.of(JDK_VERSION.majorVersion)
kotlin.jvmToolchain(JDK_VERSION.majorVersion.toInt())
afterEvaluate {
tasks.withType(Test::class) {
//this.javaLauncher.set()
this.javaLauncher.set(javaToolchains.launcherFor {
// 17 is latest at the current moment
languageVersion.set(JavaLanguageVersion.of(JDK_VERSION.majorVersion))
})
}
}
android {
compileOptions {
sourceCompatibility = JDK_VERSION
targetCompatibility = JDK_VERSION
}
compileSdk = 35
namespace = "com.fleeksoft.${project.name.replace("-", ".")}"
defaultConfig {
minSdk = 21
}
}
MicroAmper(this).configure()
}
subprojects {
apply(plugin = "kotlin-multiplatform")
kotlin {
androidTarget {
this.compilerOptions.jvmTarget.set(JVM_TARGET)
publishAllLibraryVariants()
}
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask::class) {
compilerOptions.suppressWarnings.set(true)
// @TODO: We should actually, convert warnings to errors and start removing warnings
compilerOptions.freeCompilerArgs.add("-nowarn")
compilerOptions.freeCompilerArgs.add("-Xklib-duplicated-unique-name-strategy=allow-all-with-warning")
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink::class) {
val folder = this.outputs.files.toList().firstOrNull()
val fromFolder = File(project.projectDir, "testresources")
if (folder != null) {
val copyAfterLink = tasks.create("${this.name}CopyResources", Copy::class)
copyAfterLink.from(fromFolder)
copyAfterLink.into(folder)
this.dependsOn(copyAfterLink)
}
}
}
// Tiny, coupled and limited variant of amper compatible with the current structure, so we can bump to Kotlin 2.0.0 in the meantime, while amper is discarded or evolved.
class MicroAmper(val project: Project) {
private var kotlinPlatforms = mutableListOf<String>()
private var kotlinAliases = LinkedHashMap<String, List<String>>()
private var deps = mutableListOf<Dep>()
//val kotlinBasePlatforms by lazy { kotlinPlatforms.groupBy { getKotlinBasePlatform(it) }.filter { it.value != listOf(it.key) } }
val kotlinBasePlatforms by lazy { kotlinPlatforms.groupBy { getKotlinBasePlatform(it) } }
fun getKotlinBasePlatform(platform: String): String =
platform.removeSuffix("X64").removeSuffix("X86").removeSuffix("Arm64").removeSuffix("Arm32").removeSuffix("Simulator").removeSuffix("Device")
.also {
check(it.all { it.isLowerCase() && !it.isDigit() })
}
data class Dep(val path: String, val exported: Boolean, val test: Boolean, val platform: String, val compileOnly: Boolean) {
val rplatform = platform.takeIf { it.isNotEmpty() } ?: "common"
val configuration =
"$rplatform${if (test) "Test" else "Main"}${if (exported) "Api" else if (compileOnly) "CompileOnly" else "Implementation"}"
}
fun parseFile(file: File, lines: List<String> = file.readLines()) {
var mode = ""
for (line in lines) {
val tline = line.substringBeforeLast('#').trim().takeIf { it.isNotEmpty() } ?: continue
if (line.startsWith(" ") || line.startsWith("\t") || line.startsWith("-")) {
when {
mode == "product" -> {
//println("product=$tline")
when {
tline.startsWith("platforms:") -> {
val platforms = tline.substringAfter('[').substringBeforeLast(']').split(',').map { it.trim() }
kotlinPlatforms.addAll(platforms)
}
}
}
mode == "aliases" -> {
//println("aliases=$tline")
if (tline.startsWith("-")) {
val (alias2, platforms2) = tline.split(":", limit = 2)
val alias = alias2.trim('-', ' ')
val platforms = platforms2.trim('[', ']', ' ').split(',').map { it.trim() }
//println(" -> alias=$alias, platforms=$platforms")
kotlinAliases[alias] = platforms
}
}
mode.contains("dependencies") -> {
val platform = mode.substringAfterLast('@', "")
val test = mode.startsWith("test")
val compileOnly = line.contains(Regex(":\\s*compile-only"))
val exported = line.contains(Regex(":\\s*exported"))
val path = tline.removePrefix("-").removeSuffix(": exported").removeSuffix(":exported").removeSuffix(": compile-only")
.removeSuffix(":compile-only").trim()
if (platform.isBlank() || kotlinBasePlatforms.contains(platform) || kotlinAliases.contains(platform) || platform == "apple") {
deps += Dep(path = path, exported = exported, test = test, platform = platform, compileOnly = compileOnly)
} else {
println("platform not included: $platform in $project")
}
}
}
} else {
if (tline.endsWith(":")) {
mode = tline.trimEnd(':').trim()
}
if (tline.startsWith("apply:")) {
val paths = tline.substringAfter(':').trim('[', ',', ' ', ']').split(",")
for (path in paths) {
parseFile(file.parentFile.resolve(path))
}
}
}
}
}
data class SourceSetPair(val main: KotlinSourceSet, val test: KotlinSourceSet) {
fun dependsOn(other: SourceSetPair) {
main.dependsOn(other.main)
test.dependsOn(other.test)
}
}
val sourceSetPairs = LinkedHashMap<String, SourceSetPair>()
// specific depends on more generic
fun NamedDomainObjectContainer<KotlinSourceSet>.ssDependsOn(base: String, other: String) {
if (base == other) return
//println("$base dependsOn $other")
ssPair(base).dependsOn(ssPair(other))
}
val projectFiles: Set<String> = (project.projectDir.list() ?: emptyArray()).toSet()
fun SourceDirectorySet.srcDirIfExists(path: String) {
//if (path in projectFiles) setSrcDirs(listOf(path)) //else println("file doesn't exist $path")
//srcDir(path)
setSrcDirs(listOf(path))
}
fun NamedDomainObjectContainer<KotlinSourceSet>.ssPair(name: String): SourceSetPair {
return sourceSetPairs.getOrPut(name) {
val atName = if (name == "common") "" else "@$name"
SourceSetPair(
main = maybeCreate("${name}Main").also {
it.kotlin.srcDirIfExists("src$atName")
it.resources.srcDirIfExists("resources$atName")
it.kotlin.srcDir("build/generated/ksp/$name/${name}Main/kotlin")
},
test = maybeCreate("${name}Test").also {
it.kotlin.srcDirIfExists("test$atName")
it.resources.srcDirIfExists("testResources$atName")
it.kotlin.srcDir("build/generated/ksp/$name/${name}Test/kotlin")
}
)
}
}
fun applyTo() = with(project) {
project.kotlin.sourceSets {
ssDependsOn("native", "common")
ssDependsOn("native", "nonJvm")
ssDependsOn("posix", "native")
ssDependsOn("apple", "posix")
ssDependsOn("appleNonWatchos", "apple")
ssDependsOn("appleIosTvos", "apple")
maybeCreate("commonMain").kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
for (platform in kotlinPlatforms) {
val isMacos = platform.startsWith("macos")
val isIos = platform.startsWith("ios")
val isTvos = platform.startsWith("tvos")
val isWatchos = platform.startsWith("watchos")
val isNative = platform.contains("X86") || platform.contains("X64") || platform.contains("Arm")
val isApple = isMacos || isIos || isTvos || isWatchos
val isLinux = platform.startsWith("linux")
val isWindows = platform.startsWith("mingw")
val isPosix = isLinux || isApple
val basePlatform = getKotlinBasePlatform(platform)
if (isIos || isTvos) ssDependsOn(basePlatform, "appleIosTvos")
if (isApple && !isWatchos) ssDependsOn(basePlatform, "appleNonWatchos")
if (isPosix) ssDependsOn(basePlatform, "posix")
if (isApple) ssDependsOn(basePlatform, "apple")
if (isNative) ssDependsOn(basePlatform, "native")
if (platform != basePlatform) ssDependsOn(platform, basePlatform)
}
}
for (platform in kotlinPlatforms) {
when (platform) {
"jvm" -> kotlin.jvm {
compilerOptions {
this.jvmTarget.set(JVM_TARGET)
}
}
"js" -> kotlin.js {
browser {
testTask {
useMocha {
timeout = "9s"
}
}
}
nodejs {
testTask {
useMocha {
timeout = "9s"
}
}
}
}
"wasm" -> {
kotlin.wasmJs {
browser {
testTask {
useMocha {
timeout = "9s"
}
}
}
nodejs {
testTask {
useMocha {
timeout = "9s"
}
}
}
}
kotlin.sourceSets {
ssDependsOn("wasmJs", "wasm")
}
}
"android" -> kotlin.androidTarget {}
"linuxX64" -> kotlin.linuxX64()
"linuxArm64" -> kotlin.linuxArm64()
"tvosArm64" -> kotlin.tvosArm64()
"tvosX64" -> kotlin.tvosX64()
"tvosSimulatorArm64" -> kotlin.tvosSimulatorArm64()
"macosX64" -> kotlin.macosX64()
"macosArm64" -> kotlin.macosArm64()
"iosArm64" -> kotlin.iosArm64()
"iosSimulatorArm64" -> kotlin.iosSimulatorArm64()
"iosX64" -> kotlin.iosX64()
"watchosArm64" -> kotlin.watchosArm64()
"watchosArm32" -> kotlin.watchosArm32()
"watchosDeviceArm64" -> kotlin.watchosDeviceArm64()
"watchosSimulatorArm64" -> kotlin.watchosSimulatorArm64()
"mingwX64" -> kotlin.mingwX64()
"androidNativeX86" -> kotlin.androidNativeX86()
"androidNativeX64" -> kotlin.androidNativeX64()
"androidNativeArm32" -> kotlin.androidNativeArm32()
"androidNativeArm64" -> kotlin.androidNativeArm64()
}
}
//kotlin.applyDefaultHierarchyTemplate()
kotlin.targets.forEach {
it.compilations.forEach {
it.compileTaskProvider.configure {
compilerOptions {
// apiVersion: Allow to use declarations only from the specified version of bundled libraries
// languageVersion: Provide source compatibility with specified language version
// this.apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
// this.languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
}
}
}
}
kotlin.sourceSets {
// jvm, js, wasm, android, linuxX64, linuxArm64, tvosArm64, tvosX64, tvosSimulatorArm64, macosX64, macosArm64, iosArm64, iosSimulatorArm64, iosX64, watchosArm64, watchosArm32, watchosDeviceArm64, watchosSimulatorArm64, mingwX64
for ((alias, platforms) in (kotlinAliases + kotlinBasePlatforms)) {
//for ((alias, platforms) in kotlinAliases) {
ssDependsOn(alias, "common")
for (platform in platforms) ssDependsOn(platform, alias)
}
}
//println(" -> $platforms")
dependencies {
for (dep in deps) {
add(
dep.configuration, when {
dep.path.contains('/') -> project(":${File(dep.path).name}")
dep.path.startsWith("\$") -> {
when (dep.path) {
"\$kotlin-test" -> "org.jetbrains.kotlin:kotlin-test"
else -> {
val result = libFinder.findLibrary(dep.path.replace("\$libs.", "").replace(".", "-")).getOrNull()?.get()
result?.toString() ?: TODO("Unknown ${dep.path}, $result")
}
}
}
else -> dep.path
}
)
}
}
for (target in kotlin.targets) {
target.compilations.all {
compileTaskProvider.configure {
compilerOptions {
suppressWarnings.set(true)
}
}
}
}
}
fun configure() {
val amperFile = File(project.projectDir, "module.yaml").takeIf { it.exists() } ?: return
parseFile(amperFile)
applyTo()
}
}