+ for (j = 0; j < tal_count(newdeps)-1; j++) {
+ unsigned int num;
+
+ if (have_dep(deps, newdeps[j]))
+ continue;
+
+ num = tal_count(deps)-1;
+ tal_resize(&deps, num + 2);
+ deps[num] = newdeps[j];
+ deps[num+1] = NULL;
+ }
+ }
+ return deps;
+}
+
+/* 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 = tal_fmt(ctx, "%s libs", get_info(ctx, dir));
+ lines = lines_from_cmd(cmd, "%s", cmd);
+ /* Strip final NULL. */
+ if (lines)
+ tal_resize(&lines, tal_count(lines)-1);
+ return lines;
+}
+
+/* O(n^2) but n is small. */
+static char **add_deps(char **deps1, char **deps2)
+{
+ unsigned int i, len;
+
+ len = tal_count(deps1);
+
+ for (i = 0; deps2[i]; i++) {
+ if (have_dep(deps1, deps2[i]))
+ continue;
+ tal_resize(&deps1, len + 1);
+ deps1[len-1] = tal_strdup(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 **deps, **libs;
+ unsigned int i, len;
+
+ libs = get_one_libs(ctx, dir, get_info);
+ len = tal_count(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/"))