1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#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);
}
}
if (argc > 1) {
if (strcmp(argv[1], "test") == 0) {
printf("Test mode\n");
if (fork() == 0) {
char* args[] = {"gcc", "generate/find_tests.c", "-o", "find_tests.o", NULL};
execvp(args[0], args);
return 0;
} else {
wait(NULL);
}
if (fork() == 0) {
char* args[] = {"./find_tests.o", NULL};
execvp(args[0], args);
return 0;
} else {
wait(NULL);
}
printf("\n");
if (fork() == 0) {
char* args[] = {"gcc", "tests/main.c", "-o", "test.o", NULL};
execvp(args[0], args);
return 0;
} else {
wait(NULL);
}
char* args[] = {"./test.o", NULL};
execvp(args[0], args);
return 0;
}
}
if (fork() == 0) {
char* args[] = {"gcc", "src/main.c", "-o", "main.o", NULL};
execvp(args[0], args);
return 0;
} else {
wait(NULL);
}
char* args[] = {"./main.o", NULL};
execvp(args[0], args);
return 0;
}
|