forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathz
More file actions
executable file
·737 lines (653 loc) · 23.1 KB
/
Copy pathz
File metadata and controls
executable file
·737 lines (653 loc) · 23.1 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/bin/bash
#
# Utility for building development tools for Nanvix.
#
# Run './z help' for more information on how to use this utility.
#
# Relevant Environment Variables
#
# - Z_TARGET: Target architecture.
# - Z_STAGE: Build stage.
# - Z_INSTALL_LOCATION: Installation directory.
# - Z_SYSROOT_LOCATION: Sysroot directory.
# - Z_RELEASE_NAME: Release name.
#
# shellcheck disable=SC2329
# shellcheck disable=SC2317
# shellcheck disable=SC2034
#==================================================================================================
# Fast fail on errors, unset variables, and pipe failures.
set -euo pipefail
#==================================================================================================
# Import Utils Script
#==================================================================================================
UTILS_SCRIPT_VERSION="c24dedaf76a4833b388597805e2c0625cef0c453"
UTILS_SCRIPT_URL="https://raw.githubusercontent.com/nanvix/scripts/${UTILS_SCRIPT_VERSION}/utils.sh"
utils_script="/tmp/utils-${UTILS_SCRIPT_VERSION}.sh"
# Download only if the file doesn't exist or is empty
if [[ ! -s "$utils_script" ]]; then
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$UTILS_SCRIPT_URL" -o "$utils_script" || {
echo "Failed to download utils.sh"
rm -f "$utils_script"
exit 1
}
elif command -v wget >/dev/null 2>&1; then
wget -qO "$utils_script" "$UTILS_SCRIPT_URL" || {
echo "Failed to download utils.sh"
rm -f "$utils_script"
exit 1
}
else
echo "curl or wget required"
exit 1
fi
fi
# Source the downloaded script
# shellcheck disable=SC1090
source "${utils_script}"
#==================================================================================================
# Z Constants
#==================================================================================================
# Directory containing this script (resolves symlinks).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Project Name
readonly Z_PROJECT_NAME="LLVM"
# Directory used when building, empty if not required.
readonly Z_PROJECT_BUILD_DIR=""
# Required Build Tools (tool_name => minimum_version)
readonly -A Z_PROJECT_REQUIRED_TOOLS=(
["gcc"]="13.3.0"
["make"]="4.3.0"
["wget"]="1.21.4"
["bzip2"]="1.0.8"
["tar"]="1.34.0"
["cmake"]="3.28.3"
["gh"]="2.0.0"
)
# Required System Packages for Setup
readonly Z_PROJECT_REQUIRED_PACKAGES=("build-essential" "wget" "bzip2" "tar" "cmake")
#==================================================================================================
# Project-Specific Constants
#==================================================================================================
# Build Targets
BUILD_TARGETS=("all")
# Install Targets
INSTALL_TARGETS=("install")
# Newlib release (provides C headers and libc)
readonly NEWLIB_RELEASE_TAG="nanvix-v4.4.0-e1c9d0d4c"
readonly NEWLIB_RELEASE_PATTERN="nanvix-newlib-*.tar.bz2"
# Nanvix OS release (used to obtain libposix.a, user.ld, and nanvixd binaries)
readonly NANVIX_RELEASE_TAG="v0.13.16"
readonly NANVIX_RELEASE_PATTERN="nanvix-x86-microvm-multi-process-release-128mb-*.tar.bz2"
#==================================================================================================
# Project-Specific Functions
#==================================================================================================
#
# Description
#
# Executes project-specific steps for downloading dependencies.
#
# Return Value
#
# - On success, this function returns zero.
# - On failure, this function returns non-zero.
#
download_steps() {
# Stage 0 (compiler only): no sysroot needed.
if [[ "${Z_STAGE}" == "0" ]]; then
print_info "Stage 0: no dependencies to download."
return 0
fi
# Stage 1 (runtimes): sysroot must already exist.
if [[ "${Z_STAGE}" == "1" ]]; then
local sysroot_location="${Z_SYSROOT_LOCATION:-${Z_INSTALL_LOCATION}}"
if [[ ! -d "${sysroot_location}" ]]; then
print_error "Stage 1 requires a sysroot at '${sysroot_location}'."
return 1
fi
# If C headers are missing, download newlib as a bootstrap sysroot.
if [[ ! -f "${sysroot_location}/usr/include/stdlib.h" ]]; then
print_info "Stage 1: C headers not found; downloading newlib..."
local tmp_dir
tmp_dir="$(mktemp -d)"
# Download and extract newlib (C headers and libc).
gh release download "${NEWLIB_RELEASE_TAG}" --repo nanvix/newlib \
--pattern "${NEWLIB_RELEASE_PATTERN}" \
--dir "$tmp_dir" || {
print_error "Failed to download newlib release."
rm -rf "$tmp_dir"
return 1
}
local newlib_archive
newlib_archive=$(find "$tmp_dir" -maxdepth 1 -name "${NEWLIB_RELEASE_PATTERN}" | head -1)
if [[ -z "$newlib_archive" ]]; then
print_error "No newlib archive found after download."
rm -rf "$tmp_dir"
return 1
fi
extract_tar_bz2 "$newlib_archive" "${sysroot_location}" || {
print_error "Failed to extract newlib."
rm -rf "$tmp_dir"
return 1
}
rm -rf "$tmp_dir"
if [[ ! -f "${sysroot_location}/usr/include/stdlib.h" ]]; then
print_error "Stage 1: C headers still missing after extracting newlib."
return 1
fi
print_info "Stage 1: newlib downloaded to '${sysroot_location}'."
else
print_info "Stage 1: C headers found at '${sysroot_location}'."
fi
# Download Nanvix release for libposix.a and user.ld if not already present.
local install_location="${Z_INSTALL_LOCATION}"
if [[ ! -f "${install_location}/lib/libposix.a" ]] || \
[[ ! -f "${install_location}/lib/user.ld" ]]; then
echo "Downloading Nanvix release ${NANVIX_RELEASE_TAG}..."
local nanvix_dir
nanvix_dir="$(mktemp -d)"
gh release download "${NANVIX_RELEASE_TAG}" --repo nanvix/nanvix \
--pattern "${NANVIX_RELEASE_PATTERN}" \
--dir "$nanvix_dir" || {
echo "ERROR: Failed to download Nanvix release. Ensure 'gh' is installed and authenticated."
rm -rf "$nanvix_dir"
return 1
}
local archive
archive=$(find "$nanvix_dir" -maxdepth 1 -name "${NANVIX_RELEASE_PATTERN}")
if [[ -z "$archive" ]]; then
echo "ERROR: No matching archive found in download directory."
rm -rf "$nanvix_dir"
return 1
fi
if [[ $(echo "$archive" | wc -l) -ne 1 ]]; then
echo "ERROR: Multiple matching archives found; expected exactly one."
rm -rf "$nanvix_dir"
return 1
fi
extract_tar_bz2 "$archive" "$nanvix_dir" || {
echo "ERROR: Failed to extract Nanvix release archive."
rm -rf "$nanvix_dir"
return 1
}
install -m 644 "$nanvix_dir/lib/libposix.a" "${install_location}/lib/"
install -m 644 "$nanvix_dir/lib/user.ld" "${install_location}/lib/"
# Install nanvixd binaries for integration tests.
mkdir -p "${install_location}/libexec/nanvixd"
install -m 755 "$nanvix_dir/bin/nanvixd.elf" "${install_location}/libexec/nanvixd/"
install -m 755 "$nanvix_dir/bin/kernel.elf" "${install_location}/libexec/nanvixd/"
install -m 755 "$nanvix_dir/bin/linuxd.elf" "${install_location}/libexec/nanvixd/"
install -m 755 "$nanvix_dir/bin/uservm.elf" "${install_location}/libexec/nanvixd/"
rm -rf "$nanvix_dir"
else
echo "Nanvix OS libraries already present in '${install_location}/lib/'."
fi
return 0
fi
print_error "No build stage specified. Use --stage=0 or --stage=1."
return 1
}
#
# Description
#
# Configures an LLVM stage 0 build (Clang + LLD only, no runtimes).
#
configure_stage0() {
local sysroot_location="${Z_SYSROOT_LOCATION:-${Z_INSTALL_LOCATION}}"
# Use sccache as compiler launcher if available.
local cmake_launcher_options=()
if command -v sccache >/dev/null 2>&1; then
cmake_launcher_options+=(
-DCMAKE_C_COMPILER_LAUNCHER=sccache
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache
)
fi
cmake -S llvm -B build -G Ninja \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DLLVM_PARALLEL_LINK_JOBS=1 \
-DLLVM_ENABLE_PROJECTS='clang;lld' \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_DEFAULT_TARGET_TRIPLE=i686-unknown-nanvix \
-DDEFAULT_SYSROOT="${sysroot_location}" \
-DC_INCLUDE_DIRS="${sysroot_location}/usr/include" \
-DCMAKE_BUILD_TYPE=Release \
"${cmake_launcher_options[@]}" || {
print_error "Stage 0 configuration failed."
return 1
}
return 0
}
#
# Description
#
# Configures an LLVM stage 1 build (runtimes only, using installed Clang).
#
configure_stage1() {
local sysroot_location="${Z_SYSROOT_LOCATION:-${Z_INSTALL_LOCATION}}"
# Extract LLVM version for compiler-rt install path.
local llvm_version_major
llvm_version_major=$(sed -n 's/.*LLVM_VERSION_MAJOR \([0-9]*\).*/\1/p' cmake/Modules/LLVMVersion.cmake)
# Config options for compiler-rt.
local compiler_rt_options=(
-DCOMPILER_RT_BUILD_BUILTINS=ON
-DCOMPILER_RT_BUILD_CRT=OFF
-DCOMPILER_RT_USE_LIBCXX=OFF
-DCOMPILER_RT_CXX_LIBRARY=none
-DCOMPILER_RT_CAN_EXECUTE_TESTS=OFF
-DCOMPILER_RT_BAREMETAL_BUILD=ON
-DCOMPILER_RT_BUILD_GWP_ASAN=OFF
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF
-DCOMPILER_RT_BUILD_MEMPROF=OFF
-DCOMPILER_RT_BUILD_PROFILE=OFF
-DCOMPILER_RT_BUILD_CTX_PROFILE=OFF
-DCOMPILER_RT_BUILD_SANITIZERS=OFF
-DCOMPILER_RT_BUILD_XRAY=OFF
-DCOMPILER_RT_BUILD_ORC=OFF
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
-DCOMPILER_RT_INSTALL_LIBRARY_DIR="${Z_INSTALL_LOCATION}/lib/clang/${llvm_version_major}/lib/i686-unknown-nanvix"
)
# Config options for libc++.
local libcxx_options=(
-DLIBCXX_ENABLE_SHARED=OFF
-DLIBCXX_ENABLE_STATIC=ON
-DLIBCXX_INCLUDE_TESTS=OFF
-DLIBCXX_INCLUDE_BENCHMARKS=OFF
-DLIBCXX_ENABLE_TIME_ZONE_DATABASE=OFF
-DLIBCXX_HAS_MUSL_LIBC=OFF
)
# Config options for libunwind.
local libunwind_options=(
-DLIBUNWIND_ENABLE_STATIC=ON
-DLIBUNWIND_ENABLE_SHARED=OFF
-DLIBUNWIND_USE_COMPILER_RT=ON
-DLIBUNWIND_IS_BAREMETAL=ON
-DLIBUNWIND_ADDITIONAL_COMPILE_FLAGS='-mno-sse;-mno-sse2'
)
# Config options for libcxxabi.
local libcxxabi_options=(
-DLIBCXXABI_USE_COMPILER_RT=ON
-DLIBCXXABI_ENABLE_STATIC_UNWINDER=ON
-DLIBCXXABI_ENABLE_STATIC=ON
-DLIBCXXABI_ENABLE_SHARED=OFF
)
# Use sccache as compiler launcher if available.
local cmake_launcher_options=()
if command -v sccache >/dev/null 2>&1; then
cmake_launcher_options+=(
-DCMAKE_C_COMPILER_LAUNCHER=sccache
-DCMAKE_CXX_COMPILER_LAUNCHER=sccache
)
fi
cmake -S runtimes -B build-runtimes -G Ninja \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DCMAKE_MODULE_PATH="${SCRIPT_DIR}/cmake/platforms" \
-DCMAKE_SYSTEM_NAME=Nanvix \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCMAKE_C_COMPILER="${Z_INSTALL_LOCATION}/bin/clang" \
-DCMAKE_CXX_COMPILER="${Z_INSTALL_LOCATION}/bin/clang++" \
-DCMAKE_C_COMPILER_TARGET=i686-unknown-nanvix \
-DCMAKE_CXX_COMPILER_TARGET=i686-unknown-nanvix \
-DCMAKE_SYSROOT="${sysroot_location}" \
-DLLVM_ENABLE_RUNTIMES='compiler-rt;libunwind;libcxx;libcxxabi' \
-DCMAKE_BUILD_TYPE=Release \
"${cmake_launcher_options[@]}" \
"${compiler_rt_options[@]}" \
"${libcxx_options[@]}" \
"${libunwind_options[@]}" \
"${libcxxabi_options[@]}" || {
print_error "Stage 1 configuration failed."
return 1
}
return 0
}
#
# Description
#
# Executes project-specific steps for configuring a build.
#
# Return Value
#
# - On success, this function returns zero.
# - On failure, this function returns non-zero.
#
# Usage Example
#
# configure_steps "i686-nanvix" "/usr/local"
#
configure_steps() {
if [[ "${Z_STAGE}" == "0" ]]; then
configure_stage0
elif [[ "${Z_STAGE}" == "1" ]]; then
configure_stage1
else
print_error "No build stage specified. Use --stage=0 or --stage=1."
return 1
fi
}
#
# Description
#
# Project-specific steps for building the project.
#
# Parameters
#
# - $1: Number of parallel build jobs.
#
# Usage Example
#
# build_steps 4 "all" "check"
#
build_steps() {
local parallel_build_option=$1
local build_dir="build"
if [[ "${Z_STAGE}" == "1" ]]; then
build_dir="build-runtimes"
fi
cmake --build "${build_dir}" -j "${parallel_build_option}" -v || {
print_error "Build failed for targets: '${BUILD_TARGETS[*]}'."
return 1
}
return 0
}
#
# Description
#
# Project-specific steps for installing the project.
#
# Return Value
#
# - On success, this function returns zero.
# - On failure, this function returns non-zero.
#
# Usage Example
#
# install_steps "install" "/usr/local"
#
install_steps() {
local build_dir="build"
if [[ "${Z_STAGE}" == "1" ]]; then
build_dir="build-runtimes"
fi
cmake -DCMAKE_INSTALL_PREFIX="${Z_INSTALL_LOCATION}" -P "${build_dir}/cmake_install.cmake" || {
print_error "Installation failed."
return 1
}
return 0
}
#==================================================================================================
# Main Script
#==================================================================================================
#
# Description
#
# Verifies that all expected toolchain artifacts are present after installation.
#
# Usage
#
# ./z verify
#
verify_artifacts() {
local install_location="${Z_INSTALL_LOCATION:-/opt/nanvix}"
echo "Checking libc++ artifacts..."
local failed=0
for lib in libc++.a libc++abi.a libunwind.a; do
if [[ ! -f "${install_location}/lib/${lib}" ]]; then
echo "ERROR: ${install_location}/lib/${lib} not found."
failed=1
fi
done
local builtins
builtins=$(find "${install_location}/lib/clang" -path "*/i686-unknown-nanvix/libclang_rt.builtins*.a" 2>/dev/null | head -1)
if [[ -z "$builtins" ]]; then
echo "ERROR: compiler-rt builtins library not found."
failed=1
fi
if [[ $failed -ne 0 ]]; then
echo "ERROR: Artifact verification failed."
return 1
fi
echo "All C++ runtime libraries present."
return 0
}
#
# Description
#
# Runs the Nanvix clang driver lit tests to verify the linker invocation
# produced by the toolchain matches expected behavior.
#
# Usage
#
# Called automatically by './z test'.
#
test_driver() {
local lit="${SCRIPT_DIR}/build/bin/llvm-lit"
if [[ ! -d "${SCRIPT_DIR}/build" ]]; then
echo "Skipping driver tests: build directory not found."
return 0
fi
if [[ ! -x "$lit" ]]; then
echo "ERROR: build directory exists but llvm-lit not found at '${lit}'."
return 1
fi
echo "Running Nanvix driver tests..."
"$lit" "${SCRIPT_DIR}/clang/test/Driver/nanvix.c" \
"${SCRIPT_DIR}/clang/test/Driver/nanvix.cpp" \
--no-progress-bar -v || {
echo "ERROR: Driver tests failed."
return 1
}
echo "Driver tests passed."
return 0
}
#
# Description
#
# Runs the C++ smoke test: compiles and links a minimal C++ program using the
# built clang toolchain and libc++ for the i686-unknown-nanvix target.
#
# Requires the toolchain to be already built and installed, and the Nanvix OS
# libraries (libposix.a, user.ld) to be present in the install location
# (downloaded by download_steps).
#
# Usage
#
# ./z test
#
test_smoke_cpp() {
local install_location="${Z_INSTALL_LOCATION:-/opt/nanvix}"
if [[ ! -x "${install_location}/bin/clang++" ]]; then
echo "ERROR: clang++ not found at '${install_location}/bin/clang++'. Build and install first."
return 1
fi
# Resolve libc++.a through the driver to handle target-specific sysroot paths.
local libcxx_archive
if ! libcxx_archive=$("${install_location}/bin/clang++" \
--target=i686-unknown-nanvix \
--sysroot="${install_location}" \
--print-file-name=libc++.a); then
echo "ERROR: failed to resolve libc++.a with clang++ --print-file-name."
return 1
fi
if [[ ! -f "$libcxx_archive" ]]; then
echo "ERROR: libc++ not found (resolved to '$libcxx_archive'). Build and install first."
return 1
fi
if [[ ! -f "${install_location}/lib/libposix.a" ]]; then
echo "ERROR: libposix.a not found at '${install_location}/lib/libposix.a'. Build and install first."
return 1
fi
if [[ ! -f "${install_location}/lib/user.ld" ]]; then
echo "ERROR: user.ld not found at '${install_location}/lib/user.ld'. Build and install first."
return 1
fi
echo "Compiling and linking C++ smoke test..."
local smoke_test_bin="${SCRIPT_DIR}/build/tests/smoke/hello.elf"
mkdir -p "$(dirname "$smoke_test_bin")"
"${install_location}/bin/clang++" \
--target=i686-unknown-nanvix \
--sysroot="${install_location}" \
-stdlib=libc++ \
-lposix \
-T "${install_location}/lib/user.ld" \
tests/smoke/hello.cpp \
-o "$smoke_test_bin" || {
echo "ERROR: Smoke test failed to compile and link."
rm -f "$smoke_test_bin"
return 1
}
file "$smoke_test_bin"
echo "Smoke test passed: C++ program compiled and linked successfully."
# Export path for integration test.
SMOKE_TEST_BIN_CPP="$smoke_test_bin"
return 0
}
#
# Description
#
# Runs the C smoke test: compiles and links a minimal C program using the
# built clang toolchain and newlib (libc.a) for the i686-unknown-nanvix target.
#
# Requires the toolchain to be already built and installed. The Nanvix OS
# libraries (libposix.a, user.ld) must already be present in the install
# location (downloaded by download_steps).
#
# Usage
#
# Called automatically by './z test' before test_smoke_cpp.
#
test_smoke_c() {
local install_location="${Z_INSTALL_LOCATION:-/opt/nanvix}"
if [[ ! -x "${install_location}/bin/clang" ]]; then
echo "ERROR: clang not found at '${install_location}/bin/clang'. Build and install first."
return 1
fi
# Resolve libc.a through the driver to handle target-specific sysroot paths.
local libc_archive
if ! libc_archive=$("${install_location}/bin/clang" \
--target=i686-unknown-nanvix \
--sysroot="${install_location}" \
--print-file-name=libc.a); then
echo "ERROR: failed to resolve libc.a with clang --print-file-name."
return 1
fi
if [[ ! -f "$libc_archive" ]]; then
echo "ERROR: libc not found (resolved to '$libc_archive'). Build and install first."
return 1
fi
if [[ ! -f "${install_location}/lib/libposix.a" ]]; then
echo "ERROR: libposix.a not found at '${install_location}/lib/libposix.a'. Build and install first."
return 1
fi
if [[ ! -f "${install_location}/lib/user.ld" ]]; then
echo "ERROR: user.ld not found at '${install_location}/lib/user.ld'. Build and install first."
return 1
fi
echo "Compiling and linking C smoke test..."
local smoke_test_bin="${SCRIPT_DIR}/build/tests/smoke/hello-c.elf"
mkdir -p "$(dirname "$smoke_test_bin")"
"${install_location}/bin/clang" \
--target=i686-unknown-nanvix \
--sysroot="${install_location}" \
tests/smoke/hello.c \
-lc \
-lposix \
-T "${install_location}/lib/user.ld" \
-o "$smoke_test_bin" || {
echo "ERROR: C smoke test failed to compile and link."
rm -f "$smoke_test_bin"
return 1
}
file "$smoke_test_bin"
echo "C smoke test passed: program compiled and linked successfully."
# Export path for integration test.
SMOKE_TEST_BIN_C="$smoke_test_bin"
return 0
}
#
# Description
#
# Runs the compiled C++ hello-world app on nanvixd.elf.
# Requires /dev/kvm to be available.
#
# Parameters
#
# $1: Path to the smoke test binary.
#
# Usage
#
# Called automatically by './z test' when /dev/kvm is available.
#
test_integration() {
local smoke_test_bin="$1"
local install_location="${Z_INSTALL_LOCATION:-/opt/nanvix}"
if [[ ! -f "${smoke_test_bin}" ]]; then
echo "ERROR: Smoke test binary not found at '${smoke_test_bin}'."
return 1
fi
local nanvixd="${install_location}/libexec/nanvixd/nanvixd.elf"
if [[ ! -f "${nanvixd}" ]]; then
echo "ERROR: nanvixd.elf not found at '${nanvixd}'. Run test_smoke first."
return 1
fi
echo "Running integration test: hello.cpp on nanvixd.elf..."
local timeout_seconds=30
local output
output="$(timeout "${timeout_seconds}" \
"${nanvixd}" \
-bin-dir "${install_location}/libexec/nanvixd" \
-console-file /dev/stdout \
-- "${smoke_test_bin}" </dev/null 2>&1)" || {
local rc=$?
if [[ $rc -eq 124 ]]; then
echo "ERROR: Integration test timed out after ${timeout_seconds}s."
else
echo "ERROR: Integration test failed (exit code $rc)."
fi
echo "Output:"
echo "$output"
return 1
}
echo "$output"
# Verify expected output from the hello-world program.
if echo "$output" | grep -q "value = 42"; then
echo "Integration test passed: program ran successfully on nanvixd.elf."
else
echo "ERROR: Expected output 'value = 42' not found."
return 1
fi
return 0
}
# Intercept 'verify' and 'test' commands before delegating to zmain.
# NOTE: The z framework (utils.sh) does not support registering custom
# subcommands; its command set is hardcoded. Intercepting $1 before zmain is
# the intended extension pattern. These commands won't appear in './z help'.
if [[ "${1:-}" == "verify" ]]; then
_load_project_config 2>/dev/null || true
verify_artifacts
exit $?
fi
if [[ "${1:-}" == "test" ]]; then
# Load saved configuration if available.
_load_project_config 2>/dev/null || true
test_driver || exit $?
test_smoke_c || exit $?
test_smoke_cpp || exit $?
# Run integration tests if KVM is available.
if [[ -c /dev/kvm ]]; then
if [[ -n "${SMOKE_TEST_BIN_C:-}" ]]; then
test_integration "${SMOKE_TEST_BIN_C}" || exit $?
fi
if [[ -n "${SMOKE_TEST_BIN_CPP:-}" ]]; then
test_integration "${SMOKE_TEST_BIN_CPP}" || exit $?
fi
else
echo "Skipping integration tests: /dev/kvm not available."
fi
exit 0
fi
# Run main function with all arguments
zmain "$@" || {
exit 1
}
exit 0