Skip to content

Commit 325f77e

Browse files
committed
feat: deno and meson build
1 parent 73006ae commit 325f77e

File tree

8 files changed

+121
-0
lines changed

8 files changed

+121
-0
lines changed

deno.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@discere-os/json-c.wasm",
3+
"exports": {
4+
".": "./",
5+
"./side": "./install/wasm/json-c-side.wasm",
6+
"./main": "./install/wasm/json-c-main.js"
7+
},
8+
"tasks": {
9+
"build:main:meson": "meson setup build-main --cross-file=scripts/emscripten.cross -Dmain_module=true -Dside_module=false -Dsimd=true --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-main json-c-main && meson install -C build-main",
10+
"build:side:meson": "meson setup build-side --cross-file=scripts/emscripten.cross -Dmain_module=false -Dside_module=true -Dsimd=true --prefix=$PWD/install -Dlibdir=wasm -Dbindir=wasm && meson compile -C build-side json-c-side && meson install -C build-side && python3 scripts/postinstall.py $PWD/install",
11+
"build:wasm:meson": "deno task build:main:meson && deno task build:side:meson"
12+
}
13+
}
14+

exports.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"_malloc",
3+
"_free"
4+
]
5+

meson.build

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
project('json-c.wasm', 'c', version: '0.17.0', meson_version: '>=1.3.0',
2+
default_options: ['buildtype=release'])
3+
4+
fs = import('fs')
5+
6+
if get_option('simd')
7+
add_project_arguments('-msimd128', language: 'c')
8+
endif
9+
10+
incs = include_directories('.')
11+
12+
# Collect core sources (exclude tests/apps/bench/examples)
13+
core_sources = []
14+
core_sources += fs.glob('*.c', exclude: ['tests/**', 'bench/**', 'benchmarks/**', 'examples/**', 'apps/**'])
15+
16+
core = static_library('jsonc_core', core_sources, include_directories: incs)
17+
18+
install_dir = get_option('bindir')
19+
20+
if get_option('main_module')
21+
main_link_args = [
22+
'--no-entry',
23+
'-sMODULARIZE=1',
24+
'-sEXPORT_ES6=1',
25+
'-sALLOW_MEMORY_GROWTH=1',
26+
'-sENVIRONMENT=web,webview,worker',
27+
'-sNO_FILESYSTEM=1',
28+
'-sEXPORTED_RUNTIME_METHODS=["cwrap","ccall","UTF8ToString"]',
29+
'-sEXPORTED_FUNCTIONS=@' + meson.project_source_root() / get_option('exports'),
30+
]
31+
executable('json-c-main', 'src/jsonc_wasm_module.c',
32+
link_with: core,
33+
include_directories: incs,
34+
install: true,
35+
link_args: main_link_args)
36+
endif
37+
38+
if get_option('side_module')
39+
shared_module('json-c-side', 'src/jsonc_wasm_side.c',
40+
link_whole: core,
41+
include_directories: incs,
42+
install: true,
43+
link_args: ['-sSIDE_MODULE=2', '-fPIC'])
44+
endif
45+

meson.options

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
option('main_module', type: 'boolean', value: false, description: 'Build MAIN module (ES6 + MODULARIZE)')
2+
option('side_module', type: 'boolean', value: false, description: 'Build SIDE module (for dlopen)')
3+
option('simd', type: 'boolean', value: true, description: 'Enable wasm SIMD (msimd128)')
4+
option('exports', type: 'string', value: 'exports.json', description: 'Path to EXPORTED_FUNCTIONS json for MAIN build')
5+

scripts/emscripten.cross

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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'
18+

scripts/postinstall.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
import shutil
5+
6+
def main():
7+
if len(sys.argv) < 2:
8+
print('Usage: postinstall.py <install_prefix>')
9+
sys.exit(1)
10+
prefix = sys.argv[1]
11+
wasm_dir = os.path.join(prefix, 'wasm')
12+
if not os.path.isdir(wasm_dir):
13+
return
14+
# Rename libjson-c-side.wasm -> json-c-side.wasm if present
15+
for name in os.listdir(wasm_dir):
16+
if name.startswith('libjson-c-side') and name.endswith('.wasm'):
17+
src = os.path.join(wasm_dir, name)
18+
dst = os.path.join(wasm_dir, 'json-c-side.wasm')
19+
if os.path.exists(dst):
20+
os.remove(dst)
21+
shutil.move(src, dst)
22+
23+
if __name__ == '__main__':
24+
main()
25+

src/jsonc_wasm_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdint.h>
2+
// Minimal stub entry for MAIN module builds
3+
int jsonc_wasm_version_stub(void) {
4+
return 1;
5+
}
6+

src/jsonc_wasm_side.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Empty translation unit to drive SIDE module link
2+
void __jsonc_wasm_side_marker(void) {}
3+

0 commit comments

Comments
 (0)