|
| 1 | +#!/usr/bin/env -S v |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +const total_steps = 6 |
| 6 | +const temp_prefix = 'v3_test_all' |
| 7 | + |
| 8 | +struct Config { |
| 9 | + vexe string |
| 10 | + script_dir string |
| 11 | + repo_root string |
| 12 | + tests_dir string |
| 13 | + v3_src string |
| 14 | + host_backend string |
| 15 | +} |
| 16 | + |
| 17 | +fn main() { |
| 18 | + cfg := parse_config() |
| 19 | + os.chdir(cfg.repo_root) or { fail('failed to enter ${cfg.repo_root}: ${err}') } |
| 20 | + |
| 21 | + v3_bin := temp_path('v3') |
| 22 | + hello_c_bin := temp_path('hello_c') |
| 23 | + hello_arm_bin := temp_path('hello_arm64') |
| 24 | + v4_arm_bin := temp_path('v4_arm64') |
| 25 | + v3_lang_bin := temp_path('v3_lang') |
| 26 | + v4_bin := temp_path('v4_chain') |
| 27 | + v5_bin := temp_path('v5_chain') |
| 28 | + v6_bin := temp_path('v6_chain') |
| 29 | + cleanup_files([ |
| 30 | + v3_bin, |
| 31 | + hello_c_bin, |
| 32 | + hello_c_bin + '.c', |
| 33 | + hello_arm_bin, |
| 34 | + v4_arm_bin, |
| 35 | + v3_lang_bin, |
| 36 | + v3_lang_bin + '.c', |
| 37 | + v4_bin, |
| 38 | + v4_bin + '.c', |
| 39 | + v5_bin, |
| 40 | + v5_bin + '.c', |
| 41 | + v6_bin, |
| 42 | + v6_bin + '.c', |
| 43 | + ]) |
| 44 | + |
| 45 | + section(1, 'V3 unit tests') |
| 46 | + run('${q(cfg.vexe)} -silent test ${q(cfg.script_dir)}') |
| 47 | + |
| 48 | + section(2, 'Build v3') |
| 49 | + run('${q(cfg.vexe)} -o ${q(v3_bin)} ${q(cfg.v3_src)}') |
| 50 | + |
| 51 | + section(3, 'C backend hello world') |
| 52 | + hello_v := os.join_path(cfg.tests_dir, 'hello.v') |
| 53 | + run('${q(v3_bin)} ${q(hello_v)} -b c -o ${q(hello_c_bin)}') |
| 54 | + run(q(hello_c_bin)) |
| 55 | + cleanup_files([hello_c_bin, hello_c_bin + '.c']) |
| 56 | + |
| 57 | + section(4, 'ARM64 self-host hello world') |
| 58 | + if cfg.host_backend == 'arm64' { |
| 59 | + run('${q(v3_bin)} --no-parallel -selfhost -b arm64 -o ${q(v4_arm_bin)} ${q(cfg.v3_src)}') |
| 60 | + run('${q(v4_arm_bin)} --no-parallel -b arm64 -o ${q(hello_arm_bin)} ${q(hello_v)}') |
| 61 | + run(q(hello_arm_bin)) |
| 62 | + cleanup_files([v4_arm_bin, hello_arm_bin]) |
| 63 | + } else { |
| 64 | + println(' Skipping ARM64 self-host on ${cfg.host_backend} host') |
| 65 | + } |
| 66 | + |
| 67 | + section(5, 'Self-host chain (v3->v4->v5->v6)') |
| 68 | + println(' Building v4 from v3...') |
| 69 | + run('${q(v3_bin)} --no-parallel -selfhost -o ${q(v4_bin)} ${q(cfg.v3_src)}') |
| 70 | + println(' Building v5 from v4...') |
| 71 | + run('${q(v4_bin)} --no-parallel -selfhost -o ${q(v5_bin)} ${q(cfg.v3_src)}') |
| 72 | + println(' Building v6 from v5...') |
| 73 | + run('${q(v5_bin)} --no-parallel -selfhost -o ${q(v6_bin)} ${q(cfg.v3_src)}') |
| 74 | + converged_size := assert_same_file_bytes('v5/v6 generated C output', v5_bin + '.c', v6_bin + |
| 75 | + '.c') |
| 76 | + println(' v5.c=v6.c (${converged_size} bytes) - chain converged') |
| 77 | + cleanup_files([v4_bin, v4_bin + '.c', v5_bin, v5_bin + '.c', v6_bin, v6_bin + '.c']) |
| 78 | + |
| 79 | + section(6, 'Language feature parity') |
| 80 | + lang_v := os.join_path(cfg.tests_dir, 'test_all_lang_features.v') |
| 81 | + lang_out := os.join_path(cfg.tests_dir, 'test_all_lang_features.out') |
| 82 | + run('${q(v3_bin)} ${q(lang_v)} -b c -o ${q(v3_lang_bin)}') |
| 83 | + v3_c_out := run_output(q(v3_lang_bin)) |
| 84 | + expected_out := read_text_file(lang_out) |
| 85 | + assert_same_text('language feature output', v3_c_out, expected_out) |
| 86 | + println(' v3 C OK (${v3_c_out.split_into_lines().len} lines)') |
| 87 | + println(' ARM64 coverage is the one-generation self-host smoke test in step 4') |
| 88 | + cleanup_files([v3_bin, v3_lang_bin, v3_lang_bin + '.c']) |
| 89 | + |
| 90 | + println('') |
| 91 | + println('=== ALL TESTS PASSED ===') |
| 92 | +} |
| 93 | + |
| 94 | +fn parse_config() Config { |
| 95 | + script_dir := os.real_path(@DIR) |
| 96 | + repo_root := os.real_path(os.join_path(script_dir, '..', '..')) |
| 97 | + tests_dir := os.join_path(script_dir, 'tests') |
| 98 | + vexe := absolute_path(@VEXE) |
| 99 | + if !os.is_executable(vexe) { |
| 100 | + fail('FAIL: V compiler not found: ${vexe}') |
| 101 | + } |
| 102 | + return Config{ |
| 103 | + vexe: vexe |
| 104 | + script_dir: script_dir |
| 105 | + repo_root: repo_root |
| 106 | + tests_dir: tests_dir |
| 107 | + v3_src: os.join_path(script_dir, 'v3.v') |
| 108 | + host_backend: native_backend_arch() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn native_backend_arch() string { |
| 113 | + machine := os.uname().machine.to_lower() |
| 114 | + match machine { |
| 115 | + 'x86_64', 'amd64' { |
| 116 | + return 'x64' |
| 117 | + } |
| 118 | + 'aarch64', 'arm64' { |
| 119 | + return 'arm64' |
| 120 | + } |
| 121 | + else { |
| 122 | + return machine |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +fn temp_path(name string) string { |
| 128 | + return os.join_path(os.temp_dir(), '${temp_prefix}_${name}') |
| 129 | +} |
| 130 | + |
| 131 | +fn absolute_path(path string) string { |
| 132 | + if os.is_abs_path(path) { |
| 133 | + return path |
| 134 | + } |
| 135 | + return os.join_path(os.getwd(), path) |
| 136 | +} |
| 137 | + |
| 138 | +fn section(step int, title string) { |
| 139 | + if step > 1 { |
| 140 | + println('') |
| 141 | + } |
| 142 | + println('=== ${step}/${total_steps}: ${title} ===') |
| 143 | +} |
| 144 | + |
| 145 | +fn run(cmd string) { |
| 146 | + println('> ${cmd}') |
| 147 | + code := os.system(cmd) |
| 148 | + if code != 0 { |
| 149 | + exit(code) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +fn run_output(cmd string) string { |
| 154 | + stdout_path := temp_path('stdout') |
| 155 | + cleanup_files([stdout_path]) |
| 156 | + println('> ${cmd}') |
| 157 | + code := os.system('${cmd} > ${q(stdout_path)}') |
| 158 | + if code != 0 { |
| 159 | + exit(code) |
| 160 | + } |
| 161 | + content := read_text_file(stdout_path) |
| 162 | + cleanup_files([stdout_path]) |
| 163 | + return content |
| 164 | +} |
| 165 | + |
| 166 | +fn read_text_file(path string) string { |
| 167 | + content := os.read_file(path) or { |
| 168 | + fail('FAIL: failed to read ${path}: ${err}') |
| 169 | + return '' |
| 170 | + } |
| 171 | + return content |
| 172 | +} |
| 173 | + |
| 174 | +fn read_binary_file(path string) []u8 { |
| 175 | + content := os.read_bytes(path) or { |
| 176 | + fail('FAIL: failed to read ${path}: ${err}') |
| 177 | + return []u8{} |
| 178 | + } |
| 179 | + return content |
| 180 | +} |
| 181 | + |
| 182 | +fn assert_same_file_bytes(label string, left_path string, right_path string) int { |
| 183 | + left := read_binary_file(left_path) |
| 184 | + right := read_binary_file(right_path) |
| 185 | + if left != right { |
| 186 | + fail('FAIL: ${label} differs byte-for-byte (${left.len} bytes vs ${right.len} bytes)') |
| 187 | + } |
| 188 | + return left.len |
| 189 | +} |
| 190 | + |
| 191 | +fn assert_same_text(label string, actual string, expected string) { |
| 192 | + if actual == expected { |
| 193 | + return |
| 194 | + } |
| 195 | + actual_lines := actual.split_into_lines() |
| 196 | + expected_lines := expected.split_into_lines() |
| 197 | + min_lines := if actual_lines.len < expected_lines.len { |
| 198 | + actual_lines.len |
| 199 | + } else { |
| 200 | + expected_lines.len |
| 201 | + } |
| 202 | + for i in 0 .. min_lines { |
| 203 | + if actual_lines[i] != expected_lines[i] { |
| 204 | + fail('FAIL: ${label} differs at line ${i + 1}: expected `${expected_lines[i]}`, got `${actual_lines[i]}`') |
| 205 | + } |
| 206 | + } |
| 207 | + fail('FAIL: ${label} line count differs: expected ${expected_lines.len}, got ${actual_lines.len}') |
| 208 | +} |
| 209 | + |
| 210 | +fn cleanup_files(paths []string) { |
| 211 | + for path in paths { |
| 212 | + os.rm(path) or {} |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +fn q(path string) string { |
| 217 | + return os.quoted_path(path) |
| 218 | +} |
| 219 | + |
| 220 | +fn fail(message string) { |
| 221 | + eprintln(message) |
| 222 | + exit(1) |
| 223 | +} |
0 commit comments