Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
90 lines (80 loc) · 2.42 KB
/
Copy pathrun.sh
File metadata and controls
90 lines (80 loc) · 2.42 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
# ==========================================================================
# run.sh -- compile a Strata program to a native executable and run it.
#
# Usage:
# run.sh <file.strata> standalone (file needs `int main()`)
# run.sh <file.strata> <host.c> link a host driver (provides main + externs)
#
# The host driver supplies `extern` functions and `int main`; the Strata file
# then does not need its own `main`. Tools can be overridden with the STRATAC
# and CLANG environment variables.
# ==========================================================================
set -e
if [ $# -lt 1 ]; then
echo "Usage: run.sh <file.strata> [host.c]"
echo ""
echo "Standalone: run.sh hello.strata (.strata defines int main())"
echo "With host: run.sh engine_api.strata hosts/engine_api_host.c"
echo " (host provides main + extern functions)"
exit 2
fi
SRC=$1
if [ ! -f "$SRC" ]; then
echo "error: input file not found: $SRC"
exit 2
fi
HOST=""
if [ $# -ge 2 ]; then
HOST=$2
if [ ! -f "$HOST" ]; then
echo "error: host file not found: $HOST"
exit 2
fi
fi
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
STRATAC="${STRATAC:-$SCRIPT_DIR/build/default/bin/stratac}"
CLANG="${CLANG:-clang}"
if [ ! -x "$STRATAC" ] && ! command -v "$STRATAC" >/dev/null 2>&1; then
echo "error: stratac not found: $STRATAC (set STRATAC to override)"
exit 1
fi
BASE=$(basename "$SRC" | sed 's/\.[^.]*$//')
TMPDIR="${TMPDIR:-/tmp}"
OBJ="$TMPDIR/strata_${BASE}.o"
EXE="$TMPDIR/strata_${BASE}"
LOG="$TMPDIR/strata_${BASE}.log"
echo "[1/3] compile $SRC -> $OBJ"
"$STRATAC" "$SRC" -o "$OBJ" >"$LOG" 2>&1 || {
echo "compile failed:"
cat "$LOG"
rm -f "$OBJ" "$LOG"
exit 1
}
if [ -n "$HOST" ]; then
echo "[2/3] link $HOST + $OBJ -> $EXE"
"$CLANG" "$HOST" "$OBJ" -o "$EXE" >"$LOG" 2>&1 || {
echo "link failed:"
cat "$LOG"
rm -f "$OBJ" "$EXE" "$LOG"
exit 1
}
else
echo "[2/3] link $OBJ -> $EXE"
"$CLANG" "$OBJ" -o "$EXE" >"$LOG" 2>&1 || {
echo "link failed. Pass a host driver as the second argument, e.g."
echo " run.sh $SRC hosts/driver.c"
echo "or make sure $SRC defines int main."
cat "$LOG"
rm -f "$OBJ" "$EXE" "$LOG"
exit 1
}
fi
echo "[3/3] run $EXE"
echo
"$EXE"
RC=$?
echo
rm -f "$OBJ" "$EXE" "$LOG"
echo "exit code: $RC"
exit $RC