build_one_sep (per-package compile + .wwi interfaces + link) becomes the sole build path. do_build/do_run/do_test and the ww twins all route through it; --sep is now an accepted no-op and the run-rejects-sep guard is removed. Deleted the single-file amalgamator, both stages: build_one, expand, expand_dir, peek_package (+ the wwstage twins + strictpkgmismatch). unit_has_package is retained -- the sep scan loop's inline-package check needs it. The sep-shared helpers (enumerate_dir_ww, locate_import*, import_path_form, ImportSet, and ww counterparts) stay; they back the surviving sep path. Restores missing-package enforcement under sep by construction: the sep scan loop loudly rejects an unresolvable import (cannot find package <name>) unless the package is defined inline in the same unit -- matching the deleted amalgamator and closing the silent-accept the flip would otherwise introduce. All 5 wwstage tools relink (each is built via the now-sep `ww build`); emitted asm is byte-identical to the combined build per bootstrap input, so the binary md5 delta is pure link layout, not codegen. selfhost/cmd/ww/main.combined.ww is now stale and unregenerable (its writer build_one is deleted); #90 deletes it next. Test retargets folded in (rule-11 carve-out, #61/#133 precedent): each asserts post-flip-only behavior, is un-pre-migratable unlike #93/#94/#103, and splitting reddens one side. Closes #97. - 989_slttypepref -> dir-package layout (xb imports xa so both same-leaf `invalid` types are in scope at xb.f); inline-multipackage was the amalgamator shape, deleted with the flip. - 989_sepbuild_run -> run --sep now genuinely runs (exit 7), not the old loud-reject (exit 2); + a private per-pid WW_PKGCACHE so the cs/ww per-package byte-id compare on the shared real lib pkgs (rt/time/os) no longer races concurrent siblings on the global out/.pkgcache (the flip made sep the sole path, so every test now contends that cache). - 737_direnum -> the deleted strictpkgmismatch "differs from" wording -> sep's "does not match import path" (shared substring, wwstage terser #68). - 989_lib_byteid -> corpus-completeness scan excludes generated .sepwork scratch (the old `! -name '*.combined.ww'` exclude didn't cover the new sep artifact).
2298 lines
68 KiB
Plaintext
2298 lines
68 KiB
Plaintext
// selfhost/cmd/ww/main.ww — port of cmd/ww/main.c.
|
|
//
|
|
// The user-facing driver. Plan 9 cc(1) / Hare hare(1) analogue:
|
|
//
|
|
// ww build foo.ww → w6c foo.ww > foo.s ; w6a foo.s > foo.o ;
|
|
// w6l -o foo foo.o libwwrt.a
|
|
// ww run foo.ww → build then exec
|
|
// ww version → print version
|
|
//
|
|
// Tool paths default to siblings of $0 so a fresh build runs out of
|
|
// out/bin/. Env-var overrides (WW_W6C / WW_W6A / WW_W6L / WW_LIB) are
|
|
// not yet supported in this port; the bootstrap doesn't need them.
|
|
|
|
package main;
|
|
|
|
import os;
|
|
import rt;
|
|
import strings;
|
|
|
|
// All path/string scratch buffers go on the runtime page allocator.
|
|
// One page is plenty for any path we build. PATH_MAX lives in lib/os
|
|
// (os.PATH_MAX: i32 = 4096) — the duplicate u64 def was dropped to close
|
|
// the cgen #127 mod-mangle attribution bug consumer per rule-7.
|
|
def CMD_MAX: u64 = 8192u64;
|
|
|
|
// cerr — bare stderr fragment writer for the driver's piecewise
|
|
// diagnostics. Tool-local (NOT a lib wrapper): messages are built from
|
|
// many fragments and we route through os.write to avoid libc stdio.
|
|
// .len replaces the error-prone hand-counted byte literals these sites
|
|
// carried. Lives here (the selfhost ww-driver build is a single main.ww;
|
|
// err.c's bare-message path is not ported into this tree).
|
|
fn cerr(m: str) void = {
|
|
os.write(2, m.ptr, m.len: u64);
|
|
};
|
|
|
|
// ---- C-string helpers --------------------------------------------------
|
|
|
|
fn cstrlen(p: *u8) u64 = {
|
|
let n: u64 = 0u64;
|
|
for (p[n] != 0u8) { n += 1u64; };
|
|
return n;
|
|
};
|
|
|
|
// pathstr — view a NUL-terminated *u8 as a str. Bridges the
|
|
// driver's argv-style *u8 paths to lib/os entrypoints (str
|
|
// post-task-#23).
|
|
fn pathstr(p: *u8) str = {
|
|
let r: str;
|
|
r.ptr = p;
|
|
r.len = cstrlen(p): i32;
|
|
return r;
|
|
};
|
|
|
|
fn cstreq(a: *u8, b: *u8) bool = {
|
|
return strings.compare(pathstr(a), pathstr(b)) == 0;
|
|
};
|
|
|
|
// cstreqlit — compare a NUL-terminated *u8 to a ww string literal.
|
|
fn cstreqlit(a: *u8, lit: str) bool = {
|
|
return strings.compare(pathstr(a), lit) == 0;
|
|
};
|
|
|
|
// memcpy
|
|
fn bytecpy(dst: *u8, src: *u8, n: u64) void = {
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
dst[i] = src[i];
|
|
i += 1u64;
|
|
};
|
|
};
|
|
|
|
// Copy a NUL-terminated *u8 into dst starting at off; return the new
|
|
// offset (without writing a NUL).
|
|
fn cstrinto(dst: *u8, off: u64, src: *u8) u64 = {
|
|
let i: u64 = 0u64;
|
|
for (src[i] != 0u8) {
|
|
dst[off + i] = src[i];
|
|
i += 1u64;
|
|
};
|
|
return off + i;
|
|
};
|
|
|
|
// Same, but for a ww `str` (no NUL on the source side; we copy len bytes).
|
|
fn strinto(dst: *u8, off: u64, src: str) u64 = {
|
|
let n: i32 = src.len;
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
let iu: u64 = i: u64;
|
|
dst[off + iu] = src[i];
|
|
i += 1;
|
|
};
|
|
let nu: u64 = n: u64;
|
|
return off + nu;
|
|
};
|
|
|
|
// Write a single byte, return new offset.
|
|
fn byteinto(dst: *u8, off: u64, c: u8) u64 = {
|
|
dst[off] = c;
|
|
return off + 1u64;
|
|
};
|
|
|
|
// NUL-terminate at off and return the same off (handy when passing the
|
|
// buffer to a syscall that expects a C-string).
|
|
fn cstrseal(dst: *u8, off: u64) void = {
|
|
dst[off] = 0u8;
|
|
};
|
|
|
|
// ---- Tool-path resolution ---------------------------------------------
|
|
|
|
// dirname-equivalent: copy argv[0] up to (but not including) the last
|
|
// '/' into dst, NUL-terminated. If no slash, write ".".
|
|
fn selfdirinto(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
|
let n: u64 = cstrlen(argv0);
|
|
let cut: u64 = 0u64;
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
if (argv0[i] == 47u8) { cut = i; }; // '/'
|
|
i += 1u64;
|
|
};
|
|
if (cut == 0u64) {
|
|
dst[0u64] = 46u8; // '.'
|
|
dst[1u64] = 0u8;
|
|
return;
|
|
};
|
|
if (cut + 1u64 >= dstsz) { cut = dstsz - 2u64; };
|
|
bytecpy(dst, argv0, cut);
|
|
dst[cut] = 0u8;
|
|
};
|
|
|
|
// Build "$dir/$name" (NUL-terminated) into a fresh page-sized buffer.
|
|
fn joinpath(dir: *u8, name: *u8) *u8 = {
|
|
let buf: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
buf.len = os.PATH_MAX;
|
|
let off: u64 = cstrinto(buf.ptr, 0u64, dir);
|
|
off = byteinto(buf.ptr, off, 47u8);
|
|
off = cstrinto(buf.ptr, off, name);
|
|
cstrseal(buf.ptr, off);
|
|
return buf.ptr;
|
|
};
|
|
|
|
// Same, but the second component is a ww `str` literal.
|
|
fn joinpathlit(dir: *u8, name: str) *u8 = {
|
|
let buf: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
buf.len = os.PATH_MAX;
|
|
let off: u64 = cstrinto(buf.ptr, 0u64, dir);
|
|
off = byteinto(buf.ptr, off, 47u8);
|
|
off = strinto(buf.ptr, off, name);
|
|
cstrseal(buf.ptr, off);
|
|
return buf.ptr;
|
|
};
|
|
|
|
// ---- Subprocess plumbing ----------------------------------------------
|
|
|
|
// procrun — fork, execve `path` with `argv` (NULL-terminated), wait.
|
|
// Returns the child's real exit code on clean exit, 1 on signal kill,
|
|
// -1 on fork/wait failure. Mirrors cmd/ww/main.c:do_run WEXITSTATUS:
|
|
// the build-step callers only test `!= 0`, so propagating the exact
|
|
// non-zero code leaves them unaffected while `dorun` reports the true
|
|
// program exit status (was collapsing every non-zero exit to 1; fix #16).
|
|
fn procrun(path: *u8, argv: **u8) i32 = {
|
|
let pid: i32 = os.fork();
|
|
if (pid < 0) {
|
|
cerr("ww: fork failed\n");
|
|
return -1;
|
|
};
|
|
if (pid == 0) {
|
|
os.execve(pathstr(path), argv, nil: **u8);
|
|
cerr("ww: execve failed\n");
|
|
os.exit(127);
|
|
};
|
|
let status: i32 = 0;
|
|
let r: i32 = os.wait4(pid, &status, 0i32, nil: *void);
|
|
if (r < 0) {
|
|
cerr("ww: wait4 failed\n");
|
|
return -1;
|
|
};
|
|
// Linux wait status: low byte = signal (0 if exited cleanly),
|
|
// next byte = exit code.
|
|
if ((status & 127i32) != 0) { return 1; };
|
|
let code: i32 = (status >> 8i32) & 255i32;
|
|
return code;
|
|
};
|
|
|
|
// ---- import resolution + visited-set -----------------------------------
|
|
//
|
|
// The separate-compilation producer scans each unit's top-of-file
|
|
// `import IDENT;` lines and resolves them via the colon-separated `dirs`
|
|
// search path. A per-scan visited set (linear; typical builds visit a
|
|
// handful of modules) breaks cycles. (E3-C1: the legacy single-file
|
|
// source concatenator was deleted — sep is the sole path. Task #87.)
|
|
|
|
type strnode = struct {
|
|
s: str,
|
|
snext: *strnode,
|
|
};
|
|
|
|
type expctx = struct {
|
|
out: i32, // fd we're writing the combined source to
|
|
dirs: *u8, // ":"-separated search path (NUL-terminated)
|
|
visit: *strnode,
|
|
};
|
|
|
|
fn visitseen(c: *expctx, path: str) bool = {
|
|
let n: *strnode = c.visit;
|
|
for (n != nil) {
|
|
if (n.s.len == path.len) {
|
|
let i: i32 = 0;
|
|
let eq: bool = true;
|
|
for (i < path.len) {
|
|
if (n.s[i] != path[i]) { eq = false; i = path.len; }
|
|
else { i += 1; };
|
|
};
|
|
if (eq) { return true; };
|
|
};
|
|
n = n.snext;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
fn visitadd(c: *expctx, path: str) void = {
|
|
let n: *strnode = alloc(strnode{s=path, snext=c.visit})!;
|
|
c.visit = n;
|
|
};
|
|
|
|
// Translate dots in an `import` name to slashes for path lookup.
|
|
// `encoding.utf8` → `encoding/utf8`. Mirrors Hare's hare(1)
|
|
// use-path → fs-path mapping
|
|
// (ref/hare/hare/module/srcs.ha:78 builds the same shape via
|
|
// path::push per ident part).
|
|
fn importpathform(name: *u8, namelen: u64) *u8 = {
|
|
let buf: []u8 = alloc([], namelen + 1u64)!;
|
|
let i: u64 = 0u64;
|
|
for (i < namelen) {
|
|
if (name[i] == 46u8) { buf[i] = 47u8; } // '.' -> '/'
|
|
else { buf[i] = name[i]; };
|
|
i += 1u64;
|
|
};
|
|
buf[namelen] = 0u8;
|
|
return buf.ptr;
|
|
};
|
|
|
|
// Try <dir>/<path>/ as a directory (wantdir != 0), else <dir>/<path>.ww
|
|
// as a file. Sets *isdir on hit. Symmetric with cstage locate_import_in
|
|
// for byte-id driver output (rule 10). The legacy <dir>/<name>/<name>.ww
|
|
// form was dropped in task #22 — directory-as-module enumeration
|
|
// replaces it, mirroring ref/hare/hare/module/srcs.ha (Hare has no
|
|
// `foo/foo.ha` fallback; a module IS the directory).
|
|
fn locatein(dir: *u8, dirlen: u64,
|
|
pathform: *u8, pflen: u64, isdir: *i32, wantdir: i32) *u8 = {
|
|
let buf: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
let off: u64 = 0u64;
|
|
let i: u64 = 0u64;
|
|
for (i < dirlen) { buf[off + i] = dir[i]; i += 1u64; };
|
|
off += dirlen;
|
|
buf[off] = 47u8; off += 1u64; // '/'
|
|
i = 0u64;
|
|
for (i < pflen) { buf[off + i] = pathform[i]; i += 1u64; };
|
|
off += pflen;
|
|
if (wantdir != 0i32) {
|
|
buf[off] = 0u8;
|
|
let fi: os.filestat;
|
|
let r: (void | os.oserror) = os.stat(&fi, pathstr(buf.ptr));
|
|
match (r) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32; // S_IFMT
|
|
if (t == os.mode.DIR: u32) {
|
|
*isdir = 1;
|
|
return buf.ptr;
|
|
};
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
return nil;
|
|
};
|
|
buf[off] = 46u8; off += 1u64; // '.'
|
|
buf[off] = 119u8; off += 1u64; // 'w'
|
|
buf[off] = 119u8; off += 1u64; // 'w'
|
|
buf[off] = 0u8;
|
|
if (os.access(pathstr(buf.ptr), 0i32) == 0) {
|
|
*isdir = 0;
|
|
return buf.ptr;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// Walk a colon-separated dirlist, return first hit or nil. Sets
|
|
// *isdir on hit.
|
|
//
|
|
// #98: "a module IS the directory" — a directory-package on ANY entry
|
|
// wins over a same-named sibling FILE on an EARLIER entry. The driver
|
|
// builds the searchpath srcd-first; a co-located `lib/<mod>/<mod>test.ww`
|
|
// entry makes srcd = lib/<mod>, so a self-named `import <mod>` would
|
|
// else file-hit the sibling lib/<mod>/<mod>.ww and (under --sep) fold
|
|
// inline under the wrong module-reset. Two passes — directories first,
|
|
// files only if no directory matches anywhere — let lib/<mod>/ resolve
|
|
// as the dir while a genuine leaf package with no directory (e.g.
|
|
// lib/encoding/hex imported bare as `hex`, reachable only via its file
|
|
// in srcd) still resolves in the file pass. Latent: a dir-package now
|
|
// beats an earlier-entry same-named sibling FILE — loud-failing, none
|
|
// in the corpus; tracked as #101.
|
|
fn locateimport(dirs: *u8, name: *u8, namelen: u64,
|
|
isdir: *i32) *u8 = {
|
|
let pathform: *u8 = importpathform(name, namelen);
|
|
let pflen: u64 = cstrlen(pathform);
|
|
let total: u64 = cstrlen(dirs);
|
|
let wantdir: i32 = 1i32;
|
|
for (wantdir >= 0i32) {
|
|
let p: u64 = 0u64;
|
|
for (p < total) {
|
|
let q: u64 = p;
|
|
for (q < total) {
|
|
if (dirs[q] == ':') { break; };
|
|
q += 1u64;
|
|
};
|
|
let seglen: u64 = q - p;
|
|
if (seglen > 0u64) {
|
|
let hit: *u8 = locatein(dirs + p, seglen,
|
|
pathform, pflen, isdir, wantdir);
|
|
if (hit != nil) { return hit; };
|
|
};
|
|
p = q + 1u64;
|
|
};
|
|
wantdir -= 1i32;
|
|
};
|
|
return nil;
|
|
};
|
|
|
|
// Filter for dir enumeration: keep `*.ww` minus `*test.ww` and the
|
|
// `*.combined.ww` driver-generated concat artifacts (the previous
|
|
// build leaves them in the source tree; they parse-error when
|
|
// re-included). Returns true to keep.
|
|
fn dirfilekeep(name: *u8, nlen: u64) bool = {
|
|
// nlen<=3 guard kept: a bare ".ww" (len 3) is rejected here but
|
|
// would pass strings.hassuffix(".ww"); preserves cstage parity.
|
|
if (nlen <= 3u64) { return false; };
|
|
let s: str;
|
|
s.ptr = name;
|
|
s.len = nlen: i32;
|
|
if (!strings.hassuffix(s, ".ww")) { return false; };
|
|
if (strings.hassuffix(s, "test.ww")) { return false; };
|
|
// ".combined.ww" — full 12-char match mirrors cstage
|
|
// cmd/ww/main.c enumerate_dir_ww strcmp (rule 10).
|
|
if (strings.hassuffix(s, ".combined.ww")) { return false; };
|
|
return true;
|
|
};
|
|
|
|
// Byte-wise memcmp returning < 0, 0, > 0. Rule-10 byte-id requires
|
|
// cstage and wwstage sort the same way; memcmp is the
|
|
// locale-independent total order (mirrors ref/hare/sort/cmp/cmp.ha
|
|
// strs).
|
|
fn bytecmp(a: *u8, alen: u64, b: *u8, blen: u64) i32 = {
|
|
let n: u64 = alen;
|
|
if (blen < n) { n = blen; };
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
let av: i32 = (a[i]): i32;
|
|
let bv: i32 = (b[i]): i32;
|
|
if (av < bv) { return -1; };
|
|
if (av > bv) { return 1; };
|
|
i += 1u64;
|
|
};
|
|
if (alen < blen) { return -1; };
|
|
if (alen > blen) { return 1; };
|
|
return 0;
|
|
};
|
|
|
|
// enumeratedir — list *.ww entries of `dirpath` (less *test.ww and
|
|
// *.combined.ww), byte-sort. Returns (names[], nnames) with each
|
|
// name a NUL-terminated heap copy.
|
|
fn enumeratedir(dirpath: *u8) (**u8, i32) = {
|
|
let fd: i32 = os.open(pathstr(dirpath), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return nil: **u8, 0; };
|
|
// #65: grow-dynamic (mirror cstage enumerate_dir_ww realloc-doubling,
|
|
// cmd/ww/main.c:209). The old fixed 256-name cap silently dropped every
|
|
// eligible file past it, diverging the combined.ww from cstage on a
|
|
// module dir with >256 sources.
|
|
let cap: i32 = 8;
|
|
let names: []*u8 = alloc([], cap: u64)!;
|
|
let nlens: []u64 = alloc([], cap: u64)!;
|
|
let n: i32 = 0;
|
|
let buf: []u8 = alloc([], 8192u64)!;
|
|
buf.len = 8192;
|
|
let r: i64 = os.getdents64(fd, buf.ptr, 8192u64);
|
|
for (r > 0i64) {
|
|
let off: u64 = 0u64;
|
|
let ru: u64 = r: u64;
|
|
for (off < ru) {
|
|
let blo: u64 = (buf[off + 16u64]): u64;
|
|
let bhi: u64 = (buf[off + 17u64]): u64;
|
|
let reclen: u64 = blo + (bhi * 256u64);
|
|
let nm: *u8 = buf.ptr + off + 19u64;
|
|
let nl: u64 = cstrlen(nm);
|
|
if (dirfilekeep(nm, nl)) {
|
|
if (n >= cap) {
|
|
let ncap: i32 = cap * 2;
|
|
let nn: []*u8 = alloc([], ncap: u64)!;
|
|
let nl2: []u64 = alloc([], ncap: u64)!;
|
|
let k: i32 = 0;
|
|
for (k < n) {
|
|
nn[k] = names[k];
|
|
nl2[k] = nlens[k];
|
|
k += 1;
|
|
};
|
|
names = nn;
|
|
nlens = nl2;
|
|
cap = ncap;
|
|
};
|
|
let cp: []u8 = alloc([], nl + 1u64)!;
|
|
let i: u64 = 0u64;
|
|
for (i < nl) { cp[i] = nm[i]; i += 1u64; };
|
|
cp[nl] = 0u8;
|
|
names[n] = cp.ptr;
|
|
nlens[n] = nl;
|
|
n += 1;
|
|
};
|
|
off += reclen;
|
|
};
|
|
r = os.getdents64(fd, buf.ptr, 8192u64);
|
|
};
|
|
os.close(fd);
|
|
|
|
// Insertion sort, byte-wise. n is small (≤16 in practice).
|
|
let i: i32 = 1;
|
|
for (i < n) {
|
|
let j: i32 = i;
|
|
for (j > 0) {
|
|
if (bytecmp(names[j - 1], nlens[j - 1],
|
|
names[j], nlens[j]) <= 0) { j = 0; }
|
|
else {
|
|
let t: *u8 = names[j];
|
|
names[j] = names[j - 1];
|
|
names[j - 1] = t;
|
|
let tl: u64 = nlens[j];
|
|
nlens[j] = nlens[j - 1];
|
|
nlens[j - 1] = tl;
|
|
j -= 1;
|
|
};
|
|
};
|
|
i += 1;
|
|
};
|
|
return names.ptr, n;
|
|
};
|
|
|
|
// ---- file slurp -------------------------------------------------------
|
|
|
|
fn slurp(pathcs: *u8) (*u8, u64) = {
|
|
let fd: i32 = os.open(pathstr(pathcs), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return nil, 0u64; };
|
|
let szr: (i64 | os.oserror) = os.filesize(fd);
|
|
let n: i64 = 0i64;
|
|
match (szr) {
|
|
case let v: i64 => n = v;
|
|
case let e: os.oserror => { os.close(fd); return nil, 0u64; };
|
|
};
|
|
let nu: u64 = n: u64;
|
|
let buf: []u8 = alloc([], nu + 1u64)!;
|
|
buf.len = (nu + 1u64): i32;
|
|
let rr: (i64 | os.oserror) = os.readall(fd, buf.ptr, nu);
|
|
os.close(fd);
|
|
let got: i64 = 0i64;
|
|
match (rr) {
|
|
case let v: i64 => got = v;
|
|
case let e: os.oserror => return nil, 0u64;
|
|
};
|
|
if (got != n) { return nil, 0u64; };
|
|
buf[nu] = 0u8;
|
|
return buf.ptr, nu;
|
|
};
|
|
|
|
fn isidentbyte(c: u8) bool = {
|
|
if (c >= 'a' && c <= 'z') { return true; };
|
|
if (c >= 'A' && c <= 'Z') { return true; };
|
|
if (c >= '0' && c <= '9') { return true; };
|
|
if (c == '_') { return true; };
|
|
if (c == '.') { return true; };
|
|
return false;
|
|
};
|
|
|
|
// Scan one `import IDENT;` line out of [start, end). Returns the start
|
|
// of the ident and its length, or (nil, 0) if no `import` here. The
|
|
// caller passes a slice of the source: src points at the line start.
|
|
fn scanuse(src: *u8, len: u64) (*u8, u64) = {
|
|
let i: u64 = 0u64;
|
|
// skip leading whitespace
|
|
for (i < len) {
|
|
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
|
|
i += 1u64;
|
|
};
|
|
// i+7>len guard kept: hasprefix("import") covers the 6 spell-out
|
|
// bytes, but the sep read at src[i+6] still needs i+6 < len.
|
|
if (i + 7u64 > len) { return nil, 0u64; };
|
|
let rest: str;
|
|
rest.ptr = src + i;
|
|
rest.len = (len - i): i32;
|
|
if (!strings.hasprefix(rest, "import")) { return nil, 0u64; };
|
|
let sep: u8 = src[i + 6u64];
|
|
if (sep != 32u8) { if (sep != 9u8) { return nil, 0u64; }; };
|
|
i += 7u64;
|
|
for (i < len) {
|
|
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
|
|
i += 1u64;
|
|
};
|
|
let idstart: u64 = i;
|
|
for (i < len) {
|
|
if (!isidentbyte(src[i])) { break; };
|
|
i += 1u64;
|
|
};
|
|
let idlen: u64 = i - idstart;
|
|
if (idlen == 0u64) { return nil, 0u64; };
|
|
return src + idstart, idlen;
|
|
};
|
|
|
|
// ---- Build pipeline ---------------------------------------------------
|
|
|
|
// Strip the trailing ".ww" off `src` (a NUL-terminated path) into
|
|
// `stem`, NUL-terminated. If there's no .ww, the stem is the whole
|
|
// path.
|
|
fn makestem(stem: *u8, src: *u8) void = {
|
|
let n: u64 = cstrlen(src);
|
|
let stop: u64 = n;
|
|
if (n >= 3u64) {
|
|
if (src[n - 3u64] == 46u8) { // '.'
|
|
if (src[n - 2u64] == 119u8) { // 'w'
|
|
if (src[n - 1u64] == 119u8) { // 'w'
|
|
stop = n - 3u64;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
let i: u64 = 0u64;
|
|
for (i < stop) { stem[i] = src[i]; i += 1u64; };
|
|
stem[stop] = 0u8;
|
|
};
|
|
|
|
// Append a literal suffix to `stem` (which already lives in a buffer).
|
|
fn appendlit(stem: *u8, suffix: str) *u8 = {
|
|
let buf: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
buf.len = os.PATH_MAX;
|
|
let off: u64 = cstrinto(buf.ptr, 0u64, stem);
|
|
off = strinto(buf.ptr, off, suffix);
|
|
cstrseal(buf.ptr, off);
|
|
return buf.ptr;
|
|
};
|
|
|
|
// linker flags (-L<dir>, -l<name>) grouped as one struct so buildone's
|
|
// param list stays readable. (The earlier note here claimed a 6-argument
|
|
// wwstage calling-convention cap; that is stale — emittuplerowrelocs in
|
|
// cgen.ww takes 7 params and self-compiles green, and buildone itself now
|
|
// takes 7.)
|
|
type lflags = struct {
|
|
libdirs: **u8,
|
|
nlibdirs: i32,
|
|
libs: **u8,
|
|
nlibs: i32,
|
|
};
|
|
|
|
// ---- ww build --sep — M3-tail separate-compilation driver ------------
|
|
//
|
|
// Port of cmd/ww/main.c build_one_sep (task #46/c3). The `--sep` path
|
|
// materializes each imported package's `.wwi` interface and compiles
|
|
// every package on its own (`w6c -c`), then flat-links the `.o` set.
|
|
// combined.ww stays the DEFAULT live path; --sep is additive.
|
|
//
|
|
// Each w6c pass is BOTH consumer (reads dep `.wwi` as import scope) AND
|
|
// producer (writes this package's `.wwi` via -I). Reverse-topo order
|
|
// guarantees a package's deps' `.wwi` exist before it compiles.
|
|
//
|
|
// The load-bearing rule (#56, rob-resolved): every dep is tagged by its
|
|
// FULL DOTTED import path on prepend (`//ww:module <path>`), so the
|
|
// definer's qualified symbol (#53) equals the consumer's qualified
|
|
// reference (#40) and the sep `.o`s link. The prepend is the TRANSITIVE
|
|
// closure of a package's deps (lead-ratified): a dep's interface can
|
|
// name a transitive dep's type, so the consuming unit needs the whole
|
|
// closure for resolution. The unit composition is byte-identical to the
|
|
// cstage driver (rule 10) so w6c/w6c_ww emit identical `.s`.
|
|
|
|
def SEP_MAXPKG: i32 = 256;
|
|
|
|
type seppkg = struct {
|
|
path: *u8, // dotted import path, NUL-term; root path[0]==0
|
|
entry: *u8, // resolved package dir (or file, file root), NUL-term
|
|
isdir: i32,
|
|
deps: []i32, // direct-dep indices into sepgraph.pkg
|
|
ndeps: i32,
|
|
color: i32, // tri-color DFS: 0 white, 1 gray, 2 black
|
|
};
|
|
|
|
type sepgraph = struct {
|
|
pkg: []seppkg, // alloc'd SEP_MAXPKG
|
|
n: i32,
|
|
};
|
|
|
|
// Find a package by dotted path, or add it. Returns index, -1 if full.
|
|
fn sepfindoradd(g: *sepgraph, path: *u8, entry: *u8, isdir: i32) i32 = {
|
|
let i: i32 = 0;
|
|
for (i < g.n) {
|
|
if (cstreq(g.pkg[i].path, path)) { return i; };
|
|
i += 1;
|
|
};
|
|
if (g.n >= SEP_MAXPKG) {
|
|
cerr("ww --sep: too many packages\n");
|
|
return -1;
|
|
};
|
|
let plen: u64 = cstrlen(path);
|
|
let elen: u64 = cstrlen(entry);
|
|
g.pkg[g.n].path = arenadupcstr(path, plen);
|
|
g.pkg[g.n].entry = arenadupcstr(entry, elen);
|
|
g.pkg[g.n].isdir = isdir;
|
|
let dslot: []i32 = alloc([], SEP_MAXPKG: u64)!;
|
|
dslot.len = SEP_MAXPKG;
|
|
g.pkg[g.n].deps = dslot;
|
|
g.pkg[g.n].ndeps = 0;
|
|
g.pkg[g.n].color = 0;
|
|
let r: i32 = g.n;
|
|
g.n += 1;
|
|
return r;
|
|
};
|
|
|
|
// Build "<scratch>/<base><suffix>" NUL-term; base = path, or "__root"
|
|
// for the empty root path.
|
|
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) {
|
|
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;
|
|
};
|
|
|
|
// unithaspackage — does the unit buffer declare `package <leaf>;` ANYWHERE?
|
|
// #16 ENFORCE-driver (rob A) cstage unit_has_package twin: distinguishes a
|
|
// genuinely-missing import from one satisfied by an INLINE package in the
|
|
// same single-file multi-package unit (`package aa; ... package main;
|
|
// import aa;`). Scans EVERY line (comment-skip) — not just the first
|
|
// package decl (peekpackage stops there). Decision byte-identical to
|
|
// cstage so the skip/fatal choice + driver output match (rule 10).
|
|
fn unithaspackage(buf: *u8, buflen: u64, leafp: *u8, leafn: u64) bool = {
|
|
let p: u64 = 0u64;
|
|
for (p < buflen) {
|
|
let q: u64 = p;
|
|
for (q < buflen) { if (buf[q] == 10u8) { break; }; q += 1u64; };
|
|
let s: u64 = p;
|
|
for (s < q) {
|
|
if (buf[s] != 32u8) { if (buf[s] != 9u8) { break; }; };
|
|
s += 1u64;
|
|
};
|
|
if (s < q) {
|
|
let line: str;
|
|
line.ptr = buf + s;
|
|
line.len = (q - s): i32;
|
|
if (strings.hasprefix(line, "//")) { p = q + 1u64; continue; };
|
|
if (s + 8u64 <= q) {
|
|
if (strings.hasprefix(line, "package")) {
|
|
let sep: u8 = buf[s + 7u64];
|
|
let oksep: bool = false;
|
|
if (sep == 32u8) { oksep = true; }
|
|
else { if (sep == 9u8) { oksep = true; }; };
|
|
if (oksep) {
|
|
let t: u64 = s + 8u64;
|
|
for (t < q) {
|
|
if (buf[t] != 32u8) { if (buf[t] != 9u8) { break; }; };
|
|
t += 1u64;
|
|
};
|
|
let m: u64 = 0u64;
|
|
let eq: bool = true;
|
|
for (m < leafn) {
|
|
if (t + m >= q) { eq = false; break; };
|
|
if (buf[t + m] != leafp[m]) { eq = false; break; };
|
|
m += 1u64;
|
|
};
|
|
if (eq) {
|
|
let after: u64 = t + leafn;
|
|
let term: bool = false;
|
|
if (after >= q) { term = true; }
|
|
else {
|
|
let c: u8 = buf[after];
|
|
if (c == 59u8) { term = true; } // ';'
|
|
else { if (c == 32u8) { term = true; }
|
|
else { if (c == 9u8) { term = true; }; }; };
|
|
};
|
|
if (term) { return true; };
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
p = q + 1u64;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// Scan one source file for top-level `import IDENT;`. A DIRECTORY import
|
|
// is a package boundary: add as a direct dep of pi. A FILE import is an
|
|
// intra-package split: fold its imports into pi. Mirrors cstage
|
|
// sep_scan_file (collects PATHS, not bytes).
|
|
fn sepscanfile(g: *sepgraph, pi: i32, file: *u8, searchpath: *u8,
|
|
fv: *expctx) i32 = {
|
|
let fview: str;
|
|
fview.ptr = file;
|
|
fview.len = cstrlen(file): i32;
|
|
let fdup: str = strings.dup(fview);
|
|
if (visitseen(fv, fdup)) { return 0; };
|
|
visitadd(fv, fdup);
|
|
let bufp: *u8;
|
|
let blen: u64;
|
|
bufp, blen = slurp(file);
|
|
if (bufp == nil) {
|
|
cerr("ww --sep: cannot read source\n");
|
|
return -1;
|
|
};
|
|
let i: u64 = 0u64;
|
|
for (i < blen) {
|
|
let j: u64 = i;
|
|
for (j < blen) {
|
|
if (bufp[j] == 10u8) { break; }; // '\n'
|
|
j += 1u64;
|
|
};
|
|
let idp: *u8;
|
|
let idn: u64;
|
|
idp, idn = scanuse(bufp + i, j - i);
|
|
if (idp != nil) {
|
|
let isdir: i32 = 0;
|
|
let ipath: *u8 = locateimport(searchpath, idp, idn, &isdir);
|
|
if (ipath != nil) {
|
|
if (isdir != 0) {
|
|
let nm: []u8 = alloc([], idn + 1u64)!;
|
|
let k: u64 = 0u64;
|
|
for (k < idn) { nm[k] = idp[k]; k += 1u64; };
|
|
nm[idn] = 0u8;
|
|
let di: i32 = sepfindoradd(g, nm.ptr, ipath, 1);
|
|
if (di < 0) { return -1; };
|
|
let seen: bool = false;
|
|
let m: i32 = 0;
|
|
for (m < g.pkg[pi].ndeps) {
|
|
if (g.pkg[pi].deps[m] == di) { seen = true; };
|
|
m += 1;
|
|
};
|
|
if (!seen) {
|
|
if (g.pkg[pi].ndeps >= SEP_MAXPKG) { return -1; };
|
|
g.pkg[pi].deps[g.pkg[pi].ndeps] = di;
|
|
g.pkg[pi].ndeps += 1;
|
|
};
|
|
} else {
|
|
if (sepscanfile(g, pi, ipath, searchpath, fv) < 0) {
|
|
return -1;
|
|
};
|
|
};
|
|
} else {
|
|
// #16 ENFORCE-driver (rob A): a locate-miss is legal
|
|
// when the package is defined INLINE in the same unit
|
|
// (single-file multi-package). leaf = last dotted
|
|
// component; inline `package <leaf>` -> skip (the
|
|
// checker binds it), else fatal. E3-C1: the combined
|
|
// path that owned the genuine-missing case is gone, so
|
|
// the sep producer enforces it here (INV-2). cstage
|
|
// twin; fatal text identical.
|
|
let lstart: u64 = 0u64;
|
|
let lk: u64 = 0u64;
|
|
for (lk < idn) {
|
|
if (idp[lk] == 46u8) { lstart = lk + 1u64; }; // '.'
|
|
lk += 1u64;
|
|
};
|
|
let leafp: *u8 = idp + lstart;
|
|
let leafn: u64 = idn - lstart;
|
|
if (!unithaspackage(bufp, blen, leafp, leafn)) {
|
|
cerr("ww: cannot find package ");
|
|
os.write(2, idp, idn);
|
|
cerr("\n");
|
|
os.exit(1);
|
|
};
|
|
};
|
|
};
|
|
i = j + 1u64;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// Discover pi's direct deps + recurse. Enumerate the package's own
|
|
// files (dir → *.ww less *test.ww; file → the file) and scan each.
|
|
// `color` doubles as a scanned-marker (2); reset to white before topo.
|
|
fn sepscanpkg(g: *sepgraph, pi: i32, searchpath: *u8) i32 = {
|
|
if (g.pkg[pi].color == 2) { return 0; };
|
|
g.pkg[pi].color = 2;
|
|
let fv: expctx;
|
|
fv.out = -1;
|
|
fv.dirs = searchpath;
|
|
fv.visit = nil;
|
|
let rc: i32 = 0;
|
|
if (g.pkg[pi].isdir != 0) {
|
|
let names: **u8;
|
|
let n: i32;
|
|
names, n = enumeratedir(g.pkg[pi].entry);
|
|
let dlen: u64 = cstrlen(g.pkg[pi].entry);
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
if (rc == 0) {
|
|
let nlen: u64 = cstrlen(names[i]);
|
|
let fp: []u8 = alloc([], dlen + 1u64 + nlen + 1u64)!;
|
|
let k: u64 = 0u64;
|
|
for (k < dlen) { fp[k] = g.pkg[pi].entry[k]; k += 1u64; };
|
|
fp[dlen] = 47u8; // '/'
|
|
k = 0u64;
|
|
for (k < nlen) { fp[dlen + 1u64 + k] = names[i][k]; k += 1u64; };
|
|
fp[dlen + 1u64 + nlen] = 0u8;
|
|
rc = sepscanfile(g, pi, fp.ptr, searchpath, &fv);
|
|
};
|
|
i += 1;
|
|
};
|
|
} else {
|
|
rc = sepscanfile(g, pi, g.pkg[pi].entry, searchpath, &fv);
|
|
};
|
|
if (rc < 0) { return rc; };
|
|
let k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
if (sepscanpkg(g, g.pkg[pi].deps[k], searchpath) < 0) { return -1; };
|
|
k += 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// Print one cycle-chain node: a package path, or "(root)" for the
|
|
// empty root path.
|
|
fn sepcyclenode(p: *u8) void = {
|
|
if (p[0] == 0u8) { cerr("(root)"); } else { cerr(pathstr(p)); };
|
|
};
|
|
|
|
// DFS post-order over the dep DAG → reverse-topo (deps before importer).
|
|
// Tri-color: a gray back-edge is a loud dep-cycle reject naming the chain
|
|
// (Hare deps.ha:243); stack[0..depth) is the live DFS path, so the cycle
|
|
// runs from pi's first occurrence on it to the top, closing on pi.
|
|
// Cite Hare gather (deps.ha:123).
|
|
fn septopovisit(g: *sepgraph, pi: i32, order: []i32, no: *i32,
|
|
stack: []i32, depth: i32) i32 = {
|
|
if (g.pkg[pi].color == 2) { return 0; };
|
|
if (g.pkg[pi].color == 1) {
|
|
let j: i32 = 0;
|
|
for (j < depth && stack[j] != pi) { j += 1; };
|
|
cerr("ww --sep: dependency cycle: ");
|
|
let s: i32 = j;
|
|
for (s < depth) {
|
|
sepcyclenode(g.pkg[stack[s]].path);
|
|
cerr(" -> ");
|
|
s += 1;
|
|
};
|
|
sepcyclenode(g.pkg[pi].path);
|
|
cerr("\n");
|
|
return -1;
|
|
};
|
|
g.pkg[pi].color = 1;
|
|
stack[depth] = pi;
|
|
let k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
if (septopovisit(g, g.pkg[pi].deps[k], order, no, stack, depth + 1) < 0) {
|
|
return -1;
|
|
};
|
|
k += 1;
|
|
};
|
|
g.pkg[pi].color = 2;
|
|
order[*no] = pi;
|
|
*no += 1;
|
|
return 0;
|
|
};
|
|
|
|
// Mark pi's transitive deps (excluding pi) in inset[].
|
|
fn sepmarkdeps(g: *sepgraph, pi: i32, inset: []u8) void = {
|
|
let k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
let di: i32 = g.pkg[pi].deps[k];
|
|
if (inset[di] == 0u8) {
|
|
inset[di] = 1u8;
|
|
sepmarkdeps(g, di, inset);
|
|
};
|
|
k += 1;
|
|
};
|
|
};
|
|
|
|
// Emit one of pi's own source files into the sep-unit under the
|
|
// //ww:module-reset primary boundary (so -c emits its decls, imported
|
|
// ==0). DIRECTORY imports are skipped (provided as `.wwi` ahead);
|
|
// FILE imports fold in (intra-package split).
|
|
fn sepemitbody(fd: i32, path: *u8, visit: *expctx, searchpath: *u8, modpath: *u8) void = {
|
|
let pview: str;
|
|
pview.ptr = path;
|
|
pview.len = cstrlen(path): i32;
|
|
let pdup: str = strings.dup(pview);
|
|
if (visitseen(visit, pdup)) { return; };
|
|
visitadd(visit, pdup);
|
|
let bufp: *u8;
|
|
let blen: u64;
|
|
bufp, blen = slurp(path);
|
|
if (bufp == nil) {
|
|
cerr("ww --sep: cannot read source\n");
|
|
return;
|
|
};
|
|
let i: u64 = 0u64;
|
|
for (i < blen) {
|
|
let j: u64 = i;
|
|
for (j < blen) {
|
|
if (bufp[j] == 10u8) { break; }; // '\n'
|
|
j += 1u64;
|
|
};
|
|
let idp: *u8;
|
|
let idn: u64;
|
|
idp, idn = scanuse(bufp + i, j - i);
|
|
if (idp != nil) {
|
|
let isdir: i32 = 0;
|
|
let ipath: *u8 = locateimport(searchpath, idp, idn, &isdir);
|
|
if (ipath != nil) {
|
|
if (isdir == 0) {
|
|
sepemitbody(fd, ipath, visit, searchpath, modpath);
|
|
};
|
|
};
|
|
};
|
|
i = j + 1u64;
|
|
};
|
|
// #57: tag the primary body by its full dotted import path so the
|
|
// definer mangles == the importer reference; a root build (path "")
|
|
// stays a bare reset (keeps bare main).
|
|
if (modpath != nil && modpath[0u64] != 0u8) {
|
|
let dm: str = "//ww:module-reset ";
|
|
os.writeall(fd, dm.ptr, dm.len: u64);
|
|
os.writeall(fd, modpath, cstrlen(modpath));
|
|
os.writeall(fd, "\n".ptr, 1u64);
|
|
} else {
|
|
let d: str = "//ww:module-reset\n";
|
|
os.writeall(fd, d.ptr, d.len: u64);
|
|
};
|
|
os.writeall(fd, bufp, blen);
|
|
os.writeall(fd, "\n".ptr, 1u64);
|
|
};
|
|
|
|
fn sepemitdirbody(fd: i32, dir: *u8, visit: *expctx, searchpath: *u8, modpath: *u8) void = {
|
|
let names: **u8;
|
|
let n: i32;
|
|
names, n = enumeratedir(dir);
|
|
let dlen: u64 = cstrlen(dir);
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
let nlen: u64 = cstrlen(names[i]);
|
|
let fp: []u8 = alloc([], dlen + 1u64 + nlen + 1u64)!;
|
|
let k: u64 = 0u64;
|
|
for (k < dlen) { fp[k] = dir[k]; k += 1u64; };
|
|
fp[dlen] = 47u8; // '/'
|
|
k = 0u64;
|
|
for (k < nlen) { fp[dlen + 1u64 + k] = names[i][k]; k += 1u64; };
|
|
fp[dlen + 1u64 + nlen] = 0u8;
|
|
sepemitbody(fd, fp.ptr, visit, searchpath, modpath);
|
|
i += 1;
|
|
};
|
|
};
|
|
|
|
// Compose pi's sep-unit at `unitf`: the transitive-closure `.wwi`s
|
|
// (reverse-topo order, each tagged by its dotted path), then pi's own
|
|
// body under //ww:module-reset.
|
|
fn sepcomposeunit(g: *sepgraph, pi: i32, scratch: *u8, order: []i32,
|
|
norder: i32, searchpath: *u8, unitf: *u8) i32 = {
|
|
let u: i32 = os.open(pathstr(unitf), os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (u < 0) {
|
|
cerr("ww --sep: cannot open unit\n");
|
|
return -1;
|
|
};
|
|
let inset: []u8 = alloc([], g.n: u64)!;
|
|
inset.len = g.n;
|
|
let z: i32 = 0;
|
|
for (z < g.n) { inset[z] = 0u8; z += 1; };
|
|
sepmarkdeps(g, pi, inset);
|
|
let oi: i32 = 0;
|
|
for (oi < norder) {
|
|
let dj: i32 = order[oi];
|
|
if (dj != pi) {
|
|
if (inset[dj] != 0u8) {
|
|
let wwi: *u8 = sepfname(g, dj, scratch, ".wwi");
|
|
let wb: *u8;
|
|
let wn: u64;
|
|
wb, wn = slurp(wwi);
|
|
if (wb == nil) {
|
|
cerr("ww --sep: missing wwi\n");
|
|
os.close(u);
|
|
return -1;
|
|
};
|
|
let dm: str = "//ww:module ";
|
|
os.writeall(u, dm.ptr, dm.len: u64);
|
|
os.writeall(u, g.pkg[dj].path, cstrlen(g.pkg[dj].path));
|
|
os.writeall(u, "\n".ptr, 1u64);
|
|
os.writeall(u, wb, wn);
|
|
os.writeall(u, "\n".ptr, 1u64);
|
|
};
|
|
};
|
|
oi += 1;
|
|
};
|
|
let bv: expctx;
|
|
bv.out = u;
|
|
bv.dirs = searchpath;
|
|
bv.visit = nil;
|
|
if (g.pkg[pi].isdir != 0) {
|
|
sepemitdirbody(u, g.pkg[pi].entry, &bv, searchpath, g.pkg[pi].path);
|
|
} else {
|
|
sepemitbody(u, g.pkg[pi].entry, &bv, searchpath, g.pkg[pi].path);
|
|
};
|
|
os.close(u);
|
|
return 0;
|
|
};
|
|
|
|
// archiveo — twin of cmd/ww/main.c archive_o. Writes a deterministic
|
|
// single-member SysV ar archive at `apath` wrapping the `.o` at
|
|
// `objpath`. No armap / long-name table: w6l reads each member's ELF
|
|
// .symtab directly and skips '/'-named members, so a package `.a` is
|
|
// just the global magic + one 60-byte member header + the `.o` bytes
|
|
// (newline-padded to even). Zeroed mtime/uid/gid + fixed mode + a fixed
|
|
// member name make the bytes a pure function of the `.o` content →
|
|
// cstage `.a` == wwstage `.a` (rule 10) and a stable md5 for the 5b
|
|
// cache key.
|
|
fn archiveo(objpath: *u8, apath: *u8) i32 = {
|
|
let objp: *u8;
|
|
let objn: u64;
|
|
objp, objn = slurp(objpath);
|
|
if (objp == nil) {
|
|
cerr("ww --sep: cannot read object for archive\n");
|
|
return -1;
|
|
};
|
|
let pad: u64 = 0u64;
|
|
if ((objn & 1u64) != 0u64) { pad = 1u64; };
|
|
// sizelint-ok: 8B ar(5) magic + 60B member header are FILE-FORMAT
|
|
// constants, not type sizes (CLAUDE.md rule 13 carve-out).
|
|
let total: u64 = 8u64 + 60u64 + objn + pad;
|
|
let outs: []u8 = alloc([], total)!;
|
|
let out: *u8 = outs.ptr;
|
|
|
|
// 60-byte member header at offset 8, ASCII space-filled, fields
|
|
// left-justified; the 8-byte global magic precedes it. strinto
|
|
// copies a str's bytes (the working i32-index idiom) — a direct
|
|
// `out[i] = lit[i: i32]` store trips the cgen's str-index-rvalue arm.
|
|
let h: u64 = 8u64;
|
|
let j: u64 = 0u64;
|
|
for (j < 60u64) { out[h + j] = 32u8; j += 1u64; }; // 0x20 fill
|
|
strinto(out, 0u64, "!<arch>\n"); // global magic
|
|
strinto(out, h, "pkg.o/"); // name (GNU '/' terminator)
|
|
out[h + 16u64] = 48u8; // mtime "0" (zeroed → determinism)
|
|
out[h + 28u64] = 48u8; // uid "0"
|
|
out[h + 34u64] = 48u8; // gid "0"
|
|
strinto(out, h + 40u64, "100644"); // mode (fixed octal)
|
|
// size: decimal byte-count of the .o, left-justified at [48..58)
|
|
if (objn == 0u64) {
|
|
out[h + 48u64] = 48u8;
|
|
} else {
|
|
let ndig: u64 = 0u64;
|
|
let t: u64 = objn;
|
|
for (t > 0u64) { ndig += 1u64; t = t / 10u64; };
|
|
let d: u64 = ndig;
|
|
t = objn;
|
|
for (t > 0u64) {
|
|
d -= 1u64;
|
|
out[h + 48u64 + d] = ((t % 10u64): u8) + 48u8;
|
|
t = t / 10u64;
|
|
};
|
|
};
|
|
out[h + 58u64] = 96u8; // member-header magic 0x60
|
|
out[h + 59u64] = 10u8; // 0x0a
|
|
|
|
// the .o bytes, then a '\n' pad iff the size is odd (2-byte align).
|
|
let k: u64 = 0u64;
|
|
for (k < objn) { out[h + 60u64 + k] = objp[k]; k += 1u64; };
|
|
if (pad != 0u64) { out[h + 60u64 + objn] = 10u8; };
|
|
|
|
let fd: i32 = os.open(pathstr(apath),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (fd < 0) {
|
|
cerr("ww --sep: cannot open archive\n");
|
|
return -1;
|
|
};
|
|
os.writeall(fd, out, total);
|
|
os.close(fd);
|
|
return 0;
|
|
};
|
|
|
|
// ---- 5b content-keyed package cache (#63) ----------------------------
|
|
//
|
|
// Twin of cstage's pkgcache_root/md5_file/sep_manifest/cache_lookup/
|
|
// cache_store (cmd/ww/main.c). A per-package cache under WW_PKGCACHE
|
|
// (default out/.pkgcache; gitignored, make clean wipes $(OUT)). Dev-inner-
|
|
// loop convenience only: every gate cold-compiles, so the cache changes no
|
|
// gate output. rule-10: md5 is NOT reimplemented here — both stages shell to
|
|
// the host md5sum, so a HIT's reused P.wwi/P.o stay byte-identical cs==ww.
|
|
|
|
fn pkgcacheroot() *u8 = {
|
|
match (os.getenv("WW_PKGCACHE")) {
|
|
case let s: str =>
|
|
if (s.len > 0) {
|
|
let buf: []u8 = alloc([], (s.len: u64) + 1u64)!;
|
|
let i: i32 = 0;
|
|
for (i < s.len) { buf[i] = s[i]; i += 1; };
|
|
buf[s.len] = 0u8;
|
|
return buf.ptr;
|
|
};
|
|
case void => void;
|
|
};
|
|
let d: []u8 = alloc([], 64u64)!;
|
|
d.len = 64;
|
|
let o: u64 = strinto(d.ptr, 0u64, "out/.pkgcache");
|
|
cstrseal(d.ptr, o);
|
|
return d.ptr;
|
|
};
|
|
|
|
fn pkgcachedir(cacheroot: *u8, g: *sepgraph, pi: i32) *u8 = {
|
|
if (g.pkg[pi].path[0u64] == 0u8) {
|
|
return joinpathlit(cacheroot, "__root");
|
|
};
|
|
return joinpathlit(cacheroot, pathstr(g.pkg[pi].path));
|
|
};
|
|
|
|
// md5appendhex — host md5sum of `path`, captured via a shell redirect to
|
|
// `tmp` (cache is gate-cold → host-tool dep sanctioned, rule-7). Appends the
|
|
// hex digest to `dst` at `off`; returns the new offset, or 0 on failure
|
|
// (caller treats 0 as a non-cacheable miss). Mirrors cstage md5_file's
|
|
// copy-until-whitespace parse so the digest text is byte-identical.
|
|
fn md5appendhex(dst: *u8, off: u64, path: *u8, tmp: *u8) u64 = {
|
|
let cmd: []u8 = alloc([], 8192u64)!;
|
|
cmd.len = 8192;
|
|
let co: u64 = strinto(cmd.ptr, 0u64, "md5sum '");
|
|
co = cstrinto(cmd.ptr, co, path);
|
|
co = strinto(cmd.ptr, co, "' > '");
|
|
co = cstrinto(cmd.ptr, co, tmp);
|
|
co = strinto(cmd.ptr, co, "'");
|
|
cstrseal(cmd.ptr, co);
|
|
let argv: []*u8 = alloc([], 4u64)!;
|
|
argv.len = 4;
|
|
argv[0] = "sh\0".ptr;
|
|
argv[1] = "-c\0".ptr;
|
|
argv[2] = cmd.ptr;
|
|
argv[3] = nil;
|
|
if (procrun("/bin/sh\0".ptr, argv.ptr) != 0) { return 0u64; };
|
|
let (buf, n) = slurp(tmp);
|
|
if (buf == nil) { return 0u64; };
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
let c: u8 = buf[i];
|
|
if (c == 32u8 || c == 9u8 || c == 10u8) { break; };
|
|
dst[off + i] = c;
|
|
i += 1u64;
|
|
};
|
|
if (i == 0u64) { return 0u64; };
|
|
return off + i;
|
|
};
|
|
|
|
// sepmanifest — assemble package pi's content-key manifest (D4) into `out`
|
|
// as deterministic text (see cstage sep_manifest). Returns the manifest
|
|
// length, or 0 on any md5 failure (caller → cold compile). The md5 capture
|
|
// tmp lives in `scratch` (which exists), not the cache dir (created lazily).
|
|
fn sepmanifest(g: *sepgraph, pi: i32, scratch: *u8, c6: *u8, a6: *u8,
|
|
out: *u8) u64 = {
|
|
let tmp: *u8 = sepfname(g, pi, scratch, ".md5tmp");
|
|
let off: u64 = strinto(out, 0u64, "src");
|
|
if (g.pkg[pi].isdir != 0) {
|
|
let (names, n) = enumeratedir(g.pkg[pi].entry);
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
let fp: []u8 = alloc([], os.PATH_MAX: u64)!;
|
|
fp.len = os.PATH_MAX;
|
|
let fo: u64 = cstrinto(fp.ptr, 0u64, g.pkg[pi].entry);
|
|
fo = byteinto(fp.ptr, fo, 47u8); // '/'
|
|
fo = cstrinto(fp.ptr, fo, names[i]);
|
|
cstrseal(fp.ptr, fo);
|
|
off = byteinto(out, off, 32u8); // ' '
|
|
let no: u64 = md5appendhex(out, off, fp.ptr, tmp);
|
|
if (no == 0u64) { return 0u64; };
|
|
off = no;
|
|
i += 1;
|
|
};
|
|
} else {
|
|
off = byteinto(out, off, 32u8);
|
|
let no: u64 = md5appendhex(out, off, g.pkg[pi].entry, tmp);
|
|
if (no == 0u64) { return 0u64; };
|
|
off = no;
|
|
};
|
|
off = byteinto(out, off, 10u8); // '\n'
|
|
|
|
// dep lines, sorted by dep path (insertion sort, mirrors enumeratedir).
|
|
let nd: i32 = g.pkg[pi].ndeps;
|
|
let idx: []i32 = alloc([], SEP_MAXPKG: u64)!;
|
|
idx.len = SEP_MAXPKG;
|
|
let i: i32 = 0;
|
|
for (i < nd) { idx[i] = g.pkg[pi].deps[i]; i += 1; };
|
|
i = 1;
|
|
for (i < nd) {
|
|
let j: i32 = i;
|
|
for (j > 0) {
|
|
let a: *u8 = g.pkg[idx[j - 1]].path;
|
|
let b: *u8 = g.pkg[idx[j]].path;
|
|
if (bytecmp(a, cstrlen(a), b, cstrlen(b)) <= 0) { j = 0; }
|
|
else {
|
|
let t: i32 = idx[j];
|
|
idx[j] = idx[j - 1];
|
|
idx[j - 1] = t;
|
|
j -= 1;
|
|
};
|
|
};
|
|
i += 1;
|
|
};
|
|
i = 0;
|
|
for (i < nd) {
|
|
let di: i32 = idx[i];
|
|
let wwip: *u8 = sepfname(g, di, scratch, ".wwi");
|
|
off = strinto(out, off, "dep ");
|
|
off = cstrinto(out, off, g.pkg[di].path);
|
|
off = byteinto(out, off, 32u8);
|
|
let no: u64 = md5appendhex(out, off, wwip, tmp);
|
|
if (no == 0u64) { return 0u64; };
|
|
off = no;
|
|
off = byteinto(out, off, 10u8);
|
|
i += 1;
|
|
};
|
|
|
|
off = strinto(out, off, "w6c ");
|
|
let no: u64 = md5appendhex(out, off, c6, tmp);
|
|
if (no == 0u64) { return 0u64; };
|
|
off = no;
|
|
off = byteinto(out, off, 10u8);
|
|
off = strinto(out, off, "w6a ");
|
|
no = md5appendhex(out, off, a6, tmp);
|
|
if (no == 0u64) { return 0u64; };
|
|
off = no;
|
|
off = byteinto(out, off, 10u8);
|
|
off = strinto(out, off, "flags -c -I\n");
|
|
return off;
|
|
};
|
|
|
|
// copyfile — byte-copy src→dst (TRUNC). Returns 0 on success, -1 on failure.
|
|
fn copyfile(src: *u8, dst: *u8) i32 = {
|
|
let (buf, n) = slurp(src);
|
|
if (buf == nil) { return -1; };
|
|
let fd: i32 = os.open(pathstr(dst),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (fd < 0) { return -1; };
|
|
os.writeall(fd, buf, n);
|
|
os.close(fd);
|
|
return 0;
|
|
};
|
|
|
|
// cachelookup — HIT iff the manifest equals the stored P.key byte-for-byte
|
|
// AND both cached artifacts exist; on HIT copy them into the scratch
|
|
// wwi/objf so the producer loop can skip compose+w6c+w6a.
|
|
fn cachelookup(cacheroot: *u8, g: *sepgraph, pi: i32, manifest: *u8,
|
|
mlen: u64, wwi: *u8, objf: *u8) i32 = {
|
|
let dir: *u8 = pkgcachedir(cacheroot, g, pi);
|
|
let keyp: *u8 = joinpathlit(dir, "P.key");
|
|
let cwwi: *u8 = joinpathlit(dir, "P.wwi");
|
|
let cobj: *u8 = joinpathlit(dir, "P.o");
|
|
let (stored, sn) = slurp(keyp);
|
|
if (stored == nil) { return 0; };
|
|
if (sn != mlen) { return 0; };
|
|
let i: u64 = 0u64;
|
|
for (i < mlen) {
|
|
if (stored[i] != manifest[i]) { return 0; };
|
|
i += 1u64;
|
|
};
|
|
if (os.access(pathstr(cwwi), 0i32) != 0) { return 0; };
|
|
if (os.access(pathstr(cobj), 0i32) != 0) { return 0; };
|
|
if (copyfile(cwwi, wwi) != 0) { return 0; };
|
|
if (copyfile(cobj, objf) != 0) { return 0; };
|
|
return 1;
|
|
};
|
|
|
|
// cachestore — on MISS persist the artifacts then the key (key LAST: a crash
|
|
// mid-store never leaves a key without its artifacts; the next run re-misses).
|
|
fn cachestore(cacheroot: *u8, g: *sepgraph, pi: i32, manifest: *u8,
|
|
mlen: u64, wwi: *u8, objf: *u8) void = {
|
|
let dir: *u8 = pkgcachedir(cacheroot, g, pi);
|
|
match (os.mkdirs(pathstr(dir), 493i32)) { // 0o755
|
|
case void => void;
|
|
case let e: os.oserror => void; // best-effort; copyfile surfaces a real failure
|
|
};
|
|
let cwwi: *u8 = joinpathlit(dir, "P.wwi");
|
|
let cobj: *u8 = joinpathlit(dir, "P.o");
|
|
let keyp: *u8 = joinpathlit(dir, "P.key");
|
|
if (copyfile(wwi, cwwi) != 0) { return; };
|
|
if (copyfile(objf, cobj) != 0) { return; };
|
|
let fd: i32 = os.open(pathstr(keyp),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (fd < 0) { return; };
|
|
os.writeall(fd, manifest, mlen);
|
|
os.close(fd);
|
|
};
|
|
|
|
// buildonesep — the --sep orchestration: discover deps, reverse-topo,
|
|
// the transitive producer loop (one `w6c -c -I` per package, dep-first,
|
|
// each `.o` wrapped in its own deterministic per-package `.a`), then a
|
|
// reverse-topo `w6l` of the `.a` set + libwwrt.a. Side files land in a
|
|
// cold `<stem>.sepwork` scratch dir. Twin of cstage build_one_sep.
|
|
fn buildonesep(selfdir: *u8, src: *u8, entryisdir: i32, out: *u8,
|
|
objstem: *u8, incs: *u8, lf: *lflags, istest: i32) i32 = {
|
|
let c6: *u8 = joinpathlit(selfdir, "w6c_ww");
|
|
let a6: *u8 = joinpathlit(selfdir, "w6a_ww");
|
|
let l6: *u8 = joinpathlit(selfdir, "w6l_ww");
|
|
|
|
// Default lib search path: <selfdir>/../../lib
|
|
let dotdotlib: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
dotdotlib.len = os.PATH_MAX;
|
|
{
|
|
let off: u64 = cstrinto(dotdotlib.ptr, 0u64, selfdir);
|
|
off = strinto(dotdotlib.ptr, off, "/../../lib");
|
|
cstrseal(dotdotlib.ptr, off);
|
|
};
|
|
|
|
// Source directory (mirrors buildone).
|
|
let srcd: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
srcd.len = os.PATH_MAX;
|
|
if (entryisdir != 0) {
|
|
let slen: u64 = cstrlen(src);
|
|
let k: u64 = 0u64;
|
|
for (k < slen) { srcd[k] = src[k]; k += 1u64; };
|
|
for (slen > 1u64) {
|
|
if (srcd[slen - 1u64] != 47u8) { break; };
|
|
slen -= 1u64;
|
|
};
|
|
srcd[slen] = 0u8;
|
|
} else {
|
|
let slen: u64 = cstrlen(src);
|
|
let last: u64 = slen;
|
|
let found: bool = false;
|
|
let i: u64 = slen;
|
|
for (i > 0u64) {
|
|
i -= 1u64;
|
|
if (src[i] == 47u8) { last = i; found = true; i = 0u64; };
|
|
};
|
|
if (found) {
|
|
let k: u64 = 0u64;
|
|
for (k < last) { srcd[k] = src[k]; k += 1u64; };
|
|
srcd[last] = 0u8;
|
|
} else {
|
|
srcd[0] = 46u8; // '.'
|
|
srcd[1] = 0u8;
|
|
};
|
|
};
|
|
|
|
// searchpath = srcd + ':' + incs + ':' + dotdotlib.
|
|
let searchpath: []u8 = alloc([], (os.PATH_MAX: u64) * 3u64)!;
|
|
searchpath.len = ((os.PATH_MAX: u64) * 3u64): i32;
|
|
{
|
|
let off: u64 = cstrinto(searchpath.ptr, 0u64, srcd.ptr);
|
|
off = byteinto(searchpath.ptr, off, 58u8); // ':'
|
|
if (incs[0u64] != 0u8) {
|
|
off = cstrinto(searchpath.ptr, off, incs);
|
|
off = byteinto(searchpath.ptr, off, 58u8);
|
|
};
|
|
off = cstrinto(searchpath.ptr, off, dotdotlib.ptr);
|
|
cstrseal(searchpath.ptr, off);
|
|
};
|
|
|
|
// Stem for the scratch dir (mirrors buildone).
|
|
let stem: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
stem.len = os.PATH_MAX;
|
|
if (entryisdir != 0) {
|
|
let dlen: u64 = cstrlen(srcd.ptr);
|
|
let bo: u64 = basenameoff(srcd.ptr, dlen);
|
|
let off: u64 = cstrinto(stem.ptr, 0u64, srcd.ptr);
|
|
stem[off] = 47u8; off += 1u64; // '/'
|
|
let i: u64 = bo;
|
|
for (i < dlen) { stem[off] = srcd[i]; off += 1u64; i += 1u64; };
|
|
cstrseal(stem.ptr, off);
|
|
} else {
|
|
makestem(stem.ptr, src);
|
|
};
|
|
let effstem: *u8 = stem.ptr;
|
|
if (objstem != nil) { effstem = objstem; };
|
|
let scratch: *u8 = appendlit(effstem, ".sepwork");
|
|
|
|
// rm -rf scratch (cold); recreate. Reuse os.removeall if present;
|
|
// here we mkdir and rely on TRUNC opens to overwrite stale files.
|
|
os.mkdir(pathstr(scratch), 493i32); // 0o755 (idempotent; stale files TRUNC'd)
|
|
|
|
// libwwrt.a path: <selfdir>/../lib/libwwrt.a
|
|
let libwwrt: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
libwwrt.len = os.PATH_MAX;
|
|
{
|
|
let off: u64 = cstrinto(libwwrt.ptr, 0u64, selfdir);
|
|
off = strinto(libwwrt.ptr, off, "/../lib/libwwrt.a");
|
|
cstrseal(libwwrt.ptr, off);
|
|
};
|
|
|
|
// Discover.
|
|
let pkgslot: []seppkg = alloc([], SEP_MAXPKG: u64)!;
|
|
pkgslot.len = SEP_MAXPKG;
|
|
let g: *sepgraph = alloc(sepgraph{pkg = pkgslot, n = 0})!;
|
|
let root: i32 = sepfindoradd(g, "\0".ptr, src, entryisdir);
|
|
if (root < 0) { return 1; };
|
|
// #79 (-T): lib/test is the synth main's `test.run` callee but @test
|
|
// files never `import test;`. Inject it as a direct dep of the root so
|
|
// sepscanpkg pulls test + its transitive deps; the producer adds -T to
|
|
// the root and `test.run` links against test's `.a`. Mirrors the
|
|
// combined path's auto-bundle (buildone istest) and sepscanfile dedup.
|
|
if (istest != 0) {
|
|
let td: i32 = 0;
|
|
let tp: *u8 = locateimport(searchpath.ptr, "test".ptr, "test".len: u64, &td);
|
|
if (tp != nil) {
|
|
let ti: i32 = sepfindoradd(g, "test\0".ptr, tp, td);
|
|
if (ti < 0) { return 1; };
|
|
let seen: bool = false;
|
|
let m: i32 = 0;
|
|
for (m < g.pkg[root].ndeps) {
|
|
if (g.pkg[root].deps[m] == ti) { seen = true; };
|
|
m += 1;
|
|
};
|
|
if (!seen) {
|
|
if (g.pkg[root].ndeps < SEP_MAXPKG) {
|
|
g.pkg[root].deps[g.pkg[root].ndeps] = ti;
|
|
g.pkg[root].ndeps += 1;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if (sepscanpkg(g, root, searchpath.ptr) < 0) { return 1; };
|
|
|
|
// Reset colors, reverse-topo.
|
|
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; };
|
|
|
|
// Producer loop — dep-first, one `w6c -c -I` per package.
|
|
let cacheroot: *u8 = pkgcacheroot();
|
|
let oi: i32 = 0;
|
|
for (oi < norder) {
|
|
let pi: i32 = order[oi];
|
|
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");
|
|
let objf: *u8 = sepfname(g, pi, scratch, ".o");
|
|
// 5b: skip compose+w6c+w6a on a content-key HIT. The root is
|
|
// never cached — it is the build target, always recompiled.
|
|
let manbuf: []u8 = alloc([], 16384u64)!;
|
|
manbuf.len = 16384;
|
|
let mlen: u64 = 0u64;
|
|
let cacheable: i32 = 0;
|
|
if (pi != root) {
|
|
mlen = sepmanifest(g, pi, scratch, c6, a6, manbuf.ptr);
|
|
if (mlen > 0u64) { cacheable = 1; };
|
|
};
|
|
let fresh: i32 = 0;
|
|
if (cacheable != 0) {
|
|
fresh = cachelookup(cacheroot, g, pi, manbuf.ptr, mlen, wwi, objf);
|
|
};
|
|
if (fresh == 0) {
|
|
if (sepcomposeunit(g, pi, scratch, order, norder, searchpath.ptr, unitf) < 0) {
|
|
return 1;
|
|
};
|
|
{
|
|
// BUG-1 (#69): -I <wwi> is purely the root's UNUSED
|
|
// `.wwi` output path, but it triggers wwiemit ->
|
|
// checkexportedtype on the root. A terminal binary's
|
|
// root legitimately has `export fn` over an unexported
|
|
// 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 --sep` so w6c
|
|
// synthesizes the test main; deps never get -T.
|
|
let roott: bool = (pi == root) && (istest != 0);
|
|
let alen: u64 = 8u64;
|
|
if (pi == root) { alen = 6u64; if (roott) { alen = 7u64; }; };
|
|
let argv: []*u8 = alloc([], alen)!;
|
|
argv.len = (alen: i32);
|
|
argv[0] = "w6c\0".ptr;
|
|
let k: u64 = 1u64;
|
|
if (roott) { argv[k] = "-T\0".ptr; k += 1u64; };
|
|
argv[k] = "-c\0".ptr; k += 1u64;
|
|
if (pi != root) {
|
|
argv[k] = "-I\0".ptr; k += 1u64;
|
|
argv[k] = wwi; k += 1u64;
|
|
};
|
|
argv[k] = "-o\0".ptr; k += 1u64;
|
|
argv[k] = asmf; k += 1u64;
|
|
argv[k] = unitf; k += 1u64;
|
|
argv[k] = nil;
|
|
if (procrun(c6, argv.ptr) != 0) {
|
|
cerr("ww --sep: w6c failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
{
|
|
let argv: []*u8 = alloc([], 5u64)!;
|
|
argv.len = 5;
|
|
argv[0] = "w6a\0".ptr;
|
|
argv[1] = "-o\0".ptr;
|
|
argv[2] = objf;
|
|
argv[3] = asmf;
|
|
argv[4] = nil;
|
|
if (procrun(a6, argv.ptr) != 0) {
|
|
cerr("ww --sep: w6a failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
if (cacheable != 0) {
|
|
cachestore(cacheroot, g, pi, manbuf.ptr, mlen, wwi, objf);
|
|
};
|
|
};
|
|
// Wrap each DEP package's `.o` in its own deterministic `.a`
|
|
// (5a). The ROOT stays a positional `.o` (force-loaded — it's
|
|
// the build target), so `main` is defined before any archive is
|
|
// processed (mirrors buildone's root treatment). The link
|
|
// consumes `.o`/`.a`, never `.wwi`.
|
|
if (pi != root) {
|
|
let apath: *u8 = sepfname(g, pi, scratch, ".a");
|
|
if (archiveo(objf, apath) != 0) {
|
|
cerr("ww --sep: archive failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
oi += 1;
|
|
};
|
|
|
|
// Reverse-topo link of the per-package `.a` set: root.a first
|
|
// (order[norder-1]), deps after, then libwwrt.a (which still
|
|
// selectively pulls only the runtime members a live undef needs).
|
|
// argv: 3 fixed (w6l,-o,out) + 1 per .a + 1 libwwrt + 2*nlibdirs
|
|
// + 2*nlibs + 1 nil.
|
|
let nldirs: i32 = 0;
|
|
let nllibs: i32 = 0;
|
|
let ldirs: **u8 = nil;
|
|
let llibs: **u8 = nil;
|
|
if (lf != nil) {
|
|
nldirs = lf.nlibdirs;
|
|
nllibs = lf.nlibs;
|
|
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) {
|
|
// root: positional `.o` (force-load); deps: `.a` (selective).
|
|
let suf: str = ".a";
|
|
if (order[li] == root) { suf = ".o"; };
|
|
largv[pos] = sepfname(g, order[li], 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;
|
|
if (procrun(l6, largv.ptr) != 0) {
|
|
cerr("ww --sep: w6l failed\n");
|
|
return 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// ---- Module-by-name resolution ----------------------------------------
|
|
//
|
|
// Mirrors cmd/ww/main.c:resolvemodule. Maps a name like "foo", "lib/foo",
|
|
// "foo.ww", or "." to a concrete .ww file path:
|
|
// 1. literal <name>.ww that exists → use as-is
|
|
// 2. "." → <cwd>/<basename(cwd)>.ww → that, if it exists
|
|
// 3. <name>/<basename(name)>.ww → that, if it exists
|
|
// 4. walk search path (cwd:incs:<selfdir>/../../lib):
|
|
// <dir>/<name>.ww or <dir>/<name>/<name>.ww
|
|
|
|
fn cstrendswithlit(p: *u8, lit: str) bool = {
|
|
return strings.hassuffix(pathstr(p), lit);
|
|
};
|
|
|
|
// basenameoff — return the offset of the last path segment within `p`
|
|
// (i.e. one past the final '/'). Returns 0 if there's no slash.
|
|
fn basenameoff(p: *u8, plen: u64) u64 = {
|
|
let start: u64 = 0u64;
|
|
let i: u64 = 0u64;
|
|
for (i < plen) {
|
|
if (p[i] == '/') { start = i + 1u64; };
|
|
i += 1u64;
|
|
};
|
|
return start;
|
|
};
|
|
|
|
// arenadupcstr — copy `plen` bytes from `src` into a fresh NUL-sealed
|
|
// heap buffer.
|
|
fn arenadupcstr(src: *u8, plen: u64) *u8 = {
|
|
let buf: []u8 = alloc([], plen + 1u64)!;
|
|
let i: u64 = 0u64;
|
|
for (i < plen) { buf[i] = src[i]; i += 1u64; };
|
|
buf[plen] = 0u8;
|
|
return buf.ptr;
|
|
};
|
|
|
|
// buildsearchpath — compose the colon-separated lookup path used by
|
|
// resolvemodule's case (4). Order: "." : <incs> : <selfdir>/../../lib
|
|
fn buildsearchpath(selfdir: *u8, incs: *u8) *u8 = {
|
|
let buf: []u8 = alloc([], (os.PATH_MAX: u64) * 2u64)!;
|
|
let off: u64 = 0u64;
|
|
buf[off] = 46u8; off += 1u64; // '.'
|
|
if (incs != nil) {
|
|
if (incs[0u64] != 0u8) {
|
|
buf[off] = 58u8; off += 1u64; // ':'
|
|
off = cstrinto(buf.ptr, off, incs);
|
|
};
|
|
};
|
|
buf[off] = 58u8; off += 1u64;
|
|
off = cstrinto(buf.ptr, off, selfdir);
|
|
off = strinto(buf.ptr, off, "/../../lib");
|
|
cstrseal(buf.ptr, off);
|
|
return buf.ptr;
|
|
};
|
|
|
|
// resolvemodule — map a name like "foo", "lib/foo", "foo.ww", or
|
|
// "." to a concrete entry path. Sets *isdir when the entry is a
|
|
// module directory (caller will dir-enumerate).
|
|
fn resolvemodule(selfdir: *u8, name: *u8, incs: *u8, isdir: *i32) *u8 = {
|
|
let nlen: u64 = cstrlen(name);
|
|
|
|
// (1) Literal file that exists → use as-is.
|
|
if (cstrendswithlit(name, ".ww")) {
|
|
if (os.access(pathstr(name), 0i32) == 0) {
|
|
*isdir = 0;
|
|
return arenadupcstr(name, nlen);
|
|
};
|
|
};
|
|
|
|
// (2) Existing path → use as-is, dir vs file via stat.
|
|
let fi: os.filestat;
|
|
let sr: (void | os.oserror) = os.stat(&fi, pathstr(name));
|
|
let found: bool = false;
|
|
let foundisdir: i32 = 0;
|
|
match (sr) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32; // S_IFMT
|
|
if (t == os.mode.DIR: u32) { foundisdir = 1; };
|
|
found = true;
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
if (found) {
|
|
*isdir = foundisdir;
|
|
return arenadupcstr(name, nlen);
|
|
};
|
|
|
|
// (3) Search-path lookup with dot-to-slash path translation.
|
|
let search: *u8 = buildsearchpath(selfdir, incs);
|
|
return locateimport(search, name, nlen, isdir);
|
|
};
|
|
|
|
// ---- Subcommand handlers ----------------------------------------------
|
|
|
|
fn writeusage(fd: i32) void = {
|
|
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [path] compile module to a static binary (path defaults to cwd)\n run [path] ... build then exec, passing extra args to the program\n test [path] build and run *_test.ww in the module (path defaults to cwd)\n version print version and exit\n\n path forms:\n foo.ww literal file\n foo search cwd, -I dirs, then $WW_LIB-equiv for foo.ww or foo/foo.ww\n lib/foo directory: build lib/foo/foo.ww\n . build the cwd's <basename>.ww\n";
|
|
os.write(fd, s.ptr, s.len: u64);
|
|
};
|
|
|
|
fn doversion() i32 = {
|
|
os.write(1, "ww 0.0\n".ptr, 7u64);
|
|
return 0;
|
|
};
|
|
|
|
// Compute the basename of src (without trailing ".ww") into a fresh
|
|
// buffer. Used as the default output path for `ww build`.
|
|
fn defaultoutpath(src: *u8) *u8 = {
|
|
let n: u64 = cstrlen(src);
|
|
let start: u64 = 0u64;
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
if (src[i] == 47u8) { start = i + 1u64; }; // '/'
|
|
i += 1u64;
|
|
};
|
|
let out: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
out.len = os.PATH_MAX;
|
|
let off: u64 = 0u64;
|
|
let j: u64 = start;
|
|
for (j < n) {
|
|
out[off] = src[j];
|
|
off += 1u64;
|
|
j += 1u64;
|
|
};
|
|
// Strip ".ww" if present.
|
|
if (off >= 3u64) {
|
|
if (out[off - 3u64] == 46u8) {
|
|
if (out[off - 2u64] == 119u8) {
|
|
if (out[off - 1u64] == 119u8) {
|
|
off -= 3u64;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
cstrseal(out.ptr, off);
|
|
return out.ptr;
|
|
};
|
|
|
|
fn dobuild(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
|
let src: *u8 = nil;
|
|
let outflag: *u8 = nil; // -o target (binary + intermediate stem); T3
|
|
let incs: []u8 = alloc([], (os.PATH_MAX: u64) * 2u64)!;
|
|
incs.len = ((os.PATH_MAX: u64) * 2u64): i32;
|
|
let incoff: u64 = 0u64;
|
|
cstrseal(incs.ptr, 0u64);
|
|
|
|
let maxlflags: i32 = 32;
|
|
let libdirs: []*u8 = alloc([], maxlflags: u64)!;
|
|
libdirs.len = maxlflags;
|
|
let nlibdirs: i32 = 0;
|
|
let libs: []*u8 = alloc([], maxlflags: u64)!;
|
|
libs.len = maxlflags;
|
|
let nlibs: i32 = 0;
|
|
|
|
let i: i32 = start;
|
|
for (i < argc) {
|
|
let p: *u8 = argv[i];
|
|
if (p[0u64] == 45u8) { // '-'
|
|
if (cstreqlit(p, "--sep")) { // E3-C1: sep is sole path; accepted no-op (#87)
|
|
} else { if (p[1u64] == 73u8) { // '-I'
|
|
let dir: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
dir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww build: -I needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
dir = argv[i];
|
|
};
|
|
if (incoff > 0u64) {
|
|
incs[incoff] = 58u8; // ':'
|
|
incoff += 1u64;
|
|
};
|
|
incoff = cstrinto(incs.ptr, incoff, dir);
|
|
cstrseal(incs.ptr, incoff);
|
|
} else { if (p[1u64] == 76u8) { // '-L'
|
|
let dir: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
dir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww build: -L needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
dir = argv[i];
|
|
};
|
|
if (nlibdirs >= maxlflags) {
|
|
cerr("ww build: too many -L\n");
|
|
return 2;
|
|
};
|
|
libdirs[nlibdirs] = dir;
|
|
nlibdirs += 1;
|
|
} else { if (p[1u64] == 108u8) { // '-l'
|
|
let nm: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
nm = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww build: -l needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
nm = argv[i];
|
|
};
|
|
if (nlibs >= maxlflags) {
|
|
cerr("ww build: too many -l\n");
|
|
return 2;
|
|
};
|
|
libs[nlibs] = nm;
|
|
nlibs += 1;
|
|
} else { if (p[1u64] == 111u8) { // '-o'
|
|
if (p[2u64] != 0u8) {
|
|
outflag = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww build: -o needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
outflag = argv[i];
|
|
};
|
|
} else {
|
|
cerr("ww build: unknown flag\n");
|
|
return 2;
|
|
}; }; }; }; };
|
|
} else {
|
|
if (src == nil) { src = p; };
|
|
};
|
|
i += 1;
|
|
};
|
|
|
|
if (src == nil) {
|
|
// default to cwd module
|
|
let dot: [2]u8 = ['.': u8, 0u8];
|
|
src = &dot[0];
|
|
};
|
|
let isdir: i32 = 0;
|
|
let resolved: *u8 = resolvemodule(selfdir, src, incs.ptr, &isdir);
|
|
if (resolved == nil) {
|
|
cerr("ww build: cannot find module\n");
|
|
return 1;
|
|
};
|
|
let out: *u8 = nil;
|
|
let objstem: *u8 = nil;
|
|
if (outflag != nil) {
|
|
// -o sets both the binary path and the intermediate stem so
|
|
// artifacts land beside the requested output (T3).
|
|
out = outflag;
|
|
objstem = outflag;
|
|
} else { if (isdir != 0) {
|
|
let rlen: u64 = cstrlen(resolved);
|
|
for (rlen > 1u64) {
|
|
if (resolved[rlen - 1u64] != 47u8) { break; };
|
|
rlen -= 1u64;
|
|
};
|
|
let bo: u64 = basenameoff(resolved, rlen);
|
|
let outbuf: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
outbuf.len = os.PATH_MAX;
|
|
out = outbuf.ptr;
|
|
let i: u64 = bo;
|
|
let off: u64 = 0u64;
|
|
for (i < rlen) { out[off] = resolved[i]; off += 1u64; i += 1u64; };
|
|
cstrseal(out, off);
|
|
} else {
|
|
out = defaultoutpath(resolved);
|
|
}; };
|
|
let lf: lflags;
|
|
lf.libdirs = libdirs.ptr;
|
|
lf.nlibdirs = nlibdirs;
|
|
lf.libs = libs.ptr;
|
|
lf.nlibs = nlibs;
|
|
return buildonesep(selfdir, resolved, isdir, out, objstem, incs.ptr, &lf, 0i32);
|
|
};
|
|
|
|
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
|
|
// terminated buf. Pid is folded in decimal manually since we don't
|
|
// import strconv.
|
|
fn makeruntmp(buf: *u8) void = {
|
|
let off: u64 = 0u64;
|
|
off = strinto(buf, off, "/tmp/ww_run_");
|
|
let pid: i32 = os.getpid();
|
|
// itoa for non-negative pid
|
|
let dig: [16]u8;
|
|
let n: i32 = 0;
|
|
if (pid <= 0) {
|
|
dig[n] = 48u8; // '0'
|
|
n += 1;
|
|
} else {
|
|
let v: i32 = pid;
|
|
for (v > 0) {
|
|
dig[n] = ((v % 10) + 48): u8;
|
|
n += 1;
|
|
v = v / 10;
|
|
};
|
|
};
|
|
let k: i32 = n - 1;
|
|
for (k >= 0) {
|
|
buf[off] = dig[k];
|
|
off += 1u64;
|
|
k -= 1;
|
|
};
|
|
cstrseal(buf, off);
|
|
};
|
|
|
|
fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
|
let src: *u8 = nil;
|
|
let passstart: i32 = -1; // first argv idx to pass through to program
|
|
let incs: []u8 = alloc([], (os.PATH_MAX: u64) * 2u64)!;
|
|
incs.len = ((os.PATH_MAX: u64) * 2u64): i32;
|
|
let incoff: u64 = 0u64;
|
|
cstrseal(incs.ptr, 0u64);
|
|
|
|
let maxlflags: i32 = 32;
|
|
let libdirs: []*u8 = alloc([], maxlflags: u64)!;
|
|
libdirs.len = maxlflags;
|
|
let nlibdirs: i32 = 0;
|
|
let libs: []*u8 = alloc([], maxlflags: u64)!;
|
|
libs.len = maxlflags;
|
|
let nlibs: i32 = 0;
|
|
|
|
let i: i32 = start;
|
|
for (i < argc) {
|
|
if (passstart >= 0) { i = argc; } // stop, leave rest for exec
|
|
else {
|
|
let p: *u8 = argv[i];
|
|
if (p[0u64] == 45u8) {
|
|
if (cstreqlit(p, "--sep")) { // E3-C1: sep is sole path; accepted no-op (#87)
|
|
} else { if (p[1u64] == 73u8) {
|
|
let dir: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
dir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww run: -I needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
dir = argv[i];
|
|
};
|
|
if (incoff > 0u64) {
|
|
incs[incoff] = 58u8;
|
|
incoff += 1u64;
|
|
};
|
|
incoff = cstrinto(incs.ptr, incoff, dir);
|
|
cstrseal(incs.ptr, incoff);
|
|
} else { if (p[1u64] == 76u8) {
|
|
let dir: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
dir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww run: -L needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
dir = argv[i];
|
|
};
|
|
if (nlibdirs >= maxlflags) {
|
|
cerr("ww run: too many -L\n");
|
|
return 2;
|
|
};
|
|
libdirs[nlibdirs] = dir;
|
|
nlibdirs += 1;
|
|
} else { if (p[1u64] == 108u8) {
|
|
let nm: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
nm = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww run: -l needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
nm = argv[i];
|
|
};
|
|
if (nlibs >= maxlflags) {
|
|
cerr("ww run: too many -l\n");
|
|
return 2;
|
|
};
|
|
libs[nlibs] = nm;
|
|
nlibs += 1;
|
|
} else { if (p[1u64] == 111u8) { // '-o'
|
|
// run always execs the temp binary; -o is accepted+
|
|
// ignored, mirroring the C driver's shared flag parser.
|
|
if (p[2u64] == 0u8) {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww run: -o needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
};
|
|
} else {
|
|
cerr("ww run: unknown flag\n");
|
|
return 2;
|
|
}; }; }; }; };
|
|
i += 1;
|
|
} else {
|
|
if (src == nil) {
|
|
src = p;
|
|
i += 1;
|
|
} else {
|
|
passstart = i; // remaining args go to the program
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
if (src == nil) {
|
|
let dot: [2]u8 = ['.': u8, 0u8];
|
|
src = &dot[0];
|
|
};
|
|
let isdir: i32 = 0;
|
|
let resolved: *u8 = resolvemodule(selfdir, src, incs.ptr, &isdir);
|
|
if (resolved == nil) {
|
|
cerr("ww run: cannot find module\n");
|
|
return 1;
|
|
};
|
|
|
|
let tmp: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
tmp.len = os.PATH_MAX;
|
|
makeruntmp(tmp.ptr);
|
|
let lf: lflags;
|
|
lf.libdirs = libdirs.ptr;
|
|
lf.nlibdirs = nlibdirs;
|
|
lf.libs = libs.ptr;
|
|
lf.nlibs = nlibs;
|
|
// objstem = tmp → intermediates at /tmp/ww_run_<pid>.{s,o,combined.ww},
|
|
// never next to the source (T3).
|
|
if (buildonesep(selfdir, resolved, isdir, tmp.ptr, tmp.ptr, incs.ptr, &lf, 0i32) != 0) {
|
|
os.remove(pathstr(tmp.ptr));
|
|
return 1;
|
|
};
|
|
|
|
// exec with [tmp, argv[passstart..argc), nil]
|
|
let nextra: i32 = 0;
|
|
if (passstart >= 0) { nextra = argc - passstart; };
|
|
let total: i32 = nextra + 2;
|
|
let execargv: []*u8 = alloc([], total: u64)!;
|
|
execargv.len = total;
|
|
execargv[0] = tmp.ptr;
|
|
let k: i32 = 0;
|
|
for (k < nextra) {
|
|
execargv[k + 1] = argv[passstart + k];
|
|
k += 1;
|
|
};
|
|
execargv[nextra + 1] = nil;
|
|
let rc: i32 = procrun(tmp.ptr, execargv.ptr);
|
|
os.remove(pathstr(tmp.ptr));
|
|
return rc;
|
|
};
|
|
|
|
// ---- ww test ----------------------------------------------------------
|
|
//
|
|
// Mirrors cmd/ww/main.c:dotest. Two modes:
|
|
// single-file: build+run a literal *.ww file, return its exit code
|
|
// directory: open the dir, getdents64, build+run each *_test.ww,
|
|
// report ok/FAIL per file, return 0 iff all pass.
|
|
|
|
fn runsingletest(selfdir: *u8, src: *u8, incs: *u8, compileonly: i32, outstem: *u8, pattern: *u8) i32 = {
|
|
let tmp: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
tmp.len = os.PATH_MAX;
|
|
// -o redirects the binary + its combined (objstem, T3) to <stem>; the
|
|
// default temp keeps the combined next to the source as before.
|
|
let outp: *u8 = nil;
|
|
let objstem: *u8 = nil;
|
|
if (outstem != nil) {
|
|
outp = outstem;
|
|
objstem = outstem;
|
|
} else {
|
|
makeruntmp(tmp.ptr);
|
|
outp = tmp.ptr;
|
|
};
|
|
// E3-C1: separate compilation is the sole build path (task #87).
|
|
let lf: lflags;
|
|
lf.libdirs = nil;
|
|
lf.nlibdirs = 0;
|
|
lf.libs = nil;
|
|
lf.nlibs = 0;
|
|
let bres: i32 = buildonesep(selfdir, src, 0, outp, objstem, incs, &lf, 1i32);
|
|
if (bres != 0) {
|
|
if (outstem == nil) { os.remove(pathstr(outp)); };
|
|
return 1;
|
|
};
|
|
if (compileonly != 0) { return 0; };
|
|
// #17 fnmatch filter: forward `pattern` as argv[1] so lib/test run()
|
|
// reads it via os.args. procrun execve's a NUL-terminated argv, so the
|
|
// terminator (not .len) bounds the vector. cstage twin: run_test_bin.
|
|
let execargv: []*u8 = alloc([], 3u64)!;
|
|
execargv[0] = outp;
|
|
if (pattern != nil) {
|
|
execargv.len = 3;
|
|
execargv[1] = pattern;
|
|
execargv[2] = nil;
|
|
} else {
|
|
execargv.len = 2;
|
|
execargv[1] = nil;
|
|
};
|
|
let rc: i32 = procrun(outp, execargv.ptr);
|
|
if (outstem == nil) { os.remove(pathstr(outp)); };
|
|
return rc;
|
|
};
|
|
|
|
fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
|
|
let fd: i32 = os.open(pathstr(dir), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) {
|
|
cerr("ww test: cannot open directory\n");
|
|
return 1;
|
|
};
|
|
|
|
let pass: i32 = 0;
|
|
let fail: i32 = 0;
|
|
let seen: i32 = 0; // #64: count *_test.ww matches for the zero-test gate
|
|
let buf: []u8 = alloc([], 8192u64)!;
|
|
buf.len = 8192;
|
|
let dirlen: u64 = cstrlen(dir);
|
|
|
|
let n: i64 = os.getdents64(fd, buf.ptr, 8192u64);
|
|
for (n > 0i64) {
|
|
let off: u64 = 0u64;
|
|
let nu: u64 = n: u64;
|
|
for (off < nu) {
|
|
// d_reclen at offset+16 (u16 LE), d_name at offset+19 (cstr)
|
|
let blo: u64 = (buf[off + 16u64]): u64;
|
|
let bhi: u64 = (buf[off + 17u64]): u64;
|
|
let reclen: u64 = blo + (bhi * 256u64);
|
|
let name: *u8 = buf.ptr + off + 19u64;
|
|
if (cstrendswithlit(name, "_test.ww")) {
|
|
seen += 1;
|
|
let nlen: u64 = cstrlen(name);
|
|
// path = <dir>/<name>
|
|
let path: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
path.len = os.PATH_MAX;
|
|
let poff: u64 = cstrinto(path.ptr, 0u64, dir);
|
|
path[poff] = 47u8; poff += 1u64;
|
|
let i: u64 = 0u64;
|
|
for (i < nlen) { path[poff + i] = name[i]; i += 1u64; };
|
|
poff += nlen;
|
|
cstrseal(path.ptr, poff);
|
|
// incs = <dir> so test files can `use ` siblings
|
|
let tincs: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
tincs.len = os.PATH_MAX;
|
|
let ic: u64 = cstrinto(tincs.ptr, 0u64, dir);
|
|
cstrseal(tincs.ptr, ic);
|
|
let tmp: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
tmp.len = os.PATH_MAX;
|
|
makeruntmp(tmp.ptr);
|
|
let bres: i32 = buildonesep(selfdir, path.ptr, 0, tmp.ptr, nil, tincs.ptr, nil, 1i32);
|
|
if (bres != 0) {
|
|
fail += 1;
|
|
cerr("FAIL ");
|
|
os.write(2, name, nlen);
|
|
cerr(" (build)\n");
|
|
} else {
|
|
let execargv: []*u8 = alloc([], 2u64)!;
|
|
execargv.len = 2;
|
|
execargv[0] = tmp.ptr;
|
|
execargv[1] = nil;
|
|
let rc: i32 = procrun(tmp.ptr, execargv.ptr);
|
|
if (rc == 0) {
|
|
pass += 1;
|
|
os.write(1, "ok ".ptr, 5u64);
|
|
os.write(1, name, nlen);
|
|
os.write(1, "\n".ptr, 1u64);
|
|
} else {
|
|
fail += 1;
|
|
cerr("FAIL ");
|
|
os.write(2, name, nlen);
|
|
cerr("\n");
|
|
};
|
|
};
|
|
os.remove(pathstr(tmp.ptr));
|
|
};
|
|
off += reclen;
|
|
};
|
|
n = os.getdents64(fd, buf.ptr, 8192u64);
|
|
};
|
|
os.close(fd);
|
|
// #64: a directory with no *_test.ww files is a loud failure, not a
|
|
// silent rc=0 false-green. Mirrors cstage do_test (cmd/ww/main.c:945)
|
|
// "ww test: no *_test.ww files in %s".
|
|
if (seen == 0) {
|
|
cerr("ww test: no *_test.ww files in ");
|
|
os.write(2, dir, dirlen);
|
|
cerr("\n");
|
|
return 1;
|
|
};
|
|
if (fail == 0) { return 0; };
|
|
return 1;
|
|
};
|
|
|
|
fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
|
// -I parsing mirrors dobuild/dorun so a single-file test can resolve
|
|
// transitive imports (e.g. 905_nkname asttest → tok); coupled to the
|
|
// -T flip (task #5/#10).
|
|
let target: *u8 = nil;
|
|
// #17: optional 2nd positional = fnmatch name-filter pattern, forwarded
|
|
// to the test binary as argv[1] (single-file/module only; dir-mode
|
|
// rejects). cstage twin: do_test `pattern`.
|
|
let patarg: *u8 = nil;
|
|
let incs: []u8 = alloc([], (os.PATH_MAX: u64) * 2u64)!;
|
|
incs.len = ((os.PATH_MAX: u64) * 2u64): i32;
|
|
let incoff: u64 = 0u64;
|
|
cstrseal(incs.ptr, 0u64);
|
|
|
|
// -c (compile-only) + -o <stem>: build the test binary + its lib/test-
|
|
// inclusive combined WITHOUT running it, for the byte-id gates. cstage
|
|
// twin: cmd/ww/main.c do_test (error wording identical).
|
|
let compileonly: i32 = 0;
|
|
let outstem: *u8 = nil;
|
|
let i: i32 = start;
|
|
for (i < argc) {
|
|
let p: *u8 = argv[i];
|
|
if (p[0u64] == 45u8) { // '-'
|
|
if (cstreqlit(p, "--sep")) { // E3-C1: sep is sole path; accepted no-op (#87)
|
|
} else { if (p[1u64] == 73u8) { // '-I'
|
|
let dir: *u8 = nil;
|
|
if (p[2u64] != 0u8) {
|
|
dir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww test: -I needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
dir = argv[i];
|
|
};
|
|
if (incoff > 0u64) {
|
|
incs[incoff] = 58u8; // ':'
|
|
incoff += 1u64;
|
|
};
|
|
incoff = cstrinto(incs.ptr, incoff, dir);
|
|
cstrseal(incs.ptr, incoff);
|
|
} else { if (p[1u64] == 99u8 && p[2u64] == 0u8) { // "-c"
|
|
compileonly = 1;
|
|
} else { if (p[1u64] == 111u8) { // '-o'
|
|
if (p[2u64] != 0u8) {
|
|
outstem = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww test: -o needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
outstem = argv[i];
|
|
};
|
|
} else {
|
|
cerr("ww test: unknown flag\n");
|
|
return 2;
|
|
}; }; }; }; // #79: extra close for the --sep else
|
|
} else {
|
|
if (target == nil) { target = p; }
|
|
else { if (patarg == nil) { patarg = p; }; };
|
|
};
|
|
i += 1;
|
|
};
|
|
if (target == nil) {
|
|
let dot: [2]u8 = ['.': u8, 0u8];
|
|
target = &dot[0];
|
|
};
|
|
|
|
// single-file mode: literal *.ww that exists
|
|
if (cstrendswithlit(target, ".ww")) {
|
|
if (os.access(pathstr(target), 0i32) == 0) {
|
|
return runsingletest(selfdir, target, incs.ptr, compileonly, outstem, patarg);
|
|
};
|
|
};
|
|
|
|
if (compileonly != 0 || outstem != nil) {
|
|
cerr("ww test: -c/-o need a single test file\n");
|
|
return 2;
|
|
};
|
|
// #17: a name-filter pattern is per-binary; dir mode builds one binary
|
|
// per *_test.ww, so a single pattern can't route. cstage twin parity.
|
|
if (patarg != nil) {
|
|
cerr("ww test: pattern needs a single test file\n");
|
|
return 2;
|
|
};
|
|
// otherwise treat target as a directory; enumerate *_test.ww
|
|
return rundirtests(selfdir, target);
|
|
};
|
|
|
|
// ---- Entry -------------------------------------------------------------
|
|
|
|
export fn main(argc: i32, argv: **u8) i32 = {
|
|
if (argc < 1) {
|
|
writeusage(2);
|
|
return 2;
|
|
};
|
|
|
|
// selfdir = dirname(argv[0])
|
|
let selfdir: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
selfdir.len = os.PATH_MAX;
|
|
selfdirinto(selfdir.ptr, (os.PATH_MAX: u64), argv[0]);
|
|
|
|
if (argc < 2) {
|
|
writeusage(2);
|
|
return 2;
|
|
};
|
|
let cmd: *u8 = argv[1];
|
|
if (cstreqlit(cmd, "-V")) { return doversion(); };
|
|
if (cstreqlit(cmd, "version")) { return doversion(); };
|
|
if (cstreqlit(cmd, "-h")) {
|
|
writeusage(1);
|
|
return 0;
|
|
};
|
|
if (cstreqlit(cmd, "--help")) {
|
|
writeusage(1);
|
|
return 0;
|
|
};
|
|
if (cstreqlit(cmd, "build")) {
|
|
return dobuild(selfdir.ptr, argv, argc, 2);
|
|
};
|
|
if (cstreqlit(cmd, "run")) {
|
|
return dorun(selfdir.ptr, argv, argc, 2);
|
|
};
|
|
if (cstreqlit(cmd, "test")) {
|
|
return dotest(selfdir.ptr, argv, argc, 2);
|
|
};
|
|
cerr("ww: unknown subcommand\n");
|
|
writeusage(2);
|
|
return 2;
|
|
};
|