|
| 1 | +#!/usr/bin/env bash |
| 2 | +# requires: llvm unix-shell |
| 3 | +# `[target.<triple>].sysroot` and the three target queries a board package asks. |
| 4 | +# |
| 5 | +# ⚠️ WHY THIS TEST IS TWO-SIDED THROUGHOUT |
| 6 | +# |
| 7 | +# Both features here are of the kind whose presence and absence look identical |
| 8 | +# on a machine that is already configured. A sysroot override that silently did |
| 9 | +# nothing would still build, because the target row's picolibc is what would be |
| 10 | +# used either way; a query that returned a stale value would still compile, |
| 11 | +# because there is only one toolchain and one C library installed. So every |
| 12 | +# assertion below pins BOTH states — with the override and without it, with a C |
| 13 | +# library and without one. |
| 14 | +# |
| 15 | +# The queries exist to remove a coupling that no manifest shows. `riscv-virt-rt` |
| 16 | +# declares no dependency on LLVM and none on picolibc, and still could not serve |
| 17 | +# a second toolchain or a second C library, because it had written |
| 18 | +# `clang_rt.builtins-riscv64` and `rv64gc/lp64d` into its build program. |
| 19 | +# |
| 20 | +# No emulator is needed: everything here is decided at configure and link time, |
| 21 | +# which is why `requires:` asks only for llvm. |
| 22 | +set -e |
| 23 | + |
| 24 | +TMP=$(mktemp -d) |
| 25 | +trap "rm -rf $TMP" EXIT |
| 26 | +cd "$TMP" |
| 27 | + |
| 28 | +"$MCPP" new probe > /dev/null |
| 29 | +cd probe |
| 30 | +rm -f tests/*.cpp 2>/dev/null || true |
| 31 | + |
| 32 | +# A build program that reports what the engine told it. It returns non-zero so |
| 33 | +# that mcpp surfaces the output: a build program's stderr is otherwise only |
| 34 | +# shown when it fails, and a probe nobody can read proves nothing. |
| 35 | +cat > build.mcpp <<'EOF' |
| 36 | +import mcpp; |
| 37 | +import std; |
| 38 | +int main() { |
| 39 | + std::cerr << "builtins=[" << (mcpp::target_builtins_lib() ?: "") << "]\n" |
| 40 | + << "profile=[" << (mcpp::target_libc_profile() ?: "") << "]\n" |
| 41 | + << "libc=[" << (mcpp::target_libc() ?: "") << "]\n"; |
| 42 | + return 1; |
| 43 | +} |
| 44 | +EOF |
| 45 | +cat > src/main.cpp <<'EOF' |
| 46 | +extern "C" void _start() { for (;;) {} } |
| 47 | +EOF |
| 48 | + |
| 49 | +manifest() { |
| 50 | + cat > mcpp.toml <<EOF |
| 51 | +[package] |
| 52 | +name = "probe" |
| 53 | +version = "0.1.0" |
| 54 | +
|
| 55 | +[build] |
| 56 | +target = "riscv64-none-elf" |
| 57 | +$1 |
| 58 | +EOF |
| 59 | +} |
| 60 | + |
| 61 | +# ── 1. The target row's C library, and the queries derived from it ─────────── |
| 62 | +manifest "" |
| 63 | +"$MCPP" build > q64.log 2>&1 || true |
| 64 | +grep -q 'builtins=\[clang_rt.builtins-riscv64\]' q64.log || { |
| 65 | + cat q64.log; echo "rv64 builtins query wrong"; exit 1; } |
| 66 | +grep -q 'profile=\[rv64gc/lp64d\]' q64.log || { |
| 67 | + cat q64.log; echo "rv64 libc profile query wrong"; exit 1; } |
| 68 | +grep -q 'libc=\[picolibc-riscv\]' q64.log || { |
| 69 | + cat q64.log; echo "libc name query wrong"; exit 1; } |
| 70 | + |
| 71 | +# ── 2. The SAME manifest at the other width ───────────────────────────────── |
| 72 | +# This is what makes the queries worth having: one board description, two ISA |
| 73 | +# profiles, and the varying parts come from the engine rather than from a |
| 74 | +# branch in the package. |
| 75 | +"$MCPP" build --target riscv32-none-elf > q32.log 2>&1 || true |
| 76 | +grep -q 'builtins=\[clang_rt.builtins-riscv32\]' q32.log || { |
| 77 | + cat q32.log; echo "rv32 builtins query did not follow the target"; exit 1; } |
| 78 | +grep -q 'profile=\[rv32imac/ilp32\]' q32.log || { |
| 79 | + cat q32.log; echo "rv32 libc profile query did not follow the target"; exit 1; } |
| 80 | + |
| 81 | +# ── 3. The C library is really reachable without the override ─────────────── |
| 82 | +# Establishes the control for step 4: `<stdio.h>` resolves here. |
| 83 | +cat > src/main.cpp <<'EOF' |
| 84 | +#include <stdio.h> |
| 85 | +extern "C" void _start() { (void)sizeof(FILE); for (;;) {} } |
| 86 | +EOF |
| 87 | +cat > build.mcpp <<'EOF' |
| 88 | +import mcpp; |
| 89 | +int main() { return 0; } |
| 90 | +EOF |
| 91 | +manifest "" |
| 92 | +"$MCPP" build > with_libc.log 2>&1 || { |
| 93 | + cat with_libc.log; echo "the target row's C library should have been usable"; exit 1; } |
| 94 | + |
| 95 | +# ── 4. `sysroot = ""` really removes it ───────────────────────────────────── |
| 96 | +# ⚠️ THE LOAD-BEARING ASSERTION. Without it, an override that parsed and did |
| 97 | +# nothing would pass every other check in this file. |
| 98 | +manifest ' |
| 99 | +[target.riscv64-none-elf] |
| 100 | +sysroot = ""' |
| 101 | +if "$MCPP" build > no_libc.log 2>&1; then |
| 102 | + cat no_libc.log |
| 103 | + echo "sysroot = \"\" did not remove the C library — <stdio.h> still resolved" |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | +grep -q "stdio.h" no_libc.log || { |
| 107 | + cat no_libc.log; echo "build failed, but not because the C library was gone"; exit 1; } |
| 108 | + |
| 109 | +# ── 5. The zero-libc tier still produces an image ─────────────────────────── |
| 110 | +# Removing the C library must leave a usable target, not a broken one. |
| 111 | +cat > src/main.cpp <<'EOF' |
| 112 | +static volatile unsigned char* const UART = |
| 113 | + reinterpret_cast<unsigned char*>(0x10000000); |
| 114 | +extern "C" void kmain() { |
| 115 | + for (const char* p = "zero-libc\n"; *p; ++p) |
| 116 | + *UART = static_cast<unsigned char>(*p); |
| 117 | + *reinterpret_cast<volatile unsigned int*>(0x100000) = 0x5555; |
| 118 | + for (;;) {} |
| 119 | +} |
| 120 | +asm(".section .text.entry\n.globl _start\n_start:\n" |
| 121 | + " la sp, __stack_top\n call kmain\n1: j 1b\n"); |
| 122 | +EOF |
| 123 | +cat > link.ld <<'EOF' |
| 124 | +ENTRY(_start) |
| 125 | +SECTIONS { |
| 126 | + . = 0x80000000; |
| 127 | + .text : { *(.text.entry) *(.text*) } |
| 128 | + .rodata : { *(.rodata*) } |
| 129 | + .data : { *(.data*) } |
| 130 | + .bss : { *(.bss*) *(COMMON) } |
| 131 | + . = ALIGN(16); . = . + 0x1000; __stack_top = .; |
| 132 | +} |
| 133 | +EOF |
| 134 | +manifest "ldflags = [\"-T\", \"$PWD/link.ld\"] |
| 135 | +
|
| 136 | +[target.riscv64-none-elf] |
| 137 | +sysroot = \"\"" |
| 138 | +"$MCPP" build > zero.log 2>&1 || { |
| 139 | + cat zero.log; echo "a self-contained zero-libc image should still build"; exit 1; } |
| 140 | +grep -q 'Size probe' zero.log || { |
| 141 | + cat zero.log; echo "no size summary — the freestanding link path was not taken"; exit 1; } |
| 142 | + |
| 143 | +# ── 6. On the zero-libc tier the libc-facing queries are empty ────────────── |
| 144 | +# All three answers move together: a kernel must not be handed a path into a |
| 145 | +# C library that is not there, while `builtins` is a COMPILER fact and stays. |
| 146 | +cat > build.mcpp <<'EOF' |
| 147 | +import mcpp; |
| 148 | +import std; |
| 149 | +int main() { |
| 150 | + std::cerr << "builtins=[" << (mcpp::target_builtins_lib() ?: "") << "]\n" |
| 151 | + << "profile=[" << (mcpp::target_libc_profile() ?: "") << "]\n" |
| 152 | + << "libc=[" << (mcpp::target_libc() ?: "") << "]\n"; |
| 153 | + return 1; |
| 154 | +} |
| 155 | +EOF |
| 156 | +"$MCPP" build > zeroq.log 2>&1 || true |
| 157 | +grep -q 'profile=\[\]' zeroq.log || { |
| 158 | + cat zeroq.log; echo "libc profile should be empty with no C library"; exit 1; } |
| 159 | +grep -q 'libc=\[\]' zeroq.log || { |
| 160 | + cat zeroq.log; echo "libc name should be empty with no C library"; exit 1; } |
| 161 | +grep -q 'builtins=\[clang_rt.builtins-riscv64\]' zeroq.log || { |
| 162 | + cat zeroq.log; echo "builtins is a compiler fact and must survive"; exit 1; } |
| 163 | + |
| 164 | +# ── 7. A bare name is rejected at parse time ──────────────────────────────── |
| 165 | +# Accepting it would install nothing and fail much later naming a missing libc. |
| 166 | +manifest ' |
| 167 | +[target.riscv64-none-elf] |
| 168 | +sysroot = "newlib"' |
| 169 | +if "$MCPP" build > badref.log 2>&1; then |
| 170 | + cat badref.log; echo "a bare sysroot name should have been rejected"; exit 1; fi |
| 171 | +grep -q 'xpkg reference' badref.log || { |
| 172 | + cat badref.log; echo "rejection did not explain the expected form"; exit 1; } |
| 173 | + |
| 174 | +echo "PASS: [target.X].sysroot override, zero-libc tier, and the three target queries" |
0 commit comments