diff --git a/test/wcc/995_self_rebuild.c b/test/wcc/995_self_rebuild.c index e59006ee..592f5ab6 100644 --- a/test/wcc/995_self_rebuild.c +++ b/test/wcc/995_self_rebuild.c @@ -10,6 +10,10 @@ * This is stricter than `make bootstrap`: that loop only proves the * wwdump cgen self-stabilises; this proves every wwstage tool round- * trips through the wwstage pipeline. + * + * The five tool builds run concurrently: fork() per tool, parent + * collects each pid with waitpid() and then byte-compares. Wall drops + * from sum(builds) to max(builds). */ #include #include @@ -67,47 +71,93 @@ slurp_eq(const char *a, const char *b) * Some tools have a local module dir (w6a, w6l with sibling .ww files). * inc_local is "" for tools without one (w6c, ww, wwdump). */ -static int -rebuild_one(const char *bin, const char *cwd, const char *tool, - const char *src_rel, const char *inc_local) -{ +struct buildjob { + const char *tool; + const char *src_rel; + const char *inc_local; char workdir[64]; - snprintf(workdir, sizeof workdir, "/tmp/wwsr_%d_%s", getpid(), tool); - char cmd[4096]; - snprintf(cmd, sizeof cmd, "rm -rf %s && mkdir -p %s", workdir, workdir); - if (runwait(cmd) != 0) return -1; + pid_t pid; +}; - if (inc_local && inc_local[0]) { +/* Fork a child that runs the `ww_ww build` for this tool. The child + * inherits no concurrent siblings — system() spawns a fresh /bin/sh -c. + * stderr lands in /build.err so concurrent builds don't merge + * their diagnostics. */ +static int +spawn_build(const char *bin, const char *cwd, struct buildjob *j) +{ + snprintf(j->workdir, sizeof j->workdir, "/tmp/wwsr_%d_%s", + getpid(), j->tool); + char setup[256]; + snprintf(setup, sizeof setup, "rm -rf %s && mkdir -p %s", + j->workdir, j->workdir); + if (runwait(setup) != 0) return -1; + + char cmd[4096]; + if (j->inc_local && j->inc_local[0]) { snprintf(cmd, sizeof cmd, - "cd %s && timeout 180 %s/ww_ww build -I %s/%s -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc " - "%s/%s >/dev/null 2>&1", - workdir, bin, cwd, inc_local, cwd, cwd, cwd, cwd, cwd, src_rel); + "cd %s && timeout 180 %s/ww_ww build -I %s/%s " + "-I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse " + "-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err", + j->workdir, bin, cwd, j->inc_local, + cwd, cwd, cwd, cwd, cwd, j->src_rel, j->workdir); } else { snprintf(cmd, sizeof cmd, - "cd %s && timeout 180 %s/ww_ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc " - "%s/%s >/dev/null 2>&1", - workdir, bin, cwd, cwd, cwd, cwd, cwd, src_rel); - } - if (runwait(cmd) != 0) { - fprintf(stderr, "self-rebuild FAIL: ww_ww build errored on %s\n", tool); - return -1; + "cd %s && timeout 180 %s/ww_ww build " + "-I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse " + "-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err", + j->workdir, bin, cwd, cwd, cwd, cwd, cwd, j->src_rel, j->workdir); } - char rebuilt[256], canonical[256]; - snprintf(rebuilt, sizeof rebuilt, "%s/main", workdir); - snprintf(canonical, sizeof canonical, "%s/%s_ww", bin, tool); + pid_t p = fork(); + if (p < 0) return -1; + if (p == 0) { + int rc = system(cmd); + if (rc == -1) _exit(1); + _exit(WIFEXITED(rc) ? WEXITSTATUS(rc) : 1); + } + j->pid = p; + return 0; +} - int rc = slurp_eq(rebuilt, canonical); - if (rc != 0) { - fprintf(stderr, "self-rebuild FAIL: %s rebuilt != cstage %s\n", - tool, canonical); +/* Reap one job, byte-diff rebuilt vs canonical, replay captured stderr + * on build failure, rm workdir. Returns 0 on byte-match, -1 otherwise. */ +static int +collect_build(const char *bin, struct buildjob *j) +{ + int rc = 0; + int status; + if (waitpid(j->pid, &status, 0) < 0) { + fprintf(stderr, "self-rebuild FAIL: waitpid %s\n", j->tool); + rc = -1; + } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { + fprintf(stderr, "self-rebuild FAIL: ww_ww build errored on %s\n", + j->tool); + /* Replay the captured stderr so the failure is diagnosable. */ + char errpath[256]; + snprintf(errpath, sizeof errpath, "%s/build.err", j->workdir); + FILE *f = fopen(errpath, "r"); + if (f) { + char buf[1024]; + while (fgets(buf, sizeof buf, f)) + fputs(buf, stderr); + fclose(f); + } + rc = -1; + } else { + char rebuilt[256], canonical[256]; + snprintf(rebuilt, sizeof rebuilt, "%s/main", j->workdir); + snprintf(canonical, sizeof canonical, "%s/%s_ww", bin, j->tool); + rc = slurp_eq(rebuilt, canonical); + if (rc != 0) { + fprintf(stderr, "self-rebuild FAIL: %s rebuilt != cstage %s\n", + j->tool, canonical); + } } - /* Leave the driver's intermediates (.s/.o/.combined.ww) next to the - * source — 991/992/994 read those fixtures, and `make wwstage` had - * already produced byte-identical copies anyway. */ - snprintf(cmd, sizeof cmd, "rm -rf %s", workdir); - runwait(cmd); + char cleanup[128]; + snprintf(cleanup, sizeof cleanup, "rm -rf %s", j->workdir); + runwait(cleanup); return rc; } @@ -119,26 +169,32 @@ main(void) char cwd[1024]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; - struct { - const char *tool; - const char *src; - const char *inc_local; - } tools[] = { - { "w6c", "selfhost/cmd/w6c/main.ww", "" }, - { "w6a", "selfhost/cmd/w6a/main.ww", "selfhost/cmd/w6a" }, - { "w6l", "selfhost/cmd/w6l/main.ww", "selfhost/cmd/w6l" }, - { "ww", "selfhost/cmd/ww/main.ww", "" }, - { "wwdump", "selfhost/cmd/wwdump/main.ww", "" }, - { NULL, NULL, NULL }, + struct buildjob jobs[] = { + { "w6c", "selfhost/cmd/w6c/main.ww", "", {0}, 0 }, + { "w6a", "selfhost/cmd/w6a/main.ww", "selfhost/cmd/w6a", {0}, 0 }, + { "w6l", "selfhost/cmd/w6l/main.ww", "selfhost/cmd/w6l", {0}, 0 }, + { "ww", "selfhost/cmd/ww/main.ww", "", {0}, 0 }, + { "wwdump", "selfhost/cmd/wwdump/main.ww", "", {0}, 0 }, }; + const int n = (int)(sizeof jobs / sizeof jobs[0]); - int fail = 0, n = 0; - for (int i = 0; tools[i].tool; i++) { - if (rebuild_one(bin, cwd, tools[i].tool, tools[i].src, - tools[i].inc_local) != 0) - fail++; - n++; + int spawned = 0; + for (int i = 0; i < n; i++) { + if (spawn_build(bin, cwd, &jobs[i]) != 0) { + fprintf(stderr, "self-rebuild FAIL: spawn %s\n", jobs[i].tool); + jobs[i].pid = 0; + } else { + spawned++; + } } + + int fail = n - spawned; + for (int i = 0; i < n; i++) { + if (jobs[i].pid == 0) continue; + if (collect_build(bin, &jobs[i]) != 0) + fail++; + } + if (fail) { fprintf(stderr, "self-rebuild: %d/%d tool(s) diverged\n", fail, n); return 1;