cmd: share package builds across test products

This commit is contained in:
2026-08-12 11:42:24 +09:00
parent 7d25ad9f9f
commit dd975d195c
3 changed files with 898 additions and 384 deletions

View File

@@ -418,6 +418,7 @@ def SEP_VARIANT_PRODUCTION: i32 = 0;
def SEP_VARIANT_SAME_TEST: i32 = 1;
def SEP_VARIANT_EXTERNAL: i32 = 2;
def SEP_TEST_SUPPORT_MODULE: str = "__wwtest";
def SEP_MAXPRODUCT: i32 = 2;
// Classify a selected directory entry: 1 production, 2 test, 0 skipped,
// -1 @test outside *_test.ww, -2 non-regular source.
@@ -727,6 +728,8 @@ type seppkg = struct {
nsources: i32,
isdir: i32,
variant: i32,
root: bool,
failed: bool,
testsupport: bool,
deps: []i32, // direct-dep indices into sepgraph.pkg
ndeps: i32,
@@ -738,21 +741,36 @@ type sepgraph = struct {
n: i32,
};
type sepproduct = struct {
out: *u8,
testpackage: *u8,
status: *u8,
variant: i32,
root: i32,
};
fn sepfindoraddvariant(g: *sepgraph, path: *u8, entry: *u8,
isdir: i32, variant: i32, testpackage: *u8) i32 = {
isdir: i32, variant: i32, testpackage: *u8, root: bool) i32 = {
if (cstrlen(path) >= 256u64) {
cerr("ww: package path is too long (limit 255 bytes)\n");
return -1;
};
let i: i32 = 0;
for (i < g.n) {
let testproductionpair: bool = i == 0
&& g.pkg[i].variant != SEP_VARIANT_PRODUCTION
&& variant == SEP_VARIANT_PRODUCTION && isdir != 0
let samelocation: bool = os.samefile(pathstr(g.pkg[i].entry),
pathstr(entry));
let testrootpair: bool = samelocation && isdir != 0
&& g.pkg[i].isdir != 0
&& os.samefile(pathstr(g.pkg[i].entry), pathstr(entry));
&& ((root && g.pkg[i].root
&& variant != g.pkg[i].variant)
|| (root && !g.pkg[i].root
&& variant != SEP_VARIANT_PRODUCTION
&& g.pkg[i].variant == SEP_VARIANT_PRODUCTION)
|| (!root && g.pkg[i].root
&& variant == SEP_VARIANT_PRODUCTION
&& g.pkg[i].variant != SEP_VARIANT_PRODUCTION));
if (cstreq(g.pkg[i].path, path)) {
if (testproductionpair) {
if (testrootpair) {
i += 1;
continue;
};
@@ -764,8 +782,8 @@ fn sepfindoraddvariant(g: *sepgraph, path: *u8, entry: *u8,
};
return i;
};
if (os.samefile(pathstr(g.pkg[i].entry), pathstr(entry))) {
if (testproductionpair) {
if (samelocation) {
if (testrootpair) {
i += 1;
continue;
};
@@ -799,6 +817,8 @@ fn sepfindoraddvariant(g: *sepgraph, path: *u8, entry: *u8,
g.pkg[g.n].nsources = 0;
g.pkg[g.n].isdir = isdir;
g.pkg[g.n].variant = variant;
g.pkg[g.n].root = root;
g.pkg[g.n].failed = false;
g.pkg[g.n].testsupport = false;
let dslot: []i32 = alloc([], SEP_MAXPKG: u64)!;
dslot.len = SEP_MAXPKG;
@@ -812,7 +832,7 @@ fn sepfindoraddvariant(g: *sepgraph, path: *u8, entry: *u8,
fn sepfindoradd(g: *sepgraph, path: *u8, entry: *u8, isdir: i32) i32 = {
return sepfindoraddvariant(g, path, entry, isdir,
SEP_VARIANT_PRODUCTION, nil);
SEP_VARIANT_PRODUCTION, nil, false);
};
// Release the package-owned directory-membership lists through one graph
@@ -839,18 +859,24 @@ fn sepgraphfree(g: *sepgraph) void = {
};
};
// Build "<scratch>/<base><suffix>" NUL-term; base = path, or "__root"
// for the empty root path.
// Build one artifact path. Test roots use distinct names even though both
// compiler units reset to the bare executable namespace.
fn sepfname(g: *sepgraph, pi: i32, scratch: *u8, suffix: str) *u8 = {
let buf: []u8 = alloc([], (os.PATH_MAX: u64))!;
buf.len = os.PATH_MAX;
let off: u64 = cstrinto(buf.ptr, 0u64, scratch);
off = byteinto(buf.ptr, off, 47u8); // '/'
if (g.pkg[pi].path[0u64] != 0u8) {
if (g.pkg[pi].root
&& g.pkg[pi].variant == SEP_VARIANT_SAME_TEST) {
off = strinto(buf.ptr, off, "__ww-test-same");
} else { if (g.pkg[pi].root
&& g.pkg[pi].variant == SEP_VARIANT_EXTERNAL) {
off = strinto(buf.ptr, off, "__ww-test-external");
} else { if (g.pkg[pi].path[0u64] != 0u8) {
off = cstrinto(buf.ptr, off, g.pkg[pi].path);
} else {
off = strinto(buf.ptr, off, "__root");
};
}; }; };
off = strinto(buf.ptr, off, suffix);
cstrseal(buf.ptr, off);
return buf.ptr;
@@ -1085,7 +1111,10 @@ fn sepscanfile(g: *sepgraph, pi: i32, file: *u8, searchpath: *u8,
// dependency scanning. Recurse over the resulting edges. `color` doubles as
// a loaded marker (2); reset to white before topo.
fn seploadpkg(g: *sepgraph, pi: i32, searchpath: *u8) i32 = {
if (g.pkg[pi].color == 2) { return 0; };
if (g.pkg[pi].color == 2) {
if (g.pkg[pi].failed) { return -1; };
return 0;
};
g.pkg[pi].color = 2;
let fv: expctx;
fv.out = -1;
@@ -1142,8 +1171,8 @@ fn seploadpkg(g: *sepgraph, pi: i32, searchpath: *u8) i32 = {
} else {
rc = sepscanfile(g, pi, g.pkg[pi].entry, searchpath, &fv, 0);
};
if (rc < 0) { return rc; };
if (pi == 0 && g.pkg[pi].path[0u64] == 0u8
if (rc < 0) { g.pkg[pi].failed = true; return rc; };
if (g.pkg[pi].root && g.pkg[pi].path[0u64] == 0u8
&& g.pkg[pi].name != nil) {
g.pkg[pi].path = arenadupcstr(g.pkg[pi].name,
cstrlen(g.pkg[pi].name));
@@ -1163,7 +1192,10 @@ fn seploadpkg(g: *sepgraph, pi: i32, searchpath: *u8) i32 = {
};
let k: i32 = 0;
for (k < g.pkg[pi].ndeps) {
if (seploadpkg(g, g.pkg[pi].deps[k], searchpath) < 0) { return -1; };
if (seploadpkg(g, g.pkg[pi].deps[k], searchpath) < 0) {
g.pkg[pi].failed = true;
return -1;
};
k += 1;
};
return 0;
@@ -1575,14 +1607,14 @@ fn copyfileatomic(src: *u8, dst: *u8) i32 = {
fn workdirstamptext(istest: i32, emitasm: i32) str = {
if (istest != 0) {
if (emitasm != 0) {
return "ww workdir fmt 3 mode test asm 1\n";
return "ww workdir fmt 4 mode test asm 1\n";
};
return "ww workdir fmt 3 mode test asm 0\n";
return "ww workdir fmt 4 mode test asm 0\n";
};
if (emitasm != 0) {
return "ww workdir fmt 3 mode build asm 1\n";
return "ww workdir fmt 4 mode build asm 1\n";
};
return "ww workdir fmt 3 mode build asm 0\n";
return "ww workdir fmt 4 mode build asm 0\n";
};
fn stampmatches(path: *u8, want: str) bool = {
@@ -1618,6 +1650,25 @@ fn writestampatomic(path: *u8, want: str) i32 = {
return os.rename(pathstr(tmpp), pathstr(path));
};
// A coordinator-private completion marker distinguishes a newly linked
// product from a caller-owned binary left by an earlier invocation.
fn recordproductstatus(path: *u8) i32 = {
if (path == nil) { return 0; };
let tmpp: *u8 = appendlit(path, ".new");
let fd: i32 = os.open(pathstr(tmpp),
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32);
if (fd < 0) { return -1; };
let body: str = "ok\n";
let bad: bool = false;
match (os.writeall(fd, body.ptr, body.len: u64)) {
case let n: i64 => { if (n != body.len: i64) { bad = true; }; };
case let e: os.oserror => { bad = true; };
};
if (os.close(fd) != 0) { bad = true; };
if (bad) { return -1; };
return os.rename(pathstr(tmpp), pathstr(path));
};
// cerrpath — the "ww: <head><path>\n" diagnostic shape shared by the
// workdir error sites; byte-identical wording to the cstage twin's
// fprintf(..., "%s", path) forms.
@@ -1630,9 +1681,18 @@ fn cerrpath(head: str, path: *u8, tail: str) void = {
fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
rootidentity: *u8, out: *u8,
objstem: *u8, incs: *u8, lf: *lflags, packageonly: i32, istest: i32,
rootvariant: i32, testpackage: *u8, emitasm: i32,
products: *sepproduct, nproducts: i32, emitasm: i32,
workdir: *u8, scratchout: **u8,
graphout: **sepgraph) i32 = {
if (nproducts < 1 || nproducts > SEP_MAXPRODUCT) { return 1; };
let statusi: i32 = 0;
for (statusi < nproducts) {
if (products[statusi].status != nil) {
let rr: i32 = os.remove(pathstr(products[statusi].status));
if (rr != 0 && rr != -2) { return 1; };
};
statusi += 1;
};
let c6: *u8 = joinpathlit(selfdir, "w6c_ww");
let a6: *u8 = joinpathlit(selfdir, "w6a_ww");
let l6: *u8 = joinpathlit(selfdir, "w6l_ww");
@@ -1776,9 +1836,14 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
if (graphout != nil) { *graphout = g; };
let rootpath: *u8 = "\0".ptr;
if (packageonly != 0 && rootidentity != nil) { rootpath = rootidentity; };
let root: i32 = sepfindoraddvariant(g, rootpath, src, entryisdir,
rootvariant, testpackage);
if (root < 0) { return 1; };
let producti: i32 = 0;
for (producti < nproducts) {
products[producti].root = sepfindoraddvariant(g, rootpath, src,
entryisdir, products[producti].variant,
products[producti].testpackage, true);
if (products[producti].root < 0) { return 1; };
producti += 1;
};
let testsupportmodule: str = "test";
// -T generates a dispatcher whose support qualifier is selected by the
// command. Represent that compiler-generated requirement as a direct root
@@ -1793,9 +1858,14 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
let rootissupport: bool = entryisdir != 0
&& os.samefile(pathstr(tp), pathstr(src));
let collision: bool = false;
if (!rootissupport && testpackage != nil) {
collision = cstreqlit(testpackage, "test")
|| cstreqlit(testpackage, "test_test");
producti = 0;
for (!rootissupport && producti < nproducts) {
let name: *u8 = products[producti].testpackage;
if (name != nil && (cstreqlit(name, "test")
|| cstreqlit(name, "test_test"))) {
collision = true;
};
producti += 1;
};
if (!rootissupport && !collision) {
let ud: i32 = 0;
@@ -1806,10 +1876,17 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
};
};
if (collision) { testsupportmodule = SEP_TEST_SUPPORT_MODULE; };
// A same-test build of the runtime package already owns run
// and its source imports. An external test still needs the
// colocated production node, also its one support dependency.
if (!rootissupport || rootvariant == SEP_VARIANT_EXTERNAL) {
producti = 0;
for (producti < nproducts) {
let root: i32 = products[producti].root;
// A same-test build of the runtime package already owns run
// and its source imports. An external test still needs the
// colocated production node, also its support dependency.
if (rootissupport
&& products[producti].variant != SEP_VARIANT_EXTERNAL) {
producti += 1;
continue;
};
let ti: i32 = sepfindoradd(g,
testsupportmodule.ptr, tp, td);
if (ti < 0) { return 1; };
@@ -1826,34 +1903,96 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
g.pkg[root].ndeps += 1;
};
};
producti += 1;
};
};
};
if (seploadpkg(g, root, searchpath.ptr) < 0) { return 1; };
if (rootvariant != SEP_VARIANT_PRODUCTION
&& (testpackage == nil || !cstreq(g.pkg[root].name, testpackage))) {
cerr("ww: package-test selector does not match loaded package\n");
return 1;
producti = 0;
for (producti < nproducts) {
let root: i32 = products[producti].root;
if (seploadpkg(g, root, searchpath.ptr) < 0) {
g.pkg[root].failed = true;
producti += 1;
continue;
};
if (products[producti].variant != SEP_VARIANT_PRODUCTION
&& (products[producti].testpackage == nil
|| !cstreq(g.pkg[root].name,
products[producti].testpackage))) {
cerr("ww: package-test selector does not match loaded package\n");
g.pkg[root].failed = true;
};
producti += 1;
};
let rootpackage: bool = packageonly != 0;
if (rootpackage && cstreqlit(g.pkg[root].name, "main")) {
if (rootpackage && !g.pkg[products[0].root].failed
&& cstreqlit(g.pkg[products[0].root].name, "main")) {
cerr("ww: -p requires a non-main package\n");
return 1;
};
let ci: i32 = 0;
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
let order: []i32 = alloc([], g.n: u64)!;
order.len = g.n;
let stack: []i32 = alloc([], g.n: u64)!;
stack.len = g.n;
let norder: i32 = 0;
if (septopovisit(g, root, order, &norder, stack, 0) < 0) { return 1; };
if (!rootpackage) { g.pkg[root].path = "\0".ptr; };
// Diagnose cycles per product before constructing the shared union. A
// variant-local cycle must not suppress an independent sibling root.
producti = 0;
for (producti < nproducts) {
let root: i32 = products[producti].root;
if (!g.pkg[root].failed) {
ci = 0;
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
let ignored: i32 = 0;
if (septopovisit(g, root, order,
&ignored, stack, 0) < 0) {
g.pkg[root].failed = true;
};
};
producti += 1;
};
ci = 0;
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
producti = 0;
for (producti < nproducts) {
let root: i32 = products[producti].root;
if (!g.pkg[root].failed) {
if (septopovisit(g, root, order,
&norder, stack, 0) < 0) { return 1; };
};
producti += 1;
};
if (!rootpackage) {
producti = 0;
for (producti < nproducts) {
g.pkg[products[producti].root].path = "\0".ptr;
producti += 1;
};
};
let anyfailed: bool = false;
producti = 0;
for (producti < nproducts) {
if (g.pkg[products[producti].root].failed) { anyfailed = true; };
producti += 1;
};
let oi: i32 = 0;
for (oi < norder) {
let pi: i32 = order[oi];
let dk: i32 = 0;
for (dk < g.pkg[pi].ndeps) {
if (g.pkg[g.pkg[pi].deps[dk]].failed) {
g.pkg[pi].failed = true;
};
dk += 1;
};
if (g.pkg[pi].failed) {
anyfailed = true;
oi += 1;
continue;
};
let unitf: *u8 = sepfname(g, pi, scratch, ".unit.ww");
let wwi: *u8 = sepfname(g, pi, scratch, ".wwi");
let asmf: *u8 = sepfname(g, pi, scratch, ".s");
@@ -1875,10 +2014,13 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
cu = unitnew; cw = wwinew; cs = asmnew;
co = objnew; ca = anew;
};
let needsexport: bool = (pi != root) || rootpackage;
let needsarchive: bool = (pi != root) || rootpackage;
let needsexport: bool = !g.pkg[pi].root || rootpackage;
let needsarchive: bool = !g.pkg[pi].root || rootpackage;
if (sepcomposeunit(g, pi, scratch, searchpath.ptr, cu) < 0) {
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
oi += 1;
continue;
};
let fresh: bool = false;
if (warm) {
@@ -1905,7 +2047,8 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
if (fresh) {
if (os.remove(pathstr(unitnew)) != 0) {
cerrpath("ww: cannot remove ", unitnew, "\n");
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
};
oi += 1;
continue;
@@ -1918,9 +2061,9 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
// LOCAL type (the root is never imported), which the
// export-check rejects. Build a shorter root argv
// without the -I/wwi pair; root's `.wwi` is unconsumed.
// #79: the root carries -T under `ww test` so w6c
// #79: the root carries -T under `ww test` so w6c
// synthesizes the test main; deps never get -T.
let roott: bool = (pi == root) && (istest != 0);
let roott: bool = g.pkg[pi].root && (istest != 0);
let supportt: bool = g.pkg[pi].testsupport;
let alen: u64 = 8u64;
if (!needsexport) { alen = 6u64; if (roott) { alen = 9u64; }; };
@@ -1954,7 +2097,10 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
cerr("ww: execve failed\n");
};
cerr("ww: w6c failed\n");
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
oi += 1;
continue;
};
};
if (emitasm == 0) {
@@ -1973,7 +2119,10 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
cerr("ww: execve failed\n");
};
cerr("ww: w6a failed\n");
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
oi += 1;
continue;
};
};
// Wrap each DEP package's `.o` in its own deterministic `.a`
@@ -1984,7 +2133,10 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
if (emitasm == 0 && needsarchive) {
if (archiveo(co, ca) != 0) {
cerr("ww: archive failed\n");
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
oi += 1;
continue;
};
};
// Commit order: artifacts before the unit that vouches for
@@ -2027,7 +2179,10 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
} else {
cerr("ww: cannot commit (root)\n");
};
return 1;
g.pkg[pi].failed = true;
anyfailed = true;
oi += 1;
continue;
};
};
oi += 1;
@@ -2035,7 +2190,7 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
// Tool identity commits only after every package artifact it vouches
// for is itself committed; a killed pass leaves the old identity and
// forces a full recompile, never a false reuse.
if (warm) {
if (warm && !anyfailed) {
if (!fileequal(toolc, c6)) {
if (copyfileatomic(c6, toolc) != 0) {
cerrpath("ww: cannot record ", toolc, "\n");
@@ -2057,8 +2212,10 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
};
};
};
if (emitasm != 0) { return 0; };
if (emitasm != 0) { if (anyfailed) { return 1; }; return 0; };
if (rootpackage) {
let root: i32 = products[0].root;
if (g.pkg[root].failed) { return 1; };
let archive: *u8 = sepfname(g, root, scratch, ".a");
let iface: *u8 = sepfname(g, root, scratch, ".wwi");
let outiface: *u8 = appendlit(out, ".wwi");
@@ -2070,14 +2227,8 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
return 0;
};
// Reverse-topo link: root `.o` first (order[norder-1]), dependency `.a`
// files after, then libwwrt.a (which still selectively pulls only the
// runtime members a live undef needs). A same-test root already contains
// its production sources; if that production variant is also reached
// through the runtime closure, retain its dependencies but omit its
// duplicate archive.
// argv: 3 fixed (w6l,-o,out) + one root object/archive per package
// + 1 libwwrt + 2*nlibdirs + 2*nlibs + 1 nil.
// Each product gets its own reverse-topological link closure. Shared
// production actions do not turn variant-local archives into link inputs.
let nldirs: i32 = 0;
let nllibs: i32 = 0;
let ldirs: **u8 = nil;
@@ -2088,65 +2239,93 @@ fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32,
ldirs = lf.libdirs;
llibs = lf.libs;
};
let total: i32 = 3 + norder + 1 + 2 * nldirs + 2 * nllibs + 1;
let largv: []*u8 = alloc([], total: u64)!;
largv.len = total;
largv[0] = "w6l\0".ptr;
largv[1] = "-o\0".ptr;
largv[2] = out;
let pos: i32 = 3;
let li: i32 = norder - 1;
for (li >= 0) {
let pi: i32 = order[li];
if (g.pkg[root].variant == SEP_VARIANT_SAME_TEST
&& pi != root
&& g.pkg[pi].variant == SEP_VARIANT_PRODUCTION
&& os.samefile(pathstr(g.pkg[pi].entry),
pathstr(g.pkg[root].entry))) {
li -= 1;
producti = 0;
for (producti < nproducts) {
let root: i32 = products[producti].root;
if (g.pkg[root].failed) {
anyfailed = true;
producti += 1;
continue;
};
// root: positional `.o` (force-load); deps: `.a` (selective).
let suf: str = ".a";
if (pi == root) { suf = ".o"; };
largv[pos] = sepfname(g, pi, scratch, suf);
pos += 1;
li -= 1;
};
largv[pos] = libwwrt.ptr; pos += 1;
let k: i32 = 0;
for (k < nldirs) {
largv[pos] = "-L\0".ptr;
largv[pos + 1] = ldirs[k];
pos += 2;
k += 1;
};
k = 0;
for (k < nllibs) {
largv[pos] = "-l\0".ptr;
largv[pos + 1] = llibs[k];
pos += 2;
k += 1;
};
largv[pos] = nil;
let linkargs: []str = alloc([], pos: u64)!;
let ai: i32 = 0;
for (ai < pos) {
append(linkargs, pathstr(largv[ai]));
ai += 1;
};
let linkenv: []str = os.getenvs();
let linkresult: exec.result;
exec.runstdio(pathstr(l6), linkargs, linkenv, &linkresult);
if (linkresult.termination != exec.termination.EXIT
|| linkresult.code != 0) {
if (linkresult.termination == exec.termination.ERROR
&& linkresult.code == 127) {
cerr("ww: execve failed\n");
ci = 0;
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
let linkorder: []i32 = alloc([], g.n: u64)!;
linkorder.len = g.n;
let linkstack: []i32 = alloc([], g.n: u64)!;
linkstack.len = g.n;
let nlink: i32 = 0;
if (septopovisit(g, root, linkorder, &nlink,
linkstack, 0) < 0) { return 1; };
// argv: 3 fixed + closure + libwwrt + flags + nil.
let total: i32 = 3 + nlink + 1 + 2 * nldirs + 2 * nllibs + 1;
let largv: []*u8 = alloc([], total: u64)!;
largv.len = total;
largv[0] = "w6l\0".ptr;
largv[1] = "-o\0".ptr;
largv[2] = products[producti].out;
let pos: i32 = 3;
let li: i32 = nlink - 1;
for (li >= 0) {
let pi: i32 = linkorder[li];
if (g.pkg[root].variant == SEP_VARIANT_SAME_TEST
&& pi != root
&& g.pkg[pi].variant == SEP_VARIANT_PRODUCTION
&& os.samefile(pathstr(g.pkg[pi].entry),
pathstr(g.pkg[root].entry))) {
li -= 1;
continue;
};
let suf: str = ".a";
if (pi == root) { suf = ".o"; };
largv[pos] = sepfname(g, pi, scratch, suf);
pos += 1;
li -= 1;
};
cerr("ww: w6l failed\n");
return 1;
largv[pos] = libwwrt.ptr; pos += 1;
let k: i32 = 0;
for (k < nldirs) {
largv[pos] = "-L\0".ptr;
largv[pos + 1] = ldirs[k];
pos += 2;
k += 1;
};
k = 0;
for (k < nllibs) {
largv[pos] = "-l\0".ptr;
largv[pos + 1] = llibs[k];
pos += 2;
k += 1;
};
largv[pos] = nil;
let linkargs: []str = alloc([], pos: u64)!;
let ai: i32 = 0;
for (ai < pos) {
append(linkargs, pathstr(largv[ai]));
ai += 1;
};
let linkenv: []str = os.getenvs();
let linkresult: exec.result;
exec.runstdio(pathstr(l6), linkargs, linkenv, &linkresult);
if (linkresult.termination != exec.termination.EXIT
|| linkresult.code != 0) {
if (linkresult.termination == exec.termination.ERROR
&& linkresult.code == 127) {
cerr("ww: execve failed\n");
};
cerr("ww: w6l failed\n");
g.pkg[root].failed = true;
anyfailed = true;
producti += 1;
continue;
};
if (recordproductstatus(products[producti].status) != 0) {
cerr("ww: cannot record package-test product\n");
g.pkg[root].failed = true;
anyfailed = true;
};
producti += 1;
};
if (anyfailed) { return 1; };
return 0;
};
@@ -2162,9 +2341,15 @@ fn buildonesep(selfdir: *u8, src: *u8, entryisdir: i32,
keepscratch: i32, workdir: *u8) i32 = {
let scratch: *u8 = nil;
let g: *sepgraph = nil;
let product: sepproduct;
product.out = out;
product.testpackage = testpackage;
product.status = nil;
product.variant = rootvariant;
product.root = -1;
let r: i32 = buildonesepimpl(selfdir, src, entryisdir, rootidentity,
out, objstem,
incs, lf, packageonly, istest, rootvariant, testpackage,
incs, lf, packageonly, istest, &product, 1,
emitasm, workdir, &scratch, &g);
sepgraphfree(g);
if (keepscratch == 0 && scratch != nil) {
@@ -2191,6 +2376,22 @@ fn buildonesep(selfdir: *u8, src: *u8, entryisdir: i32,
return r;
};
// Build every selected test root for one directory inside one command-owned
// package universe. The first output owns the shared cold sepwork tree.
fn buildpackagetests(selfdir: *u8, src: *u8, incs: *u8, workdir: *u8,
products: *sepproduct, nproducts: i32) i32 = {
let scratch: *u8 = nil;
let g: *sepgraph = nil;
let lf: lflags;
lf.libdirs = nil; lf.nlibdirs = 0;
lf.libs = nil; lf.nlibs = 0;
let r: i32 = buildonesepimpl(selfdir, src, 1, nil,
products[0].out, products[0].out, incs, &lf, 0, 1,
products, nproducts, 0, workdir, &scratch, &g);
sepgraphfree(g);
return r;
};
fn cstrendswithlit(p: *u8, lit: str) bool = {
return strings.hassuffix(pathstr(p), lit);
};
@@ -2817,8 +3018,7 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
let emitasm: i32 = 0;
let outstem: *u8 = nil;
let workdir: *u8 = nil;
let packagetestkind: *u8 = nil;
let packagetestname: *u8 = nil;
let products: []sepproduct = alloc([], SEP_MAXPRODUCT: u64)!;
let packageopts: bool = false;
let afterdash: bool = false;
let i: i32 = start;
@@ -2830,24 +3030,50 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
packageopts = true; afterdash = true; i += 1; continue;
};
if (cstreqlit(p, "--ww-package-test")) {
if (i + 2 >= argc || packagetestkind != nil) {
cerr("ww test: --ww-package-test needs kind and package\n");
if (i + 4 >= argc || products.len >= SEP_MAXPRODUCT) {
cerr("ww test: --ww-package-test needs kind, package, output, and status\n");
return 2;
};
packagetestkind = argv[i + 1];
packagetestname = argv[i + 2];
let pn: u64 = cstrlen(packagetestname);
if ((!cstreqlit(packagetestkind, "same")
&& !cstreqlit(packagetestkind, "external"))
let kind: *u8 = argv[i + 1];
let name: *u8 = argv[i + 2];
let output: *u8 = argv[i + 3];
let status: *u8 = argv[i + 4];
let pn: u64 = cstrlen(name);
let variant: i32 = SEP_VARIANT_EXTERNAL;
if (cstreqlit(kind, "same")) {
variant = SEP_VARIANT_SAME_TEST;
};
if ((!cstreqlit(kind, "same")
&& !cstreqlit(kind, "external"))
|| pn == 0u64 || pn >= 256u64
|| (cstreqlit(packagetestkind, "external")
|| output[0u64] == 0u8 || status[0u64] == 0u8
|| (cstreqlit(kind, "external")
&& (pn <= 5u64
|| !cstrendswithlit(packagetestname,
|| !cstrendswithlit(name,
"_test")))) {
cerr("ww test: invalid --ww-package-test variant\n");
return 2;
};
i += 3;
let duplicate: bool = false;
let producti: i32 = 0;
for (producti < products.len) {
if (products[producti].variant == variant) {
duplicate = true;
};
producti += 1;
};
if (duplicate) {
cerr("ww test: duplicate --ww-package-test variant\n");
return 2;
};
let product: sepproduct;
product.out = output;
product.testpackage = name;
product.status = status;
product.variant = variant;
product.root = -1;
append(products, product);
i += 5;
continue;
};
if (p[1u64] == 73u8) { // '-I'
@@ -2929,10 +3155,25 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
cerr("ww test: -S needs -o\n");
return 2;
};
if (packagetestkind != nil && packageopts) {
let producti: i32 = 1;
for (producti < products.len) {
let product: sepproduct = products[producti];
let j: i32 = producti;
for (j > 0 && products[j - 1].variant > product.variant) {
products[j] = products[j - 1];
j -= 1;
};
products[j] = product;
producti += 1;
};
if (products.len != 0 && packageopts) {
cerr("ww test: package-test variant rejects package options\n");
return 2;
};
if (products.len != 0 && outstem != nil) {
cerr("ww test: package-test products reject -o\n");
return 2;
};
// Go's ./... form: a trailing "..." element is a package-tree
// request for the coordinator, never a literal path — recognized
@@ -2946,7 +3187,7 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
&& target[tlen - 1u64] == '.';
};
if (istree) {
if (packagetestkind != nil) {
if (products.len != 0) {
cerr("ww test: package-test variant needs one directory\n");
return 2;
};
@@ -2965,7 +3206,7 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
return 2;
};
// -w forwards: the coordinator keys one persistent driver
// workdir per package group under the given root.
// workdir per directory plan under the given root.
return execpackagetests(selfdir, argv, argc, start,
targetindex, nil, false);
};
@@ -2991,7 +3232,7 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
};
};
if (isdir == 0) {
if (packagetestkind != nil) {
if (products.len != 0) {
cerr("ww test: package-test variant needs one directory\n");
return 2;
};
@@ -3014,21 +3255,13 @@ fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
cerr("ww test: pattern needs a single test file\n");
return 2;
};
if (packagetestkind != nil) {
if (compileonly == 0 || outstem == nil) {
cerr("ww test: package-test variant needs -c -o\n");
if (products.len != 0) {
if (compileonly == 0) {
cerr("ww test: package-test products need -c\n");
return 2;
};
let variant: i32 = SEP_VARIANT_EXTERNAL;
if (cstreqlit(packagetestkind, "same")) {
variant = SEP_VARIANT_SAME_TEST;
};
let lf: lflags;
lf.libdirs = nil; lf.nlibdirs = 0;
lf.libs = nil; lf.nlibs = 0;
return buildonesep(selfdir, resolved, 1, nil, outstem, outstem,
incs.ptr, &lf, 0i32, 1i32, variant, packagetestname,
0i32, 1i32, workdir);
return buildpackagetests(selfdir, resolved, incs.ptr, workdir,
products.ptr, products.len);
};
let replacement: *u8 = nil;
if (resolved != target) { replacement = resolved; };