mirror of
https://github.com/0x221E/ibuild.git
synced 2026-01-18 02:42:20 +00:00
Feature: Add C compiler discovery and file discovery feature
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
typedef struct Arena Arena;
|
||||
typedef struct StringPool StringPool;
|
||||
typedef struct StringView StringView;
|
||||
typedef struct DirEntry DirEntry;
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user