1
0
Fork 0
forked from fun/fun

Made the parser more path compatible to other OS\'s, especially Windows. (0.13.0)

This commit is contained in:
Johannes Findeisen 2025-09-30 16:54:47 +02:00
commit 08b1680bc6
3 changed files with 46 additions and 18 deletions

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(fun VERSION 0.12.6 LANGUAGES C)
project(fun VERSION 0.13.0 LANGUAGES C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
@ -69,7 +69,7 @@ install(TARGETS fun
# Libs
install(DIRECTORY lib/
DESTINATION /usr/lib/fun
DESTINATION /usr/share/fun/lib
FILES_MATCHING PATTERN "*.fun")
# Optionally install example scripts

View file

@ -10,6 +10,21 @@ CMAKE_FLAGS ?= -DFUN_DEBUG=OFF
ifneq ($(strip $(DEFAULT_LIB_DIR)),)
CMAKE_FLAGS += -DCMAKE_C_FLAGS="$(CMAKE_C_FLAGS) -DDEFAULT_LIB_DIR=\\\"$(DEFAULT_LIB_DIR)/\\\""
CMAKE_FLAGS += -DCMAKE_CXX_FLAGS="$(CMAKE_CXX_FLAGS) -DDEFAULT_LIB_DIR=\\\"$(DEFAULT_LIB_DIR)/\\\""
else
# Provide OS-specific defaults for Windows, macOS, Linux/Unix
ifeq ($(OS),Windows_NT)
CMAKE_FLAGS += -DCMAKE_C_FLAGS="$(CMAKE_C_FLAGS) -DDEFAULT_LIB_DIR=\\\"C:/Users/Public/fun/lib/\\\""
CMAKE_FLAGS += -DCMAKE_CXX_FLAGS="$(CMAKE_CXX_FLAGS) -DDEFAULT_LIB_DIR=\\\"C:/Users/Public/fun/lib/\\\""
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
CMAKE_FLAGS += -DCMAKE_C_FLAGS="$(CMAKE_C_FLAGS) -DDEFAULT_LIB_DIR=\\\"/Library/Application Support/fun/lib/\\\""
CMAKE_FLAGS += -DCMAKE_CXX_FLAGS="$(CMAKE_CXX_FLAGS) -DDEFAULT_LIB_DIR=\\\"/Library/Application Support/fun/lib/\\\""
else
CMAKE_FLAGS += -DCMAKE_C_FLAGS="$(CMAKE_C_FLAGS) -DDEFAULT_LIB_DIR=\\\"/usr/share/fun/lib/\\\""
CMAKE_FLAGS += -DCMAKE_CXX_FLAGS="$(CMAKE_CXX_FLAGS) -DDEFAULT_LIB_DIR=\\\"/usr/share/fun/lib/\\\""
endif
endif
endif
.PHONY: all configure build fun fun_test test_opcodes clean distclean install repl run

View file

@ -265,7 +265,7 @@ static char *preprocess_includes_internal(const char *src, int depth) {
/* Build-time default, can be overridden by compiler define -DDEFAULT_LIB_DIR=".../" */
#ifndef DEFAULT_LIB_DIR
#define DEFAULT_LIB_DIR "/usr/lib/fun/"
#define DEFAULT_LIB_DIR "/usr/share/fun/lib/"
#endif
const char *env_lib = getenv("FUN_LIB_DIR");
@ -316,37 +316,50 @@ static char *preprocess_includes_internal(const char *src, int depth) {
char *inc = NULL;
if (opener == '<') {
/* Attempt environment override if provided */
/* Angle-bracket include resolution order:
* 1) FUN_LIB_DIR (env), respecting '/' or '\' endings
* 2) DEFAULT_LIB_DIR (compile-time define)
* 3) "lib/" under current working directory (developer fallback)
*
* Always assign 'resolved' to the last attempted candidate so errors are informative.
*/
resolved[0] = '\0';
/* 1) FUN_LIB_DIR */
if (env_lib && env_lib[0]) {
size_t elen = strlen(env_lib);
int needs_slash = (elen > 0 && env_lib[elen - 1] != '/');
if (needs_slash)
snprintf(resolved, sizeof(resolved), "%s/%s", env_lib, path);
char last = env_lib[elen ? (elen - 1) : 0];
int needs_sep = !(last == '/' || last == '\\');
char sep = (last == '\\') ? '\\' : '/';
if (needs_sep)
snprintf(resolved, sizeof(resolved), "%s%c%s", env_lib, sep, path);
else
snprintf(resolved, sizeof(resolved), "%s%s", env_lib, path);
inc = read_file_all(resolved, &inc_len);
}
/* Fallback to default lib dir */
/* 2) DEFAULT_LIB_DIR */
if (!inc) {
snprintf(resolved2, sizeof(resolved2), "%s%s", DEFAULT_LIB_DIR, path);
inc = read_file_all(resolved2, &inc_len);
if (inc) {
/* reflect the actual resolved path in annotations */
strncpy(resolved, resolved2, sizeof(resolved) - 1);
resolved[sizeof(resolved) - 1] = '\0';
}
snprintf(resolved, sizeof(resolved), "%s%s", DEFAULT_LIB_DIR, path);
inc = read_file_all(resolved, &inc_len);
}
/* 3) project-local dev fallback: lib/<path> */
if (!inc) {
snprintf(resolved, sizeof(resolved), "lib/%s", path);
inc = read_file_all(resolved, &inc_len);
}
} else {
/* quoted include: relative path */
/* quoted include: relative path (cwd) */
snprintf(resolved, sizeof(resolved), "%s", path);
inc = read_file_all(resolved, &inc_len);
}
free(path);
if (!inc) {
fprintf(stderr, "Include error: cannot read '%s'\n", resolved);
fprintf(stderr, "Include error: cannot read '%s'\n", resolved[0] ? resolved : "(unresolved)");
sb_append(&out, "// include error: cannot read ");
sb_append(&out, resolved);
sb_append(&out, resolved[0] ? resolved : "(unresolved)");
sb_append(&out, "\n");
} else {
char *exp = preprocess_includes_internal(inc, depth + 1);