ww build: name current directory output

This commit is contained in:
2026-08-21 17:38:00 +09:00
parent 17b2879c3e
commit b03bbb1428
5 changed files with 436 additions and 23 deletions

View File

@@ -38,7 +38,7 @@ static const char *usage =
" -o FILE publishes a non-main archive FILE + FILE.wwi\n"
" -o DIR publishes each selected command beneath DIR\n"
" lib/... every eligible package under lib, recursively\n"
" . build the cwd's <basename>.ww\n";
" . current directory package (default when no path is given)\n";
static char *self_dir;
static const char *self_path;
@@ -7158,6 +7158,36 @@ build_import_leaf(const char *identity, char *out, size_t outsz)
snprintf(out, outsz, "%s", leaf);
}
static int
build_current_dir_spelling(const char *path)
{
if (path[0] == '\0' || path[0] == '/') return 0;
const char *p = path;
int found = 0;
for (;;) {
while (*p == '/') p++;
if (*p == '\0') return found;
if (*p++ != '.' || (*p != '\0' && *p != '/')) return 0;
found = 1;
}
}
static void
build_local_dir_leaf(const char *resolved, char *out, size_t outsz)
{
char canonical[PATH_MAX];
const char *selected = resolved;
if (build_current_dir_spelling(resolved)
&& realpath(resolved, canonical) != NULL)
selected = canonical;
char tmp[PATH_MAX];
memcpy(tmp, selected, strlen(selected) + 1);
size_t n = strlen(tmp);
while (n > 1 && tmp[n - 1] == '/') tmp[--n] = '\0';
const char *base = strrchr(tmp, '/');
snprintf(out, outsz, "%s", base ? base + 1 : tmp);
}
static int
resolve_module(const char *name, const char *incs, char *out, size_t outsz,
int *is_dir)
@@ -7428,15 +7458,12 @@ do_build(int argc, char **argv)
} else if (is_dir && root_identity != NULL) {
build_import_leaf(root_identity, out, sizeof out);
} else if (is_dir) {
char tmp[PATH_MAX];
memcpy(tmp, resolved, strlen(resolved) + 1);
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);
build_local_dir_leaf(resolved, out, sizeof out);
} else {
basename_no_ext(resolved, out, sizeof out);
}
if (!outflag[0] && is_dir && build_current_dir_spelling(resolved))
objstem = out;
const char *default_output_dir = !outflag[0] && build_output_dir(out)
? out : NULL;
if (discard_output) {