Skip to content

Commit d2af828

Browse files
committed
feat: meson build
1 parent 5f6d064 commit d2af828

File tree

7 files changed

+101
-6
lines changed

7 files changed

+101
-6
lines changed

deno.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
"demo": "deno run --allow-read --allow-write demo-deno-fixed.ts",
1818
"test": "deno test --allow-read --no-check tests/deno/",
1919
"test:basic": "deno test --allow-read --no-check tests/deno/basic.test.ts",
20-
"build": "deno task build:wasm",
21-
"build:wasm": "./build-dual.sh all",
22-
"build:side": "./build-dual.sh side",
23-
"build:main": "./build-dual.sh main",
20+
"build": "deno task build:wasm:meson",
21+
"build:main:meson": "meson setup build-main --cross-file=scripts/emscripten.cross --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-main libpng-main && meson install -C build-main",
22+
"build:side:meson": "meson setup build-side --cross-file=scripts/emscripten.cross --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-side libpng-side && meson install -C build-side",
23+
"build:wasm:meson": "deno task build:main:meson && deno task build:side:meson",
2424
"build:npm": "deno run --allow-all _build_npm.ts",
2525
"build:all": "deno task build:wasm && deno task build:npm",
2626
"publish:npm": "deno task build:all && cd npm && npm publish",
2727
"publish:dry": "deno task build:all && cd npm && npm publish --dry-run",
28-
"clean": "rm -rf build-dual/ install/ dist/ npm/",
28+
"clean": "rm -rf build-* install/ dist/ npm/",
2929
"check": "deno check src/lib/index.ts",
3030
"check:all": "deno check src/lib/index.ts && deno check demo-deno-fixed.ts"
3131
},
@@ -47,4 +47,4 @@
4747
"include": ["tests/deno/"],
4848
"exclude": ["tests/unit/"]
4949
}
50-
}
50+
}

meson.build

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
project('libpng.wasm', 'c', version: '1.6.44', meson_version: '>=1.3.0')
2+
3+
simd = get_option('simd')
4+
5+
incs = include_directories('.')
6+
7+
png_sources = files(
8+
'png.c','pngerror.c','pngget.c','pngmem.c','pngpread.c','pngread.c','pngrio.c','pngrtran.c','pngrutil.c','pngset.c','pngtrans.c','pngwio.c','pngwrite.c','pngwtran.c','pngwutil.c'
9+
)
10+
11+
cargs = []
12+
if simd
13+
cargs += ['-msimd128']
14+
endif
15+
16+
# MAIN links to Emscripten zlib
17+
executable('libpng-main',
18+
files('wasm/libpng_wasm_module.c') + png_sources,
19+
include_directories: incs,
20+
c_args: cargs,
21+
link_args: [
22+
'-sUSE_ZLIB=1','-sWASM=1','-sMODULARIZE=1','-sEXPORT_ES6=1','-sEXPORT_NAME=LibpngModule',
23+
'-sALLOW_MEMORY_GROWTH=1','-sNO_FILESYSTEM=1','-sEXPORTED_RUNTIME_METHODS=["cwrap","ccall","UTF8ToString"]','-O3','-flto'
24+
],
25+
install: true,
26+
install_dir: get_option('bindir')
27+
)
28+
29+
# SIDE relies on host to provide zlib; allow unresolved symbols
30+
shared_module('libpng-side',
31+
files('wasm/libpng_wasm_side.c') + png_sources,
32+
include_directories: incs,
33+
name_prefix: '',
34+
c_args: cargs,
35+
link_args: ['-sSIDE_MODULE=2','-fPIC','-O3','-flto','-sERROR_ON_UNDEFINED_SYMBOLS=0','-sEXPORTED_FUNCTIONS=["_libpng_wasm_version"]'],
36+
install: true,
37+
install_dir: get_option('libdir')
38+
)
39+

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, 'libpng-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/libpng_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 "png.h"
3+
4+
EMSCRIPTEN_KEEPALIVE
5+
const char* libpng_wasm_version(void) {
6+
return png_get_libpng_ver(NULL);
7+
}
8+

wasm/libpng_wasm_side.c

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

0 commit comments

Comments
 (0)