-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.gradle.kts
298 lines (252 loc) · 11 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
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
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.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJsDce
import org.jetbrains.kotlin.konan.properties.*
import org.jetbrains.kotlin.konan.util.*
//buildscript {
// repositories {
// mavenLocal()
// mavenCentral()
// maven { url = uri("https://kotlin.bintray.com/kotlin-dev") }
// }
// val kotlinVersion = "1.3.50-release-105"
// dependencies {
// classpath("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:$kotlinVersion")
// }
//}
plugins {
kotlin("multiplatform") version "1.6.21" apply true
//kotlin("multiplatform") version "1.3.41" apply true
//kotlin("multiplatform") version "1.3.50-release-105"
//id("kotlinx-serialization") version "1.3.41"
idea
}
//apply(plugin = "org.jetbrains.kotlin.multiplatform")
apply(plugin = "kotlin-dce-js")
repositories {
mavenLocal()
mavenCentral()
maven { url = uri("https://kotlin.bintray.com/kotlinx") }
//maven { url = uri("https://kotlin.bintray.com/kotlin-dev") }
}
fun KotlinOnlyTarget<*>.mainDependencies(block: KotlinDependencyHandler.() -> Unit) {
this.compilations["main"].dependencies(block)
}
fun KotlinOnlyTarget<*>.testDependencies(block: KotlinDependencyHandler.() -> Unit) {
this.compilations["test"].dependencies(block)
}
operator fun File.get(key: String) = File(this, key)
var File.textContent get() = this.readText(); set(value) = run { this.writeText(value) }
//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")
file("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\""
//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")
}
}
//metadata {
// mainDependencies {
// implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
// }
// testDependencies {
// implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
// implementation("org.jetbrains.kotlin:kotlin-test-common")
// }
//}
//common {
//}
jvm {
configureAll()
}
js {
configureAll()
browser()
compilations.all {
kotlinOptions {
sourceMap = true
metaInfo = true
moduleKind = "umd"
}
}
}
fun KotlinNativeTarget.configureNative() {
configureAll()
binaries {
executable()
}
}
if (enableNative) {
macosArm64 { configureNative() }
macosX64 { configureNative() }
linuxX64 { configureNative() }
mingwX64 { configureNative() }
sourceSets {
val nativeCommonMain = this.create("nativeCommonMain")
val nativeCommonTest = this.create("nativeCommonTest")
configure(listOf(this.getByName("macosX64Main"), this.getByName("macosArm64Main"), this.getByName("linuxX64Main"), this.getByName("mingwX64Main"))) {
dependsOn(nativeCommonMain)
}
}
}
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
val GENERATED_DO_NOT_MODIFY = "// GENERATED. Do not modify"
fun String.escapeTripleQuote(): String {
return this.replace("$", "\${DOLLAR}").replace("\"\"\"", "\${TRIPLET}")
}
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")
}
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 runDceJsKotlin = named<KotlinJsDce>("runDceJsKotlin").get()
create<Jar>("fatJar") {
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)
}
afterEvaluate {
for (it in (kotlin.targets["jvm"].compilations["main"] as KotlinJvmCompilation).runtimeDependencyFiles) {
from(if (it.isDirectory) it else zipTree(it))
}
}
with(named<Jar>("jvmJar").get())
}
val jsWebResourcesDce = create<Copy>("jsWebResourcesDce") {
dependsOn(runDceJsKotlin)
into(rootDir["docs"])
includeEmptyDirs = false
from(kotlin.sourceSets["jsMain"].resources)
from(kotlin.sourceSets["commonMain"].resources)
}
val jsWebResources = create<Copy>("jsWebResources") {
dependsOn("jsMainClasses")
into(rootDir["docs"])
includeEmptyDirs = false
from(kotlin.sourceSets["jsMain"].resources)
from(kotlin.sourceSets["commonMain"].resources)
}
create<Copy>("jsWebDce") {
dependsOn(jsWebResourcesDce)
into(rootDir["docs"])
includeEmptyDirs = false
exclude("**/*.kjsm", "**/*.kotlin_metadata", "**/*.kotlin_module", "**/*.MF", "**/*.meta.js", "**/*.map")
afterEvaluate {
from(runDceJsKotlin.destinationDir)
}
}
create<Copy>("jsWeb") {
dependsOn(jsWebResources)
into(rootDir["docs"])
includeEmptyDirs = false
exclude("**/*.kjsm", "**/*.kotlin_metadata", "**/*.kotlin_module", "**/*.MF", "**/*.meta.js", "**/*.map")
from(jsCompilations["main"].output.allOutputs)
afterEvaluate {
for (f in (jsCompilations["main"] as KotlinJsCompilation).runtimeDependencyFiles) if (f.exists() && !f.isDirectory()) from(zipTree(f.absolutePath))
}
}
create<Task>("buildDockerImage") {
afterEvaluate {
dependsOn("linkReleaseExecutableLinuxX64")
}
doLast {
exec { commandLine = listOf("docker", "build", ".", "-t", "soywiz/ktcc:latest") }
}
}
create<Task>("buildDockerImageAndPublish") {
dependsOn("buildDockerImage")
doLast {
exec { commandLine = listOf("docker", "push", "soywiz/ktcc:latest") }
}
}
}
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"))
}
}