ww+selfhost: prepend source dir to import search path

This commit is contained in:
2026-05-15 09:00:30 +09:00
parent df74d9c429
commit 075da790a4
3 changed files with 88 additions and 10 deletions

View File

@@ -225,13 +225,31 @@ build_one(const char *src, const char *out, const char *extra_includes,
else srcdir = libdir;
}
/* Compose the search path: any -I dirs first, then srcdir.
* locate_import walks them left-to-right. */
/* Compose the search path: source-file's directory first, then any
* -I dirs, then srcdir. The source-dir lead matches Hare's CWD-first
* convention (its `hare test` is run from the module dir, making CWD
* == module-dir); our wrappers don't cd, so dirname(src) is the
* closest analog. Also matches cc -I. — source-dir wins ties over
* the system path. locate_import walks left-to-right. */
char srcd[1024];
{
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", extra_includes, srcdir);
snprintf(searchpath, sizeof searchpath, "%s:%s:%s",
srcd, extra_includes, srcdir);
else
snprintf(searchpath, sizeof searchpath, "%s", srcdir);
snprintf(searchpath, sizeof searchpath, "%s:%s", srcd, srcdir);
srcdir = searchpath;
/* Strip extension to derive a stem; e.g. /tmp/foo.ww → /tmp/foo */