Go compiles only _test.go files as tests; discovery now keys on the
_test.ww suffix alone. The line-leading-@test compatibility allowance
(noncanonical filenames admitted as test sources) is removed from both
driver stages and the coordinator. An @test declaration outside a
*_test.ww file is rejected loudly ("@test declaration outside
*_test.ww", wording byte-identical cs/ww) instead of silently running
under compose or silently dropping in a non-T build (#6). Tree audit
found zero real carriers; the two allowance fixtures flip canonical
(dep_test.ww, widget_test.ww). New pins: direnum attest-noncanon
reject row (both-stage stderr parity) and the coordinator
noncanonical_attest_rejected package row.
2806 lines
82 KiB
Plaintext
2806 lines
82 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 os.exec;
|
|
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;
|
|
};
|
|
|
|
fn owncstr(s: str) *u8 = {
|
|
let b: []u8 = alloc([], (s.len + 1): u64)!;
|
|
b.len = s.len + 1;
|
|
let i: i32 = 0;
|
|
for (i < s.len) { b[i] = s[i]; i += 1; };
|
|
b[s.len] = 0u8;
|
|
return b.ptr;
|
|
};
|
|
|
|
// execpackagetests — replace the driver with the native WW package
|
|
// coordinator for directory/default invocations. Explicit generated
|
|
// package.ww roots stay on runsingletest, which is the recursion boundary when
|
|
// wwtest builds its already-aggregated package binaries.
|
|
fn execpackagetests(selfdir: *u8, argv: **u8, argc: i32, start: i32,
|
|
targetindex: i32, resolved: *u8, adddot: bool) i32 = {
|
|
let prog: *u8 = joinpathlit(selfdir, "wwtest");
|
|
match (os.getenv("WW_WWTEST")) {
|
|
case let p: str => {
|
|
if (p.len != 0) { prog = owncstr(p); };
|
|
};
|
|
case void => void;
|
|
};
|
|
|
|
let builder: *u8 = joinpathlit(selfdir, "ww_ww");
|
|
let cap: i32 = argc - start + 6;
|
|
let execargv: []*u8 = alloc([], cap: u64)!;
|
|
execargv.len = cap;
|
|
let n: i32 = 0;
|
|
execargv[n] = prog; n += 1;
|
|
execargv[n] = "package".ptr; n += 1;
|
|
execargv[n] = "--ww-driver".ptr; n += 1;
|
|
execargv[n] = builder; n += 1;
|
|
let dotted: bool = false;
|
|
let i: i32 = start;
|
|
for (i < argc) {
|
|
if (adddot && !dotted && cstreqlit(argv[i], "--")) {
|
|
execargv[n] = ".".ptr; n += 1;
|
|
dotted = true;
|
|
};
|
|
if (resolved != nil && i == targetindex) { execargv[n] = resolved; }
|
|
else { execargv[n] = argv[i]; };
|
|
n += 1;
|
|
i += 1;
|
|
};
|
|
if (adddot && !dotted) { execargv[n] = ".".ptr; n += 1; };
|
|
execargv[n] = nil;
|
|
|
|
let env: []str = os.getenvs();
|
|
let envp: []*u8 = alloc([], (env.len + 1): u64)!;
|
|
envp.len = env.len + 1;
|
|
i = 0;
|
|
for (i < env.len) { envp[i] = owncstr(env[i]); i += 1; };
|
|
envp[env.len] = nil;
|
|
os.execve(pathstr(prog), execargv.ptr, envp.ptr);
|
|
cerr("ww test: cannot exec package test coordinator\n");
|
|
return 1;
|
|
};
|
|
|
|
// ---- 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 sep unit 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 fold it
|
|
// 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;
|
|
};
|
|
|
|
// Go's contract: only *_test.ww is a test source. A line-leading @test
|
|
// declaration anywhere else would be silently dropped by a non-T build
|
|
// (#6, the Hare model), so directory enumeration rejects it loudly.
|
|
// This is the wwstage twin of cmd/ww/main.c:file_has_line_test.
|
|
fn dirfileattest(dirpath: *u8, name: *u8) bool = {
|
|
let path: *u8 = joinpath(dirpath, name);
|
|
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return false; };
|
|
let sr: (i64 | os.oserror) = os.filesize(fd);
|
|
let n: i64 = -1i64;
|
|
match (sr) {
|
|
case let v: i64 => n = v;
|
|
case let e: os.oserror => { os.close(fd); return false; };
|
|
};
|
|
if (n <= 0i64) { os.close(fd); return false; };
|
|
let b: []u8 = alloc([], n: u64)!;
|
|
b.len = n: i32;
|
|
let rr: (i64 | os.oserror) = os.readall(fd, b.ptr, n: u64);
|
|
os.close(fd);
|
|
let got: i64 = -1i64;
|
|
match (rr) {
|
|
case let v: i64 => got = v;
|
|
case let e: os.oserror => return false;
|
|
};
|
|
if (got != n) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < b.len) {
|
|
for (i < b.len && (b[i] == ' ' || b[i] == '\t'
|
|
|| b[i] == '\r')) { i += 1; };
|
|
if (i + 5 < b.len && b[i] == '@' && b[i + 1] == 't'
|
|
&& b[i + 2] == 'e' && b[i + 3] == 's'
|
|
&& b[i + 4] == 't'
|
|
&& (b[i + 5] == ' ' || b[i + 5] == '\t')) {
|
|
return true;
|
|
};
|
|
for (i < b.len && b[i] != '\n') { i += 1; };
|
|
if (i < b.len) { i += 1; };
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// Classify a directory entry for production enumeration: 1 keep,
|
|
// 0 skip (non-source or *_test.ww), -1 @test outside *_test.ww
|
|
// (caller diagnoses and fails).
|
|
fn dirfileclass(dirpath: *u8, name: *u8, nlen: u64) i32 = {
|
|
// 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 0; };
|
|
let s: str;
|
|
s.ptr = name;
|
|
s.len = nlen: i32;
|
|
if (!strings.hassuffix(s, ".ww")) { return 0; };
|
|
if (strings.hassuffix(s, "_test.ww")) { return 0; };
|
|
if (dirfileattest(dirpath, name)) { return -1; };
|
|
return 1;
|
|
};
|
|
|
|
// 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 production *.ww paths of `dirpath` (less *_test.ww
|
|
// test sources), byte-sort. A line-leading @test in any other source is
|
|
// diagnosed here and returns -2. Returns one exact pointer array of
|
|
// NUL-terminated full paths. This is the sole directory-membership
|
|
// discovery path; the owning seppkg retains the list.
|
|
fn enumeratedir(dirpath: *u8) (**u8, i32) = {
|
|
let fd: i32 = os.open(pathstr(dirpath), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return nil: **u8, -1; };
|
|
// #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 package unit 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);
|
|
let cls: i32 = dirfileclass(dirpath, nm, nl);
|
|
if (cls < 0) {
|
|
cerr("ww: ");
|
|
cerr(pathstr(joinpath(dirpath, nm)));
|
|
cerr(": @test declaration outside *_test.ww\n");
|
|
os.close(fd);
|
|
return nil: **u8, -2;
|
|
};
|
|
if (cls > 0) {
|
|
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 full: *u8 = joinpath(dirpath, nm);
|
|
names[n] = full;
|
|
nlens[n] = cstrlen(full);
|
|
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;
|
|
};
|
|
if (n == 0) {
|
|
os.free(names.ptr: *void, (cap: u64) * (size(*u8): u64));
|
|
os.free(nlens.ptr: *void, (cap: u64) * (size(u64): u64));
|
|
return nil: **u8, 0;
|
|
};
|
|
let exact: []*u8 = alloc([], n: u64)!;
|
|
exact.len = n;
|
|
let k: i32 = 0;
|
|
for (k < n) { exact[k] = names[k]; k += 1; };
|
|
// rt_free is currently a no-op, but keep the concrete owner/release
|
|
// shape correct for the driver's allocations.
|
|
os.free(names.ptr: *void, (cap: u64) * (size(*u8): u64));
|
|
os.free(nlens.ptr: *void, (cap: u64) * (size(u64): u64));
|
|
return exact.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 buildonesep'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.)
|
|
type lflags = struct {
|
|
libdirs: **u8,
|
|
nlibdirs: i32,
|
|
libs: **u8,
|
|
nlibs: i32,
|
|
};
|
|
|
|
// ---- separate-compilation driver -------------------------------------
|
|
//
|
|
// Port of cmd/ww/main.c build_one_sep (task #46/c3). The build path
|
|
// materializes each imported package's `.wwi` interface and compiles
|
|
// every package on its own (`w6c -c`), then flat-links the `.o` set.
|
|
// Separate compilation is the SOLE build path (E3-C1 flip, task #87).
|
|
//
|
|
// 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
|
|
name: *u8, // validated declared name; directory packages only
|
|
sources: **u8, // owned, byte-sorted production paths; dirs only
|
|
nsources: i32,
|
|
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: 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].name = nil;
|
|
g.pkg[g.n].sources = nil;
|
|
g.pkg[g.n].nsources = 0;
|
|
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;
|
|
};
|
|
|
|
// Release the package-owned directory-membership lists through one graph
|
|
// cleanup function. rt_free is a no-op in today's no-free runtime, but this
|
|
// records the same ownership boundary as the C bootstrap twin.
|
|
fn sepgraphfree(g: *sepgraph) void = {
|
|
if (g == nil) { return; };
|
|
let i: i32 = 0;
|
|
for (i < g.n) {
|
|
let j: i32 = 0;
|
|
for (j < g.pkg[i].nsources) {
|
|
let p: *u8 = g.pkg[i].sources[j];
|
|
os.free(p: *void, os.PATH_MAX: u64);
|
|
j += 1;
|
|
};
|
|
if (g.pkg[i].sources != nil) {
|
|
os.free(g.pkg[i].sources: *void,
|
|
(g.pkg[i].nsources: u64) * (size(*u8): u64));
|
|
};
|
|
if (g.pkg[i].name != nil) {
|
|
os.free(g.pkg[i].name: *void, cstrlen(g.pkg[i].name) + 1u64);
|
|
};
|
|
i += 1;
|
|
};
|
|
};
|
|
|
|
// 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. 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;
|
|
};
|
|
|
|
fn sepidentstart(c: u8) bool = {
|
|
if (c >= 'a' && c <= 'z') { return true; };
|
|
if (c >= 'A' && c <= 'Z') { return true; };
|
|
return c == '_';
|
|
};
|
|
|
|
fn sepidentcontinue(c: u8) bool = {
|
|
if (sepidentstart(c)) { return true; };
|
|
return c >= '0' && c <= '9';
|
|
};
|
|
|
|
// Skip the whitespace and comments accepted before and within the leading
|
|
// package clause. This is deliberately only the loader's small header
|
|
// grammar, not a second compiler lexer.
|
|
fn sepskipspace(src: *u8, n: u64, start: u64, ok: *bool) u64 = {
|
|
let i: u64 = start;
|
|
*ok = true;
|
|
for (true) {
|
|
for (i < n && (src[i] == ' ' || src[i] == '\t'
|
|
|| src[i] == '\r' || src[i] == '\n')) { i += 1u64; };
|
|
if (i + 1u64 < n && src[i] == '/' && src[i + 1u64] == '/') {
|
|
i += 2u64;
|
|
for (i < n && src[i] != '\n') { i += 1u64; };
|
|
continue;
|
|
};
|
|
if (i + 1u64 < n && src[i] == '/' && src[i + 1u64] == '*') {
|
|
i += 2u64;
|
|
let closed: bool = false;
|
|
for (i + 1u64 < n) {
|
|
if (src[i] == '*' && src[i + 1u64] == '/') {
|
|
i += 2u64;
|
|
closed = true;
|
|
break;
|
|
};
|
|
i += 1u64;
|
|
};
|
|
if (!closed) { *ok = false; return i; };
|
|
continue;
|
|
};
|
|
break;
|
|
};
|
|
return i;
|
|
};
|
|
|
|
// Parse exactly the leading loader grammar `package ident;`.
|
|
fn seppackageclause(src: *u8, n: u64, outp: **u8, outn: *u64) bool = {
|
|
let ok: bool = true;
|
|
let i: u64 = sepskipspace(src, n, 0u64, &ok);
|
|
if (!ok || i + 7u64 >= n) { return false; };
|
|
let word: str = "package";
|
|
let j: i32 = 0;
|
|
for (j < word.len) {
|
|
let ju: u64 = j: u64;
|
|
if (src[i + ju] != word[j]) { return false; };
|
|
j += 1;
|
|
};
|
|
i += 7u64;
|
|
if (i >= n || !(src[i] == ' ' || src[i] == '\t'
|
|
|| src[i] == '\r' || src[i] == '\n')) { return false; };
|
|
i = sepskipspace(src, n, i, &ok);
|
|
if (!ok || i >= n || !sepidentstart(src[i])) { return false; };
|
|
let begin: u64 = i;
|
|
i += 1u64;
|
|
for (i < n && sepidentcontinue(src[i])) { i += 1u64; };
|
|
let end: u64 = i;
|
|
i = sepskipspace(src, n, i, &ok);
|
|
if (!ok || i >= n || src[i] != ';') { return false; };
|
|
*outp = src + begin;
|
|
*outn = end - begin;
|
|
return true;
|
|
};
|
|
|
|
// Scan one already-selected source file for its leading package clause
|
|
// (when it is an owned directory source) and top-level imports. 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, ownedsource: i32) 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: cannot read source\n");
|
|
return -1;
|
|
};
|
|
if (ownedsource != 0) {
|
|
let declared: *u8 = nil;
|
|
let declaredn: u64 = 0u64;
|
|
if (!seppackageclause(bufp, blen, &declared, &declaredn)) {
|
|
cerr("ww: ");
|
|
cerr(pathstr(file));
|
|
cerr(": invalid or missing package clause\n");
|
|
return -1;
|
|
};
|
|
if (g.pkg[pi].name == nil) {
|
|
g.pkg[pi].name = arenadupcstr(declared, declaredn);
|
|
} else { if (bytecmp(g.pkg[pi].name, cstrlen(g.pkg[pi].name),
|
|
declared, declaredn) != 0) {
|
|
cerr("ww: ");
|
|
cerr(pathstr(g.pkg[pi].entry));
|
|
cerr(": conflicting package names ");
|
|
cerr(pathstr(g.pkg[pi].name));
|
|
cerr(" and ");
|
|
os.write(2, declared, declaredn);
|
|
cerr("\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) < 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 (#87): the
|
|
// legacy amalgamator 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");
|
|
return -1;
|
|
};
|
|
};
|
|
};
|
|
i = j + 1u64;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// Load pi once: a directory node takes ownership of its sorted production
|
|
// paths, then the same stored list supplies package-name validation and
|
|
// dependency scanning. Recurse over the resulting edges. `color` doubles as
|
|
// a loaded marker (2); reset to white before topo.
|
|
fn seploadpkg(g: *sepgraph, pi: i32, searchpath: *u8) i32 = {
|
|
if (g.pkg[pi].color == 2) { return 0; };
|
|
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 sources: **u8;
|
|
let nsources: i32;
|
|
sources, nsources = enumeratedir(g.pkg[pi].entry);
|
|
g.pkg[pi].sources = sources;
|
|
g.pkg[pi].nsources = nsources;
|
|
if (g.pkg[pi].nsources == -2) {
|
|
// diagnosed in enumeratedir
|
|
rc = -1;
|
|
} else { if (g.pkg[pi].nsources < 0) {
|
|
cerr("ww: cannot read directory ");
|
|
cerr(pathstr(g.pkg[pi].entry));
|
|
cerr("\n");
|
|
rc = -1;
|
|
} else { if (g.pkg[pi].nsources == 0) {
|
|
cerr("ww: ");
|
|
cerr(pathstr(g.pkg[pi].entry));
|
|
cerr(": directory contains no WW package sources\n");
|
|
rc = -1;
|
|
}; }; };
|
|
let i: i32 = 0;
|
|
for (i < g.pkg[pi].nsources) {
|
|
if (rc == 0) {
|
|
rc = sepscanfile(g, pi, g.pkg[pi].sources[i],
|
|
searchpath, &fv, 1);
|
|
};
|
|
i += 1;
|
|
};
|
|
if (rc == 0 && g.pkg[pi].path[0u64] != 0u8) {
|
|
let plen: u64 = cstrlen(g.pkg[pi].path);
|
|
let leaf: *u8 = g.pkg[pi].path;
|
|
let j: u64 = 0u64;
|
|
for (j < plen) {
|
|
if (g.pkg[pi].path[j] == '.') { leaf = g.pkg[pi].path + j + 1u64; };
|
|
j += 1u64;
|
|
};
|
|
if (!cstreq(g.pkg[pi].name, leaf)) {
|
|
cerr("ww: package ");
|
|
cerr(pathstr(g.pkg[pi].name));
|
|
cerr(" does not match import path ");
|
|
cerr(pathstr(g.pkg[pi].path));
|
|
cerr("\n");
|
|
rc = -1;
|
|
};
|
|
};
|
|
} else {
|
|
rc = sepscanfile(g, pi, g.pkg[pi].entry, searchpath, &fv, 0);
|
|
};
|
|
if (rc < 0) { return rc; };
|
|
let k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
if (seploadpkg(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: 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: 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);
|
|
};
|
|
|
|
// 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. Directory membership comes only from the
|
|
// package node loaded before planning.
|
|
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: 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: 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) {
|
|
let i: i32 = 0;
|
|
for (i < g.pkg[pi].nsources) {
|
|
sepemitbody(u, g.pkg[pi].sources[i], &bv, searchpath,
|
|
g.pkg[pi].path);
|
|
i += 1;
|
|
};
|
|
} 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).
|
|
fn archiveo(objpath: *u8, apath: *u8) i32 = {
|
|
let objp: *u8;
|
|
let objn: u64;
|
|
objp, objn = slurp(objpath);
|
|
if (objp == nil) {
|
|
cerr("ww: cannot read object for archive\n");
|
|
return -1;
|
|
};
|
|
let pad: u64 = 0u64;
|
|
if ((objn & 1u64) != 0u64) { pad = 1u64; };
|
|
// ar(5) fixes the archive magic at 8 bytes and each serialized
|
|
// member header at 60 bytes.
|
|
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: cannot open archive\n");
|
|
return -1;
|
|
};
|
|
os.writeall(fd, out, total);
|
|
os.close(fd);
|
|
return 0;
|
|
};
|
|
|
|
// buildonesep — discover deps, reverse-topo,
|
|
// the transitive producer loop (one `w6c -c -I` per package, dep-first,
|
|
// each dependency `.o` wrapped in its own deterministic per-package `.a`), then a
|
|
// reverse-topo `w6l` of the root `.o` + dependency `.a` set + libwwrt.a.
|
|
// Side files land in a cold `<stem>.sepwork` scratch dir. Twin of cstage
|
|
// build_one_sep.
|
|
// ---- -w workdir freshness ----------------------------------------------
|
|
// A `-w DIR` workdir is a caller-owned persistent package-artifact tree
|
|
// that replaces the fresh `.sepwork` scratch. Staleness is pure content
|
|
// identity, never mtime: a package is reused only when its freshly
|
|
// composed unit byte-equals the committed unit AND the tool copies
|
|
// recorded in the dir byte-equal the live tools — every decision is
|
|
// reproducible by hand with cmp(1) against plain files. Artifacts commit
|
|
// via temp + rename with the unit renamed last, so a killed build can
|
|
// never leave a committed unit vouching for uncommitted artifacts. The
|
|
// caller serializes invocations per workdir and `make clean` reclaims
|
|
// the state. Cstage twin: cmd/ww/main.c file_equal/copy_file_atomic/
|
|
// workdir_stamp_text group.
|
|
|
|
// `.s`/`.wwi` may be legitimately empty (an FFI-only package like rt
|
|
// emits no text), so committed presence is their freshness test; the
|
|
// rename-commit protocol owns integrity. `.o`/`.a` are never empty
|
|
// (ELF/ar headers), so a zero size there is always a torn write.
|
|
fn fileisreg(path: *u8) bool = {
|
|
let fi: os.filestat;
|
|
let ok: bool = false;
|
|
match (os.stat(&fi, pathstr(path))) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32; // S_IFMT
|
|
if (t == os.mode.REG: u32) { ok = true; };
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
return ok;
|
|
};
|
|
|
|
fn filesizenonzero(path: *u8) bool = {
|
|
let fi: os.filestat;
|
|
let ok: bool = false;
|
|
match (os.stat(&fi, pathstr(path))) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32; // S_IFMT
|
|
if (t == os.mode.REG: u32) {
|
|
if (fi.sz > 0u64) { ok = true; };
|
|
};
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
return ok;
|
|
};
|
|
|
|
// Byte equality of two files; absence or IO error is inequality.
|
|
fn fileequal(a: *u8, b: *u8) bool = {
|
|
let fa: i32 = os.open(pathstr(a), os.flag.RDONLY, 0i32);
|
|
if (fa < 0) { return false; };
|
|
let fb: i32 = os.open(pathstr(b), os.flag.RDONLY, 0i32);
|
|
if (fb < 0) { os.close(fa); return false; };
|
|
let bufa: []u8 = alloc([], 65536u64)!;
|
|
bufa.len = 65536;
|
|
let bufb: []u8 = alloc([], 65536u64)!;
|
|
bufb.len = 65536;
|
|
let eq: bool = true;
|
|
let done: bool = false;
|
|
for (!done) {
|
|
let na: i64 = os.read(fa, bufa.ptr, 65536u64);
|
|
let nb: i64 = os.read(fb, bufb.ptr, 65536u64);
|
|
if (na < 0 || na != nb) { eq = false; done = true; }
|
|
else { if (na == 0) { done = true; }
|
|
else {
|
|
let k: u64 = 0u64;
|
|
for (k < (na: u64)) {
|
|
if (bufa[k] != bufb[k]) {
|
|
eq = false; done = true; k = (na: u64);
|
|
};
|
|
k += 1u64;
|
|
};
|
|
}; };
|
|
};
|
|
os.close(fa);
|
|
os.close(fb);
|
|
return eq;
|
|
};
|
|
|
|
// Replace dst with src's bytes via temp + rename, so a torn write can
|
|
// never masquerade as a committed tool copy.
|
|
fn copyfileatomic(src: *u8, dst: *u8) i32 = {
|
|
let tmpp: *u8 = appendlit(dst, ".new");
|
|
let in: i32 = os.open(pathstr(src), os.flag.RDONLY, 0i32);
|
|
if (in < 0) { return -1; };
|
|
let out: i32 = os.open(pathstr(tmpp),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (out < 0) { os.close(in); return -1; };
|
|
let buf: []u8 = alloc([], 65536u64)!;
|
|
buf.len = 65536;
|
|
let bad: bool = false;
|
|
let done: bool = false;
|
|
for (!done) {
|
|
let n: i64 = os.read(in, buf.ptr, 65536u64);
|
|
if (n < 0) { bad = true; done = true; }
|
|
else { if (n == 0) { done = true; }
|
|
else {
|
|
match (os.writeall(out, buf.ptr, n: u64)) {
|
|
case let w: i64 => void;
|
|
case let e: os.oserror => { bad = true; done = true; };
|
|
};
|
|
}; };
|
|
};
|
|
os.close(in);
|
|
if (os.close(out) != 0) { bad = true; };
|
|
if (bad) { return -1; };
|
|
return os.rename(pathstr(tmpp), pathstr(dst));
|
|
};
|
|
|
|
// The stamp pins the non-content build inputs a unit compare cannot see:
|
|
// the -T/-S shape of the producer pass and the artifact protocol
|
|
// revision (bump "fmt" when the unit/archive/commit format changes).
|
|
fn workdirstamptext(istest: i32, emitasm: i32) str = {
|
|
if (istest != 0) {
|
|
if (emitasm != 0) {
|
|
return "ww workdir fmt 1 mode test asm 1\n";
|
|
};
|
|
return "ww workdir fmt 1 mode test asm 0\n";
|
|
};
|
|
if (emitasm != 0) {
|
|
return "ww workdir fmt 1 mode build asm 1\n";
|
|
};
|
|
return "ww workdir fmt 1 mode build asm 0\n";
|
|
};
|
|
|
|
fn stampmatches(path: *u8, want: str) bool = {
|
|
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return false; };
|
|
let buf: []u8 = alloc([], 128u64)!;
|
|
buf.len = 128;
|
|
let n: i64 = os.read(fd, buf.ptr, 127u64);
|
|
os.close(fd);
|
|
if (n < 0) { return false; };
|
|
if ((n: i32) != want.len) { return false; };
|
|
let k: u64 = 0u64;
|
|
let eq: bool = true;
|
|
for (k < (n: u64)) {
|
|
if (buf[k] != want.ptr[k]) { eq = false; k = (n: u64); };
|
|
k += 1u64;
|
|
};
|
|
return eq;
|
|
};
|
|
|
|
fn writestampatomic(path: *u8, want: str) i32 = {
|
|
let tmpp: *u8 = appendlit(path, ".new");
|
|
let fd: i32 = os.open(pathstr(tmpp),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
|
if (fd < 0) { return -1; };
|
|
let bad: bool = false;
|
|
match (os.writeall(fd, want.ptr, want.len: u64)) {
|
|
case let w: i64 => void;
|
|
case let e: os.oserror => { bad = true; };
|
|
};
|
|
if (os.close(fd) != 0) { bad = true; };
|
|
if (bad) { return -1; };
|
|
return os.rename(pathstr(tmpp), pathstr(path));
|
|
};
|
|
|
|
// cerrpath — the "ww: <head><path>\n" diagnostic shape shared by the
|
|
// workdir error sites; byte-identical wording to the cstage twin's
|
|
// fprintf(..., "%s", path) forms.
|
|
fn cerrpath(head: str, path: *u8, tail: str) void = {
|
|
cerr(head);
|
|
os.write(2, path, cstrlen(path));
|
|
cerr(tail);
|
|
};
|
|
|
|
fn buildonesepimpl(selfdir: *u8, src: *u8, entryisdir: i32, out: *u8,
|
|
objstem: *u8, incs: *u8, lf: *lflags, istest: i32, emitasm: i32,
|
|
workdir: *u8, scratchout: **u8, graphout: **sepgraph) 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.
|
|
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.
|
|
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 warm: bool = false;
|
|
if (workdir != nil) {
|
|
if (workdir[0u64] != 0u8) { warm = true; };
|
|
};
|
|
let scratch: *u8 = nil;
|
|
if (warm) {
|
|
let wfi: os.filestat;
|
|
let wok: bool = false;
|
|
match (os.stat(&wfi, pathstr(workdir))) {
|
|
case void => {
|
|
let wt: u32 = (wfi.mode: u32) & 61440u32; // S_IFMT
|
|
if (wt == os.mode.DIR: u32) { wok = true; };
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
if (!wok) {
|
|
cerrpath("ww: workdir ", workdir,
|
|
" is not a directory\n");
|
|
return 1;
|
|
};
|
|
// The workdir is caller-owned and persistent: no acquisition,
|
|
// no refusal, and scratchout stays nil so the wrapper never
|
|
// cleans it.
|
|
scratch = workdir;
|
|
} else {
|
|
scratch = appendlit(effstem, ".sepwork");
|
|
if (os.mkdir(pathstr(scratch), 493i32) != 0) {
|
|
cerr("ww: cannot create scratch\n");
|
|
return 1;
|
|
};
|
|
// Hand the path back only after mkdir succeeds, so the wrapper
|
|
// never removes a pre-existing path that this invocation failed
|
|
// to acquire.
|
|
if (scratchout != nil) { *scratchout = scratch; };
|
|
};
|
|
let staleall: bool = false;
|
|
let stampok: bool = false;
|
|
let toolc: *u8 = nil;
|
|
let toola: *u8 = nil;
|
|
let stampf: *u8 = nil;
|
|
let stampwant: str = "";
|
|
if (warm) {
|
|
toolc = joinpathlit(scratch, ".wwtool.w6c");
|
|
toola = joinpathlit(scratch, ".wwtool.w6a");
|
|
stampf = joinpathlit(scratch, ".wwtool.stamp");
|
|
stampwant = workdirstamptext(istest, emitasm);
|
|
stampok = stampmatches(stampf, stampwant);
|
|
staleall = !stampok;
|
|
if (!staleall) {
|
|
if (!fileequal(toolc, c6)) { staleall = true; };
|
|
};
|
|
if (!staleall) {
|
|
if (emitasm == 0) {
|
|
if (!fileequal(toola, a6)) { staleall = true; };
|
|
};
|
|
};
|
|
};
|
|
|
|
// 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})!;
|
|
if (graphout != nil) { *graphout = g; };
|
|
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
|
|
// seploadpkg pulls test + its transitive deps; the producer adds -T to
|
|
// the root and `test.run` links against test's `.a` — via the
|
|
// sepscanfile dedup-guarded dep append.
|
|
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 (seploadpkg(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 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");
|
|
let apath: *u8 = sepfname(g, pi, scratch, ".a");
|
|
let unitnew: *u8 = sepfname(g, pi, scratch, ".unit.new");
|
|
let wwinew: *u8 = sepfname(g, pi, scratch, ".wwi.new");
|
|
let asmnew: *u8 = sepfname(g, pi, scratch, ".s.new");
|
|
let objnew: *u8 = sepfname(g, pi, scratch, ".o.new");
|
|
let anew: *u8 = sepfname(g, pi, scratch, ".a.new");
|
|
// Warm mode compiles from staged `.new` paths and commits by
|
|
// rename; classic mode keeps its exact in-place paths.
|
|
let cu: *u8 = unitf;
|
|
let cw: *u8 = wwi;
|
|
let cs: *u8 = asmf;
|
|
let co: *u8 = objf;
|
|
let ca: *u8 = apath;
|
|
if (warm) {
|
|
cu = unitnew; cw = wwinew; cs = asmnew;
|
|
co = objnew; ca = anew;
|
|
};
|
|
if (sepcomposeunit(g, pi, scratch, order, norder, searchpath.ptr, cu) < 0) {
|
|
return 1;
|
|
};
|
|
let fresh: bool = false;
|
|
if (warm) {
|
|
if (!staleall) {
|
|
fresh = fileequal(unitnew, unitf);
|
|
if (fresh) { fresh = fileisreg(asmf); };
|
|
if (fresh) {
|
|
if (pi != root) {
|
|
fresh = fileisreg(wwi);
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (emitasm == 0) {
|
|
fresh = filesizenonzero(objf);
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (emitasm == 0 && pi != root) {
|
|
fresh = filesizenonzero(apath);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (os.remove(pathstr(unitnew)) != 0) {
|
|
cerrpath("ww: cannot remove ", unitnew, "\n");
|
|
return 1;
|
|
};
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
{
|
|
// 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` 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: []str = alloc([], alen)!;
|
|
append(argv, "w6c");
|
|
if (roott) { append(argv, "-T"); };
|
|
append(argv, "-c");
|
|
if (pi != root) {
|
|
append(argv, "-I");
|
|
append(argv, pathstr(cw));
|
|
};
|
|
append(argv, "-o");
|
|
append(argv, pathstr(cs));
|
|
append(argv, pathstr(cu));
|
|
let env: []str = os.getenvs();
|
|
let result: exec.result;
|
|
exec.runstdio(pathstr(c6), argv, env, &result);
|
|
if (result.termination != exec.termination.EXIT
|
|
|| result.code != 0) {
|
|
if (result.termination == exec.termination.ERROR
|
|
&& result.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
};
|
|
cerr("ww: w6c failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
if (emitasm == 0) {
|
|
let argv: []str = alloc([], 4u64)!;
|
|
append(argv, "w6a");
|
|
append(argv, "-o");
|
|
append(argv, pathstr(co));
|
|
append(argv, pathstr(cs));
|
|
let env: []str = os.getenvs();
|
|
let result: exec.result;
|
|
exec.runstdio(pathstr(a6), argv, env, &result);
|
|
if (result.termination != exec.termination.EXIT
|
|
|| result.code != 0) {
|
|
if (result.termination == exec.termination.ERROR
|
|
&& result.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
};
|
|
cerr("ww: w6a failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
// 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. The link
|
|
// consumes `.o`/`.a`, never `.wwi`.
|
|
if (emitasm == 0 && pi != root) {
|
|
if (archiveo(co, ca) != 0) {
|
|
cerr("ww: archive failed\n");
|
|
return 1;
|
|
};
|
|
};
|
|
// Commit order: artifacts before the unit that vouches for
|
|
// them, unit strictly last.
|
|
if (warm) {
|
|
let bad: bool = false;
|
|
if (pi != root) {
|
|
if (os.rename(pathstr(wwinew), pathstr(wwi)) != 0) {
|
|
bad = true;
|
|
};
|
|
};
|
|
if (!bad) {
|
|
if (os.rename(pathstr(asmnew), pathstr(asmf)) != 0) {
|
|
bad = true;
|
|
};
|
|
};
|
|
if (!bad) {
|
|
if (emitasm == 0) {
|
|
if (os.rename(pathstr(objnew), pathstr(objf)) != 0) {
|
|
bad = true;
|
|
};
|
|
};
|
|
};
|
|
if (!bad) {
|
|
if (emitasm == 0 && pi != root) {
|
|
if (os.rename(pathstr(anew), pathstr(apath)) != 0) {
|
|
bad = true;
|
|
};
|
|
};
|
|
};
|
|
if (!bad) {
|
|
if (os.rename(pathstr(unitnew), pathstr(unitf)) != 0) {
|
|
bad = true;
|
|
};
|
|
};
|
|
if (bad) {
|
|
if (g.pkg[pi].path[0u64] != 0u8) {
|
|
cerrpath("ww: cannot commit ",
|
|
g.pkg[pi].path, "\n");
|
|
} else {
|
|
cerr("ww: cannot commit (root)\n");
|
|
};
|
|
return 1;
|
|
};
|
|
};
|
|
oi += 1;
|
|
};
|
|
// Tool identity commits only after every package artifact it vouches
|
|
// for is itself committed; a killed pass leaves the old identity and
|
|
// forces a full recompile, never a false reuse.
|
|
if (warm) {
|
|
if (!fileequal(toolc, c6)) {
|
|
if (copyfileatomic(c6, toolc) != 0) {
|
|
cerrpath("ww: cannot record ", toolc, "\n");
|
|
return 1;
|
|
};
|
|
};
|
|
if (emitasm == 0) {
|
|
if (!fileequal(toola, a6)) {
|
|
if (copyfileatomic(a6, toola) != 0) {
|
|
cerrpath("ww: cannot record ", toola, "\n");
|
|
return 1;
|
|
};
|
|
};
|
|
};
|
|
if (!stampok) {
|
|
if (writestampatomic(stampf, stampwant) != 0) {
|
|
cerrpath("ww: cannot record ", stampf, "\n");
|
|
return 1;
|
|
};
|
|
};
|
|
};
|
|
if (emitasm != 0) { return 0; };
|
|
|
|
// Reverse-topo link: root `.o` first (order[norder-1]), dependency `.a`
|
|
// files after, then libwwrt.a (which still
|
|
// selectively pulls only the runtime members a live undef needs).
|
|
// argv: 3 fixed (w6l,-o,out) + one root object/archive per package
|
|
// + 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;
|
|
let linkargs: []str = alloc([], pos: u64)!;
|
|
let ai: i32 = 0;
|
|
for (ai < pos) {
|
|
append(linkargs, pathstr(largv[ai]));
|
|
ai += 1;
|
|
};
|
|
let linkenv: []str = os.getenvs();
|
|
let linkresult: exec.result;
|
|
exec.runstdio(pathstr(l6), linkargs, linkenv, &linkresult);
|
|
if (linkresult.termination != exec.termination.EXIT
|
|
|| linkresult.code != 0) {
|
|
if (linkresult.termination == exec.termination.ERROR
|
|
&& linkresult.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
};
|
|
cerr("ww: w6l failed\n");
|
|
return 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// buildonesep — `ww build` and an explicit `ww test -o` retain caller-visible
|
|
// `.sepwork` artifacts; their caller owns that exact tree. `ww run` and a
|
|
// no-output single-file test remove internal scratch on success and failure.
|
|
// The path is non-nil only after this invocation successfully created the
|
|
// exact tree. Twin of the cstage build_one_sep wrapper.
|
|
fn buildonesep(selfdir: *u8, src: *u8, entryisdir: i32, out: *u8,
|
|
objstem: *u8, incs: *u8, lf: *lflags, istest: i32, emitasm: i32,
|
|
keepscratch: i32, workdir: *u8) i32 = {
|
|
let scratch: *u8 = nil;
|
|
let g: *sepgraph = nil;
|
|
let r: i32 = buildonesepimpl(selfdir, src, entryisdir, out, objstem,
|
|
incs, lf, istest, emitasm, workdir, &scratch, &g);
|
|
sepgraphfree(g);
|
|
if (keepscratch == 0 && scratch != nil) {
|
|
if (cstrendswithlit(scratch, ".sepwork")) {
|
|
let argv: []str = alloc([], 4u64)!;
|
|
append(argv, "rm");
|
|
append(argv, "-rf");
|
|
append(argv, "--");
|
|
append(argv, pathstr(scratch));
|
|
let env: []str = os.getenvs();
|
|
let result: exec.result;
|
|
exec.runstdio("/bin/rm", argv, env, &result);
|
|
if (result.termination != exec.termination.EXIT
|
|
|| result.code != 0) {
|
|
if (result.termination == exec.termination.ERROR
|
|
&& result.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
};
|
|
cerr("ww: cannot remove scratch\n");
|
|
if (r == 0) { r = 1; };
|
|
};
|
|
};
|
|
};
|
|
return r;
|
|
};
|
|
|
|
// ---- 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 [-S] [-w DIR] [-o FILE] [path] compile module; -S stops after package asm\n run [path] ... build then exec, passing extra args to the program\n test [-S -o STEM] [-w DIR] [options] [path] build/run tests; -S emits package asm\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 lib/... every package under lib, recursively (test only)\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 workdir: *u8 = nil; // -w persistent package-artifact workdir
|
|
let emitasm: i32 = 0;
|
|
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, "-S")) {
|
|
emitasm = 1;
|
|
} 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 { if (p[1u64] == 119u8) { // '-w'
|
|
if (p[2u64] != 0u8) {
|
|
workdir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww build: -w needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
workdir = 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, emitasm, 1i32, workdir);
|
|
};
|
|
|
|
// Format the owned driver workspace /tmp/<prefix><pid> into buf. Pid is
|
|
// folded in decimal manually since this driver does not import strconv.
|
|
fn makedrivertmp(buf: *u8, prefix: str) void = {
|
|
let off: u64 = 0u64;
|
|
off = strinto(buf, off, "/tmp/");
|
|
let pk: i32 = 0;
|
|
for (pk < prefix.len) {
|
|
buf[off] = prefix[pk];
|
|
off += 1u64;
|
|
pk += 1;
|
|
};
|
|
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 (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;
|
|
makedrivertmp(tmp.ptr, "ww_run_");
|
|
if (os.mkdir(pathstr(tmp.ptr), 448i32) != 0) {
|
|
cerr("ww: cannot create temporary directory\n");
|
|
return 1;
|
|
};
|
|
let outp: *u8 = joinpathlit(tmp.ptr, "main");
|
|
let lf: lflags;
|
|
lf.libdirs = libdirs.ptr;
|
|
lf.nlibdirs = nlibdirs;
|
|
lf.libs = libs.ptr;
|
|
lf.nlibs = nlibs;
|
|
// The freshly acquired directory owns both main and main.sepwork.
|
|
if (buildonesep(selfdir, resolved, isdir, outp, outp, incs.ptr, &lf,
|
|
0i32, 0i32, 0i32, nil) != 0) {
|
|
let cleanrc: i32 = os.remove(pathstr(outp));
|
|
if (cleanrc != 0 && cleanrc != -2i32) {
|
|
cerr("ww: cannot remove temporary output\n");
|
|
};
|
|
if (os.rmdir(pathstr(tmp.ptr)) != 0) {
|
|
cerr("ww: cannot remove temporary directory\n");
|
|
};
|
|
return 1;
|
|
};
|
|
|
|
// Execute with [tmp, argv[passstart..argc)). The standard process
|
|
// facility inherits stdio and waits only for this user program.
|
|
let nextra: i32 = 0;
|
|
if (passstart >= 0) { nextra = argc - passstart; };
|
|
let execargv: []str = alloc([], (nextra + 1): u64)!;
|
|
append(execargv, pathstr(outp));
|
|
let k: i32 = 0;
|
|
for (k < nextra) {
|
|
append(execargv, pathstr(argv[passstart + k]));
|
|
k += 1;
|
|
};
|
|
let env: []str = os.getenvs();
|
|
let result: exec.result;
|
|
exec.runstdio(pathstr(outp), execargv, env, &result);
|
|
let rc: i32 = 1;
|
|
if (result.termination == exec.termination.EXIT) {
|
|
rc = result.code;
|
|
} else { if (result.termination == exec.termination.ERROR) {
|
|
if (result.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
rc = 127;
|
|
} else {
|
|
cerr("ww: process launch/wait failed\n");
|
|
rc = -1;
|
|
};
|
|
}; };
|
|
let cleanrc: i32 = os.remove(pathstr(outp));
|
|
if (cleanrc != 0 && cleanrc != -2i32) {
|
|
cerr("ww: cannot remove temporary output\n");
|
|
if (rc == 0) { rc = 1; };
|
|
};
|
|
if (os.rmdir(pathstr(tmp.ptr)) != 0) {
|
|
cerr("ww: cannot remove temporary directory\n");
|
|
if (rc == 0) { rc = 1; };
|
|
};
|
|
return rc;
|
|
};
|
|
|
|
// ---- ww test ----------------------------------------------------------
|
|
//
|
|
// Mirrors cmd/ww/main.c:dotest. Explicit regular files retain the bootstrap
|
|
// compatibility route; directory/default requests delegate to wwtest.
|
|
|
|
fn runsingletest(selfdir: *u8, src: *u8, incs: *u8, compileonly: i32,
|
|
emitasm: i32, outstem: *u8, workdir: *u8, pattern: *u8) i32 = {
|
|
let tmp: []u8 = alloc([], (os.PATH_MAX: u64))!;
|
|
tmp.len = os.PATH_MAX;
|
|
// -o redirects the binary + its caller-owned sepwork intermediates
|
|
// (objstem, T3) to <stem>; without -o both are driver-owned /tmp paths.
|
|
let outp: *u8 = nil;
|
|
let objstem: *u8 = nil;
|
|
// owntmp: the driver owns (and must clean) the /tmp workspace; with
|
|
// -o or -w the binary lands in a caller-owned location instead.
|
|
let owntmp: bool = false;
|
|
if (outstem != nil) {
|
|
outp = outstem;
|
|
objstem = outstem;
|
|
} else { if (workdir != nil) {
|
|
// The workdir owns the persistent test binary the same way it
|
|
// owns the package artifacts.
|
|
outp = joinpathlit(workdir, "main");
|
|
objstem = outp;
|
|
} else {
|
|
owntmp = true;
|
|
makedrivertmp(tmp.ptr, "ww_test_");
|
|
if (os.mkdir(pathstr(tmp.ptr), 448i32) != 0) {
|
|
cerr("ww: cannot create temporary directory\n");
|
|
return 1;
|
|
};
|
|
outp = joinpathlit(tmp.ptr, "main");
|
|
objstem = outp;
|
|
}; };
|
|
// 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 keep: i32 = 0;
|
|
if (outstem != nil) { keep = 1; };
|
|
let bres: i32 = buildonesep(selfdir, src, 0, outp, objstem, incs, &lf,
|
|
1i32, emitasm, keep, workdir);
|
|
if (bres != 0) {
|
|
if (owntmp) {
|
|
let cleanrc: i32 = os.remove(pathstr(outp));
|
|
if (cleanrc != 0 && cleanrc != -2i32) {
|
|
cerr("ww: cannot remove temporary output\n");
|
|
};
|
|
if (os.rmdir(pathstr(tmp.ptr)) != 0) {
|
|
cerr("ww: cannot remove temporary directory\n");
|
|
};
|
|
};
|
|
return 1;
|
|
};
|
|
if (compileonly != 0 || emitasm != 0) {
|
|
if (owntmp) {
|
|
let cleanbad: bool = false;
|
|
let cleanrc: i32 = os.remove(pathstr(outp));
|
|
if (cleanrc != 0 && cleanrc != -2i32) {
|
|
cerr("ww: cannot remove temporary output\n");
|
|
cleanbad = true;
|
|
};
|
|
if (os.rmdir(pathstr(tmp.ptr)) != 0) {
|
|
cerr("ww: cannot remove temporary directory\n");
|
|
cleanbad = true;
|
|
};
|
|
if (cleanbad) { return 1; };
|
|
};
|
|
return 0;
|
|
};
|
|
// #17 fnmatch filter: forward `pattern` as argv[1] so lib/test run()
|
|
// reads it via os.args. cstage twin: run_test_bin.
|
|
let execargv: []str = alloc([], 2u64)!;
|
|
append(execargv, pathstr(outp));
|
|
if (pattern != nil) {
|
|
append(execargv, pathstr(pattern));
|
|
};
|
|
let env: []str = os.getenvs();
|
|
let result: exec.result;
|
|
exec.runstdio(pathstr(outp), execargv, env, &result);
|
|
let rc: i32 = 1;
|
|
if (result.termination == exec.termination.EXIT) {
|
|
rc = result.code;
|
|
} else { if (result.termination == exec.termination.ERROR) {
|
|
if (result.code == 127) {
|
|
cerr("ww: execve failed\n");
|
|
rc = 127;
|
|
} else {
|
|
cerr("ww: process launch/wait failed\n");
|
|
rc = -1;
|
|
};
|
|
}; };
|
|
if (owntmp) {
|
|
let cleanrc: i32 = os.remove(pathstr(outp));
|
|
if (cleanrc != 0 && cleanrc != -2i32) {
|
|
cerr("ww: cannot remove temporary output\n");
|
|
if (rc == 0) { rc = 1; };
|
|
};
|
|
if (os.rmdir(pathstr(tmp.ptr)) != 0) {
|
|
cerr("ww: cannot remove temporary directory\n");
|
|
if (rc == 0) { rc = 1; };
|
|
};
|
|
};
|
|
return rc;
|
|
};
|
|
|
|
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;
|
|
let targetindex: i32 = -1;
|
|
// #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 sep unit WITHOUT running it, for the byte-id gates. cstage
|
|
// twin: cmd/ww/main.c do_test (error wording identical).
|
|
let compileonly: i32 = 0;
|
|
let emitasm: i32 = 0;
|
|
let outstem: *u8 = nil;
|
|
let workdir: *u8 = nil;
|
|
let packageopts: bool = false;
|
|
let afterdash: bool = false;
|
|
let i: i32 = start;
|
|
for (i < argc) {
|
|
let p: *u8 = argv[i];
|
|
if (afterdash) { i += 1; continue; };
|
|
if (p[0u64] == 45u8) { // '-'
|
|
if (cstreqlit(p, "--")) {
|
|
packageopts = true; afterdash = true; i += 1; continue;
|
|
};
|
|
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);
|
|
i += 1; continue;
|
|
};
|
|
if (cstreqlit(p, "-c")) { compileonly = 1; i += 1; continue; };
|
|
if (cstreqlit(p, "-S")) { emitasm = 1; i += 1; continue; };
|
|
if (cstreqlit(p, "-list")) {
|
|
packageopts = true; i += 1; continue;
|
|
};
|
|
if (cstreqlit(p, "-j") || cstreqlit(p, "-run")
|
|
|| cstreqlit(p, "-filter")) {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww test: "); os.write(2, p, cstrlen(p));
|
|
cerr(" needs an argument\n"); return 2;
|
|
};
|
|
packageopts = true; i += 2; continue;
|
|
};
|
|
let ps: str = pathstr(p);
|
|
if (strings.hasprefix(ps, "-timeout-ms=")
|
|
&& ps.len > 12) {
|
|
packageopts = true; i += 1; continue;
|
|
};
|
|
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];
|
|
};
|
|
i += 1; continue;
|
|
};
|
|
if (p[1u64] == 119u8) { // '-w'
|
|
if (p[2u64] != 0u8) {
|
|
workdir = p + 2u64;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
cerr("ww test: -w needs an argument\n");
|
|
return 2;
|
|
};
|
|
i += 1;
|
|
workdir = argv[i];
|
|
};
|
|
i += 1; continue;
|
|
};
|
|
cerr("ww test: unknown flag\n"); return 2;
|
|
} else {
|
|
if (target == nil) { target = p; targetindex = i; }
|
|
else { if (patarg == nil) { patarg = p; }; };
|
|
};
|
|
i += 1;
|
|
};
|
|
if (target == nil) {
|
|
let dot: [2]u8 = ['.': u8, 0u8];
|
|
target = &dot[0];
|
|
};
|
|
if (emitasm != 0 && outstem == nil) {
|
|
cerr("ww test: -S needs -o\n");
|
|
return 2;
|
|
};
|
|
|
|
// Go's ./... form: a trailing "..." element is a package-tree
|
|
// request for the coordinator, never a literal path — recognized
|
|
// before stat, with the directory-mode rejects.
|
|
let tlen: u64 = cstrlen(target);
|
|
let istree: bool = cstreqlit(target, "...");
|
|
if (!istree && tlen >= 4u64) {
|
|
istree = target[tlen - 4u64] == '/'
|
|
&& target[tlen - 3u64] == '.'
|
|
&& target[tlen - 2u64] == '.'
|
|
&& target[tlen - 1u64] == '.';
|
|
};
|
|
if (istree) {
|
|
if (emitasm != 0) {
|
|
cerr("ww test: -S needs a single test file\n");
|
|
return 2;
|
|
};
|
|
// -c -o forwards: the coordinator names the single
|
|
// package's artifact and rejects a multi-package fan-out.
|
|
if (outstem != nil && compileonly == 0) {
|
|
cerr("ww test: -o needs -c for a package target\n");
|
|
return 2;
|
|
};
|
|
if (patarg != nil) {
|
|
cerr("ww test: pattern needs a single test file\n");
|
|
return 2;
|
|
};
|
|
// -w forwards: the coordinator keys one persistent driver
|
|
// workdir per package group under the given root.
|
|
return execpackagetests(selfdir, argv, argc, start,
|
|
targetindex, nil, false);
|
|
};
|
|
|
|
let resolved: *u8 = target;
|
|
let isdir: i32 = 0;
|
|
let found: bool = false;
|
|
let fi: os.filestat;
|
|
match (os.stat(&fi, pathstr(target))) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32;
|
|
if (t == os.mode.DIR: u32) { isdir = 1; found = true; }
|
|
else { if (t == os.mode.REG: u32) { found = true; }; };
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
if (!found) {
|
|
resolved = resolvemodule(selfdir, target, incs.ptr, &isdir);
|
|
if (resolved == nil) {
|
|
cerr("ww test: cannot find ");
|
|
os.write(2, target, cstrlen(target)); cerr("\n");
|
|
return 1;
|
|
};
|
|
};
|
|
if (isdir == 0) {
|
|
if (packageopts) {
|
|
cerr("ww test: package options need a directory\n"); return 2;
|
|
};
|
|
return runsingletest(selfdir, resolved, incs.ptr, compileonly,
|
|
emitasm, outstem, workdir, patarg);
|
|
};
|
|
|
|
if (emitasm != 0) {
|
|
cerr("ww test: -S needs a single test file\n");
|
|
return 2;
|
|
};
|
|
if (outstem != nil && compileonly == 0) {
|
|
cerr("ww test: -o needs -c for a package target\n");
|
|
return 2;
|
|
};
|
|
if (patarg != nil) {
|
|
cerr("ww test: pattern needs a single test file\n");
|
|
return 2;
|
|
};
|
|
let replacement: *u8 = nil;
|
|
if (resolved != target) { replacement = resolved; };
|
|
return execpackagetests(selfdir, argv, argc, start, targetindex,
|
|
replacement, targetindex < 0);
|
|
};
|
|
|
|
// ---- 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;
|
|
};
|