X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fdepends.c;h=19229661f2e581f2b4cc6fc86b6d95e4cf9862e7;hp=f1e45c50ebc5fb37c1cf8a44becfbd8141c56f2d;hb=fa7a78f9d3553292b5088b1e7a89d63a2393ab2c;hpb=73753f596f5a588b90bc7ec19773173ec36230be diff --git a/tools/depends.c b/tools/depends.c index f1e45c50..19229661 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "tools.h" #include #include @@ -12,7 +13,7 @@ #include #include -static char ** __attribute__((format(printf, 2, 3))) +static char ** PRINTF_FMT(2, 3) lines_from_cmd(const void *ctx, const char *format, ...) { va_list ap; @@ -37,7 +38,7 @@ lines_from_cmd(const void *ctx, const char *format, ...) /* Be careful about trying to compile over running programs (parallel make). * temp_file helps here. */ -static char *compile_info(const void *ctx, const char *dir) +char *compile_info(const void *ctx, const char *dir) { char *info_c_file, *info, *ccandir, *compiled, *output; size_t len; @@ -48,7 +49,7 @@ static char *compile_info(const void *ctx, const char *dir) if (!info) return NULL; - info_c_file = maybe_temp_file(ctx, ".c", false, "_info"); + info_c_file = temp_file(ctx, ".c", "_info"); fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600); if (fd < 0) return NULL; @@ -62,7 +63,7 @@ static char *compile_info(const void *ctx, const char *dir) if (strrchr(ccandir, '/')) *strrchr(ccandir, '/') = '\0'; - compiled = maybe_temp_file(ctx, "", false, "info"); + compiled = temp_file(ctx, "", "info"); if (compile_and_link(ctx, info_c_file, ccandir, "", CCAN_COMPILER, CCAN_CFLAGS " -I.", "", compiled, &output)) @@ -70,17 +71,16 @@ static char *compile_info(const void *ctx, const char *dir) return NULL; } -static char **get_one_deps(const void *ctx, const char *dir, char **infofile) +static char **get_one_deps(const void *ctx, const char *dir, + char *(*get_info)(const void *ctx, const char *dir)) { char **deps, *cmd; + char *infofile = get_info(ctx, dir); - if (!*infofile) { - *infofile = compile_info(ctx, dir); - if (!*infofile) - errx(1, "Could not compile _info for '%s'", dir); - } + if (!infofile) + return NULL; - cmd = talloc_asprintf(ctx, "%s depends", *infofile); + cmd = talloc_asprintf(ctx, "%s depends", infofile); deps = lines_from_cmd(cmd, "%s", cmd); if (!deps) err(1, "Could not run '%s'", cmd); @@ -117,7 +117,7 @@ static char *replace(const void *ctx, const char *src, /* This is a terrible hack. We scan for ccan/ strings. */ static char **get_one_safe_deps(const void *ctx, const char *dir, - char **infofile) + char *(*unused)(const void *, const char *)) { char **deps, **lines, *raw, *fname; unsigned int i, n; @@ -177,17 +177,17 @@ static bool have_dep(char **deps, const char *dep) /* Gets all the dependencies, recursively. */ static char ** get_all_deps(const void *ctx, const char *dir, - char **infofile, - char **(*get_one)(const void *, const char *, char **)) + char *(*get_info)(const void *ctx, const char *dir), + char **(*get_one)(const void *, const char *, + char *(*get_info)(const void *, const char *))) { char **deps; unsigned int i; - deps = get_one(ctx, dir, infofile); + deps = get_one(ctx, dir, get_info); for (i = 0; i < talloc_array_length(deps)-1; i++) { char **newdeps; unsigned int j; - char *subinfo = NULL; char *subdir; if (!strstarts(deps[i], "ccan/")) @@ -196,7 +196,7 @@ get_all_deps(const void *ctx, const char *dir, subdir = talloc_asprintf(ctx, "%s/%s", talloc_dirname(ctx, dir), deps[i] + strlen("ccan/")); - newdeps = get_one(ctx, subdir, &subinfo); + newdeps = get_one(ctx, subdir, get_info); /* Should be short, so brute-force out dups. */ for (j = 0; j < talloc_array_length(newdeps)-1; j++) { @@ -214,23 +214,55 @@ get_all_deps(const void *ctx, const char *dir, return deps; } -char **get_libs(const void *ctx, const char *dir, - unsigned int *num, char **infofile) +/* Can return NULL: _info may not support 'libs'. */ +static char **get_one_libs(const void *ctx, const char *dir, + char *(*get_info)(const void *ctx, const char *dir)) +{ + char *cmd, **lines; + + cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir)); + lines = lines_from_cmd(cmd, "%s", cmd); + /* Strip final NULL. */ + if (lines) + lines = talloc_realloc(NULL, lines, char *, + talloc_array_length(lines)-1); + return lines; +} + +char **get_libs(const void *ctx, const char *dir, bool recurse, + char *(*get_info)(const void *ctx, const char *dir)) { - char **libs, *cmd; + char **deps, **libs; + unsigned int i, len; + + libs = get_one_libs(ctx, dir, get_info); + len = talloc_array_length(libs); + + if (recurse) { + deps = get_deps(ctx, dir, true, get_info); + for (i = 0; deps[i]; i++) { + char **newlibs, *subdir; + size_t newlen; + + if (!strstarts(deps[i], "ccan/")) + continue; - if (!*infofile) { - *infofile = compile_info(ctx, dir); - if (!*infofile) - errx(1, "Could not compile _info for '%s'", dir); + subdir = talloc_asprintf(ctx, "%s/%s", + talloc_dirname(ctx, dir), + deps[i] + strlen("ccan/")); + + newlibs = get_one_libs(ctx, subdir, get_info); + newlen = talloc_array_length(newlibs); + libs = talloc_realloc(ctx, libs, char *, len + newlen); + memcpy(&libs[len], newlibs, + sizeof(newlibs[0])*newlen); + len += newlen; + } } - cmd = talloc_asprintf(ctx, "%s libs", *infofile); - libs = lines_from_cmd(cmd, "%s", cmd); - if (!libs) - err(1, "Could not run '%s'", cmd); - /* FIXME: Do we need num arg? */ - *num = talloc_array_length(libs) - 1; + /* Append NULL entry. */ + libs = talloc_realloc(ctx, libs, char *, len + 1); + libs[len] = NULL; return libs; } @@ -239,6 +271,9 @@ static char **uniquify_deps(char **deps) { unsigned int i, j, num; + if (!deps) + return NULL; + num = talloc_array_length(deps) - 1; for (i = 0; i < num; i++) { for (j = i + 1; j < num; j++) { @@ -254,22 +289,16 @@ static char **uniquify_deps(char **deps) return talloc_realloc(NULL, deps, char *, num + 1); } -char **get_deps(const void *ctx, const char *dir, - bool recurse, char **infofile) +char **get_deps(const void *ctx, const char *dir, bool recurse, + char *(*get_info)(const void *ctx, const char *dir)) { - char *temp = NULL, **ret; - if (!infofile) - infofile = &temp; + char **ret; if (!recurse) { - ret = get_one_deps(ctx, dir, infofile); + ret = get_one_deps(ctx, dir, get_info); } else - ret = get_all_deps(ctx, dir, infofile, get_one_deps); + ret = get_all_deps(ctx, dir, get_info, get_one_deps); - if (infofile == &temp && temp) { - unlink(temp); - talloc_free(temp); - } return uniquify_deps(ret); }