Created
September 5, 2019 22:40
-
-
Save rexim/9cf36838406fe36806ad4eda494898c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void rebuild_if_modified(int argc, char *argv[]) | |
{ | |
assert(argc > 0); | |
const char *binary = argv[0]; | |
const char *source = __FILE__; | |
struct stat source_stat; | |
stat(source, &source_stat); | |
struct stat binary_stat; | |
stat(binary, &binary_stat); | |
if (source_stat.st_mtime > binary_stat.st_mtime) { | |
printf("%s is modified. Rebuilding myself...\n", source); | |
pid_t pid = fork(); | |
if (pid) { | |
int status = 0; | |
waitpid(pid, &status, 0); | |
execlp(binary, binary, NULL); | |
} else { | |
printf("Building build\n"); | |
execlp("gcc", "gcc", "-o", binary, source, NULL); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment