From 571ca145d4cfe605e23ad77b1f90df66859f2512 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 6 Nov 2023 16:20:25 +0000 Subject: [PATCH] Add infrastructure to build WebAssembly static libs via wasm-vips Co-authored-by: Ingvar Stepanyan --- .gitignore | 2 ++ README.md | 7 ++++++ build/wasm.sh | 32 +++++++++++++++++++++++++++ npm/dev-wasm32/THIRD-PARTY-NOTICES.md | 31 ++++++++++++++++++++++++++ npm/dev-wasm32/package.json | 31 ++++++++++++++++++++++++++ npm/package.json | 1 + npm/populate.sh | 4 ++-- 7 files changed, 106 insertions(+), 2 deletions(-) create mode 100755 build/wasm.sh create mode 100644 npm/dev-wasm32/THIRD-PARTY-NOTICES.md create mode 100644 npm/dev-wasm32/package.json diff --git a/.gitignore b/.gitignore index c20d1458..4d630da0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ libvips* *.log +build/wasm-vips-* deps/ target/ npm/*/* !npm/*/package.json +!npm/dev-wasm32/THIRD-PARTY-NOTICES.md npm/img-sharp-libvips-*.tgz diff --git a/README.md b/README.md index 9bac641c..a9743ed3 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,13 @@ The dylib files are compiled within the same build script as Linux. Dependency paths are modified to use the relative `@rpath` with `install_name_tool`. +### WebAssembly + +The scripts from [wasm-vips](https://github.com/kleisauke/wasm-vips) +are [used to compile](build/wasm.sh) libvips and its dependencies +as static Wasm libraries ready for further compilation into a single, +statically-linked sharp shared library. + ## Licences These scripts are licensed under the terms of the [Apache 2.0 Licence](LICENSE). diff --git a/build/wasm.sh b/build/wasm.sh new file mode 100755 index 00000000..7fdb2e05 --- /dev/null +++ b/build/wasm.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -e + +VERSION_WASM_VIPS="56f151b" + +DIR="wasm-vips-${VERSION_WASM_VIPS}" +TAG="wasm-vips:${VERSION_WASM_VIPS}" + +echo "Using ${TAG}" +cd "${0%/*}" + +if [ ! -d "$DIR" ]; then + # Download specific version of wasm-vips + mkdir "${DIR}" + curl -Ls https://github.com/kleisauke/wasm-vips/archive/${VERSION_WASM_VIPS}.tar.gz | tar xzC "${DIR}" --strip-components=1 +fi + +if [ -z "$(docker images -q ${TAG})" ]; then + # Create container with emscripten + pushd "${DIR}" + docker build -t "${TAG}" . + popd +fi + +if [ ! -d "$DIR/build/target/lib" ]; then + # Build libvips and dependencies as static Wasm libraries via emscripten + docker run --rm -v "$PWD/${DIR}":/src "${TAG}" -c "./build.sh --disable-bindings --disable-modules --disable-jxl --enable-libvips-cpp" +fi + +# Copy only the files we need +cp -r --no-preserve=mode,ownership ${DIR}/build/target/{include,lib,versions.json} ../npm/dev-wasm32 +rm -r ../npm/dev-wasm32/lib/cmake diff --git a/npm/dev-wasm32/THIRD-PARTY-NOTICES.md b/npm/dev-wasm32/THIRD-PARTY-NOTICES.md new file mode 100644 index 00000000..cef230d2 --- /dev/null +++ b/npm/dev-wasm32/THIRD-PARTY-NOTICES.md @@ -0,0 +1,31 @@ +# Third-party notices + +This software contains third-party libraries +used under the terms of the following licences: + +| Library | Used under the terms of | +|---------------|-----------------------------------------------------------------------------------------------------------| +| aom | BSD 2-Clause + [Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/) | +| cgif | MIT Licence | +| expat | MIT Licence | +| glib | LGPLv3 | +| highway | Apache-2.0 License, BSD 3-Clause | +| lcms | MIT Licence | +| libexif | LGPLv3 | +| libffi | MIT Licence | +| libheif | LGPLv3 | +| libimagequant | [BSD 2-Clause](https://github.com/lovell/libimagequant/blob/main/COPYRIGHT) | +| mozjpeg | [zlib License, IJG License, BSD-3-Clause](https://github.com/mozilla/mozjpeg/blob/master/LICENSE.md) | +| libnsgif | MIT Licence | +| libspng | [BSD 2-Clause, libpng License](https://github.com/randy408/libspng/blob/master/LICENSE) | +| libtiff | [libtiff License](https://gitlab.com/libtiff/libtiff/blob/master/LICENSE.md) (BSD-like) | +| libvips | LGPLv3 | +| libwebp | New BSD License | +| resvg | MPL-2.0 License | +| zlib-ng | [zlib Licence](https://github.com/zlib-ng/zlib-ng/blob/develop/LICENSE.md) | + +Use of libraries under the terms of the LGPLv3 is via the +"any later version" clause of the LGPLv2 or LGPLv2.1. + +Please report any errors or omissions via +https://github.com/lovell/sharp-libvips/issues/new diff --git a/npm/dev-wasm32/package.json b/npm/dev-wasm32/package.json new file mode 100644 index 00000000..ad46dc01 --- /dev/null +++ b/npm/dev-wasm32/package.json @@ -0,0 +1,31 @@ +{ + "name": "@img/sharp-libvips-dev-wasm32", + "version": "0.0.3", + "description": "Header files and static wasm32 libraries for libvips and dependencies to build sharp as wasm32", + "author": "Lovell Fuller ", + "homepage": "https://sharp.pixelplumbing.com", + "repository": { + "type": "git", + "url": "git+https://github.com/lovell/sharp-libvips.git", + "directory": "npm/dev-wasm32" + }, + "license": "LGPL-3.0-or-later", + "funding": { + "url": "https://opencollective.com/libvips" + }, + "preferUnplugged": true, + "publishConfig": { + "access": "public" + }, + "files": [ + "include", + "lib", + "versions.json" + ], + "type": "commonjs", + "exports": { + "./include": "./include/index.js", + "./lib": "./lib/index.js", + "./versions": "./versions.json" + } +} diff --git a/npm/package.json b/npm/package.json index 9f629523..c58d3873 100644 --- a/npm/package.json +++ b/npm/package.json @@ -4,6 +4,7 @@ "private": "true", "workspaces": [ "dev", + "dev-wasm32", "darwin-x64", "darwin-arm64", "linux-arm", diff --git a/npm/populate.sh b/npm/populate.sh index f53f9581..8f91a3a7 100755 --- a/npm/populate.sh +++ b/npm/populate.sh @@ -38,10 +38,10 @@ generate_index() { remove_unused() { PACKAGE="$1" - if [ "$PACKAGE" != "dev" ]; then + if [[ "$PACKAGE" != "dev"* ]]; then rm -r "npm/$PACKAGE/include" + rm "npm/$PACKAGE/THIRD-PARTY-NOTICES.md" fi - rm "npm/$PACKAGE/THIRD-PARTY-NOTICES.md" } # Download and extract per-platform binaries