@@ -143,6 +143,10 @@ val activePlatforms = if (platformFilter != null) {
143143 pdfiumPlatforms
144144}
145145
146+ // Optional directory containing pre-built shim artifacts (one sub-dir per platform named native-dir-<platform>).
147+ // When set, buildShim will copy shims from here instead of compiling for non-host-compatible platforms.
148+ val prebuiltShimsDir = findProperty(" prebuiltShimsDir" )?.toString()?.let { project.file(it) }
149+
146150val pdfiumArchiveDir = layout.buildDirectory.dir(" pdfium-archives" )
147151val pdfiumNativesDir = layout.buildDirectory.dir(" generated-natives" )
148152
@@ -311,13 +315,21 @@ val buildShim by tasks.registering {
311315 outputs.dir(buildDir)
312316
313317 doLast {
314- val compatiblePlatforms = activePlatforms.keys.filter { platform ->
315- val hostIsArm64 = hostArch == " aarch64" || hostArch == " arm64"
316- val hostIsX64 = hostArch == " x86_64" || hostArch == " amd64"
318+ val hostIsArm64 = hostArch == " aarch64" || hostArch == " arm64"
319+ val hostIsX64 = hostArch == " x86_64" || hostArch == " amd64"
320+
321+ activePlatforms.keys.forEach { platform ->
317322 val platformIsArm64 = platform.endsWith(" arm64" )
318323 val platformIsX64 = platform.endsWith(" x64" )
324+ val libDir = pdfiumNativesDir.get().asFile.resolve(" natives/$platform " )
325+ val shimLibName = when {
326+ platform.startsWith(" linux" ) -> " pdfium4j_shim.so"
327+ platform.startsWith(" darwin" ) -> " pdfium4j_shim.dylib"
328+ platform.startsWith(" windows" ) -> " pdfium4j_shim.dll"
329+ else -> error(" Unknown platform: $platform " )
330+ }
319331
320- when {
332+ val isHostCompatible = when {
321333 platform.startsWith(" darwin" ) && hostOs.contains(" mac" ) ->
322334 (hostIsArm64 && platformIsArm64) || (hostIsX64 && platformIsX64)
323335 platform.startsWith(" linux" ) && hostOs.contains(" linux" ) ->
@@ -326,18 +338,25 @@ val buildShim by tasks.registering {
326338 hostIsX64 && platformIsX64
327339 else -> false
328340 }
329- }
330-
331- if (compatiblePlatforms.isEmpty()) {
332- logger.warn(" No compatible platforms in activePlatforms for host OS $hostOs ; skipping shim build" )
333- return @doLast
334- }
335341
336- compatiblePlatforms.forEach { platform ->
342+ if (! isHostCompatible) {
343+ val prebuilt = prebuiltShimsDir
344+ if (prebuilt != null ) {
345+ val shimFile = prebuilt.resolve(" native-dir-$platform /$shimLibName " )
346+ if (shimFile.exists()) {
347+ logger.lifecycle(" [$platform ] Restoring pre-built shim from ${shimFile.absolutePath} " )
348+ proj.copy { from(shimFile); into(libDir) }
349+ } else {
350+ logger.warn(" [$platform ] Pre-built shim not found at ${shimFile.absolutePath} ; skipping" )
351+ }
352+ } else {
353+ logger.warn(" [$platform ] Platform not compatible with host OS $hostOs ; skipping shim build" )
354+ }
355+ return @forEach
356+ }
337357 val platformBuildDir = buildDir.get().asFile.resolve(platform)
338358 platformBuildDir.mkdirs()
339359
340- val libDir = pdfiumNativesDir.get().asFile.resolve(" natives/$platform " )
341360 val pdfiumRoot = layout.buildDirectory.dir(" pdfium-env-$platform " ).get().asFile
342361 pdfiumRoot.mkdirs()
343362
@@ -382,13 +401,6 @@ val buildShim by tasks.registering {
382401 runCommand(cmakeConfigCmd, " CMake configuration" )
383402 runCommand(listOf (" cmake" , " --build" , " ." , " --config" , " Release" ), " CMake build" )
384403
385- val shimLibName = when {
386- platform.startsWith(" linux" ) -> " pdfium4j_shim.so"
387- platform.startsWith(" darwin" ) -> " pdfium4j_shim.dylib"
388- platform.startsWith(" windows" ) -> " pdfium4j_shim.dll"
389- else -> error(" Unknown platform" )
390- }
391-
392404 val builtLib = platformBuildDir.walkTopDown().find { it.name == shimLibName }
393405 ? : error(" Native shim NOT FOUND after build: $shimLibName in $platformBuildDir " )
394406
0 commit comments