3724 lines
107 KiB
Plaintext
3724 lines
107 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/. WW_W6C / WW_W6A / WW_W6L select exact executable paths.
|
|
// Source-library and runtime-library overrides are not yet supported.
|
|
|
|
package main;
|
|
|
|
import os;
|
|
import os.exec;
|
|
import rt;
|
|
import strings;
|
|
import syntax;
|
|
|
|
// 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;
|
|
|
|
// 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);
|
|
};
|
|
|
|
fn cerrnum(v: i32) void = {
|
|
let digits: [16]u8;
|
|
let n: i32 = 0;
|
|
let x: i32 = v;
|
|
if (x <= 0) { digits[n] = '0'; n += 1; }
|
|
else {
|
|
for (x > 0) {
|
|
digits[n] = ((x % 10) + ('0': i32)): u8;
|
|
n += 1;
|
|
x = x / 10;
|
|
};
|
|
};
|
|
for (n > 0) { n -= 1; os.write(2, &digits[n], 1u64); };
|
|
};
|
|
|
|
fn cerrpos(file: str, line: i32, col: i32) void = {
|
|
cerr(file); cerr(":"); cerrnum(line); cerr(":"); cerrnum(col);
|
|
};
|
|
|
|
fn cstrlen(p: *u8) u64 = {
|
|
let n: u64 = 0u64;
|
|
for (p[n] != 0u8) { n += 1u64; };
|
|
return n;
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
|
|
fn cstreqlit(a: *u8, lit: str) bool = {
|
|
return strings.compare(pathstr(a), lit) == 0;
|
|
};
|
|
|
|
fn bytecpy(dst: *u8, src: *u8, n: u64) void = {
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
dst[i] = src[i];
|
|
i += 1u64;
|
|
};
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
fn byteinto(dst: *u8, off: u64, c: u8) u64 = {
|
|
dst[off] = c;
|
|
return off + 1u64;
|
|
};
|
|
|
|
fn cstrseal(dst: *u8, off: u64) void = {
|
|
dst[off] = 0u8;
|
|
};
|
|
|
|
fn selfdirinto(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
|
let n: u64 = cstrlen(argv0);
|
|
let cut: u64 = n;
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
if (argv0[i] == 47u8) { cut = i; };
|
|
i += 1u64;
|
|
};
|
|
if (cut == n) {
|
|
dst[0u64] = 46u8;
|
|
dst[1u64] = 0u8;
|
|
return;
|
|
};
|
|
if (cut == 0u64) { cut = 1u64; };
|
|
if (cut + 1u64 >= dstsz) { cut = dstsz - 2u64; };
|
|
bytecpy(dst, argv0, cut);
|
|
dst[cut] = 0u8;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
fn toolpath(selfdir: *u8, envvar: str, name: str) *u8 = {
|
|
match (os.getenv(envvar)) {
|
|
case let p: str => {
|
|
if (p.len != 0) { return owncstr(p); };
|
|
};
|
|
case void => void;
|
|
};
|
|
return joinpathlit(selfdir, name);
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
|
|
// 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. Ask the compiler parser,
|
|
// rather than a textual attribute scan, whether a production source contains
|
|
// @test; otherwise valid whitespace/comments could silently drop a test.
|
|
fn dirfileattest(dirpath: *u8, name: *u8) i32 = {
|
|
let path: *u8 = joinpath(dirpath, name);
|
|
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return -1; };
|
|
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 -1; };
|
|
};
|
|
if (n < 0i64) { os.close(fd); return -1; };
|
|
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 -1;
|
|
};
|
|
if (got != n) { return -1; };
|
|
// Preserve the directory loader's normalized package-clause diagnostic
|
|
// before the full parser performs language-level recovery. This is also
|
|
// the C/WW parity boundary for malformed clauses.
|
|
let il: syntax.lex;
|
|
syntax.lexinit(&il, pathstr(path), b.ptr, n: u64);
|
|
let ips: syntax.parser;
|
|
syntax.parserinit(&ips, &il);
|
|
let imports: *syntax.node = syntax.parseimports(&ips);
|
|
if (il.errs > 0 || ips.errs > 0) { return -1; };
|
|
if (imports.nmod.len == 0) {
|
|
cerrpos(pathstr(path), 1, 1);
|
|
cerr(": error: invalid or missing package clause\n");
|
|
return -1;
|
|
};
|
|
let l: syntax.lex;
|
|
syntax.lexinit(&l, pathstr(path), b.ptr, n: u64);
|
|
let ps: syntax.parser;
|
|
syntax.parserinit(&ps, &l);
|
|
let f: *syntax.node = syntax.parsefile(&ps);
|
|
if (l.errs > 0 || ps.errs > 0) { return -1; };
|
|
let d: *syntax.node = f.list;
|
|
for (d != nil) {
|
|
if (d.kind == syntax.nkind.N_FNDECL) {
|
|
let at: *syntax.node = d.attr;
|
|
for (at != nil) {
|
|
if (at.kind == syntax.nkind.N_ATTR
|
|
&& syntax.streq(at.str, "test")) {
|
|
return 1;
|
|
};
|
|
at = at.next;
|
|
};
|
|
};
|
|
d = d.next;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
def SEP_VARIANT_PRODUCTION: i32 = 0;
|
|
def SEP_VARIANT_SAME_TEST: i32 = 1;
|
|
def SEP_VARIANT_EXTERNAL: i32 = 2;
|
|
def SEP_ROLE_NORMAL: i32 = 0;
|
|
def SEP_ROLE_EXTERNAL_PRODUCTION: i32 = 1;
|
|
def SEP_ROLE_TEST_SUPPORT: i32 = 2;
|
|
def SEP_TEST_SUPPORT_MODULE: str = "__wwtest";
|
|
def SEP_MAXPRODUCT: i32 = 256;
|
|
def SEP_MAXCONTEXT: i32 = 257;
|
|
def SEP_ARTIFACT_MAX: i32 = 1024;
|
|
|
|
// Classify a selected directory entry: 1 production, 2 test, 0 skipped,
|
|
// -1 @test outside *_test.ww, -2 non-regular source.
|
|
fn dirfileclass(dirpath: *u8, name: *u8, nlen: u64,
|
|
variant: i32) 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; };
|
|
let istest: bool = strings.hassuffix(s, "_test.ww");
|
|
if (istest && variant == SEP_VARIANT_PRODUCTION) { return 0; };
|
|
if (!istest && variant == SEP_VARIANT_EXTERNAL) { return 0; };
|
|
let source: *u8 = joinpath(dirpath, name);
|
|
let fi: os.filestat;
|
|
let sr: (void | os.oserror) = os.lstat(&fi, pathstr(source));
|
|
let regular: bool = false;
|
|
match (sr) {
|
|
case void => {
|
|
let t: u32 = (fi.mode: u32) & 61440u32;
|
|
if (t == os.mode.REG: u32) { regular = true; };
|
|
};
|
|
case let e: os.oserror => void;
|
|
};
|
|
if (!regular) {
|
|
cerr("ww: "); cerr(pathstr(source));
|
|
cerr(": package source is not a regular file\n");
|
|
return -2;
|
|
};
|
|
if (!istest) {
|
|
let attest: i32 = dirfileattest(dirpath, name);
|
|
if (attest < 0) { return -2; };
|
|
if (attest > 0) { return -1; };
|
|
};
|
|
if (istest) { return 2; };
|
|
return 1;
|
|
};
|
|
|
|
fn dirpackagename(path: *u8) *u8 = {
|
|
let view: str;
|
|
view.ptr = path;
|
|
view.len = cstrlen(path): i32;
|
|
let bufp: *u8;
|
|
let blen: u64;
|
|
bufp, blen = slurp(path);
|
|
if (bufp == nil) {
|
|
cerr("ww: cannot read source\n");
|
|
return nil;
|
|
};
|
|
let l: syntax.lex;
|
|
syntax.lexinit(&l, view, bufp, blen);
|
|
let ps: syntax.parser;
|
|
syntax.parserinit(&ps, &l);
|
|
let imports: *syntax.node = syntax.parseimports(&ps);
|
|
if (l.errs > 0 || ps.errs > 0) { return nil; };
|
|
if (imports.nmod.len == 0) {
|
|
cerrpos(view, 1, 1);
|
|
cerr(": error: invalid or missing package clause\n");
|
|
return nil;
|
|
};
|
|
if (imports.nmod.len >= 256) {
|
|
cerrpos(imports.file, imports.line, imports.col);
|
|
cerr(": error: package name is too long\n");
|
|
return nil;
|
|
};
|
|
return arenadupcstr(imports.nmod.ptr, imports.nmod.len: u64);
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
|
|
// Enumerate a production, same-test, or external-test directory variant.
|
|
// Production files precede matching test files; each partition is byte-sorted.
|
|
fn enumeratedir(dirpath: *u8, variant: i32,
|
|
testpackage: *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 kinds: []i32 = 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, variant);
|
|
if (cls == -1) {
|
|
cerr("ww: ");
|
|
cerr(pathstr(joinpath(dirpath, nm)));
|
|
cerr(": @test declaration outside *_test.ww\n");
|
|
os.close(fd);
|
|
return nil: **u8, -2;
|
|
};
|
|
if (cls == -2) {
|
|
os.close(fd);
|
|
return nil: **u8, -2;
|
|
};
|
|
if (cls > 0) {
|
|
let full: *u8 = joinpath(dirpath, nm);
|
|
if (cls == 2) {
|
|
let pn: *u8 = dirpackagename(full);
|
|
if (pn == nil) {
|
|
os.close(fd);
|
|
return nil: **u8, -2;
|
|
};
|
|
if (testpackage == nil || !cstreq(pn, testpackage)) {
|
|
off += reclen;
|
|
continue;
|
|
};
|
|
};
|
|
if (n >= cap) {
|
|
let ncap: i32 = cap * 2;
|
|
let nn: []*u8 = alloc([], ncap: u64)!;
|
|
let nl2: []u64 = alloc([], ncap: u64)!;
|
|
let nk: []i32 = alloc([], ncap: u64)!;
|
|
let k: i32 = 0;
|
|
for (k < n) {
|
|
nn[k] = names[k];
|
|
nl2[k] = nlens[k];
|
|
nk[k] = kinds[k];
|
|
k += 1;
|
|
};
|
|
names = nn;
|
|
nlens = nl2;
|
|
kinds = nk;
|
|
cap = ncap;
|
|
};
|
|
names[n] = full;
|
|
nlens[n] = cstrlen(full);
|
|
kinds[n] = cls;
|
|
n += 1;
|
|
};
|
|
off += reclen;
|
|
};
|
|
r = os.getdents64(fd, buf.ptr, 8192u64);
|
|
};
|
|
os.close(fd);
|
|
// A failed directory read is an ERROR, not EOF: mid-walk it
|
|
// silently truncated the package source list, and on the first
|
|
// read it was misdiagnosed as "directory contains no WW package
|
|
// sources". -1 routes the caller's "cannot read directory" arm
|
|
// (the cstage caller mapping).
|
|
if (r < 0i64) {
|
|
return nil: **u8, -1;
|
|
};
|
|
|
|
// Insertion sort, byte-wise. n is small (≤16 in practice).
|
|
let i: i32 = 1;
|
|
for (i < n) {
|
|
let j: i32 = i;
|
|
for (j > 0) {
|
|
let c: i32 = kinds[j - 1] - kinds[j];
|
|
if (c == 0) {
|
|
c = bytecmp(names[j - 1], nlens[j - 1],
|
|
names[j], nlens[j]);
|
|
};
|
|
if (c <= 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;
|
|
let tk: i32 = kinds[j];
|
|
kinds[j] = kinds[j - 1];
|
|
kinds[j - 1] = tk;
|
|
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));
|
|
os.free(kinds.ptr: *void, (cap: u64) * (size(i32): 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));
|
|
os.free(kinds.ptr: *void, (cap: u64) * (size(i32): u64));
|
|
return exact.ptr, n;
|
|
};
|
|
|
|
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);
|
|
let closed: i32 = 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 || closed != 0) { return nil, 0u64; };
|
|
buf[nu] = 0u8;
|
|
return buf.ptr, nu;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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,
|
|
};
|
|
|
|
// 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.
|
|
//
|
|
// Every direct dep is tagged by its FULL DOTTED import path on prepend
|
|
// (`//ww:module <path>`), so the definer's qualified symbol equals the
|
|
// consumer's qualified reference and the sep `.o`s link. A dependency's
|
|
// compiler-owned `.wwi` carries its reachable public foreign type facts;
|
|
// separately prepending transitive interfaces is neither necessary nor
|
|
// allowed. The unit composition is byte-identical to the cstage driver.
|
|
|
|
def SEP_MAXPKG: i32 = 256;
|
|
|
|
type sepbind = struct {
|
|
kind: u8,
|
|
name: str,
|
|
target: *u8,
|
|
};
|
|
|
|
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
|
|
artifact: *u8, // non-importable product-root artifact key
|
|
name: *u8, // validated declared name; directory packages only
|
|
testpackage: *u8,
|
|
sources: **u8, // owned, byte-sorted selected paths; dirs only
|
|
nsources: i32,
|
|
isdir: i32,
|
|
variant: i32,
|
|
role: i32,
|
|
root: bool,
|
|
failed: bool,
|
|
testsupport: bool,
|
|
loaded: bool,
|
|
emitcontext: i32,
|
|
contextstate: []u8,
|
|
bindings: []sepbind,
|
|
deps: []i32, // direct-dep indices into sepgraph.pkg
|
|
ndeps: i32,
|
|
color: i32, // tri-color DFS: 0 white, 1 gray, 2 black
|
|
};
|
|
|
|
type sepcontext = struct {
|
|
root: *u8,
|
|
searchpath: *u8,
|
|
};
|
|
|
|
type sepgraph = struct {
|
|
pkg: []seppkg, // alloc'd SEP_MAXPKG
|
|
n: i32,
|
|
context: []sepcontext,
|
|
ncontext: i32,
|
|
supportcontext: i32,
|
|
};
|
|
|
|
type sepproduct = struct {
|
|
dir: *u8,
|
|
out: *u8,
|
|
testpackage: *u8,
|
|
status: *u8,
|
|
artifact: *u8,
|
|
variant: i32,
|
|
context: i32,
|
|
root: i32,
|
|
};
|
|
|
|
fn sepfindoraddvariant(g: *sepgraph, path: *u8, entry: *u8,
|
|
isdir: i32, variant: i32, testpackage: *u8, role: i32,
|
|
artifact: *u8, root: bool) i32 = {
|
|
if (cstrlen(path) >= 256u64) {
|
|
cerr("ww: package path is too long (limit 255 bytes)\n");
|
|
return -1;
|
|
};
|
|
let i: i32 = 0;
|
|
for (i < g.n) {
|
|
let samelocation: bool = os.samefile(pathstr(g.pkg[i].entry),
|
|
pathstr(entry));
|
|
if (root || g.pkg[i].root) {
|
|
if (root && g.pkg[i].root) {
|
|
if (!samelocation) { i += 1; continue; };
|
|
if (variant != g.pkg[i].variant) { i += 1; continue; };
|
|
cerr("ww: duplicate package-test root ");
|
|
cerr(pathstr(entry)); cerr("\n");
|
|
return -1;
|
|
};
|
|
// A cycle back to an ordinary production root reuses that root after
|
|
// loading assigns its declared identity. Test roots stay distinct.
|
|
if (samelocation
|
|
&& variant == SEP_VARIANT_PRODUCTION
|
|
&& g.pkg[i].variant == SEP_VARIANT_PRODUCTION
|
|
&& cstreq(g.pkg[i].path, path)) {
|
|
return i;
|
|
};
|
|
if (!samelocation) { i += 1; continue; };
|
|
let rootvariant: i32 = variant;
|
|
let productionvariant: i32 = g.pkg[i].variant;
|
|
if (!root) {
|
|
rootvariant = g.pkg[i].variant;
|
|
productionvariant = variant;
|
|
};
|
|
if (isdir != 0 && g.pkg[i].isdir != 0
|
|
&& rootvariant != SEP_VARIANT_PRODUCTION
|
|
&& productionvariant == SEP_VARIANT_PRODUCTION) {
|
|
i += 1;
|
|
continue;
|
|
};
|
|
cerr("ww: package directory "); cerr(pathstr(entry));
|
|
cerr(" has incompatible root and production variants\n");
|
|
return -1;
|
|
};
|
|
let samepath: bool = cstreq(g.pkg[i].path, path);
|
|
let sameartifact: bool = true;
|
|
if (role == SEP_ROLE_EXTERNAL_PRODUCTION) {
|
|
sameartifact = g.pkg[i].artifact != nil && artifact != nil
|
|
&& cstreq(g.pkg[i].artifact, artifact);
|
|
};
|
|
let sameaction: bool = samepath && samelocation
|
|
&& g.pkg[i].variant == variant && g.pkg[i].role == role
|
|
&& sameartifact;
|
|
if (sameaction) { return i; };
|
|
// An external test consumes the one canonical production action for
|
|
// its directory. The role only separates physically different
|
|
// packages that happen to use the same source qualifier.
|
|
if (samepath && samelocation
|
|
&& g.pkg[i].variant == SEP_VARIANT_PRODUCTION
|
|
&& variant == SEP_VARIANT_PRODUCTION
|
|
&& ((role == SEP_ROLE_EXTERNAL_PRODUCTION
|
|
&& g.pkg[i].role == SEP_ROLE_NORMAL)
|
|
|| (role == SEP_ROLE_NORMAL
|
|
&& g.pkg[i].role == SEP_ROLE_EXTERNAL_PRODUCTION))) {
|
|
return i;
|
|
};
|
|
if (samepath && (role == SEP_ROLE_EXTERNAL_PRODUCTION
|
|
|| g.pkg[i].role == SEP_ROLE_EXTERNAL_PRODUCTION)) {
|
|
i += 1;
|
|
continue;
|
|
};
|
|
if (samepath) {
|
|
if (!samelocation || g.pkg[i].variant != variant
|
|
|| g.pkg[i].role != role) {
|
|
cerr("ww: package "); cerr(pathstr(path));
|
|
cerr(" resolves to more than one location\n");
|
|
return -1;
|
|
};
|
|
return i;
|
|
};
|
|
if (samelocation) {
|
|
if (role == SEP_ROLE_TEST_SUPPORT
|
|
|| g.pkg[i].role == SEP_ROLE_TEST_SUPPORT) {
|
|
i += 1;
|
|
continue;
|
|
};
|
|
cerr("ww: package directory "); cerr(pathstr(entry));
|
|
cerr(" has identities ");
|
|
if (g.pkg[i].path[0u64] == 0u8) { cerr("(root)"); }
|
|
else { cerr(pathstr(g.pkg[i].path)); };
|
|
cerr(" and ");
|
|
if (path[0u64] == 0u8) { cerr("(root)"); }
|
|
else { cerr(pathstr(path)); };
|
|
cerr("\n");
|
|
return -1;
|
|
};
|
|
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].artifact = artifact;
|
|
g.pkg[g.n].name = nil;
|
|
g.pkg[g.n].testpackage = nil;
|
|
if (testpackage != nil) {
|
|
g.pkg[g.n].testpackage = arenadupcstr(testpackage,
|
|
cstrlen(testpackage));
|
|
};
|
|
g.pkg[g.n].sources = nil;
|
|
g.pkg[g.n].nsources = 0;
|
|
g.pkg[g.n].isdir = isdir;
|
|
g.pkg[g.n].variant = variant;
|
|
g.pkg[g.n].role = role;
|
|
g.pkg[g.n].root = root;
|
|
g.pkg[g.n].failed = false;
|
|
g.pkg[g.n].testsupport = false;
|
|
g.pkg[g.n].loaded = false;
|
|
g.pkg[g.n].emitcontext = -1;
|
|
let cslot: []u8 = alloc([], SEP_MAXCONTEXT: u64)!;
|
|
cslot.len = SEP_MAXCONTEXT;
|
|
g.pkg[g.n].contextstate = cslot;
|
|
let emptybindings: []sepbind;
|
|
g.pkg[g.n].bindings = emptybindings;
|
|
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;
|
|
};
|
|
|
|
fn sepfindoradd(g: *sepgraph, path: *u8, entry: *u8, isdir: i32) i32 = {
|
|
return sepfindoraddvariant(g, path, entry, isdir,
|
|
SEP_VARIANT_PRODUCTION, nil, SEP_ROLE_NORMAL, nil, false);
|
|
};
|
|
|
|
fn sepfindoraddrole(g: *sepgraph, path: *u8, entry: *u8, isdir: i32,
|
|
role: i32, artifact: *u8) i32 = {
|
|
return sepfindoraddvariant(g, path, entry, isdir,
|
|
SEP_VARIANT_PRODUCTION, nil, role, artifact, false);
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
};
|
|
|
|
fn sepcontextfor(g: *sepgraph, root: *u8, incs: *u8,
|
|
toolsrcdir: *u8) i32 = {
|
|
let cap: u64 = (os.PATH_MAX: u64) * 2u64;
|
|
let need: u64 = cstrlen(root) + 1u64 + cstrlen(toolsrcdir) + 1u64;
|
|
if (incs != nil && incs[0u64] != 0u8) {
|
|
need += cstrlen(incs) + 1u64;
|
|
};
|
|
if (need > cap) {
|
|
cerr("ww: package import search path is too long\n");
|
|
return -1;
|
|
};
|
|
let search: []u8 = alloc([], cap)!;
|
|
search.len = cap: i32;
|
|
let off: u64 = cstrinto(search.ptr, 0u64, root);
|
|
off = byteinto(search.ptr, off, 58u8);
|
|
if (incs != nil && incs[0u64] != 0u8) {
|
|
off = cstrinto(search.ptr, off, incs);
|
|
off = byteinto(search.ptr, off, 58u8);
|
|
};
|
|
off = cstrinto(search.ptr, off, toolsrcdir);
|
|
cstrseal(search.ptr, off);
|
|
let i: i32 = 0;
|
|
for (i < g.ncontext) {
|
|
if (cstreq(g.context[i].searchpath, search.ptr)) { return i; };
|
|
i += 1;
|
|
};
|
|
if (g.ncontext >= SEP_MAXCONTEXT) {
|
|
cerr("ww: too many package import contexts\n");
|
|
return -1;
|
|
};
|
|
g.context[g.ncontext].root = root;
|
|
g.context[g.ncontext].searchpath = search.ptr;
|
|
let result: i32 = g.ncontext;
|
|
g.ncontext += 1;
|
|
return result;
|
|
};
|
|
|
|
// Build one artifact path. Test roots use distinct names even though both
|
|
// compiler units reset to the bare executable namespace.
|
|
fn sepfname(g: *sepgraph, pi: i32, scratch: *u8, suffix: str) *u8 = {
|
|
let buf: []u8 = alloc([], SEP_ARTIFACT_MAX: u64)!;
|
|
buf.len = SEP_ARTIFACT_MAX;
|
|
let off: u64 = cstrinto(buf.ptr, 0u64, scratch);
|
|
off = byteinto(buf.ptr, off, 47u8); // '/'
|
|
if (g.pkg[pi].artifact != nil) {
|
|
off = cstrinto(buf.ptr, off, g.pkg[pi].artifact);
|
|
} else { 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;
|
|
};
|
|
|
|
// sepfname uses SEP_ARTIFACT_MAX storage. Validate the longest suffix once
|
|
// before opening files so two action identities can never alias by truncation.
|
|
fn sepvalidateartifactpaths(g: *sepgraph, scratch: *u8) i32 = {
|
|
let i: i32 = 0;
|
|
for (i < g.n) {
|
|
let base: *u8 = g.pkg[i].artifact;
|
|
if (base == nil) {
|
|
base = g.pkg[i].path;
|
|
if (base[0u64] == 0u8) { base = "__root\0".ptr; };
|
|
};
|
|
let need: u64 = cstrlen(scratch) + 1u64 + cstrlen(base)
|
|
+ ".unit.new".len: u64 + 1u64;
|
|
if (need > SEP_ARTIFACT_MAX: u64) {
|
|
cerr("ww: package artifact path is too long\n");
|
|
return -1;
|
|
};
|
|
i += 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
fn sepexternalname(pkg: *seppkg, path: *u8, n: u64,
|
|
leafonly: bool) bool = {
|
|
if (pkg.variant != SEP_VARIANT_EXTERNAL || pkg.testpackage == nil) {
|
|
return false;
|
|
};
|
|
let begin: u64 = 0u64;
|
|
if (leafonly) {
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
if (path[i] == '.') { begin = i + 1u64; };
|
|
i += 1u64;
|
|
};
|
|
};
|
|
let leafn: u64 = n - begin;
|
|
let tn: u64 = cstrlen(pkg.testpackage);
|
|
if (tn != leafn + 5u64) { return false; };
|
|
if (bytecmp(pkg.testpackage, leafn, path + begin, leafn) != 0) {
|
|
return false;
|
|
};
|
|
return pkg.testpackage[leafn] == '_'
|
|
&& pkg.testpackage[leafn + 1u64] == 't'
|
|
&& pkg.testpackage[leafn + 2u64] == 'e'
|
|
&& pkg.testpackage[leafn + 3u64] == 's'
|
|
&& pkg.testpackage[leafn + 4u64] == 't';
|
|
};
|
|
|
|
fn sepexternalproduction(pkg: *seppkg, path: *u8, n: u64) bool = {
|
|
return sepexternalname(pkg, path, n, false);
|
|
};
|
|
|
|
fn sepbindadd(bindings: *[]sepbind, kind: u8, name: str,
|
|
target: *u8) void = {
|
|
let i: i32 = 0;
|
|
for (i < len(*bindings)) {
|
|
let b: sepbind = (*bindings)[i];
|
|
if (b.kind == kind && syntax.streq(b.name, name)) {
|
|
if (target == nil && b.target == nil) { return; };
|
|
if (target != nil && b.target != nil
|
|
&& os.samefile(pathstr(target), pathstr(b.target))) {
|
|
return;
|
|
};
|
|
};
|
|
i += 1;
|
|
};
|
|
append(*bindings, sepbind {
|
|
kind = kind,
|
|
name = strings.dup(name),
|
|
target = target,
|
|
});
|
|
};
|
|
|
|
fn sepbindsame(a: []sepbind, b: []sepbind) bool = {
|
|
if (len(a) != len(b)) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < len(a)) {
|
|
let found: bool = false;
|
|
let j: i32 = 0;
|
|
for (j < len(b)) {
|
|
if (a[i].kind == b[j].kind
|
|
&& syntax.streq(a[i].name, b[j].name)) {
|
|
if (a[i].target == nil && b[j].target == nil) {
|
|
found = true;
|
|
} else { if (a[i].target != nil && b[j].target != nil
|
|
&& os.samefile(pathstr(a[i].target),
|
|
pathstr(b[j].target))) {
|
|
found = true;
|
|
}; };
|
|
};
|
|
j += 1;
|
|
};
|
|
if (!found) { return false; };
|
|
i += 1;
|
|
};
|
|
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, bindings: *[]sepbind, 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;
|
|
};
|
|
let l: syntax.lex;
|
|
syntax.lexinit(&l, fdup, bufp, blen);
|
|
let ps: syntax.parser;
|
|
syntax.parserinit(&ps, &l);
|
|
let imports: *syntax.node = syntax.parseimports(&ps);
|
|
if (l.errs > 0 || ps.errs > 0) { return -1; };
|
|
if (imports.nmod.len == 0 && ownedsource != 0) {
|
|
cerrpos(fdup, 1, 1);
|
|
cerr(": error: invalid or missing package clause\n");
|
|
return -1;
|
|
};
|
|
if (imports.nmod.len != 0
|
|
&& (ownedsource != 0 || g.pkg[pi].name == nil)) {
|
|
let declared: *u8 = imports.nmod.ptr;
|
|
let declaredn: u64 = imports.nmod.len: u64;
|
|
if (declaredn >= 256u64) {
|
|
cerrpos(imports.file, imports.line, imports.col);
|
|
cerr(": error: package name is too long\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) {
|
|
cerrpos(imports.file, imports.line, imports.col);
|
|
cerr(": error: conflicting package names ");
|
|
cerr(pathstr(g.pkg[pi].name));
|
|
cerr(" and ");
|
|
os.write(2, declared, declaredn);
|
|
cerr(" in "); cerr(pathstr(g.pkg[pi].entry)); cerr("\n");
|
|
return -1;
|
|
}; };
|
|
};
|
|
if (ownedsource != 0 && g.pkg[pi].isdir != 0) {
|
|
let pm: *syntax.node = imports.body;
|
|
for (pm != nil) {
|
|
if (!syntax.streq(pm.nmod, pathstr(g.pkg[pi].name))) {
|
|
cerrpos(pm.file, pm.line, pm.col);
|
|
cerr(": error: conflicting package names ");
|
|
cerr(pathstr(g.pkg[pi].name)); cerr(" and ");
|
|
cerr(pm.nmod); cerr(" in ");
|
|
cerr(pathstr(g.pkg[pi].entry)); cerr("\n");
|
|
return -1;
|
|
};
|
|
pm = pm.next;
|
|
};
|
|
};
|
|
let nuse: i32 = 0;
|
|
let u: *syntax.node = imports.list;
|
|
for (u != nil) {
|
|
if (u.kind == syntax.nkind.N_USE) { nuse += 1; };
|
|
u = u.next;
|
|
};
|
|
let uses: []*syntax.node = [];
|
|
if (nuse > 0) {
|
|
let allocated: []*syntax.node = alloc([], nuse: u64)!;
|
|
uses = allocated;
|
|
uses.len = nuse;
|
|
};
|
|
let ui: i32 = 0;
|
|
u = imports.list;
|
|
for (u != nil) {
|
|
if (u.kind == syntax.nkind.N_USE) { uses[ui] = u; ui += 1; };
|
|
u = u.next;
|
|
};
|
|
let si: i32 = 1;
|
|
for (si < nuse) {
|
|
let sj: i32 = si;
|
|
for (sj > 0) {
|
|
if (strings.compare(uses[sj - 1].usepath,
|
|
uses[sj].usepath) <= 0) { sj = 0; }
|
|
else {
|
|
let t: *syntax.node = uses[sj];
|
|
uses[sj] = uses[sj - 1];
|
|
uses[sj - 1] = t;
|
|
sj -= 1;
|
|
};
|
|
};
|
|
si += 1;
|
|
};
|
|
let previous: str = "";
|
|
ui = 0;
|
|
for (ui < nuse) {
|
|
u = uses[ui];
|
|
let duplicate: bool = previous.len > 0
|
|
&& syntax.streq(previous, u.usepath);
|
|
if (!duplicate) {
|
|
previous = u.usepath;
|
|
let idp: *u8 = u.usepath.ptr;
|
|
let idn: u64 = u.usepath.len: u64;
|
|
if (idn >= 256u64) {
|
|
cerrpos(u.file, u.line, u.col);
|
|
cerr(": error: import path is too long (limit 255 bytes)\n");
|
|
return -1;
|
|
};
|
|
let isdir: i32 = 0;
|
|
let externalproduction: bool = sepexternalproduction(
|
|
&g.pkg[pi], idp, idn);
|
|
let ipath: *u8 = nil;
|
|
if (externalproduction) {
|
|
ipath = g.pkg[pi].entry;
|
|
isdir = 1;
|
|
} else {
|
|
ipath = locateimport(searchpath, idp, idn, &isdir);
|
|
};
|
|
if (ipath != nil) {
|
|
if (isdir != 0) {
|
|
sepbindadd(bindings, 'D': u8, u.usepath, ipath);
|
|
let self: bool = os.samefile(pathstr(ipath),
|
|
pathstr(g.pkg[pi].entry));
|
|
if (self && sepexternalname(&g.pkg[pi], idp, idn, true)) {
|
|
externalproduction = true;
|
|
};
|
|
if (self
|
|
&& !externalproduction) {
|
|
cerrpos(u.file, u.line, u.col);
|
|
cerr(": error: self-import: package '");
|
|
if (g.pkg[pi].path[0u64] != 0u8) {
|
|
cerr(pathstr(g.pkg[pi].path));
|
|
} else { cerr(pathstr(g.pkg[pi].name)); };
|
|
cerr("' cannot import itself\n");
|
|
return -1;
|
|
};
|
|
let runtimeproduction: bool = false;
|
|
if (externalproduction) {
|
|
let gi: i32 = 0;
|
|
for (gi < g.n) {
|
|
if (g.pkg[gi].testsupport
|
|
&& syntax.streq(pathstr(g.pkg[gi].path),
|
|
u.usepath)
|
|
&& os.samefile(pathstr(g.pkg[gi].entry),
|
|
pathstr(ipath))) {
|
|
runtimeproduction = true;
|
|
};
|
|
gi += 1;
|
|
};
|
|
};
|
|
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 = -1;
|
|
if (externalproduction && !runtimeproduction) {
|
|
let art: *u8 = appendlit(g.pkg[pi].artifact,
|
|
"-production");
|
|
di = sepfindoraddrole(g, nm.ptr, ipath, 1,
|
|
SEP_ROLE_EXTERNAL_PRODUCTION, art);
|
|
} else {
|
|
di = 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 {
|
|
sepbindadd(bindings, 'F': u8, u.usepath, ipath);
|
|
if (sepscanfile(g, pi, ipath, searchpath, fv,
|
|
bindings, 0) < 0) {
|
|
return -1;
|
|
};
|
|
};
|
|
} else {
|
|
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;
|
|
let inlinepackage: bool = false;
|
|
let pm: *syntax.node = imports.body;
|
|
for (pm != nil) {
|
|
if (bytecmp(pm.nmod.ptr, pm.nmod.len: u64,
|
|
leafp, leafn) == 0) { inlinepackage = true; };
|
|
pm = pm.next;
|
|
};
|
|
if (!inlinepackage) {
|
|
cerrpos(u.file, u.line, u.col);
|
|
cerr(": error: cannot find package ");
|
|
os.write(2, idp, idn);
|
|
cerr("\n");
|
|
return -1;
|
|
} else {
|
|
sepbindadd(bindings, 'I': u8, u.usepath, nil);
|
|
};
|
|
};
|
|
};
|
|
ui += 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
fn sepdepcmp(g: *sepgraph, a: i32, b: i32) i32 = {
|
|
let r: i32 = strings.compare(pathstr(g.pkg[a].path),
|
|
pathstr(g.pkg[b].path)): i32;
|
|
if (r != 0) { return r; };
|
|
if (g.pkg[a].role < g.pkg[b].role) { return -1; };
|
|
if (g.pkg[a].role > g.pkg[b].role) { return 1; };
|
|
if (g.pkg[a].artifact == nil && g.pkg[b].artifact == nil) { return 0; };
|
|
if (g.pkg[a].artifact == nil) { return -1; };
|
|
if (g.pkg[b].artifact == nil) { return 1; };
|
|
return strings.compare(pathstr(g.pkg[a].artifact),
|
|
pathstr(g.pkg[b].artifact)): i32;
|
|
};
|
|
|
|
// Load pi once: a directory node takes ownership of its sorted production
|
|
// paths, then every selected-root context verifies the same canonical import
|
|
// bindings before the package is compiled once.
|
|
fn seploadpkg(g: *sepgraph, pi: i32, context: i32) i32 = {
|
|
if (g.pkg[pi].testsupport
|
|
&& g.supportcontext >= 0) { context = g.supportcontext; };
|
|
if (context < 0 || context >= g.ncontext) { return -1; };
|
|
if (g.pkg[pi].contextstate[context] == 2u8) {
|
|
if (g.pkg[pi].failed) { return -1; };
|
|
return 0;
|
|
};
|
|
if (g.pkg[pi].contextstate[context] == 1u8) { return 0; };
|
|
g.pkg[pi].contextstate[context] = 1u8;
|
|
let searchpath: *u8 = g.context[context].searchpath;
|
|
let fv: expctx;
|
|
fv.out = -1;
|
|
fv.dirs = searchpath;
|
|
fv.visit = nil;
|
|
let bindings: []sepbind;
|
|
let rc: i32 = 0;
|
|
if (!g.pkg[pi].loaded) {
|
|
g.pkg[pi].loaded = true;
|
|
if (g.pkg[pi].isdir != 0) {
|
|
let sources: **u8;
|
|
let nsources: i32;
|
|
sources, nsources = enumeratedir(g.pkg[pi].entry,
|
|
g.pkg[pi].variant, g.pkg[pi].testpackage);
|
|
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;
|
|
}; }; };
|
|
};
|
|
};
|
|
if (g.pkg[pi].isdir != 0) {
|
|
let i: i32 = 0;
|
|
for (i < g.pkg[pi].nsources) {
|
|
if (rc == 0) {
|
|
rc = sepscanfile(g, pi, g.pkg[pi].sources[i],
|
|
searchpath, &fv, &bindings, 1);
|
|
};
|
|
i += 1;
|
|
};
|
|
if (rc == 0 && g.pkg[pi].path[0u64] != 0u8
|
|
&& !g.pkg[pi].testsupport) {
|
|
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 { if (rc == 0) {
|
|
rc = sepscanfile(g, pi, g.pkg[pi].entry, searchpath,
|
|
&fv, &bindings, 0);
|
|
}; };
|
|
if (rc == 0 && g.pkg[pi].emitcontext < 0) {
|
|
g.pkg[pi].bindings = bindings;
|
|
g.pkg[pi].emitcontext = context;
|
|
} else { if (rc == 0 && !sepbindsame(g.pkg[pi].bindings, bindings)) {
|
|
cerr("ww: package ");
|
|
if (g.pkg[pi].path[0u64] != 0u8) {
|
|
cerr(pathstr(g.pkg[pi].path));
|
|
} else { cerr(pathstr(g.pkg[pi].name)); };
|
|
cerr(" resolves imports differently in ");
|
|
cerr(pathstr(g.context[g.pkg[pi].emitcontext].root));
|
|
cerr(" and "); cerr(pathstr(g.context[context].root)); cerr("\n");
|
|
rc = -1;
|
|
}; };
|
|
if (rc < 0) {
|
|
g.pkg[pi].contextstate[context] = 2u8;
|
|
g.pkg[pi].failed = true;
|
|
return rc;
|
|
};
|
|
if (g.pkg[pi].root && g.pkg[pi].path[0u64] == 0u8
|
|
&& g.pkg[pi].name != nil) {
|
|
g.pkg[pi].path = arenadupcstr(g.pkg[pi].name,
|
|
cstrlen(g.pkg[pi].name));
|
|
};
|
|
let si: i32 = 1;
|
|
for (si < g.pkg[pi].ndeps) {
|
|
let v: i32 = g.pkg[pi].deps[si];
|
|
let sj: i32 = si;
|
|
for (sj > 0 && sepdepcmp(g,
|
|
g.pkg[pi].deps[sj - 1], v) > 0) {
|
|
g.pkg[pi].deps[sj] = g.pkg[pi].deps[sj - 1];
|
|
sj -= 1;
|
|
};
|
|
g.pkg[pi].deps[sj] = v;
|
|
si += 1;
|
|
};
|
|
g.pkg[pi].contextstate[context] = 2u8;
|
|
let k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
if (seploadpkg(g, g.pkg[pi].deps[k], context) < 0) {
|
|
g.pkg[pi].failed = true;
|
|
return -1;
|
|
};
|
|
k += 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
fn sepvalidatemoduleclosure(g: *sepgraph, order: []i32, n: i32,
|
|
includeroot: bool) i32 = {
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
let a: i32 = order[i];
|
|
if ((includeroot || !g.pkg[a].root)
|
|
&& g.pkg[a].path[0u64] != 0u8) {
|
|
let j: i32 = i + 1;
|
|
for (j < n) {
|
|
let b: i32 = order[j];
|
|
if ((includeroot || !g.pkg[b].root)
|
|
&& cstreq(g.pkg[a].path, g.pkg[b].path)) {
|
|
cerr("ww: product closure contains multiple packages named ");
|
|
cerr(pathstr(g.pkg[a].path)); cerr("\n");
|
|
return -1;
|
|
};
|
|
j += 1;
|
|
};
|
|
};
|
|
i += 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// 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 sepwriteall(fd: i32, buf: *u8, n: u64) bool = {
|
|
match (os.writeall(fd, buf, n)) {
|
|
case let wrote: i64 => return wrote == n: i64;
|
|
case let e: os.oserror => return false;
|
|
};
|
|
};
|
|
|
|
fn sepemitbody(fd: i32, path: *u8, visit: *expctx, searchpath: *u8,
|
|
modpath: *u8, pkg: *seppkg) i32 = {
|
|
let pview: str;
|
|
pview.ptr = path;
|
|
pview.len = cstrlen(path): i32;
|
|
let pdup: str = strings.dup(pview);
|
|
if (visitseen(visit, pdup)) { return 0; };
|
|
visitadd(visit, pdup);
|
|
let bufp: *u8;
|
|
let blen: u64;
|
|
bufp, blen = slurp(path);
|
|
if (bufp == nil) {
|
|
cerr("ww: cannot read source\n");
|
|
return -1;
|
|
};
|
|
let l: syntax.lex;
|
|
syntax.lexinit(&l, pdup, bufp, blen);
|
|
let ps: syntax.parser;
|
|
syntax.parserinit(&ps, &l);
|
|
let imports: *syntax.node = syntax.parseimports(&ps);
|
|
if (l.errs > 0 || ps.errs > 0) { return -1; };
|
|
let nuse: i32 = 0;
|
|
let u: *syntax.node = imports.list;
|
|
for (u != nil) {
|
|
if (u.kind == syntax.nkind.N_USE) { nuse += 1; };
|
|
u = u.next;
|
|
};
|
|
let uses: []*syntax.node = [];
|
|
if (nuse > 0) {
|
|
let allocated: []*syntax.node = alloc([], nuse: u64)!;
|
|
uses = allocated;
|
|
uses.len = nuse;
|
|
};
|
|
let ui: i32 = 0;
|
|
u = imports.list;
|
|
for (u != nil) {
|
|
if (u.kind == syntax.nkind.N_USE) { uses[ui] = u; ui += 1; };
|
|
u = u.next;
|
|
};
|
|
let si: i32 = 1;
|
|
for (si < nuse) {
|
|
let sj: i32 = si;
|
|
for (sj > 0) {
|
|
if (strings.compare(uses[sj - 1].usepath,
|
|
uses[sj].usepath) <= 0) { sj = 0; }
|
|
else {
|
|
let t: *syntax.node = uses[sj];
|
|
uses[sj] = uses[sj - 1];
|
|
uses[sj - 1] = t;
|
|
sj -= 1;
|
|
};
|
|
};
|
|
si += 1;
|
|
};
|
|
ui = 0;
|
|
for (ui < nuse) {
|
|
u = uses[ui];
|
|
let idp: *u8 = u.usepath.ptr;
|
|
let idn: u64 = u.usepath.len: u64;
|
|
if (sepexternalproduction(pkg, idp, idn)) {
|
|
ui += 1;
|
|
continue;
|
|
};
|
|
let isdir: i32 = 0;
|
|
let ipath: *u8 = locateimport(searchpath, idp, idn, &isdir);
|
|
if (ipath != nil) {
|
|
if (isdir == 0) {
|
|
if (sepemitbody(fd, ipath, visit, searchpath,
|
|
modpath, pkg) < 0) { return -1; };
|
|
};
|
|
};
|
|
ui += 1;
|
|
};
|
|
// #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 ";
|
|
if (!sepwriteall(fd, dm.ptr, dm.len: u64)
|
|
|| !sepwriteall(fd, modpath, cstrlen(modpath))
|
|
|| !sepwriteall(fd, "\n".ptr, 1u64)) {
|
|
cerr("ww: cannot write package unit\n");
|
|
return -1;
|
|
};
|
|
} else {
|
|
let d: str = "//ww:module-reset\n";
|
|
if (!sepwriteall(fd, d.ptr, d.len: u64)) {
|
|
cerr("ww: cannot write package unit\n");
|
|
return -1;
|
|
};
|
|
};
|
|
if (!sepwriteall(fd, bufp, blen)
|
|
|| !sepwriteall(fd, "\n".ptr, 1u64)) {
|
|
cerr("ww: cannot write package unit\n");
|
|
return -1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// Compose pi's sep-unit at `unitf`: its byte-sorted DIRECT dependency
|
|
// `.wwi`s, each tagged by its dotted path, then pi's own body under
|
|
// //ww:module-reset. Compiler exports are self-contained for public type
|
|
// facts; the linker separately retains the reachable archive closure.
|
|
fn sepcomposeunit(g: *sepgraph, pi: i32, scratch: *u8,
|
|
unitf: *u8) i32 = {
|
|
if (g.pkg[pi].emitcontext < 0
|
|
|| g.pkg[pi].emitcontext >= g.ncontext) { return -1; };
|
|
let searchpath: *u8 = g.context[g.pkg[pi].emitcontext].searchpath;
|
|
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 k: i32 = 0;
|
|
for (k < g.pkg[pi].ndeps) {
|
|
let dj: i32 = g.pkg[pi].deps[k];
|
|
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 ";
|
|
if (!sepwriteall(u, dm.ptr, dm.len: u64)
|
|
|| !sepwriteall(u, g.pkg[dj].path,
|
|
cstrlen(g.pkg[dj].path))
|
|
|| !sepwriteall(u, "\n".ptr, 1u64)
|
|
|| !sepwriteall(u, wb, wn)
|
|
|| !sepwriteall(u, "\n".ptr, 1u64)) {
|
|
cerr("ww: cannot compose package unit\n");
|
|
os.close(u);
|
|
return -1;
|
|
};
|
|
k += 1;
|
|
};
|
|
let bv: expctx;
|
|
bv.out = u;
|
|
bv.dirs = searchpath;
|
|
bv.visit = nil;
|
|
let bodyrc: i32 = 0;
|
|
if (g.pkg[pi].isdir != 0) {
|
|
let i: i32 = 0;
|
|
for (i < g.pkg[pi].nsources && bodyrc == 0) {
|
|
bodyrc = sepemitbody(u, g.pkg[pi].sources[i], &bv, searchpath,
|
|
g.pkg[pi].path, &g.pkg[pi]);
|
|
i += 1;
|
|
};
|
|
} else {
|
|
bodyrc = sepemitbody(u, g.pkg[pi].entry, &bv, searchpath,
|
|
g.pkg[pi].path, &g.pkg[pi]);
|
|
};
|
|
if (os.close(u) != 0) {
|
|
cerr("ww: cannot close package unit\n");
|
|
return -1;
|
|
};
|
|
return bodyrc;
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
let bad: bool = !sepwriteall(fd, out, total);
|
|
if (os.close(fd) != 0) { bad = true; };
|
|
if (bad) {
|
|
cerr("ww: cannot write archive\n");
|
|
return -1;
|
|
};
|
|
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.
|
|
|
|
// 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 => {
|
|
if (w != n) { bad = true; done = true; };
|
|
};
|
|
case let e: os.oserror => { bad = true; done = true; };
|
|
};
|
|
}; };
|
|
};
|
|
if (os.close(in) != 0) { bad = true; };
|
|
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 5 mode test asm 1\n";
|
|
};
|
|
return "ww workdir fmt 5 mode test asm 0\n";
|
|
};
|
|
if (emitasm != 0) {
|
|
return "ww workdir fmt 4 mode build asm 1\n";
|
|
};
|
|
return "ww workdir fmt 4 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 => { if (w != want.len: i64) { bad = true; }; };
|
|
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));
|
|
};
|
|
|
|
// A coordinator-private completion marker distinguishes a newly linked
|
|
// product from a caller-owned binary left by an earlier invocation.
|
|
fn recordproductstatus(path: *u8) i32 = {
|
|
if (path == nil) { return 0; };
|
|
let tmpp: *u8 = appendlit(path, ".new");
|
|
let fd: i32 = os.open(pathstr(tmpp),
|
|
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32);
|
|
if (fd < 0) { return -1; };
|
|
let body: str = "ok\n";
|
|
let bad: bool = false;
|
|
match (os.writeall(fd, body.ptr, body.len: u64)) {
|
|
case let n: i64 => { if (n != body.len: i64) { bad = true; }; };
|
|
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));
|
|
};
|
|
|
|
// Remove every committed unit voucher before a stale-tool pass. Remaining
|
|
// artifacts cannot be reused without their matching unit, so a partial pass
|
|
// may safely record its new tool identity: successful actions have current
|
|
// units and failed/no-longer-requested actions have none.
|
|
fn invalidateworkdirunits(scratch: *u8) i32 = {
|
|
let fd: i32 = os.open(pathstr(scratch), os.flag.RDONLY, 0i32);
|
|
if (fd < 0) { return -1; };
|
|
let buf: []u8 = alloc([], 8192u64)!;
|
|
buf.len = 8192;
|
|
let rc: i32 = 0;
|
|
let r: i64 = os.getdents64(fd, buf.ptr, 8192u64);
|
|
for (r > 0i64 && rc == 0) {
|
|
let off: u64 = 0u64;
|
|
for (off < r: u64) {
|
|
let reclen: u64 = (buf[off + 16u64]): u64
|
|
+ ((buf[off + 17u64]): u64) * 256u64;
|
|
if (reclen == 0u64) { rc = -1; break; };
|
|
let name: *u8 = buf.ptr + off + 19u64;
|
|
let ns: str = pathstr(name);
|
|
if (strings.hassuffix(ns, ".unit.ww")) {
|
|
if (cstrlen(scratch) + 1u64 + cstrlen(name) + 1u64
|
|
> os.PATH_MAX: u64) {
|
|
rc = -1;
|
|
break;
|
|
};
|
|
let path: *u8 = joinpath(scratch, name);
|
|
let rr: i32 = os.remove(pathstr(path));
|
|
if (rr != 0 && rr != -2) { rc = -1; break; };
|
|
};
|
|
off += reclen;
|
|
};
|
|
if (rc == 0) { r = os.getdents64(fd, buf.ptr, 8192u64); };
|
|
};
|
|
if (r < 0i64) { rc = -1; };
|
|
if (os.close(fd) != 0) { rc = -1; };
|
|
if (rc != 0) { cerr("ww: cannot invalidate stale package units\n"); };
|
|
return rc;
|
|
};
|
|
|
|
// 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,
|
|
rootidentity: *u8, out: *u8,
|
|
objstem: *u8, incs: *u8, lf: *lflags, packageonly: i32, istest: i32,
|
|
products: *sepproduct, nproducts: i32, emitasm: i32,
|
|
workdir: *u8, scratchout: **u8,
|
|
graphout: **sepgraph) i32 = {
|
|
if (nproducts < 1 || nproducts > SEP_MAXPRODUCT) { return 1; };
|
|
let statusi: i32 = 0;
|
|
for (statusi < nproducts) {
|
|
if (products[statusi].status != nil) {
|
|
let rr: i32 = os.remove(pathstr(products[statusi].status));
|
|
if (rr != 0 && rr != -2) { return 1; };
|
|
};
|
|
statusi += 1;
|
|
};
|
|
let c6: *u8 = toolpath(selfdir, "WW_W6C", "w6c_ww");
|
|
let a6: *u8 = toolpath(selfdir, "WW_W6A", "w6a_ww");
|
|
let l6: *u8 = toolpath(selfdir, "WW_W6L", "w6l_ww");
|
|
|
|
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);
|
|
};
|
|
|
|
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;
|
|
};
|
|
};
|
|
|
|
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; };
|
|
};
|
|
};
|
|
if (staleall && invalidateworkdirunits(scratch) != 0) { return 1; };
|
|
};
|
|
|
|
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);
|
|
};
|
|
|
|
let pkgslot: []seppkg = alloc([], SEP_MAXPKG: u64)!;
|
|
pkgslot.len = SEP_MAXPKG;
|
|
let contextslot: []sepcontext = alloc([], SEP_MAXCONTEXT: u64)!;
|
|
contextslot.len = SEP_MAXCONTEXT;
|
|
let g: *sepgraph = alloc(sepgraph{
|
|
pkg = pkgslot,
|
|
n = 0,
|
|
context = contextslot,
|
|
ncontext = 0,
|
|
supportcontext = -1,
|
|
})!;
|
|
if (graphout != nil) { *graphout = g; };
|
|
let rootpath: *u8 = "\0".ptr;
|
|
if (packageonly != 0 && rootidentity != nil) { rootpath = rootidentity; };
|
|
let producti: i32 = 0;
|
|
for (producti < nproducts) {
|
|
let entry: *u8 = src;
|
|
if (products[producti].dir != nil) {
|
|
entry = products[producti].dir;
|
|
};
|
|
let contextroot: *u8 = entry;
|
|
if (entryisdir == 0) { contextroot = srcd.ptr; };
|
|
products[producti].context = sepcontextfor(g, contextroot,
|
|
incs, dotdotlib.ptr);
|
|
if (products[producti].context < 0) { return 1; };
|
|
products[producti].root = sepfindoraddvariant(g, rootpath, entry,
|
|
entryisdir, products[producti].variant,
|
|
products[producti].testpackage,
|
|
SEP_ROLE_NORMAL, products[producti].artifact, true);
|
|
if (products[producti].root < 0) { return 1; };
|
|
producti += 1;
|
|
};
|
|
let testsupportmodule: str = "test";
|
|
// -T generates a dispatcher whose support qualifier is selected by the
|
|
// command. Represent that compiler-generated requirement as a direct root
|
|
// edge. It normally coalesces with an explicit toolchain `import test`;
|
|
// when user source occupies that identity, the reserved graph alias keeps
|
|
// it distinct. The linker receives the same support archive closure.
|
|
if (istest != 0) {
|
|
let td: i32 = 0;
|
|
let tp: *u8 = locateimport(dotdotlib.ptr, "test".ptr,
|
|
"test".len: u64, &td);
|
|
if (tp != nil) {
|
|
g.supportcontext = sepcontextfor(g, dotdotlib.ptr, nil,
|
|
dotdotlib.ptr);
|
|
if (g.supportcontext < 0) { return 1; };
|
|
let collision: bool = false;
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
let rootissupport: bool = entryisdir != 0
|
|
&& os.samefile(pathstr(tp),
|
|
pathstr(g.pkg[root].entry));
|
|
let name: *u8 = products[producti].testpackage;
|
|
if (!rootissupport && name != nil
|
|
&& (cstreqlit(name, "test")
|
|
|| cstreqlit(name, "test_test"))) {
|
|
collision = true;
|
|
};
|
|
producti += 1;
|
|
};
|
|
producti = 0;
|
|
for (producti < nproducts && !collision) {
|
|
let ud: i32 = 0;
|
|
let up: *u8 = locateimport(
|
|
g.context[products[producti].context].searchpath,
|
|
"test".ptr,
|
|
"test".len: u64, &ud);
|
|
if (up != nil && !os.samefile(pathstr(tp), pathstr(up))) {
|
|
collision = true;
|
|
};
|
|
producti += 1;
|
|
};
|
|
if (collision) { testsupportmodule = SEP_TEST_SUPPORT_MODULE; };
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
let rootissupport: bool = entryisdir != 0
|
|
&& os.samefile(pathstr(tp),
|
|
pathstr(g.pkg[root].entry));
|
|
// A same-test build of the runtime package already owns run
|
|
// and its source imports. An external test still needs the
|
|
// colocated production node, also its support dependency.
|
|
if (rootissupport
|
|
&& syntax.streq(testsupportmodule, "test")
|
|
&& products[producti].variant != SEP_VARIANT_EXTERNAL) {
|
|
producti += 1;
|
|
continue;
|
|
};
|
|
let ti: i32 = -1;
|
|
if (syntax.streq(testsupportmodule,
|
|
SEP_TEST_SUPPORT_MODULE)) {
|
|
ti = sepfindoraddrole(g, testsupportmodule.ptr, tp,
|
|
td, SEP_ROLE_TEST_SUPPORT, nil);
|
|
} else {
|
|
ti = sepfindoradd(g, testsupportmodule.ptr, tp, td);
|
|
};
|
|
if (ti < 0) { return 1; };
|
|
g.pkg[ti].testsupport = true;
|
|
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;
|
|
};
|
|
};
|
|
producti += 1;
|
|
};
|
|
};
|
|
};
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
if (seploadpkg(g, root, products[producti].context) < 0) {
|
|
g.pkg[root].failed = true;
|
|
producti += 1;
|
|
continue;
|
|
};
|
|
if (products[producti].variant != SEP_VARIANT_PRODUCTION
|
|
&& (products[producti].testpackage == nil
|
|
|| !cstreq(g.pkg[root].name,
|
|
products[producti].testpackage))) {
|
|
cerr("ww: package-test selector does not match loaded package\n");
|
|
g.pkg[root].failed = true;
|
|
};
|
|
producti += 1;
|
|
};
|
|
if (sepvalidateartifactpaths(g, scratch) < 0) { return 1; };
|
|
let rootpackage: bool = packageonly != 0;
|
|
if (rootpackage && !g.pkg[products[0].root].failed
|
|
&& cstreqlit(g.pkg[products[0].root].name, "main")) {
|
|
cerr("ww: -p requires a non-main package\n");
|
|
return 1;
|
|
};
|
|
|
|
let ci: i32 = 0;
|
|
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;
|
|
// Diagnose cycles per product before constructing the shared union. A
|
|
// variant-local cycle must not suppress an independent sibling root.
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
if (!g.pkg[root].failed) {
|
|
ci = 0;
|
|
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
|
|
let ignored: i32 = 0;
|
|
if (septopovisit(g, root, order,
|
|
&ignored, stack, 0) < 0
|
|
|| sepvalidatemoduleclosure(g, order, ignored,
|
|
rootpackage) < 0) {
|
|
g.pkg[root].failed = true;
|
|
};
|
|
};
|
|
producti += 1;
|
|
};
|
|
ci = 0;
|
|
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
if (!g.pkg[root].failed) {
|
|
if (septopovisit(g, root, order,
|
|
&norder, stack, 0) < 0) { return 1; };
|
|
};
|
|
producti += 1;
|
|
};
|
|
if (!rootpackage) {
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
g.pkg[products[producti].root].path = "\0".ptr;
|
|
producti += 1;
|
|
};
|
|
};
|
|
|
|
let anyfailed: bool = false;
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
if (g.pkg[products[producti].root].failed) { anyfailed = true; };
|
|
producti += 1;
|
|
};
|
|
let oi: i32 = 0;
|
|
for (oi < norder) {
|
|
let pi: i32 = order[oi];
|
|
let dk: i32 = 0;
|
|
for (dk < g.pkg[pi].ndeps) {
|
|
if (g.pkg[g.pkg[pi].deps[dk]].failed) {
|
|
g.pkg[pi].failed = true;
|
|
};
|
|
dk += 1;
|
|
};
|
|
if (g.pkg[pi].failed) {
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
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;
|
|
};
|
|
let needsexport: bool = !g.pkg[pi].root || rootpackage;
|
|
let needsarchive: bool = !g.pkg[pi].root || rootpackage;
|
|
if (sepcomposeunit(g, pi, scratch, cu) < 0) {
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
let fresh: bool = false;
|
|
if (warm) {
|
|
if (!staleall) {
|
|
fresh = fileequal(unitnew, unitf);
|
|
if (fresh) { fresh = fileisreg(asmf); };
|
|
if (fresh) {
|
|
if (needsexport) {
|
|
fresh = fileisreg(wwi);
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (emitasm == 0) {
|
|
fresh = filesizenonzero(objf);
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (emitasm == 0 && needsarchive) {
|
|
fresh = filesizenonzero(apath);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
if (fresh) {
|
|
if (os.remove(pathstr(unitnew)) != 0) {
|
|
cerrpath("ww: cannot remove ", unitnew, "\n");
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
};
|
|
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 = g.pkg[pi].root && (istest != 0);
|
|
let supportt: bool = g.pkg[pi].testsupport;
|
|
let alen: u64 = 8u64;
|
|
if (!needsexport) { alen = 6u64; if (roott) { alen = 9u64; }; };
|
|
if (supportt) { alen += 2u64; };
|
|
let argv: []str = alloc([], alen)!;
|
|
append(argv, "w6c");
|
|
if (roott) {
|
|
append(argv, "-T");
|
|
append(argv, "--test-support-module");
|
|
append(argv, testsupportmodule);
|
|
};
|
|
if (supportt) {
|
|
append(argv, "--test-support-module");
|
|
append(argv, testsupportmodule);
|
|
};
|
|
append(argv, "-c");
|
|
if (needsexport) {
|
|
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");
|
|
};
|
|
if (g.pkg[pi].path[0u64] != 0u8) {
|
|
cerrpath("ww: w6c failed for ",
|
|
g.pkg[pi].path, "\n");
|
|
} else {
|
|
cerr("ww: w6c failed for (root)\n");
|
|
};
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
};
|
|
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");
|
|
};
|
|
if (g.pkg[pi].path[0u64] != 0u8) {
|
|
cerrpath("ww: w6a failed for ",
|
|
g.pkg[pi].path, "\n");
|
|
} else {
|
|
cerr("ww: w6a failed for (root)\n");
|
|
};
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
};
|
|
// 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 && needsarchive) {
|
|
if (archiveo(co, ca) != 0) {
|
|
cerr("ww: archive failed\n");
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
};
|
|
// Commit order: artifacts before the unit that vouches for
|
|
// them, unit strictly last.
|
|
if (warm) {
|
|
let bad: bool = false;
|
|
if (needsexport) {
|
|
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 && needsarchive) {
|
|
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");
|
|
};
|
|
g.pkg[pi].failed = true;
|
|
anyfailed = true;
|
|
oi += 1;
|
|
continue;
|
|
};
|
|
};
|
|
oi += 1;
|
|
};
|
|
// A stale pass removed every old unit voucher before compiling. Current
|
|
// successful units remain safe to vouch for when a sibling root fails;
|
|
// a killed pass retains the old identity and invalidates again next time.
|
|
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) { if (anyfailed) { return 1; }; return 0; };
|
|
if (rootpackage) {
|
|
let root: i32 = products[0].root;
|
|
if (g.pkg[root].failed) { return 1; };
|
|
let archive: *u8 = sepfname(g, root, scratch, ".a");
|
|
let iface: *u8 = sepfname(g, root, scratch, ".wwi");
|
|
let outiface: *u8 = appendlit(out, ".wwi");
|
|
if (copyfileatomic(archive, out) != 0
|
|
|| copyfileatomic(iface, outiface) != 0) {
|
|
cerrpath("ww: cannot write package artifact ", out, "\n");
|
|
return 1;
|
|
};
|
|
return 0;
|
|
};
|
|
|
|
// Each product gets its own reverse-topological link closure. Shared
|
|
// production actions do not turn variant-local archives into link inputs.
|
|
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;
|
|
};
|
|
producti = 0;
|
|
for (producti < nproducts) {
|
|
let root: i32 = products[producti].root;
|
|
if (g.pkg[root].failed) {
|
|
anyfailed = true;
|
|
producti += 1;
|
|
continue;
|
|
};
|
|
ci = 0;
|
|
for (ci < g.n) { g.pkg[ci].color = 0; ci += 1; };
|
|
let linkorder: []i32 = alloc([], g.n: u64)!;
|
|
linkorder.len = g.n;
|
|
let linkstack: []i32 = alloc([], g.n: u64)!;
|
|
linkstack.len = g.n;
|
|
let nlink: i32 = 0;
|
|
if (septopovisit(g, root, linkorder, &nlink,
|
|
linkstack, 0) < 0) { return 1; };
|
|
// argv: 3 fixed + closure + libwwrt + joined flags + nil.
|
|
let total: i32 = 3 + nlink + 1 + nldirs + nllibs + 1;
|
|
let largv: []*u8 = alloc([], total: u64)!;
|
|
largv.len = total;
|
|
largv[0] = "w6l\0".ptr;
|
|
largv[1] = "-o\0".ptr;
|
|
largv[2] = products[producti].out;
|
|
let pos: i32 = 3;
|
|
let li: i32 = nlink - 1;
|
|
for (li >= 0) {
|
|
let pi: i32 = linkorder[li];
|
|
if (g.pkg[root].variant == SEP_VARIANT_SAME_TEST
|
|
&& pi != root
|
|
&& g.pkg[pi].variant == SEP_VARIANT_PRODUCTION
|
|
&& g.pkg[pi].role != SEP_ROLE_TEST_SUPPORT
|
|
&& os.samefile(pathstr(g.pkg[pi].entry),
|
|
pathstr(g.pkg[root].entry))) {
|
|
li -= 1;
|
|
continue;
|
|
};
|
|
let suf: str = ".a";
|
|
if (pi == root) { suf = ".o"; };
|
|
largv[pos] = sepfname(g, pi, scratch, suf);
|
|
pos += 1;
|
|
li -= 1;
|
|
};
|
|
largv[pos] = libwwrt.ptr; pos += 1;
|
|
let k: i32 = 0;
|
|
for (k < nldirs) {
|
|
let n: u64 = cstrlen(ldirs[k]);
|
|
let flag: []u8 = alloc([], n + 3u64)!;
|
|
flag.len = (n + 3u64): i32;
|
|
flag[0] = 45u8; flag[1] = 76u8;
|
|
bytecpy(flag.ptr + 2u64, ldirs[k], n);
|
|
flag[n + 2u64] = 0u8;
|
|
largv[pos] = flag.ptr;
|
|
pos += 1;
|
|
k += 1;
|
|
};
|
|
k = 0;
|
|
for (k < nllibs) {
|
|
let n: u64 = cstrlen(llibs[k]);
|
|
let flag: []u8 = alloc([], n + 3u64)!;
|
|
flag.len = (n + 3u64): i32;
|
|
flag[0] = 45u8; flag[1] = 108u8;
|
|
bytecpy(flag.ptr + 2u64, llibs[k], n);
|
|
flag[n + 2u64] = 0u8;
|
|
largv[pos] = flag.ptr;
|
|
pos += 1;
|
|
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");
|
|
g.pkg[root].failed = true;
|
|
anyfailed = true;
|
|
producti += 1;
|
|
continue;
|
|
};
|
|
if (recordproductstatus(products[producti].status) != 0) {
|
|
cerr("ww: cannot record package-test product\n");
|
|
g.pkg[root].failed = true;
|
|
anyfailed = true;
|
|
};
|
|
producti += 1;
|
|
};
|
|
if (anyfailed) { 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,
|
|
rootidentity: *u8, out: *u8,
|
|
objstem: *u8, incs: *u8, lf: *lflags, packageonly: i32, istest: i32,
|
|
rootvariant: i32, testpackage: *u8, emitasm: i32,
|
|
keepscratch: i32, workdir: *u8) i32 = {
|
|
let scratch: *u8 = nil;
|
|
let g: *sepgraph = nil;
|
|
let product: sepproduct;
|
|
product.dir = src;
|
|
product.out = out;
|
|
product.testpackage = testpackage;
|
|
product.status = nil;
|
|
product.artifact = nil;
|
|
product.variant = rootvariant;
|
|
product.root = -1;
|
|
let r: i32 = buildonesepimpl(selfdir, src, entryisdir, rootidentity,
|
|
out, objstem,
|
|
incs, lf, packageonly, istest, &product, 1,
|
|
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;
|
|
};
|
|
|
|
// Build every selected directory/variant root inside one command-owned package
|
|
// universe. The first output owns the shared cold sepwork tree.
|
|
fn buildpackagetests(selfdir: *u8, src: *u8, incs: *u8, workdir: *u8,
|
|
products: *sepproduct, nproducts: i32) i32 = {
|
|
let scratch: *u8 = nil;
|
|
let g: *sepgraph = nil;
|
|
let lf: lflags;
|
|
lf.libdirs = nil; lf.nlibdirs = 0;
|
|
lf.libs = nil; lf.nlibs = 0;
|
|
let r: i32 = buildonesepimpl(selfdir, src, 1, nil,
|
|
products[0].out, products[0].out, incs, &lf, 0, 1,
|
|
products, nproducts, 0, workdir, &scratch, &g);
|
|
sepgraphfree(g);
|
|
return r;
|
|
};
|
|
|
|
fn cstrendswithlit(p: *u8, lit: str) bool = {
|
|
return strings.hassuffix(pathstr(p), lit);
|
|
};
|
|
|
|
fn productartifact(index: i32, variant: i32) *u8 = {
|
|
let buf: []u8 = alloc([], 64u64)!;
|
|
buf.len = 64;
|
|
let off: u64 = strinto(buf.ptr, 0u64, "__ww-test-");
|
|
buf[off] = (((index / 100) % 10) + 48): u8; off += 1u64;
|
|
buf[off] = (((index / 10) % 10) + 48): u8; off += 1u64;
|
|
buf[off] = ((index % 10) + 48): u8; off += 1u64;
|
|
off = byteinto(buf.ptr, off, '-': u8);
|
|
if (variant == SEP_VARIANT_SAME_TEST) {
|
|
off = strinto(buf.ptr, off, "same");
|
|
} else {
|
|
off = strinto(buf.ptr, off, "external");
|
|
};
|
|
cstrseal(buf.ptr, off);
|
|
return buf.ptr;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
// resolvemodule search-path 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;
|
|
};
|
|
|
|
// Mirrors cmd/ww/main.c:resolvemodule. 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);
|
|
|
|
if (cstrendswithlit(name, ".ww")) {
|
|
if (os.access(pathstr(name), 0i32) == 0) {
|
|
*isdir = 0;
|
|
return arenadupcstr(name, nlen);
|
|
};
|
|
};
|
|
|
|
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);
|
|
};
|
|
|
|
let search: *u8 = buildsearchpath(selfdir, incs);
|
|
return locateimport(search, name, nlen, isdir);
|
|
};
|
|
|
|
fn writeusage(fd: i32) void = {
|
|
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [-p] [-S] [-w DIR] [-I DIR] [-o FILE] [path] build a local package graph\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 the source library for foo.ww or foo/\n lib/foo directory: build its package sources\n -p emits a non-main archive FILE + FILE.wwi\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;
|
|
};
|
|
|
|
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;
|
|
};
|
|
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 packageonly: 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 (cstreqlit(p, "-p")) {
|
|
packageonly = 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) {
|
|
let dot: [2]u8 = ['.': u8, 0u8];
|
|
src = &dot[0];
|
|
};
|
|
if (packageonly != 0 && emitasm != 0) {
|
|
cerr("ww build: -p and -S cannot be combined\n");
|
|
return 2;
|
|
};
|
|
let requestedliteral: bool = false;
|
|
let requestedstat: os.filestat;
|
|
match (os.stat(&requestedstat, pathstr(src))) {
|
|
case void => requestedliteral = true;
|
|
case let e: os.oserror => void;
|
|
};
|
|
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;
|
|
};
|
|
if (packageonly != 0 && isdir == 0) {
|
|
cerr("ww build: -p needs a package directory\n");
|
|
return 2;
|
|
};
|
|
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;
|
|
let rootidentity: *u8 = nil;
|
|
if (packageonly != 0 && !requestedliteral) { rootidentity = src; };
|
|
return buildonesep(selfdir, resolved, isdir, rootidentity,
|
|
out, objstem, incs.ptr, &lf,
|
|
packageonly, 0i32, SEP_VARIANT_PRODUCTION, nil,
|
|
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();
|
|
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;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
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, nil, outp, outp, incs.ptr, &lf,
|
|
0i32, 0i32, SEP_VARIANT_PRODUCTION, nil, 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;
|
|
};
|
|
|
|
// 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;
|
|
};
|
|
|
|
// 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, nil, outp, objstem, incs, &lf,
|
|
0i32, 1i32, SEP_VARIANT_PRODUCTION, nil, 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 products: []sepproduct = alloc([], SEP_MAXPRODUCT: u64)!;
|
|
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 (cstreqlit(p, "--ww-package-test")) {
|
|
if (i + 5 >= argc || products.len >= SEP_MAXPRODUCT) {
|
|
cerr("ww test: --ww-package-test needs kind, package, directory, output, and status\n");
|
|
return 2;
|
|
};
|
|
let kind: *u8 = argv[i + 1];
|
|
let name: *u8 = argv[i + 2];
|
|
let dir: *u8 = argv[i + 3];
|
|
let output: *u8 = argv[i + 4];
|
|
let status: *u8 = argv[i + 5];
|
|
let pn: u64 = cstrlen(name);
|
|
let variant: i32 = SEP_VARIANT_EXTERNAL;
|
|
if (cstreqlit(kind, "same")) {
|
|
variant = SEP_VARIANT_SAME_TEST;
|
|
};
|
|
if ((!cstreqlit(kind, "same")
|
|
&& !cstreqlit(kind, "external"))
|
|
|| pn == 0u64 || pn >= 256u64
|
|
|| dir[0u64] == 0u8 || output[0u64] == 0u8
|
|
|| status[0u64] == 0u8
|
|
|| (cstreqlit(kind, "external")
|
|
&& (pn <= 5u64
|
|
|| !cstrendswithlit(name,
|
|
"_test")))) {
|
|
cerr("ww test: invalid --ww-package-test variant\n");
|
|
return 2;
|
|
};
|
|
let product: sepproduct;
|
|
product.dir = dir;
|
|
product.out = output;
|
|
product.testpackage = name;
|
|
product.status = status;
|
|
product.artifact = nil;
|
|
product.variant = variant;
|
|
product.root = -1;
|
|
append(products, product);
|
|
i += 6;
|
|
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;
|
|
};
|
|
let producti: i32 = 1;
|
|
for (producti < products.len) {
|
|
let product: sepproduct = products[producti];
|
|
let j: i32 = producti;
|
|
for (j > 0 && (strings.compare(pathstr(products[j - 1].dir),
|
|
pathstr(product.dir)) > 0
|
|
|| (strings.compare(pathstr(products[j - 1].dir),
|
|
pathstr(product.dir)) == 0
|
|
&& products[j - 1].variant > product.variant))) {
|
|
products[j] = products[j - 1];
|
|
j -= 1;
|
|
};
|
|
products[j] = product;
|
|
producti += 1;
|
|
};
|
|
producti = 0;
|
|
for (producti < products.len) {
|
|
if (producti > 0
|
|
&& cstreq(products[producti - 1].dir, products[producti].dir)
|
|
&& products[producti - 1].variant
|
|
== products[producti].variant) {
|
|
cerr("ww test: duplicate --ww-package-test variant for directory\n");
|
|
return 2;
|
|
};
|
|
products[producti].artifact = productartifact(producti,
|
|
products[producti].variant);
|
|
producti += 1;
|
|
};
|
|
if (products.len != 0 && packageopts) {
|
|
cerr("ww test: package-test variant rejects package options\n");
|
|
return 2;
|
|
};
|
|
if (products.len != 0 && outstem != nil) {
|
|
cerr("ww test: package-test products reject -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 (products.len != 0) {
|
|
cerr("ww test: package-test variant needs one directory\n");
|
|
return 2;
|
|
};
|
|
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
|
|
// for the complete selected test request.
|
|
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 (products.len != 0) {
|
|
cerr("ww test: package-test variant needs one directory\n");
|
|
return 2;
|
|
};
|
|
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;
|
|
};
|
|
if (products.len != 0) {
|
|
if (compileonly == 0) {
|
|
cerr("ww test: package-test products need -c\n");
|
|
return 2;
|
|
};
|
|
return buildpackagetests(selfdir, resolved, incs.ptr, workdir,
|
|
products.ptr, products.len);
|
|
};
|
|
let replacement: *u8 = nil;
|
|
if (resolved != target) { replacement = resolved; };
|
|
return execpackagetests(selfdir, argv, argc, start, targetindex,
|
|
replacement, targetindex < 0);
|
|
};
|
|
|
|
export fn main(argc: i32, argv: **u8) i32 = {
|
|
if (argc < 1) {
|
|
writeusage(2);
|
|
return 2;
|
|
};
|
|
|
|
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: ");
|
|
os.write(2, cmd, cstrlen(cmd));
|
|
cerr("\n");
|
|
writeusage(2);
|
|
return 2;
|
|
};
|