Files
ww/test/wcc/995_self_rebuild.c
Hojun-Cho a651883c14 lib/strings+test: Hare port (dup/concat/trim/index/contains/has{pre,suf}fix/compare/utf8)
Hare-faithful index/predicate family per ref/hare/strings/{dup,
concat,trim,index,suffix,contains,compare,utf8}.ha. Non-variadic
subset (concat 2-arg, trim single-rune, contains single-needle)
pending task #16 — cstage variadic-pack drops .len on multi-field
element types; ship the Hare-faithful single-arg shape now, file
the variadic upgrade as follow-up. `sub` follow-up filed as #29
(commit 2 with iterator + utf8.chars relocation).

Surface: dup, concat, trim/trimprefix/trimsuffix (single rune),
hasprefix, hassuffix (both with (str|rune) sum needle),
byteindex, rbyteindex (both with (str|rune) sum needle),
contains (single str needle), compare, toutf8, fromutf8_unsafe,
runebytes helper. (str|rune) match arms route the rune via
utf8.encoderune into a [4]u8 scratch then bytes.index/rindex —
drew-devault's directive for clean Hare-fidelity over invented
ASCII-only rune-byte arms.

byteindex / rbyteindex rune-arm semantic correction —
corpus-coverage-blind unmask. Pre-existing impl scanned for
`r: u8` (broken for all rune values >0x7F since strings.ww first
landed; no caller exercised it). Replaced with utf8.encoderune-
based scan via runebytes helper. Severity-marker: silent
wrong-result for any non-ASCII rune needle, masked by zero
in-tree callers until lib/strings + utf8 chain pulled the shape
in.

Build-system propagation: lib/strings depends transitively on
lib/encoding/utf8 (via byteindex's rune arm). cmd/ww driver's
locate_import_in (cmd/ww/main.c:85) walks `<dir>/<name>.ww` and
`<dir>/<name>/<name>.ww` only — `use utf8;` doesn't find
lib/encoding/utf8/utf8.ww without explicit `-I lib/encoding/utf8`.
Propagated through 5 wwstage-tool Makefile targets + 7 test
wrappers + test/wcc/995_self_rebuild.c sprintf lines. Task #17
filed for the principled resolver fix (subdir walk vs Hare's
qualified `use encoding::utf8;` notation).

This commit chain (#15 strings) surfaced 7 cgen bugs during
landing: #16 cstage variadic-pack, #17 resolver nested-paths,
#27 aliaslookup leaf-collision, #22 zero-init !void/void-alias
let-decl, #15-cstage retscr SSoT name, #24 composite CALL return
as composite arg, #28 N_DOT calleeparams. All blocking ones
fixed (#16/#17 deferred-with-stopgap, others fixed in their
respective commits). Pre-flight + stop-and-surface discipline
held throughout — no workarounds shipped in stdlib.

Tests:
  - 966_strings_run drives lib/strings/stringstest.ww via ww run.
    15 @test fns: dup (alloc, multibyte), concat (empty, lopsided,
    multibyte), trim/ltrim/rtrim incl. 4-byte rune U+1D68A,
    hasprefix/hassuffix with (str|rune) incl. multibyte,
    byteindex/rbyteindex both arms 1/2/3/4-byte rune coverage,
    compare. Cited from ref/hare/strings/+test.ha where vectors
    apply.

100/100 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
2026-05-18 10:28:44 +09:00

151 lines
4.5 KiB
C

/*
* 995_self_rebuild — the wwstage rebuilds itself.
*
* Drives ww_ww (the ww-side driver, which shells to w6c_ww/w6a_ww/w6l_ww)
* over each wwstage tool's source and diffs the result byte-for-byte
* against the cstage-built binary in $BIN. A green run means the
* toolchain can recompile itself without touching cc, modulo the
* cold-start binary — which is the v1.0 lock from PLAN.md.
*
* 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.
*/
#include <stdio.h>
#include <stdlib.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_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) {
if (fa) fclose(fa);
if (fb) fclose(fb);
return -1;
}
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
/* Each tool builds via `ww_ww build -I <local> -I lib/ww -I selfhost/cmd/wcc src`.
* lib/ww holds the language introspection (lex/tok/ast/parse/typ/sym);
* selfhost/cmd/wcc holds the compiler internals (mem/check/cgen*).
* lib/encoding/utf8 carries the rune codec strings.byteindex needs;
* the import resolver doesn't yet walk encoding/ subdirs (task #17),
* so the dep travels as an explicit -I until it does.
* 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)
{
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;
if (inc_local && inc_local[0]) {
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww_ww build -I %s/%s -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/lib/encoding/utf8 -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, inc_local, cwd, cwd, cwd, cwd, cwd, cwd, src_rel);
} else {
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww_ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/lib/encoding/utf8 -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, 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;
}
char rebuilt[256], canonical[256];
snprintf(rebuilt, sizeof rebuilt, "%s/main", workdir);
snprintf(canonical, sizeof canonical, "%s/%s_ww", bin, tool);
int rc = slurp_eq(rebuilt, canonical);
if (rc != 0) {
fprintf(stderr, "self-rebuild FAIL: %s rebuilt != cstage %s\n",
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);
return rc;
}
int
main(void)
{
const char *bin = absbin();
if (!bin) return 1;
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 },
};
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++;
}
if (fail) {
fprintf(stderr, "self-rebuild: %d/%d tool(s) diverged\n", fail, n);
return 1;
}
printf("self-rebuild: %d wwstage tool(s) round-trip byte-identical "
"through ww_ww + w6c_ww + w6a_ww + w6l_ww\n", n);
return 0;
}