-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompile_libtirpc.sh
More file actions
executable file
·106 lines (84 loc) · 4.25 KB
/
compile_libtirpc.sh
File metadata and controls
executable file
·106 lines (84 loc) · 4.25 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
#!/usr/bin/env bash
set -euo pipefail
# Portable libtirpc build for WASI; no apt, no sudo.
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
: "${LIND_WASM_ROOT:=${LIND_WASM_ROOT:-$(cd "$REPO_ROOT/.." && pwd)/lind-wasm}}"
BASE_SYSROOT="${BASE_SYSROOT:-$LIND_WASM_ROOT/src/glibc/sysroot}"
LLVM_BIN="${LLVM_BIN:-$(ls -d "$LIND_WASM_ROOT"/clang+llvm-*/bin 2>/dev/null | head -n1)}"
WASM_OPT="${WASM_OPT:-$LIND_WASM_ROOT/tools/binaryen/bin/wasm-opt}"
if [[ -z "${LLVM_BIN}" || ! -x "$LLVM_BIN/clang" ]]; then
echo "ERROR: LLVM not found under $LIND_WASM_ROOT"; exit 1
fi
if [[ ! -r "$BASE_SYSROOT/include/wasm32-wasi/stdio.h" ]]; then
echo "ERROR: sysroot headers missing at $BASE_SYSROOT"; exit 1
fi
CC_WASI="$LLVM_BIN/clang --target=wasm32-unknown-wasi --sysroot=$BASE_SYSROOT"
AR="$LLVM_BIN/llvm-ar"
RANLIB="$LLVM_BIN/llvm-ranlib"
OVERLAY="$REPO_ROOT/build/sysroot_overlay"
MERGE_TMP="$OVERLAY/usr/lib/wasm32-wasi/merge_tmp"
LIND_DYLINK="${LIND_DYLINK:-0}"
mkdir -p "$OVERLAY/usr/include/tirpc" "$MERGE_TMP"
echo "[libtirpc] CC=$CC_WASI"
pushd "$REPO_ROOT/libtirpc" >/dev/null
autoreconf -fi
CC="$CC_WASI" AR="$AR" RANLIB="$RANLIB" \
CFLAGS="--sysroot=$BASE_SYSROOT -O2 -g" \
LDFLAGS="--sysroot=$BASE_SYSROOT" \
PKG_CONFIG=false \
./configure --host=wasm32-unknown-wasi --disable-gssapi --disable-shared --enable-static --with-pic --sysconfdir=/etc \
ac_cv_func_malloc_0_nonnull=yes ac_cv_func_memset=yes ac_cv_func_strchr=yes
make -j
rsync -a "tirpc/" "$OVERLAY/usr/include/tirpc/"
find "src" -name '*.o' -exec cp {} "$MERGE_TMP/" \;
"$AR" rcs "$OVERLAY/usr/lib/wasm32-wasi/libtirpc.a" "$MERGE_TMP"/*.o
"$RANLIB" "$OVERLAY/usr/lib/wasm32-wasi/libtirpc.a"
popd >/dev/null
if [[ "$LIND_DYLINK" != "1" ]]; then
echo "[libtirpc] done → $OVERLAY/usr/lib/wasm32-wasi/libtirpc.a"
exit 0
fi
mkdir -p "$OVERLAY/lib"
ADD_EXPORT_TOOL="$LIND_WASM_ROOT/tools/add-export-tool/add-export-tool"
STATIC_LIB="$OVERLAY/usr/lib/wasm32-wasi/libtirpc.a"
DYNAMIC_LIB_WASM="$OVERLAY/usr/lib/wasm32-wasi/libtirpc.wasm"
DYNAMIC_LIB_OPT="$OVERLAY/usr/lib/wasm32-wasi/libtirpc.opt.wasm"
DYNAMIC_LIB_OPT_CWASM="$OVERLAY/usr/lib/wasm32-wasi/libtirpc.opt.cwasm"
DYNAMIC_STAGED_LIB="$OVERLAY/lib/libtirpc.so"
"$LLVM_BIN/clang" \
--target=wasm32-unknown-wasi \
-fPIC \
--sysroot "$BASE_SYSROOT" \
-fvisibility=default \
-Wl,--import-memory \
-Wl,--shared-memory \
-Wl,--export-dynamic \
-Wl,--experimental-pic \
-Wl,--unresolved-symbols=import-dynamic \
-Wl,-shared \
-Wl,--whole-archive \
"$STATIC_LIB" \
-Wl,--no-whole-archive \
"$LIND_WASM_ROOT/src/glibc/build/lind_debug.o" \
-g -O0 -o "$DYNAMIC_LIB_WASM" || { echo "[libtirpc] ERROR: clang compilation failed"; exit 1; }
if [[ ! -f "$DYNAMIC_LIB_WASM" ]]; then
echo "[libtirpc] ERROR: Failed to generate '$DYNAMIC_LIB_WASM'; Exiting.."
exit 1
fi
"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __wasm_apply_tls_relocs func __wasm_apply_tls_relocs optional || { echo "[libtirpc] ERROR: add-export-tool tls failed"; exit 1; }
"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __wasm_apply_global_relocs func __wasm_apply_global_relocs optional || { echo "[libtirpc] ERROR: add-export-tool global failed"; exit 1; }
"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __stack_pointer global __stack_pointer optional || { echo "[libtirpc] ERROR: add-export-tool stack pointer failed"; exit 1; }
$WASM_OPT --enable-bulk-memory --enable-threads --epoch-injection --pass-arg=epoch-import --asyncify --pass-arg=asyncify-import-globals -O2 --debuginfo "$DYNAMIC_LIB_WASM" -o "$DYNAMIC_LIB_OPT" || { echo "[libtirpc] ERROR: wasm-opt failed on '$DYNAMIC_LIB_OPT'; Exiting.."; exit 1; }
if [[ ! -f "$DYNAMIC_LIB_OPT" ]]; then
echo "[libtirpc] ERROR: Failed to generate '$DYNAMIC_LIB_OPT'; Exiting.."
exit 1
fi
# do precompile
$LIND_WASM_ROOT/scripts/lind_compile --precompile-only "$DYNAMIC_LIB_OPT"|| { echo "[libtirpc] ERROR: lind_compile failed on '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."; exit 1; }
if [[ ! -f "$DYNAMIC_LIB_OPT_CWASM" ]]; then
echo "[libtirpc] ERROR: Failed to generate '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."
exit 1
fi
cp "$DYNAMIC_LIB_OPT_CWASM" "$DYNAMIC_STAGED_LIB"
echo "[libtirpc] Dynamic shared library staged as $DYNAMIC_STAGED_LIB"