Support full paths with $embed (#2335)

* Support full paths with `$embed`

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2025-07-28 17:44:17 -04:00
committed by GitHub
parent d8daa4ac83
commit 2639338426
4 changed files with 18 additions and 2 deletions

View File

@@ -31,6 +31,7 @@
- Deprecate allocator::heap() and allocator::temp()
- Add `thread::fence` providing a thread fence.
- Place output in `out` by default for projects. Use temp folder for building at the command line.
- Allow absolute paths for `$embed`.
### Fixes
- mkdir/rmdir would not work properly with substring paths on non-windows platforms.

View File

@@ -9996,12 +9996,12 @@ static inline bool sema_expr_analyse_embed(SemaContext *context, Expr *expr, boo
}
}
if (!expr_is_const_string(filename)) RETURN_SEMA_ERROR(filename, "A compile time string was expected.");
if (!filename->const_expr.bytes.len) RETURN_SEMA_ERROR(filename, "Expected a non-empty string.");
CompilationUnit *unit = context->unit;
const char *string = filename->const_expr.bytes.ptr;
char *path;
char *name;
if (file_namesplit(unit->file->full_path, &name, &path))
if (file_path_is_relative(string) && file_namesplit(unit->file->full_path, &name, &path))
{
string = file_append_path(path, string);
}

View File

@@ -564,6 +564,20 @@ bool file_exists(const char *path)
return S_ISDIR(st.st_mode) || S_ISREG(st.st_mode) || S_ISREG(st.st_mode);
}
bool file_path_is_relative(const char *file_name)
{
assert(file_name);
size_t len = strlen(file_name);
if (!len) return false;
// returns !full_path condition
#if PLATFORM_WINDOWS
return !(file_name[0] == '\\' || (len >= 2 && char_is_letter(file_name[0]) && file_name[1] == ':'));
#else
return file_name[0] != '/';
#endif
}
#define PATH_BUFFER_SIZE 16384
static char path_buffer[PATH_BUFFER_SIZE];

View File

@@ -80,6 +80,7 @@ bool file_delete_file(const char *path);
void file_delete_dir(const char *path);
bool file_is_dir(const char *file);
bool file_exists(const char *path);
bool file_path_is_relative(const char *file_name);
FILE *file_open_read(const char *path);
bool file_touch(const char *path);
char *file_read_binary(const char *path, size_t *size);