88 lines
1.9 KiB
C
88 lines
1.9 KiB
C
|
|
#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;
|
||
|
|
}
|