-
Notifications
You must be signed in to change notification settings - Fork 29
/
build.gradle
256 lines (231 loc) · 8.69 KB
/
build.gradle
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
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
plugins {
id 'java-library'
id 'maven-publish'
id 'signing'
id "de.undercouch.download" version "4.1.1"
}
group = 'io.github.kawamuray.wasmtime'
version = '0.19.0'
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
implementation 'org.slf4j:slf4j-api:1.7.25'
testImplementation 'junit:junit:4.13'
testRuntimeOnly 'ch.qos.logback:logback-classic:1.2.3'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
withJavadocJar()
withSourcesJar()
}
javadoc.options.addStringOption('Xdoclint:none', '-quiet')
compileJava {
options.compilerArgs << '-parameters'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
test {
testLogging {
// set options for log level LIFECYCLE
events TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_OUT
exceptionFormat TestExceptionFormat.FULL
showExceptions true
showCauses true
showStackTraces true
showStandardStreams true
}
}
static def jniLibOsClassifier() {
def os = System.getProperty("os.name").toLowerCase()
if (os.contains("linux")) {
return "linux"
}
if (os.contains("mac os") || os.contains("darwin")) {
return "macos"
}
if (os.contains("windows")){
return "windows"
}
throw new RuntimeException("platform not supported: " + System.getProperty("os.name"))
}
static def jniLibArchClassifier() {
def target = rustTargetTriple() ?: ""
// Use the first part of the rust target triple as the arch classifier if set, otherwise assume "x86_64"
return target ? target.split("-")[0] : "x86_64"
}
static def rustTargetTriple() {
System.getenv("JNILIB_RUST_TARGET")
}
sourcesJar {
dependsOn 'metaProperties', 'copyJniLib'
}
sourceSets {
main {
resources {
srcDirs += [ "build/metaProperties", "build/jni-libs" ]
}
}
}
processResources {
dependsOn 'metaProperties', 'copyJniLib'
}
task metaProperties() {
def output = new File(project.buildDir, "metaProperties/wasmtime-java-meta.properties")
outputs.file(output)
doLast {
def props = new Properties()
props.setProperty("jnilib.version", "${project.version}")
props.store(output.newOutputStream(), null)
}
}
def javah4xVersion = '0.9.0'
task downloadJavah4x(type: Download) {
src "https://github.com/kawamuray/javah4x/releases/download/v${javah4xVersion}/javah4x-bin-${javah4xVersion}.zip"
dest new File(project.buildDir, "tmp")
overwrite false
}
task unzipJavah4x(type: Copy) {
dependsOn 'downloadJavah4x'
from zipTree(new File(project.buildDir, "tmp/javah4x-bin-${javah4xVersion}.zip"))
into new File(project.buildDir, "tmp")
}
task generateJniInterfaces(type:JavaExec) {
def targets = [
'io.github.kawamuray.wasmtime.Engine',
'io.github.kawamuray.wasmtime.Func',
'io.github.kawamuray.wasmtime.Instance',
'io.github.kawamuray.wasmtime.Linker',
'io.github.kawamuray.wasmtime.Memory',
'io.github.kawamuray.wasmtime.Global',
'io.github.kawamuray.wasmtime.Module',
'io.github.kawamuray.wasmtime.Store',
'io.github.kawamuray.wasmtime.Config',
'io.github.kawamuray.wasmtime.Caller',
'io.github.kawamuray.wasmtime.wasi.WasiCtx',
'io.github.kawamuray.wasmtime.wasi.WasiCtxBuilder']
dependsOn 'unzipJavah4x', 'compileJava'
// JNI shared object isn't available at this stage
environment 'WASMTIME_JNI_LOAD_DISABLED', '1'
// The downloaded executable is actually a shell script followed by JAR content.
// Avoid executing it as a binary and use it as a JAR in classpath to make it work not only on Unix platform
// but also on Windows.
classpath "${project.buildDir}/tmp/javah4x-${javah4xVersion}"
// Giving `sourceSets.main.runtimeClasspath` directly causes implicit dependency for `classes` task to be
// added from this task, causing cyclic dependency. We only need compiled java classes so converting
// existing entries to path and collect so it doesn't adds extra dependency.
classpath sourceSets.main.runtimeClasspath.collect { it.path }
main = 'javah4x.Javah4x'
args(['rust', './wasmtime-jni/src'] + targets)
// Manually compare the source (class files) and output (rust sources) to run this task only when any
// update is made on classes.
outputs.upToDateWhen {
def smallestGeneratedTimestamp = targets.collect {
new File(project.rootDir, "wasmtime-jni/src/${it.replace(".", "_")}/mod.rs").lastModified()
}.min()
def largestSourceTimestamp = targets.collect {
def path = "classes/java/main/" + it.replace(".", "/") + ".class"
new File(project.buildDir, path).lastModified()
}.max()
smallestGeneratedTimestamp >= largestSourceTimestamp
}
}
task buildJniLib(type:Exec) {
dependsOn 'generateJniInterfaces'
workingDir './wasmtime-jni'
def args = ['cargo', 'build', '--release']
def triple = rustTargetTriple()
if (triple != null) {
args += ['--target', triple]
}
commandLine args
}
task copyJniLib(type: Copy) {
dependsOn 'buildJniLib'
def targetDir = rustTargetTriple() ?: ""
from "wasmtime-jni/target/$targetDir/release"
include '*.so', '*.dylib', "*.dll"
rename "^(lib)?wasmtime_jni", "\$1wasmtime_jni_${project.version}_${jniLibOsClassifier()}_${jniLibArchClassifier()}"
into new File(project.buildDir, "jni-libs")
}
task universalJar(type: Jar) {
dependsOn 'jar'
archiveClassifier.set("universal")
from(zipTree(jar.outputs.files.singleFile)) {
// We use libs gathered from different OS build environments so don't use one built in this build.
exclude "libwasmtime_jni_*"
exclude "wasmtime_jni_*"
}
from new File(project.buildDir, "jni-libs")
}
static def addDependencyNodes(node, deps, scope) {
deps.each {
def dependencyNode = node.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', scope)
}
}
publishing {
repositories {
maven {
url "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username findProperty("sonatypeUsername")
password findProperty("sonatypePassword")
}
}
}
publications {
mavenJava(MavenPublication) {
artifact source: universalJar, classifier: null
artifact sourcesJar
artifact javadocJar
pom {
name = 'wasmtime-java'
description = 'Java or JVM-language binding for Wasmtime'
url = 'https://github.com/kawamuray/wasmtime-java'
scm {
url = 'https://github.com/kawamuray/wasmtime-java.git'
connection = 'scm:https://github.com/kawamuray/wasmtime-java.git'
developerConnection = 'scm:git@github.com:kawamuray/wasmtime-java.git'
}
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'https://www.apache.org/licenses/LICENSE-2.0'
}
}
developers {
developer {
name = 'Yuto Kawamura'
email = 'kawamuray.dadada@gmail.com'
}
}
withXml { xml ->
// As we can't use `from components.java`, we have to build dependencies list in .pom manually.
def node = xml.asNode().appendNode('dependencies')
addDependencyNodes(node, project.configurations.api.allDependencies, 'compile')
addDependencyNodes(node, project.configurations.implementation.allDependencies
.findAll { !project.configurations.api.allDependencies.contains(it) }, 'runtime')
}
}
}
}
signing {
required { gradle.taskGraph.hasTask("publish") }
sign publishing.publications.mavenJava
}
}