Files
ww/test/wcc/992_w6l_ww.c
Hojun-Cho eb6083e54a test: retarget Pattern-B gates to sep .sepwork layout; 915 off private global (M4 E3, #93)
The driver flip moves build artifacts from next-to-source <stem>.s to a
.sepwork/ scratch dir. 29 Pattern-B gates now build `ww build --sep -o <stem>`
and read <stem>.sepwork/__root.s (multi-package gates concat all
<stem>.sepwork/*.s, since cross-package labels live in per-package .s).
All intermediates redirect to /tmp (WW_PKGCACHE + -o), so the corpus runs
parallel-safe with no source-tree pollution. Tests pass now (--sep is live)
and survive the flip.

915 additionally retargeted off strconv's PRIVATE left_shift_table (a let,
not export) — which separate compilation correctly hides — onto a test-local
package that exports its own probe table (#96). The combined path only linked
it via a single-unit private leak; encapsulation is now honored under sep.

Test-only; all 5 *_ww binaries HOLD. 989_m1mangle_run deferred (blocked by
#99, imported-package fn main mangling under sep).
2026-06-18 11:16:56 +09:00

150 lines
4.6 KiB
C

/*
* 992_w6l_ww — phase-10 marker for the ww-side w6l port.
*
* Diffs the C-built `w6l` against the ww-built `w6l_ww` on a corpus of
* link inputs. Both must produce byte-identical static ELF binaries.
* The corpus mixes:
* - a single .o input (no archive resolution)
* - a .o + libwwrt.a link, exercising the SysV `ar` two-pass loader
*
* Any divergence is a port bug in selfhost/cmd/w6l/.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return 1;
}
static const char *
absbin(void)
{
const char *b = getenv("BIN");
if (!b) b = "out/bin";
if (b[0] == '/') return b;
static char buf[2048];
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
return buf;
}
static int
slurp(const char *path, char **outbuf, size_t *outlen)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n < 0) { fclose(f); return -1; }
char *b = malloc((size_t)n + 1);
if (!b) { fclose(f); return -1; }
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
b[n] = '\0';
fclose(f);
*outbuf = b;
*outlen = (size_t)n;
return 0;
}
/* #93 sep layout: the flip stops emitting a monolithic <tool>/main.o;
* the linkable unit is now the per-package .o + reverse-topo .a set the
* sep driver assembles (a raw `w6l <root>.o *.a libwwrt.a` from the test
* side fails — `undefined reference` — because it can't reproduce the
* driver's topo order). So isolate the LINKER by driving the sep build's
* own link step twice over identical .o inputs (a shared pkgcache makes
* the second build's package objects a cache hit → only the final link
* re-runs): once with the default C w6l, once with WW_W6L=w6l_ww. Diff
* the two real tool binaries. Exercises the full archive two-pass loader
* + reverse-topo .a resolution — a stronger link than the old single
* main.o. Flip-invariant: --sep is live on trunk, default post-flip. */
static int
build_and_diff(const char *bin, const char *cwd, const char *tool,
const char *cache)
{
char cstem[256], wstem[256], cmd[4096];
snprintf(cstem, sizeof cstem, "/tmp/wwl_%d_c", getpid());
snprintf(wstem, sizeof wstem, "/tmp/wwl_%d_w", getpid());
snprintf(cmd, sizeof cmd,
"WW_PKGCACHE=%s timeout 300 %s/ww build --sep -o %s "
"%s/selfhost/cmd/%s >/dev/null 2>&1", cache, bin, cstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: C-link sep-build of %s\n", tool);
return -1;
}
snprintf(cmd, sizeof cmd,
"WW_W6L=%s/w6l_ww WW_PKGCACHE=%s timeout 300 %s/ww build --sep "
"-o %s %s/selfhost/cmd/%s >/dev/null 2>&1",
bin, cache, bin, wstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: ww-link sep-build of %s\n", tool);
snprintf(cmd, sizeof cmd, "rm -rf %s %s.sepwork", cstem, cstem);
if (system(cmd)) {}
return -1;
}
char *bc = NULL, *bw = NULL;
size_t nc = 0, nw = 0;
int rc = 0;
if (slurp(cstem, &bc, &nc) < 0 || slurp(wstem, &bw, &nw) < 0) {
fprintf(stderr, "w6l_ww FAIL: cannot read output for %s\n", tool);
rc = -1;
} else if (nc != nw || memcmp(bc, bw, nc) != 0) {
fprintf(stderr, "w6l_ww FAIL: %s — C %zu vs ww %zu bytes\n",
tool, nc, nw);
rc = -1;
}
free(bc); free(bw);
snprintf(cmd, sizeof cmd, "rm -rf %s %s %s.sepwork %s.sepwork",
cstem, wstem, cstem, wstem);
if (system(cmd)) {}
return rc;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* Real bootstrap-style links: each selfhost tool's full sep build
* (root .o + reverse-topo dep .a set + libwwrt.a). One shared
* pkgcache across all builds so each tool's second (w6l_ww) build
* re-links only. (wwdump is excluded — it imports the compiler-
* internal cmd packages syntax/check/cgen, which don't resolve
* under the lib-path sep dep scan; w6a/w6l are self-contained
* lib-only tools.) */
const char *tools[] = { "w6a", "w6l", NULL };
char cache[256];
snprintf(cache, sizeof cache, "/tmp/wwl_%d_pkgc", getpid());
int fail = 0;
int n = 0;
for (int i = 0; tools[i]; i++) {
if (build_and_diff(bin, cwd, tools[i], cache) != 0) fail++;
n++;
}
char rmc[320];
snprintf(rmc, sizeof rmc, "rm -rf %s", cache);
if (system(rmc)) {}
if (fail) {
fprintf(stderr, "w6l_ww: %d/%d diff(s) failed\n", fail, n);
return 1;
}
printf("w6l_ww: byte-identical to C w6l on %d selfhost tool links "
"(sep root .o + reverse-topo .a set + libwwrt.a)\n", n);
return 0;
}