-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·366 lines (307 loc) · 8.07 KB
/
setup.sh
File metadata and controls
executable file
·366 lines (307 loc) · 8.07 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env bash
# Ora bootstrap script.
# Installs/checks local dependencies, syncs submodules, and builds Ora.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$SCRIPT_DIR"
PROFILE="full" # minimal | full | dev
RUN_TESTS=0
SKIP_DEPS=0
SKIP_SUBMODULES=0
SKIP_BUILD=0
NO_LLVM_BOOTSTRAP=0
MIN_ZIG_VERSION="0.15.0"
LLVM_REPO_URL="https://github.com/llvm/llvm-project.git"
LLVM_COMMIT="ee8c14be14deabace692ab51f5d5d432b0a83d58"
# colors (enabled only on tty)
if [[ -t 1 ]]; then
BOLD='\033[1m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
BOLD=''
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
log_info() { printf "%b[info]%b %s\n" "$BLUE" "$NC" "$*"; }
log_ok() { printf "%b[ok]%b %s\n" "$GREEN" "$NC" "$*"; }
log_warn() { printf "%b[warn]%b %s\n" "$YELLOW" "$NC" "$*"; }
log_err() { printf "%b[err]%b %s\n" "$RED" "$NC" "$*" >&2; }
die() {
log_err "$*"
exit 1
}
usage() {
cat <<USAGE
Ora Setup
Usage:
./setup.sh [options]
Options:
--profile <minimal|full|dev> Setup profile (default: full)
--run-tests Run tests after build
--skip-deps Do not install system packages
--skip-submodules Do not sync/update git submodules
--skip-build Do not build Ora
--no-llvm-bootstrap Do not auto-clone vendor/llvm-project when missing
-h, --help Show this help
Profiles:
minimal Build fast with -Dskip-mlir=true when MLIR artifacts already exist.
full Build complete toolchain (default).
dev Full profile plus developer-oriented tool checks.
USAGE
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--profile)
[[ $# -ge 2 ]] || die "--profile requires a value"
PROFILE="$2"
shift 2
;;
--run-tests)
RUN_TESTS=1
shift
;;
--skip-deps)
SKIP_DEPS=1
shift
;;
--skip-submodules)
SKIP_SUBMODULES=1
shift
;;
--skip-build)
SKIP_BUILD=1
shift
;;
--no-llvm-bootstrap)
NO_LLVM_BOOTSTRAP=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown argument: $1"
;;
esac
done
case "$PROFILE" in
minimal|full|dev) ;;
*) die "invalid profile '$PROFILE' (expected: minimal|full|dev)" ;;
esac
}
version_ge() {
# Returns 0 when $1 >= $2
local IFS=.
local -a lhs rhs
local i
lhs=($1)
rhs=($2)
for ((i=${#lhs[@]}; i<3; i++)); do lhs[i]=0; done
for ((i=${#rhs[@]}; i<3; i++)); do rhs[i]=0; done
for i in 0 1 2; do
if ((10#${lhs[i]} > 10#${rhs[i]})); then
return 0
fi
if ((10#${lhs[i]} < 10#${rhs[i]})); then
return 1
fi
done
return 0
}
platform_detect() {
case "$(uname -s)" in
Darwin) PLATFORM="macos" ;;
Linux) PLATFORM="linux" ;;
*) PLATFORM="unsupported" ;;
esac
if [[ "$PLATFORM" == "unsupported" ]]; then
die "unsupported platform: $(uname -s). Use Linux or macOS."
fi
log_info "platform: $PLATFORM"
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
}
check_zig() {
require_cmd zig
local zig_ver
zig_ver="$(zig version)"
log_info "zig version: $zig_ver"
if ! version_ge "$zig_ver" "$MIN_ZIG_VERSION"; then
die "Zig $MIN_ZIG_VERSION+ required (found $zig_ver)"
fi
log_ok "Zig version is compatible"
}
has_mlir_artifacts() {
local lib_dir="$ROOT_DIR/vendor/mlir/lib"
[[ -d "$lib_dir" ]] || return 1
compgen -G "$lib_dir/libMLIR-C.*" >/dev/null || return 1
compgen -G "$lib_dir/libMLIROraDialectC.*" >/dev/null || return 1
compgen -G "$lib_dir/libMLIRSIRDialect.*" >/dev/null || return 1
return 0
}
install_deps_macos() {
require_cmd brew
local packages=(git cmake z3)
local dev_packages=(python ninja)
if [[ "$PROFILE" == "dev" ]]; then
packages+=("${dev_packages[@]}")
fi
log_info "installing packages via Homebrew: ${packages[*]}"
brew install "${packages[@]}"
log_ok "macOS dependencies are installed"
}
install_deps_linux() {
require_cmd apt-get
local -a packages=(
build-essential
git
cmake
clang
libc++-dev
libc++abi-dev
pkg-config
z3
libz3-dev
)
if [[ "$PROFILE" == "dev" ]]; then
packages+=(python3 ninja-build cargo)
fi
local SUDO=""
if [[ "$(id -u)" -ne 0 ]]; then
if command -v sudo >/dev/null 2>&1; then
SUDO="sudo"
else
die "apt-get requires root privileges (install sudo or run as root)"
fi
fi
log_info "updating apt package index"
$SUDO apt-get update -qq
log_info "installing packages via apt: ${packages[*]}"
$SUDO apt-get install -y "${packages[@]}"
log_ok "Linux dependencies are installed"
}
install_system_dependencies() {
if [[ "$SKIP_DEPS" -eq 1 ]]; then
log_warn "skipping dependency installation (--skip-deps)"
return
fi
case "$PLATFORM" in
macos) install_deps_macos ;;
linux) install_deps_linux ;;
*) die "unsupported platform: $PLATFORM" ;;
esac
}
sync_submodules() {
if [[ "$SKIP_SUBMODULES" -eq 1 ]]; then
log_warn "skipping submodule sync (--skip-submodules)"
return
fi
if [[ ! -d "$ROOT_DIR/.git" ]]; then
log_warn "not a git checkout; skipping submodule sync"
return
fi
require_cmd git
log_info "syncing submodules"
git -C "$ROOT_DIR" submodule sync --recursive
git -C "$ROOT_DIR" submodule update --init --recursive --depth=1
log_ok "submodules synced"
}
ensure_llvm_source() {
if [[ "$NO_LLVM_BOOTSTRAP" -eq 1 ]]; then
if [[ -d "$ROOT_DIR/vendor/llvm-project/llvm" ]]; then
log_ok "llvm-project source is present"
return
fi
die "vendor/llvm-project is missing and --no-llvm-bootstrap was specified"
fi
require_cmd git
log_info "ensuring pinned llvm-project commit: $LLVM_COMMIT"
if [[ ! -d "$ROOT_DIR/vendor/llvm-project/.git" ]]; then
rm -rf "$ROOT_DIR/vendor/llvm-project"
git init "$ROOT_DIR/vendor/llvm-project"
git -C "$ROOT_DIR/vendor/llvm-project" remote add origin "$LLVM_REPO_URL"
fi
if ! git -C "$ROOT_DIR/vendor/llvm-project" remote get-url origin >/dev/null 2>&1; then
git -C "$ROOT_DIR/vendor/llvm-project" remote add origin "$LLVM_REPO_URL"
fi
git -C "$ROOT_DIR/vendor/llvm-project" fetch --depth=1 origin "$LLVM_COMMIT"
git -C "$ROOT_DIR/vendor/llvm-project" checkout --detach FETCH_HEAD
log_ok "llvm-project pinned at $(git -C "$ROOT_DIR/vendor/llvm-project" rev-parse HEAD)"
}
build_ora() {
if [[ "$SKIP_BUILD" -eq 1 ]]; then
log_warn "skipping build (--skip-build)"
return
fi
local -a cmd
if [[ "$PROFILE" == "minimal" ]] && has_mlir_artifacts; then
cmd=(zig build -Dskip-mlir=true)
log_info "using fast build with existing MLIR artifacts"
else
cmd=(zig build)
if [[ "$PROFILE" == "minimal" ]]; then
log_warn "MLIR artifacts not found; running full build instead"
fi
fi
log_info "running: ${cmd[*]}"
(cd "$ROOT_DIR" && "${cmd[@]}")
log_ok "build completed"
}
run_tests() {
if [[ "$RUN_TESTS" -eq 0 ]]; then
return
fi
local -a cmd
if [[ "$PROFILE" == "minimal" ]] && has_mlir_artifacts; then
cmd=(zig build test -Dskip-mlir=true)
else
cmd=(zig build test)
fi
log_info "running tests: ${cmd[*]}"
(cd "$ROOT_DIR" && "${cmd[@]}")
log_ok "tests completed"
}
print_optional_tool_status() {
if [[ "$PROFILE" != "dev" ]]; then
return
fi
local tools=(python3 cargo cast anvil)
local tool
log_info "developer tool status"
for tool in "${tools[@]}"; do
if command -v "$tool" >/dev/null 2>&1; then
log_ok "$tool is available"
else
log_warn "$tool not found"
fi
done
}
main() {
parse_args "$@"
printf "%bOra Setup%b\n" "$BOLD" "$NC"
log_info "profile: $PROFILE"
platform_detect
check_zig
install_system_dependencies
sync_submodules
if [[ "$PROFILE" != "minimal" ]]; then
ensure_llvm_source
fi
build_ora
run_tests
print_optional_tool_status
printf "\n"
log_ok "setup finished"
printf "%s\n" "next: ./zig-out/bin/ora --help"
}
main "$@"