-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.gradle.kts
209 lines (183 loc) · 7.62 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
import java.io.ByteArrayOutputStream
plugins {
val kotlinVersion = "2.0.0"
id("org.springframework.boot") version "3.3.1"
id("io.spring.dependency-management") version "1.1.5"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
project.group = "me.stageguard"
project.version = "2.5.2"
repositories {
maven("https://packages.jetbrains.team/maven/p/skija/maven")
mavenLocal()
mavenCentral()
gradlePluginPortal()
}
val exposedVersion = "0.32.1"
val hikariVersion = "5.0.1"
val mysqlVersion = "8.0.29"
val miraiSlf4jBridgeVersion = "1.2.0"
val skijaVersion = "0.102.0"
val ktorServerVersion = "2.0.2"
val ktormVersion = "3.5.0"
val atomicFUVersion = "0.17.3"
val host: String = System.getProperty("os.name")
val arch: String = System.getProperty("os.arch")
val targetOs = when {
host == "Mac OS X" -> "macos"
host.startsWith("Win") -> "windows"
host.startsWith("Linux") -> "linux"
else -> error("Unsupported OS: $host")
}
val targetArch = when (arch) {
"x86_64", "amd64" -> "x64"
"aarch64" -> "arm64"
else -> error("Unsupported arch: $arch")
}
dependencies {
//kotlinx utilities
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
implementation("org.jetbrains.kotlinx:atomicfu-jvm:$atomicFUVersion")
//skija
when {
host.startsWith("Windows") ->
api("io.github.humbleui:skija-windows:$skijaVersion")
host == "Mac OS X" ->
when (arch) {
"x86_64", "amd64" -> api("io.github.humbleui:skija-macos-x64:$skijaVersion")
"aarch64" -> api("io.github.humbleui:skija-macos-arm64:$skijaVersion")
else -> error("Unsupported arch: $arch")
}
host == "Linux" ->
api("io.github.humbleui:skija-linux:$skijaVersion")
else -> error("Unsupported platform: $host")
}
//database related lib
implementation("org.ktorm:ktorm-core:${ktormVersion}")
implementation("mysql:mysql-connector-java:$mysqlVersion")
implementation("com.zaxxer:HikariCP:$hikariVersion")
// onebot required
implementation("com.mikuac:shiro:2.2.8")
implementation("org.springframework.boot:spring-boot-starter-websocket:3.3.1")
//network
implementation("io.ktor:ktor-server-netty:$ktorServerVersion")
implementation("io.ktor:ktor-client-core:$ktorServerVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorServerVersion")
//apache utilities
implementation("commons-io:commons-io:2.11.0")
implementation("commons-codec:commons-codec:1.15")
implementation("org.apache.commons:commons-math3:3.6.1")
implementation("org.apache.commons:commons-compress:1.21")
implementation("org.tukaani:xz:1.9")
//javascript engine
implementation("org.mozilla:rhino:1.7.14")
//test suite
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.5.21")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
implementation(kotlin("stdlib-jdk8"))
}
kotlin {
sourceSets {
all {
languageSettings {
optIn("kotlin.RequiresOptIn")
optIn("kotlin.ExperimentalStdlibApi")
optIn("kotlin.contracts.ExperimentalContracts")
}
}
}
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
val version: Task by tasks.creating {
group = "verification"
file("$projectDir/version")
.writeText("OsuMapSuggester-${targetOs}-${targetArch}-${project.version}")
}
val checkCargo: Task by tasks.creating {
group = "build"
project.exec {
commandLine("cargo", "--version")
}.assertNormalExitValue()
}
val buildJniNative: Task by tasks.creating {
group = "build"
dependsOn(checkCargo)
project.exec {
workingDir("$projectDir/rosu-pp-jni/")
commandLine("cargo", "build", "--color=always", "--release")
}.assertNormalExitValue()
val libFile = when {
host.startsWith("Windows") -> "rosu_pp.dll"
host == "Mac OS X" -> "librosu_pp.dylib"
host == "Linux" -> "librosu_pp.so"
else -> throw Error("Unsupported platform: $host")
}
val buildOutputLib = file("$projectDir/rosu-pp-jni/target/release/$libFile")
buildOutputLib.copyTo(file("$projectDir/src/main/resources/$libFile"), overwrite = true)
}
val generateJniHeaders: Task by tasks.creating {
group = "build"
dependsOn(tasks.getByName("compileKotlin"))
// For caching
val path = "build/generated/jni"
inputs.dir("src/main/kotlin")
outputs.dir(path)
doLast {
val javaHome = org.gradle.internal.jvm.Jvm.current().javaHome
val javap = javaHome.resolve("bin").walk().firstOrNull { it.name.startsWith("javap") }?.absolutePath ?: error("javap not found")
val javac = javaHome.resolve("bin").walk().firstOrNull { it.name.startsWith("javac") }?.absolutePath ?: error("javac not found")
val buildDir = file("build/classes/kotlin/main")
val tmpDir = file("build/tmp/jvmJni").apply { mkdirs() }
val bodyExtractingRegex = """^.+\Rpublic \w* ?class ([^\s]+).*\{\R((?s:.+))\}\R$""".toRegex()
val nativeMethodExtractingRegex = """.*\bnative\b.*""".toRegex()
buildDir.walkTopDown()
.filter { "META" !in it.absolutePath }
.forEach { file ->
if (!file.isFile) return@forEach
val output = ByteArrayOutputStream().use {
project.exec {
commandLine(javap, "-private", "-cp", buildDir.absolutePath, file.absolutePath)
standardOutput = it
}.assertNormalExitValue()
it.toByteArray().decodeToString()
}
val (qualifiedName, methodInfo) = bodyExtractingRegex.find(output)?.destructured ?: return@forEach
val lastDot = qualifiedName.lastIndexOf('.')
val packageName = qualifiedName.substring(0, lastDot)
val className = qualifiedName.substring(lastDot+1, qualifiedName.length)
val nativeMethods =
nativeMethodExtractingRegex.findAll(methodInfo).map { it.groups }.flatMap { it.asSequence().mapNotNull { group -> group?.value } }.toList()
if (nativeMethods.isEmpty()) return@forEach
val source = buildString {
appendLine("package $packageName;")
appendLine("public class $className {")
for (method in nativeMethods) {
if ("()" in method) appendLine(method)
else {
val updatedMethod = StringBuilder(method).apply {
var count = 0
var i = 0
while (i < length) {
if (this[i] == ',' || this[i] == ')') insert(i, " arg${count++}".also { i += it.length + 1 })
else i++
}
}
appendLine(updatedMethod)
}
}
appendLine("}")
}
val outputFile = tmpDir.resolve(packageName.replace(".", "/")).apply { mkdirs() }.resolve("$className.java").apply { delete() }.apply { createNewFile() }
outputFile.writeText(source)
project.exec {
commandLine(javac, "-h", path, outputFile.absolutePath)
}.assertNormalExitValue()
}
}
}