Skip to content

Commit

Permalink
Improve cpp nix builder
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Sep 7, 2024
1 parent 00082be commit 24e63ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
44 changes: 11 additions & 33 deletions codecs/mozjpeg/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,24 @@
system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) stdenv runCommand writeShellScriptBin;
inherit (pkgs) callPackage stdenv;

buildSquooshCppCodec = callPackage (import ../../nix/squoosh-cxx-builder) {};
mkInstallable = callPackage (import ../../nix/mk-installable) {};

in
rec {
mkInstallable {
packages = rec {

default = mozjpeg-squoosh;
mozjpeg-squoosh = stdenv.mkDerivation {
mozjpeg-squoosh = buildSquooshCppCodec {
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 ${./.}/Makefile $out/
'';
nativeBuildInputs = [
pkgs.emscripten
pkgs.mozjpeg
];
src = ./.;
MOZJPEG = mozjpeg;
dontConfigure = true;
buildPhase = ''
export HOME=$TMPDIR
emmake make -j$(nproc)
'';
installPhase = ''
mkdir -p $out
cp -r enc $out
'';
decoder = null;
};

mozjpeg = stdenv.mkDerivation {
name = "mozjpeg";
src = mozjpeg-src;
Expand Down Expand Up @@ -82,17 +71,6 @@
'';
dontFixup = true;
};
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 ${mozjpeg-squoosh}/* wasm_build/
'';
};
apps = {
install = {
type = "app";
program = "${packages.installScript}/bin/install.sh";
};
};
}
);
Expand Down
2 changes: 1 addition & 1 deletion codecs/rotate/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
system:
let
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) callPackage writeShellScriptBin;
inherit (pkgs) callPackage;

buildSquooshRustCodec= callPackage (import ../../nix/squoosh-rust-builder) {fenix = fenix.packages.${system};};
mkInstallable = callPackage (import ../../nix/mk-installable) {};
Expand Down
31 changes: 31 additions & 0 deletions nix/squoosh-cxx-builder/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
pkgs,
stdenv,
runCommand,
}:
{
name,
src,
nativeBuildInputs ? [ ],
encoder ? "enc",
decoder ? "dec",
...
}@args:

stdenv.mkDerivation (
final:
args
// {
inherit name src;
nativeBuildInputs = [ pkgs.emscripten ] ++ nativeBuildInputs;
buildPhase = ''
export HOME=$TMPDIR
emmake make -j$(nproc)
'';
installPhase = ''
mkdir -p $out
${if (encoder != null) then "cp -r ${encoder} $out" else ""}
${if (decoder != null) then "cp -r ${decoder} $out" else ""}
'';
}
)

0 comments on commit 24e63ea

Please sign in to comment.