X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fdepends.c;h=a7c353c27d04571473808b67692219cc46cf2b12;hp=7f232233b32b90bda37a9b5f35422e3c399ce6b0;hb=0f6d854ab9d85ac7e4487ff3eee464be6bb528aa;hpb=c221eaae3ca00678135497f52b3eabba5f59130d diff --git a/tools/depends.c b/tools/depends.c index 7f232233..a7c353c2 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -4,11 +4,11 @@ #include #include #include +#include #include "tools.h" #include #include #include -#include #include #include #include @@ -38,9 +38,9 @@ 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; + char *info_c_file, *info, *compiled, *output; size_t len; int fd; @@ -59,32 +59,32 @@ static char *compile_info(const void *ctx, const char *dir) if (close(fd) != 0) return NULL; - ccandir = talloc_dirname(ctx, dir); - if (strrchr(ccandir, '/')) - *strrchr(ccandir, '/') = '\0'; - compiled = temp_file(ctx, "", "info"); - if (compile_and_link(ctx, info_c_file, ccandir, "", + if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "", CCAN_COMPILER, CCAN_CFLAGS " -I.", "", compiled, &output)) return compiled; 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, const char *style, + 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 %s", infofile, style); deps = lines_from_cmd(cmd, "%s", cmd); - if (!deps) - err(1, "Could not run '%s'", cmd); + if (!deps) { + /* You must understand depends, maybe not testdepends. */ + if (streq(style, "depends")) + err(1, "Could not run '%s'", cmd); + deps = talloc(ctx, char *); + deps[0] = NULL; + } return deps; } @@ -118,10 +118,12 @@ 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) + const char *style, + char *(*unused)(const void *, const char *)) { char **deps, **lines, *raw, *fname; unsigned int i, n; + bool correct_style = false; fname = talloc_asprintf(ctx, "%s/_info", dir); raw = grab_file(fname, fname, NULL); @@ -141,6 +143,14 @@ static char **get_one_safe_deps(const void *ctx, if (lines[i][0] == '#') continue; + if (strstr(lines[i], "\"testdepends\"")) + correct_style = streq(style, "testdepends"); + else if (strstr(lines[i], "\"depends\"")) + correct_style = streq(style, "depends"); + + if (!correct_style) + continue; + /* Start of line, or after ". */ if (strstarts(lines[i], "ccan/")) str = lines[i]; @@ -177,27 +187,26 @@ 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 **)) +get_all_deps(const void *ctx, const char *dir, const char *style, + char *(*get_info)(const void *ctx, const char *dir), + char **(*get_one)(const void *, const char *, 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, style, 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/")) continue; subdir = talloc_asprintf(ctx, "%s/%s", - talloc_dirname(ctx, dir), - deps[i] + strlen("ccan/")); - newdeps = get_one(ctx, subdir, &subinfo); + find_ccan_dir(dir), deps[i]); + newdeps = get_one(ctx, subdir, "depends", get_info); /* Should be short, so brute-force out dups. */ for (j = 0; j < talloc_array_length(newdeps)-1; j++) { @@ -215,23 +224,76 @@ 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; +} + +/* O(n^2) but n is small. */ +static char **add_deps(char **deps1, char **deps2) +{ + unsigned int i, len; + + len = talloc_array_length(deps1); + + for (i = 0; deps2[i]; i++) { + if (have_dep(deps1, deps2[i])) + continue; + deps1 = talloc_realloc(NULL, deps1, char *, len + 1); + deps1[len-1] = talloc_steal(deps1, deps2[i]); + deps1[len++] = NULL; + } + return deps1; +} + +char **get_libs(const void *ctx, const char *dir, const char *style, + 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 (style) { + deps = get_deps(ctx, dir, style, true, get_info); + if (streq(style, "testdepends")) + deps = add_deps(deps, + get_deps(ctx, dir, "depends", 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", + find_ccan_dir(dir), deps[i]); + + 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; } @@ -240,6 +302,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++) { @@ -255,33 +320,28 @@ 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, const char *style, + 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, style, get_info); } else - ret = get_all_deps(ctx, dir, infofile, get_one_deps); + ret = get_all_deps(ctx, dir, style, get_info, get_one_deps); - if (infofile == &temp && temp) { - unlink(temp); - talloc_free(temp); - } return uniquify_deps(ret); } -char **get_safe_ccan_deps(const void *ctx, const char *dir, +char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style, bool recurse) { char **ret; if (!recurse) { - ret = get_one_safe_deps(ctx, dir, NULL); + ret = get_one_safe_deps(ctx, dir, style, NULL); } else { - ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps); + ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps); } return uniquify_deps(ret); }