Skip to content

Commit dbdfdac

Browse files
authored
refactor: enhance CI configuration to support pre-built shim artifacts and improve platform compatibility checks (#49)
1 parent 15f9344 commit dbdfdac

2 files changed

Lines changed: 32 additions & 43 deletions

File tree

.github/workflows/publish.yml

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,6 @@ jobs:
7575
pattern: native-dir-*
7676
path: build/native-artifacts
7777

78-
- name: Normalize native artifact layout
79-
run: |
80-
set -euo pipefail
81-
rm -rf build/generated-natives/natives
82-
mkdir -p build/generated-natives/natives
83-
84-
found=0
85-
for artifact_dir in build/native-artifacts/native-dir-*; do
86-
if [[ ! -d "$artifact_dir" ]]; then
87-
continue
88-
fi
89-
found=1
90-
platform="${artifact_dir##*native-dir-}"
91-
target="build/generated-natives/natives/$platform"
92-
mkdir -p "$target"
93-
cp -a "$artifact_dir"/. "$target"/
94-
done
95-
96-
if [[ "$found" -eq 0 ]]; then
97-
echo "::error::No native artifacts were downloaded"
98-
exit 1
99-
fi
100-
10178
- name: Cache PDFium binaries
10279
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
10380
with:
@@ -114,7 +91,7 @@ jobs:
11491
sed -i "s/version = \".*\"/version = \"$VERSION\"/" build.gradle.kts
11592
echo "Publishing version: $VERSION"
11693
117-
- run: ./gradlew verifyNativeJars build -x test --warning-mode all
94+
- run: ./gradlew verifyNativeJars build -x test -PprebuiltShimsDir=build/native-artifacts --warning-mode all
11895

11996
- run: ./gradlew publishAggregationToCentralPortal
12097
env:

build.gradle.kts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
146150
val pdfiumArchiveDir = layout.buildDirectory.dir("pdfium-archives")
147151
val 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

Comments
 (0)