forked from Morwenn/cpp-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·326 lines (274 loc) · 11.8 KB
/
build.sh
File metadata and controls
executable file
·326 lines (274 loc) · 11.8 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
#!/bin/bash
#
# cpp-sort One-click Build Script
# Features: dependency download, build, unit tests, performance benchmarks
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_ok() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Get number of parallel jobs
get_nproc() { nproc 2>/dev/null || echo 4; }
# Count compilation targets in build directory
count_targets() {
local build_dir="$1"
# Count unique compile targets in CMake build.make files
find "$build_dir" -name "build.make" -exec grep -h "\.cpp\.o:" {} \; 2>/dev/null | \
grep -v "flags.make" | grep -v "compiler_depend" | sort -u | wc -l
}
# ============================================================================
# Compile with single-line progress display
# Shows [current/total] format, single-line update in terminal
# ============================================================================
compile_progress() {
local prefix="$1"
local total="$2"
# Check if stdout is a terminal for single-line display
if [ -t 1 ]; then
stdbuf -oL -eL awk -v prefix="$prefix" -v total="$total" -v BLUE='\033[0;34m' -v NC='\033[0m' '
BEGIN { count = 0 }
/Building C[XX]+ object/ {
count++
match($0, /object [^ ]+/)
file = substr($0, RSTART+7, RLENGTH-7)
printf "\r\033[K%s[INFO]%s %s [%d/%d] %s", BLUE, NC, prefix, count, total, file
fflush(stdout)
}
/Linking/ {
printf "\r\033[K%s[INFO]%s %s [%d/%d] linking...", BLUE, NC, prefix, count, total
fflush(stdout)
}
END {
printf "\r\033[K%s[INFO]%s %s [%d/%d] done.\n", BLUE, NC, prefix, count, (total > count ? total : count)
}
'
else
# Non-terminal: simple progress output
awk -v prefix="$prefix" -v total="$total" '
BEGIN { count = 0 }
/Building C[XX]+ object/ {
count++
match($0, /object [^ ]+/)
file = substr($0, RSTART+7, RLENGTH-7)
printf "[INFO] %s [%d/%d] %s\n", prefix, count, total, file
}
END {
printf "[INFO] %s [%d/%d] done.\n", prefix, count, (total > count ? total : count)
}
'
fi
}
# ============================================================================
# Step 1: Download test dependencies (Catch2, RapidCheck)
# ============================================================================
download_dependencies() {
log_info "Checking test dependencies..."
mkdir -p external
# Download Catch2
if [ ! -f "external/catch2/CMakeLists.txt" ]; then
log_info "Downloading Catch2..."
rm -rf external/catch2
git clone --depth 1 --branch v3.7.0 https://bgithub.xyz/catchorg/Catch2.git external/catch2
log_ok "Catch2 downloaded"
else
log_info "Catch2 already exists"
fi
# Download RapidCheck
if [ ! -f "external/rapidcheck/CMakeLists.txt" ]; then
log_info "Downloading RapidCheck..."
rm -rf external/rapidcheck
git clone --depth 1 https://bgithub.xyz/emil-e/rapidcheck.git external/rapidcheck
log_ok "RapidCheck downloaded"
else
log_info "RapidCheck already exists"
fi
log_ok "Dependencies ready"
}
# ============================================================================
# Step 2: Build main project
# ============================================================================
build_project() {
local build_dir="$SCRIPT_DIR/build"
local nproc=$(get_nproc)
log_info "Building main project..."
# CMake configure
cmake -S "$SCRIPT_DIR" -B "$build_dir" \
-DCPPSORT_BUILD_TESTING=ON \
-DCMAKE_CXX_FLAGS="-Wno-all -Wno-extra -Wno-interference-size" \
-Wno-dev 2>/dev/null
# Count targets for progress display
local total=$(count_targets "$build_dir")
# Build with progress
cmake --build "$build_dir" -j"$nproc" -- 2>&1 | compile_progress "Building" "$total"
log_ok "Main project built"
}
# ============================================================================
# Step 3: Run unit tests
# ============================================================================
run_unit_tests() {
local build_dir="$SCRIPT_DIR/build"
log_info "Running unit tests..."
local output
output=$(cd "$build_dir" && ctest --output-on-failure -j$(get_nproc) 2>&1) || {
echo "$output"
log_error "Unit tests failed"
exit 1
}
echo "$output"
# Extract stats
local passed=$(echo "$output" | grep -oP '\d+(?= tests passed)' | tail -1)
local total=$(echo "$output" | grep -oP '\d+(?= tests? passed)' | tail -1)
log_ok "Unit tests passed: ${passed}/${total} tests"
}
# ============================================================================
# Step 4: Build performance benchmarks
# ============================================================================
build_benchmarks() {
local build_dir="$SCRIPT_DIR/benchmarks/sort-comparison/build"
local nproc=$(get_nproc)
log_info "Building benchmarks..."
cmake -S "$SCRIPT_DIR/benchmarks/sort-comparison" -B "$build_dir" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_FLAGS="-Wno-all -Wno-extra -Wno-interference-size" \
-Wno-dev 2>/dev/null
local total=$(count_targets "$build_dir")
cmake --build "$build_dir" -j"$nproc" -- 2>&1 | compile_progress "Building benchmarks" "$total"
log_ok "Benchmarks built"
}
# ============================================================================
# Step 5: Run performance benchmarks
# ============================================================================
run_benchmarks() {
log_info "Running performance benchmarks..."
"$SCRIPT_DIR/benchmarks/sort-comparison/build/bench-sort-comparison"
log_ok "Benchmarks complete"
}
# ============================================================================
# Step 6: Run parallel sorters tests
# ============================================================================
run_parallel_tests() {
local build_dir="$SCRIPT_DIR/benchmarks/sort-comparison/build"
local nproc=$(get_nproc)
log_info "Running parallel sorters tests..."
# Build if needed
cmake -S "$SCRIPT_DIR/benchmarks/sort-comparison" -B "$build_dir" \
-DCMAKE_BUILD_TYPE=Release -Wno-dev 2>/dev/null
local total=$(count_targets "$build_dir")
cmake --build "$build_dir" -j"$nproc" -- 2>&1 | compile_progress "Building parallel" "$total"
"$build_dir/bench-parallel"
log_ok "Parallel tests complete"
}
# ============================================================================
# Step 7: Run serial vs parallel comparison
# ============================================================================
run_compare_tests() {
local algo="${1:-all}"
local build_dir="$SCRIPT_DIR/benchmarks/sort-comparison/build"
local nproc=$(get_nproc)
log_info "Running serial vs parallel comparison (${algo})..."
cmake -S "$SCRIPT_DIR/benchmarks/sort-comparison" -B "$build_dir" \
-DCMAKE_BUILD_TYPE=Release -Wno-dev 2>/dev/null
local total=$(count_targets "$build_dir")
cmake --build "$build_dir" -j"$nproc" -- 2>&1 | compile_progress "Building comparison" "$total"
if [ "$algo" = "all" ]; then
"$build_dir/bench-serial-parallel"
else
"$build_dir/bench-serial-parallel" -c "$algo"
fi
log_ok "Comparison complete"
}
# ============================================================================
# Step 8: Run SIMD sorter tests
# ============================================================================
run_simd_tests() {
log_info "Running SIMD sorter tests..."
"$SCRIPT_DIR/benchmarks/sort-comparison/build/test-simd-sorter"
log_ok "SIMD tests complete"
}
# ============================================================================
# Show help
# ============================================================================
show_help() {
cat << EOF
Usage: $0 [options]
Options:
-h, --help Show this help message
-d, --deps Download dependencies only
-b, --build Build project only
-t, --test Run unit tests only
-B, --bench Run performance benchmarks only
-p, --parallel Run parallel sorters tests
-s, --simd Run SIMD sorter tests only
-c <algo> Run serial vs parallel comparison
Algorithms: all, merge, quick, pdq, tim, heap, grail, simd
--no-deps Skip dependency download
--no-test Skip unit tests
--no-bench Skip performance benchmarks
--all Execute all steps (default)
Examples:
$0 # Execute all steps
$0 --no-deps # Skip dependency download
$0 -b -t # Build and test only
$0 -c merge # Compare merge_sorter vs parallel_merge_sorter
EOF
}
# ============================================================================
# Main
# ============================================================================
main() {
local run_deps=true
local run_build=true
local run_test=true
local run_bench=true
local run_parallel=true
local run_simd=true
local run_compare=false
local compare_algo="all"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help) show_help; exit 0 ;;
-d|--deps) run_deps=true; run_build=false; run_test=false; run_bench=false; run_parallel=false; run_simd=false; run_compare=false ;;
-b|--build) run_deps=false; run_build=true; run_test=false; run_bench=false; run_parallel=false; run_simd=false; run_compare=false ;;
-t|--test) run_deps=false; run_build=false; run_test=true; run_bench=false; run_parallel=false; run_simd=false; run_compare=false ;;
-B|--bench) run_deps=false; run_build=false; run_test=false; run_bench=true; run_parallel=false; run_simd=false; run_compare=false ;;
-p|--parallel) run_deps=false; run_build=false; run_test=false; run_bench=false; run_parallel=true; run_simd=false; run_compare=false ;;
-s|--simd) run_deps=false; run_build=false; run_test=false; run_bench=false; run_parallel=false; run_simd=true; run_compare=false ;;
-c) run_deps=false; run_build=false; run_test=false; run_bench=false; run_parallel=false; run_simd=false; run_compare=true
[[ $# -gt 1 && ! "$2" =~ ^- ]] && { compare_algo="$2"; shift; } ;;
--no-deps) run_deps=false ;;
--no-test) run_test=false ;;
--no-bench) run_bench=false ;;
--no-parallel) run_parallel=false ;;
--no-simd) run_simd=false ;;
--all) run_deps=true; run_build=true; run_test=true; run_bench=true; run_parallel=true; run_simd=true; run_compare=false ;;
*) log_error "Unknown option: $1"; show_help; exit 1 ;;
esac
shift
done
# Check required tools
command -v git >/dev/null || { log_error "git not installed"; exit 1; }
command -v cmake >/dev/null || { log_error "cmake not installed"; exit 1; }
# Start
log_info "Starting cpp-sort build..."
# Execute steps
$run_deps && download_dependencies
$run_build && build_project
$run_test && run_unit_tests
$run_parallel && run_parallel_tests
$run_compare && run_compare_tests "$compare_algo"
($run_bench || $run_simd) && build_benchmarks
$run_simd && run_simd_tests
$run_bench && run_benchmarks
log_ok "All tasks completed!"
}
main "$@"