From a9b69f588dcfe9609a1946e71764de45d6f45920 Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Thu, 5 Nov 2026 01:01:51 +0800 Subject: [PATCH 1/7] fix(deps): do not require bundled libc++ in macOS pdfium-static archives Signed-off-by: Shoufa Zhang --- tools/scripts/download_dev_deps.py | 15 +++++++++- tools/scripts/test_download_dev_deps.py | 37 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tools/scripts/download_dev_deps.py b/tools/scripts/download_dev_deps.py index d81d8758e23..ec4c60c4f3b 100755 --- a/tools/scripts/download_dev_deps.py +++ b/tools/scripts/download_dev_deps.py @@ -202,6 +202,19 @@ def pdfium_url(goos: str, goarch: str) -> str: return f"{gh_proxy()}https://github.com/kognitos/pdfium-static/releases/download/chromium%2F{PDFIUM_VERSION}/{PDFIUM_STATIC_ASSETS[(goos, goarch)]}" +def pdfium_required_files(goos: str) -> list[str]: + """Required files inside the pdfium-static archive for `goos`. + + kognitos/pdfium-static bundles lib/libc++.a and lib/libc++abi.a only in the + Linux tarballs (which build.sh links with -fuse-ld=lld on Linux); the macOS + tarballs ship lib/libpdfium.a and include/fpdfview.h and rely on the system + libc++ from the macOS SDK. + """ + if goos == "linux": + return ["lib/libpdfium.a", "lib/libc++.a", "lib/libc++abi.a", "include/fpdfview.h"] + return ["lib/libpdfium.a", "include/fpdfview.h"] + + def office_oxide_url(repo_root: str, goos: str, goarch: str) -> str: """office_oxide native tarball for the target platform. The version is derived from go.mod so it always matches what the Go build actually links @@ -724,7 +737,7 @@ def main(): # Ensures build.sh can find them without network access. office_version = go_module_version("github.com/yfedoseev/office_oxide/go", REPO_ROOT) extractions = [ - (PDFIUM_STATIC_ASSETS[(goos, goarch)], "pdfium-static", ["lib/libpdfium.a", "lib/libc++.a", "lib/libc++abi.a", "include/fpdfview.h"], None), + (PDFIUM_STATIC_ASSETS[(goos, goarch)], "pdfium-static", pdfium_required_files(goos), None), (PDF_OXIDE_ASSETS[(goos, goarch)], "pdf_oxide", [f"lib/{goos}_{goarch}/libpdf_oxide.a", "include/pdf_oxide.h"], None), (OFFICE_OXIDE_ASSETS[(goos, goarch)], "office_oxide", ["lib/liboffice_oxide.a", "include/office_oxide_c/office_oxide.h"], office_version), ] diff --git a/tools/scripts/test_download_dev_deps.py b/tools/scripts/test_download_dev_deps.py index f4095ae8c08..e3e05505793 100644 --- a/tools/scripts/test_download_dev_deps.py +++ b/tools/scripts/test_download_dev_deps.py @@ -282,6 +282,43 @@ def test_incomplete_native_archive_preserves_existing_directory(tmp_path): assert (target / "existing").read_bytes() == b"keep" +def test_pdfium_required_files_by_platform(): + # Linux pdfium-static archives bundle libc++.a + libc++abi.a (linked by + # build.sh with -fuse-ld=lld on Linux), whereas macOS archives ship only + # lib/libpdfium.a and include/fpdfview.h and rely on the macOS SDK libc++. + assert native.pdfium_required_files("linux") == [ + "lib/libpdfium.a", + "lib/libc++.a", + "lib/libc++abi.a", + "include/fpdfview.h", + ] + assert native.pdfium_required_files("darwin") == [ + "lib/libpdfium.a", + "include/fpdfview.h", + ] + + +def test_extracts_darwin_pdfium_archive_without_bundled_libcxx(tmp_path): + archive = tmp_path / "pdfium-mac-arm64-static.tgz" + with tarfile.open(archive, "w:gz") as tf: + for rel, payload in [ + ("lib/libpdfium.a", b"!\npdfium"), + ("include/fpdfview.h", b"// header"), + ]: + info = tarfile.TarInfo(rel) + info.size = len(payload) + tf.addfile(info, io.BytesIO(payload)) + + darwin_target = tmp_path / "pdfium-darwin" + native._extract_native_archive(str(archive), str(darwin_target), native.pdfium_required_files("darwin")) + assert (darwin_target / "lib" / "libpdfium.a").is_file() + assert (darwin_target / "include" / "fpdfview.h").is_file() + + linux_target = tmp_path / "pdfium-linux" + with pytest.raises(RuntimeError, match="missing required native file"): + native._extract_native_archive(str(archive), str(linux_target), native.pdfium_required_files("linux")) + + @pytest.mark.parametrize("kind", ["traversal", "absolute", "symlink", "hardlink", "fifo", "device"]) def test_native_archive_rejects_unsafe_members(tmp_path, kind): target = tmp_path / "native" From e83aafc9f3678f05f7d887a68ff8781eaae73f32 Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Thu, 5 Nov 2026 01:22:44 +0800 Subject: [PATCH 2/7] fix(deps): track target platform stamp in extracted native archives Signed-off-by: Shoufa Zhang --- tools/scripts/download_dev_deps.py | 24 +++++++++++---- tools/scripts/test_download_dev_deps.py | 39 ++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/tools/scripts/download_dev_deps.py b/tools/scripts/download_dev_deps.py index ec4c60c4f3b..4038ce23139 100755 --- a/tools/scripts/download_dev_deps.py +++ b/tools/scripts/download_dev_deps.py @@ -355,7 +355,7 @@ def _valid_download(filename): return True -def _extract_native_archive(archive_path, target, required_files, version=None): +def _extract_native_archive(archive_path, target, required_files, version=None, platform=None): """Extract ARCHIVE_PATH into TARGET, verifying REQUIRED_FILES (and, for .a files, an optional null-delimited VERSION stamp) before promoting the staged tree. @@ -363,8 +363,19 @@ def _extract_native_archive(archive_path, target, required_files, version=None): Path-traversal / absolute / non-regular members are rejected before extraction, and an incomplete archive raises instead of replacing a previously good directory.""" - - def complete(directory): + platform_token = None + if platform is not None: + platform_token = f"{platform[0]}/{platform[1]}" if isinstance(platform, tuple) else str(platform) + + def complete(directory, check_platform=False): + """Return True when directory holds all required_files, version stamps, and matching platform.""" + if check_platform and platform_token is not None: + stamp_path = os.path.join(directory, ".ragflow_platform") + if not os.path.isfile(stamp_path): + return False + with open(stamp_path, encoding="utf-8") as stamp_file: + if stamp_file.read().strip() != platform_token: + return False for name in required_files: path = os.path.join(directory, name) if not os.path.isfile(path) or os.path.getsize(path) == 0: @@ -375,7 +386,7 @@ def complete(directory): return False return True - if complete(target): + if complete(target, check_platform=True): print(f" ✓ {os.path.basename(target)} already extracted to {target}") return os.makedirs(os.path.dirname(target), exist_ok=True) @@ -389,6 +400,9 @@ def complete(directory): archive.extractall(staging, members=members) if not complete(staging): raise RuntimeError(f"{archive_path} is missing required native files or version {version}") + if platform_token is not None: + with open(os.path.join(staging, ".ragflow_platform"), "w", encoding="utf-8") as stamp_file: + stamp_file.write(f"{platform_token}\n") if os.path.isdir(target): shutil.rmtree(target) os.replace(staging, target) @@ -749,7 +763,7 @@ def main(): continue target = os.path.join(NATIVE_LIBS_DIR, subdir) try: - _extract_native_archive(archive_path, target, required_files, version) + _extract_native_archive(archive_path, target, required_files, version, platform=(goos, goarch)) except (tarfile.TarError, EOFError, RuntimeError): os.unlink(archive_path) raise diff --git a/tools/scripts/test_download_dev_deps.py b/tools/scripts/test_download_dev_deps.py index e3e05505793..e59d52cb1bf 100644 --- a/tools/scripts/test_download_dev_deps.py +++ b/tools/scripts/test_download_dev_deps.py @@ -283,9 +283,7 @@ def test_incomplete_native_archive_preserves_existing_directory(tmp_path): def test_pdfium_required_files_by_platform(): - # Linux pdfium-static archives bundle libc++.a + libc++abi.a (linked by - # build.sh with -fuse-ld=lld on Linux), whereas macOS archives ship only - # lib/libpdfium.a and include/fpdfview.h and rely on the macOS SDK libc++. + """Verify PDFium required archive entries for Linux vs. macOS.""" assert native.pdfium_required_files("linux") == [ "lib/libpdfium.a", "lib/libc++.a", @@ -299,6 +297,7 @@ def test_pdfium_required_files_by_platform(): def test_extracts_darwin_pdfium_archive_without_bundled_libcxx(tmp_path): + """Darwin PDFium archives without bundled libc++ extract on macOS and fail Linux checks.""" archive = tmp_path / "pdfium-mac-arm64-static.tgz" with tarfile.open(archive, "w:gz") as tf: for rel, payload in [ @@ -310,7 +309,12 @@ def test_extracts_darwin_pdfium_archive_without_bundled_libcxx(tmp_path): tf.addfile(info, io.BytesIO(payload)) darwin_target = tmp_path / "pdfium-darwin" - native._extract_native_archive(str(archive), str(darwin_target), native.pdfium_required_files("darwin")) + native._extract_native_archive( + str(archive), + str(darwin_target), + native.pdfium_required_files("darwin"), + platform=("darwin", "arm64"), + ) assert (darwin_target / "lib" / "libpdfium.a").is_file() assert (darwin_target / "include" / "fpdfview.h").is_file() @@ -319,6 +323,33 @@ def test_extracts_darwin_pdfium_archive_without_bundled_libcxx(tmp_path): native._extract_native_archive(str(archive), str(linux_target), native.pdfium_required_files("linux")) +def test_extract_native_archive_replaces_cached_tree_when_platform_changes(tmp_path): + """Switching target platform/arch replaces a cached PDFium tree instead of reusing it.""" + def write_pdfium_tar(path, payload): + """Create a minimal Darwin PDFium tar.gz with the given libpdfium.a payload.""" + with tarfile.open(path, "w:gz") as tf: + for rel, data in [ + ("lib/libpdfium.a", payload), + ("include/fpdfview.h", b"// header"), + ]: + info = tarfile.TarInfo(rel) + info.size = len(data) + tf.addfile(info, io.BytesIO(data)) + + arm64_archive = tmp_path / "pdfium-mac-arm64-static.tgz" + amd64_archive = tmp_path / "pdfium-mac-x64-static.tgz" + write_pdfium_tar(arm64_archive, b"!\narm64-pdfium") + write_pdfium_tar(amd64_archive, b"!\namd64-pdfium") + + target = tmp_path / "pdfium-static" + required = native.pdfium_required_files("darwin") + native._extract_native_archive(str(arm64_archive), str(target), required, platform=("darwin", "arm64")) + assert (target / "lib" / "libpdfium.a").read_bytes() == b"!\narm64-pdfium" + + native._extract_native_archive(str(amd64_archive), str(target), required, platform=("darwin", "amd64")) + assert (target / "lib" / "libpdfium.a").read_bytes() == b"!\namd64-pdfium" + + @pytest.mark.parametrize("kind", ["traversal", "absolute", "symlink", "hardlink", "fifo", "device"]) def test_native_archive_rejects_unsafe_members(tmp_path, kind): target = tmp_path / "native" From a6f1013638cb4bcc4192aa65f9781e9f91ad9aef Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Tue, 10 Nov 2026 00:13:22 +0800 Subject: [PATCH 3/7] fix(deps): scope platform stamp to pdfium and update macos doc - In internal/development.md, separate CGO_LDFLAGS for darwin to omit libc++.a and libc++abi.a, matching build.sh. - Scope platform and archive hash stamp extraction in download_dev_deps.py strictly to pdfium-static. - Invalidate cached pdfium-static tree when archive hash changes. - Add regression test for archive content hash invalidation. Signed-off-by: Shoufa Zhang --- internal/development.md | 27 ++++++++++++------ tools/scripts/download_dev_deps.py | 32 ++++++++++++++++----- tools/scripts/test_download_dev_deps.py | 38 +++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/internal/development.md b/internal/development.md index a0b388b5669..b400cadc4c0 100644 --- a/internal/development.md +++ b/internal/development.md @@ -184,14 +184,25 @@ The image contains Go DeepDoc weights, tokenizer assets, and the BPE table. Deep > fi > > export CGO_CFLAGS="-I${RAGFLOW_DEPS}/office_oxide/include/office_oxide_c" -> export CGO_LDFLAGS="\ -> ${RAGFLOW_DEPS}/office_oxide/lib/liboffice_oxide.a \ -> ${RAGFLOW_DEPS}/pdfium-static/lib/libpdfium.a \ -> ${RAGFLOW_DEPS}/pdfium-static/lib/libc++.a \ -> ${RAGFLOW_DEPS}/pdfium-static/lib/libc++abi.a \ -> ${RAGFLOW_DEPS}/pdf_oxide/lib/${PLATFORM}/libpdf_oxide.a \ -> ${ORT_LINK} \ -> ${SYS_LIBS}" +> if [ "$TARGET_OS" = "darwin" ]; then +> # macOS (ld64): PDFium uses system libc++; bundled libc++.a / libc++abi.a +> # are not present in the macOS pdfium-static distribution. +> export CGO_LDFLAGS="\ +> ${RAGFLOW_DEPS}/office_oxide/lib/liboffice_oxide.a \ +> ${RAGFLOW_DEPS}/pdfium-static/lib/libpdfium.a \ +> ${RAGFLOW_DEPS}/pdf_oxide/lib/${PLATFORM}/libpdf_oxide.a \ +> ${ORT_LINK} \ +> ${SYS_LIBS}" +> else +> export CGO_LDFLAGS="\ +> ${RAGFLOW_DEPS}/office_oxide/lib/liboffice_oxide.a \ +> ${RAGFLOW_DEPS}/pdfium-static/lib/libpdfium.a \ +> ${RAGFLOW_DEPS}/pdfium-static/lib/libc++.a \ +> ${RAGFLOW_DEPS}/pdfium-static/lib/libc++abi.a \ +> ${RAGFLOW_DEPS}/pdf_oxide/lib/${PLATFORM}/libpdf_oxide.a \ +> ${ORT_LINK} \ +> ${SYS_LIBS}" +> fi > ``` > > All four native libraries are statically linked — no `LD_LIBRARY_PATH` or `-Wl,-rpath` needed. diff --git a/tools/scripts/download_dev_deps.py b/tools/scripts/download_dev_deps.py index 4038ce23139..7a36a4a649e 100755 --- a/tools/scripts/download_dev_deps.py +++ b/tools/scripts/download_dev_deps.py @@ -355,7 +355,7 @@ def _valid_download(filename): return True -def _extract_native_archive(archive_path, target, required_files, version=None, platform=None): +def _extract_native_archive(archive_path, target, required_files, version=None, platform=None, archive_hash=None): """Extract ARCHIVE_PATH into TARGET, verifying REQUIRED_FILES (and, for .a files, an optional null-delimited VERSION stamp) before promoting the staged tree. @@ -369,13 +369,18 @@ def _extract_native_archive(archive_path, target, required_files, version=None, def complete(directory, check_platform=False): """Return True when directory holds all required_files, version stamps, and matching platform.""" - if check_platform and platform_token is not None: + if check_platform and (platform_token is not None or archive_hash is not None): stamp_path = os.path.join(directory, ".ragflow_platform") if not os.path.isfile(stamp_path): return False with open(stamp_path, encoding="utf-8") as stamp_file: - if stamp_file.read().strip() != platform_token: - return False + lines = [line.strip() for line in stamp_file if line.strip()] + stamp_platform = lines[0] if lines else "" + stamp_hash = lines[1] if len(lines) > 1 else "" + if platform_token is not None and stamp_platform != platform_token: + return False + if archive_hash is not None and stamp_hash != archive_hash: + return False for name in required_files: path = os.path.join(directory, name) if not os.path.isfile(path) or os.path.getsize(path) == 0: @@ -400,9 +405,14 @@ def complete(directory, check_platform=False): archive.extractall(staging, members=members) if not complete(staging): raise RuntimeError(f"{archive_path} is missing required native files or version {version}") - if platform_token is not None: + if platform_token is not None or archive_hash is not None: with open(os.path.join(staging, ".ragflow_platform"), "w", encoding="utf-8") as stamp_file: - stamp_file.write(f"{platform_token}\n") + if platform_token is not None: + stamp_file.write(f"{platform_token}\n") + else: + stamp_file.write("\n") + if archive_hash is not None: + stamp_file.write(f"{archive_hash}\n") if os.path.isdir(target): shutil.rmtree(target) os.replace(staging, target) @@ -762,8 +772,16 @@ def main(): print(f" Skipping extraction: {archive} not found") continue target = os.path.join(NATIVE_LIBS_DIR, subdir) + archive_hash = _sha256_of(archive_path) if subdir == "pdfium-static" else None try: - _extract_native_archive(archive_path, target, required_files, version, platform=(goos, goarch)) + _extract_native_archive( + archive_path, + target, + required_files, + version, + platform=(goos, goarch) if subdir == "pdfium-static" else None, + archive_hash=archive_hash, + ) except (tarfile.TarError, EOFError, RuntimeError): os.unlink(archive_path) raise diff --git a/tools/scripts/test_download_dev_deps.py b/tools/scripts/test_download_dev_deps.py index e59d52cb1bf..c40b331fee7 100644 --- a/tools/scripts/test_download_dev_deps.py +++ b/tools/scripts/test_download_dev_deps.py @@ -350,6 +350,44 @@ def write_pdfium_tar(path, payload): assert (target / "lib" / "libpdfium.a").read_bytes() == b"!\namd64-pdfium" +def test_extract_native_archive_replaces_cached_tree_when_hash_changes(tmp_path): + """Updating archive content replaces a cached tree even when platform stays identical.""" + def write_pdfium_tar(path, payload): + with tarfile.open(path, "w:gz") as tf: + for rel, data in [ + ("lib/libpdfium.a", payload), + ("include/fpdfview.h", b"// header"), + ]: + info = tarfile.TarInfo(rel) + info.size = len(data) + tf.addfile(info, io.BytesIO(data)) + + archive_v1 = tmp_path / "pdfium-v1.tgz" + archive_v2 = tmp_path / "pdfium-v2.tgz" + write_pdfium_tar(archive_v1, b"!\npdfium-v1") + write_pdfium_tar(archive_v2, b"!\npdfium-v2") + + target = tmp_path / "pdfium-static" + required = native.pdfium_required_files("darwin") + native._extract_native_archive( + str(archive_v1), + str(target), + required, + platform=("darwin", "arm64"), + archive_hash=native._sha256_of(str(archive_v1)), + ) + assert (target / "lib" / "libpdfium.a").read_bytes() == b"!\npdfium-v1" + + native._extract_native_archive( + str(archive_v2), + str(target), + required, + platform=("darwin", "arm64"), + archive_hash=native._sha256_of(str(archive_v2)), + ) + assert (target / "lib" / "libpdfium.a").read_bytes() == b"!\npdfium-v2" + + @pytest.mark.parametrize("kind", ["traversal", "absolute", "symlink", "hardlink", "fifo", "device"]) def test_native_archive_rejects_unsafe_members(tmp_path, kind): target = tmp_path / "native" From 95d7532aca856bc465fc638be0dc70624c32767c Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Wed, 11 Nov 2026 00:32:35 +0800 Subject: [PATCH 4/7] fix(build): publish PDFium linker cache atomically --- tools/scripts/native_lib_version.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tools/scripts/native_lib_version.sh b/tools/scripts/native_lib_version.sh index 2c5917e7906..67c3f3e8926 100644 --- a/tools/scripts/native_lib_version.sh +++ b/tools/scripts/native_lib_version.sh @@ -63,7 +63,7 @@ pdfium_static_version() { # changes, even under the same version. Keep these copies in the project's # writable cache so a pre-provisioned native dependency cache can be read-only. pdfium_static_link_dir() ( - local source_dir="$1" target_os="$2" cache_dir="$3" + local source_dir="$1" target_os="$2" cache_dir="$3/archives" local -a archives=(libpdfium.a) hash_command=(sha256sum) if [ "$target_os" = "linux" ]; then archives+=(libc++.a libc++abi.a) @@ -73,24 +73,26 @@ pdfium_static_link_dir() ( local hashes digest link_dir hashes="$(cd "$source_dir" && "${hash_command[@]}" "${archives[@]}")" || return 1 digest="$(printf '%s\n' "$hashes" | "${hash_command[@]}")" || return 1 - link_dir="$cache_dir/${digest%% *}" + digest="${digest%% *}" + link_dir="$cache_dir/$digest" if [ ! -d "$link_dir" ]; then mkdir -p "$cache_dir" || return 1 - local staging archive staged_hashes + local staging staged_dir archive staged_hashes staging="$(mktemp -d "$cache_dir/.pdfium.XXXXXX")" || return 1 trap 'rm -rf -- "$staging"' EXIT + staged_dir="$staging/$digest" + mkdir "$staged_dir" || return 1 for archive in "${archives[@]}"; do - cp "$source_dir/$archive" "$staging/$archive" || return 1 + cp "$source_dir/$archive" "$staged_dir/$archive" || return 1 done - staged_hashes="$(cd "$staging" && "${hash_command[@]}" "${archives[@]}")" || return 1 + staged_hashes="$(cd "$staged_dir" && "${hash_command[@]}" "${archives[@]}")" || return 1 if [ "$staged_hashes" != "$hashes" ]; then echo "PDFium archives changed while preparing linker inputs. Retry the build." >&2 return 1 fi - # Publish all archives at once. A concurrent build may win this race. - if ln -sn "${staging##*/}" "$link_dir" 2>/dev/null; then - trap - EXIT - elif [ ! -d "$link_dir" ]; then + # Rename into the cache parent in one operation. The published directory + # is outside the attempt's cleanup, and another build may publish first. + if ! mv -f "$staged_dir" "$cache_dir/" 2>/dev/null && [ ! -d "$link_dir" ]; then echo "Could not publish the PDFium linker cache at $link_dir" >&2 return 1 fi From 2b3bcf524069e568e66307f1bf8528ce6eff96b2 Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Wed, 11 Nov 2026 01:26:48 +0800 Subject: [PATCH 5/7] fix(build): handle PDFium setup interruptions --- internal/development.md | 45 +++++++++++++++-------------- tools/scripts/native_lib_version.sh | 2 ++ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/internal/development.md b/internal/development.md index 2b50c0ef0fe..807449c3644 100644 --- a/internal/development.md +++ b/internal/development.md @@ -201,29 +201,32 @@ The image contains Go DeepDoc weights, tokenizer assets, and the BPE table. Deep > PDF_OXIDE_PREFIX="${RAGFLOW_DEPS}/pdf_oxide/v${PDF_OXIDE_VERSION}" > # Keep the Go link cache keyed to PDFium contents, even for a reissued archive > # with the same version. Resolve the same linker paths as build.sh. -> PDFIUM_LINK_DIR="$(pdfium_static_link_dir "${PDFIUM_PREFIX}/lib" "$TARGET_OS" "${REPO_ROOT}/.cache/pdfium")" || exit 1 +> if PDFIUM_LINK_DIR="$(pdfium_static_link_dir "${PDFIUM_PREFIX}/lib" "$TARGET_OS" "${REPO_ROOT}/.cache/pdfium")"; then +> OFFICE_OXIDE_A="${OFFICE_OXIDE_PREFIX}/lib/liboffice_oxide.a" +> PDFIUM_A="${PDFIUM_LINK_DIR}/libpdfium.a" +> PDF_OXIDE_A="${PDF_OXIDE_PREFIX}/lib/${PLATFORM}/libpdf_oxide.a" +> # macOS PDFium uses the system libc++ provided by the flags above. +> PDFIUM_CXX_ARCHIVES="" +> if [ "$TARGET_OS" = "linux" ]; then +> PDFIUM_CXX_ARCHIVES="\"${PDFIUM_LINK_DIR}/libc++.a\" \"${PDFIUM_LINK_DIR}/libc++abi.a\"" +> fi > -> OFFICE_OXIDE_A="${OFFICE_OXIDE_PREFIX}/lib/liboffice_oxide.a" -> PDFIUM_A="${PDFIUM_LINK_DIR}/libpdfium.a" -> PDF_OXIDE_A="${PDF_OXIDE_PREFIX}/lib/${PLATFORM}/libpdf_oxide.a" -> # macOS PDFium uses the system libc++ provided by the flags above. -> PDFIUM_CXX_ARCHIVES="" -> if [ "$TARGET_OS" = "linux" ]; then -> PDFIUM_CXX_ARCHIVES="\"${PDFIUM_LINK_DIR}/libc++.a\" \"${PDFIUM_LINK_DIR}/libc++abi.a\"" +> # Derive the include dir from the resolved office_oxide archive: the shell does +> # not expand globs inside double quotes, so a literal +> # ".../office_oxide/v*/include/..." passed to CGO_CFLAGS would never resolve and +> # cgo would fail to find office_oxide.h on the first native compile. +> export CGO_CFLAGS="-I$(dirname "$(dirname "$OFFICE_OXIDE_A")")/include/office_oxide_c" +> export CGO_LDFLAGS="\ +> ${OFFICE_OXIDE_A} \ +> \"${PDFIUM_A}\" \ +> ${PDFIUM_CXX_ARCHIVES} \ +> ${PDF_OXIDE_A} \ +> ${ORT_LINK} \ +> ${SYS_LIBS}" +> else +> printf '%s\n' 'Could not prepare PDFium linker inputs. Run uv run tools/scripts/download_dev_deps.py.' >&2 +> false > fi -> -> # Derive the include dir from the resolved office_oxide archive: the shell does -> # not expand globs inside double quotes, so a literal -> # ".../office_oxide/v*/include/..." passed to CGO_CFLAGS would never resolve and -> # cgo would fail to find office_oxide.h on the first native compile. -> export CGO_CFLAGS="-I$(dirname "$(dirname "$OFFICE_OXIDE_A")")/include/office_oxide_c" -> export CGO_LDFLAGS="\ -> ${OFFICE_OXIDE_A} \ -> \"${PDFIUM_A}\" \ -> ${PDFIUM_CXX_ARCHIVES} \ -> ${PDF_OXIDE_A} \ -> ${ORT_LINK} \ -> ${SYS_LIBS}" > ``` > > All four native libraries are statically linked — no `LD_LIBRARY_PATH` or `-Wl,-rpath` needed. diff --git a/tools/scripts/native_lib_version.sh b/tools/scripts/native_lib_version.sh index 67c3f3e8926..0d8bc3b05dd 100644 --- a/tools/scripts/native_lib_version.sh +++ b/tools/scripts/native_lib_version.sh @@ -80,6 +80,8 @@ pdfium_static_link_dir() ( local staging staged_dir archive staged_hashes staging="$(mktemp -d "$cache_dir/.pdfium.XXXXXX")" || return 1 trap 'rm -rf -- "$staging"' EXIT + trap 'rm -rf -- "$staging"; exit 130' INT + trap 'rm -rf -- "$staging"; exit 143' TERM staged_dir="$staging/$digest" mkdir "$staged_dir" || return 1 for archive in "${archives[@]}"; do From 319f6530fcc04efc7306e63c92fd72d8000bbbfe Mon Sep 17 00:00:00 2001 From: Shoufa Zhang Date: Wed, 11 Nov 2026 01:43:21 +0800 Subject: [PATCH 6/7] docs(build): clear stale cgo flags when PDFium setup fails --- internal/development.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/development.md b/internal/development.md index 807449c3644..b8e06feed98 100644 --- a/internal/development.md +++ b/internal/development.md @@ -224,11 +224,14 @@ The image contains Go DeepDoc weights, tokenizer assets, and the BPE table. Deep > ${ORT_LINK} \ > ${SYS_LIBS}" > else +> unset CGO_CFLAGS CGO_LDFLAGS > printf '%s\n' 'Could not prepare PDFium linker inputs. Run uv run tools/scripts/download_dev_deps.py.' >&2 > false > fi > ``` > +> When sourcing this block from a build script, check its exit status before invoking Go. +> > All four native libraries are statically linked — no `LD_LIBRARY_PATH` or `-Wl,-rpath` needed. > > **ONNX Runtime is mandatory for the production binary.** The in-process (Go) From 3d0b22a524469b9391b32d19894632a65e9136e3 Mon Sep 17 00:00:00 2001 From: zhangshoufa Date: Fri, 13 Nov 2026 13:34:31 +0800 Subject: [PATCH 7/7] style(download): satisfy ruff-format on download_dev_deps scripts The ragflow_preflight lefthook CI job fails on ruff-format because the native_lib_provisioned / _extract_native_archive / extract_native_lib signatures were hand-wrapped without a magic trailing comma, which ruff joins within the 200-char line length. Reformat the two touched Python files with `ruff format` (format-only, no behavior change): signature lines joined, one blank line added after nested docstrings. Verified locally: all pre-commit hooks pass in LEFTHOOK_CHECK_ONLY mode, `ruff check` clean, and the downloader suite reports the same 100 passed / 2 pre-existing macOS ORT-path failures as before. Co-authored-by: Qoder --- tools/scripts/download_dev_deps.py | 28 +++++++------------------ tools/scripts/test_download_dev_deps.py | 2 ++ 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/tools/scripts/download_dev_deps.py b/tools/scripts/download_dev_deps.py index 5f7c1b84526..7a44a1c0aad 100755 --- a/tools/scripts/download_dev_deps.py +++ b/tools/scripts/download_dev_deps.py @@ -493,9 +493,7 @@ def _stamp_present(data, version, stamp_format): return needle in data -def native_lib_provisioned( - directory, required_files, version=None, stamp_format="null", *, platform=None, archive_hash=None -): +def native_lib_provisioned(directory, required_files, version=None, stamp_format="null", *, platform=None, archive_hash=None): """Check required files and version stamps in a provisioned native library. PDFium callers also supply the target platform and, when available, the @@ -526,9 +524,7 @@ def native_lib_provisioned( return True -def _extract_native_archive( - archive_path, target, required_files, version=None, stamp_format="null", *, platform=None, archive_hash=None -): +def _extract_native_archive(archive_path, target, required_files, version=None, stamp_format="null", *, platform=None, archive_hash=None): """Extract ARCHIVE_PATH into TARGET, verifying REQUIRED_FILES (and, for .a files, an optional VERSION stamp) before promoting the staged tree. @@ -544,9 +540,7 @@ def _extract_native_archive( if platform is not None: platform_token = f"{platform[0]}/{platform[1]}" if isinstance(platform, tuple) else str(platform) - if native_lib_provisioned( - target, required_files, version, stamp_format, platform=platform, archive_hash=archive_hash - ): + if native_lib_provisioned(target, required_files, version, stamp_format, platform=platform, archive_hash=archive_hash): print(f" ✓ {os.path.basename(target)} already extracted to {target}") return os.makedirs(os.path.dirname(target), exist_ok=True) @@ -580,9 +574,7 @@ def _extract_native_archive( print(f" ✓ extracted {os.path.basename(archive_path)} to {target}") -def extract_native_lib( - archive_path, subdir, dir_version, required_files, stamp_version=None, stamp_format="null", *, platform=None -): +def extract_native_lib(archive_path, subdir, dir_version, required_files, stamp_version=None, stamp_format="null", *, platform=None): """Extract a native static archive into the version-keyed cache dir ~/ragflow-native-libs///. @@ -607,9 +599,7 @@ def extract_native_lib( target = os.path.join(native_libs_dir(), subdir, dir_version) archive_hash = _sha256_of(archive_path) if platform is not None else None try: - _extract_native_archive( - archive_path, target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash - ) + _extract_native_archive(archive_path, target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash) except (tarfile.TarError, EOFError, RuntimeError): os.unlink(archive_path) raise @@ -990,9 +980,7 @@ def download_native_archives(goos, goarch): target = os.path.join(native_libs_dir(), subdir, dir_version) platform = (goos, goarch) if subdir == "pdfium-static" else None archive_hash = _sha256_of(archive) if platform is not None and os.path.isfile(archive) else None - if native_lib_provisioned( - target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash - ): + if native_lib_provisioned(target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash): print(f" ✓ {subdir} {dir_version} already provisioned at {target}; skipping download") continue print(f"Downloading {archive} from {url}...") @@ -1051,9 +1039,7 @@ def main(): target = os.path.join(native_libs_dir(), subdir, dir_version) platform = (goos, goarch) if subdir == "pdfium-static" else None archive_hash = _sha256_of(archive) if platform is not None and os.path.isfile(archive) else None - if native_lib_provisioned( - target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash - ): + if native_lib_provisioned(target, required_files, stamp_version, stamp_format, platform=platform, archive_hash=archive_hash): print(f" ✓ {subdir} {dir_version} already provisioned at {target}; skipping extraction") continue archive_path = os.path.join(RAGFLOW_DEPS_DIR, archive) diff --git a/tools/scripts/test_download_dev_deps.py b/tools/scripts/test_download_dev_deps.py index 215e4bc1087..cfda5f1c869 100644 --- a/tools/scripts/test_download_dev_deps.py +++ b/tools/scripts/test_download_dev_deps.py @@ -370,6 +370,7 @@ def test_extracts_darwin_pdfium_archive_without_bundled_libcxx(tmp_path): def test_extract_native_archive_replaces_cached_tree_when_platform_changes(tmp_path): """Switching target platform/arch replaces a cached PDFium tree instead of reusing it.""" + def write_pdfium_tar(path, payload): """Create a minimal Darwin PDFium tar.gz with the given libpdfium.a payload.""" with tarfile.open(path, "w:gz") as tf: @@ -397,6 +398,7 @@ def write_pdfium_tar(path, payload): def test_extract_native_lib_replaces_cached_pdfium_when_hash_changes(tmp_path, monkeypatch): """A changed archive replaces PDFium cached under the same version and platform.""" + def write_pdfium_tar(path, payload): with tarfile.open(path, "w:gz") as tf: for rel, data in [