Skip to content

Commit

Permalink
Add WASM build (resolves #28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Feb 23, 2022
1 parent 4e3111a commit e962e11
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/images/header.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ jobs:
target_cpu: x86
runner: windows-2019

- artifact: pdfium-wasm
target_os: wasm
target_cpu: wasm
runner: ubuntu-20.04

runs-on: ${{ matrix.runner }}
steps:
- name: Checkout this repo
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ Here are the download links for latest release:
<td>x86</td>
<td><a href="https://github.com/bblanchon/pdfium-binaries/releases/latest/download/pdfium-win-x86.tgz">pdfium-win-x86.tgz</a></td>
</tr>

<tr>
<td colspan="2">WebAssembly<sup>1</sup></td>
<td><a href="https://github.com/bblanchon/pdfium-binaries/releases/latest/download/pdfium-wasm.tgz">pdfium-wasm.tgz</a></td>
</tr>
</table>

<small>1: WebAssembly build is experimental; please [provide feedback](https://github.com/bblanchon/pdfium-binaries/issues/28).</small>

See the [Releases page](https://github.com/bblanchon/pdfium-binaries/releases) to download older versions of PDFium.

Several [NuGet packages](https://www.nuget.org/packages/bblanchon.PDFium/) are also available.
Expand Down
72 changes: 72 additions & 0 deletions patches/wasm/build.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
diff --git a/config/BUILDCONFIG.gn b/config/BUILDCONFIG.gn
index fcc7e1283..a6e65682d 100644
--- a/config/BUILDCONFIG.gn
+++ b/config/BUILDCONFIG.gn
@@ -262,6 +262,8 @@ if (target_os == "android") {
_default_toolchain = "//build/toolchain/aix:$target_cpu"
} else if (target_os == "zos") {
_default_toolchain = "//build/toolchain/zos:$target_cpu"
+} else if (target_cpu == "wasm") {
+ _default_toolchain = "//build/toolchain/wasm:emscripten"
} else {
assert(false, "Unsupported target_os: $target_os")
}
@@ -300,9 +302,10 @@ is_linux = current_os == "linux"
is_mac = current_os == "mac"
is_nacl = current_os == "nacl"
is_win = current_os == "win" || current_os == "winuwp"
+is_wasm = current_os == "wasm"

is_apple = is_ios || is_mac
-is_posix = !is_win && !is_fuchsia
+is_posix = !is_win && !is_fuchsia && !is_wasm

# =============================================================================
# TARGET DEFAULTS
@@ -377,7 +380,6 @@ if (is_android) {

if (is_clang && !is_nacl) {
default_compiler_configs += [
- "//build/config/clang:find_bad_constructs",
"//build/config/clang:extra_warnings",
]
}
diff --git a/config/compiler/BUILD.gn b/config/compiler/BUILD.gn
index 10937225b..5fed442e4 100644
--- a/config/compiler/BUILD.gn
+++ b/config/compiler/BUILD.gn
@@ -287,6 +287,8 @@ config("compiler") {
configs += [ "//build/config/aix:compiler" ]
} else if (current_os == "zos") {
configs += [ "//build/config/zos:compiler" ]
+ } else if (current_os == "wasm") {
+ configs += [ "//build/config/wasm:compiler" ]
}

configs += [
diff --git a/config/compiler/compiler.gni b/config/compiler/compiler.gni
index 8e7bdd8f0..124dbd963 100644
--- a/config/compiler/compiler.gni
+++ b/config/compiler/compiler.gni
@@ -221,7 +221,7 @@ declare_args() {
# (which lld doesn't support either).
# The default linker everywhere else.
use_lld = is_clang && !(is_mac && is_gpu_fyi_bot) && current_os != "zos" &&
- !(is_ios && (is_cronet_build || target_environment == "catalyst"))
+ !(is_ios && (is_cronet_build || target_environment == "catalyst")) && !is_wasm
}

declare_args() {
diff --git a/toolchain/toolchain.gni b/toolchain/toolchain.gni
index 9874c7a12..59f84c36f 100644
--- a/toolchain/toolchain.gni
+++ b/toolchain/toolchain.gni
@@ -52,6 +52,8 @@ if (is_apple) {
shlib_extension = ".so"
} else if (is_win) {
shlib_extension = ".dll"
+} if (is_wasm) {
+ shlib_extension = ".so"
} else {
assert(false, "Platform not supported")
}
7 changes: 7 additions & 0 deletions patches/wasm/config.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
config("compiler") {
defines = [
# Enable fseeko() and ftello() (required by libopenjpeg20)
# https://github.com/emscripten-core/emscripten/issues/4932
"_POSIX_C_SOURCE=200112",
]
}
26 changes: 26 additions & 0 deletions patches/wasm/pdfium.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index ffa1b60..92224c5 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -125,7 +125,7 @@ source_set("fxcrt") {
"//third_party/icu:icuuc",
]

- if (is_posix || is_fuchsia) {
+ if (is_posix || is_fuchsia || is_wasm) {
sources += [
"cfx_fileaccess_posix.cpp",
"cfx_fileaccess_posix.h",
diff --git a/core/fxge/BUILD.gn b/core/fxge/BUILD.gn
index 7dafd7d..6d1fff9 100644
--- a/core/fxge/BUILD.gn
+++ b/core/fxge/BUILD.gn
@@ -162,7 +162,7 @@ source_set("fxge") {
]
}

- if (is_linux || is_chromeos) {
+ if (is_linux || is_chromeos || is_wasm) {
sources += [ "linux/fx_linux_impl.cpp" ]
}

16 changes: 16 additions & 0 deletions patches/wasm/toolchain.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import("//build/toolchain/gcc_toolchain.gni")

gcc_toolchain("emscripten") {
cc = "emcc"
cxx = "em++"

readelf = "llvm-readobj"
ar = "emar"
ld = cxx
nm = "emnm"

toolchain_args = {
current_cpu = "wasm"
current_os = "wasm"
}
}
13 changes: 12 additions & 1 deletion steps/01-install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -eux

PATH_FILE=${GITHUB_PATH:-.path}
PATH_FILE=${GITHUB_PATH:-$PWD/.path}
TARGET_OS=${PDFium_TARGET_OS:?}
TARGET_CPU=${PDFium_TARGET_CPU:?}
CURRENT_CPU=${PDFium_CURRENT_CPU:-x64}
Expand Down Expand Up @@ -30,4 +30,15 @@ case "$TARGET_OS-$TARGET_CPU" in
sudo apt-get update
sudo apt-get install -y g++-aarch64-linux-gnu
;;

wasm-wasm)
git clone https://github.com/emscripten-core/emsdk.git
pushd emsdk
./emsdk install latest
./emsdk activate latest
echo "$PWD/upstream/emscripten" >> "$PATH_FILE"
echo "$PWD/upstream/bin" >> "$PATH_FILE"
popd
;;

esac
11 changes: 10 additions & 1 deletion steps/03-patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ OS="${PDFium_TARGET_OS:?}"

pushd "${SOURCE}"

git apply -v "$PATCHES/shared_library.patch"
[ "$OS" != "wasm" ] && git apply -v "$PATCHES/shared_library.patch"
git apply -v "$PATCHES/public_headers.patch"

[ "${PDFium_V8:-}" == "enabled" ] && git apply -v "$PATCHES/v8_init.patch"
Expand All @@ -22,6 +22,15 @@ case "$OS" in
git -C third_party/libjpeg_turbo apply -v "$PATCHES/ios/libjpeg_turbo.patch"
;;

wasm)
git apply -v "$PATCHES/wasm/pdfium.patch"
git -C build apply -v "$PATCHES/wasm/build.patch"
mkdir -p "build/toolchain/wasm"
cp "$PATCHES/wasm/toolchain.gn" "build/toolchain/wasm/BUILD.gn"
mkdir -p "build/config/wasm"
cp "$PATCHES/wasm/config.gn" "build/config/wasm/BUILD.gn"
;;

win)
git apply -v "$PATCHES/win/pdfium.patch"
git -C build apply -v "$PATCHES/win/build.patch"
Expand Down
3 changes: 3 additions & 0 deletions steps/05-configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ mkdir -p "$BUILD"
win)
echo 'pdf_use_win32_gdi = true'
;;
wasm):
echo 'pdf_is_complete_lib = true'
;;
esac

) | sort > "$BUILD/args.gn"
Expand Down
16 changes: 15 additions & 1 deletion steps/06-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,19 @@

SOURCE=${PDFium_SOURCE_DIR:-pdfium}
BUILD_DIR=${PDFium_BUILD_DIR:-$SOURCE/out}
TARGET_CPU=${PDFium_TARGET_CPU:?}

ninja -C "$BUILD_DIR" pdfium
ninja -C "$BUILD_DIR" pdfium

if [ "$TARGET_CPU" == "wasm" ]; then
LIBPDFIUMA="$BUILD_DIR/obj/libpdfium.a"
EXPORTED_FUNCTIONS=$(emnm $LIBPDFIUMA --format=just-symbols | grep "^FPDF" | sed 's/^/_/' | paste -sd "," -)
em++ \
-s EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS" \
-s LLD_REPORT_UNDEFINED \
-s WASM=1 \
-s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' \
-o "$BUILD_DIR/pdfium.html" \
"$LIBPDFIUMA" \
--no-entry
fi
14 changes: 13 additions & 1 deletion steps/07-pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ case "$OS" in
mv "$BUILD/libpdfium.dylib" "$STAGING_LIB"
;;

wasm)
mv "$BUILD/pdfium.html" "$STAGING_LIB"
mv "$BUILD/pdfium.js" "$STAGING_LIB"
mv "$BUILD/pdfium.wasm" "$STAGING_LIB"
rm -rf "$STAGING/include/cpp"
rm "$STAGING/PDFiumConfig.cmake"
;;

win)
mv "$BUILD/pdfium.dll.lib" "$STAGING_LIB"
mkdir -p "$STAGING_BIN"
Expand All @@ -57,7 +65,11 @@ BUILD=$(echo "$VERSION" | cut -d. -f3)
PATCH=$(echo "$VERSION" | cut -d. -f4)
END

ARTIFACT_BASE="$PWD/pdfium-$OS-$CPU"
if [ "$OS" != "$CPU" ]; then
ARTIFACT_BASE="$PWD/pdfium-$OS-$CPU"
else
ARTIFACT_BASE="$PWD/pdfium-$OS"
fi
[ "$V8" == "enabled" ] && ARTIFACT_BASE="$ARTIFACT_BASE-v8"
[ "$CFG" == "Debug" ] && ARTIFACT_BASE="$ARTIFACT_BASE-debug"
ARTIFACT="$ARTIFACT_BASE.tgz"
Expand Down
5 changes: 5 additions & 0 deletions steps/08-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ case "$OS" in
-A "$ARCH"
)
;;

wasm)
# TODO: add test for Wasm
exit
;;
esac

CMAKE_ARGS+=("$SOURCE_DIR")
Expand Down

0 comments on commit e962e11

Please sign in to comment.