Make include paths relative to the source file

This commit is contained in:
2025-09-24 10:19:40 +02:00
parent 1366d3c6d4
commit 4ce9bdcffd
2 changed files with 34 additions and 2 deletions
+33 -1
View File
@@ -3585,7 +3585,7 @@ WL_Compiler *wl_compiler_init(WL_Arena *arena)
return compiler;
}
WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String content)
WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String path, WL_String content)
{
if (compiler->err)
return (WL_AddResult) { .type=WL_ADD_ERROR };
@@ -3596,6 +3596,38 @@ WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String content)
return (WL_AddResult) { .type=WL_ADD_ERROR };
}
// Make include paths relative to the parent file
if (path.len > 0) {
String parent = { path.ptr, path.len };
char sep = '/';
while (parent.len > 0 && parent.ptr[parent.len-1] != sep)
parent.len--;
if (parent.len > 0) {
Node *include = pres.includes;
while (include) {
char *dst = alloc(compiler->arena, parent.len + include->include_path.len + 1, 1);
if (dst == NULL) {
// TODO
}
memcpy(dst,
parent.ptr,
parent.len);
memcpy(dst + parent.len,
include->include_path.ptr,
include->include_path.len);
include->include_path = (String) { dst, parent.len + include->include_path.len };
include = include->include_next;
}
}
}
CompiledFile compiled_file = {
.file = compiler->waiting_file,
.root = pres.node,