Treader-open/Builder/_link_to_mupdf_1.23.7.ps1

170 lines
6.4 KiB
PowerShell

param(
[string]$Action = "",
[string]$ExtraParam = ""
)
# 获取脚本所在目录作为构建目录
$BUILD_DIR = $PSScriptRoot
Set-Location -Path $BUILD_DIR
$VERSION_TAG = "1.23.7"
$MUPDF_FOLDER = "mupdf-$VERSION_TAG"
if ($Action -eq "fdroid") {
$MUPDF_FOLDER = "$MUPDF_FOLDER-fdroid"
}
# 克隆 MuPDF 仓库
if (-not (Test-Path -Path $MUPDF_FOLDER)) {
git clone --recursive git://git.ghostscript.com/mupdf.git --branch $VERSION_TAG $MUPDF_FOLDER
}
$MUPDF_ROOT = Join-Path -Path $BUILD_DIR -ChildPath $MUPDF_FOLDER
$MUPDF_JAVA = Join-Path -Path $MUPDF_ROOT -ChildPath "platform/librera"
$SRC = Join-Path -Path $BUILD_DIR -ChildPath "jni/~mupdf-$VERSION_TAG"
$DEST = Join-Path -Path $MUPDF_ROOT -ChildPath "source"
$LIBS = Join-Path -Path $BUILD_DIR -ChildPath "..\app\src\main\jniLibs"
Write-Host "MUPDF : $VERSION_TAG"
Write-Host "================== "
# 创建必要目录
New-Item -ItemType Directory -Force -Path $SRC | Out-Null
New-Item -ItemType Directory -Force -Path $MUPDF_JAVA\jni | Out-Null
Set-Location -Path $MUPDF_FOLDER
Write-Host "=================="
# 清理操作
if ($Action -eq "clean") {
git reset --hard
git clean -f -d
Remove-Item -Recurse -Force -Path "generated" -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force -Path "build" -ErrorAction SilentlyContinue
make clean
}
# 构建 MuPDF
if (-not (Test-Path -Path "build/release")) {
make OS=mingw32-cross generate
make OS=mingw32-cross build=release
# make release
}
Set-Location -Path $BUILD_DIR
# 复制 JNI 文件
Remove-Item -Recurse -Force -Path "$MUPDF_JAVA\jni" -ErrorAction SilentlyContinue
Copy-Item -Recurse -Path "jni" -Destination "$MUPDF_JAVA\jni"
Rename-Item -Path "$MUPDF_JAVA\jni\Android-$VERSION_TAG.mk" -NewName "Android.mk" -Force
# 准备 jniLibs 目录
Remove-Item -Recurse -Force -Path $LIBS -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path $LIBS | Out-Null
# 创建符号链接(需要以管理员权限运行)
$null = New-Item -ItemType Junction -Force -Path "$LIBS\armeabi-v7a" -Target "$MUPDF_JAVA\libs\armeabi-v7a"
$null = New-Item -ItemType Junction -Force -Path "$LIBS\arm64-v8a" -Target "$MUPDF_JAVA\libs\arm64-v8a"
$null = New-Item -ItemType Junction -Force -Path "$LIBS\x86" -Target "$MUPDF_JAVA\libs\x86"
$null = New-Item -ItemType Junction -Force -Path "$LIBS\x86_64" -Target "$MUPDF_JAVA\libs\x86_64"
# 文件复制操作
if ($Action -eq "copy") {
# 从 MuPDF 复制到本地 SRC
Copy-Item -Path "$DEST\html\css-apply.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\html\epub-doc.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\html\html-layout.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\html\html-parse.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\cbz\mucbz.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\cbz\muimg.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\load-webp.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\image.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\unzip.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\directory.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\xml.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\list-device.c" -Destination "$SRC" -Force
Copy-Item -Path "$DEST\fitz\image-imp.h" -Destination "$SRC" -Force
Copy-Item -Path "$MUPDF_ROOT\include\mupdf\fitz\compressed-buffer.h" -Destination "$SRC" -Force
Copy-Item -Path "$MUPDF_ROOT\include\mupdf\fitz\context.h" -Destination "$SRC" -Force
}
else {
# 从本地 SRC 复制到 MuPDF
Copy-Item -Path "$SRC\css-apply.c" -Destination "$DEST\html" -Force
Copy-Item -Path "$SRC\epub-doc.c" -Destination "$DEST\html" -Force
Copy-Item -Path "$SRC\html-layout.c" -Destination "$DEST\html" -Force
Copy-Item -Path "$SRC\html-parse.c" -Destination "$DEST\html" -Force
Copy-Item -Path "$SRC\mucbz.c" -Destination "$DEST\cbz" -Force
Copy-Item -Path "$SRC\muimg.c" -Destination "$DEST\cbz" -Force
Copy-Item -Path "$SRC\load-webp.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\image.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\unzip.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\directory.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\xml.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\list-device.c" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\image-imp.h" -Destination "$DEST\fitz" -Force
Copy-Item -Path "$SRC\compressed-buffer.h" -Destination "$MUPDF_ROOT\include\mupdf\fitz" -Force
Copy-Item -Path "$SRC\context.h" -Destination "$MUPDF_ROOT\include\mupdf\fitz" -Force
Set-Location -Path $MUPDF_JAVA
$NDK_VERSION = "28.2.13676358"
$FDROID_NDK_VERSION = "21.4.7075529"
# Windows 路径配置
$ANDROID_HOME = $env:ANDROID_HOME
if (-not $ANDROID_HOME) {
$ANDROID_HOME = "${env:LOCALAPPDATA}\Android\Sdk"
}
$NDK_PATHS = @(
"${env:ProgramFiles}\Android\android-sdk\ndk\$NDK_VERSION",
"$ANDROID_HOME\ndk\$NDK_VERSION",
"${env:ProgramFiles(x86)}\Android\android-sdk\ndk\$NDK_VERSION"
)
# 清理 NDK 构建
if ($Action -eq "clean_ndk") {
Remove-Item -Recurse -Force -Path "$MUPDF_JAVA\obj" -ErrorAction SilentlyContinue
$ndkBuildPath = if ($ExtraParam -eq "fdroid") {
"$ANDROID_HOME\ndk\$FDROID_NDK_VERSION\ndk-build.cmd"
} else {
"$ANDROID_HOME\ndk\$NDK_VERSION\ndk-build.cmd"
}
if (Test-Path -Path $ndkBuildPath) {
& $ndkBuildPath clean
}
}
# 执行 NDK 构建
$ndkBuildPath = if ($Action -eq "fdroid") {
"$ANDROID_HOME\ndk\$FDROID_NDK_VERSION\ndk-build.cmd"
} else {
"$ANDROID_HOME\ndk\$NDK_VERSION\ndk-build.cmd"
}
if (Test-Path -Path $ndkBuildPath) {
$ABIs = @("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
$Platform = if ($Action -eq "fdroid") { "android-16" } else { "android-24" }
foreach ($abi in $ABIs) {
$env:APP_ABI = $abi
$env:APP_PLATFORM = $Platform
& $ndkBuildPath NDK_APPLICATION_MK=jni/Application.mk
}
Write-Host "=================="
Write-Host "NDK: $ndkBuildPath"
Write-Host "APP_PLATFORM=$Platform"
}
else {
Write-Host "NDK not found at $ndkBuildPath"
}
}
Write-Host "=================="
Write-Host "MUPDF: $MUPDF_JAVA"
Write-Host "JNI: $LIBS"
Write-Host "=================="