X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fdepends.c;h=06e87c32e248d94223f9a67bb8bbdde9f6383745;hp=f416257a67775a5ecfd6700ee28d1afcc666c1b4;hb=30b5119b127b739e369cf497d830e8f2a97591f7;hpb=37ca11df87fa3cc97aca321a76e564e4058d6900 diff --git a/tools/depends.c b/tools/depends.c index f416257a..06e87c32 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -3,6 +3,7 @@ #include "tools.h" #include #include +#include static char ** __attribute__((format(printf, 3, 4))) lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) @@ -19,19 +20,42 @@ lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) if (!p) err(1, "Executing '%s'", cmd); - buffer = grab_fd(ctx, fileno(p)); + buffer = grab_fd(ctx, fileno(p), NULL); if (!buffer) err(1, "Reading from '%s'", cmd); pclose(p); - return split(ctx, buffer, "\n", num); + return strsplit(ctx, buffer, "\n", num); +} + +static int unlink_info(char *infofile) +{ + unlink(infofile); + return 0; +} + +/* Be careful about trying to compile over running programs (parallel make) */ +static char *compile_info(const void *ctx, const char *dir) +{ + char *infofile = talloc_asprintf(ctx, "%s/_info.%u", dir, getpid()); + char *cmd = talloc_asprintf(ctx, "cc " CFLAGS " -o %s %s/_info.c", + infofile, dir); + talloc_set_destructor(infofile, unlink_info); + if (system(cmd) != 0) + return NULL; + + return infofile; } static char **get_one_deps(const void *ctx, const char *dir, unsigned int *num) { - char **deps, *cmd; + char **deps, *cmd, *infofile; + + infofile = compile_info(ctx, dir); + if (!infofile) + errx(1, "Could not compile _info for '%s'", dir); - cmd = talloc_asprintf(ctx, "%s/_info depends", dir); + cmd = talloc_asprintf(ctx, "%s depends", infofile); deps = lines_from_cmd(cmd, num, "%s", cmd); if (!deps) err(1, "Could not run '%s'", cmd);