From 37f4b53b9a6f33b70f9de3846d966f1fa76b20bf Mon Sep 17 00:00:00 2001 From: aspiring_aurelian Date: Fri, 8 Dec 2023 13:52:37 -0600 Subject: Add args and very basic parsing --- main.c | 46 ++++++++++++++++++++++++++++++++++++++-------- makefile | 2 +- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index b3d9eea..1024d17 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -40,12 +41,30 @@ void *create_make() { } -int main() { +int main(int argc, char *argv[]) { /* Struct to hold files */ f_comp_data files; files.f1 = "main.c"; files.f2 = "./bin/debug"; + /* Parse args to determine which mode to run in + * Not very robust, needs improvement + */ + enum { RUN, BUILD, BUILD_RELEASE } mode = RUN; + if (argc > 1) { + switch(argv[1][0]) { + case 'r': break; + case 'b': + if (argc > 2) { + mode = BUILD_RELEASE; + files.f2 = "./bin/release"; + break; + } + mode = BUILD; break; + default: printf("Unknown argument: '%s'\nDefaulting to 'run'\n", argv[1]); + } + } + /* Threads to: * create makefile * check age of source vs. bin @@ -56,9 +75,13 @@ int main() { mkdir("./bin", 0777); - void *newer; pthread_join(f_thread, NULL); + + /* Get value from thread which checks if the bin + * is older than the source code + */ + void *newer; pthread_join(check_thread, &newer); int recomp = *(int *) newer; @@ -66,17 +89,24 @@ int main() { pid_t make_pid; if ((make_pid = fork()) == 0) { printf("Recompiling...\n"); - execl("/usr/bin/make", "make", "debug", NULL); + switch (mode) { + case BUILD_RELEASE: + execl("/usr/bin/make", "make", "release", NULL); + break; + default: + 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); + if (mode == RUN) { + pid_t x_pid; + if ((x_pid = fork()) == 0) { + execl(files.f2, "tmp", NULL); + } + waitpid(x_pid, NULL, 0); } - waitpid(x_pid, NULL, 0); - return 0; } diff --git a/makefile b/makefile index 429650c..6f51c67 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ debug: main.c gcc -Wall main.c -o ./bin/debug release: main.c - gcc -O2 -march=native main.c -o ./bin/release + gcc -O2 -march=native main.c -o ./bin/release \ No newline at end of file -- cgit v1.2.3