Files
rspektrum/build_web.sh
T
tyler b6942d8577 Refactor: extract platform abstraction for subprocess spawning
Move all Unix-specific syscalls (fork, execvp, waitpid, strerror, /tmp)
into a platform layer so the same spectrogram.c can target Windows
(CreateProcess) and WebAssembly (stubs). Key changes:

- src/platform.h: public API with spawn handle, error codes, path helpers
- src/platform_linux.c: fork/execvp implementation, /tmp, strerror
- src/platform_win32.c: CreateProcess implementation, temp dir lookup
- src/platform_web.c: stub implementations (no subprocess support on web)
- src/spectrogram.c: consume only platform.h; replace pid_t/fork/execvp
  with Platform_SpawnChild/WaitForChild; use Platform_GetTempDir() for
  /tmp path; replace strdup with malloc/memcpy for portability
- Build: add platform_*.c to gmake, Premake, and build_web.sh
2026-05-14 18:16:21 -07:00

89 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# Build spectrogram viewer for the web using Emscripten
# Usage: ./build_web.sh [debug|release]
set -e
# Get script directory (project root)
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
BUILD_TYPE=${1:-release}
RAYLIB_DIR="$SCRIPT_DIR/build/external/raylib-master"
SRC_DIR="$SCRIPT_DIR/src"
BUILD_DIR="$SCRIPT_DIR/bin/web"
RAYLIB_BUILD="$BUILD_DIR/raylib_build"
OUTPUT="$BUILD_DIR/rspektrum.html"
# Create build directories
mkdir -p "$BUILD_DIR" "$RAYLIB_BUILD"
echo "=== Building Spectrogram Viewer for Web ($BUILD_TYPE) ==="
echo "Raylib dir: $RAYLIB_DIR"
# Common compile flags for raylib
RAYLIB_CFLAGS="-Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2"
if [ "$BUILD_TYPE" = "debug" ]; then
RAYLIB_CFLAGS="-g -O0 -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2"
fi
echo "=== Step 1: Compiling raylib for web ==="
# Compile raylib source files
emcc -c "$RAYLIB_DIR/src/rcore.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/rcore.o"
emcc -c "$RAYLIB_DIR/src/rshapes.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/rshapes.o"
emcc -c "$RAYLIB_DIR/src/rtextures.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/rtextures.o"
emcc -c "$RAYLIB_DIR/src/rtext.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/rtext.o"
emcc -c "$RAYLIB_DIR/src/rmodels.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/rmodels.o"
emcc -c "$RAYLIB_DIR/src/raudio.c" $RAYLIB_CFLAGS -o "$RAYLIB_BUILD/raudio.o"
# Create static library
cd "$RAYLIB_BUILD"
emar rcs libraylib.a rcore.o rshapes.o rtextures.o rtext.o rmodels.o raudio.o
cd "$SCRIPT_DIR"
# Compile spectrogram (web version — no subprocess support)
# platform_web.c provides stub implementations for spawn/error functions
echo "=== Step 2: Compiling spectrogram + platform for web ==="
cd "$SCRIPT_DIR"
# Compile platform_web.o (handles fork/execvp stubs for web)
emcc -c -Os -Wall -DPLATFORM_WEB "$SRC_DIR/platform_web.c" -o "$RAYLIB_BUILD/platform_web.o"
# Compile spectrogram
emcc -c -Os -Wall -DPLATFORM_WEB "$SRC_DIR/spectrogram.c" \
-I"$RAYLIB_DIR/src" \
-I"$RAYLIB_DIR/src/external" \
-I"$RAYLIB_DIR/src/external/glfw/include" \
-Iinclude \
-o "$RAYLIB_BUILD/spectrogram.o"
# Linker flags
LDFLAGS="-s USE_GLFW=3 -s ASYNCIFY -s TOTAL_MEMORY=67108864 -s FORCE_FILESYSTEM=1 --preload-file resources@resources --preload-file fonts/DejaVuSansMono.ttf@fonts/DejaVuSansMono.ttf --shell-file $SCRIPT_DIR/web_shell.html -s NO_EXIT_RUNTIME=1"
if [ "$BUILD_TYPE" = "debug" ]; then
LDFLAGS="$LDFLAGS -g -O0 -s ASSERTIONS=1"
else
LDFLAGS="$LDFLAGS -O2"
fi
# Compile and link
emcc -o "$OUTPUT" \
"$RAYLIB_BUILD/platform_web.o" \
"$RAYLIB_BUILD/spectrogram.o" \
-I"$RAYLIB_DIR/src" \
-I"$RAYLIB_DIR/src/external" \
-I"$RAYLIB_DIR/src/external/glfw/include" \
-Iinclude \
-DPLATFORM_WEB \
"$RAYLIB_BUILD/libraylib.a" \
$LDFLAGS
echo ""
echo "=== Build complete ==="
echo "Output: $OUTPUT"
echo ""
echo "To test locally, run:"
echo " cd $BUILD_DIR && python3 -m http.server 8080"
echo "Then open http://localhost:8080/rspektrum.html in your browser"