Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions gnulib/compile_gnulib.sh
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When LIND_DYLINK=1, it configures with --enable-shared --disable-static but then links the static archive (libgnu.a) into a shared wasm. If configure is building a shared lib, why re-link from the .a? If it's building static to
then convert, the configure flags should stay --enable-static

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ CC_WASI="$LLVM_BIN/clang --target=wasm32-unknown-wasi --sysroot=$BASE_SYSROOT"
AR="$LLVM_BIN/llvm-ar"
RANLIB="$LLVM_BIN/llvm-ranlib"

LIND_DYLINK="${LIND_DYLINK:-0}"
WASM_OPT="${WASM_OPT:-$LIND_WASM_ROOT/tools/binaryen/bin/wasm-opt}"

# Where your gnulib checkout lives
GNULIB_DIR="${GNULIB_DIR:-$REPO_ROOT/gnulib}"

Expand Down Expand Up @@ -85,15 +88,22 @@ if [[ -x ./bootstrap ]]; then
./bootstrap --no-git --skip-po || true
fi

if [[ "$LIND_DYLINK" == "1" ]]; then
EXTRA_CFLAGS="-fPIC"
CONFIG_ARGS="--enable-shared --disable-static"
else
EXTRA_CFLAGS=""
CONFIG_ARGS="--disable-shared --enable-static"
fi

CC="$CC_WASI" AR="$AR" RANLIB="$RANLIB" \
CFLAGS="--sysroot=$BASE_SYSROOT -O2 -g" \
CFLAGS="--sysroot=$BASE_SYSROOT -O2 -g $EXTRA_CFLAGS" \
LDFLAGS="--sysroot=$BASE_SYSROOT" \
PKG_CONFIG=false \
./configure \
--host=wasm32-unknown-wasi \
--disable-shared \
--enable-static \
--disable-nls \
$CONFIG_ARGS \
"${CACHE_VARS[@]}"

make -j
Expand Down Expand Up @@ -130,6 +140,63 @@ fi

popd >/dev/null

echo "[gnulib] done → $OVERLAY/usr/lib/wasm32-wasi/libgnu.a"
echo "[gnulib] headers → $OVERLAY/usr/include/gnulib/"
if [[ "$LIND_DYLINK" != "1" ]]; then
echo "[gnulib] done → $OVERLAY/usr/lib/wasm32-wasi/libgnu.a"
echo "[gnulib] headers → $OVERLAY/usr/include/gnulib/"
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/libgnu.a"
DYNAMIC_LIB_WASM="$OVERLAY/usr/lib/wasm32-wasi/libgnu.wasm"
DYNAMIC_LIB_OPT="$OVERLAY/usr/lib/wasm32-wasi/libgnu.opt.wasm"
DYNAMIC_LIB_OPT_CWASM="$OVERLAY/usr/lib/wasm32-wasi/libgnu.opt.cwasm"
DYNAMIC_STAGED_LIB="$OVERLAY/lib/libgnu.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 "[libgnu] ERROR: clang compilation failed"; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_WASM" ]]; then
echo "[libgnu] 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 "[libgnu] 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 "[libgnu] ERROR: add-export-tool global failed"; exit 1; }

"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __stack_pointer global __stack_pointer optional || { echo "[libgnu] 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 "[libgnu] ERROR: wasm-opt failed on '$DYNAMIC_LIB_OPT'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT" ]]; then
echo "[libgnu] 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 "[libgnu] ERROR: lind_compile failed on '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT_CWASM" ]]; then
echo "[libgnu] ERROR: Failed to generate '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."
exit 1
fi

cp "$DYNAMIC_LIB_OPT_CWASM" "$DYNAMIC_STAGED_LIB"
echo "[libgnu] Dynamic shared library staged as $DYNAMIC_STAGED_LIB"
65 changes: 63 additions & 2 deletions libtirpc/compile_libtirpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

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
Expand All @@ -22,6 +22,7 @@ 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"
Expand All @@ -42,4 +43,64 @@ find "src" -name '*.o' -exec cp {} "$MERGE_TMP/" \;
"$RANLIB" "$OVERLAY/usr/lib/wasm32-wasi/libtirpc.a"

popd >/dev/null
echo "[libtirpc] done → $OVERLAY/usr/lib/wasm32-wasi/libtirpc.a"
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"

117 changes: 116 additions & 1 deletion openssl/compile_openssl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ if [[ ! -r "$BASE_SYSROOT/include/wasm32-wasi/stdio.h" ]]; then
echo "ERROR: sysroot headers missing at $BASE_SYSROOT"; exit 1
fi

LIND_DYLINK="${LIND_DYLINK:-0}"
WASM_OPT="${WASM_OPT:-$LIND_WASM_ROOT/tools/binaryen/bin/wasm-opt}"

CC_WASI="$LLVM_BIN/clang --target=wasm32-unknown-wasi --sysroot=$BASE_SYSROOT"
AR="$LLVM_BIN/llvm-ar"
RANLIB="$LLVM_BIN/llvm-ranlib"
Expand Down Expand Up @@ -47,4 +50,116 @@ cp libcrypto.a "$OVERLAY/usr/lib/wasm32-wasi/"
rsync -a include/openssl/ "$OVERLAY/usr/include/openssl/"

popd >/dev/null
echo "[openssl] done → $OVERLAY/usr/lib/wasm32-wasi/libssl.a, libcrypto.a"
if [[ "$LIND_DYLINK" != "1" ]]; then
echo "[openssl] done → $OVERLAY/usr/lib/wasm32-wasi/libssl.a, libcrypto.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/libssl.a"
DYNAMIC_LIB_WASM="$OVERLAY/usr/lib/wasm32-wasi/libssl.wasm"
DYNAMIC_LIB_OPT="$OVERLAY/usr/lib/wasm32-wasi/libssl.opt.wasm"
DYNAMIC_LIB_OPT_CWASM="$OVERLAY/usr/lib/wasm32-wasi/libssl.opt.cwasm"
DYNAMIC_STAGED_LIB="$OVERLAY/lib/libssl.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 "[openssl] ERROR: clang compilation failed"; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_WASM" ]]; then
echo "[openssl] 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 "[openssl] 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 "[openssl] ERROR: add-export-tool global failed"; exit 1; }

"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __stack_pointer global __stack_pointer optional || { echo "[openssl] 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 "[openssl] ERROR: wasm-opt failed on '$DYNAMIC_LIB_OPT'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT" ]]; then
echo "[openssl] 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 "[openssl] ERROR: lind_compile failed on '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT_CWASM" ]]; then
echo "[openssl] ERROR: Failed to generate '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."
exit 1
fi

cp "$DYNAMIC_LIB_OPT_CWASM" "$DYNAMIC_STAGED_LIB"
echo "[openssl] Dynamic shared library staged as $DYNAMIC_STAGED_LIB"

STATIC_LIB="$OVERLAY/usr/lib/wasm32-wasi/libcrypto.a"
DYNAMIC_LIB_WASM="$OVERLAY/usr/lib/wasm32-wasi/libcrypto.wasm"
DYNAMIC_LIB_OPT="$OVERLAY/usr/lib/wasm32-wasi/libcrypto.opt.wasm"
DYNAMIC_LIB_OPT_CWASM="$OVERLAY/usr/lib/wasm32-wasi/libcrypto.opt.cwasm"
DYNAMIC_STAGED_LIB="$OVERLAY/lib/libcrypto.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 "[openssl] ERROR: clang compilation failed"; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_WASM" ]]; then
echo "[openssl] 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 "[openssl] 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 "[openssl] ERROR: add-export-tool global failed"; exit 1; }

"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __stack_pointer global __stack_pointer optional || { echo "[openssl] 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 "[openssl] ERROR: wasm-opt failed on '$DYNAMIC_LIB_OPT'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT" ]]; then
echo "[openssl] 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 "[openssl] ERROR: lind_compile failed on '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT_CWASM" ]]; then
echo "[openssl] ERROR: Failed to generate '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."
exit 1
fi

cp "$DYNAMIC_LIB_OPT_CWASM" "$DYNAMIC_STAGED_LIB"
echo "[openssl] Dynamic shared library staged as $DYNAMIC_STAGED_LIB"

64 changes: 63 additions & 1 deletion zlib/compile_zlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ if [[ ! -r "$BASE_SYSROOT/include/wasm32-wasi/stdio.h" ]]; then
echo "ERROR: sysroot headers missing at $BASE_SYSROOT"; exit 1
fi

LIND_DYLINK="${LIND_DYLINK:-0}"
WASM_OPT="${WASM_OPT:-$LIND_WASM_ROOT/tools/binaryen/bin/wasm-opt}"

CC_WASI="$LLVM_BIN/clang --target=wasm32-unknown-wasi --sysroot=$BASE_SYSROOT"
AR="$LLVM_BIN/llvm-ar"
RANLIB="$LLVM_BIN/llvm-ranlib"
Expand All @@ -40,4 +43,63 @@ cp libz.a "$OVERLAY/usr/lib/wasm32-wasi/libz.a"
cp zlib.h zconf.h "$OVERLAY/usr/include/"

popd >/dev/null
echo "[zlib] done → $OVERLAY/usr/lib/wasm32-wasi/libz.a"

if [[ "$LIND_DYLINK" != "1" ]]; then
echo "[zlib] done ?~F~R $OVERLAY/usr/lib/wasm32-wasi/libz.a"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broken unicode arrow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seem to print properly though. Will recheck.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly fine but might just want to avoid this stuff not sure about portability

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/libz.a"
DYNAMIC_LIB_WASM="$OVERLAY/usr/lib/wasm32-wasi/libz.wasm"
DYNAMIC_LIB_OPT="$OVERLAY/usr/lib/wasm32-wasi/libz.opt.wasm"
DYNAMIC_LIB_OPT_CWASM="$OVERLAY/usr/lib/wasm32-wasi/libz.opt.cwasm"
DYNAMIC_STAGED_LIB="$OVERLAY/lib/libz.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 "[zlib] ERROR: clang compilation failed"; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_WASM" ]]; then
echo "[zlib] 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 "[zlib] 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 "[zlib] ERROR: add-export-tool global failed"; exit 1; }

"$ADD_EXPORT_TOOL" "$DYNAMIC_LIB_WASM" "$DYNAMIC_LIB_WASM" __stack_pointer global __stack_pointer optional || { echo "[zlib] 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 "[zlib] ERROR: wasm-opt failed on '$DYNAMIC_LIB_OPT'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT" ]]; then
echo "[zlib] 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 "[zlib] ERROR: lind_compile failed on '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."; exit 1; }

if [[ ! -f "$DYNAMIC_LIB_OPT_CWASM" ]]; then
echo "[zlib] ERROR: Failed to generate '$DYNAMIC_LIB_OPT_CWASM'; Exiting.."
exit 1
fi

cp "$DYNAMIC_LIB_OPT_CWASM" "$DYNAMIC_STAGED_LIB"
echo "[zlib] Dynamic shared library staged as $DYNAMIC_STAGED_LIB"