ww: implement local vendor package semantics
This commit is contained in:
@@ -41,6 +41,19 @@ struct importin {
|
||||
u64 len;
|
||||
};
|
||||
|
||||
struct importmap {
|
||||
const char *source;
|
||||
const char *path;
|
||||
int seen;
|
||||
};
|
||||
|
||||
static const char *
|
||||
importleaf(const char *path)
|
||||
{
|
||||
const char *dot = strrchr(path, '.');
|
||||
return dot != NULL ? dot + 1 : path;
|
||||
}
|
||||
|
||||
static Node *
|
||||
parseinput(Arena *a, const char *file, char *buf, u64 len,
|
||||
const char *mod, const char *testsupport, int commandpackage, int *bad)
|
||||
@@ -85,11 +98,15 @@ main(int argc, char **argv)
|
||||
* only codegen (emit imported==0 decls
|
||||
* only; treat `.wwi` deps as external) */
|
||||
struct importin *imports = calloc((size_t)argc, sizeof *imports);
|
||||
if (imports == NULL) {
|
||||
struct importmap *maps = calloc((size_t)argc, sizeof *maps);
|
||||
if (imports == NULL || maps == NULL) {
|
||||
fputs("w6c: out of memory\n", stderr);
|
||||
free(maps);
|
||||
free(imports);
|
||||
return 1;
|
||||
}
|
||||
int nimports = 0;
|
||||
int nmaps = 0;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
const char *a = argv[i];
|
||||
if (strcmp(a, "-o") == 0 && i + 1 < argc) {
|
||||
@@ -120,6 +137,14 @@ main(int argc, char **argv)
|
||||
imports[nimports].path = argv[++i];
|
||||
imports[nimports].file = argv[++i];
|
||||
nimports++;
|
||||
} else if (strcmp(a, "--import-map") == 0) {
|
||||
if (i + 2 >= argc) {
|
||||
fputs("w6c: --import-map requires source and path\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
maps[nmaps].source = argv[++i];
|
||||
maps[nmaps].path = argv[++i];
|
||||
nmaps++;
|
||||
} else if (a[0] == '-') {
|
||||
fprintf(stderr, "w6c: unknown flag %s\n", a);
|
||||
return 2;
|
||||
@@ -132,13 +157,17 @@ main(int argc, char **argv)
|
||||
}
|
||||
if (src == NULL) {
|
||||
fputs("usage: w6c [-T|--test-package] [--command-package] [--entry] [-c] [-I out.wwi] "
|
||||
"[--import path dep.wwi]... [-o out.s] file.ww\n", stderr);
|
||||
"[--import path dep.wwi]... [--import-map source path]... [-o out.s] file.ww\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
if (nimports > 0 && !sepmode) {
|
||||
fputs("w6c: --import requires -c\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
if (nmaps > 0 && !sepmode) {
|
||||
fputs("w6c: --import-map requires -c\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
if ((entrymode || testpackage || commandpackage) && !sepmode) {
|
||||
fputs("w6c: --entry, --test-package, and --command-package require -c\n", stderr);
|
||||
return 2;
|
||||
@@ -157,6 +186,33 @@ main(int argc, char **argv)
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < nmaps; i++) {
|
||||
if (maps[i].source[0] == '\0' || maps[i].path[0] == '\0') {
|
||||
fputs("w6c: --import-map path is empty\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
if (i > 0 && strcmp(maps[i-1].source, maps[i].source) >= 0) {
|
||||
fputs("w6c: --import-map sources must be sorted and unique\n",
|
||||
stderr);
|
||||
return 2;
|
||||
}
|
||||
if (strcmp(importleaf(maps[i].source),
|
||||
importleaf(maps[i].path)) != 0) {
|
||||
fputs("w6c: --import-map must preserve import leaf\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
int direct = 0;
|
||||
for (int j = 0; j < nimports; j++)
|
||||
if (strcmp(maps[i].path, imports[j].path) == 0) {
|
||||
direct = 1;
|
||||
break;
|
||||
}
|
||||
if (!direct) {
|
||||
fputs("w6c: --import-map target is not a direct import\n",
|
||||
stderr);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
if (testsupport != NULL && (!sepmode
|
||||
|| (strcmp(testsupport, "test") != 0
|
||||
&& strcmp(testsupport, "__wwtest") != 0))) {
|
||||
@@ -203,6 +259,23 @@ main(int argc, char **argv)
|
||||
Node *file = parseinput(a, src, buf, len, NULL, testsupport,
|
||||
commandpackage || entrymode, &bad);
|
||||
if (bad) return 1;
|
||||
/* Source keeps its effective spelling and leaf alias, while package
|
||||
* resolution supplies the expanded canonical owner. Rewrite only the
|
||||
* primary import key before imported interface nodes are prepended. */
|
||||
for (Node *u = file->list; u; u = u->next) {
|
||||
if (u->kind != N_USE || u->usepath == NULL) continue;
|
||||
for (int i = 0; i < nmaps; i++)
|
||||
if (strcmp(u->usepath, maps[i].source) == 0) {
|
||||
u->usepath = maps[i].path;
|
||||
maps[i].seen = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < nmaps; i++) {
|
||||
if (maps[i].seen) continue;
|
||||
fputs("w6c: --import-map source is not in primary input\n", stderr);
|
||||
return 2;
|
||||
}
|
||||
if (head != NULL) {
|
||||
tail->next = file->list;
|
||||
file->list = head;
|
||||
@@ -250,6 +323,7 @@ main(int argc, char **argv)
|
||||
freearena(a);
|
||||
for (int i = 0; i < nimports; i++) free(imports[i].buf);
|
||||
free(imports);
|
||||
free(maps);
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
1144
cmd/ww/main.c
1144
cmd/ww/main.c
File diff suppressed because it is too large
Load Diff
@@ -45,6 +45,7 @@ type pkgplan = struct {
|
||||
identity: str,
|
||||
root: str,
|
||||
workdir: str,
|
||||
workdircreated: bool,
|
||||
buildout: str,
|
||||
builderr: str,
|
||||
start: i32,
|
||||
@@ -726,15 +727,21 @@ fn pkgsetplanpaths(p: *pkgplan, groups: []pkggroup, root: str, index: i32,
|
||||
p.root = strings.concat(root, "/plan-", num);
|
||||
if (!pkgmakedir(p.root)) { return false; };
|
||||
p.workdir = "";
|
||||
p.workdircreated = false;
|
||||
if (workroot.len != 0) {
|
||||
// Canonical request-directory identity makes equivalent spellings and
|
||||
// product reorderings select one command-scoped driver workdir.
|
||||
p.workdir = strings.concat(workroot, "/",
|
||||
pkgworkkey(p.sourceroot));
|
||||
match (os.mkdirs(p.workdir, 448)) {
|
||||
match (os.mkdirs(workroot, 448)) {
|
||||
case void => void;
|
||||
case let e: os.oserror => return false;
|
||||
};
|
||||
let mr: i32 = os.mkdir(p.workdir, 448i32);
|
||||
if (mr == 0) { p.workdircreated = true; }
|
||||
else if (mr != -17 || !pkgisdir(p.workdir)) {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
p.buildout = strings.concat(p.root, "/build.stdout");
|
||||
p.builderr = strings.concat(p.root, "/build.stderr");
|
||||
@@ -1466,6 +1473,15 @@ export fn packagecommand(args: []str) int = {
|
||||
i = 0;
|
||||
for (i < plans.len) {
|
||||
failed += pkgemitplan(&plans[i], groups, compileonly);
|
||||
// The driver commits its tool stamp only after semantic preflight. If
|
||||
// this request minted the persistent directory and rejection left it
|
||||
// unstamped, reclaim it only if it is still empty. A concurrent or
|
||||
// interrupted writer's contents are never recursively removed.
|
||||
if (plans[i].workdircreated
|
||||
&& !os.exists(strings.concat(plans[i].workdir, "/.wwtool.stamp"))) {
|
||||
let rr: i32 = os.rmdir(plans[i].workdir);
|
||||
if (rr != 0 && rr != -2) { failed += 1; };
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
if (!pkgremoveall(tmproot)) {
|
||||
|
||||
@@ -79,6 +79,31 @@ fn exportownermatches(buf: *u8, n: u64, path: *u8) bool = {
|
||||
return buf[need - 1u64] == 10u8;
|
||||
};
|
||||
|
||||
type importmap = struct {
|
||||
source: *u8,
|
||||
path: *u8,
|
||||
seen: bool,
|
||||
};
|
||||
|
||||
fn allocimportmaps(count: i32) ([]importmap | nomem) = {
|
||||
let value: []importmap = alloc([], count: u64)?;
|
||||
return value;
|
||||
};
|
||||
|
||||
fn allocimportptrs(count: i32) ([]*u8 | nomem) = {
|
||||
let value: []*u8 = alloc([], count: u64)?;
|
||||
return value;
|
||||
};
|
||||
|
||||
fn importleaf(path: *u8) str = {
|
||||
let whole: str = pathstr(path);
|
||||
let (prefix, suffix) = strings.rcut(whole, ".");
|
||||
// rcut returns (whole, empty) when absent and (prefix, empty) for a
|
||||
// trailing delimiter. Preserve that distinction to match strrchr.
|
||||
if (suffix.len != 0 || prefix.len != whole.len) { return suffix; };
|
||||
return whole;
|
||||
};
|
||||
|
||||
export fn main(argc: i32, argv: **u8) i32 = {
|
||||
let src: *u8 = nil;
|
||||
let out: *u8 = nil;
|
||||
@@ -91,11 +116,41 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
let sepmode: i32 = 0i32; // -c: #22 M3 separate-compile / primary-
|
||||
// only codegen (emit imported==0 decls
|
||||
// only; treat `.wwi` deps as external)
|
||||
let importpaths: []*u8 = alloc([], argc: u64)!;
|
||||
let pathallocation: ([]*u8 | nomem) = allocimportptrs(argc);
|
||||
let fileallocation: ([]*u8 | nomem) = allocimportptrs(argc);
|
||||
let importpaths: []*u8;
|
||||
let importfiles: []*u8;
|
||||
match (pathallocation) {
|
||||
case let value: []*u8 => importpaths = value;
|
||||
case nomem => {
|
||||
let m: str = "w6c: out of memory\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
match (fileallocation) {
|
||||
case let value: []*u8 => importfiles = value;
|
||||
case nomem => {
|
||||
let m: str = "w6c: out of memory\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
importpaths.len = argc;
|
||||
let importfiles: []*u8 = alloc([], argc: u64)!;
|
||||
importfiles.len = argc;
|
||||
let nimports: i32 = 0;
|
||||
let mapallocation: ([]importmap | nomem) = allocimportmaps(argc);
|
||||
let importmaps: []importmap;
|
||||
match (mapallocation) {
|
||||
case let value: []importmap => importmaps = value;
|
||||
case nomem => {
|
||||
let m: str = "w6c: out of memory\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 1;
|
||||
};
|
||||
};
|
||||
importmaps.len = argc;
|
||||
let nmaps: i32 = 0;
|
||||
|
||||
let i: i32 = 1;
|
||||
for (i < argc) {
|
||||
@@ -144,6 +199,17 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
importfiles[nimports] = argv[i + 2];
|
||||
nimports += 1;
|
||||
i += 2;
|
||||
} else { if (cstreq(a, "--import-map")) {
|
||||
if (i + 2 >= argc) {
|
||||
let m: str = "w6c: --import-map requires source and path\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
importmaps[nmaps].source = argv[i + 1];
|
||||
importmaps[nmaps].path = argv[i + 2];
|
||||
importmaps[nmaps].seen = false;
|
||||
nmaps += 1;
|
||||
i += 2;
|
||||
} else { if (a[0u64] == 45u8) {
|
||||
let m: str = "w6c: unknown flag\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
@@ -155,12 +221,12 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
return 2;
|
||||
};
|
||||
src = a;
|
||||
}; }; }; }; }; }; }; }; }; };
|
||||
}; }; }; }; }; }; }; }; }; }; };
|
||||
i += 1;
|
||||
};
|
||||
|
||||
if (src == nil) {
|
||||
let m: str = "usage: w6c_ww [-T|--test-package] [--command-package] [--entry] [-c] [-I out.wwi] [--import path dep.wwi]... [-o out.s] file.ww\n";
|
||||
let m: str = "usage: w6c_ww [-T|--test-package] [--command-package] [--entry] [-c] [-I out.wwi] [--import path dep.wwi]... [--import-map source path]... [-o out.s] file.ww\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
@@ -169,6 +235,11 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
if (nmaps > 0 && sepmode == 0) {
|
||||
let m: str = "w6c: --import-map requires -c\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
if ((entrymode != 0 || testpackage != 0 || commandpackage != 0)
|
||||
&& sepmode == 0) {
|
||||
let m: str = "w6c: --entry, --test-package, and --command-package require -c\n";
|
||||
@@ -195,6 +266,43 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
};
|
||||
importi += 1;
|
||||
};
|
||||
let mapi: i32 = 0;
|
||||
for (mapi < nmaps) {
|
||||
if (importmaps[mapi].source[0u64] == 0u8
|
||||
|| importmaps[mapi].path[0u64] == 0u8) {
|
||||
let m: str = "w6c: --import-map path is empty\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
if (mapi > 0 && strings.compare(pathstr(importmaps[mapi - 1].source),
|
||||
pathstr(importmaps[mapi].source)) >= 0) {
|
||||
let m: str = "w6c: --import-map sources must be sorted and unique\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
if (!syntax.streq(importleaf(importmaps[mapi].source),
|
||||
importleaf(importmaps[mapi].path))) {
|
||||
let m: str = "w6c: --import-map must preserve import leaf\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
let direct: bool = false;
|
||||
let directi: i32 = 0;
|
||||
for (directi < nimports) {
|
||||
if (cstreq(importmaps[mapi].path,
|
||||
pathstr(importpaths[directi]))) {
|
||||
direct = true;
|
||||
break;
|
||||
};
|
||||
directi += 1;
|
||||
};
|
||||
if (!direct) {
|
||||
let m: str = "w6c: --import-map target is not a direct import\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
mapi += 1;
|
||||
};
|
||||
if (testsupport != nil && (sepmode == 0
|
||||
|| (!cstreq(testsupport, "test")
|
||||
&& !cstreq(testsupport, "__wwtest")))) {
|
||||
@@ -275,6 +383,31 @@ export fn main(argc: i32, argv: **u8) i32 = {
|
||||
// `if (l.errs || p.errs) return 1;` — broken AST otherwise reaches
|
||||
// cgen and emits junk asm with a zero exit (silent miscompile).
|
||||
if (l.errs > 0 || ps.errs > 0) { return 1; };
|
||||
let use: *node = f.list;
|
||||
for (use != nil) {
|
||||
if (use.kind == syntax.nkind.N_USE && use.usepath.len != 0) {
|
||||
mapi = 0;
|
||||
for (mapi < nmaps) {
|
||||
if (syntax.streq(use.usepath,
|
||||
pathstr(importmaps[mapi].source))) {
|
||||
use.usepath = pathstr(importmaps[mapi].path);
|
||||
importmaps[mapi].seen = true;
|
||||
break;
|
||||
};
|
||||
mapi += 1;
|
||||
};
|
||||
};
|
||||
use = use.next;
|
||||
};
|
||||
mapi = 0;
|
||||
for (mapi < nmaps) {
|
||||
if (!importmaps[mapi].seen) {
|
||||
let m: str = "w6c: --import-map source is not in primary input\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
return 2;
|
||||
};
|
||||
mapi += 1;
|
||||
};
|
||||
if (importhead != nil) {
|
||||
importtail.next = f.list;
|
||||
f.list = importhead;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -907,7 +907,7 @@ fn writediamond(td: str, reverse: bool) str = {
|
||||
"/.wwtool.w6a")), testenv.readfile(assembler))
|
||||
|| !testenv.same(testenv.readfile(strings.concat(work,
|
||||
"/.wwtool.stamp")),
|
||||
"ww workdir fmt 12 mode build asm 0\n")) {
|
||||
"ww workdir fmt 13 mode build asm 0\n")) {
|
||||
fail("driver-identity", "persistent artifacts or identities are incomplete");
|
||||
};
|
||||
let coldwwi: str = testenv.readfile(strings.concat(work, "/dep.wwi"));
|
||||
|
||||
Reference in New Issue
Block a user