-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.gradle.kts
280 lines (231 loc) · 10.5 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
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.approximateDeclarationType
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJsCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("multiplatform") version "2.0.20-RC2" apply true
id("org.barfuin.gradle.taskinfo") version "2.2.0"
idea
}
//project.gradle.taskGraph.whenReady { println(project.gradle.taskGraph.allTasks) }
repositories {
mavenLocal()
mavenCentral()
}
//val jsIndex = file("src/jsMain/resources/index.html")
//jsIndex.textContent = jsIndex.textContent.replace(Regex("Kotlin C Compiler WIP Version ([\\d\\.]*)"), "Kotlin C Compiler WIP Version $version")
//println(file("src/commonMain/kotlin/com/soywiz/ktcc/internal/version.kt").textContent)
//fun KotlinTargetContainerWithPresetFunctions.common(callback: KotlinOnlyTarget<*>.() -> Unit) {
// callback(presets.getByName("common").createTarget("common") as KotlinOnlyTarget<*>)
//}
//val enableNative = false
val enableNative = System.getenv("KTCC_NO_ENABLE_NATIVE").isNullOrBlank()
kotlin {
fun KotlinTarget.configureAll() {
compilations.all {
//kotlinOptions.freeCompilerArgs = listOf("-progressive", "-Xskip-metadata-version-check")
}
}
fun KotlinNativeTarget.configureNative() {
configureAll()
binaries {
executable()
}
}
//common {
//}
jvm {
configureAll()
}
js {
configureAll()
browser()
binaries.executable()
compilerOptions {
this.target = "es2015"
this.useEsClasses = true
sourceMap = true
moduleKind = JsModuleKind.MODULE_ES
}
}
if (enableNative) {
macosArm64 { configureNative() }
macosX64 { configureNative() }
linuxX64 { configureNative() }
mingwX64 { configureNative() }
//sourceSets {
// val nativeMain = this.create("nativeMain")
// val nativeTest = this.create("nativeTest")
// configure(listOf(this.getByName("macosX64Main"), this.getByName("macosArm64Main"), this.getByName("linuxX64Main"), this.getByName("mingwX64Main"))) {
// dependsOn(nativeMain)
// }
//}
}
sourceSets["commonMain"].kotlin.srcDir("build/gen")
dependencies {
//val serializationRuntimeVersion = "0.11.1"
//commonMainImplementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serializationRuntimeVersion")
//add("jsMainImplementation", "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationRuntimeVersion")
commonMainImplementation("org.jetbrains.kotlin:kotlin-stdlib-common")
commonTestImplementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
commonTestImplementation("org.jetbrains.kotlin:kotlin-test-common")
add("jsTestImplementation", "org.jetbrains.kotlin:kotlin-test-js")
add("jvmTestImplementation", "org.jetbrains.kotlin:kotlin-test-junit")
add("jvmTestImplementation", "junit:junit:4.12")
add("jvmMainImplementation", "org.jetbrains.kotlin:kotlin-compiler")
// Scripting
//add("jvmMainImplementation", "org.jetbrains.kotlin:kotlin-script-runtime")
//add("jvmMainImplementation", "org.jetbrains.kotlin:kotlin-scripting-compiler")
//add("jvmMainImplementation", "org.jetbrains.kotlin:kotlin-scripting-jsr223")
}
}
val mainClassName = "com.soywiz.ktcc.cli.CLI"
val jsCompilations = kotlin.targets["js"].compilations
open class GenerateSourcesTask : DefaultTask() {
private val rootDir = project.rootDir
private val version = project.version
private val GENERATED_DO_NOT_MODIFY = "// GENERATED. Do not modify"
private fun String.escapeTripleQuote(): String {
return this.replace("$", "\${DOLLAR}").replace("\"\"\"", "\${TRIPLET}")
}
private operator fun File.get(key: String) = File(this, key)
private var File.textContent get() = this.readText(); set(value) = run { this.writeText(value) }
fun generateIncludes(): String {
val lines = arrayListOf<String>()
lines += GENERATED_DO_NOT_MODIFY
lines += "package com.soywiz.ktcc.headers"
lines += "private val DOLLAR = '$'"
lines += "private val TRIPLET = \"\\\"\\\"\\\"\""
lines += "val CStdIncludes = CIncludes().apply {"
val includeDir = File(rootDir, "include").absoluteFile
for (file in includeDir.walkTopDown().toList().sortedBy { it.absolutePath }) {
if (!file.name.endsWith(".h")) continue
val ktFile = File(file.absolutePath.removeSuffix(".h") + ".kt")
val cFile = File(file.absolutePath.removeSuffix(".h") + ".c")
//println(file)
lines += buildString {
append("FILE(\"${file.relativeTo(includeDir).path}\", \"\"\"${file.readText().escapeTripleQuote()}\"\"\"")
if (ktFile.exists()) append(", ktImpl = \"\"\"${ktFile.readText().escapeTripleQuote()}\"\"\"")
if (cFile.exists()) append(", cImpl = \"\"\"${cFile.readText().escapeTripleQuote()}\"\"\"")
append(")")
}
}
lines += "}.map.toMap()"
lines += ""
return lines.joinToString("\n")
}
@TaskAction
fun action() {
File(rootDir, "build/gen/kotlin/com/soywiz/ktcc/internal/version.kt").also { it.parentFile.mkdirs() }.textContent = "package com.soywiz.ktcc.internal\n\ninternal val KTCC_VERSION = \"$version\""
File(rootDir, "build/gen/kotlin/com/soywiz/ktcc/headers/CStdIncludesGenerated.kt").also { it.parentFile.mkdirs() }.writeText(generateIncludes())
File(rootDir, "build/gen/kotlin/com/soywiz/ktcc/gen/KotlinGen.kt").also { it.parentFile.mkdirs() }.writeText(
"$GENERATED_DO_NOT_MODIFY\n" +
"package com.soywiz.ktcc.gen\n\n" +
"private val DOLLAR = '$'\n" +
"private val TRIPLET = \"\\\"\\\"\\\"\"\n" +
"val KotlinRuntime = \"\"\"${File(rootDir, "src/commonMain/kotlin/Runtime.kt").readText().escapeTripleQuote()}\"\"\"\n" +
"val KotlinRuntimeJvm = \"\"\"${File(rootDir, "src/jvmMain/kotlin/RuntimeJvm.kt").readText().escapeTripleQuote()}\"\"\"\n"
)
}
}
tasks {
val generateSources by creating(GenerateSourcesTask::class)
//val runDceJsKotlin = named<KotlinJsDce>("runDceJsKotlin").get()
val jvmJar by getting(Jar::class)
val compileKotlinJvm by getting(KotlinCompile::class)
compileKotlinJvm.dependsOn(generateSources)
//println("compileKotlinJvm=${compileKotlinJvm::class}")
val fatJar by creating(Jar::class) {
duplicatesStrategy = org.gradle.api.file.DuplicatesStrategy.INCLUDE
archiveBaseName.set("${project.name}-all")
//archiveVersion.set(null as String?)
archiveVersion.set("")
archiveAppendix.set("")
manifest {
attributes("Main-Class" to mainClassName)
}
//println(configurations.names.toList())
from(provider { configurations["jvmRuntimeClasspath"].map { if (it.isDirectory) it else zipTree(it) } })
with(jvmJar)
}
//val jsWebResourcesDce by creating(Copy::class) {
// dependsOn(runDceJsKotlin)
// into(file("docs"))
// includeEmptyDirs = false
// from(kotlin.sourceSets["jsMain"].resources)
// from(kotlin.sourceSets["commonMain"].resources)
//}
val jsWebResources by creating(Copy::class) {
dependsOn("jsMainClasses")
into(file("docs"))
includeEmptyDirs = false
from(kotlin.sourceSets["jsMain"].resources)
from(kotlin.sourceSets["commonMain"].resources)
}
//val jsWebDce by creating(Copy::class) {
// dependsOn(jsWebResourcesDce)
// into(file("docs"))
// includeEmptyDirs = false
// exclude("**/*.kjsm", "**/*.kotlin_metadata", "**/*.kotlin_module", "**/*.MF", "**/*.meta.js", "**/*.map")
// from(named("compileProductionExecutableKotlinJs").get().outputs)
//}
val jsWeb by creating(Copy::class) {
dependsOn(jsWebResources)
into(file("docs"))
includeEmptyDirs = false
exclude("**/*.kjsm", "**/*.kotlin_metadata", "**/*.kotlin_module", "**/*.MF", "**/*.meta.js", "**/*.map")
//from(named("compileProductionExecutableKotlinJs").get().outputs)
from(named("compileDevelopmentExecutableKotlinJs").get().outputs)
}
val buildDockerImage by creating {
afterEvaluate {
dependsOn("linkReleaseExecutableLinuxX64")
}
doLast {
exec { commandLine = listOf("docker", "build", ".", "-t", "soywiz/ktcc:latest") }
}
}
val buildDockerImageAndPublish by creating {
dependsOn("buildDockerImage")
doLast {
exec { commandLine = listOf("docker", "push", "soywiz/ktcc:latest") }
}
}
project.tasks.withType(ProcessResources::class) {
this.dependsOn(generateSources)
//println("resources=$this : ${this::class}")
}
val prepare by creating {
dependsOn(generateSources)
}
}
afterEvaluate {
tasks.getByName("linuxX64Test").enabled = false
tasks.getByName("macosArm64Test").enabled = false
tasks.getByName("macosX64Test").enabled = false
tasks.getByName("mingwX64Test").enabled = false
}
//compilations.all {
// kotlinOptions {
// freeCompilerArgs = ["-progressive", "-Xskip-metadata-version-check"]
// }
//}
rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin::class.java) {
//rootProject.the<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>().download = false
}
tasks.withType(Test::class.java).all {
testLogging {
setEvents(setOf(TestLogEvent.PASSED,TestLogEvent.SKIPPED,TestLogEvent.FAILED,TestLogEvent.STANDARD_OUT,TestLogEvent.STANDARD_ERROR))
//setEvents(setOf("skipped", "failed", "standardError"))
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
idea {
module {
excludeDirs = excludeDirs + setOf(file(".gradle"), file("@old"), file("doc"), file("docs"), file("samples"), file("gradle"), file("build"), file("include"), file(".idea"), file(".github"), file("temp"), file("build/gen"))
}
}