v / cmd / v2 / test_v2_self.sh
73 lines · 57 sloc · 2.12 KB · 11af49ebc0927ba5baec8260d26040444ae48b61
Raw
1#!/usr/bin/env bash
2set -euo pipefail
3
4script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
5repo_root="$(cd -- "${script_dir}/../.." && pwd)"
6
7v2_source="${script_dir}/v2.v"
8v2_bin="${script_dir}/v2"
9v3_bin="${script_dir}/v3"
10v4_bin="${script_dir}/v4"
11v5_bin="${script_dir}/v5"
12
13# Variable for the backend (originally cleanc)
14backend="cleanc"
15
16v1_compiler="${V:-${repo_root}/v}"
17if [[ ! -x "${v1_compiler}" ]]; then
18 fallback_v1_compiler="${HOME}/code/v/v"
19 if [[ -x "${fallback_v1_compiler}" ]]; then
20 v1_compiler="${fallback_v1_compiler}"
21 else
22 echo "FAILURE: v1 compiler not found or not executable: ${v1_compiler}"
23 exit 1
24 fi
25fi
26
27# V1's formatter may clobber v2 source files — backup and restore.
28v2_src="${repo_root}/vlib/v2"
29v2_bak="/tmp/v2_src_bak_self_test"
30rm -rf "${v2_bak}"
31cp -R "${v2_src}" "${v2_bak}"
32
33# Build v2 with v1.
34rm -f "${v2_bin}" "${v3_bin}" "${v3_bin}.c" "${v4_bin}" "${v4_bin}.c" "${v5_bin}" "${v5_bin}.c"
35"${v1_compiler}" -gc none -cc cc -o "${v2_bin}" "${v2_source}"
36
37# Restore v2 sources after V1 build.
38rsync -a --delete "${v2_bak}/" "${v2_src}/"
39
40# Use clang instead of TCC for v2-compiled C code.
41export V2CC="${V2CC:-cc}"
42
43# Clear stale caches to avoid reusing incompatible generated C across runs.
44rm -rf /tmp/v2_cleanc_obj_cache
45
46# Use v2 to compile itself to v3 (using defined backend).
47"${v2_bin}" -gc none -o "${v3_bin}" -backend "${backend}" "${v2_source}"
48
49echo "SUCCESS: v2 successfully compiled itself to v3"
50echo "v3 binary size: $(ls -lh "${v3_bin}" | awk '{print $5}')"
51
52if [[ "${backend}" != "cleanc" ]]; then
53 "${v3_bin}" -gc none -o "${v4_bin}" -backend "${backend}" "${v2_source}"
54 printf '\nV4 compiled\n\n'
55
56 "${v4_bin}" -gc none -o "${v5_bin}" -backend "${backend}" "${v2_source}"
57 printf '\nV5 compiled\n\n'
58fi
59
60# Test that v3 runs and produces expected output.
61output="$("${v3_bin}" 2>&1 || true)"
62expected='At least 1 .v file expected'
63
64if echo "${output}" | grep -q "${expected}"; then
65 echo "SUCCESS"
66else
67 echo "FAILURE: Expected '${expected}' but got:"
68 echo "${output}"
69 exit 1
70fi
71
72echo ''
73echo '=== SELF-COMPILATION TEST PASSED ==='
74