summaryrefslogtreecommitdiff
path: root/build.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-05-29 12:19:52 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-05-29 12:19:52 +0530
commitc38475eb5d82f6e587faf2956a7eba3085890e99 (patch)
treedcfd2cf494ce4a92d998a0b7b4ebbf859ab3957f /build.c
parent764ecc4127e3a2f967679be5778b263cf4927b90 (diff)
logging system works
Diffstat (limited to 'build.c')
-rw-r--r--build.c164
1 files changed, 72 insertions, 92 deletions
diff --git a/build.c b/build.c
index fc2312f..53fe506 100644
--- a/build.c
+++ b/build.c
@@ -6,6 +6,19 @@
#include <time.h>
#include <string.h>
+int status;
+
+#define RUN_CMD(...) do { \
+ if (fork() == 0) { \
+ char* args[] = __VA_ARGS__; \
+ print_cmd(args); \
+ execvp(args[0], args); \
+ return 0; \
+ } else { \
+ wait(&status); \
+ } \
+} while (0);
+
void print_cmd(char** args) {
int i = 0;
while (args[i] != NULL) {
@@ -14,6 +27,11 @@ void print_cmd(char** args) {
printf("\n");
}
+int clean_mode();
+int test_mode();
+int server_mode();
+int engine_mode();
+
// https://github.com/tsoding/nob.h/blob/0a08926d8094fc4ae678155c5d73ae21d1f96f3f/nob.h#L2413
int needs_rebuild(char* bin_file, char* source_file) {
struct stat statbuf = {0};
@@ -32,113 +50,75 @@ int needs_rebuild(char* bin_file, char* source_file) {
return input_path_time > output_path_time;
}
+#define ENGINE_O "output/engine.o"
+#define SERVER_O "output/server.o"
+#define LOG_O "output/log.o"
+
+#define LOGGER_C "src/logger/logger.c"
+
int main(int argc, char** argv) {
if (needs_rebuild(argv[0], __FILE__)) {
- if (fork() == 0) {
- char* args[] = {"gcc", __FILE__, "-o", argv[0], NULL};
- print_cmd(args);
- execvp(args[0], args);
+ RUN_CMD({"gcc", __FILE__, "-o", argv[0], NULL});
+ if (status != 0) {
+ // gcc error
return 0;
- } else {
- int x;
- wait(&x);
- if (x != 0) {
- // gcc error
- return 0;
- }
- print_cmd(argv);
- execvp(argv[0], argv);
}
+ print_cmd(argv);
+ execvp(argv[0], argv);
}
- if (fork() == 0) {
- char* args[] = {"mkdir", "-p", "output", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
+ RUN_CMD({"rm", "/dev/mqueue/logs", NULL});
+ RUN_CMD({"mkdir", "-p", "output", NULL});
+ RUN_CMD({"gcc", "src/log.c", "-o", LOG_O, NULL});
if (argc > 1) {
if (strcmp(argv[1], "test") == 0) {
- printf("Test mode\n");
- if (fork() == 0) {
- char* args[] = {"gcc", "generate/find_tests.c", "-o", "output/find_tests.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
- if (fork() == 0) {
- char* args[] = {"./output/find_tests.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
- printf("\n");
- if (fork() == 0) {
- char* args[] = {"gcc", "tests/main.c", "-o", "output/test.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
-
- char* args[] = {"./output/test.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
+ return test_mode();
}
if (strcmp(argv[1], "server") == 0) {
- printf("Server mode\n");
- if (fork() == 0) {
- char* args[] = {"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
- if (fork() == 0) {
- char* args[] = {"gcc", "src/server.c", "-lcrypto", "-o", "output/server.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
-
- if (fork() == 0) {
- char* args[] = {"gcc", "src/engine.c", "-o", "output/main.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
-
- char* args[] = {"./output/server.o", "./output/main.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
+ return server_mode();
+ }
+ if (strcmp(argv[1], "clean") == 0) {
+ return clean_mode();
}
}
+ return engine_mode();
+}
- if (fork() == 0) {
- char* args[] = {"gcc", "src/engine.c", "-o", "output/main.o", NULL};
- print_cmd(args);
- execvp(args[0], args);
- return 0;
- } else {
- wait(NULL);
- }
+int clean_mode() {
+ char* args[] = {"rm", "-r", "output", NULL};
+ print_cmd(args);
+ execvp(args[0], args);
+ return 0;
+}
+
+int test_mode() {
+ RUN_CMD({"gcc", "generate/find_tests.c", LOGGER_C, "-o", "output/find_tests.o", NULL});
+ RUN_CMD({"./output/find_tests.o", NULL});
+ printf("\n");
+ RUN_CMD({"gcc", "tests/main.c", LOGGER_C, "-o", "output/test.o", NULL});
+
+ char* args[] = {LOG_O, "./output/test.o", NULL};
+ print_cmd(args);
+ execvp(args[0], args);
+ return 0;
+}
+
+int server_mode() {
+ RUN_CMD({"rm", "/dev/mqueue/server_to_engine", "/dev/mqueue/engine_to_server", NULL});
+ RUN_CMD({"gcc", "src/server.c", LOGGER_C, "-lcrypto", "-o", SERVER_O, NULL});
+ RUN_CMD({"gcc", "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL});
+
+ char* args[] = {LOG_O, SERVER_O, ENGINE_O, NULL};
+ print_cmd(args);
+ execvp(args[0], args);
+ return 0;
+}
+
+int engine_mode() {
+ RUN_CMD({"gcc", "src/engine.c", LOGGER_C, "-o", ENGINE_O, NULL});
- char* args[] = {"./output/main.o", NULL};
+ char* args[] = {LOG_O, ENGINE_O, NULL};
print_cmd(args);
execvp(args[0], args);
return 0;