From de014b1fe6f14678f0b10077688c23b3f26f79f9 Mon Sep 17 00:00:00 2001 From: aspiring_aurelian Date: Fri, 8 Dec 2023 07:34:18 -0600 Subject: Working version, only debug --- .gitignore | 1 + main.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- makefile | 4 +++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ba077a4..0e9ae81 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bin +tmp.c diff --git a/main.c b/main.c index 7380acf..06fdcce 100644 --- a/main.c +++ b/main.c @@ -1,14 +1,60 @@ #include #include +#include +#include +#include #include +#include +#include -int main() { - int fd = creat("makefile", S_IRWXU | S_IXGRP | S_IXOTH); +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"); + 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; } diff --git a/makefile b/makefile index 9e8eafd..239005f 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,4 @@ debug: main.c - gcc -Wall main.c -o ./bin/debug \ No newline at end of file + gcc -Wall main.c -o ./bin/debug +release: main.c + gcc -O2 -march-native main.c -o ./bin/release \ No newline at end of file -- cgit v1.2.3