cmd: share package builds across test products
This commit is contained in:
428
cmd/ww/main.c
428
cmd/ww/main.c
@@ -300,6 +300,7 @@ source_has_test_decl(const char *path)
|
||||
#define SEP_VARIANT_SAME_TEST 1
|
||||
#define SEP_VARIANT_EXTERNAL 2
|
||||
#define SEP_TEST_SUPPORT_MODULE "__wwtest"
|
||||
#define SEP_MAXPRODUCT 2
|
||||
|
||||
/* Test-file package classification uses the compiler's imports-only parser.
|
||||
* The coordinator chooses variants, but the command owns which real source
|
||||
@@ -495,6 +496,8 @@ struct seppkg {
|
||||
int nsources;
|
||||
int is_dir;
|
||||
int variant; /* SEP_VARIANT_*; dependencies are production */
|
||||
int root; /* independently compiled/linkable product root */
|
||||
int failed; /* discovery/compile failure reaches this action */
|
||||
int test_support; /* compiler-generated -T support package */
|
||||
int deps[SEP_MAXPKG]; /* direct-dep indices into sepgraph.pkg */
|
||||
int ndeps;
|
||||
@@ -506,9 +509,18 @@ struct sepgraph {
|
||||
int n;
|
||||
};
|
||||
|
||||
struct sepproduct {
|
||||
const char *out;
|
||||
const char *test_package;
|
||||
const char *status;
|
||||
int variant;
|
||||
int root;
|
||||
};
|
||||
|
||||
static int
|
||||
sep_find_or_add_variant(struct sepgraph *g, const char *path,
|
||||
const char *entry, int is_dir, int variant, const char *test_package)
|
||||
const char *entry, int is_dir, int variant, const char *test_package,
|
||||
int root)
|
||||
{
|
||||
if (strlen(path) >= sizeof g->pkg[0].path) {
|
||||
fprintf(stderr, "ww: package path is too long (limit %zu bytes)\n",
|
||||
@@ -526,13 +538,18 @@ sep_find_or_add_variant(struct sepgraph *g, const char *path,
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < g->n; i++) {
|
||||
int test_production_pair = i == 0
|
||||
&& g->pkg[i].variant != SEP_VARIANT_PRODUCTION
|
||||
&& variant == SEP_VARIANT_PRODUCTION && is_dir
|
||||
&& g->pkg[i].is_dir
|
||||
&& strcmp(g->pkg[i].canon, canon) == 0;
|
||||
int same_location = strcmp(g->pkg[i].canon, canon) == 0;
|
||||
int test_root_pair = same_location && is_dir && g->pkg[i].is_dir
|
||||
&& ((root && g->pkg[i].root
|
||||
&& variant != g->pkg[i].variant)
|
||||
|| (root && !g->pkg[i].root
|
||||
&& variant != SEP_VARIANT_PRODUCTION
|
||||
&& g->pkg[i].variant == SEP_VARIANT_PRODUCTION)
|
||||
|| (!root && g->pkg[i].root
|
||||
&& variant == SEP_VARIANT_PRODUCTION
|
||||
&& g->pkg[i].variant != SEP_VARIANT_PRODUCTION));
|
||||
if (strcmp(g->pkg[i].path, path) == 0) {
|
||||
if (test_production_pair)
|
||||
if (test_root_pair)
|
||||
continue;
|
||||
if (strcmp(g->pkg[i].canon, canon) != 0
|
||||
|| g->pkg[i].variant != variant) {
|
||||
@@ -546,11 +563,12 @@ sep_find_or_add_variant(struct sepgraph *g, const char *path,
|
||||
free(canon);
|
||||
return i;
|
||||
}
|
||||
if (strcmp(g->pkg[i].canon, canon) == 0) {
|
||||
if (same_location) {
|
||||
/* A test root and a production variant reached by its imports or
|
||||
* runtime closure intentionally may share one directory. No other
|
||||
* physical-directory alias is permitted. */
|
||||
if (test_production_pair)
|
||||
* runtime closure, and the selected test roots themselves,
|
||||
* intentionally may share one directory. No other physical-directory
|
||||
* alias is permitted. */
|
||||
if (test_root_pair)
|
||||
continue;
|
||||
fprintf(stderr,
|
||||
"ww: package directory %s has identities %s and %s\n",
|
||||
@@ -573,6 +591,8 @@ sep_find_or_add_variant(struct sepgraph *g, const char *path,
|
||||
free(canon);
|
||||
p->is_dir = is_dir;
|
||||
p->variant = variant;
|
||||
p->root = root;
|
||||
p->failed = 0;
|
||||
p->test_support = 0;
|
||||
p->name[0] = '\0';
|
||||
p->test_package[0] = '\0';
|
||||
@@ -591,7 +611,7 @@ sep_find_or_add(struct sepgraph *g, const char *path, const char *entry,
|
||||
int is_dir)
|
||||
{
|
||||
return sep_find_or_add_variant(g, path, entry, is_dir,
|
||||
SEP_VARIANT_PRODUCTION, NULL);
|
||||
SEP_VARIANT_PRODUCTION, NULL, 0);
|
||||
}
|
||||
|
||||
/* Release the one package-owned directory-membership list. Every graph exit
|
||||
@@ -608,12 +628,23 @@ sep_graph_free(struct sepgraph *g)
|
||||
free(g);
|
||||
}
|
||||
|
||||
/* Dots stay (legal in filenames); the root's empty path becomes "__root". */
|
||||
/* Dots stay (legal in filenames). Product roots have distinct artifact names
|
||||
* even though each compiler unit resets to the bare executable namespace. */
|
||||
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";
|
||||
const char *base = g->pkg[pi].path;
|
||||
if (g->pkg[pi].root) {
|
||||
if (g->pkg[pi].variant == SEP_VARIANT_SAME_TEST)
|
||||
base = "__ww-test-same";
|
||||
else if (g->pkg[pi].variant == SEP_VARIANT_EXTERNAL)
|
||||
base = "__ww-test-external";
|
||||
else if (base[0] == '\0')
|
||||
base = "__root";
|
||||
} else if (base[0] == '\0') {
|
||||
base = "__root";
|
||||
}
|
||||
snprintf(out, outsz, "%s/%s%s", scratch, base, suffix);
|
||||
}
|
||||
|
||||
@@ -861,7 +892,7 @@ sep_scan_file(struct sepgraph *g, int pi, const char *file,
|
||||
static int
|
||||
sep_load_pkg(struct sepgraph *g, int pi, const char *searchpath)
|
||||
{
|
||||
if (g->pkg[pi].color == 2) return 0;
|
||||
if (g->pkg[pi].color == 2) return g->pkg[pi].failed ? -1 : 0;
|
||||
g->pkg[pi].color = 2;
|
||||
struct ImportSet fv = {0};
|
||||
int rc = 0;
|
||||
@@ -901,12 +932,15 @@ sep_load_pkg(struct sepgraph *g, int pi, const char *searchpath)
|
||||
}
|
||||
for (int i = 0; i < fv.n; i++) free(fv.paths[i]);
|
||||
free(fv.paths);
|
||||
if (rc < 0) return rc;
|
||||
if (rc < 0) {
|
||||
g->pkg[pi].failed = 1;
|
||||
return rc;
|
||||
}
|
||||
/* Give a non-main root its declared identity before recursively loading
|
||||
* dependencies. A back-edge can then reuse node 0 and reach the normal
|
||||
* cycle detector instead of looking like a location alias. Executable
|
||||
* roots are reset to the bare-root identity after loading. */
|
||||
if (pi == 0 && g->pkg[pi].path[0] == '\0'
|
||||
if (g->pkg[pi].root && g->pkg[pi].path[0] == '\0'
|
||||
&& g->pkg[pi].name[0] != '\0') {
|
||||
size_t n = strlen(g->pkg[pi].name);
|
||||
memcpy(g->pkg[pi].path, g->pkg[pi].name, n + 1);
|
||||
@@ -924,8 +958,10 @@ sep_load_pkg(struct sepgraph *g, int pi, const char *searchpath)
|
||||
/* 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_load_pkg(g, g->pkg[pi].deps[k], searchpath) < 0)
|
||||
if (sep_load_pkg(g, g->pkg[pi].deps[k], searchpath) < 0) {
|
||||
g->pkg[pi].failed = 1;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1239,20 +1275,38 @@ copy_file_atomic(const char *src, const char *dst)
|
||||
return rename(tmp, dst);
|
||||
}
|
||||
|
||||
/* A coordinator-private completion marker distinguishes a newly linked
|
||||
* product from a caller-owned binary left behind by an earlier invocation. */
|
||||
static int
|
||||
record_product_status(const char *path)
|
||||
{
|
||||
if (path == NULL) return 0;
|
||||
char tmp[1100];
|
||||
snprintf(tmp, sizeof tmp, "%s.new", path);
|
||||
FILE *f = fopen(tmp, "wb");
|
||||
if (f == NULL) return -1;
|
||||
int bad = fputs("ok\n", f) == EOF;
|
||||
if (fclose(f) != 0) bad = 1;
|
||||
if (bad) return -1;
|
||||
return rename(tmp, path);
|
||||
}
|
||||
|
||||
/* The stamp pins the non-content build inputs a unit compare cannot see:
|
||||
* the -T/-S shape of the producer pass and the artifact protocol
|
||||
* revision (bump "fmt" when the unit/archive/commit format changes). */
|
||||
static void
|
||||
workdir_stamp_text(char *buf, size_t bufsz, int is_test, int emit_asm)
|
||||
{
|
||||
snprintf(buf, bufsz, "ww workdir fmt 3 mode %s asm %d\n",
|
||||
snprintf(buf, bufsz, "ww workdir fmt 4 mode %s asm %d\n",
|
||||
is_test ? "test" : "build", emit_asm);
|
||||
}
|
||||
|
||||
/* build_one_sep — discover_deps, reverse_topo,
|
||||
* the transitive producer loop (one `w6c -c -I` per package, dep-first,
|
||||
/* build_sep_plan — discover dependencies for every requested product in one
|
||||
* package universe, compile the dependency-first union once, then link each
|
||||
* root from its own complete reachable archive closure. 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
|
||||
* reverse-topo `w6l` of each root `.o` + dep `.a` set + libwwrt.a. Side
|
||||
* files land in a cold `<stem>.sepwork` dir, or under the persistent
|
||||
* `-w` workdir with content-identity package reuse. */
|
||||
static int
|
||||
@@ -1260,10 +1314,15 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
const char *root_identity, const char *out,
|
||||
const char *objstem, const char *extra_includes, const char *extra_libs,
|
||||
const char *extra_libdirs, int package_only, int is_test,
|
||||
int root_variant, const char *test_package, int emit_asm,
|
||||
struct sepproduct *products, int nproducts, int emit_asm,
|
||||
const char *workdir, char *scratchout, size_t scratchoutsz,
|
||||
struct sepgraph **graphout)
|
||||
{
|
||||
if (nproducts < 1 || nproducts > SEP_MAXPRODUCT) return 1;
|
||||
for (int i = 0; i < nproducts; i++)
|
||||
if (products[i].status != NULL
|
||||
&& unlink(products[i].status) != 0 && errno != ENOENT)
|
||||
return 1;
|
||||
const char *c6 = toolpath("WW_W6C", "w6c");
|
||||
const char *a6 = toolpath("WW_W6A", "w6a");
|
||||
const char *l6 = toolpath("WW_W6L", "w6l");
|
||||
@@ -1365,9 +1424,11 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
if (graphout) *graphout = g;
|
||||
const char *rootpath = package_only && root_identity
|
||||
? root_identity : "";
|
||||
int root = sep_find_or_add_variant(g, rootpath, src, entry_is_dir,
|
||||
root_variant, test_package);
|
||||
if (root < 0) return 1;
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
products[i].root = sep_find_or_add_variant(g, rootpath, src,
|
||||
entry_is_dir, products[i].variant, products[i].test_package, 1);
|
||||
if (products[i].root < 0) return 1;
|
||||
}
|
||||
const char *test_support_module = "test";
|
||||
/* -T generates a dispatcher whose support qualifier is selected by the
|
||||
* command. Represent that compiler-generated requirement as a direct root
|
||||
@@ -1384,9 +1445,13 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
&& strcmp(tc, rc) == 0;
|
||||
free(tc);
|
||||
free(rc);
|
||||
int collision = !root_is_support && test_package != NULL
|
||||
&& (strcmp(test_package, "test") == 0
|
||||
|| strcmp(test_package, "test_test") == 0);
|
||||
int collision = 0;
|
||||
for (int i = 0; !root_is_support && i < nproducts; i++) {
|
||||
const char *name = products[i].test_package;
|
||||
if (name != NULL && (strcmp(name, "test") == 0
|
||||
|| strcmp(name, "test_test") == 0))
|
||||
collision = 1;
|
||||
}
|
||||
char userpath[1024];
|
||||
int userdir = 0;
|
||||
if (!root_is_support && !collision
|
||||
@@ -1401,10 +1466,14 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
free(uc);
|
||||
}
|
||||
if (collision) test_support_module = SEP_TEST_SUPPORT_MODULE;
|
||||
/* A same-test build of the runtime package already owns run
|
||||
* and its source imports. An external test still needs the
|
||||
* colocated production node, which is also its one support dep. */
|
||||
if (!root_is_support || root_variant == SEP_VARIANT_EXTERNAL) {
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
int root = products[i].root;
|
||||
/* A same-test build of the runtime package already owns run
|
||||
* and its source imports. An external test still needs the
|
||||
* colocated production node, which is also its support dep. */
|
||||
if (root_is_support
|
||||
&& products[i].variant != SEP_VARIANT_EXTERNAL)
|
||||
continue;
|
||||
int ti = sep_find_or_add(g, test_support_module, tpath,
|
||||
tdir);
|
||||
if (ti < 0) return 1;
|
||||
@@ -1419,32 +1488,69 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sep_load_pkg(g, root, srcdir) < 0) return 1;
|
||||
if (root_variant != SEP_VARIANT_PRODUCTION
|
||||
&& (test_package == NULL
|
||||
|| strcmp(g->pkg[root].name, test_package) != 0)) {
|
||||
fprintf(stderr,
|
||||
"ww: package-test selector does not match loaded package\n");
|
||||
return 1;
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
int root = products[i].root;
|
||||
if (sep_load_pkg(g, root, srcdir) < 0) {
|
||||
g->pkg[root].failed = 1;
|
||||
continue;
|
||||
}
|
||||
if (products[i].variant != SEP_VARIANT_PRODUCTION
|
||||
&& (products[i].test_package == NULL
|
||||
|| strcmp(g->pkg[root].name,
|
||||
products[i].test_package) != 0)) {
|
||||
fprintf(stderr,
|
||||
"ww: package-test selector does not match loaded package\n");
|
||||
g->pkg[root].failed = 1;
|
||||
}
|
||||
}
|
||||
int root_package = package_only;
|
||||
if (root_package && strcmp(g->pkg[root].name, "main") == 0) {
|
||||
if (root_package && !g->pkg[products[0].root].failed
|
||||
&& strcmp(g->pkg[products[0].root].name, "main") == 0) {
|
||||
fprintf(stderr, "ww: -p requires a non-main package\n");
|
||||
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) {
|
||||
if (order == NULL || stack == NULL) {
|
||||
free(stack); free(order); return 1;
|
||||
}
|
||||
/* Diagnose cycles per product before constructing the shared union. A
|
||||
* variant-local cycle must not suppress an independent sibling root. */
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
int root = products[i].root;
|
||||
if (g->pkg[root].failed) continue;
|
||||
for (int pi = 0; pi < g->n; pi++) g->pkg[pi].color = 0;
|
||||
int ignored = 0;
|
||||
if (sep_topo_visit(g, root, order, &ignored, stack, 0) < 0)
|
||||
g->pkg[root].failed = 1;
|
||||
}
|
||||
for (int pi = 0; pi < g->n; pi++) g->pkg[pi].color = 0;
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
int root = products[i].root;
|
||||
if (!g->pkg[root].failed
|
||||
&& sep_topo_visit(g, root, order, &norder, stack, 0) < 0) {
|
||||
free(stack); free(order); return 1;
|
||||
}
|
||||
}
|
||||
free(stack);
|
||||
if (!root_package) g->pkg[root].path[0] = '\0';
|
||||
if (!root_package)
|
||||
for (int i = 0; i < nproducts; i++)
|
||||
if (g->pkg[products[i].root].path[0] != '\0')
|
||||
g->pkg[products[i].root].path[0] = '\0';
|
||||
|
||||
int any_failed = 0;
|
||||
for (int i = 0; i < nproducts; i++)
|
||||
if (g->pkg[products[i].root].failed) any_failed = 1;
|
||||
for (int oi = 0; oi < norder; oi++) {
|
||||
int pi = order[oi];
|
||||
for (int k = 0; k < g->pkg[pi].ndeps; k++)
|
||||
if (g->pkg[g->pkg[pi].deps[k]].failed)
|
||||
g->pkg[pi].failed = 1;
|
||||
if (g->pkg[pi].failed) {
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
char unitf[1024], wwi[1024], asmf[1024], obj[1024], apath[1024];
|
||||
char unitnew[1024], wwinew[1024], asmnew[1024], objnew[1024];
|
||||
char anew[1024], cmd[8192];
|
||||
@@ -1465,10 +1571,13 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
const char *cs = warm ? asmnew : asmf;
|
||||
const char *co = warm ? objnew : obj;
|
||||
const char *ca = warm ? anew : apath;
|
||||
int needs_export = pi != root || root_package;
|
||||
int needs_archive = pi != root || root_package;
|
||||
if (sep_compose_unit(g, pi, scratch, srcdir,
|
||||
cu) < 0) { free(order); return 1; }
|
||||
int needs_export = !g->pkg[pi].root || root_package;
|
||||
int needs_archive = !g->pkg[pi].root || root_package;
|
||||
if (sep_compose_unit(g, pi, scratch, srcdir, cu) < 0) {
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
if (warm && !stale_all && file_equal(unitnew, unitf)
|
||||
&& file_is_reg(asmf)
|
||||
&& (!needs_export || file_is_reg(wwi))
|
||||
@@ -1477,7 +1586,8 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
if (unlink(unitnew) != 0) {
|
||||
fprintf(stderr, "ww: cannot remove %s\n",
|
||||
unitnew);
|
||||
free(order); return 1;
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -1492,7 +1602,7 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
/* #79: the root carries -T under `ww test`
|
||||
* so w6c synthesizes the test main. Deps never
|
||||
* get -T. */
|
||||
if (is_test)
|
||||
if (is_test && g->pkg[pi].root)
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s -T --test-support-module %s -c -o %s %s",
|
||||
c6, test_support_module, cs, cu);
|
||||
@@ -1509,14 +1619,18 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
if (run(cmd) != 0) {
|
||||
fprintf(stderr, "ww: w6c failed for %s\n",
|
||||
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
||||
free(order); return 1;
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
if (!emit_asm) {
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s", a6, co, cs);
|
||||
if (run(cmd) != 0) {
|
||||
fprintf(stderr, "ww: w6a failed for %s\n",
|
||||
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
||||
free(order); return 1;
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* wrap each DEP package's `.o` in its own deterministic `.a`
|
||||
@@ -1528,7 +1642,9 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
if (archive_o(co, ca) != 0) {
|
||||
fprintf(stderr, "ww: archive failed for %s\n",
|
||||
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
||||
free(order); return 1;
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* Commit order: artifacts before the unit that vouches for
|
||||
@@ -1542,14 +1658,16 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
|| rename(unitnew, unitf) != 0) {
|
||||
fprintf(stderr, "ww: cannot commit %s\n",
|
||||
g->pkg[pi].path[0] ? g->pkg[pi].path : "(root)");
|
||||
free(order); return 1;
|
||||
g->pkg[pi].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Tool identity commits only after every package artifact it vouches
|
||||
* for is itself committed; a killed pass leaves the old identity and
|
||||
* forces a full recompile, never a false reuse. */
|
||||
if (warm) {
|
||||
if (warm && !any_failed) {
|
||||
if (!file_equal(toolc, c6)
|
||||
&& copy_file_atomic(c6, toolc) != 0) {
|
||||
fprintf(stderr, "ww: cannot record %s\n", toolc);
|
||||
@@ -1573,8 +1691,10 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
if (emit_asm) { free(order); return 0; }
|
||||
if (emit_asm) { free(order); return any_failed ? 1 : 0; }
|
||||
if (root_package) {
|
||||
int root = products[0].root;
|
||||
if (g->pkg[root].failed) { free(order); return 1; }
|
||||
char archive[1024], iface[1024], outiface[1100];
|
||||
sep_fname(g, root, scratch, ".a", archive, sizeof archive);
|
||||
sep_fname(g, root, scratch, ".wwi", iface, sizeof iface);
|
||||
@@ -1589,12 +1709,11 @@ build_one_sep_impl(const char *src, int entry_is_dir,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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.
|
||||
* A same-test root already contains its production sources; if that
|
||||
* production variant is also reached through the runtime closure, keep
|
||||
* traversing its dependencies but do not link its duplicate archive. */
|
||||
free(order);
|
||||
/* Each product gets its own reverse-topological closure: root `.o` first,
|
||||
* then every transitively reachable dependency `.a`, then libwwrt.a. A
|
||||
* same-test root already contains its production sources, so its colocated
|
||||
* production archive is omitted without dropping that node's dependencies. */
|
||||
char rtargs[2048] = {0};
|
||||
char rtpath[1024];
|
||||
snprintf(rtpath, sizeof rtpath, "%s/libwwrt.a", libdir);
|
||||
@@ -1606,31 +1725,55 @@ build_one_sep_impl(const char *src, int entry_is_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--) {
|
||||
int pi = order[oi];
|
||||
if (g->pkg[root].variant == SEP_VARIANT_SAME_TEST
|
||||
&& pi != root
|
||||
&& g->pkg[pi].variant == SEP_VARIANT_PRODUCTION
|
||||
&& strcmp(g->pkg[pi].canon, g->pkg[root].canon) == 0)
|
||||
continue;
|
||||
char path[1024];
|
||||
sep_fname(g, pi, scratch,
|
||||
pi == 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);
|
||||
if (rc != 0) { fprintf(stderr, "ww: w6l failed\n"); return 1; }
|
||||
return 0;
|
||||
for (int i = 0; i < nproducts; i++) {
|
||||
int root = products[i].root;
|
||||
if (g->pkg[root].failed) { any_failed = 1; continue; }
|
||||
for (int pi = 0; pi < g->n; pi++) g->pkg[pi].color = 0;
|
||||
int *linkorder = calloc((size_t)g->n, sizeof *linkorder);
|
||||
int *linkstack = calloc((size_t)g->n, sizeof *linkstack);
|
||||
int nlink = 0;
|
||||
if (linkorder == NULL || linkstack == NULL
|
||||
|| sep_topo_visit(g, root, linkorder, &nlink,
|
||||
linkstack, 0) < 0) {
|
||||
free(linkstack); free(linkorder); return 1;
|
||||
}
|
||||
free(linkstack);
|
||||
char objs[8192] = {0};
|
||||
for (int oi = nlink - 1; oi >= 0; oi--) {
|
||||
int pi = linkorder[oi];
|
||||
if (g->pkg[root].variant == SEP_VARIANT_SAME_TEST
|
||||
&& pi != root
|
||||
&& g->pkg[pi].variant == SEP_VARIANT_PRODUCTION
|
||||
&& strcmp(g->pkg[pi].canon, g->pkg[root].canon) == 0)
|
||||
continue;
|
||||
char path[1024];
|
||||
sep_fname(g, pi, scratch,
|
||||
pi == root ? ".o" : ".a", path, sizeof path);
|
||||
size_t n = strlen(objs);
|
||||
snprintf(objs + n, sizeof objs - n, "%s%s",
|
||||
n ? " " : "", path);
|
||||
}
|
||||
free(linkorder);
|
||||
char cmd[16384];
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s %s%s%s%s%s",
|
||||
l6, products[i].out, objs, rtargs,
|
||||
libdirset[0] ? " " : "", libdirset,
|
||||
libargs[0] ? " " : "", libargs);
|
||||
if (run(cmd) != 0) {
|
||||
fprintf(stderr, "ww: w6l failed\n");
|
||||
g->pkg[root].failed = 1;
|
||||
any_failed = 1;
|
||||
continue;
|
||||
}
|
||||
if (record_product_status(products[i].status) != 0) {
|
||||
fprintf(stderr, "ww: cannot record package-test product\n");
|
||||
g->pkg[root].failed = 1;
|
||||
any_failed = 1;
|
||||
}
|
||||
}
|
||||
return any_failed ? 1 : 0;
|
||||
}
|
||||
|
||||
/* build_one_sep — thin wrapper over build_one_sep_impl. `ww build` and an
|
||||
@@ -1649,9 +1792,16 @@ build_one_sep(const char *src, int entry_is_dir, const char *root_identity,
|
||||
{
|
||||
char scratch[1100] = {0};
|
||||
struct sepgraph *g = NULL;
|
||||
struct sepproduct product = {
|
||||
.out = out,
|
||||
.test_package = test_package,
|
||||
.status = NULL,
|
||||
.variant = root_variant,
|
||||
.root = -1,
|
||||
};
|
||||
int r = build_one_sep_impl(src, entry_is_dir, root_identity, out, objstem,
|
||||
extra_includes, extra_libs, extra_libdirs, package_only, is_test,
|
||||
root_variant, test_package, emit_asm, workdir, scratch,
|
||||
&product, 1, emit_asm, workdir, scratch,
|
||||
sizeof scratch, &g);
|
||||
sep_graph_free(g);
|
||||
if (!keepscratch && scratch[0]) {
|
||||
@@ -1679,6 +1829,22 @@ build_one_sep(const char *src, int entry_is_dir, const char *root_identity,
|
||||
return r;
|
||||
}
|
||||
|
||||
/* The package coordinator submits all selected roots for one directory in one
|
||||
* request. Its first output owns the shared cold sepwork tree; every product
|
||||
* remains an independent root compile and link inside that tree. */
|
||||
static int
|
||||
build_package_tests(const char *src, const char *extra_includes,
|
||||
const char *workdir, struct sepproduct *products, int nproducts)
|
||||
{
|
||||
char scratch[1100] = {0};
|
||||
struct sepgraph *g = NULL;
|
||||
int r = build_one_sep_impl(src, 1, NULL, products[0].out,
|
||||
products[0].out, extra_includes, "", "", 0, 1,
|
||||
products, nproducts, 0, workdir, scratch, sizeof scratch, &g);
|
||||
sep_graph_free(g);
|
||||
return r;
|
||||
}
|
||||
|
||||
static int
|
||||
do_version(void)
|
||||
{
|
||||
@@ -1985,8 +2151,8 @@ static int
|
||||
do_test(int argc, char **argv)
|
||||
{
|
||||
const char *src = NULL;
|
||||
const char *package_test_kind = NULL;
|
||||
const char *package_test_name = NULL;
|
||||
struct sepproduct products[SEP_MAXPRODUCT];
|
||||
int nproducts = 0;
|
||||
char incs[2048] = {0};
|
||||
/* -c (Go's `go test -c`) builds the test binary without running it.
|
||||
* -S + -o <stem> stops after the lib/test-inclusive package `.s`
|
||||
@@ -2032,25 +2198,42 @@ do_test(int argc, char **argv)
|
||||
} else if (strcmp(argv[i], "-c") == 0) {
|
||||
compileonly = 1;
|
||||
} else if (strcmp(argv[i], "--ww-package-test") == 0) {
|
||||
if (i + 2 >= argc || package_test_kind != NULL) {
|
||||
if (i + 4 >= argc || nproducts >= SEP_MAXPRODUCT) {
|
||||
fprintf(stderr,
|
||||
"ww test: --ww-package-test needs kind and package\n");
|
||||
"ww test: --ww-package-test needs kind, package, output, and status\n");
|
||||
return 2;
|
||||
}
|
||||
package_test_kind = argv[++i];
|
||||
package_test_name = argv[++i];
|
||||
size_t pn = strlen(package_test_name);
|
||||
if ((strcmp(package_test_kind, "same") != 0
|
||||
&& strcmp(package_test_kind, "external") != 0)
|
||||
const char *kind = argv[++i];
|
||||
const char *name = argv[++i];
|
||||
const char *output = argv[++i];
|
||||
const char *status = argv[++i];
|
||||
size_t pn = strlen(name);
|
||||
int variant = strcmp(kind, "same") == 0
|
||||
? SEP_VARIANT_SAME_TEST : SEP_VARIANT_EXTERNAL;
|
||||
if ((strcmp(kind, "same") != 0
|
||||
&& strcmp(kind, "external") != 0)
|
||||
|| pn == 0 || pn >= sizeof ((struct seppkg *)0)->name
|
||||
|| (strcmp(package_test_kind, "external") == 0
|
||||
|| output[0] == '\0' || status[0] == '\0'
|
||||
|| (strcmp(kind, "external") == 0
|
||||
&& (pn <= 5
|
||||
|| strcmp(package_test_name + pn - 5,
|
||||
|| strcmp(name + pn - 5,
|
||||
"_test") != 0))) {
|
||||
fprintf(stderr,
|
||||
"ww test: invalid --ww-package-test variant\n");
|
||||
return 2;
|
||||
}
|
||||
for (int p = 0; p < nproducts; p++)
|
||||
if (products[p].variant == variant) {
|
||||
fprintf(stderr,
|
||||
"ww test: duplicate --ww-package-test variant\n");
|
||||
return 2;
|
||||
}
|
||||
products[nproducts].out = output;
|
||||
products[nproducts].test_package = name;
|
||||
products[nproducts].status = status;
|
||||
products[nproducts].variant = variant;
|
||||
products[nproducts].root = -1;
|
||||
nproducts++;
|
||||
} else if (strcmp(argv[i], "-S") == 0) {
|
||||
emit_asm = 1;
|
||||
} else if (strcmp(argv[i], "-list") == 0) {
|
||||
@@ -2101,18 +2284,32 @@ do_test(int argc, char **argv)
|
||||
fprintf(stderr, "ww test: -S needs -o\n");
|
||||
return 2;
|
||||
}
|
||||
if (package_test_kind != NULL && packageopts) {
|
||||
for (int i = 1; i < nproducts; i++) {
|
||||
struct sepproduct p = products[i];
|
||||
int j = i;
|
||||
while (j > 0 && products[j - 1].variant > p.variant) {
|
||||
products[j] = products[j - 1];
|
||||
j--;
|
||||
}
|
||||
products[j] = p;
|
||||
}
|
||||
if (nproducts != 0 && packageopts) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant rejects package options\n");
|
||||
return 2;
|
||||
}
|
||||
if (nproducts != 0 && outstem[0]) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test products reject -o\n");
|
||||
return 2;
|
||||
}
|
||||
/* Go's ./... form: a trailing "..." element is a package-tree
|
||||
* request for the coordinator, never a literal path — recognized
|
||||
* before stat, with the directory-mode rejects. */
|
||||
size_t tlen = strlen(target);
|
||||
if (strcmp(target, "...") == 0 ||
|
||||
(tlen >= 4 && strcmp(target + tlen - 4, "/...") == 0)) {
|
||||
if (package_test_kind != NULL) {
|
||||
if (nproducts != 0) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant needs one directory\n");
|
||||
return 2;
|
||||
@@ -2135,7 +2332,7 @@ do_test(int argc, char **argv)
|
||||
return 2;
|
||||
}
|
||||
/* -w forwards: the coordinator keys one persistent driver
|
||||
* workdir per package group under the given root. */
|
||||
* workdir per directory plan under the given root. */
|
||||
return exec_package_tests(argc, argv, src, NULL, 0);
|
||||
}
|
||||
struct stat st;
|
||||
@@ -2165,17 +2362,14 @@ do_test(int argc, char **argv)
|
||||
"ww test: pattern needs a single test file\n");
|
||||
return 2;
|
||||
}
|
||||
if (package_test_kind != NULL) {
|
||||
if (!compileonly || !outstem[0]) {
|
||||
if (nproducts != 0) {
|
||||
if (!compileonly) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant needs -c -o\n");
|
||||
"ww test: package-test products need -c\n");
|
||||
return 2;
|
||||
}
|
||||
int variant = strcmp(package_test_kind, "same") == 0
|
||||
? SEP_VARIANT_SAME_TEST : SEP_VARIANT_EXTERNAL;
|
||||
return build_one_sep(resolved, 1, NULL, outstem, outstem,
|
||||
incs, "", "", 0, 1, variant, package_test_name, 0,
|
||||
1, workdir);
|
||||
return build_package_tests(resolved, incs, workdir,
|
||||
products, nproducts);
|
||||
}
|
||||
return exec_package_tests(argc, argv, src, resolved, 0);
|
||||
}
|
||||
@@ -2184,7 +2378,7 @@ do_test(int argc, char **argv)
|
||||
"ww test: package options need a directory\n");
|
||||
return 2;
|
||||
}
|
||||
if (package_test_kind != NULL) {
|
||||
if (nproducts != 0) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant needs one directory\n");
|
||||
return 2;
|
||||
@@ -2245,7 +2439,7 @@ do_test(int argc, char **argv)
|
||||
return rc;
|
||||
}
|
||||
if (S_ISREG(st.st_mode)) {
|
||||
if (package_test_kind != NULL) {
|
||||
if (nproducts != 0) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant needs one directory\n");
|
||||
return 2;
|
||||
@@ -2324,16 +2518,14 @@ do_test(int argc, char **argv)
|
||||
fprintf(stderr, "ww test: pattern needs a single test file\n");
|
||||
return 2;
|
||||
}
|
||||
if (package_test_kind != NULL) {
|
||||
if (!compileonly || !outstem[0]) {
|
||||
if (nproducts != 0) {
|
||||
if (!compileonly) {
|
||||
fprintf(stderr,
|
||||
"ww test: package-test variant needs -c -o\n");
|
||||
"ww test: package-test products need -c\n");
|
||||
return 2;
|
||||
}
|
||||
int variant = strcmp(package_test_kind, "same") == 0
|
||||
? SEP_VARIANT_SAME_TEST : SEP_VARIANT_EXTERNAL;
|
||||
return build_one_sep(target, 1, NULL, outstem, outstem, incs,
|
||||
"", "", 0, 1, variant, package_test_name, 0, 1, workdir);
|
||||
return build_package_tests(target, incs, workdir,
|
||||
products, nproducts);
|
||||
}
|
||||
return exec_package_tests(argc, argv, src, NULL, src == NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user