1 #include <ccan/str/str.h>
2 #include <ccan/talloc/talloc.h>
3 #include <ccan/grab_file/grab_file.h>
4 #include <ccan/str_talloc/str_talloc.h>
5 #include <ccan/read_write_all/read_write_all.h>
6 #include <ccan/compiler/compiler.h>
16 static char ** PRINTF_FMT(2, 3)
17 lines_from_cmd(const void *ctx, const char *format, ...)
24 cmd = talloc_vasprintf(ctx, format, ap);
29 err(1, "Executing '%s'", cmd);
31 buffer = grab_fd(ctx, fileno(p), NULL);
33 err(1, "Reading from '%s'", cmd);
36 return strsplit(ctx, buffer, "\n");
39 /* Be careful about trying to compile over running programs (parallel make).
40 * temp_file helps here. */
41 char *compile_info(const void *ctx, const char *dir)
43 char *info_c_file, *info, *ccandir, *compiled, *output;
47 /* Copy it to a file with proper .c suffix. */
48 info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
52 info_c_file = temp_file(ctx, ".c", "_info");
53 fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
56 if (!write_all(fd, info, len))
62 ccandir = talloc_dirname(ctx, dir);
63 if (strrchr(ccandir, '/'))
64 *strrchr(ccandir, '/') = '\0';
66 compiled = temp_file(ctx, "", "info");
67 if (compile_and_link(ctx, info_c_file, ccandir, "",
68 CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
74 static char **get_one_deps(const void *ctx, const char *dir, const char *style,
75 char *(*get_info)(const void *ctx, const char *dir))
78 char *infofile = get_info(ctx, dir);
83 cmd = talloc_asprintf(ctx, "%s %s", infofile, style);
84 deps = lines_from_cmd(cmd, "%s", cmd);
86 /* You must understand depends, maybe not testdepends. */
87 if (streq(style, "depends"))
88 err(1, "Could not run '%s'", cmd);
89 deps = talloc(ctx, char *);
95 /* Make copy of src, replacing "from" with "to". */
96 static char *replace(const void *ctx, const char *src,
97 const char *from, const char *to)
99 char *ret = talloc_strdup(ctx, "");
100 unsigned int rlen, len, add;
104 const char *next = strstr(src+len, from);
106 add = strlen(src+len) + 1;
108 add = next - (src+len);
110 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
111 memcpy(ret+rlen, src+len, add);
116 strcpy(ret+rlen, to);
122 /* This is a terrible hack. We scan for ccan/ strings. */
123 static char **get_one_safe_deps(const void *ctx,
126 char *(*unused)(const void *, const char *))
128 char **deps, **lines, *raw, *fname;
130 bool correct_style = false;
132 fname = talloc_asprintf(ctx, "%s/_info", dir);
133 raw = grab_file(fname, fname, NULL);
135 errx(1, "Could not open %s", fname);
137 /* Replace \n by actual line breaks, and split it. */
138 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
140 deps = talloc_array(ctx, char *, talloc_array_length(lines));
142 for (n = i = 0; lines[i]; i++) {
146 /* Ignore lines starting with # (e.g. #include) */
147 if (lines[i][0] == '#')
150 if (strstr(lines[i], "\"testdepends\""))
151 correct_style = streq(style, "testdepends");
152 else if (strstr(lines[i], "\"depends\""))
153 correct_style = streq(style, "depends");
158 /* Start of line, or after ". */
159 if (strstarts(lines[i], "ccan/"))
162 str = strstr(lines[i], "\"ccan/");
168 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
171 deps[n++] = talloc_strndup(deps, str, len);
176 /* Make sure talloc_array_length() works */
177 return talloc_realloc(NULL, deps, char *, n + 1);
180 static bool have_dep(char **deps, const char *dep)
184 for (i = 0; deps[i]; i++)
185 if (streq(deps[i], dep))
192 /* Gets all the dependencies, recursively. */
194 get_all_deps(const void *ctx, const char *dir, const char *style,
195 char *(*get_info)(const void *ctx, const char *dir),
196 char **(*get_one)(const void *, const char *, const char *,
197 char *(*get_info)(const void *, const char *)))
202 deps = get_one(ctx, dir, style, get_info);
203 for (i = 0; i < talloc_array_length(deps)-1; i++) {
208 if (!strstarts(deps[i], "ccan/"))
211 subdir = talloc_asprintf(ctx, "%s/%s",
212 talloc_dirname(ctx, dir),
213 deps[i] + strlen("ccan/"));
214 newdeps = get_one(ctx, subdir, "depends", get_info);
216 /* Should be short, so brute-force out dups. */
217 for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
220 if (have_dep(deps, newdeps[j]))
223 num = talloc_array_length(deps)-1;
224 deps = talloc_realloc(NULL, deps, char *, num + 2);
225 deps[num] = newdeps[j];
232 /* Can return NULL: _info may not support 'libs'. */
233 static char **get_one_libs(const void *ctx, const char *dir,
234 char *(*get_info)(const void *ctx, const char *dir))
238 cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
239 lines = lines_from_cmd(cmd, "%s", cmd);
240 /* Strip final NULL. */
242 lines = talloc_realloc(NULL, lines, char *,
243 talloc_array_length(lines)-1);
247 /* O(n^2) but n is small. */
248 static char **add_deps(char **deps1, char **deps2)
252 len = talloc_array_length(deps1);
254 for (i = 0; deps2[i]; i++) {
255 if (have_dep(deps1, deps2[i]))
257 deps1 = talloc_realloc(NULL, deps1, char *, len + 1);
258 deps1[len-1] = talloc_steal(deps1, deps2[i]);
264 char **get_libs(const void *ctx, const char *dir, const char *style,
265 char *(*get_info)(const void *ctx, const char *dir))
270 libs = get_one_libs(ctx, dir, get_info);
271 len = talloc_array_length(libs);
274 deps = get_deps(ctx, dir, style, true, get_info);
275 if (streq(style, "testdepends"))
276 deps = add_deps(deps,
277 get_deps(ctx, dir, "depends", true,
280 for (i = 0; deps[i]; i++) {
281 char **newlibs, *subdir;
284 if (!strstarts(deps[i], "ccan/"))
287 subdir = talloc_asprintf(ctx, "%s/%s",
288 talloc_dirname(ctx, dir),
289 deps[i] + strlen("ccan/"));
291 newlibs = get_one_libs(ctx, subdir, get_info);
292 newlen = talloc_array_length(newlibs);
293 libs = talloc_realloc(ctx, libs, char *, len + newlen);
294 memcpy(&libs[len], newlibs,
295 sizeof(newlibs[0])*newlen);
300 /* Append NULL entry. */
301 libs = talloc_realloc(ctx, libs, char *, len + 1);
306 /* FIXME: This is O(n^2), which is dumb. */
307 static char **uniquify_deps(char **deps)
309 unsigned int i, j, num;
314 num = talloc_array_length(deps) - 1;
315 for (i = 0; i < num; i++) {
316 for (j = i + 1; j < num; j++) {
317 if (streq(deps[i], deps[j])) {
318 memmove(&deps[j], &deps[j+1],
319 (num - j - 1) * sizeof(char *));
325 /* Make sure talloc_array_length() works */
326 return talloc_realloc(NULL, deps, char *, num + 1);
329 char **get_deps(const void *ctx, const char *dir, const char *style,
331 char *(*get_info)(const void *ctx, const char *dir))
336 ret = get_one_deps(ctx, dir, style, get_info);
338 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
340 return uniquify_deps(ret);
343 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
348 ret = get_one_safe_deps(ctx, dir, style, NULL);
350 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
352 return uniquify_deps(ret);