AtlatestRepositorysigil-web-repl
sigil-web-repl / tree / scriptsemsdk-shell
1
#!/usr/bin/env bash2
#3
# emsdk-shell - Run commands in an Emscripten environment on Guix4
#5
# This script creates an FHS-compatible container with emsdk available.6
# emsdk is installed to ~/.emsdk/emsdk and persists between invocations.7
#8
# Usage:9
# ./scripts/emsdk-shell # Interactive shell with emcc available10
# ./scripts/emsdk-shell emcc --help # Run a single command11
# ./scripts/emsdk-shell make wasm # Run make with wasm target12
#13
# First run will install emsdk (~400MB download).14
#16
set -e18
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"19
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"20
EMSDK_HOME="${EMSDK_HOME:-$HOME/.emsdk}"22
# Packages needed for FHS container23
GUIX_PACKAGES=(24
coreutils25
bash26
gcc-toolchain27
python28
git29
node30
xz31
nss-certs32
tar33
gzip34
zlib35
make36
grep37
sed38
)40
# Ensure emsdk directory exists41
mkdir -p "$EMSDK_HOME"43
# Build the guix shell command44
GUIX_CMD=(45
guix shell46
--container47
--emulate-fhs48
--network49
--share="$PROJECT_DIR=/project"50
--share="$EMSDK_HOME=/emsdk-home"51
)53
# Add all packages54
for pkg in "${GUIX_PACKAGES[@]}"; do55
GUIX_CMD+=("$pkg")56
done58
# Setup script to run inside the container59
SETUP_SCRIPT='60
export SSL_CERT_DIR=/etc/ssl/certs61
export EMSDK_QUIET=163
# Prevent Emscripten from finding host headers64
# These vars would cause emcc to use host includes, which breaks the build65
unset CPATH66
unset C_INCLUDE_PATH67
unset CPLUS_INCLUDE_PATH69
# Install emsdk if not present70
if [ ! -d "/emsdk-home/emsdk" ]; then71
echo "Installing emsdk (first-time setup)..."72
cd /emsdk-home73
git clone https://github.com/emscripten-core/emsdk.git74
cd emsdk75
./emsdk install latest76
./emsdk activate latest77
echo "emsdk installation complete."78
fi80
# Activate emsdk81
cd /emsdk-home/emsdk82
source ./emsdk_env.sh 2>/dev/null84
# Change to project directory85
cd /project86
'88
if [ $# -eq 0 ]; then89
# Interactive shell90
FULL_SCRIPT="$SETUP_SCRIPT91
exec bash"92
exec "${GUIX_CMD[@]}" -- bash -c "$FULL_SCRIPT"93
else94
# Run provided command95
FULL_SCRIPT="$SETUP_SCRIPT96
$*"97
exec "${GUIX_CMD[@]}" -- bash -c "$FULL_SCRIPT"98
fi