Fix: Build phase for C.

This commit is contained in:
0x221E
2026-01-18 18:24:00 +01:00
parent abe970c173
commit 3cba603e7c
3 changed files with 135 additions and 0 deletions

87
src/build.c Normal file
View File

@@ -0,0 +1,87 @@
#include "build.h"
#include <stddef.h>
#include <assert.h>
#include "memory.h"
#include "string.h"
#include "discovery.h"
#include "command.h"
#include "io.h"
#include "utils.h"
// Only supports C and multiple files
void build(BuildContext* bc)
{
assert(bc != NULL);
assert(bc->sp != NULL);
int sc = build_c_to_o(bc);
if(sc != 0) DIE("Compilation failed!");
link(bc);
}
void link(BuildContext* bc)
{
assert(bc != NULL);
if(bc->obj_files == NULL) DIE("Linking process was triggered, however there are no object files.");
Command cmd;
cmd.a = bc->a;
cmd.app = bc->d->cc;
size_t len = bc->d->c_count + 2;
const char** args = (const char**)arena_alloc(bc->a, sizeof(const char*) * len);
args[0] = bc->d->cc.buf;
for(size_t i = 0; i < bc->d->c_count; i++)
{
args[i + 1] = bc->obj_files[i]->buf;
}
args[len - 1] = NULL;
cmd.args = args;
int status = command_run(&cmd);
if(status != 0)
DIE("Linking failed with status code: %d");
LOG_DEBUG("Linking completed!");
}
int build_c_to_o(BuildContext* bc)
{
assert(bc != NULL);
if(bc->d->c_count == 0)
return 1;
CommandOptions co;
co.a = bc->a;
co.sp = bc->sp;
co.bp = bc->build_profile;
co.c = bc->c;
co.app = bc->d->cc;
StringView* arr = (StringView*)arena_alloc(bc->a, sizeof(StringView) * 1);
co.files = arr;
StringView** obj_files = (StringView**)arena_alloc(bc->a, sizeof(StringView*) * bc->d->c_count);
bc->obj_files = obj_files;
for(size_t i = 0; i < bc->d->c_count; i++)
{
co.files[0] = bc->d->c_files[i]->path;
Command cmd = command_create_f_to_o(&co);
int status = command_run(&cmd);
if(status != 0)
DIE("Failed to compile %s, compiler exited with status code: %d", bc->d->c_files[i]->path.buf, status);
bc->obj_files[i] = &bc->d->c_files[i]->path;
LOG_DEBUG("Compiling %s", bc->d->c_files[i]->path.buf);
}
return 0;
}

36
src/build.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef BUILD_H
#define BUILD_H
#include "common.h"
typedef struct Configuration Configuration;
typedef struct Discovery Discovery;
typedef struct Arena Arena;
typedef struct StringPool StringPool;
typedef struct StringView StringView;
typedef struct Command Command;
struct BuildContext
{
// Memory
Arena* a;
StringPool* sp;
// Build Params
enum BuildProfile build_profile;
Configuration* c;
Discovery* d;
StringView** obj_files;
};
typedef enum BuildProfile BuildProfile;
typedef struct BuildContext BuildContext;
void build(BuildContext* bc);
void link(BuildContext* bc);
int build_c_to_o(BuildContext* bc);
#endif

12
src/common.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef COMMON_H
#define COMMON_H
enum BuildProfile
{
B_DEBUG,
B_PROD
};
typedef enum BuildProfile BuildProfile;
#endif