Skip to content

Commit 6543af6

Browse files
committed
feat: meson build
1 parent 122533c commit 6543af6

File tree

8 files changed

+118
-6
lines changed

8 files changed

+118
-6
lines changed

deno.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
"test:basic": "deno test --allow-read --no-check tests/deno/basic.test.ts",
2121
"bench": "deno bench --allow-read bench/",
2222
"bench:compression": "deno bench --allow-read bench/compression.bench.ts",
23-
"build": "deno task build:wasm",
24-
"build:wasm": "./build-dual.sh",
25-
"build:side": "npm run build:side",
26-
"build:main": "npm run build:main",
23+
"build": "deno task build:wasm:meson",
24+
"build:main:meson": "meson setup build-main --cross-file=scripts/emscripten.cross --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-main libjpeg-main && meson install -C build-main",
25+
"build:side:meson": "meson setup build-side --cross-file=scripts/emscripten.cross --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-side libjpeg-side && meson install -C build-side",
26+
"build:wasm:meson": "deno task build:main:meson && deno task build:side:meson",
2727
"build:npm": "deno run --allow-all _build_npm.ts",
2828
"build:all": "deno task build:wasm && deno task build:npm",
2929
"publish:npm": "deno task build:all && cd npm && npm publish",
3030
"publish:dry": "deno task build:all && cd npm && npm publish --dry-run",
3131
"publish:jsr": "deno publish",
32-
"clean": "rm -rf build-main/ build-side/ dist/ npm/",
32+
"clean": "rm -rf build-* install/ dist/ npm/",
3333
"check": "deno check src/lib/index.ts",
3434
"check:all": "deno check src/lib/index.ts && deno check demo-deno.ts",
3535
"fmt": "deno fmt src/ tests/ demo-deno.ts bench/",
@@ -54,4 +54,4 @@
5454
"include": ["tests/deno/"],
5555
"exclude": ["tests/unit/"]
5656
}
57-
}
57+
}

meson.build

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
project('libjpeg-turbo.wasm', 'c', version: '3.0.5', meson_version: '>=1.3.0')
2+
3+
simd = get_option('simd')
4+
cargs = []
5+
if simd
6+
cargs += ['-msimd128']
7+
endif
8+
9+
executable('libjpeg-main',
10+
files('wasm/libjpeg_wasm_module.c'),
11+
c_args: cargs,
12+
link_args: [
13+
'-sUSE_LIBJPEG=1','-sWASM=1','-sMODULARIZE=1','-sEXPORT_ES6=1','-sEXPORT_NAME=LibjpegModule',
14+
'-sALLOW_MEMORY_GROWTH=1','-sNO_FILESYSTEM=1','-sEXPORTED_RUNTIME_METHODS=["cwrap","ccall","UTF8ToString"]','-O3','-flto'
15+
],
16+
install: true,
17+
install_dir: get_option('bindir')
18+
)
19+
20+
shared_module('libjpeg-side',
21+
files('wasm/libjpeg_wasm_side.c'),
22+
name_prefix: '',
23+
c_args: cargs,
24+
link_args: ['-sSIDE_MODULE=2','-fPIC','-O3','-flto','-sEXPORTED_FUNCTIONS=["_libjpeg_wasm_version"]'],
25+
install: true,
26+
install_dir: get_option('libdir')
27+
)
28+

meson.options

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
option('main_module', type: 'boolean', value: true)
2+
option('side_module', type: 'boolean', value: true)
3+
option('simd', type: 'boolean', value: true)

scripts/emscripten.cross

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[binaries]
2+
c = 'emcc'
3+
cpp = 'em++'
4+
ar = 'emar'
5+
ranlib = 'emranlib'
6+
strip = 'llvm-strip'
7+
pkgconfig = 'em-pkg-config'
8+
9+
[built-in options]
10+
c_args = []
11+
c_link_args = []
12+
13+
[host_machine]
14+
system = 'emscripten'
15+
cpu_family = 'wasm32'
16+
cpu = 'wasm32'
17+
endian = 'little'

scripts/postinstall.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
import os, sys, shutil
3+
4+
def main():
5+
prefix = os.environ.get('MESON_INSTALL_DESTDIR_PREFIX') or (sys.argv[1] if len(sys.argv) > 1 else None)
6+
if not prefix:
7+
prefix = os.getcwd()
8+
wasm_dir = os.path.join(prefix, 'wasm')
9+
if not os.path.isdir(wasm_dir):
10+
return 0
11+
target = os.path.join(wasm_dir, 'libjpeg-turbo-side.wasm')
12+
if os.path.exists(target):
13+
return 0
14+
for f in os.listdir(wasm_dir):
15+
if f.endswith('.wasm'):
16+
shutil.copy2(os.path.join(wasm_dir,f), target)
17+
break
18+
return 0
19+
20+
if __name__ == '__main__':
21+
raise SystemExit(main())
22+

wasm/libjpeg_wasm_module.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <emscripten.h>
2+
#include "wasm/jversion.h"
3+
4+
EMSCRIPTEN_KEEPALIVE
5+
const char* libjpeg_wasm_version(void) {
6+
return JVERSION;
7+
}
8+

wasm/libjpeg_wasm_side.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "wasm/jversion.h"
2+
3+
const char* libjpeg_wasm_version(void) {
4+
return JVERSION;
5+
}
6+

wasm/wasm_compat_side.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* wasm_compat_side.c - SIDE_MODULE compatibility layer for libjpeg-turbo
3+
* Copyright (c) 2025 Superstruct Ltd, New Zealand
4+
* Licensed under same license as libjpeg-turbo
5+
*/
6+
7+
#include <emscripten.h>
8+
9+
/* SIDE_MODULE export declarations for libjpeg-turbo */
10+
EMSCRIPTEN_KEEPALIVE int wasm_side_module_init(void) {
11+
return 1; /* Success */
12+
}
13+
14+
EMSCRIPTEN_KEEPALIVE const char* wasm_side_module_version(void) {
15+
return "3.1.2-wasm-native";
16+
}
17+
18+
/* Forward declarations for SIDE_MODULE exports */
19+
extern int jpeg_wasm_init(void);
20+
extern void jpeg_wasm_cleanup(void);
21+
22+
EMSCRIPTEN_KEEPALIVE int libjpeg_turbo_side_init(void) {
23+
return jpeg_wasm_init();
24+
}
25+
26+
EMSCRIPTEN_KEEPALIVE void libjpeg_turbo_side_cleanup(void) {
27+
jpeg_wasm_cleanup();
28+
}

0 commit comments

Comments
 (0)