This commit is contained in:
Surma
2024-08-14 14:07:21 +01:00
parent d278efb148
commit 6d061d8fa2
9 changed files with 415 additions and 92 deletions

View File

@@ -2,15 +2,15 @@
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": "nixpkgs",
"nixpkgs": "nixpkgs_2",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1723012113,
"narHash": "sha256-AJGsmwDnheWMjZWUqgiGtBjbxMmvLvMp5WJhmTRhJ4w=",
"lastModified": 1723616992,
"narHash": "sha256-jDHYfEECzFwZm4huz7AbPjlH3jJ4/2ns9PddtFA5XMY=",
"owner": "nix-community",
"repo": "fenix",
"rev": "3dab4ada5b0c5a22d56dbfd7e140c16f3df2e69a",
"rev": "7bad6c7ff73b784a9c7de9147626c8d5d5072809",
"type": "github"
},
"original": {
@@ -38,22 +38,6 @@
}
},
"nixpkgs": {
"locked": {
"lastModified": 1722813957,
"narHash": "sha256-IAoYyYnED7P8zrBFMnmp7ydaJfwTnwcnqxUElC1I26Y=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "cb9a96f23c491c081b38eab96d22fa958043c9fa",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1717179513,
"narHash": "sha256-vboIEwIQojofItm2xGCdZCzW96U85l9nDW3ifMuAIdM=",
@@ -69,22 +53,38 @@
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1723362943,
"narHash": "sha256-dFZRVSgmJkyM0bkPpaYRtG/kRMRTorUIDj8BxoOt1T4=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "a58bc8ad779655e790115244571758e8de055e3d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs_2",
"nixpkgs": "nixpkgs",
"rust-helpers": "rust-helpers",
"wasm-bindgen": "wasm-bindgen"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1722925734,
"narHash": "sha256-5maDP51jkTUCHDU7tKdbsMQTk0QMNZjNGZw2z++YaaI=",
"lastModified": 1723561310,
"narHash": "sha256-a3KMMsIDvdo+ClLabh5wfJoa17YTSvy2wDLb8yZCXvc=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "b23142209ef632475915e858f5f20901ef7c12da",
"rev": "78c2bdce860dbd996a8083224d01a96660dd6a15",
"type": "github"
},
"original": {
@@ -94,6 +94,24 @@
"type": "github"
}
},
"rust-helpers": {
"inputs": {
"fenix": "fenix",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1,
"narHash": "sha256-AP5n4QOJS+7V9O0hnZPavnvctfsgfwybu5XOT1HdRcw=",
"path": "/nix/store/azy8v63kkb5d39z0zmpixx3hhq2l4h6i-source/nix/rust-helpers",
"type": "path"
},
"original": {
"path": "/nix/store/azy8v63kkb5d39z0zmpixx3hhq2l4h6i-source/nix/rust-helpers",
"type": "path"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
@@ -117,12 +135,12 @@
},
"locked": {
"lastModified": 1,
"narHash": "sha256-ZBQyty6638OHI5UZBk79+VY3LOAO51OexUMn4AQWS3Y=",
"path": "/nix/store/qmv8wrsh9s2i7z7b5a5iak6nark83267-source/nix/wasm-bindgen",
"narHash": "sha256-gb2y+PtDL1uqCVebD4ViN5ruWOmHLburm1gp6xWxx9Y=",
"path": "/nix/store/azy8v63kkb5d39z0zmpixx3hhq2l4h6i-source/nix/wasm-bindgen",
"type": "path"
},
"original": {
"path": "/nix/store/qmv8wrsh9s2i7z7b5a5iak6nark83267-source/nix/wasm-bindgen",
"path": "/nix/store/azy8v63kkb5d39z0zmpixx3hhq2l4h6i-source/nix/wasm-bindgen",
"type": "path"
}
}

View File

@@ -2,76 +2,70 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/24.05";
flake-utils.url = "github:numtide/flake-utils";
fenix.url = "github:nix-community/fenix";
wasm-bindgen = {
url = "../../nix/wasm-bindgen";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};
rust-helpers = {
url = "../../nix/rust-helpers";
inputs.nixpkgs.follows = "nixpkgs";
};
squoosh-codec-builders = {
url = "../../nix/squoosh-codec-builders";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-helpers.follows = "rust-helpers";
inputs.wasm-bindgen.follows = "wasm-bindgen";
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
fenix,
wasm-bindgen,
squoosh-codec-builders,
}:
flake-utils.lib.eachDefaultSystem (
system:
with nixpkgs.legacyPackages.${system};
let
target = "wasm32-unknown-unknown";
pkgs = nixpkgs.legacyPackages.${system};
toolchain =
with fenix.packages.${system};
combine [
stable.rustc
stable.cargo
targets.${target}.stable.rust-std
];
src = ./.;
rustPlatform = pkgs.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
};
cargoLockFile = "${src}/Cargo.lock";
vendor = rustPlatform.importCargoLock {
lockFile = cargoLockFile;
};
wasm-bindgen-bin = wasm-bindgen.lib.buildFromCargoLock {
inherit system cargoLockFile;
sha256 = "sha256-HTElSB76gqCpDu8S0ZJlfd/S4ftMrbwxFgJM9OXBRz8=";
};
in
with pkgs;
# wasm-bindgen-bin = wasm-bindgen.lib.buildFromCargoLock {
# inherit system cargoLock;
# sha256 = "sha256-HTElSB76gqCpDu8S0ZJlfd/S4ftMrbwxFgJM9OXBRz8=";
# };
{
packages = rec {
default = resize-squoosh;
resize-squoosh = stdenv.mkDerivation {
name = "squoosh-resize";
inherit src;
nativeBuildInputs = [
toolchain
wasm-bindgen-bin
];
dontConfigure = true;
buildPhase = ''
runHook preBuild
export CARGO_HOME=$TMPDIR/.cargo
cargo build \
--config 'source.crates-io.replace-with="vendored-sources"' \
--config 'source.vendored-sources.directory="${vendor}"' \
--offline \
--target ${target} -r
wasm-bindgen --target web --out-dir $out ./target/wasm32-unknown-unknown/release/*.wasm
runHook postBuild
'';
dontInstall = true;
resize-squoosh = squoosh-codec-builders.lib.buildSquooshCodecRust {
name = "resize-squoosh";
inherit system src;
cargoLock = {
lockFile = "${src}/Cargo.lock";
};
};
# resize-squoosh = stdenv.mkDerivation {
# name = "squoosh-resize";
# inherit src;
# nativeBuildInputs = [
# toolchain
# wasm-bindgen-bin
# ];
# dontConfigure = true;
# buildPhase = ''
# runHook preBuild
# export CARGO_HOME=$TMPDIR/.cargo
# cargo build \
# --config 'source.crates-io.replace-with="vendored-sources"' \
# --config 'source.vendored-sources.directory="${vendoredDependencies}"' \
# --offline \
# --target ${target} -r
# wasm-bindgen --target web --out-dir $out ./target/wasm32-unknown-unknown/release/*.wasm
# runHook postBuild
# '';
# dontInstall = true;
# };
installScript = writeShellScriptBin "install.sh" ''
${pkgs.coreutils}/bin/mkdir -p wasm_build

View File

@@ -0,0 +1,34 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {Uint8Array} input_image
* @param {number} input_width
* @param {number} input_height
* @param {number} output_width
* @param {number} output_height
* @param {number} typ_idx
* @param {boolean} premultiply
* @param {boolean} color_space_conversion
* @returns {Uint8ClampedArray}
*/
export function resize(input_image: Uint8Array, input_width: number, input_height: number, output_width: number, output_height: number, typ_idx: number, premultiply: boolean, color_space_conversion: boolean): Uint8ClampedArray;
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly resize: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number) => number;
readonly __wbindgen_free: (a: number, b: number) => void;
}
/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;

120
codecs/resize/wasm_build/squoosh_resize.js generated Normal file
View File

@@ -0,0 +1,120 @@
let wasm;
let cachegetUint8Memory0 = null;
function getUint8Memory0() {
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachegetUint8Memory0;
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
let cachegetInt32Memory0 = null;
function getInt32Memory0() {
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachegetInt32Memory0;
}
let cachegetUint8ClampedMemory0 = null;
function getUint8ClampedMemory0() {
if (cachegetUint8ClampedMemory0 === null || cachegetUint8ClampedMemory0.buffer !== wasm.memory.buffer) {
cachegetUint8ClampedMemory0 = new Uint8ClampedArray(wasm.memory.buffer);
}
return cachegetUint8ClampedMemory0;
}
function getClampedArrayU8FromWasm0(ptr, len) {
return getUint8ClampedMemory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} input_image
* @param {number} input_width
* @param {number} input_height
* @param {number} output_width
* @param {number} output_height
* @param {number} typ_idx
* @param {boolean} premultiply
* @param {boolean} color_space_conversion
* @returns {Uint8ClampedArray}
*/
export function resize(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
var ptr0 = passArray8ToWasm0(input_image, wasm.__wbindgen_malloc);
var len0 = WASM_VECTOR_LEN;
wasm.resize(retptr, ptr0, len0, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getClampedArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
async function load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);
} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
} else {
throw e;
}
}
}
const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);
} else {
const instance = await WebAssembly.instantiate(module, imports);
if (instance instanceof WebAssembly.Instance) {
return { instance, module };
} else {
return instance;
}
}
}
async function init(input) {
if (typeof input === 'undefined') {
input = new URL('squoosh_resize_bg.wasm', import.meta.url);
}
const imports = {};
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}
const { instance, module } = await load(await input, imports);
wasm = instance.exports;
init.__wbindgen_wasm_module = module;
return wasm;
}
export default init;

Binary file not shown.

View File

@@ -0,0 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function resize(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number): number;
export function __wbindgen_free(a: number, b: number): void;

View File

@@ -0,0 +1,74 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/24.05";
fenix.url = "github:nix-community/fenix";
};
outputs =
{
self,
nixpkgs,
fenix,
}:
{
lib = {
buildRustPackage =
{
system,
target,
src,
cargoLock ? {
lockFile = "${src}/Cargo.lock";
},
release ? true,
...
}@args:
with nixpkgs.legacyPackages.${system};
let
# Setup a toolchain for the the host system targeting `target`.
toolchain =
with fenix.packages.${system};
combine [
stable.rustc
stable.cargo
targets.${target}.stable.rust-std
];
# Create `vendor` folder with all dependencies.
vendoredDependencies = rustPlatform.importCargoLock cargoLock;
rustcTargetDir = "target/${target}/${if release then "release" else "debug"}";
in
stdenv.mkDerivation (
(removeAttrs args [ "cargoLock" ])
// {
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
toolchain
jq
rsync
];
dontConfigure = true;
buildPhase = ''
runHook preBuild
export CARGO_HOME=$TMPDIR/.cargo
cargo build \
--config 'source.crates-io.replace-with="vendored-sources"' \
--config 'source.vendored-sources.directory="${vendoredDependencies}"' \
--offline \
--target ${target} ${if release then "-r" else ""}
runHook postBuild
'';
installPhase = ''
runHook preInstall;
mkdir -p $out
find ${rustcTargetDir} -type f -maxdepth 1 | \
xargs -I ___X -n1 cp ___X $out
runHook postInstall;
'';
}
);
};
};
}

View File

@@ -0,0 +1,66 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/24.05";
fenix.url = "github:nix-community/fenix";
wasm-bindgen = {
url = "../wasm-bindgen";
inputs.nixpkgs.follows = "nixpkgs";
};
rust-helpers = {
url = "../rust-helpers";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
fenix,
wasm-bindgen,
rust-helpers,
}:
{
lib = {
buildSquooshCodecRust =
{
name,
system,
src,
cargoLock ? {
lockFile = "${src}/Cargo.lock";
},
wasmBindgenSha,
...
}@args:
with nixpkgs.legacyPackages.${system};
let
wasm-bindgen-bin = wasm-bindgen.lib.buildFromCargoLock {
inherit system cargoLock;
sha256 = wasmBindgenSha;
};
codecBuild = rust-helpers.lib.buildRustPackage {
inherit system src cargoLock;
name = "${name}-codec";
target = "wasm32-unknown-unknown";
};
in
stdenv.mkDerivation (
(removeAttrs args [ "cargoLock" ])
// {
inherit codecBuild;
dontConfigure = true;
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ wasm-bindgen-bin ];
buildPhase = ''
runHook preBuild
wasm-bindgen --target web --out-dir $out $codecBuild/*.wasm
runHook postBuild
'';
dontInstall = true;
}
);
};
};
}

View File

@@ -9,38 +9,48 @@
version,
sha256,
}:
with nixpkgs.legacyPackages.${system};
let
pkgs = nixpkgs.legacyPackages.${system};
wasm-bindgen-src = pkgs.fetchCrate {
src = pkgs.fetchCrate {
pname = "wasm-bindgen-cli";
inherit version sha256;
};
in
pkgs.rustPlatform.buildRustPackage {
name = "wasm-bindgen-cli";
buildInputs = [
pkgs.curl
pkgs.darwin.apple_sdk.frameworks.Security
];
src = wasm-bindgen-src;
cargoLock = {
lockFile = "${wasm-bindgen-src}/Cargo.lock";
lockFile = "${src}/Cargo.lock";
};
in
rustPlatform.buildRustPackage {
name = "wasm-bindgen-cli";
inherit src cargoLock;
buildInputs = [
curl
darwin.apple_sdk.frameworks.Security
];
doCheck = false;
};
buildFromCargoLock =
{
system,
cargoLockFile,
cargoLock,
sha256,
}:
with nixpkgs.legacyPackages.${system};
assert (cargoLock.lockFile or null == null) != (cargoLock.lockFileContents or null == null);
let
pkgs = nixpkgs.legacyPackages.${system};
cargoLock = pkgs.lib.importTOML cargoLockFile;
lockFileContents =
if cargoLock.lockFile != null then
builtins.readFile cargoLock.lockFile
else
cargoLock.lockFileContents;
parsedLockFile = builtins.fromTOML lockFileContents;
wasm-bindgen-version =
(pkgs.lib.lists.findFirst (x: x.name == "wasm-bindgen") null cargoLock.package).version;
(lib.lists.findFirst (x: x.name == "wasm-bindgen") null parsedLockFile.package).version;
in
assert wasm-bindgen-version != null;
self.lib.build {
inherit system sha256;
version = wasm-bindgen-version;