forked from openjdk/jextract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
263 lines (217 loc) · 8 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
257
258
259
260
261
262
263
import org.apache.tools.ant.taskdefs.condition.Os
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
plugins {
id "java"
}
sourceSets {
test {
java {
srcDirs = ['test/jtreg', file("test/jtreg/generator").listFiles(), 'test/lib', 'test/testng']
// exclude all test files from compilation. Jtreg compiles these for us when tests run
exclude "**/*"
}
}
}
dependencies {
// add jtreg jars as dependencies of tests
testImplementation fileTree(dir: findProperty("jtreg_home") + "/lib/", include: "*.jar")
}
def static checkPath(String p) {
if (!Files.exists(Path.of(p))) {
throw new IllegalArgumentException("Error: the path ${p} does not exist");
}
}
def llvm_home = project.property("llvm_home")
checkPath(llvm_home)
checkPath("${llvm_home}/lib/clang")
def clang_versions = new File("${llvm_home}/lib/clang/").list();
if (clang_versions.length == 0) {
throw new IllegalArgumentException("Could not detect clang version." +
" Make sure a ${llvm_home}/lib/clang/<VERSION> directory exists")
}
def clang_version = clang_versions[0]
def jextract_version = "22"
def jmods_dir = "$buildDir/jmods"
def jextract_jmod_file = "$jmods_dir/org.openjdk.jextract.jmod"
def jextract_jmod_inputs = "$buildDir/jmod_inputs"
def jextract_app_dir = "$buildDir/jextract"
def jextract_runtime_dir = "$jextract_app_dir/runtime"
def jextract_bin_dir = "$jextract_app_dir/bin"
def clang_include_dir = "${llvm_home}/lib/clang/${clang_version}/include"
checkPath(clang_include_dir)
def os_lib_dir = Os.isFamily(Os.FAMILY_WINDOWS)? "bin" : "lib"
def os_script_extension = Os.isFamily(Os.FAMILY_WINDOWS)? ".bat" : ""
def libclang_dir = "${llvm_home}/${os_lib_dir}"
checkPath(libclang_dir)
repositories {
mavenCentral()
}
compileJava {
options.release = 22
options.compilerArgs << "--enable-preview"
options.fork = true
options.forkOptions.executable = "${jdk22_home}/bin/javac"
}
jar {
archiveBaseName = 'org.openjdk.jextract'
archiveVersion = project.version
}
task copyLibClang(type: Sync) {
into("$buildDir/jmod_inputs")
from("${libclang_dir}") {
include("*clang.*")
include("libLLVM.*")
exclude("clang.exe")
into("libs")
}
from("$clang_include_dir") {
include("*.h")
into("conf/jextract")
}
}
task createJextractJmod(type: Exec) {
dependsOn jar, copyLibClang
// if these inputs or outputs change, gradle will rerun the task
inputs.file(jar.archiveFile.get())
inputs.dir("$jextract_jmod_inputs")
outputs.file(jextract_jmod_file)
doFirst {
delete(jextract_jmod_file)
}
executable = "${jdk22_home}/bin/jmod"
args = [
"create",
"--module-version=$jextract_version",
"--class-path=" + jar.archiveFile.get(),
"--libs=$jextract_jmod_inputs/libs",
"--conf=$jextract_jmod_inputs/conf",
"${jextract_jmod_file}"
]
}
task createJextractImage(type: Exec) {
dependsOn createJextractJmod
// if these inputs or outputs change, gradle will rerun the task
inputs.file("$jextract_jmod_file")
outputs.dir(jextract_app_dir)
def quote_jlink_opts = Os.isFamily(Os.FAMILY_WINDOWS)?
'\\"--enable-native-access=org.openjdk.jextract\\" \\"--enable-preview\""' :
'"--enable-native-access=org.openjdk.jextract" "--enable-preview"'
doFirst {
delete(jextract_app_dir)
project.mkdir "${jextract_bin_dir}"
}
executable = "${jdk22_home}/bin/jlink"
args = [
"--module-path=$jmods_dir",
"--add-modules=org.openjdk.jextract",
"--output=${jextract_runtime_dir}",
"--strip-debug", "--no-man-pages", "--no-header-files",
"--add-options",
"${quote_jlink_opts}"
]
doLast {
// Add launcher scripts
Path unixOut = Path.of("${jextract_bin_dir}/jextract");
Files.copy(Path.of("$projectDir/src/main/jextract"), unixOut);
if (unixOut.getFileSystem().supportedFileAttributeViews().contains("posix")) {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(unixOut);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(unixOut, perms);
}
Files.copy(Path.of("$projectDir/src/main/jextract.bat"), Path.of("${jextract_bin_dir}/jextract.bat"))
}
}
// build the jextract image when the build or assemble task is run
assemble.dependsOn(createJextractImage)
// very simple integration test for generated jextract
task verify(type: Exec) {
dependsOn createJextractImage
executable = "${jextract_bin_dir}/jextract${os_script_extension}"
args = [ "test.h", "--output", "$buildDir/integration_test" ]
}
// jlink a JDK image with org.openjdk.jextract for testing
task createRuntimeImageForTest(type: Exec) {
dependsOn createJextractJmod
def out_dir = "$buildDir/jextract-jdk-test-image"
// if these inputs or outputs change, gradle will rerun the task
inputs.file("$jextract_jmod_file")
outputs.dir(out_dir)
doFirst {
delete(out_dir)
}
executable = "${jdk22_home}/bin/jlink"
args = [
"--module-path=$jmods_dir" + File.pathSeparator + "$jdk22_home/jmods",
"--add-modules=ALL-MODULE-PATH",
"--output=$out_dir",
]
}
task cmakeConfigure(type: Exec) {
executable = "cmake"
args = [
"-B", "$buildDir/testlib-build",
"-S", "$projectDir/test/test-support",
"-DTEST_SOURCE_ROOT:FILEPATH=$projectDir/test",
"-DCMAKE_BUILD_TYPE:STRING=Release",
"-DCMAKE_INSTALL_PREFIX:FILEPATH=$buildDir/testlib-install"
]
}
task cmakeBuild(type: Exec) {
dependsOn cmakeConfigure
executable = "cmake"
args = [
"--build", "$buildDir/testlib-build",
"--config", "Release",
"--target", "install"
]
}
void createJtregTask(String name, boolean coverage, String os_lib_dir) {
tasks.register(name, JavaExec) {
dependsOn createRuntimeImageForTest,cmakeBuild
if (findProperty("jtreg_home") == null) {
throw new GradleException("jtreg_home is not defined")
}
// e.g.: <jacoco repo>/org.jacoco.agent/target/classes/jacocoagent.jar
if (coverage && findProperty("jacoco_agent") == null) {
throw new GradleException("jacoco_agent is not defined")
}
workingDir = "$buildDir"
classpath = files(findProperty("jtreg_home") + "/lib/jtreg.jar")
args = [
"-jdk", "$buildDir/jextract-jdk-test-image",
"-nativepath:$buildDir/testlib-install/${os_lib_dir}",
"-javaoption:--enable-preview",
"-javaoption:--enable-native-access=org.openjdk.jextract,ALL-UNNAMED",
"-avm", "-conc:auto", "-verbose:summary,fail,error",
"-retain:fail,error",
]
if (coverage) {
String jacocoAgent = findProperty("jacoco_agent")
String coverageFile = "$buildDir/jacoco-run/jextract.exec"
String includes = "org.openjdk.jextract.*"
args += "-javaoption:-javaagent:$jacocoAgent=destfile=$coverageFile,includes=$includes"
}
args += "../test"
}
}
createJtregTask("jtreg", false, os_lib_dir)
createJtregTask("jtregWithCoverage", true, os_lib_dir)
tasks.register("coverage", JavaExec) {
dependsOn jtregWithCoverage
// e.g.: <jacoco repo>/org.jacoco.cli/target/org.jacoco.cli-0.8.12-SNAPSHOT-nodeps.jar
if (findProperty("jacoco_cli") == null) {
throw new GradleException("jacoco_cli is not defined")
}
classpath = files(findProperty("jacoco_cli"))
args = [
"report",
"$buildDir/jacoco-run/jextract.exec",
"--classfiles", "$buildDir/classes/java/main",
"--sourcefiles", "$projectDir/src/main/java",
"--html", "$buildDir/jacoco-report"
]
}