-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
build-webassembly.sh
executable file
·90 lines (80 loc) · 2.2 KB
/
build-webassembly.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
if ! command -v emcc >/dev/null 2>&1; then
echo 'Error: emcc is not installed.' >&2
echo 'Install from https://emscripten.org/docs/getting_started/downloads.html'
exit 1
fi
OUTPUT_BASE="salam-wa"
EDITOR_DIR="../Salam-Editor/"
MEMORY_FLAGS="-s ALLOW_MEMORY_GROWTH=1"
RUNTIME_FLAGS="-s EXIT_RUNTIME=0 -s NO_EXIT_RUNTIME=1"
COMMON_FLAGS="-s EXPORTED_RUNTIME_METHODS=['callMain'] -s TOTAL_STACK=8388608" # 8MB (8 * 1024 * 1024)
sources=(
"src/log.c"
"src/file.c"
"src/memory.c"
"src/array.c"
"src/parser.c"
"src/parser_layout.c"
"src/generator.c"
"src/generator_layout.c"
"src/downloader.c"
"src/generator_salam.c"
"src/generator_layout_style.c"
"src/generator_identifier.c"
"src/string_buffer.c"
"src/validator.c"
"src/validator_style.c"
"src/hashmap.c"
"src/hashmap_custom.c"
"src/array_custom.c"
"src/lexer.c"
"src/ast.c"
"src/ast_layout.c"
"src/ast_layout_style.c"
"src/main.c"
)
DEBUG=0
if [[ "$1" == "debug" ]]; then
DEBUG=1
echo "Debug mode enabled."
DEBUG_FLAGS="-s VERBOSE=1 -s ASSERTIONS=2"
else
DEBUG_FLAGS=""
echo "Debug mode not enabled."
fi
echo "Compiling C files to WebAssembly..."
emcc "${sources[@]}" -o ${OUTPUT_BASE}.html \
${MEMORY_FLAGS} \
${RUNTIME_FLAGS} \
${COMMON_FLAGS} \
${DEBUG_FLAGS} \
-s EXPORTED_FUNCTIONS="['_main']"
if [ $? -eq 0 ]; then
echo "Compilation successful. Output files:"
echo " ${OUTPUT_BASE}.html"
echo " ${OUTPUT_BASE}.js"
echo " ${OUTPUT_BASE}.wasm"
if command -v npx >/dev/null 2>&1; then
echo "Transpiling JavaScript for older browsers..."
if npx --no-install babel ${OUTPUT_BASE}.js --out-file ${OUTPUT_BASE}.transpiled.js --compact false; then
mv ${OUTPUT_BASE}.transpiled.js ${OUTPUT_BASE}.js
else
echo "Warning: Babel transpiling failed. JavaScript was not transpiled."
fi
else
echo "Warning: npx command not found. JavaScript will not be transpiled for older browsers."
fi
if [ -d "$EDITOR_DIR" ]; then
echo "Copying output files to $EDITOR_DIR"
cp ${OUTPUT_BASE}.html "$EDITOR_DIR"
cp ${OUTPUT_BASE}.js "$EDITOR_DIR"
cp ${OUTPUT_BASE}.wasm "$EDITOR_DIR"
echo "Files copied successfully."
else
echo "Directory $EDITOR_DIR does not exist. Skipping copy."
fi
else
echo "Compilation failed."
exit 1
fi