summaryrefslogtreecommitdiff
path: root/build.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-05-22 18:13:07 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-05-22 18:13:07 +0530
commitb0a585e21362137c90a6484de44889d2117d9c7a (patch)
tree1a07edeef988a20d45698585bf03e05339d9af34 /build.c
parent2da12c5862b8250d7c0e609609cb58f97012d619 (diff)
using nob.h rebuild system
Diffstat (limited to 'build.c')
-rw-r--r--build.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/build.c b/build.c
index b8740fc..56c4d74 100644
--- a/build.c
+++ b/build.c
@@ -1,7 +1,41 @@
#include <sys/wait.h>
+#include <sys/stat.h>
#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include <string.h>
+
+// https://github.com/tsoding/nob.h/blob/0a08926d8094fc4ae678155c5d73ae21d1f96f3f/nob.h#L2413
+int needs_rebuild(char* bin_file, char* source_file) {
+ struct stat statbuf = {0};
+ if (stat(bin_file, &statbuf) < 0) {
+ if (errno == ENOENT) return 1;
+ return -1;
+ }
+ time_t output_path_time = statbuf.st_mtime;
+
+ if (stat(source_file, &statbuf) < 0) {
+ fprintf(stderr, "could not stat %s: %s", source_file, strerror(errno));
+ return -1;
+ }
+
+ time_t input_path_time = statbuf.st_mtime;
+ return input_path_time > output_path_time;
+}
+
+int main(int argc, char** argv) {
+ if (needs_rebuild(argv[0], __FILE__)) {
+ if (fork() == 0) {
+ char* args[] = {"gcc", __FILE__, "-o", argv[0], NULL};
+ execvp(args[0], args);
+ return 0;
+ } else {
+ wait(NULL);
+ execvp(argv[0], argv);
+ }
+ }
-int main() {
if (fork() == 0) {
char* args[] = {"gcc", "src/main.c", "-o", "main.o", NULL};
execvp(args[0], args);