-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlind_compile
More file actions
executable file
·533 lines (487 loc) · 16.5 KB
/
lind_compile
File metadata and controls
executable file
·533 lines (487 loc) · 16.5 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env bash
set -Eeuo pipefail
# Cross-compile C programs to wasm using the lind-wasm toolchain.
#
# Default pipeline:
# .c -> .wasm (clang) -> (wasm-opt) -> .cwasm (lind-boot --precompile)
#
# Usage:
# # full pipeline (default)
# lind_compile hello.c
#
# # run individual steps
# lind_compile --compile-only hello.c # .c -> .wasm
# lind_compile --opt-only hello.wasm # optimize existing .wasm in place
# lind_compile --precompile-only hello.wasm # .wasm -> .cwasm
#
# # print commands (any mode)
# lind_compile --print-cmd hello.c
# lind_compile --compile-only --verbose hello.c
#
# Notes:
# - Options must be specified *before* the source path.
# - For compile-only and default modes, the input must be a .c file.
# - For opt-only and precompile-only modes, the input must be a .wasm file.
#
# No assumption about current working directory.
usage() {
cat >&2 <<EOF
usage: $(basename "$0") [OPTIONS] <source> [-- <clang args>]
Modes (at most one, defaults to full pipeline):
(no mode flag) : .c -> .wasm -> (wasm-opt) -> .cwasm
--compile-only : .c -> .wasm
--opt-only : .wasm -> .wasm (in-place optimization)
--precompile-only : .wasm -> .cwasm (AOT compile via lind-boot)
Compilation backend (applies to full and --compile-only modes):
(no backend flag) : dynamic build — PIE executable with dlopen support (default)
-s, --static : static build — traditional WASM binary without dynamic linking
--compile-library : library build — shared WASM library (.wasm) to be loaded via dlopen
Note: --compile-library takes precedence over -s/--static if both are given.
Common options:
-v, --print-cmd, --verbose print the underlying commands that are executed
--print-args, --debug-args print parsed arguments after option handling
--compile-grate add grate-specific clang flags (exports pass_fptr_to_wt);
compatible with dynamic and static backends
--fpcast-emu enable function-pointer-cast emulation via wasm-opt
(applies to full and --opt-only modes; for dynamic/library
builds also passes --pass-arg=relocatable-fpcast)
--no-default-clang-flags do not add lind_compile's optional clang flags
-h, --help show this help
Additional clang args:
Any extra arguments after the source file are passed through to clang.
Use -- to separate lind_compile flags from clang flags if needed.
Examples:
$(basename "$0") hello.c # dynamic build (default)
$(basename "$0") -s hello.c # static build
$(basename "$0") --compile-library mylib.c # shared library for dlopen
$(basename "$0") --compile-only hello.c
$(basename "$0") --compile-grate hello.c
$(basename "$0") --fpcast-emu hello.c
$(basename "$0") --opt-only build/hello.wasm
$(basename "$0") --precompile-only build/hello.wasm
$(basename "$0") --print-cmd hello.c
$(basename "$0") --no-default-clang-flags hello.c -- -target=wasm32-unknown-wasi
$(basename "$0") hello.c -- -lm
$(basename "$0") hello.c -lm
EOF
}
MODE="full" # full, compile-only, opt-only, precompile-only
PRINT_CMDS="false"
SHARED_BUILD="true"
WITH_FPCAST="false"
PRINT_ARGS="false"
NO_DEFAULT_CLANG_FLAGS="false"
COMPILE_GRATE="false"
COMPILE_LIBRARY="false"
ORIGINAL_ARGS=("$@")
set_mode() {
local new_mode="$1"
if [[ "${MODE}" != "full" && "${MODE}" != "${new_mode}" ]]; then
echo "error: multiple mode flags specified (already ${MODE}, got ${new_mode})" >&2
usage
exit 2
fi
MODE="${new_mode}"
}
run_cmd() {
# Helper to optionally print and then run a command.
if [[ "${PRINT_CMDS}" == "true" ]]; then
printf '+ ' >&2
printf '%q ' "$@" >&2
printf '\n' >&2
fi
"$@"
}
# --- option parsing ---
while [[ "$#" -gt 0 ]]; do
case "$1" in
--compile-only)
set_mode "compile-only"
;;
--opt-only)
set_mode "opt-only"
;;
--precompile-only)
set_mode "precompile-only"
;;
-v|--print-cmd|--verbose)
PRINT_CMDS="true"
;;
-s|--static)
SHARED_BUILD="false"
;;
--fpcast-emu)
WITH_FPCAST="true"
;;
--print-args|--debug-args)
PRINT_ARGS="true"
;;
--no-default-clang-flags)
NO_DEFAULT_CLANG_FLAGS="true"
;;
--compile-library)
COMPILE_LIBRARY="true"
;;
--compile-grate)
COMPILE_GRATE="true"
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
echo "error: unknown option: $1" >&2
usage
exit 2
;;
*)
# first non-option: treat as source; stop parsing options
break
;;
esac
shift
done
if [[ "$#" -lt 1 ]]; then
echo "error: expected a source file argument" >&2
usage
exit 2
fi
SRC_ARG="$1"
shift
EXTRA_CLANG_ARGS=("$@")
if [[ "${#EXTRA_CLANG_ARGS[@]}" -gt 0 && "${EXTRA_CLANG_ARGS[0]}" == "--" ]]; then
EXTRA_CLANG_ARGS=("${EXTRA_CLANG_ARGS[@]:1}")
fi
if [[ "${PRINT_ARGS}" == "true" ]]; then
printf '[DEBUG] original args: ' >&2
printf '%q ' "${ORIGINAL_ARGS[@]}" >&2
printf '\n' >&2
printf '[DEBUG] mode: %s\n' "${MODE}" >&2
printf '[DEBUG] src arg: %s\n' "${SRC_ARG}" >&2
printf '[DEBUG] extra clang args: ' >&2
printf '%q ' "${EXTRA_CLANG_ARGS[@]}" >&2
printf '\n' >&2
fi
# --- repo root discovery (env var -> script dir -> git) ---
if [[ -n "${LIND_WASM_ROOT:-}" && -d "${LIND_WASM_ROOT}" ]]; then
REPO_ROOT="${LIND_WASM_ROOT}"
else
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "${SCRIPT_DIR}/../Makefile" ]]; then
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
else
if command -v git >/dev/null 2>&1; then
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)"
else
REPO_ROOT=""
fi
fi
fi
if [[ -z "${REPO_ROOT}" || ! -d "${REPO_ROOT}" ]]; then
echo "ERROR: Could not locate lind-wasm repo root." >&2
echo "Hint: export LIND_WASM_ROOT=/path/to/lind-wasm" >&2
exit 2
fi
# --- args & path normalization ---
# Normalize input path to absolute
if [[ "${SRC_ARG}" = /* ]]; then
SRC="${SRC_ARG}"
else
SRC="$PWD/${SRC_ARG}"
fi
SRC_DIR="$(cd -- "$(dirname -- "${SRC}")" && pwd)"
SRC_BASE="$(basename -- "${SRC}")"
SRC="${SRC_DIR}/${SRC_BASE}"
[[ -f "${SRC}" ]] || { echo "error: source not found: ${SRC}" >&2; exit 2; }
EXT="${SRC##*.}"
OUT_WASM=""
OUT_CWASM=""
case "${MODE}" in
full|"compile-only")
if [[ "${EXT}" != "c" ]]; then
echo "error: ${MODE} mode expects a .c file, got: ${SRC}" >&2
usage
exit 2
fi
OUT_WASM="${SRC%.c}.wasm"
OUT_CWASM="${SRC%.c}.cwasm"
;;
"opt-only")
if [[ "${EXT}" != "wasm" ]]; then
echo "error: ${MODE} mode expects a .wasm file, got: ${SRC}" >&2
usage
exit 2
fi
OUT_WASM="${SRC}"
;;
"precompile-only")
if [[ "${EXT}" != "wasm" ]]; then
echo "error: ${MODE} mode expects a .wasm file, got: ${SRC}" >&2
usage
exit 2
fi
OUT_WASM="${SRC}"
OUT_CWASM="${SRC%.wasm}.cwasm"
;;
*)
echo "error: internal: unknown mode '${MODE}'" >&2
exit 2
;;
esac
# --- tool paths & quick checks (anchored to repo) ---
CLANG_BIN="clang" # must be on PATH
WASM_OPT_BIN="${REPO_ROOT}/tools/binaryen/bin/wasm-opt"
LINDBOOT_BIN="${REPO_ROOT}/build/lind-boot"
LINDFS_ROOT="${REPO_ROOT}/lindfs/"
if [[ ! -x "${LINDBOOT_BIN}" ]]; then
echo "ERROR: lind-boot missing: ${LINDBOOT_BIN}" >&2
exit 127 # Note: This is the traditional "command not found" exit code, can be changed as needed
fi
SYSROOT="${SYSROOT:-${REPO_ROOT}/build/sysroot}"
if [[ ! -d "${SYSROOT}" ]]; then
SYSROOT="${REPO_ROOT}/src/glibc/sysroot"
fi
# Only check tools needed for the selected mode, to be more flexible
if [[ "${MODE}" == "full" || "${MODE}" == "compile-only" ]]; then
command -v "${CLANG_BIN}" >/dev/null 2>&1 || { echo "error: clang not found on PATH" >&2; exit 2; }
[[ -d "${SYSROOT}" ]] || { echo "error: sysroot missing at ${SYSROOT}" >&2; exit 2; }
fi
if [[ "${MODE}" == "full" || "${MODE}" == "opt-only" ]]; then
[[ -x "${WASM_OPT_BIN}" ]] || { echo "error: wasm-opt not found at ${WASM_OPT_BIN}" >&2; exit 2; }
fi
if [[ "${MODE}" == "full" || "${MODE}" == "precompile-only" ]]; then
[[ -x "${LINDBOOT_BIN}" ]] || { echo "error: lind-boot not found at ${LINDBOOT_BIN}" >&2; exit 2; }
fi
# --- debug output (post-normalization) ---
if [[ "${PRINT_ARGS}" == "true" && ( "${MODE}" == "full" || "${MODE}" == "compile-only" ) ]]; then
debug_mandatory_clang_flags=(
"--sysroot=${SYSROOT}"
--target=wasm32-unknown-wasi
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export="__stack_pointer",--export=__stack_low,--export=__tls_base
)
debug_default_clang_flags=(
-pthread
-g
-O0
)
if [[ "${COMPILE_GRATE}" == "true" ]]; then
debug_default_clang_flags+=(-Wl,--export=pass_fptr_to_wt)
fi
if [[ "${NO_DEFAULT_CLANG_FLAGS}" == "true" ]]; then
debug_default_clang_flags=()
fi
printf '[DEBUG] mandatory clang args: ' >&2
printf '%q ' "${debug_mandatory_clang_flags[@]}" >&2
printf '\n' >&2
printf '[DEBUG] default clang args: ' >&2
printf '%q ' "${debug_default_clang_flags[@]}" >&2
printf '\n' >&2
printf '[DEBUG] effective clang args: ' >&2
printf '%q ' "${debug_mandatory_clang_flags[@]}" "${debug_default_clang_flags[@]}" "${EXTRA_CLANG_ARGS[@]}" "${SRC}" -o "${OUT_WASM}" >&2
printf '\n' >&2
fi
# --- debug output (post-normalization) ---
if [[ "${PRINT_ARGS}" == "true" && ( "${MODE}" == "full" || "${MODE}" == "compile-only" ) ]]; then
debug_mandatory_clang_flags=(
"--sysroot=${SYSROOT}"
--target=wasm32-unknown-wasi
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export="__stack_pointer",--export=__stack_low,--export=__tls_base
)
debug_default_clang_flags=(
-pthread
-g
-O0
)
if [[ "${COMPILE_GRATE}" == "true" ]]; then
debug_default_clang_flags+=(-Wl,--export=pass_fptr_to_wt)
fi
if [[ "${NO_DEFAULT_CLANG_FLAGS}" == "true" ]]; then
debug_default_clang_flags=()
fi
printf '[DEBUG] mandatory clang args: ' >&2
printf '%q ' "${debug_mandatory_clang_flags[@]}" >&2
printf '\n' >&2
printf '[DEBUG] default clang args: ' >&2
printf '%q ' "${debug_default_clang_flags[@]}" >&2
printf '\n' >&2
printf '[DEBUG] effective clang args: ' >&2
printf '%q ' "${debug_mandatory_clang_flags[@]}" "${debug_default_clang_flags[@]}" "${EXTRA_CLANG_ARGS[@]}" "${SRC}" -o "${OUT_WASM}" >&2
printf '\n' >&2
fi
# --- individual steps ---
do_static_compile() {
local -a mandatory_clang_flags=(
"--sysroot=${SYSROOT}"
--target=wasm32-unknown-wasi
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export="__stack_pointer",--export=__stack_low,--export=__tls_base,--export=__wasm_init_tls
)
local -a default_clang_flags=(
-pthread
-g
-O0
)
if [[ "${COMPILE_GRATE}" == "true" ]]; then
default_clang_flags+=(-Wl,--export=pass_fptr_to_wt)
fi
if [[ "${NO_DEFAULT_CLANG_FLAGS}" == "true" ]]; then
default_clang_flags=()
fi
run_cmd "${CLANG_BIN}" "${mandatory_clang_flags[@]}" "${default_clang_flags[@]}" \
"${EXTRA_CLANG_ARGS[@]}" "${SRC}" "$SYSROOT/lib/wasm32-wasi/lind_utils.o" -o "${OUT_WASM}"
}
do_dynamic_compile() {
local -a mandatory_clang_flags=(
--target=wasm32-unknown-wasi
"--sysroot=${SYSROOT}"
"-nostartfiles" # we manually enforce the crt1 file for dynamic build
-Wl,-pie # required flag for compiling into shared wasm binary
-Wl,--import-table # table needs to be shared across module in dynamic build
-Wl,--import-memory # memory needs to be shared across module for threading support
-Wl,--export-memory
-Wl,--max-memory=67108864
-Wl,--allow-undefined # treat undefined symbol as external library symbol
-Wl,--unresolved-symbols=import-dynamic
-Wl,--export=__wasm_call_ctors
-Wl,--export-if-defined=__wasm_init_tls
-Wl,--export=__tls_base
)
local -a default_clang_flags=(
-pthread
-g
-O0
-fPIC
)
if [[ "${COMPILE_GRATE}" == "true" ]]; then
default_clang_flags+=(-Wl,--export=pass_fptr_to_wt)
fi
if [[ "${NO_DEFAULT_CLANG_FLAGS}" == "true" ]]; then
default_clang_flags=()
fi
run_cmd "${CLANG_BIN}" "${default_clang_flags[@]}" "${mandatory_clang_flags[@]}"\
"${EXTRA_CLANG_ARGS[@]}" "${SRC}" "$SYSROOT/lib/wasm32-wasi/set_stack_pointer.o" "$SYSROOT/lib/wasm32-wasi/crt1_shared.o" "$SYSROOT/lib/wasm32-wasi/lind_utils.o" -o "${OUT_WASM}"
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_tls_relocs func __wasm_apply_tls_relocs optional
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_global_relocs func __wasm_apply_global_relocs optional
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __stack_pointer global __stack_pointer
}
do_library_compile() {
local -a mandatory_clang_flags=(
--target=wasm32-unknown-wasi
"--sysroot=${SYSROOT}"
"-fvisibility=default"
"-Wl,--import-memory"
"-Wl,--shared-memory"
"-Wl,--export-dynamic"
"-Wl,--experimental-pic"
"-Wl,--unresolved-symbols=import-dynamic"
"-Wl,-shared"
-Wl,--export=__wasm_call_ctors
-Wl,--export-if-defined=__wasm_init_tls
-Wl,--export=__tls_base
)
local -a default_clang_flags=(
-g
-O0
-fPIC
)
if [[ "${NO_DEFAULT_CLANG_FLAGS}" == "true" ]]; then
default_clang_flags=()
fi
run_cmd "${CLANG_BIN}" "${default_clang_flags[@]}" "${mandatory_clang_flags[@]}"\
"${EXTRA_CLANG_ARGS[@]}" "${SRC}" "$SYSROOT/lib/wasm32-wasi/lind_utils.o" -o "${OUT_WASM}"
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_tls_relocs func __wasm_apply_tls_relocs optional
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __wasm_apply_global_relocs func __wasm_apply_global_relocs optional
run_cmd $REPO_ROOT/tools/add-export-tool/add-export-tool "${OUT_WASM}" "${OUT_WASM}" __stack_pointer global __stack_pointer
}
do_compile() {
if [[ "${COMPILE_LIBRARY}" == "true" ]]; then
do_library_compile
else
if [[ "${SHARED_BUILD}" == "true" ]]; then
do_dynamic_compile
else
do_static_compile
fi
fi
}
do_optimize() {
FPCAST_ARG=""
if [[ "${WITH_FPCAST}" == "true" ]]; then
if [[ "${SHARED_BUILD}" == "true" || "${COMPILE_LIBRARY}" == "true" ]]; then
FPCAST_ARG="--fpcast-emu --pass-arg=relocatable-fpcast"
else
FPCAST_ARG="--fpcast-emu"
fi
fi
if [[ "${SHARED_BUILD}" == "true" ]]; then
# --enable-bulk-memory --enable-threads: required wasm feature enabled for lind
# --epoch-injection --pass-arg=epoch-import: enable signal support, with dynamic build feature (epoch-import), with hint for applying epoch on main module (epoch-main-module)
# --asyncify --pass-arg=asyncify-import-globals --pass-arg=epoch-main-module: enable asyncify for multiprocess, with dynamic build feature (import-globals)
# --debuginfo: retain debug information for better debugging experience
run_cmd "${WASM_OPT_BIN}" \
--enable-bulk-memory --enable-threads \
--epoch-injection --pass-arg=epoch-import --pass-arg=epoch-main-module \
--asyncify --pass-arg=asyncify-import-globals \
$FPCAST_ARG \
--debuginfo \
"${OUT_WASM}" -o "${OUT_WASM}"
else
if [[ "${COMPILE_LIBRARY}" == "true" ]]; then
run_cmd "${WASM_OPT_BIN}" \
--enable-bulk-memory --enable-threads \
--epoch-injection --pass-arg=epoch-import \
--asyncify --pass-arg=asyncify-import-globals \
$FPCAST_ARG \
--debuginfo \
"${OUT_WASM}" -o "${OUT_WASM}"
else
run_cmd "${WASM_OPT_BIN}" --epoch-injection --asyncify $FPCAST_ARG -O2 --debuginfo "${OUT_WASM}" -o "${OUT_WASM}"
fi
fi
}
do_precompile() {
run_cmd "${LINDBOOT_BIN}" --precompile "${OUT_WASM}"
}
cp_to_lindfs() {
mkdir -p "${LINDFS_ROOT}"
if [[ -n "${OUT_CWASM}" && -f "${OUT_CWASM}" ]]; then
cp "${OUT_CWASM}" "${LINDFS_ROOT}"
else
cp "${OUT_WASM}" "${LINDFS_ROOT}"
fi
}
# --- run selected pipeline ---
case "${MODE}" in
full)
do_compile
do_optimize
do_precompile
cp_to_lindfs
;;
"compile-only")
do_compile
cp_to_lindfs
;;
"opt-only")
do_optimize
;;
"precompile-only")
do_precompile
cp_to_lindfs
;;
esac
# --- result summary ---
case "${MODE}" in
full)
echo "OK: ${OUT_CWASM}"
;;
"compile-only"|"opt-only")
echo "OK: ${OUT_WASM}"
;;
"precompile-only")
echo "OK: ${OUT_CWASM}"
;;
esac