Skip to content

Commit

Permalink
Build webp
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Aug 7, 2024
1 parent a0f9fea commit b5bd766
Show file tree
Hide file tree
Showing 37 changed files with 473 additions and 6,253 deletions.
16 changes: 16 additions & 0 deletions codecs/webp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ LIBWEBP_FLAGS = -I${WEBP}/include -L${WEBP}/lib -lwebp

$(OUT_JS):
$(LD) \
-O3 \
-flto \
-std=c++17 \
-s FILESYSTEM=0 \
-s PTHREAD_POOL_SIZE=navigator.hardwareConcurrency \
-s ALLOW_MEMORY_GROWTH=1 \
-s TEXTDECODER=2 \
-s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 \
$(LIBWEBP_FLAGS) \
$(LDFLAGS) \
-lembind \
Expand All @@ -28,6 +36,14 @@ $(OUT_JS):

%.o: %.cpp
$(CXX) -c \
-O3 \
-flto \
-std=c++17 \
-s FILESYSTEM=0 \
-s PTHREAD_POOL_SIZE=navigator.hardwareConcurrency \
-s ALLOW_MEMORY_GROWTH=1 \
-s TEXTDECODER=2 \
-s NODEJS_CATCH_EXIT=0 -s NODEJS_CATCH_REJECTION=0 \
$(LIBWEBP_FLAGS) \
$(CXXFLAGS) \
-o $@ \
Expand Down
168 changes: 94 additions & 74 deletions codecs/webp/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,112 @@
flake-utils,
webp-src,
}:
let
optionSets = {
base = {
simd = false;
};
simd = {
simd = true;
};
};
in
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
with pkgs;
rec {
packages = rec {
default = webp-squoosh;
webp-squoosh = stdenv.mkDerivation {
name = "mozjpeg-squoosh";
# Only copy files that are actually relevant to avoid unnecessary
# cache invalidations.
src = runCommand "src" { } ''
mkdir $out
cp -r ${./.}/enc $out/
cp -r ${./.}/dec $out/
cp ${./.}/Makefile $out/
'';
nativeBuildInputs = [
emscripten
webp
];
WEBP = webp;
dontConfigure = true;
buildPhase = ''
export HOME=$TMPDIR
emmake make -j$(nproc)
'';
installPhase = ''
mkdir -p $out
cp -r enc dec $out
packageBuilder =
with pkgs;
name:
{ simd }:
{
"webp-squoosh-${name}" = stdenv.mkDerivation {
name = "webp-squoosh-${name}";
# Only copy files that are actually relevant to avoid unnecessary
# cache invalidations.
src = runCommand "src" { } ''
mkdir $out
cp -r ${./.}/enc $out/
cp -r ${./.}/dec $out/
cp ${./.}/Makefile $out/
'';
nativeBuildInputs = [
emscripten
self.packages.${system}."webp-${name}"
];
WEBP = self.packages.${system}."webp-${name}";
dontConfigure = true;
buildPhase = ''
export HOME=$TMPDIR
emmake make -j$(nproc)
'';
installPhase = ''
mkdir -p $out
cp -r enc dec $out
'';
};
"webp-${name}" = stdenv.mkDerivation {
name = "webp-${name}";
src = webp-src;
nativeBuildInputs = [
emscripten
cmake
];
configurePhase = ''
# $HOME is required for Emscripten to work.
# See: https://nixos.org/manual/nixpkgs/stable/#emscripten
export HOME=$TMPDIR
mkdir -p $TMPDIR/build
emcmake cmake \
-DCMAKE_INSTALL_PREFIX=$out \
-DCMAKE_DISABLE_FIND_PACKAGE_Threads=1 \
-DWEBP_BUILD_ANIM_UTILS=0 \
-DWEBP_BUILD_CWEBP=0 \
-DWEBP_BUILD_DWEBP=0 \
-DWEBP_BUILD_GIF2WEBP=0 \
-DWEBP_BUILD_IMG2WEBP=0 \
-DWEBP_BUILD_VWEBP=0 \
-DWEBP_BUILD_WEBPINFO=0 \
-DWEBP_BUILD_WEBPMUX=0 \
-DWEBP_BUILD_EXTRAS=0 \
${if simd then "-DWEBP_ENABLE_SIMD=1" else ""} \
-B $TMPDIR/build \
.
'';
buildPhase = ''
export HOME=$TMPDIR
cd $TMPDIR/build
emmake make V=1 -j$(nproc) --trace
'';
installPhase = ''
cd $TMPDIR/build
make install
'';
dontFixup = true;
};
"install-${name}" = writeShellScriptBin "install.sh" ''
${pkgs.coreutils}/bin/mkdir -p wasm_build/${name}
${pkgs.rsync}/bin/rsync --chmod=u+w -r ${
self.packages.${system}."webp-squoosh-${name}"
}/* wasm_build/${name}
'';
};
webp = stdenv.mkDerivation {
name = "webp";
src = webp-src;
nativeBuildInputs = [
# autoconf
# automake
# libtool
emscripten
# pkg-config
cmake
];
configurePhase = ''
# $HOME is required for Emscripten to work.
# See: https://nixos.org/manual/nixpkgs/stable/#emscripten
export HOME=$TMPDIR
mkdir -p $TMPDIR/build
emcmake cmake \
-DCMAKE_INSTALL_PREFIX=$out \
-DCMAKE_DISABLE_FIND_PACKAGE_Threads=1 \
-DWEBP_BUILD_ANIM_UTILS=0 \
-DWEBP_BUILD_CWEBP=0 \
-DWEBP_BUILD_DWEBP=0 \
-DWEBP_BUILD_GIF2WEBP=0 \
-DWEBP_BUILD_IMG2WEBP=0 \
-DWEBP_BUILD_VWEBP=0 \
-DWEBP_BUILD_WEBPINFO=0 \
-DWEBP_BUILD_WEBPMUX=0 \
-DWEBP_BUILD_EXTRAS=0 \
-B $TMPDIR/build \
.
'';
buildPhase = ''
export HOME=$TMPDIR
cd $TMPDIR/build
emmake make V=1 -j$(nproc) --trace
'';
installPhase = ''
cd $TMPDIR/build
make install
'';
dontFixup = true;
};

forEachOption = pkgs.callPackage (import ../../nix/for-each-option.nix) { };
packageVariants = forEachOption packageBuilder optionSets;
in
with pkgs;
{
packages = packageVariants // {
installScript = writeShellScriptBin "install.sh" ''
${pkgs.coreutils}/bin/rm -rf wasm_build
${pkgs.coreutils}/bin/mkdir -p wasm_build
${pkgs.rsync}/bin/rsync --chmod=u+w -r ${webp-squoosh}/* wasm_build/
${self.packages.${system}.install-base}/bin/install.sh
${self.packages.${system}.install-simd}/bin/install.sh
'';
};
apps = {
install = {
type = "app";
program = "${packages.installScript}/bin/install.sh";
program = "${self.packages.${system}.installScript}/bin/install.sh";
};
};
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions codecs/webp/wasm_build/base/dec/webp_dec.js

Large diffs are not rendered by default.

Binary file added codecs/webp/wasm_build/base/dec/webp_dec.wasm
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions codecs/webp/wasm_build/base/enc/webp_enc.js

Large diffs are not rendered by default.

Binary file added codecs/webp/wasm_build/base/enc/webp_enc.wasm
Binary file not shown.
Loading

0 comments on commit b5bd766

Please sign in to comment.