#include #include #include #include #include #include #include #include typedef struct thread_data { char *f1; char *f2; } thread_data; void *f1_newer(void *arg) { thread_data *files = (thread_data *) arg; char *f1 = files->f1; char *f2 = files->f2; struct stat f1_attr; struct stat f2_attr; stat(f1, &f1_attr); stat(f2, &f2_attr); int diff = difftime(f1_attr.st_mtime, f2_attr.st_mtime); int ret = 0; if (diff <= 0) { pthread_exit(&ret); } ret = 1; pthread_exit(&ret); } 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() { thread_data files; files.f1 = "main.c"; files.f2 = "./bin/debug"; pthread_t f_thread, check_thread; pthread_create(&f_thread, NULL, create_make, NULL); pthread_create(&check_thread, NULL, f1_newer, (void *)&files); mkdir("./bin", 0777); char *source = "main.c"; char *bin = "./bin/debug"; void *newer; pthread_join(f_thread, NULL); pthread_join(check_thread, &newer); int recomp = *(int *) newer; if (recomp) { 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/tmp", "tmp", NULL); } waitpid(x_pid, NULL, 0); return 0; }