Attempt at Nix’ing mozjpeg

This commit is contained in:
Surma
2024-08-06 18:30:17 +01:00
parent 19beb1a7ab
commit 9ce5da7531
3 changed files with 147 additions and 42 deletions

View File

@@ -1,10 +1,6 @@
CODEC_URL := https://github.com/mozilla/mozjpeg/archive/v3.3.1.tar.gz
CODEC_DIR := node_modules/mozjpeg
CODEC_OUT_RELATIVE := .libs/libjpeg.a rdswitch.o
CODEC_OUT := $(addprefix $(CODEC_DIR)/, $(CODEC_OUT_RELATIVE))
ENVIRONMENT = worker ENVIRONMENT = worker
OUT_JS := enc/mozjpeg_enc.js enc/mozjpeg_node_enc.js dec/mozjpeg_node_dec.js OUT_JS := enc/mozjpeg_enc.js
OUT_WASM := $(OUT_JS:.js=.wasm) OUT_WASM := $(OUT_JS:.js=.wasm)
.PHONY: all clean .PHONY: all clean
@@ -15,47 +11,15 @@ all: $(OUT_JS)
$(filter enc/%,$(OUT_JS)): enc/mozjpeg_enc.cpp $(filter enc/%,$(OUT_JS)): enc/mozjpeg_enc.cpp
$(filter dec/%,$(OUT_JS)): dec/mozjpeg_dec.cpp $(filter dec/%,$(OUT_JS)): dec/mozjpeg_dec.cpp
enc/mozjpeg_node_enc.js dec/mozjpeg_node_dec.js: ENVIRONMENT = node %.js:
%.js: $(CODEC_OUT)
$(CXX) \ $(CXX) \
-I $(CODEC_DIR) \ -I ${MOZJPEG}/include \
${CXXFLAGS} \ ${CXXFLAGS} \
${LDFLAGS} \ ${LDFLAGS} \
--bind \ --bind \
-s ENVIRONMENT=$(ENVIRONMENT) \ -s ENVIRONMENT=$(ENVIRONMENT) \
-s EXPORT_ES6=1 \ -s EXPORT_ES6=1 \
-o $@ \ -o $@ \
$+ ${MOZJPEG}/lib/rdswitch.o \
${MOZJPEG}/lib/libjpeg.a \
# This one is a bit special: there is no rule for .libs/libjpeg.a $+ \
# so we use libjpeg.la which implicitly builds that one instead.
$(CODEC_DIR)/.libs/libjpeg.a: $(CODEC_DIR)/Makefile
$(MAKE) -C $(CODEC_DIR) libjpeg.la
$(CODEC_DIR)/rdswitch.o: $(CODEC_DIR)/Makefile
$(MAKE) -C $(CODEC_DIR) rdswitch.o
$(CODEC_DIR)/Makefile: $(CODEC_DIR)/configure
cd $(CODEC_DIR) && ./configure \
--disable-shared \
--without-turbojpeg \
--without-simd \
--without-arith-enc \
--without-arith-dec \
--with-build-date=squoosh
# ^ If not provided with a dummy value, MozJPEG includes a build date in the
# binary as part of the version string, making binaries different each time.
$(CODEC_DIR)/configure: $(CODEC_DIR)/configure.ac
cd $(CODEC_DIR) && autoreconf -iv
$(CODEC_DIR)/configure.ac: $(CODEC_DIR)
$(CODEC_DIR):
mkdir -p $@
curl -sL $(CODEC_URL) | tar xz --strip 1 -C $@
clean:
$(RM) $(OUT_JS) $(OUT_WASM)
$(MAKE) -C $(CODEC_DIR) clean

43
codecs/mozjpeg/flake.lock generated Normal file
View File

@@ -0,0 +1,43 @@
{
"nodes": {
"mozjpeg": {
"flake": false,
"locked": {
"lastModified": 1499684294,
"narHash": "sha256-frpQdkk7bJE5qbV70fdL1FsC4eI0Fm8FWshqBQxCRtk=",
"owner": "mozilla",
"repo": "mozjpeg",
"rev": "f154ccc091cbc22141cdfd531e5ad1fdc5bc53c7",
"type": "github"
},
"original": {
"owner": "mozilla",
"ref": "v3.3.1",
"repo": "mozjpeg",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1722640603,
"narHash": "sha256-TcXjLVNd3VeH1qKPH335Tc4RbFDbZQX+d7rqnDUoRaY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "81610abc161d4021b29199aa464d6a1a521e0cc9",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"mozjpeg": "mozjpeg",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

98
codecs/mozjpeg/flake.nix Normal file
View File

@@ -0,0 +1,98 @@
{
inputs = {
mozjpeg = {
url = "github:mozilla/mozjpeg/v3.3.1";
flake = false;
# type = "github";
# owner = "mozilla";
# repo = "mozjpeg";
# rev = "v3.3.1";
# hash = "sha256-frpQdkk7bJE5qbV70fdL1FsC4eI0Fm8FWshqBQxCRtk=";
};
};
outputs =
{
self,
nixpkgs,
mozjpeg,
}:
let
system = "aarch64-darwin";
pkgs = import nixpkgs { inherit system; };
in
with pkgs;
rec {
packages.${system} = {
default = stdenv.mkDerivation {
name = "mozjpeg-squoosh";
src = ./.;
nativeBuildInputs = [ emscripten packages.${system}.mozjpeg ];
MOZJPEG = packages.${system}.mozjpeg;
dontConfigure = true;
buildPhase = ''
export HOME=$TMPDIR
emmake make -j$(nproc)
'';
installPhase = ''
mkdir -p $out
cp
'';
};
mozjpeg = stdenv.mkDerivation {
name = "mozjpeg";
src = mozjpeg;
nativeBuildInputs = [
autoconf
automake
libtool
emscripten
pkg-config
];
preConfigure = ''
# $HOME is required for Emscripten to work.
# See: https://nixos.org/manual/nixpkgs/stable/#emscripten
export HOME=$TMPDIR
autoreconf -if
'';
# configurePhase = ''
# runHook preConfigure
# emconfigure ./configure $configureFlags
# runHook postConfigure
# '';
configureScript = "emconfigure ./configure";
configureFlags = [
"--disable-shared"
"--without-turbojpeg"
"--without-simd"
"--without-arith-enc"
"--without-arith-dec"
"--with-build-date=squoosh"
];
buildFlags = [
"libjpeg.la"
"rdswitch.o"
];
buildPhase = ''
emmake make -j$(nproc) $buildFlags
'';
installPhase = ''
mkdir -p $out/lib
mkdir -p $out/include
cp .libs/libjpeg.a $out/lib
cp rdswitch.o $out/lib
cp *.h $out/include
'';
checkPhase = ''
true
'';
};
};
devShells.${system}.default = pkgs.mkShell {
shellHook = ''
echo "Path to MozJPEG: ${mozjpeg}"
'';
};
};
}