Skip to content

Instantly share code, notes, and snippets.

@aprell
Created October 7, 2019 15:49
Show Gist options
  • Save aprell/8e1c95d93f51ad9ad0514f2b19a7c1d2 to your computer and use it in GitHub Desktop.
Save aprell/8e1c95d93f51ad9ad0514f2b19a7c1d2 to your computer and use it in GitHub Desktop.
// https://rain-1.github.io/why/why.html
// https://github.com/rain-1/makes/tree/master/previous-version/makes.c
//
// Example usage:
// makes foo foo.c foo.h && gcc -Wall -Wextra -o foo foo.c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define NEXT do { argc--; argv++; } while (0)
#define VERBOSE if (verbose)
static const char *usage =
"Usage: %s [OPTION] target prereq [prereq...]\n"
"\n"
"OPTIONS:\n"
"\t -h Print this help and exit\n"
"\t -v Increase verbosity\n";
int main(int argc, const char *argv[])
{
const char *name = argv[0];
bool verbose = false;
bool rebuild = false;
struct stat s1, s2;
NEXT;
// At least two non-optional arguments (target and prereq)
if (argc < 2 || !strcmp("-h", argv[0])) {
printf(usage, name);
exit(EXIT_SUCCESS);
}
if (!strcmp("-v", argv[0])) {
if (argc < 3) {
printf(usage, name);
exit(EXIT_SUCCESS);
}
verbose = 1;
NEXT;
}
if (getenv("MAKES_VERBOSE")) {
verbose = 1;
}
int ret;
if ((ret = stat(argv[0], &s1)) == -1) {
VERBOSE fprintf(stderr, "[makes] target \"%s\" does not exist\n", argv[0]);
rebuild = 1;
}
for (int i = 1; i < argc; i++) {
if ((ret = stat(argv[i], &s2)) == 0) {
if (s2.st_mtime > s1.st_mtime) {
VERBOSE fprintf(stderr, "[makes] prerequisite \"%s\" is newer\n", argv[i]);
rebuild = 1;
}
} else {
perror("stat");
exit(EXIT_FAILURE);
}
}
return !rebuild;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment