From abe970c173f5c513c632cc6d177fe58ada7c92ee Mon Sep 17 00:00:00 2001 From: 0x221E Date: Sun, 18 Jan 2026 16:43:23 +0100 Subject: [PATCH] Feature: Add capability to build multiple files. --- build.sh | 2 +- example/main.c | 8 +++++++ example/test.c | 1 + src/command.c | 61 +++++++++++++++++++++++++++++++++++--------------- src/command.h | 14 +++++++++++- src/config.h | 19 ++++++++++++++-- src/main.c | 18 +++++++++++---- 7 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 example/main.c create mode 100644 example/test.c diff --git a/build.sh b/build.sh index 2783d96..9cfc730 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -gcc -o ibuild src/main.c src/memory.c src/utils.c src/string.c src/discovery.c src/io.c src/command.c -fsanitize=address -g -DDEBUG -Wall -Wextra -Wpedantic -Wshadow +gcc -o example/ibuild src/main.c src/memory.c src/utils.c src/string.c src/discovery.c src/io.c src/command.c src/build.c -fsanitize=address -g -DDEBUG -Wall -Wextra -Wpedantic -Wshadow diff --git a/example/main.c b/example/main.c new file mode 100644 index 0000000..17e3c26 --- /dev/null +++ b/example/main.c @@ -0,0 +1,8 @@ +#include + +int main(void) +{ + printf("Hello world!\n"); + printf("This is build 2!\n"); + return 0; +} diff --git a/example/test.c b/example/test.c new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/example/test.c @@ -0,0 +1 @@ + diff --git a/src/command.c b/src/command.c index 08825ee..f5e78eb 100644 --- a/src/command.c +++ b/src/command.c @@ -12,31 +12,56 @@ // TODO: This file is UNSAFE. This file is purely here for MVP purposes, and to self-host this program itself. -Command command_create(Arena* a, Discovery* d, Configuration* c) +Command command_create_f_to_o(CommandOptions* co) { - assert(d != NULL); - assert(a != NULL); - assert(c != NULL); + assert(co != NULL); + + size_t len_args = 6; + + size_t flag_count = 0; + + StringView* dep_flags_buf = NULL; - Command cmd; - cmd.a = a; - cmd.app = d->cc; - - size_t args_len = d->c_count + 2; - const char** args_buf = (const char**)arena_alloc(a, sizeof(const char*) * (args_len)); - args_buf[0] = d->cc.buf; - - for(size_t i = 0; i < d->c_count; i++) + switch(co->bp) { - args_buf[i+1] = d->c_files[i]->path.buf; - LOG_DEBUG("File to build: %s", d->c_files[i]->path); + case B_DEBUG: + len_args += co->c->debug_compiler_flags_c; + flag_count = co->c->debug_compiler_flags_c; + dep_flags_buf = co->c->debug_compiler_flags; + break; + case B_PROD: + len_args += co->c->prod_compiler_flags_c; + flag_count = co->c->prod_compiler_flags_c; + dep_flags_buf = co->c->prod_compiler_flags; + break; + default: + DIE("No configuration was selected!"); + } + + const char** args_buf = (const char**)arena_alloc(co->a, sizeof(const char*) * len_args); + args_buf[0] = co->app.buf; + args_buf[1] = "-c"; + args_buf[2] = co->files[0].buf; + args_buf[3] = "-o"; + + StringView o_ext = STR_LIT(co->sp, ".o"); + StringView f_o = string_concat_ss(co->sp, &co->files[0], &o_ext); + args_buf[4] = f_o.buf; + args_buf[5] = "-MMD"; + + for (size_t i = 0; i < flag_count; i++) + { + args_buf[i + 5] = dep_flags_buf[i].buf; } - args_buf[args_len - 1] = NULL; - cmd.args = args_buf; - return cmd; + args_buf[len_args] = NULL; + + LOG_DEBUG("Command created, first 4 args: %s %s %s %s %s | total args: %d", args_buf[0], args_buf[1], args_buf[2], args_buf[3], args_buf[4], len_args); + + return (Command){.a = co->a, .app = co->app, .args = args_buf}; } +// TODO: Add input sanitization to cmd->app and cmd->args int command_run(Command* cmd) { assert(cmd != NULL); diff --git a/src/command.h b/src/command.h index 1d13273..73afd35 100644 --- a/src/command.h +++ b/src/command.h @@ -2,11 +2,22 @@ #define COMMAND_H #include "string.h" +#include "common.h" typedef struct Arena Arena; typedef struct Discovery Discovery; typedef struct Configuration Configuration; +struct CommandOptions +{ + Arena* a; + StringPool* sp; + BuildProfile bp; + Configuration* c; + StringView app; + StringView* files; +}; + struct Command { Arena* a; @@ -16,8 +27,9 @@ struct Command typedef struct Argument Argument; typedef struct Command Command; +typedef struct CommandOptions CommandOptions; -Command command_create(Arena* a, Discovery* d, Configuration* c); +Command command_create_f_to_o(CommandOptions* co); int command_run(Command* cmd); diff --git a/src/config.h b/src/config.h index 6481538..395fed1 100644 --- a/src/config.h +++ b/src/config.h @@ -5,9 +5,24 @@ struct Configuration { - StringView compiler_path; + // C-Compiler Flags + StringView cc_compiler_path; StringView src_dir; - StringView* src_files; + + // General Flags + //StringView* src_files; + + // Debug Profile + StringView* debug_compiler_flags; + size_t debug_compiler_flags_c; + + // Production Profile + StringView* prod_compiler_flags; + size_t prod_compiler_flags_c; + + // Linker arguments + StringView* linker_flags; + size_t linker_flags_c; }; typedef struct Configuration Configuration; diff --git a/src/main.c b/src/main.c index a5bed61..b43f2ff 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include "discovery.h" #include "command.h" #include "config.h" +#include "build.h" int main(void) { @@ -22,11 +23,20 @@ int main(void) Discovery d = discovery_create(&a_d, &sp); discovery_discover(&d); - Arena a_cmd = arena_create(&b, MEM_ARENA_MAX_CAP); - Configuration sample_config; - Command cmd = command_create(&a_cmd, &d, &sample_config); - command_run(&cmd); + /* Command cmd = command_create(&a_cmd, &d, &sample_config); */ + /* command_run(&cmd); */ + + Arena a_b = arena_create(&b, 1024*1024*5); + + BuildContext bc; + bc.build_profile = B_DEBUG; + bc.d = &d; + bc.c = &sample_config; + bc.a = &a_b; + bc.sp = &sp; + + build(&bc); return 0; }