diff --git a/apps/maple-research/docs/pdf-ocr.md b/apps/maple-research/docs/pdf-ocr.md index b4cd12514..5a35cbd5c 100644 --- a/apps/maple-research/docs/pdf-ocr.md +++ b/apps/maple-research/docs/pdf-ocr.md @@ -12,7 +12,7 @@ The runtime is part of the application package; users do not download it when op | Platform | ONNX Runtime packaging | | --- | --- | -| macOS | Microsoft's official universal2 dylib, loaded from the app Frameworks directory; both Apple silicon and Intel are retained. Minimum macOS is 13.4. | +| macOS | Microsoft's official Apple silicon and Intel dylibs, joined into one universal library and loaded from the app Frameworks directory. Minimum macOS is 13.4. | | Linux | Microsoft's x64 or aarch64 shared library, installed under Maple's private library directory. | | Windows | Microsoft's x64 DLL, installed next to `maple.exe` and loaded by its explicit path. | | Android | Microsoft's official Android AAR, with `libonnxruntime.so` staged for arm64-v8a, armeabi-v7a, x86, and x86_64. | diff --git a/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-nuget.sh b/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-nuget.sh index 88a91199b..cefde160e 100644 --- a/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-nuget.sh +++ b/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-nuget.sh @@ -1,24 +1,22 @@ #!/usr/bin/env bash -# Extracts one native library from Microsoft's official ONNX Runtime NuGet -# package. The package is checked against its pin before anything is read from -# it. The caller must still check the extracted library against that library's -# own pin. +# Extracts native libraries from Microsoft's official ONNX Runtime NuGet +# package: one download, then an entry and a destination per library. The +# package is checked against its pin before anything is read from it. The +# caller must still check each extracted library against that library's own +# pin. # # The sourcing script provides onnxruntime-pins.sh and verify_sha256. -onnxruntime_nuget_extract_library() ( +onnxruntime_nuget_extract_libraries() ( set -euo pipefail version="$1" - entry="$2" - destination="$3" + shift url="$(onnxruntime_nuget_package_url_for_version "${version}")" expected="$(onnxruntime_nuget_package_sha256_for_version "${version}")" - destination_dir="$(dirname "${destination}")" - mkdir -p "${destination_dir}" - package="$(mktemp "${destination_dir}/.onnxruntime-nuget.XXXXXX")" - staged="$(mktemp "${destination}.XXXXXX")" + package="$(mktemp "${TMPDIR:-/tmp}/onnxruntime-nuget.XXXXXX")" + staged="" trap 'rm -f "${package}" "${staged}"' EXIT curl -fL --retry 5 --retry-delay 2 --retry-all-errors \ @@ -26,13 +24,22 @@ onnxruntime_nuget_extract_library() ( --output "${package}" verify_sha256 "ONNX Runtime NuGet package" "${package}" "${expected}" || exit 1 - entry_count="$(unzip -Z1 "${package}" | grep -Fxc -- "${entry}" || true)" - if [ "${entry_count}" != "1" ]; then - echo "Expected exactly one ${entry} in the ONNX Runtime NuGet package; found ${entry_count}." >&2 - exit 1 - fi + while [ "$#" -ge 2 ]; do + entry="$1" + destination="$2" + shift 2 - unzip -p "${package}" "${entry}" > "${staged}" || exit 1 - chmod 0755 "${staged}" - mv "${staged}" "${destination}" + entry_count="$(unzip -Z1 "${package}" | grep -Fxc -- "${entry}" || true)" + if [ "${entry_count}" != "1" ]; then + echo "Expected exactly one ${entry} in the ONNX Runtime NuGet package; found ${entry_count}." >&2 + exit 1 + fi + + mkdir -p "$(dirname "${destination}")" + staged="$(mktemp "${destination}.XXXXXX")" + unzip -p "${package}" "${entry}" > "${staged}" || exit 1 + chmod 0755 "${staged}" + mv "${staged}" "${destination}" + staged="" + done ) diff --git a/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-pins.sh b/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-pins.sh index 26c1c38b5..e59fce4eb 100644 --- a/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-pins.sh +++ b/apps/maple-research/frontend/src-tauri/scripts/onnxruntime-pins.sh @@ -43,25 +43,28 @@ onnxruntime_linux_aarch64_dylib_sha256_for_version() { esac } -onnxruntime_macos_universal2_archive_sha256_for_version() { +# macOS takes one library per architecture from the NuGet package and joins +# them. Microsoft signs these files separately from the universal library its +# release archive carried, so the code is the same but the hashes are not. +onnxruntime_macos_x64_dylib_sha256_for_version() { case "$1" in 1.23.2) - printf '%s\n' "49ae8e3a66ccb18d98ad3fe7f5906b6d7887df8a5edd40f49eb2b14e20885809" + printf '%s\n' "8c9c78de65ea3786f987c0d980e9c1b13a3a5fbc6b3e2965ba05b450e6e4c054" ;; *) - echo "No pinned macOS universal2 ONNX Runtime archive SHA-256 for version '$1'." >&2 + echo "No pinned macOS x64 ONNX Runtime dylib SHA-256 for version '$1'." >&2 return 1 ;; esac } -onnxruntime_macos_universal2_dylib_sha256_for_version() { +onnxruntime_macos_arm64_dylib_sha256_for_version() { case "$1" in 1.23.2) - printf '%s\n' "6fee21a0dbcaa98fe082cb4f7ed07ec5def439df36198f47b61dc205e7d2a1fa" + printf '%s\n' "d306d2bc768540766c7ed8a1e0ff05d2870c77a934ebeee4a7bafa1b732ef299" ;; *) - echo "No pinned macOS universal2 ONNX Runtime dylib SHA-256 for version '$1'." >&2 + echo "No pinned macOS arm64 ONNX Runtime dylib SHA-256 for version '$1'." >&2 return 1 ;; esac diff --git a/apps/maple-research/frontend/src-tauri/scripts/provide-linux-onnxruntime.sh b/apps/maple-research/frontend/src-tauri/scripts/provide-linux-onnxruntime.sh index 049ea237c..583558ac9 100755 --- a/apps/maple-research/frontend/src-tauri/scripts/provide-linux-onnxruntime.sh +++ b/apps/maple-research/frontend/src-tauri/scripts/provide-linux-onnxruntime.sh @@ -62,7 +62,7 @@ verify_sha256() { if [ ! -f "${ORT_DYLIB}" ]; then rm -rf "${ORT_ROOT}" - onnxruntime_nuget_extract_library "${ORT_VERSION}" "${ORT_NUGET_ENTRY}" "${ORT_DYLIB}" + onnxruntime_nuget_extract_libraries "${ORT_VERSION}" "${ORT_NUGET_ENTRY}" "${ORT_DYLIB}" fi verify_sha256 "ONNX Runtime shared library" "${ORT_DYLIB}" "${ORT_DYLIB_SHA256}" diff --git a/apps/maple-research/frontend/src-tauri/scripts/provide-macos-onnxruntime.sh b/apps/maple-research/frontend/src-tauri/scripts/provide-macos-onnxruntime.sh index 698c60f2e..f18e25fe2 100755 --- a/apps/maple-research/frontend/src-tauri/scripts/provide-macos-onnxruntime.sh +++ b/apps/maple-research/frontend/src-tauri/scripts/provide-macos-onnxruntime.sh @@ -4,6 +4,7 @@ set -euo pipefail ORT_VERSION="${ORT_VERSION:-1.23.2}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPT_DIR}/onnxruntime-pins.sh" +source "${SCRIPT_DIR}/onnxruntime-nuget.sh" if [ "$(uname -s)" != "Darwin" ]; then echo "macOS ONNX Runtime provisioning must run on macOS." >&2 @@ -13,11 +14,9 @@ fi TAURI_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" ORT_ROOT="${TAURI_DIR}/onnxruntime-macos" ORT_DIR="${ORT_ROOT}/onnxruntime-osx-universal2-${ORT_VERSION}" -ORT_ARCHIVE="onnxruntime-osx-universal2-${ORT_VERSION}.tgz" -ORT_URL="https://github.com/microsoft/onnxruntime/releases/download/v${ORT_VERSION}/${ORT_ARCHIVE}" ORT_DYLIB="${ORT_DIR}/lib/libonnxruntime.${ORT_VERSION}.dylib" -ORT_ARCHIVE_SHA256="$(onnxruntime_macos_universal2_archive_sha256_for_version "${ORT_VERSION}")" -ORT_DYLIB_SHA256="$(onnxruntime_macos_universal2_dylib_sha256_for_version "${ORT_VERSION}")" +ORT_X64_SHA256="$(onnxruntime_macos_x64_dylib_sha256_for_version "${ORT_VERSION}")" +ORT_ARM64_SHA256="$(onnxruntime_macos_arm64_dylib_sha256_for_version "${ORT_VERSION}")" sha256_file() { shasum -a 256 "$1" | awk '{print $1}' @@ -38,32 +37,37 @@ verify_sha256() { fi } -if [ ! -f "${ORT_DYLIB}" ]; then +# The universal library is checked one architecture at a time, because the +# pins are for the two files Microsoft publishes, not for lipo's layout. +verify_universal_dylib() ( + slice="$(mktemp "${ORT_ROOT}/.slice.XXXXXX")" + trap 'rm -f "${slice}"' EXIT + + lipo -thin x86_64 "${ORT_DYLIB}" -output "${slice}" || exit 1 + verify_sha256 "ONNX Runtime x86_64 dylib" "${slice}" "${ORT_X64_SHA256}" || exit 1 + lipo -thin arm64 "${ORT_DYLIB}" -output "${slice}" || exit 1 + verify_sha256 "ONNX Runtime arm64 dylib" "${slice}" "${ORT_ARM64_SHA256}" || exit 1 +) + +# A library from before this provider used NuGet has other signatures and +# fails the check once; it is fetched again rather than reported. +if [ ! -f "${ORT_DYLIB}" ] || ! verify_universal_dylib 2>/dev/null; then rm -rf "${ORT_ROOT}" - mkdir -p "${ORT_ROOT}" - archive_path="${ORT_ROOT}/${ORT_ARCHIVE}" + mkdir -p "${ORT_DIR}/lib" + x64_dylib="${ORT_ROOT}/libonnxruntime.x86_64.dylib" + arm64_dylib="${ORT_ROOT}/libonnxruntime.arm64.dylib" - curl -fL --retry 5 --retry-delay 2 --retry-all-errors \ - "${ORT_URL}" \ - --output "${archive_path}" + onnxruntime_nuget_extract_libraries "${ORT_VERSION}" \ + "runtimes/osx-x64/native/libonnxruntime.dylib" "${x64_dylib}" \ + "runtimes/osx-arm64/native/libonnxruntime.dylib" "${arm64_dylib}" - verify_sha256 "ONNX Runtime archive" "${archive_path}" "${ORT_ARCHIVE_SHA256}" - tar -xzf "${archive_path}" -C "${ORT_ROOT}" - rm -f "${archive_path}" + verify_sha256 "ONNX Runtime x86_64 dylib" "${x64_dylib}" "${ORT_X64_SHA256}" + verify_sha256 "ONNX Runtime arm64 dylib" "${arm64_dylib}" "${ORT_ARM64_SHA256}" + lipo -create "${x64_dylib}" "${arm64_dylib}" -output "${ORT_DYLIB}" + rm -f "${x64_dylib}" "${arm64_dylib}" fi -verify_sha256 "ONNX Runtime shared library" "${ORT_DYLIB}" "${ORT_DYLIB_SHA256}" - -architectures="$(lipo -archs "${ORT_DYLIB}")" -for required_architecture in arm64 x86_64; do - case " ${architectures} " in - *" ${required_architecture} "*) ;; - *) - echo "Expected a universal arm64+x86_64 ONNX Runtime dylib, got: ${architectures}" >&2 - exit 1 - ;; - esac -done +verify_universal_dylib echo "ORT_LIB_LOCATION=${ORT_DIR}" echo "ORT_SKIP_DOWNLOAD=true" diff --git a/apps/maple-research/frontend/src-tauri/scripts/provide-windows-onnxruntime.sh b/apps/maple-research/frontend/src-tauri/scripts/provide-windows-onnxruntime.sh index 0afd5e652..3767cc7d6 100755 --- a/apps/maple-research/frontend/src-tauri/scripts/provide-windows-onnxruntime.sh +++ b/apps/maple-research/frontend/src-tauri/scripts/provide-windows-onnxruntime.sh @@ -60,7 +60,7 @@ to_native_path() { if [ ! -f "${ORT_DLL}" ]; then rm -rf "${ORT_ROOT}" - onnxruntime_nuget_extract_library "${ORT_VERSION}" "${ORT_NUGET_ENTRY}" "${ORT_DLL}" + onnxruntime_nuget_extract_libraries "${ORT_VERSION}" "${ORT_NUGET_ENTRY}" "${ORT_DLL}" fi verify_sha256 "ONNX Runtime DLL" "${ORT_DLL}" "${ORT_DLL_SHA256}"