From 728b4f67dab3a5d4e8b03ebb9c5513bb005b1f7e Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 29 Sep 2025 18:22:40 +0200 Subject: [PATCH] Added a custom lib dir. E.g.: FUN_LIB_DIR="/some/dir\ ./path/to/fun and added DEFAULT_LIB_DIR as compile time option. --- CMakeLists.txt | 2 +- Makefile | 8 ++++++++ src/parser_utils.c | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d3d193d..095dfdb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.16) -project(fun VERSION 0.12.2 LANGUAGES C) +project(fun VERSION 0.12.3 LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/Makefile b/Makefile index 2454352..8f426c8 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,14 @@ CMAKE ?= cmake # Default to a build with extra logging disabled CMAKE_FLAGS ?= -DFUN_DEBUG=OFF +# Optional: override default library directory for #include <...> lookups +# Usage: make DEFAULT_LIB_DIR=/custom/fun/lib +# This injects a compiler define: -DDEFAULT_LIB_DIR="/custom/fun/lib/" +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)/\\\"" +endif + .PHONY: all configure build fun fun_test test_opcodes clean distclean install repl run all: clean fun fun_test test_opcodes diff --git a/src/parser_utils.c b/src/parser_utils.c index 2b01fec..b0d1e2a 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -263,7 +263,12 @@ static char *preprocess_includes_internal(const char *src, int depth) { return strdup(""); } - const char *LIB_DIR = "/usr/lib/fun/"; + /* Build-time default, can be overridden by compiler define -DDEFAULT_LIB_DIR=".../" */ + #ifndef DEFAULT_LIB_DIR + #define DEFAULT_LIB_DIR "/usr/lib/fun/" + #endif + + const char *env_lib = getenv("FUN_LIB_DIR"); size_t len = strlen(src); StrBuf out; sb_init(&out); @@ -304,18 +309,40 @@ static char *preprocess_includes_internal(const char *src, int depth) { while (k < len && src[k] != '\n') k++; if (k < len && src[k] == '\n') k++; - /* resolve file path */ + /* resolve file path; for <...> try FUN_LIB_DIR first, then default */ char resolved[1024]; + char resolved2[1024]; + size_t inc_len = 0; + char *inc = NULL; + if (opener == '<') { - snprintf(resolved, sizeof(resolved), "%s%s", LIB_DIR, path); + /* Attempt environment override if provided */ + 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); + else + snprintf(resolved, sizeof(resolved), "%s%s", env_lib, path); + inc = read_file_all(resolved, &inc_len); + } + /* Fallback to 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'; + } + } } else { + /* quoted include: relative path */ snprintf(resolved, sizeof(resolved), "%s", path); + inc = read_file_all(resolved, &inc_len); } free(path); - /* read and recursively expand */ - size_t inc_len = 0; - char *inc = read_file_all(resolved, &inc_len); if (!inc) { fprintf(stderr, "Include error: cannot read '%s'\n", resolved); sb_append(&out, "// include error: cannot read ");