selfhost/cmd/{ww,wwdump}: drop snake_case from main.ww helpers
This commit is contained in:
@@ -352,8 +352,8 @@ fn cstreq(a: *u8, b: *u8) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
// cstreq_lit — compare a NUL-terminated *u8 to a ww string literal.
|
||||
fn cstreq_lit(a: *u8, lit: str) bool = {
|
||||
// cstreqlit — compare a NUL-terminated *u8 to a ww string literal.
|
||||
fn cstreqlit(a: *u8, lit: str) bool = {
|
||||
let n: i32 = lit.len;
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
@@ -364,7 +364,7 @@ fn cstreq_lit(a: *u8, lit: str) bool = {
|
||||
};
|
||||
|
||||
// startswith — does a have b as a prefix?
|
||||
fn cstr_startswith(a: *u8, b: *u8) bool = {
|
||||
fn cstrstartswith(a: *u8, b: *u8) bool = {
|
||||
let i: u64 = 0u64;
|
||||
for (b[i] != 0u8) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
@@ -384,7 +384,7 @@ fn bytecpy(dst: *u8, src: *u8, n: u64) void = {
|
||||
|
||||
// Copy a NUL-terminated *u8 into dst starting at off; return the new
|
||||
// offset (without writing a NUL).
|
||||
fn cstr_into(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
fn cstrinto(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
let i: u64 = 0u64;
|
||||
for (src[i] != 0u8) {
|
||||
dst[off + i] = src[i];
|
||||
@@ -394,7 +394,7 @@ fn cstr_into(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
};
|
||||
|
||||
// Same, but for a ww `str` (no NUL on the source side; we copy len bytes).
|
||||
fn str_into(dst: *u8, off: u64, src: str) u64 = {
|
||||
fn strinto(dst: *u8, off: u64, src: str) u64 = {
|
||||
let n: i32 = src.len;
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
@@ -407,14 +407,14 @@ fn str_into(dst: *u8, off: u64, src: str) u64 = {
|
||||
};
|
||||
|
||||
// Write a single byte, return new offset.
|
||||
fn byte_into(dst: *u8, off: u64, c: u8) u64 = {
|
||||
fn byteinto(dst: *u8, off: u64, c: u8) u64 = {
|
||||
dst[off] = c;
|
||||
return off + 1u64;
|
||||
};
|
||||
|
||||
// NUL-terminate at off and return the same off (handy when passing the
|
||||
// buffer to a syscall that expects a C-string).
|
||||
fn cstr_seal(dst: *u8, off: u64) void = {
|
||||
fn cstrseal(dst: *u8, off: u64) void = {
|
||||
dst[off] = 0u8;
|
||||
};
|
||||
|
||||
@@ -422,7 +422,7 @@ fn cstr_seal(dst: *u8, off: u64) void = {
|
||||
|
||||
// dirname-equivalent: copy argv[0] up to (but not including) the last
|
||||
// '/' into dst, NUL-terminated. If no slash, write ".".
|
||||
fn self_dir_into(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
fn selfdirinto(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
let n: u64 = cstrlen(argv0);
|
||||
let cut: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
@@ -441,31 +441,31 @@ fn self_dir_into(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
};
|
||||
|
||||
// Build "$dir/$name" (NUL-terminated) into a fresh page-sized buffer.
|
||||
fn join_path(dir: *u8, name: *u8) *u8 = {
|
||||
fn joinpath(dir: *u8, name: *u8) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, dir);
|
||||
off = byte_into(buf, off, 47u8);
|
||||
off = cstr_into(buf, off, name);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, dir);
|
||||
off = byteinto(buf, off, 47u8);
|
||||
off = cstrinto(buf, off, name);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// Same, but the second component is a ww `str` literal.
|
||||
fn join_path_lit(dir: *u8, name: str) *u8 = {
|
||||
fn joinpathlit(dir: *u8, name: str) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, dir);
|
||||
off = byte_into(buf, off, 47u8);
|
||||
off = str_into(buf, off, name);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, dir);
|
||||
off = byteinto(buf, off, 47u8);
|
||||
off = strinto(buf, off, name);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// ---- Subprocess plumbing ----------------------------------------------
|
||||
|
||||
// proc_run — fork, execve `path` with `argv` (NULL-terminated), wait.
|
||||
// procrun — fork, execve `path` with `argv` (NULL-terminated), wait.
|
||||
// Returns 0 on clean exit-0, 1 on any non-zero exit or signal kill,
|
||||
// -1 on fork/wait failure.
|
||||
fn proc_run(path: *u8, argv: **u8) i32 = {
|
||||
fn procrun(path: *u8, argv: **u8) i32 = {
|
||||
let pid: i32 = os.fork();
|
||||
if (pid < 0) {
|
||||
os.write(2, "ww: fork failed\n".ptr, 16u64);
|
||||
@@ -509,7 +509,7 @@ type expctx = struct {
|
||||
visit: *strnode,
|
||||
};
|
||||
|
||||
fn visit_seen(c: *expctx, path: str) bool = {
|
||||
fn visitseen(c: *expctx, path: str) bool = {
|
||||
let n: *strnode = c.visit;
|
||||
for (n != nil) {
|
||||
if (n.s.len == path.len) {
|
||||
@@ -526,7 +526,7 @@ fn visit_seen(c: *expctx, path: str) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
fn visit_add(c: *expctx, path: str) void = {
|
||||
fn visitadd(c: *expctx, path: str) void = {
|
||||
let n: *strnode = amalloc(c.a, 32u64): *strnode;
|
||||
n.s = path;
|
||||
n.snext = c.visit;
|
||||
@@ -535,7 +535,7 @@ fn visit_add(c: *expctx, path: str) void = {
|
||||
|
||||
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
||||
// arena-resident path if found, else nil.
|
||||
fn locate_in(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
||||
fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
||||
// candidate 1: <dir>/<name>.ww
|
||||
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||
let off: u64 = 0u64;
|
||||
@@ -575,7 +575,7 @@ fn locate_in(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 =
|
||||
};
|
||||
|
||||
// Walk a colon-separated dirlist, return first hit or nil.
|
||||
fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
let total: u64 = cstrlen(dirs);
|
||||
let p: u64 = 0u64;
|
||||
for (p < total) {
|
||||
@@ -584,9 +584,9 @@ fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
if (dirs[q] == 58u8) { break; }; // ':'
|
||||
q += 1u64;
|
||||
};
|
||||
let seg_len: u64 = q - p;
|
||||
if (seg_len > 0u64) {
|
||||
let hit: *u8 = locate_in(a, dirs + p, seg_len, name, name_len);
|
||||
let seglen: u64 = q - p;
|
||||
if (seglen > 0u64) {
|
||||
let hit: *u8 = locatein(a, dirs + p, seglen, name, name_len);
|
||||
if (hit != nil) { return hit; };
|
||||
};
|
||||
p = q + 1u64;
|
||||
@@ -596,7 +596,7 @@ fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
|
||||
// ---- file slurp -------------------------------------------------------
|
||||
|
||||
fn read_all(path_cs: *u8) (*u8, u64) = {
|
||||
fn readall(path_cs: *u8) (*u8, u64) = {
|
||||
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) { return nil, 0u64; };
|
||||
let n: i64 = os.filesize(fd);
|
||||
@@ -610,7 +610,7 @@ fn read_all(path_cs: *u8) (*u8, u64) = {
|
||||
return buf, nu;
|
||||
};
|
||||
|
||||
fn is_ident_byte(c: u8) bool = {
|
||||
fn isidentbyte(c: u8) bool = {
|
||||
if (c >= 97u8) { if (c <= 122u8) { return true; }; }; // a..z
|
||||
if (c >= 65u8) { if (c <= 90u8) { return true; }; }; // A..Z
|
||||
if (c >= 48u8) { if (c <= 57u8) { return true; }; }; // 0..9
|
||||
@@ -622,7 +622,7 @@ fn is_ident_byte(c: u8) bool = {
|
||||
// Scan one `use IDENT;` line out of [start, end). Returns the start of
|
||||
// the ident and its length, or (nil, 0) if no `use` here. The caller
|
||||
// passes a slice of the source: src points at the line start.
|
||||
fn scan_use(src: *u8, len: u64) (*u8, u64) = {
|
||||
fn scanuse(src: *u8, len: u64) (*u8, u64) = {
|
||||
let i: u64 = 0u64;
|
||||
// skip leading whitespace
|
||||
for (i < len) {
|
||||
@@ -640,27 +640,27 @@ fn scan_use(src: *u8, len: u64) (*u8, u64) = {
|
||||
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
|
||||
i += 1u64;
|
||||
};
|
||||
let id_start: u64 = i;
|
||||
let idstart: u64 = i;
|
||||
for (i < len) {
|
||||
if (!is_ident_byte(src[i])) { break; };
|
||||
if (!isidentbyte(src[i])) { break; };
|
||||
i += 1u64;
|
||||
};
|
||||
let id_len: u64 = i - id_start;
|
||||
if (id_len == 0u64) { return nil, 0u64; };
|
||||
return src + id_start, id_len;
|
||||
let idlen: u64 = i - idstart;
|
||||
if (idlen == 0u64) { return nil, 0u64; };
|
||||
return src + idstart, idlen;
|
||||
};
|
||||
|
||||
// Recursively expand `path` into c.out. Imported files are emitted
|
||||
// before their importer; cycles are broken via the visited set.
|
||||
fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
let plen: u64 = cstrlen(path_cs);
|
||||
let path_str: str = astrndup(c.a, path_cs, plen);
|
||||
if (visit_seen(c, path_str)) { return; };
|
||||
visit_add(c, path_str);
|
||||
let pathstr: str = astrndup(c.a, path_cs, plen);
|
||||
if (visitseen(c, pathstr)) { return; };
|
||||
visitadd(c, pathstr);
|
||||
|
||||
let bufp: *u8;
|
||||
let blen: u64;
|
||||
bufp, blen = read_all(path_cs);
|
||||
bufp, blen = readall(path_cs);
|
||||
if (bufp == nil) {
|
||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||
return;
|
||||
@@ -675,11 +675,11 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
if (bufp[j] == 10u8) { break; }; // '\n'
|
||||
j += 1u64;
|
||||
};
|
||||
let id_p: *u8;
|
||||
let id_n: u64;
|
||||
id_p, id_n = scan_use(bufp + i, j - i);
|
||||
if (id_p != nil) {
|
||||
let ipath: *u8 = locate_import(c.a, c.dirs, id_p, id_n);
|
||||
let idp: *u8;
|
||||
let idn: u64;
|
||||
idp, idn = scanuse(bufp + i, j - i);
|
||||
if (idp != nil) {
|
||||
let ipath: *u8 = locateimport(c.a, c.dirs, idp, idn);
|
||||
if (ipath != nil) {
|
||||
expand(c, ipath);
|
||||
};
|
||||
@@ -697,7 +697,7 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
// Strip the trailing ".ww" off `src` (a NUL-terminated path) into
|
||||
// `stem`, NUL-terminated. If there's no .ww, the stem is the whole
|
||||
// path.
|
||||
fn make_stem(stem: *u8, src: *u8) void = {
|
||||
fn makestem(stem: *u8, src: *u8) void = {
|
||||
let n: u64 = cstrlen(src);
|
||||
let stop: u64 = n;
|
||||
if (n >= 3u64) {
|
||||
@@ -715,25 +715,25 @@ fn make_stem(stem: *u8, src: *u8) void = {
|
||||
};
|
||||
|
||||
// Append a literal suffix to `stem` (which already lives in a buffer).
|
||||
fn append_lit(stem: *u8, suffix: str) *u8 = {
|
||||
fn appendlit(stem: *u8, suffix: str) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, stem);
|
||||
off = str_into(buf, off, suffix);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, stem);
|
||||
off = strinto(buf, off, suffix);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// linker flags bundled as a struct so build_one stays at the wwstage
|
||||
// linker flags bundled as a struct so buildone stays at the wwstage
|
||||
// w6c's 6-argument calling-convention limit.
|
||||
type lflags = struct {
|
||||
libdirs: **u8,
|
||||
n_libdirs: i32,
|
||||
nlibdirs: i32,
|
||||
libs: **u8,
|
||||
n_libs: i32,
|
||||
nlibs: i32,
|
||||
};
|
||||
|
||||
// build_one — compile `src` into the executable named `out`.
|
||||
// self_dir: NUL-terminated dir containing this driver and the
|
||||
// buildone — compile `src` into the executable named `out`.
|
||||
// selfdir: NUL-terminated dir containing this driver and the
|
||||
// wwstage tools (w6c_ww/w6a_ww/w6l_ww)
|
||||
// src: NUL-terminated path to the .ww file
|
||||
// out: NUL-terminated desired output path
|
||||
@@ -744,46 +744,46 @@ type lflags = struct {
|
||||
// touches no C-built code at runtime. The C `ww` driver in cmd/ww/
|
||||
// still drives the C-built w6c/w6a/w6l. Test 993 pins the two
|
||||
// pipelines to byte-identical output on a corpus.
|
||||
fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
fn buildone(selfdir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
let a: *arena = newarena();
|
||||
|
||||
let c6: *u8 = join_path_lit(self_dir, "w6c_ww");
|
||||
let a6: *u8 = join_path_lit(self_dir, "w6a_ww");
|
||||
let l6: *u8 = join_path_lit(self_dir, "w6l_ww");
|
||||
let c6: *u8 = joinpathlit(selfdir, "w6c_ww");
|
||||
let a6: *u8 = joinpathlit(selfdir, "w6a_ww");
|
||||
let l6: *u8 = joinpathlit(selfdir, "w6l_ww");
|
||||
|
||||
// Default lib search path: <self_dir>/../../lib
|
||||
let dotdot_lib: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
// Default lib search path: <selfdir>/../../lib
|
||||
let dotdotlib: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
{
|
||||
let off: u64 = cstr_into(dotdot_lib, 0u64, self_dir);
|
||||
off = str_into(dotdot_lib, off, "/../../lib");
|
||||
cstr_seal(dotdot_lib, off);
|
||||
let off: u64 = cstrinto(dotdotlib, 0u64, selfdir);
|
||||
off = strinto(dotdotlib, off, "/../../lib");
|
||||
cstrseal(dotdotlib, off);
|
||||
};
|
||||
|
||||
// Compose searchpath: incs + ':' + dotdot_lib (or just dotdot_lib).
|
||||
// Compose searchpath: incs + ':' + dotdotlib (or just dotdotlib).
|
||||
let searchpath: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
{
|
||||
let off: u64 = 0u64;
|
||||
if (incs[0u64] != 0u8) {
|
||||
off = cstr_into(searchpath, off, incs);
|
||||
off = byte_into(searchpath, off, 58u8); // ':'
|
||||
off = cstrinto(searchpath, off, incs);
|
||||
off = byteinto(searchpath, off, 58u8); // ':'
|
||||
};
|
||||
off = cstr_into(searchpath, off, dotdot_lib);
|
||||
cstr_seal(searchpath, off);
|
||||
off = cstrinto(searchpath, off, dotdotlib);
|
||||
cstrseal(searchpath, off);
|
||||
};
|
||||
|
||||
// stem, .s, .o, .combined.ww, libwwrt.a
|
||||
let stem: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_stem(stem, src);
|
||||
let asmf: *u8 = append_lit(stem, ".s");
|
||||
let objf: *u8 = append_lit(stem, ".o");
|
||||
let combined: *u8 = append_lit(stem, ".combined.ww");
|
||||
makestem(stem, src);
|
||||
let asmf: *u8 = appendlit(stem, ".s");
|
||||
let objf: *u8 = appendlit(stem, ".o");
|
||||
let combined: *u8 = appendlit(stem, ".combined.ww");
|
||||
|
||||
// libwwrt.a path: <self_dir>/../lib/libwwrt.a
|
||||
// libwwrt.a path: <selfdir>/../lib/libwwrt.a
|
||||
let libwwrt: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
{
|
||||
let off: u64 = cstr_into(libwwrt, 0u64, self_dir);
|
||||
off = str_into(libwwrt, off, "/../lib/libwwrt.a");
|
||||
cstr_seal(libwwrt, off);
|
||||
let off: u64 = cstrinto(libwwrt, 0u64, selfdir);
|
||||
off = strinto(libwwrt, off, "/../lib/libwwrt.a");
|
||||
cstrseal(libwwrt, off);
|
||||
};
|
||||
|
||||
// Step 1: expand `use`s into the combined file.
|
||||
@@ -810,7 +810,7 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[2] = asmf;
|
||||
argv[3] = combined;
|
||||
argv[4] = nil;
|
||||
if (proc_run(c6, argv) != 0) {
|
||||
if (procrun(c6, argv) != 0) {
|
||||
os.write(2, "ww: w6c failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -824,7 +824,7 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[2] = objf;
|
||||
argv[3] = asmf;
|
||||
argv[4] = nil;
|
||||
if (proc_run(a6, argv) != 0) {
|
||||
if (procrun(a6, argv) != 0) {
|
||||
os.write(2, "ww: w6a failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -832,21 +832,21 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
|
||||
// Step 4: w6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
|
||||
{
|
||||
let nl_dirs: i32 = 0;
|
||||
let nl_libs: i32 = 0;
|
||||
let l_dirs: **u8 = nil;
|
||||
let l_libs: **u8 = nil;
|
||||
let nldirs: i32 = 0;
|
||||
let nllibs: i32 = 0;
|
||||
let ldirs: **u8 = nil;
|
||||
let llibs: **u8 = nil;
|
||||
if (lf != nil) {
|
||||
nl_dirs = lf.n_libdirs;
|
||||
nl_libs = lf.n_libs;
|
||||
l_dirs = lf.libdirs;
|
||||
l_libs = lf.libs;
|
||||
nldirs = lf.nlibdirs;
|
||||
nllibs = lf.nlibs;
|
||||
ldirs = lf.libdirs;
|
||||
llibs = lf.libs;
|
||||
};
|
||||
// argv slots: 5 fixed (w6l, -o, out, objf, libwwrt)
|
||||
// + 2 * n_libdirs (-L, dir)
|
||||
// + 2 * n_libs (-l, name)
|
||||
// + 2 * nlibdirs (-L, dir)
|
||||
// + 2 * nlibs (-l, name)
|
||||
// + 1 nil terminator.
|
||||
let total: i32 = 5 + 2 * nl_dirs + 2 * nl_libs + 1;
|
||||
let total: i32 = 5 + 2 * nldirs + 2 * nllibs + 1;
|
||||
let argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
argv[0] = "w6l\0".ptr;
|
||||
argv[1] = "-o\0".ptr;
|
||||
@@ -855,21 +855,21 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[4] = libwwrt;
|
||||
let pos: i32 = 5;
|
||||
let k: i32 = 0;
|
||||
for (k < nl_dirs) {
|
||||
for (k < nldirs) {
|
||||
argv[pos] = "-L\0".ptr;
|
||||
argv[pos + 1] = l_dirs[k];
|
||||
argv[pos + 1] = ldirs[k];
|
||||
pos += 2;
|
||||
k += 1;
|
||||
};
|
||||
k = 0;
|
||||
for (k < nl_libs) {
|
||||
for (k < nllibs) {
|
||||
argv[pos] = "-l\0".ptr;
|
||||
argv[pos + 1] = l_libs[k];
|
||||
argv[pos + 1] = llibs[k];
|
||||
pos += 2;
|
||||
k += 1;
|
||||
};
|
||||
argv[pos] = nil;
|
||||
if (proc_run(l6, argv) != 0) {
|
||||
if (procrun(l6, argv) != 0) {
|
||||
os.write(2, "ww: w6l failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -879,15 +879,15 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
|
||||
// ---- Module-by-name resolution ----------------------------------------
|
||||
//
|
||||
// Mirrors cmd/ww/main.c:resolve_module. Maps a name like "foo", "lib/foo",
|
||||
// Mirrors cmd/ww/main.c:resolvemodule. Maps a name like "foo", "lib/foo",
|
||||
// "foo.ww", or "." to a concrete .ww file path:
|
||||
// 1. literal <name>.ww that exists → use as-is
|
||||
// 2. "." → <cwd>/<basename(cwd)>.ww → that, if it exists
|
||||
// 3. <name>/<basename(name)>.ww → that, if it exists
|
||||
// 4. walk search path (cwd:incs:<self_dir>/../../lib):
|
||||
// 4. walk search path (cwd:incs:<selfdir>/../../lib):
|
||||
// <dir>/<name>.ww or <dir>/<name>/<name>.ww
|
||||
|
||||
fn cstr_endswith_lit(p: *u8, lit: str) bool = {
|
||||
fn cstrendswithlit(p: *u8, lit: str) bool = {
|
||||
let plen: u64 = cstrlen(p);
|
||||
let slen: u64 = lit.len: u64;
|
||||
if (plen < slen) { return false; };
|
||||
@@ -901,9 +901,9 @@ fn cstr_endswith_lit(p: *u8, lit: str) bool = {
|
||||
return true;
|
||||
};
|
||||
|
||||
// basename_off — return the offset of the last path segment within `p`
|
||||
// basenameoff — return the offset of the last path segment within `p`
|
||||
// (i.e. one past the final '/'). Returns 0 if there's no slash.
|
||||
fn basename_off(p: *u8, plen: u64) u64 = {
|
||||
fn basenameoff(p: *u8, plen: u64) u64 = {
|
||||
let start: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
for (i < plen) {
|
||||
@@ -913,9 +913,9 @@ fn basename_off(p: *u8, plen: u64) u64 = {
|
||||
return start;
|
||||
};
|
||||
|
||||
// arena_dup_cstr — copy `plen` bytes from `src` into a fresh NUL-sealed
|
||||
// arenadupcstr — copy `plen` bytes from `src` into a fresh NUL-sealed
|
||||
// arena buffer.
|
||||
fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
fn arenadupcstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
let buf: *u8 = amalloc(a, plen + 1u64): *u8;
|
||||
let i: u64 = 0u64;
|
||||
for (i < plen) { buf[i] = src[i]; i += 1u64; };
|
||||
@@ -923,8 +923,8 @@ fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
return buf;
|
||||
};
|
||||
|
||||
// build_dir_module_path — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
|
||||
fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
|
||||
// builddirmodulepath — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
|
||||
fn builddirmodulepath(a: *arena, dir: *u8, dlen: u64,
|
||||
base: *u8, blen: u64) *u8 = {
|
||||
let need: u64 = dlen + 1u64 + blen + 3u64 + 1u64;
|
||||
let buf: *u8 = amalloc(a, need): *u8;
|
||||
@@ -941,32 +941,32 @@ fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
|
||||
return buf;
|
||||
};
|
||||
|
||||
// build_search_path — compose the colon-separated lookup path used by
|
||||
// resolve_module's case (4). Order: "." : <incs> : <self_dir>/../../lib
|
||||
fn build_search_path(a: *arena, self_dir: *u8, incs: *u8) *u8 = {
|
||||
// buildsearchpath — compose the colon-separated lookup path used by
|
||||
// resolvemodule's case (4). Order: "." : <incs> : <selfdir>/../../lib
|
||||
fn buildsearchpath(a: *arena, selfdir: *u8, incs: *u8) *u8 = {
|
||||
let buf: *u8 = amalloc(a, PATH_MAX * 2u64): *u8;
|
||||
let off: u64 = 0u64;
|
||||
buf[off] = 46u8; off += 1u64; // '.'
|
||||
if (incs != nil) {
|
||||
if (incs[0u64] != 0u8) {
|
||||
buf[off] = 58u8; off += 1u64; // ':'
|
||||
off = cstr_into(buf, off, incs);
|
||||
off = cstrinto(buf, off, incs);
|
||||
};
|
||||
};
|
||||
buf[off] = 58u8; off += 1u64;
|
||||
off = cstr_into(buf, off, self_dir);
|
||||
off = str_into(buf, off, "/../../lib");
|
||||
cstr_seal(buf, off);
|
||||
off = cstrinto(buf, off, selfdir);
|
||||
off = strinto(buf, off, "/../../lib");
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
fn resolvemodule(a: *arena, selfdir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
let nlen: u64 = cstrlen(name);
|
||||
|
||||
// (1) literal .ww file that exists
|
||||
if (cstr_endswith_lit(name, ".ww")) {
|
||||
if (cstrendswithlit(name, ".ww")) {
|
||||
if (os.access(name, 0i32) == 0) {
|
||||
return arena_dup_cstr(a, name, nlen);
|
||||
return arenadupcstr(a, name, nlen);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -977,11 +977,11 @@ fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
let r: i64 = os.getcwd(cwd, PATH_MAX);
|
||||
if (r <= 0i64) { return nil; };
|
||||
let cwdlen: u64 = (r: u64) - 1u64; // strip trailing NUL
|
||||
let bo: u64 = basename_off(cwd, cwdlen);
|
||||
let bo: u64 = basenameoff(cwd, cwdlen);
|
||||
let blen: u64 = cwdlen - bo;
|
||||
let dot: *u8 = amalloc(a, 2u64): *u8;
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
let probe: *u8 = build_dir_module_path(a, dot, 1u64,
|
||||
let probe: *u8 = builddirmodulepath(a, dot, 1u64,
|
||||
cwd + bo, blen);
|
||||
if (os.access(probe, 0i32) == 0) { return probe; };
|
||||
return nil;
|
||||
@@ -989,31 +989,31 @@ fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
};
|
||||
|
||||
// (3) <name>/<basename(name)>.ww — directory-as-module
|
||||
let bo: u64 = basename_off(name, nlen);
|
||||
let probe: *u8 = build_dir_module_path(a, name, nlen,
|
||||
let bo: u64 = basenameoff(name, nlen);
|
||||
let probe: *u8 = builddirmodulepath(a, name, nlen,
|
||||
name + bo, nlen - bo);
|
||||
if (os.access(probe, 0i32) == 0) { return probe; };
|
||||
|
||||
// (4) search path lookup
|
||||
let search: *u8 = build_search_path(a, self_dir, incs);
|
||||
return locate_import(a, search, name, nlen);
|
||||
let search: *u8 = buildsearchpath(a, selfdir, incs);
|
||||
return locateimport(a, search, name, nlen);
|
||||
};
|
||||
|
||||
// ---- Subcommand handlers ----------------------------------------------
|
||||
|
||||
fn write_usage(fd: i32) void = {
|
||||
fn writeusage(fd: i32) void = {
|
||||
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [path] compile module to a static binary (path defaults to cwd)\n run [path] ... build then exec, passing extra args to the program\n test [path] build and run *_test.ww in the module (path defaults to cwd)\n version print version and exit\n\n path forms:\n foo.ww literal file\n foo search cwd, -I dirs, then $WW_LIB-equiv for foo.ww or foo/foo.ww\n lib/foo directory: build lib/foo/foo.ww\n . build the cwd's <basename>.ww\n";
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
fn do_version() i32 = {
|
||||
fn doversion() i32 = {
|
||||
os.write(1, "ww 0.0\n".ptr, 7u64);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Compute the basename of src (without trailing ".ww") into a fresh
|
||||
// buffer. Used as the default output path for `ww build`.
|
||||
fn default_out_path(src: *u8) *u8 = {
|
||||
fn defaultoutpath(src: *u8) *u8 = {
|
||||
let n: u64 = cstrlen(src);
|
||||
let start: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
@@ -1039,22 +1039,22 @@ fn default_out_path(src: *u8) *u8 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
cstr_seal(out, off);
|
||||
cstrseal(out, off);
|
||||
return out;
|
||||
};
|
||||
|
||||
fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dobuild(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let src: *u8 = nil;
|
||||
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
let inc_off: u64 = 0u64;
|
||||
cstr_seal(incs, 0u64);
|
||||
let incoff: u64 = 0u64;
|
||||
cstrseal(incs, 0u64);
|
||||
|
||||
let max_lflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libs: i32 = 0;
|
||||
let maxlflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibs: i32 = 0;
|
||||
|
||||
let i: i32 = start;
|
||||
for (i < argc) {
|
||||
@@ -1072,12 +1072,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (inc_off > 0u64) {
|
||||
incs[inc_off] = 58u8; // ':'
|
||||
inc_off += 1u64;
|
||||
if (incoff > 0u64) {
|
||||
incs[incoff] = 58u8; // ':'
|
||||
incoff += 1u64;
|
||||
};
|
||||
inc_off = cstr_into(incs, inc_off, dir);
|
||||
cstr_seal(incs, inc_off);
|
||||
incoff = cstrinto(incs, incoff, dir);
|
||||
cstrseal(incs, incoff);
|
||||
} else { if (p[1u64] == 76u8) { // '-L'
|
||||
let dir: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -1090,12 +1090,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (n_libdirs >= max_lflags) {
|
||||
if (nlibdirs >= maxlflags) {
|
||||
os.write(2, "ww build: too many -L\n".ptr, 22u64);
|
||||
return 2;
|
||||
};
|
||||
libdirs[n_libdirs] = dir;
|
||||
n_libdirs += 1;
|
||||
libdirs[nlibdirs] = dir;
|
||||
nlibdirs += 1;
|
||||
} else { if (p[1u64] == 108u8) { // '-l'
|
||||
let nm: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -1108,12 +1108,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
nm = argv[i];
|
||||
};
|
||||
if (n_libs >= max_lflags) {
|
||||
if (nlibs >= maxlflags) {
|
||||
os.write(2, "ww build: too many -l\n".ptr, 22u64);
|
||||
return 2;
|
||||
};
|
||||
libs[n_libs] = nm;
|
||||
n_libs += 1;
|
||||
libs[nlibs] = nm;
|
||||
nlibs += 1;
|
||||
} else {
|
||||
os.write(2, "ww build: unknown flag\n".ptr, 23u64);
|
||||
return 2;
|
||||
@@ -1130,26 +1130,26 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
src = dot;
|
||||
};
|
||||
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
|
||||
let resolved: *u8 = resolvemodule(a, selfdir, src, incs);
|
||||
if (resolved == nil) {
|
||||
os.write(2, "ww build: cannot find module\n".ptr, 29u64);
|
||||
return 1;
|
||||
};
|
||||
let out: *u8 = default_out_path(resolved);
|
||||
let out: *u8 = defaultoutpath(resolved);
|
||||
let lf: lflags;
|
||||
lf.libdirs = libdirs;
|
||||
lf.n_libdirs = n_libdirs;
|
||||
lf.nlibdirs = nlibdirs;
|
||||
lf.libs = libs;
|
||||
lf.n_libs = n_libs;
|
||||
return build_one(self_dir, resolved, out, incs, &lf);
|
||||
lf.nlibs = nlibs;
|
||||
return buildone(selfdir, resolved, out, incs, &lf);
|
||||
};
|
||||
|
||||
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
|
||||
// terminated buf. Pid is folded in decimal manually since we don't
|
||||
// import strconv.
|
||||
fn make_run_tmp(buf: *u8) void = {
|
||||
fn makeruntmp(buf: *u8) void = {
|
||||
let off: u64 = 0u64;
|
||||
off = str_into(buf, off, "/tmp/ww_run_");
|
||||
off = strinto(buf, off, "/tmp/ww_run_");
|
||||
let pid: i32 = os.getpid();
|
||||
// itoa for non-negative pid
|
||||
let dig: [16]u8;
|
||||
@@ -1171,26 +1171,26 @@ fn make_run_tmp(buf: *u8) void = {
|
||||
off += 1u64;
|
||||
k -= 1;
|
||||
};
|
||||
cstr_seal(buf, off);
|
||||
cstrseal(buf, off);
|
||||
};
|
||||
|
||||
fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let src: *u8 = nil;
|
||||
let pass_start: i32 = -1; // first argv idx to pass through to program
|
||||
let passstart: i32 = -1; // first argv idx to pass through to program
|
||||
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
let inc_off: u64 = 0u64;
|
||||
cstr_seal(incs, 0u64);
|
||||
let incoff: u64 = 0u64;
|
||||
cstrseal(incs, 0u64);
|
||||
|
||||
let max_lflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libs: i32 = 0;
|
||||
let maxlflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibs: i32 = 0;
|
||||
|
||||
let i: i32 = start;
|
||||
for (i < argc) {
|
||||
if (pass_start >= 0) { i = argc; } // stop, leave rest for exec
|
||||
if (passstart >= 0) { i = argc; } // stop, leave rest for exec
|
||||
else {
|
||||
let p: *u8 = argv[i];
|
||||
if (p[0u64] == 45u8) {
|
||||
@@ -1206,12 +1206,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (inc_off > 0u64) {
|
||||
incs[inc_off] = 58u8;
|
||||
inc_off += 1u64;
|
||||
if (incoff > 0u64) {
|
||||
incs[incoff] = 58u8;
|
||||
incoff += 1u64;
|
||||
};
|
||||
inc_off = cstr_into(incs, inc_off, dir);
|
||||
cstr_seal(incs, inc_off);
|
||||
incoff = cstrinto(incs, incoff, dir);
|
||||
cstrseal(incs, incoff);
|
||||
} else { if (p[1u64] == 76u8) {
|
||||
let dir: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -1224,12 +1224,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (n_libdirs >= max_lflags) {
|
||||
if (nlibdirs >= maxlflags) {
|
||||
os.write(2, "ww run: too many -L\n".ptr, 20u64);
|
||||
return 2;
|
||||
};
|
||||
libdirs[n_libdirs] = dir;
|
||||
n_libdirs += 1;
|
||||
libdirs[nlibdirs] = dir;
|
||||
nlibdirs += 1;
|
||||
} else { if (p[1u64] == 108u8) {
|
||||
let nm: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -1242,12 +1242,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
nm = argv[i];
|
||||
};
|
||||
if (n_libs >= max_lflags) {
|
||||
if (nlibs >= maxlflags) {
|
||||
os.write(2, "ww run: too many -l\n".ptr, 20u64);
|
||||
return 2;
|
||||
};
|
||||
libs[n_libs] = nm;
|
||||
n_libs += 1;
|
||||
libs[nlibs] = nm;
|
||||
nlibs += 1;
|
||||
} else {
|
||||
os.write(2, "ww run: unknown flag\n".ptr, 21u64);
|
||||
return 2;
|
||||
@@ -1258,7 +1258,7 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
src = p;
|
||||
i += 1;
|
||||
} else {
|
||||
pass_start = i; // remaining args go to the program
|
||||
passstart = i; // remaining args go to the program
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -1269,64 +1269,64 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
src = dot;
|
||||
};
|
||||
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
|
||||
let resolved: *u8 = resolvemodule(a, selfdir, src, incs);
|
||||
if (resolved == nil) {
|
||||
os.write(2, "ww run: cannot find module\n".ptr, 27u64);
|
||||
return 1;
|
||||
};
|
||||
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
makeruntmp(tmp);
|
||||
let lf: lflags;
|
||||
lf.libdirs = libdirs;
|
||||
lf.n_libdirs = n_libdirs;
|
||||
lf.nlibdirs = nlibdirs;
|
||||
lf.libs = libs;
|
||||
lf.n_libs = n_libs;
|
||||
if (build_one(self_dir, resolved, tmp, incs, &lf) != 0) {
|
||||
lf.nlibs = nlibs;
|
||||
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
|
||||
os.unlink(tmp);
|
||||
return 1;
|
||||
};
|
||||
|
||||
// exec with [tmp, argv[pass_start..argc), nil]
|
||||
let n_extra: i32 = 0;
|
||||
if (pass_start >= 0) { n_extra = argc - pass_start; };
|
||||
let total: i32 = n_extra + 2;
|
||||
let exec_argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
// exec with [tmp, argv[passstart..argc), nil]
|
||||
let nextra: i32 = 0;
|
||||
if (passstart >= 0) { nextra = argc - passstart; };
|
||||
let total: i32 = nextra + 2;
|
||||
let execargv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
let k: i32 = 0;
|
||||
for (k < n_extra) {
|
||||
exec_argv[k + 1] = argv[pass_start + k];
|
||||
for (k < nextra) {
|
||||
execargv[k + 1] = argv[passstart + k];
|
||||
k += 1;
|
||||
};
|
||||
exec_argv[n_extra + 1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
execargv[nextra + 1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
// ---- ww test ----------------------------------------------------------
|
||||
//
|
||||
// Mirrors cmd/ww/main.c:do_test. Two modes:
|
||||
// Mirrors cmd/ww/main.c:dotest. Two modes:
|
||||
// single-file: build+run a literal *.ww file, return its exit code
|
||||
// directory: open the dir, getdents64, build+run each *_test.ww,
|
||||
// report ok/FAIL per file, return 0 iff all pass.
|
||||
|
||||
fn run_single_test(self_dir: *u8, src: *u8) i32 = {
|
||||
fn runsingletest(selfdir: *u8, src: *u8) i32 = {
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
if (build_one(self_dir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
makeruntmp(tmp);
|
||||
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
os.unlink(tmp);
|
||||
return 1;
|
||||
};
|
||||
let exec_argv: **u8 = os.alloc(16u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
exec_argv[1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
|
||||
let fd: i32 = os.open(dir, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) {
|
||||
os.write(2, "ww test: cannot open directory\n".ptr, 31u64);
|
||||
@@ -1348,33 +1348,33 @@ fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
let bhi: u64 = (buf[off + 17u64]): u64;
|
||||
let reclen: u64 = blo + (bhi * 256u64);
|
||||
let name: *u8 = buf + off + 19u64;
|
||||
if (cstr_endswith_lit(name, "_test.ww")) {
|
||||
if (cstrendswithlit(name, "_test.ww")) {
|
||||
let nlen: u64 = cstrlen(name);
|
||||
// path = <dir>/<name>
|
||||
let path: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let p_off: u64 = cstr_into(path, 0u64, dir);
|
||||
path[p_off] = 47u8; p_off += 1u64;
|
||||
let poff: u64 = cstrinto(path, 0u64, dir);
|
||||
path[poff] = 47u8; poff += 1u64;
|
||||
let i: u64 = 0u64;
|
||||
for (i < nlen) { path[p_off + i] = name[i]; i += 1u64; };
|
||||
p_off += nlen;
|
||||
cstr_seal(path, p_off);
|
||||
for (i < nlen) { path[poff + i] = name[i]; i += 1u64; };
|
||||
poff += nlen;
|
||||
cstrseal(path, poff);
|
||||
// incs = <dir> so test files can `use ` siblings
|
||||
let tincs: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let ic: u64 = cstr_into(tincs, 0u64, dir);
|
||||
cstr_seal(tincs, ic);
|
||||
let ic: u64 = cstrinto(tincs, 0u64, dir);
|
||||
cstrseal(tincs, ic);
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
let bres: i32 = build_one(self_dir, path, tmp, tincs, nil);
|
||||
makeruntmp(tmp);
|
||||
let bres: i32 = buildone(selfdir, path, tmp, tincs, nil);
|
||||
if (bres != 0) {
|
||||
fail += 1;
|
||||
os.write(2, "FAIL ".ptr, 5u64);
|
||||
os.write(2, name, nlen);
|
||||
os.write(2, " (build)\n".ptr, 9u64);
|
||||
} else {
|
||||
let exec_argv: **u8 = os.alloc(16u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
exec_argv[1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
if (rc == 0) {
|
||||
pass += 1;
|
||||
os.write(1, "ok ".ptr, 5u64);
|
||||
@@ -1398,7 +1398,7 @@ fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
return 1;
|
||||
};
|
||||
|
||||
fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let target: *u8;
|
||||
if (start >= argc) {
|
||||
@@ -1410,54 +1410,54 @@ fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
};
|
||||
|
||||
// single-file mode: literal *.ww that exists
|
||||
if (cstr_endswith_lit(target, ".ww")) {
|
||||
if (cstrendswithlit(target, ".ww")) {
|
||||
if (os.access(target, 0i32) == 0) {
|
||||
return run_single_test(self_dir, target);
|
||||
return runsingletest(selfdir, target);
|
||||
};
|
||||
};
|
||||
|
||||
// otherwise treat target as a directory; enumerate *_test.ww
|
||||
return run_dir_tests(self_dir, target);
|
||||
return rundirtests(selfdir, target);
|
||||
};
|
||||
|
||||
// ---- Entry -------------------------------------------------------------
|
||||
|
||||
export fn main(argc: i32, argv: **u8) i32 = {
|
||||
if (argc < 1) {
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
|
||||
// self_dir = dirname(argv[0])
|
||||
let self_dir: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
self_dir_into(self_dir, PATH_MAX, argv[0]);
|
||||
// selfdir = dirname(argv[0])
|
||||
let selfdir: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
selfdirinto(selfdir, PATH_MAX, argv[0]);
|
||||
|
||||
if (argc < 2) {
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
let cmd: *u8 = argv[1];
|
||||
if (cstreq_lit(cmd, "-V")) { return do_version(); };
|
||||
if (cstreq_lit(cmd, "version")) { return do_version(); };
|
||||
if (cstreq_lit(cmd, "-h")) {
|
||||
write_usage(1);
|
||||
if (cstreqlit(cmd, "-V")) { return doversion(); };
|
||||
if (cstreqlit(cmd, "version")) { return doversion(); };
|
||||
if (cstreqlit(cmd, "-h")) {
|
||||
writeusage(1);
|
||||
return 0;
|
||||
};
|
||||
if (cstreq_lit(cmd, "--help")) {
|
||||
write_usage(1);
|
||||
if (cstreqlit(cmd, "--help")) {
|
||||
writeusage(1);
|
||||
return 0;
|
||||
};
|
||||
if (cstreq_lit(cmd, "build")) {
|
||||
return do_build(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "build")) {
|
||||
return dobuild(selfdir, argv, argc, 2);
|
||||
};
|
||||
if (cstreq_lit(cmd, "run")) {
|
||||
return do_run(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "run")) {
|
||||
return dorun(selfdir, argv, argc, 2);
|
||||
};
|
||||
if (cstreq_lit(cmd, "test")) {
|
||||
return do_test(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "test")) {
|
||||
return dotest(selfdir, argv, argc, 2);
|
||||
};
|
||||
os.write(2, "ww: unknown subcommand\n".ptr, 23u64);
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,8 +36,8 @@ fn cstreq(a: *u8, b: *u8) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
// cstreq_lit — compare a NUL-terminated *u8 to a ww string literal.
|
||||
fn cstreq_lit(a: *u8, lit: str) bool = {
|
||||
// cstreqlit — compare a NUL-terminated *u8 to a ww string literal.
|
||||
fn cstreqlit(a: *u8, lit: str) bool = {
|
||||
let n: i32 = lit.len;
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
@@ -48,7 +48,7 @@ fn cstreq_lit(a: *u8, lit: str) bool = {
|
||||
};
|
||||
|
||||
// startswith — does a have b as a prefix?
|
||||
fn cstr_startswith(a: *u8, b: *u8) bool = {
|
||||
fn cstrstartswith(a: *u8, b: *u8) bool = {
|
||||
let i: u64 = 0u64;
|
||||
for (b[i] != 0u8) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
@@ -68,7 +68,7 @@ fn bytecpy(dst: *u8, src: *u8, n: u64) void = {
|
||||
|
||||
// Copy a NUL-terminated *u8 into dst starting at off; return the new
|
||||
// offset (without writing a NUL).
|
||||
fn cstr_into(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
fn cstrinto(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
let i: u64 = 0u64;
|
||||
for (src[i] != 0u8) {
|
||||
dst[off + i] = src[i];
|
||||
@@ -78,7 +78,7 @@ fn cstr_into(dst: *u8, off: u64, src: *u8) u64 = {
|
||||
};
|
||||
|
||||
// Same, but for a ww `str` (no NUL on the source side; we copy len bytes).
|
||||
fn str_into(dst: *u8, off: u64, src: str) u64 = {
|
||||
fn strinto(dst: *u8, off: u64, src: str) u64 = {
|
||||
let n: i32 = src.len;
|
||||
let i: i32 = 0;
|
||||
for (i < n) {
|
||||
@@ -91,14 +91,14 @@ fn str_into(dst: *u8, off: u64, src: str) u64 = {
|
||||
};
|
||||
|
||||
// Write a single byte, return new offset.
|
||||
fn byte_into(dst: *u8, off: u64, c: u8) u64 = {
|
||||
fn byteinto(dst: *u8, off: u64, c: u8) u64 = {
|
||||
dst[off] = c;
|
||||
return off + 1u64;
|
||||
};
|
||||
|
||||
// NUL-terminate at off and return the same off (handy when passing the
|
||||
// buffer to a syscall that expects a C-string).
|
||||
fn cstr_seal(dst: *u8, off: u64) void = {
|
||||
fn cstrseal(dst: *u8, off: u64) void = {
|
||||
dst[off] = 0u8;
|
||||
};
|
||||
|
||||
@@ -106,7 +106,7 @@ fn cstr_seal(dst: *u8, off: u64) void = {
|
||||
|
||||
// dirname-equivalent: copy argv[0] up to (but not including) the last
|
||||
// '/' into dst, NUL-terminated. If no slash, write ".".
|
||||
fn self_dir_into(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
fn selfdirinto(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
let n: u64 = cstrlen(argv0);
|
||||
let cut: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
@@ -125,31 +125,31 @@ fn self_dir_into(dst: *u8, dstsz: u64, argv0: *u8) void = {
|
||||
};
|
||||
|
||||
// Build "$dir/$name" (NUL-terminated) into a fresh page-sized buffer.
|
||||
fn join_path(dir: *u8, name: *u8) *u8 = {
|
||||
fn joinpath(dir: *u8, name: *u8) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, dir);
|
||||
off = byte_into(buf, off, 47u8);
|
||||
off = cstr_into(buf, off, name);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, dir);
|
||||
off = byteinto(buf, off, 47u8);
|
||||
off = cstrinto(buf, off, name);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// Same, but the second component is a ww `str` literal.
|
||||
fn join_path_lit(dir: *u8, name: str) *u8 = {
|
||||
fn joinpathlit(dir: *u8, name: str) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, dir);
|
||||
off = byte_into(buf, off, 47u8);
|
||||
off = str_into(buf, off, name);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, dir);
|
||||
off = byteinto(buf, off, 47u8);
|
||||
off = strinto(buf, off, name);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// ---- Subprocess plumbing ----------------------------------------------
|
||||
|
||||
// proc_run — fork, execve `path` with `argv` (NULL-terminated), wait.
|
||||
// procrun — fork, execve `path` with `argv` (NULL-terminated), wait.
|
||||
// Returns 0 on clean exit-0, 1 on any non-zero exit or signal kill,
|
||||
// -1 on fork/wait failure.
|
||||
fn proc_run(path: *u8, argv: **u8) i32 = {
|
||||
fn procrun(path: *u8, argv: **u8) i32 = {
|
||||
let pid: i32 = os.fork();
|
||||
if (pid < 0) {
|
||||
os.write(2, "ww: fork failed\n".ptr, 16u64);
|
||||
@@ -193,7 +193,7 @@ type expctx = struct {
|
||||
visit: *strnode,
|
||||
};
|
||||
|
||||
fn visit_seen(c: *expctx, path: str) bool = {
|
||||
fn visitseen(c: *expctx, path: str) bool = {
|
||||
let n: *strnode = c.visit;
|
||||
for (n != nil) {
|
||||
if (n.s.len == path.len) {
|
||||
@@ -210,7 +210,7 @@ fn visit_seen(c: *expctx, path: str) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
fn visit_add(c: *expctx, path: str) void = {
|
||||
fn visitadd(c: *expctx, path: str) void = {
|
||||
let n: *strnode = amalloc(c.a, 32u64): *strnode;
|
||||
n.s = path;
|
||||
n.snext = c.visit;
|
||||
@@ -219,7 +219,7 @@ fn visit_add(c: *expctx, path: str) void = {
|
||||
|
||||
// Try <dir>/<name>.ww then <dir>/<name>/<name>.ww. Returns NUL-terminated
|
||||
// arena-resident path if found, else nil.
|
||||
fn locate_in(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
||||
fn locatein(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 = {
|
||||
// candidate 1: <dir>/<name>.ww
|
||||
let buf: *u8 = amalloc(a, PATH_MAX): *u8;
|
||||
let off: u64 = 0u64;
|
||||
@@ -259,7 +259,7 @@ fn locate_in(a: *arena, dir: *u8, dir_len: u64, name: *u8, name_len: u64) *u8 =
|
||||
};
|
||||
|
||||
// Walk a colon-separated dirlist, return first hit or nil.
|
||||
fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
fn locateimport(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
let total: u64 = cstrlen(dirs);
|
||||
let p: u64 = 0u64;
|
||||
for (p < total) {
|
||||
@@ -268,9 +268,9 @@ fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
if (dirs[q] == 58u8) { break; }; // ':'
|
||||
q += 1u64;
|
||||
};
|
||||
let seg_len: u64 = q - p;
|
||||
if (seg_len > 0u64) {
|
||||
let hit: *u8 = locate_in(a, dirs + p, seg_len, name, name_len);
|
||||
let seglen: u64 = q - p;
|
||||
if (seglen > 0u64) {
|
||||
let hit: *u8 = locatein(a, dirs + p, seglen, name, name_len);
|
||||
if (hit != nil) { return hit; };
|
||||
};
|
||||
p = q + 1u64;
|
||||
@@ -280,7 +280,7 @@ fn locate_import(a: *arena, dirs: *u8, name: *u8, name_len: u64) *u8 = {
|
||||
|
||||
// ---- file slurp -------------------------------------------------------
|
||||
|
||||
fn read_all(path_cs: *u8) (*u8, u64) = {
|
||||
fn readall(path_cs: *u8) (*u8, u64) = {
|
||||
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) { return nil, 0u64; };
|
||||
let n: i64 = os.filesize(fd);
|
||||
@@ -294,7 +294,7 @@ fn read_all(path_cs: *u8) (*u8, u64) = {
|
||||
return buf, nu;
|
||||
};
|
||||
|
||||
fn is_ident_byte(c: u8) bool = {
|
||||
fn isidentbyte(c: u8) bool = {
|
||||
if (c >= 97u8) { if (c <= 122u8) { return true; }; }; // a..z
|
||||
if (c >= 65u8) { if (c <= 90u8) { return true; }; }; // A..Z
|
||||
if (c >= 48u8) { if (c <= 57u8) { return true; }; }; // 0..9
|
||||
@@ -306,7 +306,7 @@ fn is_ident_byte(c: u8) bool = {
|
||||
// Scan one `use IDENT;` line out of [start, end). Returns the start of
|
||||
// the ident and its length, or (nil, 0) if no `use` here. The caller
|
||||
// passes a slice of the source: src points at the line start.
|
||||
fn scan_use(src: *u8, len: u64) (*u8, u64) = {
|
||||
fn scanuse(src: *u8, len: u64) (*u8, u64) = {
|
||||
let i: u64 = 0u64;
|
||||
// skip leading whitespace
|
||||
for (i < len) {
|
||||
@@ -324,27 +324,27 @@ fn scan_use(src: *u8, len: u64) (*u8, u64) = {
|
||||
if (src[i] != 32u8) { if (src[i] != 9u8) { break; }; };
|
||||
i += 1u64;
|
||||
};
|
||||
let id_start: u64 = i;
|
||||
let idstart: u64 = i;
|
||||
for (i < len) {
|
||||
if (!is_ident_byte(src[i])) { break; };
|
||||
if (!isidentbyte(src[i])) { break; };
|
||||
i += 1u64;
|
||||
};
|
||||
let id_len: u64 = i - id_start;
|
||||
if (id_len == 0u64) { return nil, 0u64; };
|
||||
return src + id_start, id_len;
|
||||
let idlen: u64 = i - idstart;
|
||||
if (idlen == 0u64) { return nil, 0u64; };
|
||||
return src + idstart, idlen;
|
||||
};
|
||||
|
||||
// Recursively expand `path` into c.out. Imported files are emitted
|
||||
// before their importer; cycles are broken via the visited set.
|
||||
fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
let plen: u64 = cstrlen(path_cs);
|
||||
let path_str: str = astrndup(c.a, path_cs, plen);
|
||||
if (visit_seen(c, path_str)) { return; };
|
||||
visit_add(c, path_str);
|
||||
let pathstr: str = astrndup(c.a, path_cs, plen);
|
||||
if (visitseen(c, pathstr)) { return; };
|
||||
visitadd(c, pathstr);
|
||||
|
||||
let bufp: *u8;
|
||||
let blen: u64;
|
||||
bufp, blen = read_all(path_cs);
|
||||
bufp, blen = readall(path_cs);
|
||||
if (bufp == nil) {
|
||||
os.write(2, "ww: cannot read source\n".ptr, 23u64);
|
||||
return;
|
||||
@@ -359,11 +359,11 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
if (bufp[j] == 10u8) { break; }; // '\n'
|
||||
j += 1u64;
|
||||
};
|
||||
let id_p: *u8;
|
||||
let id_n: u64;
|
||||
id_p, id_n = scan_use(bufp + i, j - i);
|
||||
if (id_p != nil) {
|
||||
let ipath: *u8 = locate_import(c.a, c.dirs, id_p, id_n);
|
||||
let idp: *u8;
|
||||
let idn: u64;
|
||||
idp, idn = scanuse(bufp + i, j - i);
|
||||
if (idp != nil) {
|
||||
let ipath: *u8 = locateimport(c.a, c.dirs, idp, idn);
|
||||
if (ipath != nil) {
|
||||
expand(c, ipath);
|
||||
};
|
||||
@@ -381,7 +381,7 @@ fn expand(c: *expctx, path_cs: *u8) void = {
|
||||
// Strip the trailing ".ww" off `src` (a NUL-terminated path) into
|
||||
// `stem`, NUL-terminated. If there's no .ww, the stem is the whole
|
||||
// path.
|
||||
fn make_stem(stem: *u8, src: *u8) void = {
|
||||
fn makestem(stem: *u8, src: *u8) void = {
|
||||
let n: u64 = cstrlen(src);
|
||||
let stop: u64 = n;
|
||||
if (n >= 3u64) {
|
||||
@@ -399,25 +399,25 @@ fn make_stem(stem: *u8, src: *u8) void = {
|
||||
};
|
||||
|
||||
// Append a literal suffix to `stem` (which already lives in a buffer).
|
||||
fn append_lit(stem: *u8, suffix: str) *u8 = {
|
||||
fn appendlit(stem: *u8, suffix: str) *u8 = {
|
||||
let buf: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let off: u64 = cstr_into(buf, 0u64, stem);
|
||||
off = str_into(buf, off, suffix);
|
||||
cstr_seal(buf, off);
|
||||
let off: u64 = cstrinto(buf, 0u64, stem);
|
||||
off = strinto(buf, off, suffix);
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
// linker flags bundled as a struct so build_one stays at the wwstage
|
||||
// linker flags bundled as a struct so buildone stays at the wwstage
|
||||
// w6c's 6-argument calling-convention limit.
|
||||
type lflags = struct {
|
||||
libdirs: **u8,
|
||||
n_libdirs: i32,
|
||||
nlibdirs: i32,
|
||||
libs: **u8,
|
||||
n_libs: i32,
|
||||
nlibs: i32,
|
||||
};
|
||||
|
||||
// build_one — compile `src` into the executable named `out`.
|
||||
// self_dir: NUL-terminated dir containing this driver and the
|
||||
// buildone — compile `src` into the executable named `out`.
|
||||
// selfdir: NUL-terminated dir containing this driver and the
|
||||
// wwstage tools (w6c_ww/w6a_ww/w6l_ww)
|
||||
// src: NUL-terminated path to the .ww file
|
||||
// out: NUL-terminated desired output path
|
||||
@@ -428,46 +428,46 @@ type lflags = struct {
|
||||
// touches no C-built code at runtime. The C `ww` driver in cmd/ww/
|
||||
// still drives the C-built w6c/w6a/w6l. Test 993 pins the two
|
||||
// pipelines to byte-identical output on a corpus.
|
||||
fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
fn buildone(selfdir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
let a: *arena = newarena();
|
||||
|
||||
let c6: *u8 = join_path_lit(self_dir, "w6c_ww");
|
||||
let a6: *u8 = join_path_lit(self_dir, "w6a_ww");
|
||||
let l6: *u8 = join_path_lit(self_dir, "w6l_ww");
|
||||
let c6: *u8 = joinpathlit(selfdir, "w6c_ww");
|
||||
let a6: *u8 = joinpathlit(selfdir, "w6a_ww");
|
||||
let l6: *u8 = joinpathlit(selfdir, "w6l_ww");
|
||||
|
||||
// Default lib search path: <self_dir>/../../lib
|
||||
let dotdot_lib: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
// Default lib search path: <selfdir>/../../lib
|
||||
let dotdotlib: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
{
|
||||
let off: u64 = cstr_into(dotdot_lib, 0u64, self_dir);
|
||||
off = str_into(dotdot_lib, off, "/../../lib");
|
||||
cstr_seal(dotdot_lib, off);
|
||||
let off: u64 = cstrinto(dotdotlib, 0u64, selfdir);
|
||||
off = strinto(dotdotlib, off, "/../../lib");
|
||||
cstrseal(dotdotlib, off);
|
||||
};
|
||||
|
||||
// Compose searchpath: incs + ':' + dotdot_lib (or just dotdot_lib).
|
||||
// Compose searchpath: incs + ':' + dotdotlib (or just dotdotlib).
|
||||
let searchpath: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
{
|
||||
let off: u64 = 0u64;
|
||||
if (incs[0u64] != 0u8) {
|
||||
off = cstr_into(searchpath, off, incs);
|
||||
off = byte_into(searchpath, off, 58u8); // ':'
|
||||
off = cstrinto(searchpath, off, incs);
|
||||
off = byteinto(searchpath, off, 58u8); // ':'
|
||||
};
|
||||
off = cstr_into(searchpath, off, dotdot_lib);
|
||||
cstr_seal(searchpath, off);
|
||||
off = cstrinto(searchpath, off, dotdotlib);
|
||||
cstrseal(searchpath, off);
|
||||
};
|
||||
|
||||
// stem, .s, .o, .combined.ww, libwwrt.a
|
||||
let stem: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_stem(stem, src);
|
||||
let asmf: *u8 = append_lit(stem, ".s");
|
||||
let objf: *u8 = append_lit(stem, ".o");
|
||||
let combined: *u8 = append_lit(stem, ".combined.ww");
|
||||
makestem(stem, src);
|
||||
let asmf: *u8 = appendlit(stem, ".s");
|
||||
let objf: *u8 = appendlit(stem, ".o");
|
||||
let combined: *u8 = appendlit(stem, ".combined.ww");
|
||||
|
||||
// libwwrt.a path: <self_dir>/../lib/libwwrt.a
|
||||
// libwwrt.a path: <selfdir>/../lib/libwwrt.a
|
||||
let libwwrt: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
{
|
||||
let off: u64 = cstr_into(libwwrt, 0u64, self_dir);
|
||||
off = str_into(libwwrt, off, "/../lib/libwwrt.a");
|
||||
cstr_seal(libwwrt, off);
|
||||
let off: u64 = cstrinto(libwwrt, 0u64, selfdir);
|
||||
off = strinto(libwwrt, off, "/../lib/libwwrt.a");
|
||||
cstrseal(libwwrt, off);
|
||||
};
|
||||
|
||||
// Step 1: expand `use`s into the combined file.
|
||||
@@ -494,7 +494,7 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[2] = asmf;
|
||||
argv[3] = combined;
|
||||
argv[4] = nil;
|
||||
if (proc_run(c6, argv) != 0) {
|
||||
if (procrun(c6, argv) != 0) {
|
||||
os.write(2, "ww: w6c failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -508,7 +508,7 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[2] = objf;
|
||||
argv[3] = asmf;
|
||||
argv[4] = nil;
|
||||
if (proc_run(a6, argv) != 0) {
|
||||
if (procrun(a6, argv) != 0) {
|
||||
os.write(2, "ww: w6a failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -516,21 +516,21 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
|
||||
// Step 4: w6l -o <out> <stem>.o libwwrt.a [-L<dir>...] [-l<name>...]
|
||||
{
|
||||
let nl_dirs: i32 = 0;
|
||||
let nl_libs: i32 = 0;
|
||||
let l_dirs: **u8 = nil;
|
||||
let l_libs: **u8 = nil;
|
||||
let nldirs: i32 = 0;
|
||||
let nllibs: i32 = 0;
|
||||
let ldirs: **u8 = nil;
|
||||
let llibs: **u8 = nil;
|
||||
if (lf != nil) {
|
||||
nl_dirs = lf.n_libdirs;
|
||||
nl_libs = lf.n_libs;
|
||||
l_dirs = lf.libdirs;
|
||||
l_libs = lf.libs;
|
||||
nldirs = lf.nlibdirs;
|
||||
nllibs = lf.nlibs;
|
||||
ldirs = lf.libdirs;
|
||||
llibs = lf.libs;
|
||||
};
|
||||
// argv slots: 5 fixed (w6l, -o, out, objf, libwwrt)
|
||||
// + 2 * n_libdirs (-L, dir)
|
||||
// + 2 * n_libs (-l, name)
|
||||
// + 2 * nlibdirs (-L, dir)
|
||||
// + 2 * nlibs (-l, name)
|
||||
// + 1 nil terminator.
|
||||
let total: i32 = 5 + 2 * nl_dirs + 2 * nl_libs + 1;
|
||||
let total: i32 = 5 + 2 * nldirs + 2 * nllibs + 1;
|
||||
let argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
argv[0] = "w6l\0".ptr;
|
||||
argv[1] = "-o\0".ptr;
|
||||
@@ -539,21 +539,21 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
argv[4] = libwwrt;
|
||||
let pos: i32 = 5;
|
||||
let k: i32 = 0;
|
||||
for (k < nl_dirs) {
|
||||
for (k < nldirs) {
|
||||
argv[pos] = "-L\0".ptr;
|
||||
argv[pos + 1] = l_dirs[k];
|
||||
argv[pos + 1] = ldirs[k];
|
||||
pos += 2;
|
||||
k += 1;
|
||||
};
|
||||
k = 0;
|
||||
for (k < nl_libs) {
|
||||
for (k < nllibs) {
|
||||
argv[pos] = "-l\0".ptr;
|
||||
argv[pos + 1] = l_libs[k];
|
||||
argv[pos + 1] = llibs[k];
|
||||
pos += 2;
|
||||
k += 1;
|
||||
};
|
||||
argv[pos] = nil;
|
||||
if (proc_run(l6, argv) != 0) {
|
||||
if (procrun(l6, argv) != 0) {
|
||||
os.write(2, "ww: w6l failed\n".ptr, 15u64);
|
||||
return 1;
|
||||
};
|
||||
@@ -563,15 +563,15 @@ fn build_one(self_dir: *u8, src: *u8, out: *u8, incs: *u8, lf: *lflags) i32 = {
|
||||
|
||||
// ---- Module-by-name resolution ----------------------------------------
|
||||
//
|
||||
// Mirrors cmd/ww/main.c:resolve_module. Maps a name like "foo", "lib/foo",
|
||||
// Mirrors cmd/ww/main.c:resolvemodule. Maps a name like "foo", "lib/foo",
|
||||
// "foo.ww", or "." to a concrete .ww file path:
|
||||
// 1. literal <name>.ww that exists → use as-is
|
||||
// 2. "." → <cwd>/<basename(cwd)>.ww → that, if it exists
|
||||
// 3. <name>/<basename(name)>.ww → that, if it exists
|
||||
// 4. walk search path (cwd:incs:<self_dir>/../../lib):
|
||||
// 4. walk search path (cwd:incs:<selfdir>/../../lib):
|
||||
// <dir>/<name>.ww or <dir>/<name>/<name>.ww
|
||||
|
||||
fn cstr_endswith_lit(p: *u8, lit: str) bool = {
|
||||
fn cstrendswithlit(p: *u8, lit: str) bool = {
|
||||
let plen: u64 = cstrlen(p);
|
||||
let slen: u64 = lit.len: u64;
|
||||
if (plen < slen) { return false; };
|
||||
@@ -585,9 +585,9 @@ fn cstr_endswith_lit(p: *u8, lit: str) bool = {
|
||||
return true;
|
||||
};
|
||||
|
||||
// basename_off — return the offset of the last path segment within `p`
|
||||
// basenameoff — return the offset of the last path segment within `p`
|
||||
// (i.e. one past the final '/'). Returns 0 if there's no slash.
|
||||
fn basename_off(p: *u8, plen: u64) u64 = {
|
||||
fn basenameoff(p: *u8, plen: u64) u64 = {
|
||||
let start: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
for (i < plen) {
|
||||
@@ -597,9 +597,9 @@ fn basename_off(p: *u8, plen: u64) u64 = {
|
||||
return start;
|
||||
};
|
||||
|
||||
// arena_dup_cstr — copy `plen` bytes from `src` into a fresh NUL-sealed
|
||||
// arenadupcstr — copy `plen` bytes from `src` into a fresh NUL-sealed
|
||||
// arena buffer.
|
||||
fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
fn arenadupcstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
let buf: *u8 = amalloc(a, plen + 1u64): *u8;
|
||||
let i: u64 = 0u64;
|
||||
for (i < plen) { buf[i] = src[i]; i += 1u64; };
|
||||
@@ -607,8 +607,8 @@ fn arena_dup_cstr(a: *arena, src: *u8, plen: u64) *u8 = {
|
||||
return buf;
|
||||
};
|
||||
|
||||
// build_dir_module_path — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
|
||||
fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
|
||||
// builddirmodulepath — alloc <dir>/<base>.ww, NUL-terminated, in `a`.
|
||||
fn builddirmodulepath(a: *arena, dir: *u8, dlen: u64,
|
||||
base: *u8, blen: u64) *u8 = {
|
||||
let need: u64 = dlen + 1u64 + blen + 3u64 + 1u64;
|
||||
let buf: *u8 = amalloc(a, need): *u8;
|
||||
@@ -625,32 +625,32 @@ fn build_dir_module_path(a: *arena, dir: *u8, dlen: u64,
|
||||
return buf;
|
||||
};
|
||||
|
||||
// build_search_path — compose the colon-separated lookup path used by
|
||||
// resolve_module's case (4). Order: "." : <incs> : <self_dir>/../../lib
|
||||
fn build_search_path(a: *arena, self_dir: *u8, incs: *u8) *u8 = {
|
||||
// buildsearchpath — compose the colon-separated lookup path used by
|
||||
// resolvemodule's case (4). Order: "." : <incs> : <selfdir>/../../lib
|
||||
fn buildsearchpath(a: *arena, selfdir: *u8, incs: *u8) *u8 = {
|
||||
let buf: *u8 = amalloc(a, PATH_MAX * 2u64): *u8;
|
||||
let off: u64 = 0u64;
|
||||
buf[off] = 46u8; off += 1u64; // '.'
|
||||
if (incs != nil) {
|
||||
if (incs[0u64] != 0u8) {
|
||||
buf[off] = 58u8; off += 1u64; // ':'
|
||||
off = cstr_into(buf, off, incs);
|
||||
off = cstrinto(buf, off, incs);
|
||||
};
|
||||
};
|
||||
buf[off] = 58u8; off += 1u64;
|
||||
off = cstr_into(buf, off, self_dir);
|
||||
off = str_into(buf, off, "/../../lib");
|
||||
cstr_seal(buf, off);
|
||||
off = cstrinto(buf, off, selfdir);
|
||||
off = strinto(buf, off, "/../../lib");
|
||||
cstrseal(buf, off);
|
||||
return buf;
|
||||
};
|
||||
|
||||
fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
fn resolvemodule(a: *arena, selfdir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
let nlen: u64 = cstrlen(name);
|
||||
|
||||
// (1) literal .ww file that exists
|
||||
if (cstr_endswith_lit(name, ".ww")) {
|
||||
if (cstrendswithlit(name, ".ww")) {
|
||||
if (os.access(name, 0i32) == 0) {
|
||||
return arena_dup_cstr(a, name, nlen);
|
||||
return arenadupcstr(a, name, nlen);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -661,11 +661,11 @@ fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
let r: i64 = os.getcwd(cwd, PATH_MAX);
|
||||
if (r <= 0i64) { return nil; };
|
||||
let cwdlen: u64 = (r: u64) - 1u64; // strip trailing NUL
|
||||
let bo: u64 = basename_off(cwd, cwdlen);
|
||||
let bo: u64 = basenameoff(cwd, cwdlen);
|
||||
let blen: u64 = cwdlen - bo;
|
||||
let dot: *u8 = amalloc(a, 2u64): *u8;
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
let probe: *u8 = build_dir_module_path(a, dot, 1u64,
|
||||
let probe: *u8 = builddirmodulepath(a, dot, 1u64,
|
||||
cwd + bo, blen);
|
||||
if (os.access(probe, 0i32) == 0) { return probe; };
|
||||
return nil;
|
||||
@@ -673,31 +673,31 @@ fn resolve_module(a: *arena, self_dir: *u8, name: *u8, incs: *u8) *u8 = {
|
||||
};
|
||||
|
||||
// (3) <name>/<basename(name)>.ww — directory-as-module
|
||||
let bo: u64 = basename_off(name, nlen);
|
||||
let probe: *u8 = build_dir_module_path(a, name, nlen,
|
||||
let bo: u64 = basenameoff(name, nlen);
|
||||
let probe: *u8 = builddirmodulepath(a, name, nlen,
|
||||
name + bo, nlen - bo);
|
||||
if (os.access(probe, 0i32) == 0) { return probe; };
|
||||
|
||||
// (4) search path lookup
|
||||
let search: *u8 = build_search_path(a, self_dir, incs);
|
||||
return locate_import(a, search, name, nlen);
|
||||
let search: *u8 = buildsearchpath(a, selfdir, incs);
|
||||
return locateimport(a, search, name, nlen);
|
||||
};
|
||||
|
||||
// ---- Subcommand handlers ----------------------------------------------
|
||||
|
||||
fn write_usage(fd: i32) void = {
|
||||
fn writeusage(fd: i32) void = {
|
||||
let s: str = "usage: ww [-V] <subcommand> [args...]\n -V print version and exit\n build [path] compile module to a static binary (path defaults to cwd)\n run [path] ... build then exec, passing extra args to the program\n test [path] build and run *_test.ww in the module (path defaults to cwd)\n version print version and exit\n\n path forms:\n foo.ww literal file\n foo search cwd, -I dirs, then $WW_LIB-equiv for foo.ww or foo/foo.ww\n lib/foo directory: build lib/foo/foo.ww\n . build the cwd's <basename>.ww\n";
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
fn do_version() i32 = {
|
||||
fn doversion() i32 = {
|
||||
os.write(1, "ww 0.0\n".ptr, 7u64);
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Compute the basename of src (without trailing ".ww") into a fresh
|
||||
// buffer. Used as the default output path for `ww build`.
|
||||
fn default_out_path(src: *u8) *u8 = {
|
||||
fn defaultoutpath(src: *u8) *u8 = {
|
||||
let n: u64 = cstrlen(src);
|
||||
let start: u64 = 0u64;
|
||||
let i: u64 = 0u64;
|
||||
@@ -723,22 +723,22 @@ fn default_out_path(src: *u8) *u8 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
cstr_seal(out, off);
|
||||
cstrseal(out, off);
|
||||
return out;
|
||||
};
|
||||
|
||||
fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dobuild(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let src: *u8 = nil;
|
||||
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
let inc_off: u64 = 0u64;
|
||||
cstr_seal(incs, 0u64);
|
||||
let incoff: u64 = 0u64;
|
||||
cstrseal(incs, 0u64);
|
||||
|
||||
let max_lflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libs: i32 = 0;
|
||||
let maxlflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibs: i32 = 0;
|
||||
|
||||
let i: i32 = start;
|
||||
for (i < argc) {
|
||||
@@ -756,12 +756,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (inc_off > 0u64) {
|
||||
incs[inc_off] = 58u8; // ':'
|
||||
inc_off += 1u64;
|
||||
if (incoff > 0u64) {
|
||||
incs[incoff] = 58u8; // ':'
|
||||
incoff += 1u64;
|
||||
};
|
||||
inc_off = cstr_into(incs, inc_off, dir);
|
||||
cstr_seal(incs, inc_off);
|
||||
incoff = cstrinto(incs, incoff, dir);
|
||||
cstrseal(incs, incoff);
|
||||
} else { if (p[1u64] == 76u8) { // '-L'
|
||||
let dir: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -774,12 +774,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (n_libdirs >= max_lflags) {
|
||||
if (nlibdirs >= maxlflags) {
|
||||
os.write(2, "ww build: too many -L\n".ptr, 22u64);
|
||||
return 2;
|
||||
};
|
||||
libdirs[n_libdirs] = dir;
|
||||
n_libdirs += 1;
|
||||
libdirs[nlibdirs] = dir;
|
||||
nlibdirs += 1;
|
||||
} else { if (p[1u64] == 108u8) { // '-l'
|
||||
let nm: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -792,12 +792,12 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
nm = argv[i];
|
||||
};
|
||||
if (n_libs >= max_lflags) {
|
||||
if (nlibs >= maxlflags) {
|
||||
os.write(2, "ww build: too many -l\n".ptr, 22u64);
|
||||
return 2;
|
||||
};
|
||||
libs[n_libs] = nm;
|
||||
n_libs += 1;
|
||||
libs[nlibs] = nm;
|
||||
nlibs += 1;
|
||||
} else {
|
||||
os.write(2, "ww build: unknown flag\n".ptr, 23u64);
|
||||
return 2;
|
||||
@@ -814,26 +814,26 @@ fn do_build(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
src = dot;
|
||||
};
|
||||
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
|
||||
let resolved: *u8 = resolvemodule(a, selfdir, src, incs);
|
||||
if (resolved == nil) {
|
||||
os.write(2, "ww build: cannot find module\n".ptr, 29u64);
|
||||
return 1;
|
||||
};
|
||||
let out: *u8 = default_out_path(resolved);
|
||||
let out: *u8 = defaultoutpath(resolved);
|
||||
let lf: lflags;
|
||||
lf.libdirs = libdirs;
|
||||
lf.n_libdirs = n_libdirs;
|
||||
lf.nlibdirs = nlibdirs;
|
||||
lf.libs = libs;
|
||||
lf.n_libs = n_libs;
|
||||
return build_one(self_dir, resolved, out, incs, &lf);
|
||||
lf.nlibs = nlibs;
|
||||
return buildone(selfdir, resolved, out, incs, &lf);
|
||||
};
|
||||
|
||||
// Format the scratch path /tmp/ww_run_<pid> into buf. Returns NUL-
|
||||
// terminated buf. Pid is folded in decimal manually since we don't
|
||||
// import strconv.
|
||||
fn make_run_tmp(buf: *u8) void = {
|
||||
fn makeruntmp(buf: *u8) void = {
|
||||
let off: u64 = 0u64;
|
||||
off = str_into(buf, off, "/tmp/ww_run_");
|
||||
off = strinto(buf, off, "/tmp/ww_run_");
|
||||
let pid: i32 = os.getpid();
|
||||
// itoa for non-negative pid
|
||||
let dig: [16]u8;
|
||||
@@ -855,26 +855,26 @@ fn make_run_tmp(buf: *u8) void = {
|
||||
off += 1u64;
|
||||
k -= 1;
|
||||
};
|
||||
cstr_seal(buf, off);
|
||||
cstrseal(buf, off);
|
||||
};
|
||||
|
||||
fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dorun(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let src: *u8 = nil;
|
||||
let pass_start: i32 = -1; // first argv idx to pass through to program
|
||||
let passstart: i32 = -1; // first argv idx to pass through to program
|
||||
let incs: *u8 = os.alloc(PATH_MAX * 2u64): *u8;
|
||||
let inc_off: u64 = 0u64;
|
||||
cstr_seal(incs, 0u64);
|
||||
let incoff: u64 = 0u64;
|
||||
cstrseal(incs, 0u64);
|
||||
|
||||
let max_lflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((max_lflags: u64) * 8u64): **u8;
|
||||
let n_libs: i32 = 0;
|
||||
let maxlflags: i32 = 32;
|
||||
let libdirs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibdirs: i32 = 0;
|
||||
let libs: **u8 = os.alloc((maxlflags: u64) * 8u64): **u8;
|
||||
let nlibs: i32 = 0;
|
||||
|
||||
let i: i32 = start;
|
||||
for (i < argc) {
|
||||
if (pass_start >= 0) { i = argc; } // stop, leave rest for exec
|
||||
if (passstart >= 0) { i = argc; } // stop, leave rest for exec
|
||||
else {
|
||||
let p: *u8 = argv[i];
|
||||
if (p[0u64] == 45u8) {
|
||||
@@ -890,12 +890,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (inc_off > 0u64) {
|
||||
incs[inc_off] = 58u8;
|
||||
inc_off += 1u64;
|
||||
if (incoff > 0u64) {
|
||||
incs[incoff] = 58u8;
|
||||
incoff += 1u64;
|
||||
};
|
||||
inc_off = cstr_into(incs, inc_off, dir);
|
||||
cstr_seal(incs, inc_off);
|
||||
incoff = cstrinto(incs, incoff, dir);
|
||||
cstrseal(incs, incoff);
|
||||
} else { if (p[1u64] == 76u8) {
|
||||
let dir: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -908,12 +908,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
dir = argv[i];
|
||||
};
|
||||
if (n_libdirs >= max_lflags) {
|
||||
if (nlibdirs >= maxlflags) {
|
||||
os.write(2, "ww run: too many -L\n".ptr, 20u64);
|
||||
return 2;
|
||||
};
|
||||
libdirs[n_libdirs] = dir;
|
||||
n_libdirs += 1;
|
||||
libdirs[nlibdirs] = dir;
|
||||
nlibdirs += 1;
|
||||
} else { if (p[1u64] == 108u8) {
|
||||
let nm: *u8 = nil;
|
||||
if (p[2u64] != 0u8) {
|
||||
@@ -926,12 +926,12 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
i += 1;
|
||||
nm = argv[i];
|
||||
};
|
||||
if (n_libs >= max_lflags) {
|
||||
if (nlibs >= maxlflags) {
|
||||
os.write(2, "ww run: too many -l\n".ptr, 20u64);
|
||||
return 2;
|
||||
};
|
||||
libs[n_libs] = nm;
|
||||
n_libs += 1;
|
||||
libs[nlibs] = nm;
|
||||
nlibs += 1;
|
||||
} else {
|
||||
os.write(2, "ww run: unknown flag\n".ptr, 21u64);
|
||||
return 2;
|
||||
@@ -942,7 +942,7 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
src = p;
|
||||
i += 1;
|
||||
} else {
|
||||
pass_start = i; // remaining args go to the program
|
||||
passstart = i; // remaining args go to the program
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -953,64 +953,64 @@ fn do_run(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
dot[0] = 46u8; dot[1] = 0u8;
|
||||
src = dot;
|
||||
};
|
||||
let resolved: *u8 = resolve_module(a, self_dir, src, incs);
|
||||
let resolved: *u8 = resolvemodule(a, selfdir, src, incs);
|
||||
if (resolved == nil) {
|
||||
os.write(2, "ww run: cannot find module\n".ptr, 27u64);
|
||||
return 1;
|
||||
};
|
||||
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
makeruntmp(tmp);
|
||||
let lf: lflags;
|
||||
lf.libdirs = libdirs;
|
||||
lf.n_libdirs = n_libdirs;
|
||||
lf.nlibdirs = nlibdirs;
|
||||
lf.libs = libs;
|
||||
lf.n_libs = n_libs;
|
||||
if (build_one(self_dir, resolved, tmp, incs, &lf) != 0) {
|
||||
lf.nlibs = nlibs;
|
||||
if (buildone(selfdir, resolved, tmp, incs, &lf) != 0) {
|
||||
os.unlink(tmp);
|
||||
return 1;
|
||||
};
|
||||
|
||||
// exec with [tmp, argv[pass_start..argc), nil]
|
||||
let n_extra: i32 = 0;
|
||||
if (pass_start >= 0) { n_extra = argc - pass_start; };
|
||||
let total: i32 = n_extra + 2;
|
||||
let exec_argv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
// exec with [tmp, argv[passstart..argc), nil]
|
||||
let nextra: i32 = 0;
|
||||
if (passstart >= 0) { nextra = argc - passstart; };
|
||||
let total: i32 = nextra + 2;
|
||||
let execargv: **u8 = os.alloc((total: u64) * 8u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
let k: i32 = 0;
|
||||
for (k < n_extra) {
|
||||
exec_argv[k + 1] = argv[pass_start + k];
|
||||
for (k < nextra) {
|
||||
execargv[k + 1] = argv[passstart + k];
|
||||
k += 1;
|
||||
};
|
||||
exec_argv[n_extra + 1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
execargv[nextra + 1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
// ---- ww test ----------------------------------------------------------
|
||||
//
|
||||
// Mirrors cmd/ww/main.c:do_test. Two modes:
|
||||
// Mirrors cmd/ww/main.c:dotest. Two modes:
|
||||
// single-file: build+run a literal *.ww file, return its exit code
|
||||
// directory: open the dir, getdents64, build+run each *_test.ww,
|
||||
// report ok/FAIL per file, return 0 iff all pass.
|
||||
|
||||
fn run_single_test(self_dir: *u8, src: *u8) i32 = {
|
||||
fn runsingletest(selfdir: *u8, src: *u8) i32 = {
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
if (build_one(self_dir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
makeruntmp(tmp);
|
||||
if (buildone(selfdir, src, tmp, "\0".ptr, nil) != 0) {
|
||||
os.unlink(tmp);
|
||||
return 1;
|
||||
};
|
||||
let exec_argv: **u8 = os.alloc(16u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
exec_argv[1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
os.unlink(tmp);
|
||||
return rc;
|
||||
};
|
||||
|
||||
fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
fn rundirtests(selfdir: *u8, dir: *u8) i32 = {
|
||||
let fd: i32 = os.open(dir, os.O_RDONLY, 0i32);
|
||||
if (fd < 0) {
|
||||
os.write(2, "ww test: cannot open directory\n".ptr, 31u64);
|
||||
@@ -1032,33 +1032,33 @@ fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
let bhi: u64 = (buf[off + 17u64]): u64;
|
||||
let reclen: u64 = blo + (bhi * 256u64);
|
||||
let name: *u8 = buf + off + 19u64;
|
||||
if (cstr_endswith_lit(name, "_test.ww")) {
|
||||
if (cstrendswithlit(name, "_test.ww")) {
|
||||
let nlen: u64 = cstrlen(name);
|
||||
// path = <dir>/<name>
|
||||
let path: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let p_off: u64 = cstr_into(path, 0u64, dir);
|
||||
path[p_off] = 47u8; p_off += 1u64;
|
||||
let poff: u64 = cstrinto(path, 0u64, dir);
|
||||
path[poff] = 47u8; poff += 1u64;
|
||||
let i: u64 = 0u64;
|
||||
for (i < nlen) { path[p_off + i] = name[i]; i += 1u64; };
|
||||
p_off += nlen;
|
||||
cstr_seal(path, p_off);
|
||||
for (i < nlen) { path[poff + i] = name[i]; i += 1u64; };
|
||||
poff += nlen;
|
||||
cstrseal(path, poff);
|
||||
// incs = <dir> so test files can `use ` siblings
|
||||
let tincs: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
let ic: u64 = cstr_into(tincs, 0u64, dir);
|
||||
cstr_seal(tincs, ic);
|
||||
let ic: u64 = cstrinto(tincs, 0u64, dir);
|
||||
cstrseal(tincs, ic);
|
||||
let tmp: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
make_run_tmp(tmp);
|
||||
let bres: i32 = build_one(self_dir, path, tmp, tincs, nil);
|
||||
makeruntmp(tmp);
|
||||
let bres: i32 = buildone(selfdir, path, tmp, tincs, nil);
|
||||
if (bres != 0) {
|
||||
fail += 1;
|
||||
os.write(2, "FAIL ".ptr, 5u64);
|
||||
os.write(2, name, nlen);
|
||||
os.write(2, " (build)\n".ptr, 9u64);
|
||||
} else {
|
||||
let exec_argv: **u8 = os.alloc(16u64): **u8;
|
||||
exec_argv[0] = tmp;
|
||||
exec_argv[1] = nil;
|
||||
let rc: i32 = proc_run(tmp, exec_argv);
|
||||
let execargv: **u8 = os.alloc(16u64): **u8;
|
||||
execargv[0] = tmp;
|
||||
execargv[1] = nil;
|
||||
let rc: i32 = procrun(tmp, execargv);
|
||||
if (rc == 0) {
|
||||
pass += 1;
|
||||
os.write(1, "ok ".ptr, 5u64);
|
||||
@@ -1082,7 +1082,7 @@ fn run_dir_tests(self_dir: *u8, dir: *u8) i32 = {
|
||||
return 1;
|
||||
};
|
||||
|
||||
fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
fn dotest(selfdir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
let a: *arena = newarena();
|
||||
let target: *u8;
|
||||
if (start >= argc) {
|
||||
@@ -1094,53 +1094,53 @@ fn do_test(self_dir: *u8, argv: **u8, argc: i32, start: i32) i32 = {
|
||||
};
|
||||
|
||||
// single-file mode: literal *.ww that exists
|
||||
if (cstr_endswith_lit(target, ".ww")) {
|
||||
if (cstrendswithlit(target, ".ww")) {
|
||||
if (os.access(target, 0i32) == 0) {
|
||||
return run_single_test(self_dir, target);
|
||||
return runsingletest(selfdir, target);
|
||||
};
|
||||
};
|
||||
|
||||
// otherwise treat target as a directory; enumerate *_test.ww
|
||||
return run_dir_tests(self_dir, target);
|
||||
return rundirtests(selfdir, target);
|
||||
};
|
||||
|
||||
// ---- Entry -------------------------------------------------------------
|
||||
|
||||
export fn main(argc: i32, argv: **u8) i32 = {
|
||||
if (argc < 1) {
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
|
||||
// self_dir = dirname(argv[0])
|
||||
let self_dir: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
self_dir_into(self_dir, PATH_MAX, argv[0]);
|
||||
// selfdir = dirname(argv[0])
|
||||
let selfdir: *u8 = os.alloc(PATH_MAX): *u8;
|
||||
selfdirinto(selfdir, PATH_MAX, argv[0]);
|
||||
|
||||
if (argc < 2) {
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
let cmd: *u8 = argv[1];
|
||||
if (cstreq_lit(cmd, "-V")) { return do_version(); };
|
||||
if (cstreq_lit(cmd, "version")) { return do_version(); };
|
||||
if (cstreq_lit(cmd, "-h")) {
|
||||
write_usage(1);
|
||||
if (cstreqlit(cmd, "-V")) { return doversion(); };
|
||||
if (cstreqlit(cmd, "version")) { return doversion(); };
|
||||
if (cstreqlit(cmd, "-h")) {
|
||||
writeusage(1);
|
||||
return 0;
|
||||
};
|
||||
if (cstreq_lit(cmd, "--help")) {
|
||||
write_usage(1);
|
||||
if (cstreqlit(cmd, "--help")) {
|
||||
writeusage(1);
|
||||
return 0;
|
||||
};
|
||||
if (cstreq_lit(cmd, "build")) {
|
||||
return do_build(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "build")) {
|
||||
return dobuild(selfdir, argv, argc, 2);
|
||||
};
|
||||
if (cstreq_lit(cmd, "run")) {
|
||||
return do_run(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "run")) {
|
||||
return dorun(selfdir, argv, argc, 2);
|
||||
};
|
||||
if (cstreq_lit(cmd, "test")) {
|
||||
return do_test(self_dir, argv, argc, 2);
|
||||
if (cstreqlit(cmd, "test")) {
|
||||
return dotest(selfdir, argv, argc, 2);
|
||||
};
|
||||
os.write(2, "ww: unknown subcommand\n".ptr, 23u64);
|
||||
write_usage(2);
|
||||
writeusage(2);
|
||||
return 2;
|
||||
};
|
||||
|
||||
@@ -6713,8 +6713,8 @@ fn argstr(p: *u8) str = {
|
||||
return s;
|
||||
};
|
||||
|
||||
// streq_lit — compare a NUL-terminated argv entry to a string literal.
|
||||
fn streq_lit(p: *u8, lit: str) bool = {
|
||||
// streqlit — compare a NUL-terminated argv entry to a string literal.
|
||||
fn streqlit(p: *u8, lit: str) bool = {
|
||||
let i: i32 = 0;
|
||||
for (i < lit.len) {
|
||||
if (p[i] != lit[i]) { return false; };
|
||||
@@ -6731,13 +6731,13 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
let i: i32 = 1;
|
||||
for (i < argc) {
|
||||
let a: *u8 = argv[i];
|
||||
if (streq_lit(a, "-t")) {
|
||||
if (streqlit(a, "-t")) {
|
||||
mode = 116;
|
||||
} else { if (streq_lit(a, "-a")) {
|
||||
} else { if (streqlit(a, "-a")) {
|
||||
mode = 97; // 'a'
|
||||
} else { if (streq_lit(a, "-r")) {
|
||||
} else { if (streqlit(a, "-r")) {
|
||||
mode = 114; // 'r' — resolve / name-check
|
||||
} else { if (streq_lit(a, "-c")) {
|
||||
} else { if (streqlit(a, "-c")) {
|
||||
mode = 99; // 'c' — codegen / emit asm
|
||||
} else { if (path == nil) {
|
||||
path = a;
|
||||
@@ -6749,9 +6749,9 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
return 2;
|
||||
};
|
||||
|
||||
let fd_or_err: (i32 | str) = os.tryopen(path, os.O_RDONLY, 0i32);
|
||||
let fdorerr: (i32 | str) = os.tryopen(path, os.O_RDONLY, 0i32);
|
||||
let fd: i32 = -1;
|
||||
match (fd_or_err) {
|
||||
match (fdorerr) {
|
||||
case let v: i32 => fd = v;
|
||||
case let e: str => {
|
||||
os.write(2, "wwdump: cannot open ".ptr, 20u64);
|
||||
|
||||
@@ -38,8 +38,8 @@ fn argstr(p: *u8) str = {
|
||||
return s;
|
||||
};
|
||||
|
||||
// streq_lit — compare a NUL-terminated argv entry to a string literal.
|
||||
fn streq_lit(p: *u8, lit: str) bool = {
|
||||
// streqlit — compare a NUL-terminated argv entry to a string literal.
|
||||
fn streqlit(p: *u8, lit: str) bool = {
|
||||
let i: i32 = 0;
|
||||
for (i < lit.len) {
|
||||
if (p[i] != lit[i]) { return false; };
|
||||
@@ -56,13 +56,13 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
let i: i32 = 1;
|
||||
for (i < argc) {
|
||||
let a: *u8 = argv[i];
|
||||
if (streq_lit(a, "-t")) {
|
||||
if (streqlit(a, "-t")) {
|
||||
mode = 116;
|
||||
} else { if (streq_lit(a, "-a")) {
|
||||
} else { if (streqlit(a, "-a")) {
|
||||
mode = 97; // 'a'
|
||||
} else { if (streq_lit(a, "-r")) {
|
||||
} else { if (streqlit(a, "-r")) {
|
||||
mode = 114; // 'r' — resolve / name-check
|
||||
} else { if (streq_lit(a, "-c")) {
|
||||
} else { if (streqlit(a, "-c")) {
|
||||
mode = 99; // 'c' — codegen / emit asm
|
||||
} else { if (path == nil) {
|
||||
path = a;
|
||||
@@ -74,9 +74,9 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
return 2;
|
||||
};
|
||||
|
||||
let fd_or_err: (i32 | str) = os.tryopen(path, os.O_RDONLY, 0i32);
|
||||
let fdorerr: (i32 | str) = os.tryopen(path, os.O_RDONLY, 0i32);
|
||||
let fd: i32 = -1;
|
||||
match (fd_or_err) {
|
||||
match (fdorerr) {
|
||||
case let v: i32 => fd = v;
|
||||
case let e: str => {
|
||||
os.write(2, "wwdump: cannot open ".ptr, 20u64);
|
||||
|
||||
Reference in New Issue
Block a user