build_one_sep (per-package compile + .wwi interfaces + link) becomes the sole build path. do_build/do_run/do_test and the ww twins all route through it; --sep is now an accepted no-op and the run-rejects-sep guard is removed. Deleted the single-file amalgamator, both stages: build_one, expand, expand_dir, peek_package (+ the wwstage twins + strictpkgmismatch). unit_has_package is retained -- the sep scan loop's inline-package check needs it. The sep-shared helpers (enumerate_dir_ww, locate_import*, import_path_form, ImportSet, and ww counterparts) stay; they back the surviving sep path. Restores missing-package enforcement under sep by construction: the sep scan loop loudly rejects an unresolvable import (cannot find package <name>) unless the package is defined inline in the same unit -- matching the deleted amalgamator and closing the silent-accept the flip would otherwise introduce. All 5 wwstage tools relink (each is built via the now-sep `ww build`); emitted asm is byte-identical to the combined build per bootstrap input, so the binary md5 delta is pure link layout, not codegen. selfhost/cmd/ww/main.combined.ww is now stale and unregenerable (its writer build_one is deleted); #90 deletes it next. Test retargets folded in (rule-11 carve-out, #61/#133 precedent): each asserts post-flip-only behavior, is un-pre-migratable unlike #93/#94/#103, and splitting reddens one side. Closes #97. - 989_slttypepref -> dir-package layout (xb imports xa so both same-leaf `invalid` types are in scope at xb.f); inline-multipackage was the amalgamator shape, deleted with the flip. - 989_sepbuild_run -> run --sep now genuinely runs (exit 7), not the old loud-reject (exit 2); + a private per-pid WW_PKGCACHE so the cs/ww per-package byte-id compare on the shared real lib pkgs (rt/time/os) no longer races concurrent siblings on the global out/.pkgcache (the flip made sep the sole path, so every test now contends that cache). - 737_direnum -> the deleted strictpkgmismatch "differs from" wording -> sep's "does not match import path" (shared substring, wwstage terser #68). - 989_lib_byteid -> corpus-completeness scan excludes generated .sepwork scratch (the old `! -name '*.combined.ww'` exclude didn't cover the new sep artifact).
1500 lines
50 KiB
C
1500 lines
50 KiB
C
/*
|
|
* ww — the user-facing driver. Plan 9 cc(1) / Hare hare(1) analogue.
|
|
*
|
|
* Pipeline:
|
|
* ww build foo.ww → w6c foo.ww > foo.s ; w6a foo.s > foo.o ;
|
|
* w6l -o foo foo.o <runtime.o>
|
|
* ww run foo.ww → build then exec ./foo
|
|
*
|
|
* Tool paths default to siblings of $0 (so a fresh build runs out of
|
|
* out/bin/), and can be overridden with WW_W6C / WW_W6A / WW_W6L.
|
|
*/
|
|
#include "ww.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/stat.h>
|
|
#include <dirent.h>
|
|
#include <libgen.h>
|
|
|
|
static const char *usage =
|
|
"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"
|
|
" fmt <path> reformat ww source\n"
|
|
" version print version and exit\n"
|
|
"\n"
|
|
" path forms:\n"
|
|
" foo.ww literal file\n"
|
|
" foo search cwd, -I dirs, then $WW_LIB for foo.ww or foo/foo.ww\n"
|
|
" lib/foo directory: build lib/foo/foo.ww\n"
|
|
" . build the cwd's <basename>.ww\n";
|
|
|
|
static char *self_dir; /* directory containing this binary */
|
|
|
|
static const char *
|
|
toolpath(const char *envvar, const char *name)
|
|
{
|
|
const char *p = getenv(envvar);
|
|
if (p && p[0]) return p;
|
|
static char buf[1024];
|
|
snprintf(buf, sizeof buf, "%s/%s", self_dir, name);
|
|
return strdup(buf);
|
|
}
|
|
|
|
static int
|
|
run(const char *cmd)
|
|
{
|
|
int rc = system(cmd);
|
|
if (rc == -1) return -1;
|
|
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
|
return 1;
|
|
}
|
|
|
|
/* run_test_bin — exec the built test binary with an optional name-filter
|
|
* pattern as argv[1] (lib/test run() reads it via os.args). fork+execv
|
|
* (not system()) so glob metacharacters in the pattern reach the binary
|
|
* verbatim instead of being expanded by the shell. Mirrors the wwstage
|
|
* twin (selfhost/cmd/ww/main.ww runsingletest, which always builds an
|
|
* execargv for procrun). #17 fnmatch filter. */
|
|
static int
|
|
run_test_bin(const char *bin, const char *pattern)
|
|
{
|
|
pid_t pid = fork();
|
|
if (pid < 0) { perror("ww: fork"); return -1; }
|
|
if (pid == 0) {
|
|
char *xargv[3];
|
|
xargv[0] = (char *)bin;
|
|
if (pattern) { xargv[1] = (char *)pattern; xargv[2] = NULL; }
|
|
else { xargv[1] = NULL; }
|
|
execv(bin, xargv);
|
|
perror("ww: exec");
|
|
_exit(127);
|
|
}
|
|
int status = 0;
|
|
waitpid(pid, &status, 0);
|
|
if (WIFEXITED(status)) return WEXITSTATUS(status);
|
|
return 1;
|
|
}
|
|
|
|
/* Set of imported module paths, kept on the heap. Used to break
|
|
* cycles in `use` resolution. Linear because typical imports are
|
|
* a handful per build. */
|
|
struct ImportSet {
|
|
char **paths;
|
|
int n, cap;
|
|
};
|
|
|
|
static int
|
|
import_seen(struct ImportSet *s, const char *path)
|
|
{
|
|
for (int i = 0; i < s->n; i++)
|
|
if (strcmp(s->paths[i], path) == 0) return 1;
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
import_add(struct ImportSet *s, const char *path)
|
|
{
|
|
if (s->n + 1 > s->cap) {
|
|
s->cap = s->cap ? s->cap * 2 : 8;
|
|
s->paths = realloc(s->paths, s->cap * sizeof *s->paths);
|
|
}
|
|
s->paths[s->n++] = strdup(path);
|
|
}
|
|
|
|
/* Translate dots in an `import` name to slashes for path lookup.
|
|
* `encoding.utf8` → `encoding/utf8`. Mirrors Hare hare(1)'s
|
|
* use-path → fs-path mapping (ref/hare/hare/module/srcs.ha:78
|
|
* builds the same shape via path::push per ident part). */
|
|
static void
|
|
import_path_form(const char *name, char *out, size_t outsz)
|
|
{
|
|
size_t i;
|
|
for (i = 0; i + 1 < outsz && name[i] != '\0'; i++)
|
|
out[i] = (name[i] == '.') ? '/' : name[i];
|
|
out[i] = '\0';
|
|
}
|
|
|
|
/* try <dir>/<path>/ as a directory (want_dir), else <dir>/<path>.ww as
|
|
* a file. Sets *is_dir on hit. Symmetric with wwstage locatein 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
|
|
* fallback matching `foo/foo.ha`; a module IS the directory). */
|
|
static int
|
|
locate_import_in(const char *dir, const char *path_form, char *out,
|
|
size_t outsz, int *is_dir, int want_dir)
|
|
{
|
|
struct stat st;
|
|
if (want_dir) {
|
|
snprintf(out, outsz, "%s/%s", dir, path_form);
|
|
if (stat(out, &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
*is_dir = 1;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
snprintf(out, outsz, "%s/%s.ww", dir, path_form);
|
|
if (access(out, 0) == 0) {
|
|
*is_dir = 0;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* Walk a colon-separated dirlist trying to resolve `path_form`. Returns
|
|
* 1 on the first hit and writes the concrete path + dir/file marker.
|
|
*
|
|
* #98: "a module IS the directory" — a directory-package on ANY entry
|
|
* wins over a same-named sibling FILE on an EARLIER entry. The driver
|
|
* builds the searchpath srcd-first; a co-located `lib/<mod>/<mod>test.ww`
|
|
* entry makes srcd = lib/<mod>, so a self-named `import <mod>` would
|
|
* else file-hit the sibling lib/<mod>/<mod>.ww and (under --sep) fold
|
|
* inline under the wrong module-reset → "package <mod> does not match
|
|
* import path <importer>". 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. */
|
|
static int
|
|
locate_import(const char *dirs, const char *path_form, char *out,
|
|
size_t outsz, int *is_dir)
|
|
{
|
|
for (int want_dir = 1; want_dir >= 0; want_dir--) {
|
|
const char *p = dirs;
|
|
while (*p) {
|
|
const char *e = strchr(p, ':');
|
|
size_t n = e ? (size_t)(e - p) : strlen(p);
|
|
if (n > 0 && n < outsz) {
|
|
char dir[1024];
|
|
if (n >= sizeof dir) n = sizeof dir - 1;
|
|
memcpy(dir, p, n);
|
|
dir[n] = '\0';
|
|
if (locate_import_in(dir, path_form, out,
|
|
outsz, is_dir, want_dir)) return 1;
|
|
}
|
|
if (!e) break;
|
|
p = e + 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* memcmp-based string compare for qsort. Byte-wise total order is
|
|
* locale-independent; rule-10 byte-id requires the two stages sort
|
|
* the same way. (strcmp would work today but Hare-fidelity points
|
|
* at memcmp via ref/hare/sort/cmp/cmp.ha:9.) */
|
|
static int
|
|
strs_cmp(const void *a, const void *b)
|
|
{
|
|
const char *sa = *(const char *const *)a;
|
|
const char *sb = *(const char *const *)b;
|
|
return strcmp(sa, sb);
|
|
}
|
|
|
|
/* enumerate_dir_ww — collect *.ww names in `dirpath` excluding
|
|
* *test.ww, sort byte-wise. Returns count; caller frees entries. */
|
|
static int
|
|
enumerate_dir_ww(const char *dirpath, char ***out_files)
|
|
{
|
|
DIR *d = opendir(dirpath);
|
|
if (d == NULL) { *out_files = NULL; return 0; }
|
|
char **arr = NULL;
|
|
int n = 0, cap = 0;
|
|
struct dirent *ent;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
const char *nm = ent->d_name;
|
|
size_t nl = strlen(nm);
|
|
if (nl <= 3) continue;
|
|
if (strcmp(nm + nl - 3, ".ww") != 0) continue;
|
|
/* skip "*test.ww" (no underscore — bytestest.ww
|
|
* ostest.ww utf8test.ww — Hare convention is _test.ha
|
|
* but ww corpus settled on the un-underscored shape). */
|
|
if (nl >= 7 && strcmp(nm + nl - 7, "test.ww") == 0)
|
|
continue;
|
|
/* skip "*.combined.ww" — driver-generated concat
|
|
* artifacts (the previous build leaves them next to
|
|
* the source). They look like .ww but parse-erroring
|
|
* when re-included. */
|
|
if (nl >= 12 && strcmp(nm + nl - 12, ".combined.ww") == 0)
|
|
continue;
|
|
if (n + 1 > cap) {
|
|
cap = cap ? cap * 2 : 8;
|
|
arr = realloc(arr, cap * sizeof *arr);
|
|
}
|
|
arr[n++] = strdup(nm);
|
|
}
|
|
closedir(d);
|
|
if (n > 1) qsort(arr, n, sizeof *arr, strs_cmp);
|
|
*out_files = arr;
|
|
return n;
|
|
}
|
|
|
|
/* ====================================================================
|
|
* ww build — separate-compilation driver (task #46/c3).
|
|
* ====================================================================
|
|
* This is the SOLE build path (E3-C1 flip, task #87): the legacy
|
|
* single-file amalgamator (build_one/expand) is gone. Each imported
|
|
* package's `.wwi` interface is materialized and every package is
|
|
* compiled on its own (`w6c -c`), then the `.o` set is flat-linked.
|
|
*
|
|
* Each w6c pass is BOTH consumer (reads dep `.wwi` as import scope) AND
|
|
* producer (writes this package's `.wwi` for its importers via -I), so
|
|
* a package's interface is materialized as a side effect of compiling
|
|
* it. Reverse-topo order guarantees a package's deps' `.wwi` exist
|
|
* before it compiles.
|
|
*
|
|
* The load-bearing rule (ken #56, rob-resolved): every dep is tagged by
|
|
* its FULL DOTTED import path on prepend (`//ww:module <path>`), so the
|
|
* definer's qualified symbol (#53) equals the consumer's qualified
|
|
* reference (#40) and the sep `.o`s link. A dep is NEVER bare-embedded.
|
|
*
|
|
* The prepend is the TRANSITIVE closure of a package's deps (lead-
|
|
* ratified, superseding rob-c3-spec §1.3 "direct deps"): a dep's public
|
|
* interface can name a transitive dep's type (os exposes time.instant),
|
|
* so the consuming unit needs the whole closure for name RESOLUTION —
|
|
* direct-deps-only does not type-check. This mirrors harec reading the
|
|
* transitive `.td` closure and is consistent with the current flat-unit
|
|
* transitive-namespace model (the visibility tighten is task #45,
|
|
* deferred post-M4).
|
|
*/
|
|
#define SEP_MAXPKG 256
|
|
|
|
struct seppkg {
|
|
char path[256]; /* dotted import path; "" == root/primary */
|
|
char entry[1024]; /* resolved package dir (or file, for a file root) */
|
|
int is_dir;
|
|
int deps[SEP_MAXPKG]; /* direct-dep indices into sepgraph.pkg */
|
|
int ndeps;
|
|
int color; /* tri-color DFS: 0 white, 1 gray, 2 black */
|
|
};
|
|
|
|
struct sepgraph {
|
|
struct seppkg pkg[SEP_MAXPKG];
|
|
int n;
|
|
};
|
|
|
|
/* Find a package by dotted path, or add it. Returns its index, -1 full. */
|
|
static int
|
|
sep_find_or_add(struct sepgraph *g, const char *path, const char *entry,
|
|
int is_dir)
|
|
{
|
|
for (int i = 0; i < g->n; i++)
|
|
if (strcmp(g->pkg[i].path, path) == 0) return i;
|
|
if (g->n >= SEP_MAXPKG) {
|
|
fprintf(stderr, "ww --sep: too many packages (limit %d)\n",
|
|
SEP_MAXPKG);
|
|
return -1;
|
|
}
|
|
struct seppkg *p = &g->pkg[g->n];
|
|
snprintf(p->path, sizeof p->path, "%s", path);
|
|
snprintf(p->entry, sizeof p->entry, "%s", entry);
|
|
p->is_dir = is_dir;
|
|
p->ndeps = 0;
|
|
p->color = 0;
|
|
return g->n++;
|
|
}
|
|
|
|
/* Sanitize a package's dotted path into a scratch-file basename. Dots
|
|
* stay (legal in filenames); the root's empty path becomes "__root". */
|
|
static void
|
|
sep_fname(const struct sepgraph *g, int pi, const char *scratch,
|
|
const char *suffix, char *out, size_t outsz)
|
|
{
|
|
const char *base = g->pkg[pi].path[0] ? g->pkg[pi].path : "__root";
|
|
snprintf(out, outsz, "%s/%s%s", scratch, base, suffix);
|
|
}
|
|
|
|
/* unit_has_package — does `path` declare `package <leaf>;` ANYWHERE?
|
|
* #16 ENFORCE-driver (rob A): distinguishes a genuinely-missing import
|
|
* from one satisfied by an INLINE package in the same unit. Unlike
|
|
* peek_package (stops at the FIRST package decl), this scans every line
|
|
* — single-file multi-package fixtures carry several `package` decls. The
|
|
* comment-skip line scan + name match mirror peek_package, uncapped. The
|
|
* wwstage twin unithaspackage must stay byte-identical (rule 10). */
|
|
static int
|
|
unit_has_package(const char *path, const char *leaf)
|
|
{
|
|
FILE *in = fopen(path, "rb");
|
|
if (in == NULL) return 0;
|
|
char line[2048];
|
|
int found = 0;
|
|
while (fgets(line, sizeof line, in)) {
|
|
const char *p = line;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
if (p[0] == '/' && p[1] == '/') continue;
|
|
if (strncmp(p, "package ", 8) != 0
|
|
&& strncmp(p, "package\t", 8) != 0) continue;
|
|
p += 8;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
size_t i = 0;
|
|
while (leaf[i] != '\0' && leaf[i] == p[i]) i++;
|
|
if (leaf[i] == '\0') {
|
|
char c = p[i];
|
|
if (c == ';' || c == ' ' || c == '\t'
|
|
|| c == '\n' || c == '\0') { found = 1; break; }
|
|
}
|
|
}
|
|
fclose(in);
|
|
return found;
|
|
}
|
|
|
|
/* Scan one source file for top-level `import IDENT;`, resolving each. A
|
|
* DIRECTORY import is a package boundary: add it as a direct dep of pkg
|
|
* `pi`. A FILE import is an intra-package split — fold its imports into
|
|
* `pi` (its bytes join pi's body at emit time). Mirrors expand's scan
|
|
* but collects package PATHS instead of concatenating bytes (§1.1). */
|
|
static int
|
|
sep_scan_file(struct sepgraph *g, int pi, const char *file,
|
|
const char *searchpath, struct ImportSet *filevisit)
|
|
{
|
|
if (import_seen(filevisit, file)) return 0;
|
|
import_add(filevisit, file);
|
|
FILE *in = fopen(file, "rb");
|
|
if (in == NULL) {
|
|
fprintf(stderr, "ww --sep: cannot read %s\n", file);
|
|
return -1;
|
|
}
|
|
char line[2048];
|
|
int rc = 0;
|
|
while (fgets(line, sizeof line, in)) {
|
|
const char *p = line;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
if (strncmp(p, "import ", 7) != 0 && strncmp(p, "import\t", 7) != 0)
|
|
continue;
|
|
p += 7;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
char name[256] = {0};
|
|
int j = 0;
|
|
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')
|
|
|| *p == '_' || *p == '.' || (*p >= '0' && *p <= '9'))
|
|
if (j + 1 < (int)sizeof name) name[j++] = *p++;
|
|
if (j == 0) continue;
|
|
char path_form[256];
|
|
import_path_form(name, path_form, sizeof path_form);
|
|
char ipath[1024];
|
|
int is_dir = 0;
|
|
/* #16 ENFORCE-driver: an unresolvable import is a hard error,
|
|
* not a silent skip — EXCEPT when the package is defined INLINE
|
|
* in the same unit (single-file multi-package; leaf = the last
|
|
* dotted component). E3-C1: the combined path that used to own
|
|
* the genuine-missing case is gone, so the sep producer enforces
|
|
* it here (INV-2, by construction). Mirrors the deleted expand. */
|
|
if (!locate_import(searchpath, path_form, ipath, sizeof ipath,
|
|
&is_dir)) {
|
|
const char *dot = strrchr(name, '.');
|
|
const char *leaf = dot ? dot + 1 : name;
|
|
if (unit_has_package(file, leaf))
|
|
continue; /* inline-satisfied */
|
|
fprintf(stderr, "ww: cannot find package %s\n", name);
|
|
exit(1);
|
|
}
|
|
if (is_dir) {
|
|
int di = sep_find_or_add(g, name, ipath, 1);
|
|
if (di < 0) { rc = -1; break; }
|
|
int seen = 0;
|
|
for (int k = 0; k < g->pkg[pi].ndeps; k++)
|
|
if (g->pkg[pi].deps[k] == di) { seen = 1; break; }
|
|
if (!seen) {
|
|
if (g->pkg[pi].ndeps >= SEP_MAXPKG) { rc = -1; break; }
|
|
g->pkg[pi].deps[g->pkg[pi].ndeps++] = di;
|
|
}
|
|
} else if (sep_scan_file(g, pi, ipath, searchpath, filevisit) < 0) {
|
|
rc = -1; break;
|
|
}
|
|
}
|
|
fclose(in);
|
|
return rc;
|
|
}
|
|
|
|
/* Discover pkg pi's direct deps + recurse. Enumerate the package's own
|
|
* source files (dir → *.ww less *test.ww; file → the file) and scan
|
|
* each. `color` doubles as a scanned-marker here (2 == scanned); it is
|
|
* reset to white before the topo pass. */
|
|
static int
|
|
sep_scan_pkg(struct sepgraph *g, int pi, const char *searchpath)
|
|
{
|
|
if (g->pkg[pi].color == 2) return 0;
|
|
g->pkg[pi].color = 2;
|
|
struct ImportSet fv = {0};
|
|
int rc = 0;
|
|
if (g->pkg[pi].is_dir) {
|
|
char **files = NULL;
|
|
int n = enumerate_dir_ww(g->pkg[pi].entry, &files);
|
|
for (int i = 0; i < n && rc == 0; i++) {
|
|
char fp[1024];
|
|
snprintf(fp, sizeof fp, "%s/%s", g->pkg[pi].entry, files[i]);
|
|
rc = sep_scan_file(g, pi, fp, searchpath, &fv);
|
|
}
|
|
for (int i = 0; i < n; i++) free(files[i]);
|
|
free(files);
|
|
} else {
|
|
rc = sep_scan_file(g, pi, g->pkg[pi].entry, searchpath, &fv);
|
|
}
|
|
for (int i = 0; i < fv.n; i++) free(fv.paths[i]);
|
|
free(fv.paths);
|
|
if (rc < 0) return rc;
|
|
/* recurse into freshly-added deps (sep_find_or_add may have grown
|
|
* g->n during the scan; iterate by index). */
|
|
for (int k = 0; k < g->pkg[pi].ndeps; k++)
|
|
if (sep_scan_pkg(g, g->pkg[pi].deps[k], searchpath) < 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
/* DFS post-order over the dep DAG → reverse-topo (deps before importer),
|
|
* cite Hare gather (deps.ha:123). 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 back on pi. */
|
|
static int
|
|
sep_topo_visit(struct sepgraph *g, int pi, int *order, int *no,
|
|
int *stack, int depth)
|
|
{
|
|
if (g->pkg[pi].color == 2) return 0;
|
|
if (g->pkg[pi].color == 1) {
|
|
int j = 0;
|
|
while (j < depth && stack[j] != pi) j++;
|
|
fprintf(stderr, "ww --sep: dependency cycle: ");
|
|
for (int s = j; s < depth; s++)
|
|
fprintf(stderr, "%s -> ", g->pkg[stack[s]].path[0]
|
|
? g->pkg[stack[s]].path : "(root)");
|
|
fprintf(stderr, "%s\n",
|
|
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
|
return -1;
|
|
}
|
|
g->pkg[pi].color = 1;
|
|
stack[depth] = pi;
|
|
for (int k = 0; k < g->pkg[pi].ndeps; k++)
|
|
if (sep_topo_visit(g, g->pkg[pi].deps[k], order, no,
|
|
stack, depth + 1) < 0)
|
|
return -1;
|
|
g->pkg[pi].color = 2;
|
|
order[(*no)++] = pi;
|
|
return 0;
|
|
}
|
|
|
|
/* Mark pi's transitive deps (excluding pi) in inset[]. */
|
|
static void
|
|
sep_mark_deps(struct sepgraph *g, int pi, char *inset)
|
|
{
|
|
for (int k = 0; k < g->pkg[pi].ndeps; k++) {
|
|
int di = g->pkg[pi].deps[k];
|
|
if (!inset[di]) { inset[di] = 1; sep_mark_deps(g, di, inset); }
|
|
}
|
|
}
|
|
|
|
/* 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 of the
|
|
* body); FILE imports fold in (intra-package split). */
|
|
static void
|
|
sep_emit_body(FILE *out, const char *path, struct ImportSet *visited,
|
|
const char *searchpath, const char *modpath)
|
|
{
|
|
if (import_seen(visited, path)) return;
|
|
import_add(visited, path);
|
|
FILE *in = fopen(path, "rb");
|
|
if (in == NULL) {
|
|
fprintf(stderr, "ww --sep: cannot read %s\n", path);
|
|
return;
|
|
}
|
|
char line[2048];
|
|
while (fgets(line, sizeof line, in)) {
|
|
const char *p = line;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
if (strncmp(p, "import ", 7) != 0 && strncmp(p, "import\t", 7) != 0)
|
|
continue;
|
|
p += 7;
|
|
while (*p == ' ' || *p == '\t') p++;
|
|
char name[256] = {0};
|
|
int j = 0;
|
|
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')
|
|
|| *p == '_' || *p == '.' || (*p >= '0' && *p <= '9'))
|
|
if (j + 1 < (int)sizeof name) name[j++] = *p++;
|
|
if (j == 0) continue;
|
|
char path_form[256];
|
|
import_path_form(name, path_form, sizeof path_form);
|
|
char ipath[1024];
|
|
int is_dir = 0;
|
|
if (!locate_import(searchpath, path_form, ipath, sizeof ipath,
|
|
&is_dir))
|
|
continue;
|
|
if (!is_dir)
|
|
sep_emit_body(out, ipath, visited, searchpath, modpath);
|
|
}
|
|
/* #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 != NULL && modpath[0] != '\0')
|
|
fprintf(out, "//ww:module-reset %s\n", modpath);
|
|
else
|
|
fputs("//ww:module-reset\n", out);
|
|
rewind(in);
|
|
int ch;
|
|
while ((ch = fgetc(in)) != EOF) fputc(ch, out);
|
|
fputc('\n', out);
|
|
fclose(in);
|
|
}
|
|
|
|
static void
|
|
sep_emit_dir_body(FILE *out, const char *dir, struct ImportSet *visited,
|
|
const char *searchpath, const char *modpath)
|
|
{
|
|
char **files = NULL;
|
|
int n = enumerate_dir_ww(dir, &files);
|
|
for (int i = 0; i < n; i++) {
|
|
char fp[1024];
|
|
snprintf(fp, sizeof fp, "%s/%s", dir, files[i]);
|
|
sep_emit_body(out, fp, visited, searchpath, modpath);
|
|
free(files[i]);
|
|
}
|
|
free(files);
|
|
}
|
|
|
|
/* Compose pi's sep-unit at `unitf`: the transitive-closure `.wwi`s
|
|
* (reverse-topo order, each tagged by its dotted path), then pi's own
|
|
* body under //ww:module-reset. */
|
|
static int
|
|
sep_compose_unit(struct sepgraph *g, int pi, const char *scratch,
|
|
const int *order, int norder, const char *searchpath, const char *unitf)
|
|
{
|
|
FILE *u = fopen(unitf, "wb");
|
|
if (u == NULL) {
|
|
fprintf(stderr, "ww --sep: cannot open %s\n", unitf);
|
|
return -1;
|
|
}
|
|
char inset[SEP_MAXPKG] = {0};
|
|
sep_mark_deps(g, pi, inset);
|
|
for (int oi = 0; oi < norder; oi++) {
|
|
int dj = order[oi];
|
|
if (dj == pi || !inset[dj]) continue;
|
|
char wwi[1024];
|
|
sep_fname(g, dj, scratch, ".wwi", wwi, sizeof wwi);
|
|
FILE *wf = fopen(wwi, "rb");
|
|
if (wf == NULL) {
|
|
fprintf(stderr, "ww --sep: missing %s\n", wwi);
|
|
fclose(u);
|
|
return -1;
|
|
}
|
|
fprintf(u, "//ww:module %s\n", g->pkg[dj].path);
|
|
int ch;
|
|
while ((ch = fgetc(wf)) != EOF) fputc(ch, u);
|
|
fputc('\n', u);
|
|
fclose(wf);
|
|
}
|
|
struct ImportSet bodyvisit = {0};
|
|
if (g->pkg[pi].is_dir)
|
|
sep_emit_dir_body(u, g->pkg[pi].entry, &bodyvisit, searchpath,
|
|
g->pkg[pi].path);
|
|
else
|
|
sep_emit_body(u, g->pkg[pi].entry, &bodyvisit, searchpath,
|
|
g->pkg[pi].path);
|
|
for (int i = 0; i < bodyvisit.n; i++) free(bodyvisit.paths[i]);
|
|
free(bodyvisit.paths);
|
|
fclose(u);
|
|
return 0;
|
|
}
|
|
|
|
/* archive_o — write a deterministic single-member SysV ar archive at
|
|
* `apath` wrapping the object at `objpath`. No armap / long-name table:
|
|
* w6l reads each member's ELF .symtab directly (obj.c elf_globals) and
|
|
* skips '/'-named members, so a package `.a` needs only the global magic,
|
|
* one 60-byte member header, and the `.o` bytes (newline-padded to even).
|
|
* Zeroed mtime/uid/gid + fixed mode + a fixed member name make the bytes
|
|
* a pure function of the `.o` content → cstage `.a` == wwstage `.a`
|
|
* (rule 10) and a stable md5 for the 5b cache key. The wwstage twin is
|
|
* archiveo (selfhost/cmd/ww/main.ww). */
|
|
static int
|
|
archive_o(const char *objpath, const char *apath)
|
|
{
|
|
FILE *in = fopen(objpath, "rb");
|
|
if (in == NULL) {
|
|
fprintf(stderr, "ww --sep: cannot read %s\n", objpath);
|
|
return -1;
|
|
}
|
|
fseek(in, 0, SEEK_END);
|
|
long n = ftell(in);
|
|
fseek(in, 0, SEEK_SET);
|
|
if (n < 0) { fclose(in); return -1; }
|
|
unsigned char *buf = malloc((size_t)n);
|
|
if (buf == NULL) { fclose(in); return -1; }
|
|
if (fread(buf, 1, (size_t)n, in) != (size_t)n) {
|
|
free(buf); fclose(in); return -1;
|
|
}
|
|
fclose(in);
|
|
|
|
FILE *out = fopen(apath, "wb");
|
|
if (out == NULL) {
|
|
fprintf(stderr, "ww --sep: cannot open %s\n", apath);
|
|
free(buf);
|
|
return -1;
|
|
}
|
|
fwrite("!<arch>\n", 1, 8, out);
|
|
/* sizelint-ok: the 60-byte ar(5) member header and its field offsets
|
|
* are a FILE-FORMAT constant, not a type size (CLAUDE.md rule 13). */
|
|
char hdr[60];
|
|
memset(hdr, ' ', sizeof hdr);
|
|
memcpy(hdr + 0, "pkg.o/", 6); /* GNU short-name '/' terminator */
|
|
hdr[16] = '0'; /* mtime (zeroed → determinism) */
|
|
hdr[28] = '0'; /* uid (zeroed) */
|
|
hdr[34] = '0'; /* gid (zeroed) */
|
|
memcpy(hdr + 40, "100644", 6); /* mode (fixed octal) */
|
|
char sz[12];
|
|
int szn = snprintf(sz, sizeof sz, "%lu", (unsigned long)n);
|
|
memcpy(hdr + 48, sz, (size_t)szn);
|
|
hdr[58] = 0x60; /* member-header magic byte */
|
|
hdr[59] = 0x0a;
|
|
fwrite(hdr, 1, sizeof hdr, out);
|
|
fwrite(buf, 1, (size_t)n, out);
|
|
if (n & 1) fputc('\n', out); /* members are 2-byte aligned */
|
|
fclose(out);
|
|
free(buf);
|
|
return 0;
|
|
}
|
|
|
|
/* ---- 5b content-keyed package cache (#63) ----------------------------
|
|
*
|
|
* A per-package cache under WW_PKGCACHE (default out/.pkgcache; gitignored,
|
|
* make clean wipes $(OUT)). Purely a dev-inner-loop convenience: every
|
|
* bootstrap/byte-id gate cold-compiles (--sep scratch is wiped each run), so
|
|
* the cache changes NO gate output. Mirrors out/.testcache + Hare get_cache.
|
|
*
|
|
* rule-10: md5 is NOT reimplemented per stage — both stages shell to the
|
|
* host md5sum and assemble an identical text manifest, so a HIT's reused
|
|
* P.wwi/P.o stay byte-identical cs==ww by construction. The compiler-binary
|
|
* line keys CODEGEN identity, so it is intentionally stage-specific (w6c vs
|
|
* w6c_ww) — each stage maintains its own cache namespace; the cacheable
|
|
* OUTPUTS it reuses remain byte-identical. */
|
|
|
|
static const char *
|
|
pkgcache_root(void)
|
|
{
|
|
const char *p = getenv("WW_PKGCACHE");
|
|
if (p && p[0]) return p;
|
|
return "out/.pkgcache";
|
|
}
|
|
|
|
/* Hex md5 digest of `path` via the host tool. The cache is gate-cold, so a
|
|
* host-tool dep is sanctioned (md5sum is already a build dependency). */
|
|
static int
|
|
md5_file(const char *path, char *hex, size_t hexsz)
|
|
{
|
|
char cmd[1200];
|
|
snprintf(cmd, sizeof cmd, "md5sum '%s' 2>/dev/null", path);
|
|
FILE *p = popen(cmd, "r");
|
|
if (p == NULL) return -1;
|
|
char line[256];
|
|
char *got = fgets(line, sizeof line, p);
|
|
pclose(p);
|
|
if (got == NULL) return -1;
|
|
size_t i = 0;
|
|
while (i + 1 < hexsz && line[i] && line[i] != ' '
|
|
&& line[i] != '\t' && line[i] != '\n') i++;
|
|
if (i == 0) return -1;
|
|
memcpy(hex, line, i);
|
|
hex[i] = '\0';
|
|
return 0;
|
|
}
|
|
|
|
/* Assemble package pi's content-key manifest (D4) into `out` as deterministic
|
|
* text: P's own *.ww md5s (sorted by name) | each DIRECT dep's .wwi md5
|
|
* (sorted by dep path) | the w6c/w6a md5s | the flag string. DIRECT deps
|
|
* only — reverse-topo folds transitivity bottom-up, since a dep's .wwi
|
|
* already folds ITS deps. Returns 0 on success, -1 on any md5/truncation
|
|
* failure (caller treats that as non-cacheable → cold compile). */
|
|
static int
|
|
sep_manifest(struct sepgraph *g, int pi, const char *scratch,
|
|
const char *c6, const char *a6, char *out, size_t outsz)
|
|
{
|
|
size_t off = 0;
|
|
char hex[64];
|
|
|
|
off += (size_t)snprintf(out + off, outsz - off, "src");
|
|
if (g->pkg[pi].is_dir) {
|
|
char **files = NULL;
|
|
int n = enumerate_dir_ww(g->pkg[pi].entry, &files);
|
|
int rc = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
char fp[1024];
|
|
snprintf(fp, sizeof fp, "%s/%s", g->pkg[pi].entry,
|
|
files[i]);
|
|
if (rc == 0 && md5_file(fp, hex, sizeof hex) == 0)
|
|
off += (size_t)snprintf(out + off, outsz - off,
|
|
" %s", hex);
|
|
else
|
|
rc = -1;
|
|
free(files[i]);
|
|
}
|
|
free(files);
|
|
if (rc != 0) return -1;
|
|
} else {
|
|
if (md5_file(g->pkg[pi].entry, hex, sizeof hex) != 0)
|
|
return -1;
|
|
off += (size_t)snprintf(out + off, outsz - off, " %s", hex);
|
|
}
|
|
off += (size_t)snprintf(out + off, outsz - off, "\n");
|
|
|
|
/* dep lines, sorted by dep path (deps[] are in import-appearance
|
|
* order; insertion-sort the indices for a deterministic manifest). */
|
|
int nd = g->pkg[pi].ndeps;
|
|
int idx[SEP_MAXPKG];
|
|
for (int i = 0; i < nd; i++) idx[i] = g->pkg[pi].deps[i];
|
|
for (int i = 1; i < nd; i++) {
|
|
int v = idx[i], j = i;
|
|
while (j > 0 &&
|
|
strcmp(g->pkg[idx[j-1]].path, g->pkg[v].path) > 0) {
|
|
idx[j] = idx[j-1];
|
|
j--;
|
|
}
|
|
idx[j] = v;
|
|
}
|
|
for (int i = 0; i < nd; i++) {
|
|
int di = idx[i];
|
|
char wwip[1024];
|
|
sep_fname(g, di, scratch, ".wwi", wwip, sizeof wwip);
|
|
if (md5_file(wwip, hex, sizeof hex) != 0) return -1;
|
|
off += (size_t)snprintf(out + off, outsz - off,
|
|
"dep %s %s\n", g->pkg[di].path, hex);
|
|
}
|
|
|
|
if (md5_file(c6, hex, sizeof hex) != 0) return -1;
|
|
off += (size_t)snprintf(out + off, outsz - off, "w6c %s\n", hex);
|
|
if (md5_file(a6, hex, sizeof hex) != 0) return -1;
|
|
off += (size_t)snprintf(out + off, outsz - off, "w6a %s\n", hex);
|
|
off += (size_t)snprintf(out + off, outsz - off, "flags -c -I\n");
|
|
if (off >= outsz) return -1; /* truncated → unusable key */
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
pkgcache_dir(struct sepgraph *g, int pi, char *out, size_t outsz)
|
|
{
|
|
const char *base = g->pkg[pi].path[0] ? g->pkg[pi].path : "__root";
|
|
snprintf(out, outsz, "%s/%s", pkgcache_root(), base);
|
|
}
|
|
|
|
/* HIT iff a freshly recomputed manifest equals the stored P.key byte-for-byte
|
|
* AND both cached artifacts exist; on HIT copy them into the scratch wwi/obj
|
|
* paths so the producer loop can skip compose+w6c+w6a. */
|
|
static int
|
|
cache_lookup(struct sepgraph *g, int pi, const char *manifest,
|
|
const char *wwi, const char *obj)
|
|
{
|
|
char dir[1024], keyp[1100], cwwi[1100], cobj[1100], cmd[4096];
|
|
pkgcache_dir(g, pi, dir, sizeof dir);
|
|
snprintf(keyp, sizeof keyp, "%s/P.key", dir);
|
|
snprintf(cwwi, sizeof cwwi, "%s/P.wwi", dir);
|
|
snprintf(cobj, sizeof cobj, "%s/P.o", dir);
|
|
|
|
FILE *f = fopen(keyp, "rb");
|
|
if (f == NULL) return 0;
|
|
char stored[16384];
|
|
size_t sn = fread(stored, 1, sizeof stored, f);
|
|
int more = (fgetc(f) != EOF);
|
|
fclose(f);
|
|
if (more || sn != strlen(manifest) || memcmp(stored, manifest, sn) != 0)
|
|
return 0;
|
|
if (access(cwwi, 0) != 0 || access(cobj, 0) != 0) return 0;
|
|
snprintf(cmd, sizeof cmd, "cp -f '%s' '%s'", cwwi, wwi);
|
|
if (run(cmd) != 0) return 0;
|
|
snprintf(cmd, sizeof cmd, "cp -f '%s' '%s'", cobj, obj);
|
|
if (run(cmd) != 0) return 0;
|
|
return 1;
|
|
}
|
|
|
|
/* On MISS, persist the freshly compiled artifacts then the manifest. The key
|
|
* is written LAST so a crash mid-store never leaves a key whose artifacts are
|
|
* absent/partial (the next run simply re-misses). */
|
|
static void
|
|
cache_store(struct sepgraph *g, int pi, const char *manifest,
|
|
const char *wwi, const char *obj)
|
|
{
|
|
char dir[1024], keyp[1100], cwwi[1100], cobj[1100], cmd[4096];
|
|
pkgcache_dir(g, pi, dir, sizeof dir);
|
|
snprintf(cmd, sizeof cmd, "mkdir -p '%s'", dir);
|
|
if (run(cmd) != 0) return;
|
|
snprintf(cwwi, sizeof cwwi, "%s/P.wwi", dir);
|
|
snprintf(cobj, sizeof cobj, "%s/P.o", dir);
|
|
snprintf(keyp, sizeof keyp, "%s/P.key", dir);
|
|
snprintf(cmd, sizeof cmd, "cp -f '%s' '%s'", wwi, cwwi);
|
|
if (run(cmd) != 0) return;
|
|
snprintf(cmd, sizeof cmd, "cp -f '%s' '%s'", obj, cobj);
|
|
if (run(cmd) != 0) return;
|
|
FILE *f = fopen(keyp, "wb");
|
|
if (f == NULL) return;
|
|
fputs(manifest, f);
|
|
fclose(f);
|
|
}
|
|
|
|
/* build_one_sep — the --sep orchestration: discover_deps, reverse_topo,
|
|
* the transitive producer loop (one `w6c -c -I` per package, dep-first,
|
|
* each DEP `.o` wrapped in its own deterministic `.a`), then a
|
|
* reverse-topo `w6l` of the root `.o` + dep `.a` set + libwwrt.a. Side
|
|
* files land in a cold `<stem>.sepwork` dir (content-keyed cache = 5b). */
|
|
static int
|
|
build_one_sep(const char *src, int entry_is_dir, const char *out,
|
|
const char *objstem, const char *extra_includes, const char *extra_libs,
|
|
const char *extra_libdirs, int is_test)
|
|
{
|
|
const char *c6 = toolpath("WW_W6C", "w6c");
|
|
const char *a6 = toolpath("WW_W6A", "w6a");
|
|
const char *l6 = toolpath("WW_W6L", "w6l");
|
|
const char *libdir = getenv("WW_LIB");
|
|
if (libdir == NULL || libdir[0] == 0) {
|
|
static char libbuf[1024];
|
|
snprintf(libbuf, sizeof libbuf, "%s/../lib", self_dir);
|
|
libdir = libbuf;
|
|
}
|
|
const char *srcdir = getenv("WW_SRCLIB");
|
|
static char srcbuf[1024];
|
|
if (srcdir == NULL || srcdir[0] == 0) {
|
|
snprintf(srcbuf, sizeof srcbuf, "%s/../../lib", self_dir);
|
|
if (access(srcbuf, 0) == 0) srcdir = srcbuf;
|
|
else if (access("lib", 0) == 0) srcdir = "lib";
|
|
else srcdir = libdir;
|
|
}
|
|
/* search path: source-dir, then -I dirs, then srcdir (mirrors
|
|
* build_one). */
|
|
char srcd[1024];
|
|
if (entry_is_dir) {
|
|
snprintf(srcd, sizeof srcd, "%s", src);
|
|
size_t n = strlen(srcd);
|
|
while (n > 1 && srcd[n-1] == '/') srcd[--n] = '\0';
|
|
} else {
|
|
const char *slash = strrchr(src, '/');
|
|
if (slash) {
|
|
size_t n = (size_t)(slash - src);
|
|
if (n >= sizeof srcd) n = sizeof srcd - 1;
|
|
memcpy(srcd, src, n);
|
|
srcd[n] = '\0';
|
|
} else { srcd[0] = '.'; srcd[1] = '\0'; }
|
|
}
|
|
static char searchpath[4096];
|
|
if (extra_includes && extra_includes[0])
|
|
snprintf(searchpath, sizeof searchpath, "%s:%s:%s",
|
|
srcd, extra_includes, srcdir);
|
|
else
|
|
snprintf(searchpath, sizeof searchpath, "%s:%s", srcd, srcdir);
|
|
srcdir = searchpath;
|
|
|
|
char stem[1024];
|
|
if (entry_is_dir) {
|
|
const char *b = strrchr(srcd, '/');
|
|
const char *base = b ? b + 1 : srcd;
|
|
snprintf(stem, sizeof stem, "%s/%s", srcd, base);
|
|
} else {
|
|
snprintf(stem, sizeof stem, "%s", src);
|
|
char *dot = strrchr(stem, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
}
|
|
const char *ostem = (objstem && objstem[0]) ? objstem : stem;
|
|
char scratch[1100];
|
|
snprintf(scratch, sizeof scratch, "%s.sepwork", ostem);
|
|
{ char m[1200]; snprintf(m, sizeof m, "rm -rf %s", scratch); run(m); }
|
|
if (mkdir(scratch, 0755) != 0) {
|
|
fprintf(stderr, "ww --sep: cannot create scratch %s\n", scratch);
|
|
return 1;
|
|
}
|
|
|
|
struct sepgraph *g = calloc(1, sizeof *g);
|
|
if (g == NULL) return 1;
|
|
int root = sep_find_or_add(g, "", src, entry_is_dir);
|
|
if (root < 0) { free(g); return 1; }
|
|
/* #79 (-T): lib/test is the synth main's `test.run` callee but @test
|
|
* files never `import test;`. Inject it as a direct dep of the root so
|
|
* sep_scan_pkg pulls test + its transitive deps; the producer adds -T
|
|
* to the root and `test.run` links against test's `.a`. Mirrors the
|
|
* combined path's auto-bundle (build_one is_test) and the sep_scan_file
|
|
* dedup-guarded dep append. */
|
|
if (is_test) {
|
|
char tpath[1024];
|
|
int tdir = 0;
|
|
if (locate_import(srcdir, "test", tpath, sizeof tpath, &tdir)) {
|
|
int ti = sep_find_or_add(g, "test", tpath, tdir);
|
|
if (ti < 0) { free(g); return 1; }
|
|
int seen = 0;
|
|
for (int k = 0; k < g->pkg[root].ndeps; k++)
|
|
if (g->pkg[root].deps[k] == ti) { seen = 1; break; }
|
|
if (!seen && g->pkg[root].ndeps < SEP_MAXPKG)
|
|
g->pkg[root].deps[g->pkg[root].ndeps++] = ti;
|
|
}
|
|
}
|
|
if (sep_scan_pkg(g, root, srcdir) < 0) { free(g); return 1; }
|
|
for (int i = 0; i < g->n; i++) g->pkg[i].color = 0;
|
|
int *order = calloc((size_t)g->n, sizeof *order);
|
|
int *stack = calloc((size_t)g->n, sizeof *stack);
|
|
int norder = 0;
|
|
if (order == NULL || stack == NULL ||
|
|
sep_topo_visit(g, root, order, &norder, stack, 0) < 0) {
|
|
free(stack); free(order); free(g); return 1;
|
|
}
|
|
free(stack);
|
|
|
|
/* producer loop — dep-first, one `w6c -c -I` pass per package. */
|
|
for (int oi = 0; oi < norder; oi++) {
|
|
int pi = order[oi];
|
|
char unitf[1024], wwi[1024], asmf[1024], obj[1024], cmd[8192];
|
|
sep_fname(g, pi, scratch, ".unit.ww", unitf, sizeof unitf);
|
|
sep_fname(g, pi, scratch, ".wwi", wwi, sizeof wwi);
|
|
sep_fname(g, pi, scratch, ".s", asmf, sizeof asmf);
|
|
sep_fname(g, pi, scratch, ".o", obj, sizeof obj);
|
|
/* 5b: skip compose+w6c+w6a on a content-key HIT. The root is
|
|
* never cached — it is the build target, always recompiled. */
|
|
char manifest[16384];
|
|
int cacheable = (pi != root) && sep_manifest(g, pi, scratch,
|
|
c6, a6, manifest, sizeof manifest) == 0;
|
|
int fresh = cacheable && cache_lookup(g, pi, manifest, wwi, obj);
|
|
if (!fresh) {
|
|
if (sep_compose_unit(g, pi, scratch, order, norder, srcdir,
|
|
unitf) < 0) { free(order); free(g); return 1; }
|
|
/* BUG-1 (#69): -I <wwi> is purely the root's UNUSED
|
|
* `.wwi` output path, but it triggers wwi_emit →
|
|
* check_exported_type 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. Skip -I for the root; its `.wwi`
|
|
* is never consumed. */
|
|
if (pi == root)
|
|
/* #79: the root carries -T under `ww test --sep`
|
|
* so w6c synthesizes the test main. Deps never
|
|
* get -T. */
|
|
snprintf(cmd, sizeof cmd, "%s %s-c -o %s %s",
|
|
c6, is_test ? "-T " : "", asmf, unitf);
|
|
else
|
|
snprintf(cmd, sizeof cmd, "%s -c -I %s -o %s %s",
|
|
c6, wwi, asmf, unitf);
|
|
if (run(cmd) != 0) {
|
|
fprintf(stderr, "ww --sep: w6c failed for %s\n",
|
|
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
|
free(order); free(g); return 1;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s", a6, obj, asmf);
|
|
if (run(cmd) != 0) {
|
|
fprintf(stderr, "ww --sep: w6a failed for %s\n",
|
|
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
|
free(order); free(g); return 1;
|
|
}
|
|
if (cacheable) cache_store(g, pi, manifest, wwi, obj);
|
|
}
|
|
/* 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, always fully linked), so `main` is defined
|
|
* before any archive is processed, matching build_one's root
|
|
* treatment. The link consumes `.o`/`.a`, never `.wwi`. */
|
|
if (pi != root) {
|
|
char apath[1024];
|
|
sep_fname(g, pi, scratch, ".a", apath, sizeof apath);
|
|
if (archive_o(obj, apath) != 0) {
|
|
fprintf(stderr, "ww --sep: archive failed for %s\n",
|
|
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
|
free(order); free(g); return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* reverse-topo link: root `.o` first (order[norder-1], force-loaded),
|
|
* then transitive dep `.a` in reverse-topo order, then libwwrt.a —
|
|
* each archive selectively pulls only members satisfying a live
|
|
* undef. */
|
|
char rtargs[2048] = {0};
|
|
char rtpath[1024];
|
|
snprintf(rtpath, sizeof rtpath, "%s/libwwrt.a", libdir);
|
|
if (access(rtpath, 0) == 0) {
|
|
snprintf(rtargs, sizeof rtargs, "%s", rtpath);
|
|
} else {
|
|
char a1[1024], a2[1024];
|
|
snprintf(a1, sizeof a1, "%s/../obj/rt/start.o", self_dir);
|
|
snprintf(a2, sizeof a2, "%s/../obj/rt/syscall.o", self_dir);
|
|
snprintf(rtargs, sizeof rtargs, "%s %s", a1, a2);
|
|
}
|
|
char objs[8192] = {0};
|
|
for (int oi = norder - 1; oi >= 0; oi--) {
|
|
char path[1024];
|
|
/* root: positional `.o` (force-load); deps: `.a` (selective). */
|
|
sep_fname(g, order[oi], scratch,
|
|
order[oi] == root ? ".o" : ".a", path, sizeof path);
|
|
size_t n = strlen(objs);
|
|
snprintf(objs + n, sizeof objs - n, "%s%s", n ? " " : "", path);
|
|
}
|
|
const char *libargs = (extra_libs && extra_libs[0]) ? extra_libs : "";
|
|
const char *libdirset = (extra_libdirs && extra_libdirs[0]) ? extra_libdirs : "";
|
|
char cmd[16384];
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s %s%s%s%s%s",
|
|
l6, out, objs, rtargs,
|
|
libdirset[0] ? " " : "", libdirset,
|
|
libargs[0] ? " " : "", libargs);
|
|
int rc = run(cmd);
|
|
free(order); free(g);
|
|
if (rc != 0) { fprintf(stderr, "ww --sep: w6l failed\n"); return 1; }
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
do_version(void)
|
|
{
|
|
printf("ww %s\n", WW_VERSION);
|
|
return 0;
|
|
}
|
|
|
|
/* Compose the standard module search path: cwd : <extra-includes> : $WW_LIB
|
|
* source dir. The `extra` string is colon-separated -I dirs from argv. */
|
|
static const char *
|
|
search_path(const char *extra, char *buf, size_t bufsz)
|
|
{
|
|
const char *libdir = getenv("WW_SRCLIB");
|
|
static char libbuf[1024];
|
|
if (libdir == NULL || libdir[0] == 0) {
|
|
libdir = getenv("WW_LIB");
|
|
}
|
|
if (libdir == NULL || libdir[0] == 0) {
|
|
snprintf(libbuf, sizeof libbuf, "%s/../../lib", self_dir);
|
|
if (access(libbuf, 0) == 0) libdir = libbuf;
|
|
else if (access("lib", 0) == 0) libdir = "lib";
|
|
else {
|
|
snprintf(libbuf, sizeof libbuf, "%s/../lib", self_dir);
|
|
libdir = libbuf;
|
|
}
|
|
}
|
|
if (extra && extra[0])
|
|
snprintf(buf, bufsz, ".:%s:%s", extra, libdir);
|
|
else
|
|
snprintf(buf, bufsz, ".:%s", libdir);
|
|
return buf;
|
|
}
|
|
|
|
/* basename_no_ext: last path segment with any trailing ".ww" stripped. */
|
|
static void
|
|
basename_no_ext(const char *path, char *out, size_t outsz)
|
|
{
|
|
const char *base = strrchr(path, '/');
|
|
base = base ? base + 1 : path;
|
|
snprintf(out, outsz, "%s", base);
|
|
char *dot = strrchr(out, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
}
|
|
|
|
/* resolve_module: turn a name into a concrete entry path.
|
|
* foo.ww → use as-is if it exists
|
|
* <existing dir> → returns the dir path (caller dir-enumerates)
|
|
* . → cwd as a directory
|
|
* foo (bare) → walk cwd:incs:WW_LIB; first hit is dir or file.
|
|
* Sets *is_dir on hit. Dir resolution drives directory-as-module
|
|
* enumeration in build_one. */
|
|
static int
|
|
resolve_module(const char *name, const char *incs, char *out, size_t outsz,
|
|
int *is_dir)
|
|
{
|
|
struct stat st;
|
|
if (stat(name, &st) == 0) {
|
|
if (S_ISREG(st.st_mode)) {
|
|
snprintf(out, outsz, "%s", name);
|
|
*is_dir = 0;
|
|
return 1;
|
|
}
|
|
if (S_ISDIR(st.st_mode)) {
|
|
snprintf(out, outsz, "%s", name);
|
|
*is_dir = 1;
|
|
return 1;
|
|
}
|
|
}
|
|
char sp[4096];
|
|
search_path(incs, sp, sizeof sp);
|
|
char path_form[256];
|
|
import_path_form(name, path_form, sizeof path_form);
|
|
return locate_import(sp, path_form, out, outsz, is_dir);
|
|
}
|
|
|
|
/* Append `path` to a heap string-array. Caller frees each entry + the array. */
|
|
static void
|
|
strarr_push(char ***arr, int *n, int *cap, const char *path)
|
|
{
|
|
if (*n + 1 > *cap) {
|
|
*cap = *cap ? *cap * 2 : 8;
|
|
*arr = realloc(*arr, *cap * sizeof **arr);
|
|
}
|
|
(*arr)[(*n)++] = strdup(path);
|
|
}
|
|
|
|
/* collect_tests: enumerate _test.ww files in <dir> (no recursion).
|
|
* Returns 0 on success and writes the file list + count, -1 on failure. */
|
|
static int
|
|
collect_tests(const char *dir, char ***files, int *n)
|
|
{
|
|
DIR *d = opendir(dir);
|
|
if (!d) return -1;
|
|
*files = NULL;
|
|
*n = 0;
|
|
int cap = 0;
|
|
struct dirent *ent;
|
|
while ((ent = readdir(d)) != NULL) {
|
|
const char *nm = ent->d_name;
|
|
size_t nl = strlen(nm);
|
|
const char *suf = "_test.ww";
|
|
size_t sl = strlen(suf);
|
|
if (nl <= sl) continue;
|
|
if (strcmp(nm + nl - sl, suf) != 0) continue;
|
|
char path[1024];
|
|
snprintf(path, sizeof path, "%s/%s", dir, nm);
|
|
strarr_push(files, n, &cap, path);
|
|
}
|
|
closedir(d);
|
|
return 0;
|
|
}
|
|
|
|
/* Parse the standard -I/-L/-l/-o flags into incs/libdirs/libs/outpath. The
|
|
* first non-flag positional becomes *src_out. Returns the index past the last
|
|
* arg consumed for positionals (so callers can pick up trailing args), or -1
|
|
* if a flag is missing its argument (diagnostic already emitted). `cmd` names
|
|
* the subcommand for the diagnostic, byte-identical to the wwstage twin's
|
|
* per-subcommand wording (selfhost/cmd/ww/main.ww dobuild/dorun). */
|
|
static int
|
|
parse_build_flags(const char *cmd, int argc, char **argv,
|
|
char *incs, size_t incsz,
|
|
char *libdirs, size_t libdirsz,
|
|
char *libs, size_t libsz,
|
|
char *outpath, size_t outsz,
|
|
const char **src_out)
|
|
{
|
|
*src_out = NULL;
|
|
int i = 0;
|
|
for (; i < argc; i++) {
|
|
if (strcmp(argv[i], "--sep") == 0) {
|
|
/* E3-C1 flip: separate compilation is now the sole build
|
|
* path, so --sep no longer selects anything. Retained as an
|
|
* accepted no-op (every subcommand) so the existing --sep
|
|
* gate corpus keeps driving the default path. (Task #87.) */
|
|
;
|
|
} else if (strncmp(argv[i], "-l", 2) == 0 && argv[i][2]) {
|
|
size_t n = strlen(libs);
|
|
snprintf(libs + n, libsz - n,
|
|
"%s%s", n ? " " : "", argv[i]);
|
|
} else if (strcmp(argv[i], "-l") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww %s: -l needs an argument\n", cmd);
|
|
return -1;
|
|
}
|
|
size_t n = strlen(libs);
|
|
snprintf(libs + n, libsz - n,
|
|
"%s-l%s", n ? " " : "", argv[++i]);
|
|
} else if (strcmp(argv[i], "-L") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww %s: -L needs an argument\n", cmd);
|
|
return -1;
|
|
}
|
|
size_t n = strlen(libdirs);
|
|
snprintf(libdirs + n, libdirsz - n,
|
|
"%s-L%s", n ? " " : "", argv[++i]);
|
|
} else if (strncmp(argv[i], "-L", 2) == 0 && argv[i][2]) {
|
|
size_t n = strlen(libdirs);
|
|
snprintf(libdirs + n, libdirsz - n,
|
|
"%s%s", n ? " " : "", argv[i]);
|
|
} else if (strcmp(argv[i], "-I") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww %s: -I needs an argument\n", cmd);
|
|
return -1;
|
|
}
|
|
size_t n = strlen(incs);
|
|
snprintf(incs + n, incsz - n,
|
|
"%s%s", n ? ":" : "", argv[++i]);
|
|
} else if (strncmp(argv[i], "-I", 2) == 0 && argv[i][2]) {
|
|
size_t n = strlen(incs);
|
|
snprintf(incs + n, incsz - n,
|
|
"%s%s", n ? ":" : "", argv[i] + 2);
|
|
} else if (strcmp(argv[i], "-o") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww %s: -o needs an argument\n", cmd);
|
|
return -1;
|
|
}
|
|
snprintf(outpath, outsz, "%s", argv[++i]);
|
|
} else if (strncmp(argv[i], "-o", 2) == 0 && argv[i][2]) {
|
|
snprintf(outpath, outsz, "%s", argv[i] + 2);
|
|
} else if (*src_out == NULL) {
|
|
*src_out = argv[i];
|
|
} else {
|
|
break; /* leave remaining argv to caller (run-args) */
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
static int
|
|
do_build(int argc, char **argv)
|
|
{
|
|
const char *src = NULL;
|
|
char libs[2048] = {0};
|
|
char libdirs[2048] = {0};
|
|
char incs[2048] = {0};
|
|
char outflag[1024] = {0};
|
|
if (parse_build_flags("build", argc, argv, incs, sizeof incs,
|
|
libdirs, sizeof libdirs, libs, sizeof libs,
|
|
outflag, sizeof outflag, &src) < 0)
|
|
return 2;
|
|
if (src == NULL) src = "."; /* default: build cwd */
|
|
char resolved[1024];
|
|
int is_dir = 0;
|
|
if (!resolve_module(src, incs, resolved, sizeof resolved, &is_dir)) {
|
|
fprintf(stderr, "ww build: cannot find module %s\n", src);
|
|
return 1;
|
|
}
|
|
char out[1024];
|
|
const char *objstem = NULL;
|
|
if (outflag[0]) {
|
|
/* -o sets both the binary path and the intermediate stem so
|
|
* artifacts land beside the requested output (T3). */
|
|
snprintf(out, sizeof out, "%s", outflag);
|
|
objstem = out;
|
|
} else if (is_dir) {
|
|
char tmp[1024];
|
|
snprintf(tmp, sizeof tmp, "%s", resolved);
|
|
size_t n = strlen(tmp);
|
|
while (n > 1 && tmp[n-1] == '/') tmp[--n] = '\0';
|
|
const char *b = strrchr(tmp, '/');
|
|
snprintf(out, sizeof out, "%s", b ? b + 1 : tmp);
|
|
} else {
|
|
basename_no_ext(resolved, out, sizeof out);
|
|
}
|
|
return build_one_sep(resolved, is_dir, out, objstem, incs, libs,
|
|
libdirs, 0);
|
|
}
|
|
|
|
static int
|
|
do_run(int argc, char **argv)
|
|
{
|
|
const char *src = NULL;
|
|
char libs[2048] = {0};
|
|
char libdirs[2048] = {0};
|
|
char incs[2048] = {0};
|
|
char outflag[1024] = {0}; /* -o accepted+ignored: run always uses the temp */
|
|
int next = parse_build_flags("run", argc, argv, incs, sizeof incs,
|
|
libdirs, sizeof libdirs, libs, sizeof libs,
|
|
outflag, sizeof outflag, &src);
|
|
if (next < 0) return 2;
|
|
if (src == NULL) src = ".";
|
|
char resolved[1024];
|
|
int is_dir = 0;
|
|
if (!resolve_module(src, incs, resolved, sizeof resolved, &is_dir)) {
|
|
fprintf(stderr, "ww run: cannot find module %s\n", src);
|
|
return 1;
|
|
}
|
|
char tmp[1024];
|
|
snprintf(tmp, sizeof tmp, "/tmp/ww_run_%d", getpid());
|
|
/* objstem = tmp → intermediates land at /tmp/ww_run_<pid>.{s,o,
|
|
* combined.ww}, never next to the source (T3). */
|
|
if (build_one_sep(resolved, is_dir, tmp, tmp, incs, libs, libdirs, 0) != 0)
|
|
return 1;
|
|
/* exec the built binary with any trailing argv as its argv. */
|
|
pid_t pid = fork();
|
|
if (pid < 0) { perror("ww: fork"); unlink(tmp); return 1; }
|
|
if (pid == 0) {
|
|
int n_extra = argc - next;
|
|
char **xargv = calloc((size_t)n_extra + 2, sizeof *xargv);
|
|
xargv[0] = tmp;
|
|
for (int i = 0; i < n_extra; i++) xargv[i+1] = argv[next + i];
|
|
xargv[n_extra+1] = NULL;
|
|
execv(tmp, xargv);
|
|
perror("ww: exec");
|
|
_exit(127);
|
|
}
|
|
int status = 0;
|
|
waitpid(pid, &status, 0);
|
|
unlink(tmp);
|
|
if (WIFEXITED(status)) return WEXITSTATUS(status);
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
do_test(int argc, char **argv)
|
|
{
|
|
const char *src = NULL;
|
|
char incs[2048] = {0};
|
|
/* -c (compile-only, Go's `go test -c`) + -o <stem> build the test
|
|
* binary (and its lib/test-inclusive combined, via build_one's
|
|
* is_test auto-bundle + the T3 objstem redirect) WITHOUT running it —
|
|
* the byte-id gates feed <stem>.combined.ww to raw w6c -T / w6c_ww -T.
|
|
* -T stays internal to w6c; the driver never sees it. -l/-L carry no
|
|
* meaning for a test build, so they (and any unknown flag) are rejected
|
|
* rather than silently swallowed — byte-identical wording to the
|
|
* wwstage twin (selfhost/cmd/ww/main.ww dotest). */
|
|
int compileonly = 0;
|
|
char outstem[1024] = {0};
|
|
/* #17: an optional second positional after the target is a fnmatch
|
|
* name-filter pattern, forwarded to the test binary as argv[1]. Only
|
|
* meaningful for a single test file/module — rejected in dir mode. */
|
|
const char *pattern = NULL;
|
|
for (int i = 0; i < argc; i++) {
|
|
if (argv[i][0] == '-') {
|
|
if (strcmp(argv[i], "--sep") == 0) {
|
|
/* E3-C1 flip: sep is the sole path; --sep is an
|
|
* accepted no-op (task #87). */
|
|
continue;
|
|
}
|
|
if (argv[i][1] == 'I') {
|
|
const char *dir;
|
|
if (argv[i][2]) {
|
|
dir = argv[i] + 2;
|
|
} else {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww test: -I needs an argument\n");
|
|
return 2;
|
|
}
|
|
dir = argv[++i];
|
|
}
|
|
size_t n = strlen(incs);
|
|
snprintf(incs + n, sizeof incs - n,
|
|
"%s%s", n ? ":" : "", dir);
|
|
} else if (strcmp(argv[i], "-c") == 0) {
|
|
compileonly = 1;
|
|
} else if (strcmp(argv[i], "-o") == 0) {
|
|
if (i + 1 >= argc) {
|
|
fprintf(stderr,
|
|
"ww test: -o needs an argument\n");
|
|
return 2;
|
|
}
|
|
snprintf(outstem, sizeof outstem, "%s", argv[++i]);
|
|
} else if (argv[i][1] == 'o' && argv[i][2]) {
|
|
snprintf(outstem, sizeof outstem, "%s", argv[i] + 2);
|
|
} else {
|
|
fprintf(stderr, "ww test: unknown flag\n");
|
|
return 2;
|
|
}
|
|
} else if (src == NULL) {
|
|
src = argv[i];
|
|
} else if (pattern == NULL) {
|
|
pattern = argv[i];
|
|
}
|
|
}
|
|
const char *target = src ? src : ".";
|
|
struct stat st;
|
|
if (stat(target, &st) != 0) {
|
|
/* not a literal path — try module resolution and run as
|
|
* a single test program. */
|
|
char resolved[1024];
|
|
int is_dir = 0;
|
|
if (!resolve_module(target, incs, resolved, sizeof resolved,
|
|
&is_dir)) {
|
|
fprintf(stderr, "ww test: cannot find %s\n", target);
|
|
return 1;
|
|
}
|
|
char tmp[1024];
|
|
const char *outp;
|
|
if (outstem[0]) outp = outstem;
|
|
else { snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d", getpid()); outp = tmp; }
|
|
int br = build_one_sep(resolved, is_dir, outp,
|
|
outstem[0] ? outstem : NULL, incs, "", "", 1);
|
|
if (br != 0) return 1;
|
|
if (compileonly) return 0;
|
|
int rc = run_test_bin(outp, pattern);
|
|
if (!outstem[0]) unlink(outp);
|
|
return rc;
|
|
}
|
|
if (S_ISREG(st.st_mode)) {
|
|
/* single .ww file — build, then run unless -c (compile-only). */
|
|
char tmp[1024];
|
|
const char *outp;
|
|
if (outstem[0]) outp = outstem;
|
|
else { snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d", getpid()); outp = tmp; }
|
|
int br = build_one_sep(target, 0, outp, outstem[0] ? outstem : NULL,
|
|
incs, "", "", 1);
|
|
if (br != 0) return 1;
|
|
if (compileonly) return 0;
|
|
int rc = run_test_bin(outp, pattern);
|
|
if (!outstem[0]) unlink(outp);
|
|
return rc;
|
|
}
|
|
if (!S_ISDIR(st.st_mode)) {
|
|
fprintf(stderr, "ww test: %s is neither file nor directory\n", target);
|
|
return 1;
|
|
}
|
|
if (compileonly || outstem[0]) {
|
|
fprintf(stderr, "ww test: -c/-o need a single test file\n");
|
|
return 2;
|
|
}
|
|
/* #17: a name-filter pattern is per-binary; directory mode builds one
|
|
* binary per *_test.ww, so a single pattern can't sensibly route. */
|
|
if (pattern) {
|
|
fprintf(stderr, "ww test: pattern needs a single test file\n");
|
|
return 2;
|
|
}
|
|
/* directory — run every *_test.ww inside. */
|
|
char **files = NULL;
|
|
int n = 0;
|
|
if (collect_tests(target, &files, &n) < 0) {
|
|
fprintf(stderr, "ww test: cannot read directory %s\n", target);
|
|
return 1;
|
|
}
|
|
if (n == 0) {
|
|
fprintf(stderr, "ww test: no *_test.ww files in %s\n", target);
|
|
return 1;
|
|
}
|
|
int pass = 0, fail = 0;
|
|
for (int i = 0; i < n; i++) {
|
|
char tmp[1024];
|
|
snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d_%d", getpid(), i);
|
|
const char *label = strrchr(files[i], '/');
|
|
label = label ? label + 1 : files[i];
|
|
int rc = build_one_sep(files[i], 0, tmp, NULL, target, "", "", 1);
|
|
if (rc != 0) {
|
|
fprintf(stderr, "FAIL %s (build)\n", label);
|
|
fail++;
|
|
} else {
|
|
int xrc = run(tmp);
|
|
if (xrc == 0) {
|
|
printf("ok %s\n", label);
|
|
pass++;
|
|
} else {
|
|
printf("FAIL %s (rc=%d)\n", label, xrc);
|
|
fail++;
|
|
}
|
|
}
|
|
unlink(tmp);
|
|
free(files[i]);
|
|
}
|
|
free(files);
|
|
printf("ww test: %d pass, %d fail\n", pass, fail);
|
|
return fail == 0 ? 0 : 1;
|
|
}
|
|
|
|
static int
|
|
do_fmt(int argc, char **argv)
|
|
{
|
|
(void)argc; (void)argv;
|
|
fputs("ww: fmt: not implemented in this phase\n", stderr);
|
|
return 1;
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
if (argc >= 1) {
|
|
char buf[1024];
|
|
snprintf(buf, sizeof buf, "%s", argv[0]);
|
|
self_dir = strdup(dirname(buf));
|
|
}
|
|
if (argc < 2) { fputs(usage, stderr); return 2; }
|
|
const char *cmd = argv[1];
|
|
if (strcmp(cmd, "-V") == 0 || strcmp(cmd, "version") == 0)
|
|
return do_version();
|
|
if (strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0) {
|
|
fputs(usage, stdout); return 0;
|
|
}
|
|
if (strcmp(cmd, "build") == 0) return do_build(argc - 2, argv + 2);
|
|
if (strcmp(cmd, "run") == 0) return do_run(argc - 2, argv + 2);
|
|
if (strcmp(cmd, "test") == 0) return do_test(argc - 2, argv + 2);
|
|
if (strcmp(cmd, "fmt") == 0) return do_fmt(argc - 2, argv + 2);
|
|
fprintf(stderr, "ww: unknown subcommand: %s\n", cmd);
|
|
fputs(usage, stderr);
|
|
return 2;
|
|
}
|