@@ -44,6 +44,8 @@ dependencies {
44
44
implementation(libs.truffleRuntime)
45
45
46
46
testImplementation(projects.pklCommonsTest)
47
+ testImplementation(" net.java.dev.jna:jna:5.17.0" )
48
+ testImplementation(" net.java.dev.jna:jna-platform:5.17.0" )
47
49
48
50
fun sharedLibrary (osAndArch : String ) = files(nativeLibraryOutputFiles(osAndArch))
49
51
@@ -121,137 +123,138 @@ private fun NativeImageBuild.setClasspath() {
121
123
}
122
124
123
125
val macNativeLibraryAmd64 by
124
- tasks.registering(NativeImageBuild ::class ) {
125
- outputDir = project.layout.buildDirectory.dir(" libs/macos-amd64" )
126
- imageName = executableSpec.name.map { " $it -macos-amd64" }
127
- mainClass = executableSpec.mainClass
128
- amd64()
129
- setClasspath()
130
- extraNativeImageArgs = listOf (" --shared" )
131
-
132
- setOutputFiles(" macos-amd64" )
133
- }
126
+ tasks.registering(NativeImageBuild ::class ) {
127
+ outputDir = project.layout.buildDirectory.dir(" libs/macos-amd64" )
128
+ imageName = executableSpec.name.map { " $it -macos-amd64" }
129
+ mainClass = executableSpec.mainClass
130
+ amd64()
131
+ setClasspath()
132
+ extraNativeImageArgs = listOf (" --shared" )
133
+
134
+ setOutputFiles(" macos-amd64" )
135
+ }
134
136
135
137
val macNativeLibraryAarch64 by
136
- tasks.registering(NativeImageBuild ::class ) {
137
- outputDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" )
138
- imageName = executableSpec.name.map { " $it -macos-aarch64" }
139
- mainClass = executableSpec.mainClass
140
- aarch64()
141
- setClasspath()
142
- extraNativeImageArgs = listOf (" --shared" )
143
-
144
- setOutputFiles(" macos-aarch64" )
145
- }
138
+ tasks.registering(NativeImageBuild ::class ) {
139
+ outputDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" )
140
+ imageName = executableSpec.name.map { " $it -macos-aarch64" }
141
+ mainClass = executableSpec.mainClass
142
+ aarch64()
143
+ setClasspath()
144
+ extraNativeImageArgs = listOf (" --shared" )
145
+
146
+ setOutputFiles(" macos-aarch64" )
147
+ }
146
148
147
149
val linuxNativeLibraryAmd64 by
148
- tasks.registering(NativeImageBuild ::class ) {
149
- outputDir = project.layout.buildDirectory.dir(" libs/linux-amd64" )
150
- imageName = executableSpec.name.map { " $it -linux-amd64" }
151
- mainClass = executableSpec.mainClass
152
- amd64()
153
- setClasspath()
154
- extraNativeImageArgs = listOf (" --shared" )
155
-
156
- setOutputFiles(" linux-amd64" )
157
- }
150
+ tasks.registering(NativeImageBuild ::class ) {
151
+ outputDir = project.layout.buildDirectory.dir(" libs/linux-amd64" )
152
+ imageName = executableSpec.name.map { " $it -linux-amd64" }
153
+ mainClass = executableSpec.mainClass
154
+ amd64()
155
+ setClasspath()
156
+ extraNativeImageArgs = listOf (" --shared" )
157
+
158
+ setOutputFiles(" linux-amd64" )
159
+ }
158
160
159
161
val linuxNativeLibraryAarch64 by
160
- tasks.registering(NativeImageBuild ::class ) {
161
- outputDir = project.layout.buildDirectory.dir(" libs/linux-aarch64" )
162
- imageName = executableSpec.name.map { " $it -linux-aarch64" }
163
- mainClass = executableSpec.mainClass
164
- aarch64()
165
- setClasspath()
166
-
167
- extraNativeImageArgs =
168
- listOf (
169
- " --shared" ,
170
- // Ensure compatibility for kernels with page size set to 4k, 16k and 64k
171
- // (e.g. Raspberry Pi 5, Asahi Linux)
172
- " -H:PageSize=65536" ,
173
- )
162
+ tasks.registering(NativeImageBuild ::class ) {
163
+ outputDir = project.layout.buildDirectory.dir(" libs/linux-aarch64" )
164
+ imageName = executableSpec.name.map { " $it -linux-aarch64" }
165
+ mainClass = executableSpec.mainClass
166
+ aarch64()
167
+ setClasspath()
168
+
169
+ extraNativeImageArgs =
170
+ listOf (
171
+ " --shared" ,
172
+ // Ensure compatibility for kernels with page size set to 4k, 16k and 64k
173
+ // (e.g. Raspberry Pi 5, Asahi Linux)
174
+ " -H:PageSize=65536" ,
175
+ )
174
176
175
- setOutputFiles(" linux-aarch64" )
176
- }
177
+ setOutputFiles(" linux-aarch64" )
178
+ }
177
179
178
180
val alpineNativeLibraryAmd64 by
179
- tasks.registering(NativeImageBuild ::class ) {
180
- outputDir = project.layout.buildDirectory.dir(" libs/alpine-linux-amd64" )
181
- imageName = executableSpec.name.map { " $it -alpine-linux-amd64" }
182
- mainClass = executableSpec.mainClass
183
- amd64()
184
- setClasspath()
185
-
186
- extraNativeImageArgs =
187
- listOf (
188
- " --shared" ,
189
- // TODO(kushal): https://github.com/oracle/graal/issues/3053
190
- " --libc=musl" ,
191
- )
181
+ tasks.registering(NativeImageBuild ::class ) {
182
+ outputDir = project.layout.buildDirectory.dir(" libs/alpine-linux-amd64" )
183
+ imageName = executableSpec.name.map { " $it -alpine-linux-amd64" }
184
+ mainClass = executableSpec.mainClass
185
+ amd64()
186
+ setClasspath()
187
+
188
+ extraNativeImageArgs =
189
+ listOf (
190
+ " --shared" ,
191
+ // TODO(kushal): https://github.com/oracle/graal/issues/3053
192
+ " --libc=musl" ,
193
+ )
192
194
193
- setOutputFiles(" alpine-linux-amd64" )
194
- }
195
+ setOutputFiles(" alpine-linux-amd64" )
196
+ }
195
197
196
198
val windowsNativeLibraryAmd64 by
197
- tasks.registering(NativeImageBuild ::class ) {
198
- outputDir = project.layout.buildDirectory.dir(" libs/windows-amd64" )
199
- imageName = executableSpec.name.map { " $it -windows-amd64" }
200
- mainClass = executableSpec.mainClass
201
- amd64()
202
- setClasspath()
203
- extraNativeImageArgs = listOf (" --shared" , " -Dfile.encoding=UTF-8" )
204
-
205
- setOutputFiles(" windows-amd64" )
206
- }
199
+ tasks.registering(NativeImageBuild ::class ) {
200
+ outputDir = project.layout.buildDirectory.dir(" libs/windows-amd64" )
201
+ imageName = executableSpec.name.map { " $it -windows-amd64" }
202
+ mainClass = executableSpec.mainClass
203
+ amd64()
204
+ setClasspath()
205
+ extraNativeImageArgs = listOf (" --shared" , " -Dfile.encoding=UTF-8" )
206
+
207
+ setOutputFiles(" windows-amd64" )
208
+ }
207
209
208
210
val assembleNative by
209
- tasks.existing {
210
- // TODO(kushal): Remove this later. Only exists to debug output files are in the graph.
211
- finalizedBy(validateNativeLibraryFilestasks)
212
- }
211
+ tasks.existing {
212
+ // TODO(kushal): Remove this later. Only exists to debug output files are in the graph.
213
+ finalizedBy(validateNativeLibraryFilestasks)
214
+ }
213
215
214
216
// TODO(kushal): Remove this later. Only exists to debug output files are in the graph.
215
- val validateNativeLibraryFilestasks by tasks.registering {
216
- val assembleTasks = mutableSetOf<TaskProvider <NativeImageBuild >>()
217
-
218
- when {
219
- buildInfo.os.isMacOsX -> {
220
- assembleTasks.add(macNativeLibraryAmd64)
221
- if (buildInfo.arch == " aarch64" ) {
222
- assembleTasks.add(macNativeLibraryAarch64)
217
+ val validateNativeLibraryFilestasks by
218
+ tasks.registering {
219
+ val assembleTasks = mutableSetOf<TaskProvider <NativeImageBuild >>()
220
+
221
+ when {
222
+ buildInfo.os.isMacOsX -> {
223
+ assembleTasks.add(macNativeLibraryAmd64)
224
+ if (buildInfo.arch == " aarch64" ) {
225
+ assembleTasks.add(macNativeLibraryAarch64)
226
+ }
223
227
}
224
- }
225
228
226
- buildInfo.os.isWindows -> {
227
- assembleTasks.add(windowsNativeLibraryAmd64)
228
- }
229
+ buildInfo.os.isWindows -> {
230
+ assembleTasks.add(windowsNativeLibraryAmd64)
231
+ }
229
232
230
- buildInfo.os.isLinux && buildInfo.arch == " aarch64" -> {
231
- assembleTasks.add(linuxNativeLibraryAarch64)
232
- }
233
+ buildInfo.os.isLinux && buildInfo.arch == " aarch64" -> {
234
+ assembleTasks.add(linuxNativeLibraryAarch64)
235
+ }
233
236
234
- buildInfo.os.isLinux && buildInfo.arch == " amd64" -> {
235
- assembleTasks.add(linuxNativeLibraryAmd64)
236
- if (buildInfo.hasMuslToolchain) {
237
- assembleTasks.add(alpineNativeLibraryAmd64)
237
+ buildInfo.os.isLinux && buildInfo.arch == " amd64" -> {
238
+ assembleTasks.add(linuxNativeLibraryAmd64)
239
+ if (buildInfo.hasMuslToolchain) {
240
+ assembleTasks.add(alpineNativeLibraryAmd64)
241
+ }
238
242
}
239
243
}
240
- }
241
244
242
- dependsOn(assembleTasks)
245
+ dependsOn(assembleTasks)
243
246
244
- doLast {
245
- for (taskProvider in assembleTasks) {
246
- val task = taskProvider.get()
247
- val outputFiles = task.outputs.files.files
247
+ doLast {
248
+ for (taskProvider in assembleTasks) {
249
+ val task = taskProvider.get()
250
+ val outputFiles = task.outputs.files.files
248
251
249
- println (" ==== Validating Native Library Files Exist ====" )
250
- println (" ${task.name} outputs:" )
251
- outputFiles.forEach { file -> println (" - ${file.absolutePath} (exists: ${file.exists()} )" ) }
252
+ println (" ==== Validating Native Library Files Exist ====" )
253
+ println (" ${task.name} outputs:" )
254
+ outputFiles.forEach { file -> println (" - ${file.absolutePath} (exists: ${file.exists()} )" ) }
255
+ }
252
256
}
253
257
}
254
- }
255
258
256
259
// Expose underlying task's outputs
257
260
private fun <T : Task > Task.wraps (other : TaskProvider <T >) {
@@ -271,31 +274,46 @@ val assembleNativeAlpineLinuxAmd64 by tasks.existing { wraps(alpineNativeLibrary
271
274
272
275
val assembleNativeWindowsAmd64 by tasks.existing { wraps(windowsNativeLibraryAmd64) }
273
276
274
- val macNativeFullLibraryAarch64 by tasks.registering(Exec ::class ) {
275
- dependsOn(macNativeLibraryAarch64)
277
+ val macNativeFullLibraryAarch64 by
278
+ tasks.registering(Exec ::class ) {
279
+ dependsOn(macNativeLibraryAarch64)
280
+
281
+ val libraryOutputDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" ).get()
282
+ val projectDir = project.layout.projectDirectory.asFile.path
283
+
284
+ workingDir = libraryOutputDir.asFile
285
+
286
+ // TODO: Make this portable.
287
+ commandLine(
288
+ " /usr/bin/cc" ,
289
+ " -shared" ,
290
+ " -o" ,
291
+ " libpkl.dylib" ,
292
+ " $projectDir /src/main/c/libpkl.c" ,
293
+ " -I$projectDir /src/main/c" ,
294
+ " -I$libraryOutputDir " ,
295
+ " -L$libraryOutputDir " ,
296
+ " -lpkl-internal-macos-aarch64" ,
297
+ )
298
+ }
276
299
277
- val libraryOutputDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" ).get()
278
- val projectDir = project.layout.projectDirectory.asFile.path
300
+ val macNativeFullLibraryAarch64Copy by
301
+ tasks.registering(Exec ::class ) {
302
+ dependsOn(macNativeFullLibraryAarch64)
279
303
280
- workingDir = libraryOutputDir.asFile
304
+ val libraryOutputDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" ).get()
305
+ val projectDir = project.layout.projectDirectory.asFile.path
281
306
282
- // TODO: Make this portable.
283
- commandLine(" /usr/bin/cc" , " -shared" ,
284
- " -o" , " libpkl.dylib" ,
285
- " $projectDir /src/main/c/libpkl.c" ,
286
- " -I$projectDir /src/main/c" ,
287
- " -I$libraryOutputDir " ,
288
- " -L$libraryOutputDir " ,
289
- " -lpkl-internal-macos-aarch64" )
290
- }
307
+ workingDir = libraryOutputDir.asFile
291
308
292
- val macNativeFullLibraryAarch64Copy by tasks.registering( Exec :: class ) {
293
- dependsOn(macNativeFullLibraryAarch64)
309
+ commandLine( " cp " , " $projectDir /src/main/c/libpkl.h " , libraryOutputDir)
310
+ }
294
311
295
- val libraryOutputDir = project.layout.buildDirectory.dir( " libs/macos-aarch64 " ).get()
296
- val projectDir = project.layout.projectDirectory.asFile.path
312
+ tasks.withType< Test > {
313
+ dependsOn(macNativeFullLibraryAarch64Copy)
297
314
298
- workingDir = libraryOutputDir.asFile
315
+ val nativeLibsDir = project.layout.buildDirectory.dir(" libs/macos-aarch64" ).get().asFile
316
+ jvmArgs(" -Djna.library.path=${nativeLibsDir.absolutePath} " )
299
317
300
- commandLine( " cp " , " $projectDir /src/main/c/libpkl.h " , libraryOutputDir )
318
+ useJUnitPlatform( )
301
319
}
0 commit comments