-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
255 lines (210 loc) · 8.07 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
import org.gradle.internal.extensions.stdlib.capitalized
import java.io.PrintWriter
import java.nio.file.Files
import java.nio.file.Paths
plugins {
alias(libs.plugins.kotlinJvm)
alias(libs.plugins.changelog)
alias(libs.plugins.ktlint)
}
group = "com.jzbrooks"
version = "0.0.5"
repositories {
mavenCentral()
google()
}
val r8: Configuration by configurations.creating
val targets = mapOf(
"macos" to listOf("arm64"),
"windows" to listOf("x64", "arm64"),
"linux" to listOf("x64", "arm64"),
)
val macosArm64RuntimeOnly: Configuration by configurations.creating {
extendsFrom(configurations["runtimeOnly"])
}
val windowsX64RuntimeOnly: Configuration by configurations.creating {
extendsFrom(configurations["runtimeOnly"])
}
val windowsArm64RuntimeOnly: Configuration by configurations.creating {
extendsFrom(configurations["runtimeOnly"])
}
val linuxX64RuntimeOnly: Configuration by configurations.creating {
extendsFrom(configurations["runtimeOnly"])
}
val linuxArm64RuntimeOnly: Configuration by configurations.creating {
extendsFrom(configurations["runtimeOnly"])
}
dependencies {
implementation(libs.skiko)
implementation(libs.vgo)
implementation(libs.vgoCore)
macosArm64RuntimeOnly(libs.skikoMacArm)
linuxArm64RuntimeOnly(libs.skikoLinuxArm)
linuxX64RuntimeOnly(libs.skikoLinuxIntel)
windowsArm64RuntimeOnly(libs.skikoWindowsArm)
windowsX64RuntimeOnly(libs.skikoWindowsIntel)
r8(libs.r8)
testImplementation(platform(libs.junitBom))
testImplementation(libs.junitJupiter)
}
sourceSets {
main {
kotlin.srcDir("src/generated/kotlin")
}
}
tasks {
test {
useJUnitPlatform()
}
val generateConstants by registering {
finalizedBy("compileKotlin")
outputs.files("$projectDir/src/generated/kotlin/com/jzbrooks/vat/BuildConstants.kt")
doLast {
val generatedDirectory = Paths.get("$projectDir/src/generated/kotlin/com/jzbrooks/vat")
Files.createDirectories(generatedDirectory)
val generatedFile = generatedDirectory.resolve("BuildConstants.kt")
PrintWriter(generatedFile.toFile()).use { output ->
val buildConstantsClass =
buildString {
appendLine(
"""
|package com.jzbrooks.vat
|
|internal object BuildConstants {
""".trimMargin(),
)
val vatProperties = mapOf("VERSION" to version)
for (property in vatProperties) {
append(" const val ")
append(property.key.uppercase())
append(" = \"")
append(property.value)
appendLine('"')
}
appendLine("}")
}
output.write(buildConstantsClass)
}
}
}
named("runKtlintCheckOverMainSourceSet") {
dependsOn(generateConstants)
}
named("runKtlintFormatOverMainSourceSet") {
dependsOn(generateConstants)
}
compileKotlin {
dependsOn(generateConstants)
}
for ((os, architectures) in targets) {
for (arch in architectures) {
val target = "$os${arch.capitalized()}"
register<Jar>("${target}Jar") {
manifest {
attributes["Main-Class"] = "com.jzbrooks.vat.MainKt"
attributes["Bundle-Version"] = version
}
archiveBaseName.set("vat-$os-$arch")
destinationDirectory.set(layout.buildDirectory.dir("libs/debug"))
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
val sourceClasses = sourceSets.main.get().output.classesDirs
inputs.files(sourceClasses)
from(sourceClasses.files)
from(configurations["${target}RuntimeOnly"].asFileTree.files.map(::zipTree))
from(configurations.runtimeClasspath.get().asFileTree.files.map(::zipTree))
exclude(
"**/*.kotlin_metadata",
"**/*.kotlin_module",
"**/*.kotlin_builtins",
"**/module-info.class",
"META-INF/maven/**",
"META-INF/*.version",
"META-INF/LICENSE*",
"META-INF/LGPL2.1",
"META-INF/DEPENDENCIES",
"META-INF/AL2.0",
"META-INF/BCKEY.DSA",
"META-INF/BC2048KE.DSA",
"META-INF/BCKEY.SF",
"META-INF/BC2048KE.SF",
"**/NOTICE*",
"javax/activation/**",
"xsd/catalog.xml",
)
}
register<JavaExec>("${target}Optimize") {
description = "Runs r8 on the jar application."
group = "build"
inputs.file("build/libs/debug/vat-$os-$arch-$version.jar")
outputs.file("build/libs/vat-$os-$arch-$version.jar")
val javaHome = System.getProperty("java.home")
classpath(r8)
mainClass = "com.android.tools.r8.R8"
args(
"--release",
"--classfile",
"--lib",
javaHome,
"--output",
"build/libs/vat-$os-$arch.jar",
"--pg-conf",
"optimize.pro",
"build/libs/debug/vat-$os-$arch-$version.jar",
)
dependsOn("${target}Jar")
}
if (os != "windows") {
val binaryFileProp = layout.buildDirectory.file("libs/vat-$os-$arch")
register("${target}Binary") {
description = "Prepends shell script in the jar to improve CLI"
group = "build"
dependsOn("${target}Optimize")
inputs.file("build/libs/vat-$os-$arch.jar")
outputs.file(binaryFileProp)
doLast {
val binaryFile = binaryFileProp.get().asFile
binaryFile.parentFile.mkdirs()
binaryFile.delete()
binaryFile.appendText("#!/bin/sh\n\nexec java \$JAVA_OPTS -jar \$0 \"\$@\"\n\n")
file("build/libs/vat-$os-$arch.jar").inputStream()
.use { binaryFile.appendBytes(it.readBytes()) }
binaryFile.setExecutable(true, false)
}
}
}
}
}
val osName = System.getProperty("os.name")
val targetOs =
when {
osName == "Mac OS X" -> "macos"
osName.startsWith("Win") -> "windows"
osName.startsWith("Linux") -> "linux"
else -> error("Unsupported OS: $osName")
}
val osArch = System.getProperty("os.arch")
val targetArch =
when (osArch) {
"x86_64", "amd64" -> "x64"
"aarch64" -> "arm64"
else -> error("Unsupported arch: $osArch")
}
jar {
description = "Runs r8 on the jar application for the current system ($targetOs-$targetArch)."
group = "build"
dependsOn("$targetOs${targetArch.capitalized()}Jar")
}
val optimize by registering {
description = "Runs r8 on the jar application for the current system ($targetOs-$targetArch)."
group = "build"
dependsOn("$targetOs${targetArch.capitalized()}Optimize")
}
if (targetOs != "windows") {
val binary by registering {
description =
"Prepends shell script in the jar to improve CLI for the current system ($targetOs-$targetArch)"
group = "build"
dependsOn("$targetOs${targetArch.capitalized()}Binary")
}
}
}