Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
39 changes: 39 additions & 0 deletions libtirpc/bootstrap_wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
set -e

if command -v apt >/dev/null 2>&1; then
echo "[INFO] Installing autoconf, automake, libtool..."
apt update
apt install -y autoconf automake libtool
fi

if [ -f ./autogen.sh ]; then
chmod +x autogen.sh
./autogen.sh
fi

export AR=/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/llvm-ar
Copy link
Member

Choose a reason for hiding this comment

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

Since the script depends on several specific tools (clang, wasm-opt, wasmtime, and a specific SYSROOT), it might be a good idea to move all the path exports and environment variable setups to the top of the script using fallbacks like this:

export CLANG="${CLANG:-/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang}"
export CXX="${CXX:-/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang++}"
export AR="${AR:-/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/llvm-ar}"
export LD="${LD:-/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/wasm-ld}"
export RANLIB="${RANLIB:-/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/llvm-ranlib}"

export WASM_OPT="${WASM_OPT:-/home/lind/lind-wasm/tools/binaryen/bin/wasm-opt}"
export WASMTIME="${WASMTIME:-/home/lind/lind-wasm/src/wasmtime/target/debug/wasmtime}"

export SYSROOT="${SYSROOT:-/home/lind/lind-wasm/src/glibc/sysroot}"

export CFLAGS="${CFLAGS:---sysroot=$SYSROOT -g -O2}"
export CXXFLAGS="${CXXFLAGS:---sysroot=$SYSROOT -g -O2}"
export LDFLAGS="${LDFLAGS:---sysroot=$SYSROOT -Wl,--import-memory,--export-memory,--max-memory=67108864,--export=__stack_pointer,--export=__stack_low}"

This way you could modify them at the time you call the compile without having to edit the script, like this:
WASMTIME=$HOME/.cargo/bin/wasmtime ./compile_lmbench.sh

And if you later want to move it to the Makefile to adhere to the build contract style we talked about in Issue 10

export CC="/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang --target=wasm32-unknown-wasi"
export CXX="/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang++ --target=wasm32-unknown-wasi"
export LD=/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/wasm-ld

export SYSROOT="/home/lind/lind-wasm/src/glibc/sysroot"
export CFLAGS="--sysroot=$SYSROOT -g -O2"
export CXXFLAGS="--sysroot=$SYSROOT -g -O2"
export LDFLAGS="--sysroot=$SYSROOT -Wl,--import-memory,--export-memory,--max-memory=67108864,--export=__stack_pointer,--export=__stack_low"

./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

echo "[INFO] Moving .o files to sysroot merge_tmp..."

mkdir -p /home/lind/lind-wasm/src/glibc/sysroot/lib/wasm32-wasi/merge_tmp
Copy link
Contributor

Choose a reason for hiding this comment

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

@m-hemmings this is where the sysroot stuff comes into play, I like your overlay idea but i'm not sure runbin and I completely understand it

Copy link
Member

Choose a reason for hiding this comment

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

@rennergade The idea should be that the app does all it needs to in its own context and then the tooling script moves what needs to be moved. So I wonder, could this merge be done in the app's own folders and then have the merged file be placed in the sysroot_overlay folder with the same folder structure it would need if it went to sysroot? or is it required that it be done in situ in the actual sysroot?
The original idea was that the app would not modify the actual sysroot at all, but we can revisit that if it doesn't fit

find src -name '*.o' -exec mv {} /home/lind/lind-wasm/src/glibc/sysroot/lib/wasm32-wasi/merge_tmp/ \;
49 changes: 49 additions & 0 deletions lmbench/src/bootstrap_wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
set -e

echo "[STEP 1] Copy TIRPC headers to sysroot..."
mkdir -p /home/lind/lind-wasm/src/glibc/sysroot/include/wasm32-wasi/rpc
sudo cp -r /usr/include/tirpc/rpc/* /home/lind/lind-wasm/src/glibc/sysroot/include/wasm32-wasi/rpc/
sudo cp /usr/include/tirpc/netconfig.h /home/lind/lind-wasm/src/glibc/sysroot/include/wasm32-wasi/

echo "[STEP 2] Ensure output directory is owned by current user..."
sudo mkdir -p /home/lind/lind-wasm/src/glibc/sysroot/lib/wasm32-wasi/merge_tmp
sudo chown -R "$(whoami)" /home/lind/lind-wasm/src/glibc/sysroot/lib/wasm32-wasi/merge_tmp

echo "[STEP 3] Compile lib_*.c and getopt.c to wasm .o files..."
SRC_DIR="/home/lind/lind-wasm/lind-wasm-apps/lmbench/src"
OUT_DIR="/home/lind/lind-wasm/src/glibc/sysroot/lib/wasm32-wasi/merge_tmp"
CLANG="/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang"
SYSROOT="/home/lind/lind-wasm/src/glibc/sysroot"

for f in "$SRC_DIR"/lib_*.c "$SRC_DIR"/getopt.c; do
fname=$(basename "$f" .c)
echo "Compiling $fname.c ..."
if [[ "$fname" == "lib_sched" ]]; then
"$CLANG" --target=wasm32-unknown-wasi --sysroot="$SYSROOT" \
-O2 -g -Wno-return-type -c "$f" -o "$OUT_DIR/$fname.o"
else
"$CLANG" --target=wasm32-unknown-wasi --sysroot="$SYSROOT" \
-O2 -g -c "$f" -o "$OUT_DIR/$fname.o"
fi
done

echo "[STEP 4] Extract original libc.a objects into merge_tmp..."
LLVM_AR="/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/llvm-ar"
LIBC_A="$SYSROOT/lib/wasm32-wasi/libc.a"

cd "$OUT_DIR"
"$LLVM_AR" x "$LIBC_A"

echo "[STEP 5] Delete current libc.a..."
sudo rm -f "$LIBC_A"
echo "Deleted libc.a"

echo "[STEP 6] Rebuild libc.a from all .o files..."
sudo "$LLVM_AR" rcs "$LIBC_A" "$OUT_DIR"/*.o

echo "[STEP 7] Run ranlib..."
sudo /home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/llvm-ranlib "$LIBC_A"

echo "[DONE] libc.a rebuilt with merged .o files."

56 changes: 56 additions & 0 deletions lmbench/src/compile_lmbench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
set +e

CLANG="/home/lind/lind-wasm/clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04/bin/clang"
WASM_OPT="/home/lind/lind-wasm/tools/binaryen/bin/wasm-opt"
WASMTIME="/home/lind/lind-wasm/src/wasmtime/target/debug/wasmtime"
SYSROOT="/home/lind/lind-wasm/src/glibc/sysroot"

SRC_DIR="/home/lind/lind-wasm/lind-wasm-apps/lmbench/src"
OUT_DIR="/home/lind/lind-wasm/lind-wasm-apps/lmbench/src/wasm-bin"

mkdir -p "$OUT_DIR"

fail_count=0
cfiles=$(find "$SRC_DIR" -maxdepth 1 -name "*.c" \
! -name "getopt.c" \
! -name "lat_usleep.c" \
! -name "lib_*.c")

for cfile in $cfiles; do
base=$(basename "$cfile" .c)
wasm_tmp="$SRC_DIR/$base.wasm"
ofile="$OUT_DIR/$base.cwasm"

echo "[*] Compiling $base.c → $ofile with clang -std=gnu89"

"$CLANG" -std=gnu89 -pthread --target=wasm32-unknown-wasi --sysroot="$SYSROOT" \
-Wl,--import-memory,--export-memory,--max-memory=67108864,--export=__stack_pointer,--export=__stack_low \
"$cfile" -g -O0 -o "$wasm_tmp"

if [ ! -f "$wasm_tmp" ]; then
echo "Failed to compile $base.c (clang error)"
((fail_count++))
continue
fi

"$WASM_OPT" --epoch-injection --asyncify -O2 --debuginfo "$wasm_tmp" -o "$wasm_tmp"

"$WASMTIME" compile "$wasm_tmp" -o "$ofile"

if [ ! -f "$ofile" ]; then
echo "Failed to compile $base.wasm to $base.cwasm"
((fail_count++))
continue
fi

rm -f "$wasm_tmp"
done

total_count=$(echo "$cfiles" | wc -l)
echo ""
echo "total number: $total_count"
echo "compile error number: $fail_count"
echo "all the compiled file in: $OUT_DIR"