Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·234 lines (199 loc) · 6.77 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·234 lines (199 loc) · 6.77 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
#!/bin/bash
SOURCE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="$SOURCE_ROOT/build-scripts"
# Canonical build order — every build script must be listed here.
# Dependencies must appear before their dependents (e.g. sqlite3 before proj).
BUILD_ORDER=(cli11 fmt libaec nlohmann_json pybind11 qhull sqlite3 proj libpq libpqxx)
# Transitive dependencies — must match DEPENDENCIES in CMakeLists.txt.
# Use DEP_REQUIRES_<name> variables (bash 3.2 has no associative arrays).
DEP_REQUIRES_proj="sqlite3 nlohmann_json"
DEP_REQUIRES_libpqxx="libpq"
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Options:
--install-path <path> Install prefix (default: ./deps, or \$INSTALL_PREFIX)
--build-path <path> Build directory (default: ./deps-build, or \$BUILD_PATH)
--with-deps <list> Comma-separated list of deps to build (default: all)
--list-avail List available dependencies and exit
--verbose, -v Enable verbose output (set -ex)
-h, --help Show this help
EOF
}
require_arg() {
if [[ $# -lt 2 || "$2" == --* ]]; then
echo "Error: $1 requires a value"
usage
exit 1
fi
}
# Resolve transitive deps and return them ordered by BUILD_ORDER.
resolve_deps() {
local needed=" $* "
# Fixed-point: expand transitive deps until stable
local changed=true
while [[ "$changed" == true ]]; do
changed=false
for dep in $needed; do
local var="DEP_REQUIRES_${dep}"
local reqs="${!var}"
for req in $reqs; do
if [[ "$needed" != *" $req "* ]]; then
needed="$needed$req "
changed=true
fi
done
done
done
# Order by BUILD_ORDER (only include resolved deps)
local ordered=""
for dep in "${BUILD_ORDER[@]}"; do
if [[ "$needed" == *" $dep "* ]]; then
ordered="$ordered $dep"
fi
done
echo $ordered
}
list_avail() {
echo "Available dependencies:"
for dep in "${BUILD_ORDER[@]}"; do
echo " $dep"
done
exit 0
}
load_and_build() {
local script_path="$1"
local script_name=$(basename "$script_path" .sh)
echo "Processing: $script_name"
# Create subshell to isolate each script's environment
(
source "$script_path" || exit 1
if declare -f build > /dev/null; then
echo "Running build for $script_name..."
build || exit 1
else
echo "Error: No build function in $script_name"
exit 1
fi
)
if [[ $? -ne 0 ]]; then
echo "FAILED: $script_name"
exit 1
fi
echo "---"
}
main() {
# Defaults from env vars (backwards compat), overridden by flags
local install_path="${INSTALL_PREFIX:-$(pwd)/deps}"
local build_path="${BUILD_PATH:-$(pwd)/deps-build}"
local with_deps=""
local verbose=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--install-path) require_arg "$@"; install_path="$2"; shift 2 ;;
--build-path) require_arg "$@"; build_path="$2"; shift 2 ;;
--with-deps) require_arg "$@"; with_deps="$2"; shift 2 ;;
--list-avail) list_avail ;;
--verbose|-v) verbose=true; shift ;;
-h|--help) usage; exit 0 ;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
# Enable shell options: -e always, -x only when verbose
if [[ "$verbose" == true ]]; then
set -ex
else
set -e
fi
export INSTALL_PREFIX="$install_path"
mkdir -p "$INSTALL_PREFIX"
export BUILD_PATH="$build_path"
mkdir -p "$BUILD_PATH"
export SOURCE_ROOT
echo "Starting build process"
echo " INSTALL_PREFIX=$INSTALL_PREFIX"
echo " BUILD_PATH=$BUILD_PATH"
# Check if script directory exists
if [[ ! -d "$SCRIPT_DIR" ]]; then
echo "Error: Script directory '$SCRIPT_DIR' not found!"
exit 1
fi
# Build the script list: selected deps or all
declare -a scripts_to_build=()
if [[ -n "$with_deps" ]]; then
IFS=',' read -ra selected <<< "$with_deps"
local -a requested=()
for dep in "${selected[@]}"; do
dep=$(echo "$dep" | xargs) # trim whitespace
if [[ ! -f "$SCRIPT_DIR/${dep}.sh" ]]; then
echo "Error: Unknown dependency '$dep' (no script at $SCRIPT_DIR/${dep}.sh)"
exit 1
fi
requested+=("$dep")
done
# Resolve transitive dependencies and order by BUILD_ORDER
local -a resolved
read -ra resolved <<< "$(resolve_deps "${requested[@]}")"
# Report auto-added deps
for dep in "${resolved[@]}"; do
local was_requested=false
for req in "${requested[@]}"; do
[[ "$req" == "$dep" ]] && was_requested=true && break
done
if [[ "$was_requested" == false ]]; then
echo " Auto-enabling: $dep (transitive dependency)"
fi
done
echo " Build order: ${resolved[*]}"
for dep in "${resolved[@]}"; do
if [[ ! -f "$SCRIPT_DIR/${dep}.sh" ]]; then
echo "Error: Transitive dependency '$dep' has no script at $SCRIPT_DIR/${dep}.sh"
exit 1
fi
scripts_to_build+=("$SCRIPT_DIR/${dep}.sh")
done
else
echo " Building all dependencies (using BUILD_ORDER)"
# Validate: every .sh in build-scripts/ must be listed in BUILD_ORDER
shopt -s nullglob
for script in "$SCRIPT_DIR"/*.sh; do
local name=$(basename "$script" .sh)
local found=false
for ordered in "${BUILD_ORDER[@]}"; do
if [[ "$ordered" == "$name" ]]; then
found=true
break
fi
done
if [[ "$found" == false ]]; then
echo "Error: '$name' exists in build-scripts/ but is not listed in BUILD_ORDER"
exit 1
fi
done
for dep in "${BUILD_ORDER[@]}"; do
script="$SCRIPT_DIR/${dep}.sh"
if [[ ! -f "$script" ]]; then
echo "Error: BUILD_ORDER lists '$dep' but no script at $script"
exit 1
fi
scripts_to_build+=("$script")
done
fi
if [[ ${#scripts_to_build[@]} -eq 0 ]]; then
echo "No dependencies to build"
exit 0
fi
# Process selected scripts
for script in "${scripts_to_build[@]}"; do
load_and_build "$script"
done
echo "All builds completed successfully!"
echo "Cleaning build directory: $BUILD_PATH"
rm -rf "$BUILD_PATH"
}
main "$@"