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
|
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
int f1_newer(char *f1, char *f2) {
struct stat f1_attr;
struct stat f2_attr;
stat(f1, &f1_attr);
stat(f2, &f2_attr);
if (difftime(f1_attr.st_mtime, f2_attr.st_mtime) <= 0) {
return 0;
}
return 1;
}
void *create_make() {
/* Create simple makefile */
int fd = creat("makefile", 00644);
FILE *file = fdopen(fd, "w");
fprintf(file, "debug: main.c\n\tgcc -Wall main.c -o ./bin/debug\nrelease: main.c\n\tgcc -O2 -march-native main.c -o ./bin/release");
fclose(file);
return NULL;
}
int main() {
pthread_t f_thread;
pthread_create(&f_thread, NULL, create_make, NULL);
mkdir("./bin", 0777);
char *source = "main.c";
char *bin = "./bin/debug";
pthread_join(f_thread, NULL);
if (f1_newer(source, bin)) {
pid_t make_pid;
if ((make_pid = fork()) == 0) {
printf("Recompiling...\n");
execl("/usr/bin/make", "make", "debug", NULL);
}
waitpid(make_pid, NULL, 0);
}
pid_t x_pid;
if ((x_pid = fork()) == 0) {
execl("./bin/debug", "debug", NULL);
}
waitpid(x_pid, NULL, 0);
return 0;
}
|