Skip to content

Instantly share code, notes, and snippets.

@rexim
Created September 5, 2019 22:40
Show Gist options
  • Save rexim/9cf36838406fe36806ad4eda494898c6 to your computer and use it in GitHub Desktop.
Save rexim/9cf36838406fe36806ad4eda494898c6 to your computer and use it in GitHub Desktop.
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