Files
ziran/tests/native_build_cache_cases.zi
T
waozi 4838ce9b2f Improve runtimes, add an experimental direct backend, and prune Ziran tests
Prepare short portable helpers and scalar record reads, improve JSON SIMD scanning, expose one-line execution, and publish qualified Alpha benchmark artifacts. Add the experimental verified lowered execution, serialization and Linux x86-64 backend with source maps.

Physically remove 263 redundant test entry points and unused helpers, migrate retained helpers to Ziran, and execute the full retained suite without sharding. Allow ignored backup state for the managed physical Alpha workflow.

Local validation: all 268 retained tests passed across the full run and corrected targeted reruns; bootstrap matches. Current assembly verifier, x86-64, executor and serializer tests passed. Website examples and all 4661 local links passed. Six Alpha JSON/text workloads retain verified artifacts, raw samples and explicit unsupported Wasm diagnostics. The mobile sub-500 ns target remains pending.
2026-11-11 18:46:34 -03:00

214 lines
17 KiB
Ziran

#import "native_test"
#import "native_build_support"
#import "native_cache_support"
#import "check_support"
#import "std/byte_text_linux"
#import "std/file"
#import "std/file_tree_linux"
#import "std/json_document"
#import "std/process_run_linux"
#import "std/text_tools"
#import "std/timer_linux"
using Vectors :: #import "std/vec"
CacheChangedHeader :: (fixture: CacheFixture, header: string) -> bool {
test := fixture.test
ready := CheckPath(test.work, "ready"); release := CheckPath(test.work, "release")
NativeSet(test, "CACHE_GATE", "1"); NativeSet(test, "CACHE_READY", ready); NativeSet(test, "CACHE_RELEASE", release)
log := CheckPath(test.work, "gated.log")
options: ProcessOptions = .{.arguments = fixture.command, .environment = test.environment[:], .new_session = true,
.output_path = log, .merge_error = true}
child: ProcessRun
if !NativeAssert(test, StartProcess(options, *child), "start concurrent header fixture") { return false }
defer {
if !PollProcess(*child) { unused SignalProcess(*child, 15, true) }
unused WaitProcess(*child)
}
deadline := MonotonicMilliseconds() + 30000
while !PathExists(ready) {
if !NativeAssert(test, !PollProcess(*child) && MonotonicMilliseconds() < deadline, CheckConcat("concurrent compiler gate failed\n", NativeRead(test, log))) { return false }
unused SleepMilliseconds(5)
}
if !NativeWrite(test, header, "#define CONFIG 13\n") || !NativeWrite(test, release, "") { return false }
while !PollProcess(*child) {
if !NativeAssert(test, MonotonicMilliseconds() < deadline, "concurrent compiler did not finish") { return false }
unused SleepMilliseconds(5)
}
if !NativeAssert(test, child.code == 0, NativeRead(test, log)) { return false }
run := string.[CheckPath(fixture.output, "app")]; unused NativeRun(test, run[:], "", 51)
return NativeAssert(test, !PathExists(CheckPath(fixture.output, ".objects/host.c.o.inputs")), "header changed during compilation was cached as an old object's input")
}
CacheProbeCounters :: (fixture: CacheFixture, path: string, clang: bool) -> bool {
test := fixture.test
contents := NativeRead(test, path)
lines := TextLines(contents)
if !NativeAssert(test, lines.count == 2, "wrong native cache profile record count") { return false }
document: JsonDocument
if !NativeAssert(test, JsonRead(*document, lines[1]), "invalid reuse profile") { return false }
counters := JsonMember(*document, document.root, "counters")
objects := NativeObjects(fixture.output)
if !NativeAssert(test, NativeCounter(*document, counters, "native_probe_compile") == (ifx clang then 1 else 0) &&
NativeCounter(*document, counters, "native_probe_pipe") == (ifx clang then 0 else 1) &&
NativeCounter(*document, counters, "native_object_reuse") == objects.count, "native probe/reuse counters disagree") { return false }
if !NativeAssert(test, JsonRead(*document, lines[0]), "invalid fresh compile profile") { return false }
counters = JsonMember(*document, document.root, "counters")
return NativeAssert(test, NativeCounter(*document, counters, "native_object_compile") == objects.count &&
NativeCounter(*document, counters, "native_object_pipe") == (ifx clang then 0 else objects.count), "native object pipe counters disagree")
}
#program_export
main :: (argc: s32, argv: **u8) -> s32 {
test := NativeStart(argc, argv, "native-build-cache-")
if test.failed { return 1 }
defer NativeCleanup(*test)
compiler := NativeCompiler()
output := CheckPath(test.work, "out with spaces")
headers := CheckPath(test.work, "headers")
header := CheckPath(headers, "config #$ space.h")
host := CheckPath(output, "host.c")
source := CheckPath(test.work, "app.zi")
command := string.[test.ziran, "build", "--target=c", "--exe", "--entry", "app:Answer", "--diagnostics=json", "--root", test.work, "-o", output, source]
fixture := CacheFixture.{*test, command[:], compiler, output}
if !CachePrepare(fixture) { return 1 }
flags := CheckConcat("-O2 -isystem ", headers)
if !CacheBuild(fixture, 42) { return 1 }
stamps := NativeFiles(output, ".inputs", true)
if !NativeAssert(*test, stamps.count > 0, "compiler not eligible for native object reuse") || !CacheBuild(fixture, 42, "exact") { return 1 }
leaf := string.["leaf.c.o"]; stable := string.["stable.c.o"]; host_object := string.["host.c.o"]
stable_app := string.["stable.c.o", "app.c.o"]
if !NativeWrite(*test, CheckPath(test.work, "leaf.zi"), CacheLeaf(2)) || !CacheBuild(fixture, 43, "exact", leaf[:]) { return 1 }
if !NativeWrite(*test, CheckPath(test.work, "shared.zi"), "Increment :: 10;\n") || !CacheBuild(fixture, 46, "contains", leaf[:], stable[:]) { return 1 }
if !NativeWrite(*test, CheckPath(test.work, "stable.zi"), "Read :: () -> s32 { three: s32 = 3; return three }\n") || !CacheBuild(fixture, 46, "contains", stable_app[:], leaf[:]) { return 1 }
time_reference := CheckPath(test.work, "time-reference")
if !CachePreserveTime(*test, header, time_reference, false) || !NativeWrite(*test, header, "#define CONFIG 12\n") ||
!CachePreserveTime(*test, header, time_reference, true) || !CacheBuild(fixture, 47, "exact", host_object[:]) { return 1 }
if !CacheLibrary(fixture, 21) || !CacheBuild(fixture, 48, "exact") { return 1 }
flags = CheckConcat(flags, " -DBUMP=1"); NativeSet(*test, "CFLAGS", flags)
if !CacheBuild(fixture, 49, "all") { return 1 }
NativeSet(*test, "ZIRAN_CACHE_TEST_ENV", "changed")
if !CacheBuild(fixture, 49, "exact") { return 1 }
NativeSet(*test, "SOURCE_DATE_EPOCH", "1")
if !CacheBuild(fixture, 49, "all") || !CacheBuild(fixture, 49, "exact") { return 1 }
object := CheckPath(output, ".objects/stable.c.o")
if !NativeWrite(*test, CheckConcat(object, ".inputs"), "truncated") || !CacheBuild(fixture, 49, "exact", stable[:]) ||
!NativeWrite(*test, object, "not an object") || !CacheBuild(fixture, 49, "exact", stable[:]) { return 1 }
unused RemoveFile(object)
if !CacheBuild(fixture, 49, "exact", stable[:]) { return 1 }
unused RemoveFile(header)
if !CacheBuild(fixture, 49, "", cache_no_names[:], cache_no_names[:], true) || !NativeWrite(*test, header, "#define CONFIG 12\n") || !CacheBuild(fixture, 49, "contains", host_object[:]) { return 1 }
empty := CheckPath(headers, "empty.h"); large := CheckPath(headers, "large.h")
large_bytes: Vec(u8); BuilderAppend(large_bytes, "/*")
for index: 0..16999 { unused index; BuilderAppend(large_bytes, "x") }; BuilderAppend(large_bytes, "*/\n")
if !NativeWrite(*test, empty, "") || !NativeWrite(*test, large, BuilderFinish(large_bytes)) ||
!NativeWrite(*test, host, "#include \"empty.h\"\n#include \"large.h\"\nint host(void) { return 34; }\n") || !CacheBuild(fixture, 49) { return 1 }
stamp := NativeRead(*test, CheckPath(output, ".objects/host.c.o.inputs"))
digest_files := string.[empty, large, CheckPath(output, ".objects/host.c.o")]
for index: cast(s64)0..digest_files.count - 1 {
digest: Vec(u8)
if !NativeAssert(*test, TextHexDecodeInto(CheckHash(digest_files[index]), *digest, 32), "decode independent SHA-256") { return 1 }
expected := TextFromBytes(digest[:])
if !NativeAssert(*test, ifx index == 2 then stamp.count >= 80 && stamp[48:80] == expected else TextContains(stamp, expected), "native input stamp digest differs from SHA-256") { return 1 }
}
if !CacheBuild(fixture, 49, "exact") { return 1 }
early := CheckPath(test.work, "early"); late := CheckPath(test.work, "late")
if !NativeAssert(*test, CreateDirectories(CheckPath(early, "nested")), "create earlier include folder") ||
!NativeWrite(*test, CheckPath(late, "nested/value.h"), "#define VALUE 34\n") || !NativeWrite(*test, host, "#include \"nested/value.h\"\nint host(void) { return VALUE; }\n") { return 1 }
flags = CheckConcat(flags, CheckConcat(" -I", CheckConcat(early, CheckConcat(" -I", late)))); NativeSet(*test, "CFLAGS", flags)
if !CacheBuild(fixture, 49) || !CacheBuild(fixture, 49, "exact") || !NativeWrite(*test, CheckPath(early, "nested/value.h"), "#define VALUE 35\n") || !CacheBuild(fixture, 50, "exact", host_object[:]) { return 1 }
if !NativeWrite(*test, host, "#if __has_include(\"optional.h\")\n#include \"optional.h\"\n#else\n#define VALUE 35\n#endif\nint host(void) { return VALUE; }\n") ||
!CacheBuild(fixture, 50) || !CacheBuild(fixture, 50, "exact") || !NativeWrite(*test, CheckPath(headers, "optional.h"), "#define VALUE 36\n") || !CacheBuild(fixture, 51, "contains", host_object[:]) { return 1 }
if !NativeWrite(*test, host, "#define HEADER \"optional.h\"\n#if __has_include(HEADER)\n#include HEADER\n#endif\nint host(void) { return VALUE; }\n") || !CacheBuild(fixture, 51) || !CacheBuild(fixture, 51, "exact", host_object[:]) { return 1 }
if !NativeWrite(*test, host, "int host(void) { return 36 + (__TIME__[0] == 0); }\n") || !CacheBuild(fixture, 51) || !CacheBuild(fixture, 51, "exact", host_object[:]) { return 1 }
proxy := CheckPath(test.work, "gcc"); proxy_source := CheckPath(test.work, "proxy.c")
if !NativeWrite(*test, proxy_source, NativeRead(*test, CheckPath(test.repo, "tests/fixtures/native_cache/proxy.c.in"))) || !CacheProxy(fixture, proxy_source, proxy, 0) { return 1 }
NativeSet(*test, "CC", proxy)
if !NativeWrite(*test, host, "#include \"config #$ space.h\"\nint host(void) { return CONFIG + 24; }\n") || !CacheBuild(fixture, 51) || !CacheBuild(fixture, 51, "exact") { return 1 }
if !CachePreserveTime(*test, proxy, time_reference, false) || !CacheProxy(fixture, proxy_source, proxy, 1) || !CachePreserveTime(*test, proxy, time_reference, true) ||
!CacheBuild(fixture, 51, "all") || !CacheBuild(fixture, 51, "exact") { return 1 }
probe_command: Vec(string); VecPush(probe_command, compiler)
if !NativeWords(flags, *probe_command) { return 1 }
probe_options := string.["-I", output, "-pipe", "-v", "-x", "c", "-c", "/dev/null", "-o", "/dev/null"]
for option: probe_options { VecPush(probe_command, option) }
probe := NativeRun(*test, probe_command[:])
original_lines := TextLines(probe.error)
lines: Vec(string); assembler := ""
for line: original_lines {
words: Vec(string)
if assembler == "" && TextHasPrefix(line, " ") && NativeWords(line, *words) && words.count > 0 && TextHasSuffix(PathName(words[0]), "as") { assembler = line }
else { VecPush(lines, line) }
}
early_at: s64 = -1; late_at: s64 = -1
for index: cast(s64)0..lines.count - 1 {
if early_at < 0 && (TextHasPrefix(lines[index], "ignoring nonexistent directory") || TextHasPrefix(lines[index], "#include ")) { early_at = index }
if lines[index] == "End of search list." { late_at = index + 1 }
}
if assembler != "" && early_at >= 0 && late_at >= 0 {
probe_path := CheckPath(test.work, "compiler-probe.txt"); NativeSet(*test, "CACHE_PROBE_TEXT", probe_path)
if !CacheProbeText(*test, probe_path, lines[:], assembler, early_at) || !CacheBuild(fixture, 51) ||
!CacheProbeText(*test, probe_path, lines[:], assembler, late_at) || !CacheBuild(fixture, 51, "exact") ||
!CacheProbeText(*test, probe_path, lines[:], assembler, early_at) || !CacheBuild(fixture, 51, "exact") { return 1 }
prefix := "\tcompiled by GNU C version 99.0.0, "
libraries := "GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version 0.27\n"
banner := "GNU assembler version 2.44 (target) using BFD version 2.44\n"
ordinary := CheckConcat(probe.error, CheckConcat(prefix, CheckConcat(libraries, banner)))
interleaved := CheckConcat(probe.error, CheckConcat(prefix, CheckConcat(banner, libraries)))
if !NativeWrite(*test, probe_path, ordinary) || !CacheBuild(fixture, 51) || !NativeWrite(*test, probe_path, interleaved) || !CacheBuild(fixture, 51, "exact") ||
!NativeWrite(*test, probe_path, ordinary) || !CacheBuild(fixture, 51, "exact") || !NativeWrite(*test, probe_path, TextReplace(ordinary, "GMP version 6.3.0", "GMP version 7.0.0")) ||
!CacheBuild(fixture, 51, "all") || !CacheBuild(fixture, 51, "exact") { return 1 }
NativeUnset(*test, "CACHE_PROBE_TEXT")
}
config := CheckPath(test.work, "compiler-config")
if !NativeWrite(*test, config, "0\n") { return 1 }; NativeSet(*test, "CACHE_CONFIG", config)
if !NativeWrite(*test, host, "#include \"config #$ space.h\"\nint host(void) { return CONFIG + 24 + CACHE_ADDEND; }\n") || !CacheBuild(fixture, 51) || !CacheBuild(fixture, 51, "exact") { return 1 }
if !CachePreserveTime(*test, config, time_reference, false) || !NativeWrite(*test, config, "1\n") || !CachePreserveTime(*test, config, time_reference, true) ||
!CacheBuild(fixture, 52, "all") || !CacheBuild(fixture, 52, "exact") { return 1 }
alias := CheckPath(test.work, "cc")
copy := string.["cp", "-p", proxy, alias]; unused NativeRun(*test, copy[:]); NativeSet(*test, "CC", alias)
if !NativeWrite(*test, config, "0\n") || !CacheBuild(fixture, 51) || !CacheBuild(fixture, 51, "exact") || !NativeWrite(*test, config, "1\n") || !CacheBuild(fixture, 52, "all") || !CacheBuild(fixture, 52, "exact") { return 1 }
NativeSet(*test, "CC", proxy)
compile_log := CheckPath(test.work, "compile-flags.log"); NativeSet(*test, "CACHE_COMPILE_LOG", compile_log)
if !CacheBuild(fixture, 52) { return 1 }
objects := NativeObjects(output)
if !CacheRepeatedLines(*test, compile_log, "1", objects.count) { return 1 }
previous := NativeRead(*test, compile_log)
if !CacheBuild(fixture, 52, "exact") || !NativeAssert(*test, NativeRead(*test, compile_log) == previous, "reuse unexpectedly ran the object compiler") { return 1 }
NativeSet(*test, "CFLAGS", CheckConcat(flags, " -pipe")); unused RemoveFile(compile_log)
if !CacheBuild(fixture, 52) || !CacheRepeatedLines(*test, compile_log, "1", objects.count) { return 1 }
if !NativeWrite(*test, CheckPath(test.work, "-pipe"), "#define PIPE_FLAG_INPUT 1\n") { return 1 }
NativeSet(*test, "CFLAGS", CheckConcat(flags, " -include -pipe")); unused RemoveFile(compile_log)
if !CacheBuild(fixture, 52, "", cache_no_names[:], cache_no_names[:], false, test.work) || !CacheRepeatedLines(*test, compile_log, "2", objects.count) { return 1 }
NativeSet(*test, "CFLAGS", flags); NativeSet(*test, "CACHE_FAIL_PROBE", "1"); unused RemoveFile(compile_log)
if !CacheBuild(fixture, 52) || !CacheRepeatedLines(*test, compile_log, "0", objects.count) { return 1 }
NativeUnset(*test, "CACHE_FAIL_PROBE"); NativeUnset(*test, "CACHE_COMPILE_LOG")
probe_log := CheckPath(test.work, "probe.log"); NativeSet(*test, "CACHE_PROBE_LOG", probe_log)
if !CacheBuild(fixture, 52) || !NativeWrite(*test, probe_log, "") || !CacheBuild(fixture, 52, "exact") || !CacheRepeatedLines(*test, probe_log, "pipe", 1) { return 1 }
profile := CheckPath(test.work, "probe-profile.jsonl"); NativeSet(*test, "ZIRAN_PROFILE", profile); NativeSet(*test, "SOURCE_DATE_EPOCH", "2")
if !CacheBuild(fixture, 52) || !CacheBuild(fixture, 52, "exact") || !CacheProbeCounters(fixture, profile, false) { return 1 }
NativeUnset(*test, "ZIRAN_PROFILE")
version := CheckPath(test.work, "compiler-version")
if !NativeWrite(*test, version, "1\n") { return 1 }; NativeSet(*test, "CACHE_VERSION_CONFIG", version)
if !CacheBuild(fixture, 52) || !CacheBuild(fixture, 52, "exact") || !CachePreserveTime(*test, version, time_reference, false) || !NativeWrite(*test, version, "2\n") ||
!CachePreserveTime(*test, version, time_reference, true) || !CacheBuild(fixture, 52, "all") || !CacheBuild(fixture, 52, "exact") { return 1 }
NativeUnset(*test, "CACHE_VERSION_CONFIG")
failures := string.["CACHE_FAIL_PROBE", "CACHE_EMPTY_PROBE", "CACHE_BINARY_PROBE", "CACHE_AMBIGUOUS_PROBE", "CACHE_LARGE_PROBE"]
for option: failures {
NativeSet(*test, option, "1")
if !CacheBuild(fixture, 52, "all") || !CacheBuild(fixture, 52, "all") { return 1 }
NativeUnset(*test, option)
}
NativeUnset(*test, "CACHE_CONFIG"); NativeUnset(*test, "CACHE_PROBE_LOG")
if !NativeWrite(*test, host, "#include \"config #$ space.h\"\nint host(void) { return CONFIG + 24; }\n") || !CacheChangedHeader(fixture, header) || !CacheBuild(fixture, 52, "exact", host_object[:]) || !CacheBuild(fixture, 52, "exact") { return 1 }
clang := ""
candidates := string.["/bin/clang", "/usr/bin/clang", "/bin/clang-19", "/usr/bin/clang-19"]
for candidate: candidates { if clang == "" && IsFile(candidate) { clang = TextDetach(ResolvePath(candidate)) } }
if clang != "" {
NativeSet(*test, "CC", clang); NativeUnset(*test, "CACHE_GATE"); NativeUnset(*test, "CACHE_READY"); NativeUnset(*test, "CACHE_RELEASE")
profile = CheckPath(test.work, "clang-profile.jsonl"); NativeSet(*test, "ZIRAN_PROFILE", profile)
if !CacheBuild(fixture, 52) || !CacheBuild(fixture, 52, "exact") || !CacheProbeCounters(fixture, profile, true) { return 1 }
}
if test.failed { return 1 }
print("Local native cache tests: content hashes, include lookup, tool identity, flags, environment, probes and concurrent edits passed\n")
return 0
}