-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
151 lines (134 loc) · 4.55 KB
/
Copy pathbuild.sh
File metadata and controls
151 lines (134 loc) · 4.55 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/sh
# Build the native self-hosted MiniLang compiler on a Linux x86-64 host.
# The result is staged in a private temporary directory and smoke-tested before
# it replaces the requested output, so a failed bootstrap never destroys the
# last known-good compiler.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
COMPILER=${MINILANG_COMPILER:-}
OUTPUT=${MINILANG_COMPILER_OUTPUT:-"$ROOT/build/mlc_linux_x64"}
SKIP_SMOKE=0
KEEP_OBJECTS=0
REPLACE=1
usage() {
printf '%s\n' "Usage: ./build.sh [--compiler PATH] [--output PATH] [--no-replace] [--skip-smoke] [--keep-objects]"
}
while [ "$#" -gt 0 ]; do
case "$1" in
--compiler)
[ "$#" -ge 2 ] || { usage >&2; exit 2; }
COMPILER=$2
shift 2
;;
--output)
[ "$#" -ge 2 ] || { usage >&2; exit 2; }
OUTPUT=$2
shift 2
;;
--no-replace) REPLACE=0; shift ;;
--skip-smoke) SKIP_SMOKE=1; shift ;;
--keep-objects) KEEP_OBJECTS=1; shift ;;
--help|-h) usage; exit 0 ;;
*) printf 'Unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;;
esac
done
case "$OUTPUT" in
/*) ;;
*) OUTPUT="$ROOT/$OUTPUT" ;;
esac
if [ -z "$COMPILER" ]; then
if [ -x "$ROOT/build/mlc_linux_x64" ]; then
COMPILER="$ROOT/build/mlc_linux_x64"
elif [ -f "$ROOT/../MiniLangCompilerPy/mlc_win64.py" ]; then
COMPILER="$ROOT/../MiniLangCompilerPy/mlc_win64.py"
else
printf '%s\n' "No bootstrap compiler found; pass --compiler PATH." >&2
exit 2
fi
fi
case "$COMPILER" in
/*) ;;
*) COMPILER="$ROOT/$COMPILER" ;;
esac
[ -f "$COMPILER" ] || { printf 'Compiler not found: %s\n' "$COMPILER" >&2; exit 2; }
STAGE_DIR=$(mktemp -d "${TMPDIR:-/tmp}/minilang-build.XXXXXXXX")
cleanup() {
# STAGE_DIR comes directly from mktemp and is never recomputed.
rm -rf -- "$STAGE_DIR"
}
trap cleanup EXIT HUP INT TERM
STAGE_OUTPUT="$STAGE_DIR/mlc_linux_x64.next"
ENTRY="$ROOT/mlc_win64.ml"
invoke_compiler() {
case "$COMPILER" in
*.py) python3 "$COMPILER" "$@" ;;
*) "$COMPILER" "$@" ;;
esac
}
printf 'Compiler: %s\nEntry: %s\nStage: %s\nOutput: %s\n' "$COMPILER" "$ENTRY" "$STAGE_OUTPUT" "$OUTPUT"
START_SECONDS=$(date +%s)
invoke_compiler \
"$ENTRY" "$STAGE_OUTPUT" \
-I "$ROOT" \
--target linux-x64 \
--heap-reserve 8g \
--heap-commit 512m \
--heap-shrink \
--heap-shrink-min 16m \
--gc-limit 1536m \
--object-pipeline
chmod 755 "$STAGE_OUTPUT"
"$STAGE_OUTPUT" --version
if [ "$SKIP_SMOKE" -eq 0 ]; then
SMOKE_SOURCE="$STAGE_DIR/smoke.ml"
SMOKE_OUTPUT="$STAGE_DIR/smoke.object"
SMOKE_MONOLITHIC="$STAGE_DIR/smoke.monolithic"
printf '%s\n' 'print "hello-linux"' 'x = 1' 'print "x=" + x' > "$SMOKE_SOURCE"
"$STAGE_OUTPUT" "$SMOKE_SOURCE" "$SMOKE_MONOLITHIC" -I "$ROOT" --target linux-x64
"$STAGE_OUTPUT" "$SMOKE_SOURCE" "$SMOKE_OUTPUT" -I "$ROOT" --target linux-x64 --object-pipeline
chmod 755 "$SMOKE_MONOLITHIC" "$SMOKE_OUTPUT"
cmp -s "$SMOKE_MONOLITHIC" "$SMOKE_OUTPUT" || {
printf '%s\n' "Linux monolithic and MLO smoke images are not byte-identical." >&2
exit 2
}
SMOKE_RESULT=$($SMOKE_OUTPUT)
[ "$SMOKE_RESULT" = "hello-linux
x=1" ] || { printf 'Unexpected smoke-test output:\n%s\n' "$SMOKE_RESULT" >&2; exit 2; }
# Exercise Linux-hosted project/path handling as well as the direct CLI. The
# parent component is intentional: it catches array/path normalization bugs.
SMOKE_MANIFEST="$STAGE_DIR/minilang.toml"
SMOKE_PROJECT="$STAGE_DIR/smoke.project"
printf '%s\n' \
'[project]' \
'entry = "smoke.ml"' \
'output = "nested/../smoke.project"' \
'target = "linux-x64"' \
'object_pipeline = true' \
'incremental = false' > "$SMOKE_MANIFEST"
"$STAGE_OUTPUT" --project "$SMOKE_MANIFEST"
chmod 755 "$SMOKE_PROJECT"
cmp -s "$SMOKE_MONOLITHIC" "$SMOKE_PROJECT" || {
printf '%s\n' "Linux project and direct smoke images are not byte-identical." >&2
exit 2
}
fi
mkdir -p -- "$(dirname -- "$OUTPUT")"
if [ "$REPLACE" -eq 0 ] && [ "$OUTPUT" = "$COMPILER" ]; then
OUTPUT="$OUTPUT.next"
fi
# Refuse before publishing the new compiler. This preserves the old output if
# the caller asked to retain objects but already owns the destination folder.
OBJECT_OUTPUT=
if [ "$KEEP_OBJECTS" -eq 1 ] && [ -d "$STAGE_DIR/tmp" ]; then
OBJECT_OUTPUT="$OUTPUT.objects"
if [ -e "$OBJECT_OUTPUT" ]; then
printf 'Refusing to replace existing object directory: %s\n' "$OBJECT_OUTPUT" >&2
exit 2
fi
fi
mv -f -- "$STAGE_OUTPUT" "$OUTPUT"
if [ -n "$OBJECT_OUTPUT" ]; then
mv -- "$STAGE_DIR/tmp" "$OBJECT_OUTPUT"
fi
END_SECONDS=$(date +%s)
printf 'Build complete in %ss.\nWrote: %s\n' "$((END_SECONDS - START_SECONDS))" "$OUTPUT"