From 38ca8ae719149e08d43aa8addbaa9b914cae9452 Mon Sep 17 00:00:00 2001 From: 0x221E Date: Sat, 17 Jan 2026 18:18:26 +0100 Subject: [PATCH] Feature: Add C compiler discovery and file discovery feature --- src/discovery.c | 74 +++++++++++++++++++++++++++++++++++++++++++------ src/discovery.h | 9 +++++- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/src/discovery.c b/src/discovery.c index bebf4d2..d60e956 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -7,6 +7,9 @@ #include #include +#include +#include +#include Discovery discovery_create(Arena* a, StringPool* sp) { @@ -31,23 +34,18 @@ Discovery discovery_create(Arena* a, StringPool* sp) return d; } -void discovery_cleanup(Discovery* d) -{ - -} - void discovery_discover(Discovery* d) { assert(d != NULL); DirOpContext doc; doc.a_f = d->a; doc.sp = d->sp; - StringView path = SV(d->sp, "."); + StringView path = STR_LIT(d->sp, "."); size_t count = dir_get_recursive(&doc, &path); d->file_count = count; LOG_DEBUG("Discovered %d files.", count); - StringView c_ext = SV(d->sp, ".c"); + StringView c_ext = STR_LIT(d->sp, ".c"); ExtDiscoveryResult c_res = discovery_discover_ext(d, d->a, &c_ext); if(c_res.mem != NULL) @@ -57,7 +55,7 @@ void discovery_discover(Discovery* d) d->c_files = c_res.mem; } - StringView h_ext = SV(d->sp, ".h"); + StringView h_ext = STR_LIT(d->sp, ".h"); ExtDiscoveryResult h_res = discovery_discover_ext(d, d->a, &h_ext); if(h_res.mem != NULL) @@ -66,6 +64,35 @@ void discovery_discover(Discovery* d) d->h_count = h_res.len; d->h_files = h_res.mem; } + + if(d->c_count > 0) + { + discovery_discover_ccompiler(d); + } +} + +void discovery_discover_ccompiler(Discovery* d) +{ + assert(d != NULL); + + StringView gcc = STR_LIT(d->sp, "gcc"); + StringView cc_gcc = discovery_discover_path(d, &gcc); + + if(cc_gcc.buf != NULL) + { + d->cc = cc_gcc; + return; + } + + StringView clang = STR_LIT(d->sp, "clang"); + StringView cc_clang = discovery_discover_path(d, &clang); + + if(cc_clang.buf != NULL) + { + d->cc = cc_clang; + return; + } + LOG_DEBUG("No C compilers were found, however, %d C files were found. Not installed?", d->c_count); } // TODO: Make the search logic faster, or at least search all of the wanted extension at once, uniformly. @@ -91,9 +118,40 @@ ExtDiscoveryResult discovery_discover_ext(Discovery* d, Arena* a, StringView* ex DirEntry** entry = (DirEntry**)arena_alloc(a, sizeof(DirEntry*)); *entry = &d->files[i]; count++; + continue; } } if(count > 0) return (ExtDiscoveryResult){.mem = res, .len = count}; return (ExtDiscoveryResult){.mem = NULL, .len = 0}; } + +StringView discovery_discover_path(Discovery* d, StringView* s) +{ + assert(d != NULL); + assert(s != NULL); + + char* path_env = getenv("PATH"); + + if(path_env == NULL) return (StringView){.buf = NULL, .len = 0}; + + char* path = strdup(path_env); + + errno = 0; + if(path == NULL && errno != 0) DIE("String copy failure."); + + char* current = strtok(path, ":"); + do + { + LOG_DEBUG("Checking: %s for %s in PATH.", current, s->buf); + StringView p = string_create(d->sp, current, strlen(current)); + StringView fp = path_concat_ss(d->sp, &p, s); + if(access(fp.buf, X_OK) == 0) + { + LOG_DEBUG("Found %s in PATH:%s", s->buf, p.buf); + return fp; + } + }while((current = strtok(NULL, ":")) != NULL); + + return (StringView){.buf = NULL, .len = 0}; +} diff --git a/src/discovery.h b/src/discovery.h index 62c883c..52bad01 100644 --- a/src/discovery.h +++ b/src/discovery.h @@ -3,11 +3,12 @@ typedef struct Arena Arena; typedef struct StringPool StringPool; -typedef struct StringView StringView; typedef struct DirEntry DirEntry; #include +#include "string.h" + struct Discovery { // Memory @@ -25,6 +26,9 @@ struct Discovery size_t c_count; size_t h_count; size_t cpp_count; + + StringView cc; + StringView cxx; }; struct ExtDiscoveryResult @@ -40,6 +44,9 @@ Discovery discovery_create(Arena* a, StringPool* sp); void discovery_cleanup(Discovery* d); void discovery_discover(Discovery* d); +void discovery_discover_ccompiler(Discovery* d); + ExtDiscoveryResult discovery_discover_ext(Discovery* d, Arena* a, StringView* ext); +StringView discovery_discover_path(Discovery* d, StringView* s); #endif