summaryrefslogtreecommitdiffstats
path: root/Tools/wasm/wasi-env
blob: 6c2d56e0e5e32b16466cd15a35d30ff5d181cb32 (plain)
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
#!/bin/sh
set -e

# function
usage() {
    echo "wasi-env - Run command with WASI-SDK"
    echo ""
    echo "wasi-env is a helper to set various environment variables to"
    echo "run configure and make with WASI-SDK. A WASI-SDK must be either"
    echo "installed at /opt/wasi-sdk or the env var 'WASI_SDK_PATH' must"
    echo "set to the root of a WASI-SDK."
    echo ""
    echo "Usage: wasi-env command [...]"
    echo ""
    echo "    -h --help     display this help and exit"
    echo ""
}

case $1 in
    -h|--help)
        usage
        exit
        ;;
esac

if test -z "$1"; then
    echo "ERROR: command required" >&2
    usage
    exit 1
fi

WASI_SDK_PATH="${WASI_SDK_PATH:-/opt/wasi-sdk}"
WASI_SYSROOT="${WASI_SDK_PATH}/share/wasi-sysroot"

if ! test -x "${WASI_SDK_PATH}/bin/clang"; then
    echo "Error: ${WASI_SDK_PATH}/bin/clang does not exist." >&2
    exit 2
fi

CC="${WASI_SDK_PATH}/bin/clang"
CPP="${WASI_SDK_PATH}/bin/clang-cpp"
CXX="${WASI_SDK_PATH}/bin/clang++"

# --sysroot is required if WASI-SDK is not installed in /opt/wasi-sdk.
if test "${WASI_SDK_PATH}" != "/opt/wasi-sdk"; then
    CC="${CC} --sysroot=${WASI_SYSROOT}"
    CPP="${CPP} --sysroot=${WASI_SYSROOT}"
    CXX="${CXX} --sysroot=${WASI_SYSROOT}"
fi

# use ccache if available
if command -v ccache >/dev/null 2>&1; then
    CC="ccache ${CC}"
    CPP="ccache ${CPP}"
    CXX="ccache ${CXX}"
fi

LDSHARED="${WASI_SDK_PATH}/bin/wasm-ld"
AR="${WASI_SDK_PATH}/bin/llvm-ar"
RANLIB="${WASI_SDK_PATH}/bin/ranlib"

# instruct pkg-config to use sysroot
PKG_CONFIG_PATH=""
PKG_CONFIG_LIBDIR="${WASI_SYSROOT}/lib/pkgconfig:${WASI_SYSROOT}/share/pkgconfig"
PKG_CONFIG_SYSROOT_DIR="${WASI_SYSROOT}"

PATH="${WASI_SDK_PATH}/bin:${PATH}"

export WASI_SDK_PATH WASI_SYSROOT
export CC CPP CXX LDSHARED AR RANLIB
export CFLAGS LDFLAGS
export PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR
export PATH

exec "$@"