1 #include <ccan/str/str.h>
2 #include <ccan/read_write_all/read_write_all.h>
3 #include <ccan/rbuf/rbuf.h>
4 #include <ccan/tal/path/path.h>
5 #include <ccan/tal/grab_file/grab_file.h>
6 #include <ccan/compiler/compiler.h>
7 #include <ccan/err/err.h>
16 static char ** PRINTF_FMT(2, 3)
17 lines_from_cmd(const void *ctx, const char *format, ...)
26 cmd = tal_vfmt(ctx, format, ap);
31 err(1, "Executing '%s'", cmd);
33 /* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */
34 rbuf_init(&in, fileno(p), tal_arr(ctx, char, 0), 0, membuf_tal_realloc);
35 str = rbuf_read_str(&in, 0);
37 err(1, "Reading from '%s'", cmd);
40 return tal_strsplit(ctx, str, "\n", STR_EMPTY_OK);
43 /* Be careful about trying to compile over running programs (parallel make).
44 * temp_file helps here. */
45 char *compile_info(const void *ctx, const char *dir)
47 char *info_c_file, *info, *compiled, *output;
50 /* Copy it to a file with proper .c suffix. */
51 info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
55 info_c_file = temp_file(ctx, ".c", "_info");
56 fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
59 if (!write_all(fd, info, tal_count(info)-1)) {
67 compiled = temp_file(ctx, "", "info");
68 if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
69 compiler, cflags, "", compiled, &output))
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 = tal_fmt(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 = tal(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 = tal_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 tal_resize(&ret, 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 = path_join(ctx, dir, "_info");
133 raw = grab_file(fname, fname);
135 errx(1, "Could not open %s", fname);
137 /* Replace \n by actual line breaks, and split it. */
138 lines = tal_strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n",
141 deps = tal_arr(ctx, char *, tal_count(lines));
143 for (n = i = 0; lines[i]; i++) {
147 /* Ignore lines starting with # (e.g. #include) */
148 if (lines[i][0] == '#')
151 if (strstr(lines[i], "\"testdepends\""))
152 correct_style = streq(style, "testdepends");
153 else if (strstr(lines[i], "\"depends\""))
154 correct_style = streq(style, "depends");
159 /* Start of line, or after ". */
160 if (strstarts(lines[i], "ccan/"))
163 str = strstr(lines[i], "\"ccan/");
169 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
172 deps[n++] = tal_strndup(deps, str, len);
177 /* Make sure tal_array_length() works */
178 tal_resize(&deps, n + 1);
182 static bool have_dep(char **deps, const char *dep)
186 for (i = 0; deps[i]; i++)
187 if (streq(deps[i], dep))
194 /* Gets all the dependencies, recursively. */
196 get_all_deps(const void *ctx, const char *dir, const char *style,
197 char *(*get_info)(const void *ctx, const char *dir),
198 char **(*get_one)(const void *, const char *, const char *,
199 char *(*get_info)(const void *, const char *)))
204 deps = get_one(ctx, dir, style, get_info);
207 for (i = 0; i < tal_count(deps)-1; i++) {
212 if (!strstarts(deps[i], "ccan/"))
215 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
216 newdeps = get_one(ctx, subdir, "depends", get_info);
218 /* Should be short, so brute-force out dups. */
219 for (j = 0; j < tal_count(newdeps)-1; j++) {
222 if (have_dep(deps, newdeps[j]))
225 num = tal_count(deps)-1;
226 tal_resize(&deps, num + 2);
227 deps[num] = newdeps[j];
234 /* Can return NULL: _info may not support prop. */
235 static char **get_one_prop(const void *ctx, const char *dir, const char *prop,
236 char *(*get_info)(const void *ctx, const char *dir))
240 cmd = tal_fmt(ctx, "%s %s", get_info(ctx, dir), prop);
241 lines = lines_from_cmd(cmd, "%s", cmd);
242 /* Strip final NULL. */
244 tal_resize(&lines, tal_count(lines)-1);
248 static char **get_one_libs(const void *ctx, const char *dir,
249 char *(*get_info)(const void *ctx, const char *dir))
251 return get_one_prop(ctx, dir, "libs", get_info);
254 static char **get_one_cflags(const void *ctx, const char *dir,
255 char *(*get_info)(const void *ctx, const char *dir))
257 return get_one_prop(ctx, dir, "cflags", get_info);
260 static char **get_one_ccanlint(const void *ctx, const char *dir,
261 char *(*get_info)(const void *ctx, const char *dir))
263 return get_one_prop(ctx, dir, "ccanlint", get_info);
266 /* O(n^2) but n is small. */
267 static char **add_deps(char **deps1, char **deps2)
271 len = tal_count(deps1);
273 for (i = 0; deps2[i]; i++) {
274 if (have_dep(deps1, deps2[i]))
276 tal_resize(&deps1, len + 1);
277 deps1[len-1] = tal_strdup(deps1, deps2[i]);
283 char **get_cflags(const void *ctx, const char *dir,
284 char *(*get_info)(const void *ctx, const char *dir))
288 flags = get_one_cflags(ctx, dir, get_info);
289 len = tal_count(flags);
290 tal_resize(&flags, len + 1);
295 char **get_ccanlint(const void *ctx, const char *dir,
296 char *(*get_info)(const void *ctx, const char *dir))
300 ccanlint = get_one_ccanlint(ctx, dir, get_info);
301 len = tal_count(ccanlint);
302 tal_resize(&ccanlint, len + 1);
303 ccanlint[len] = NULL;
307 static char *get_one_ported(const void *ctx, const char *dir,
308 char *(*get_info)(const void *ctx, const char *dir))
310 char **ported = get_one_prop(ctx, dir, "ported", get_info);
312 /* No news is good news. */
313 if (tal_count(ported) == 0)
316 if (tal_count(ported) != 1)
317 errx(1, "%s/_info ported gave %zu lines, not one",
318 dir, tal_count(ported));
320 if (streq(ported[0], ""))
326 char *get_ported(const void *ctx, const char *dir, bool recurse,
327 char *(*get_info)(const void *ctx, const char *dir))
331 msg = get_one_ported(ctx, dir, get_info);
337 char **deps = get_deps(ctx, dir, "depends", true, get_info);
338 for (i = 0; deps[i]; i++) {
340 if (!strstarts(deps[i], "ccan/"))
343 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
344 msg = get_one_ported(ctx, subdir, get_info);
352 char **get_libs(const void *ctx, const char *dir, const char *style,
353 char *(*get_info)(const void *ctx, const char *dir))
358 libs = get_one_libs(ctx, dir, get_info);
359 len = tal_count(libs);
362 deps = get_deps(ctx, dir, style, true, get_info);
363 if (streq(style, "testdepends"))
364 deps = add_deps(deps,
365 get_deps(ctx, dir, "depends", true,
368 for (i = 0; deps[i]; i++) {
369 char **newlibs, *subdir;
372 if (!strstarts(deps[i], "ccan/"))
375 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
377 newlibs = get_one_libs(ctx, subdir, get_info);
378 newlen = tal_count(newlibs);
379 tal_resize(&libs, len + newlen);
380 memcpy(&libs[len], newlibs,
381 sizeof(newlibs[0])*newlen);
386 /* Append NULL entry. */
387 tal_resize(&libs, len + 1);
392 /* FIXME: This is O(n^2), which is dumb. */
393 static char **uniquify_deps(char **deps)
395 unsigned int i, j, num;
400 num = tal_count(deps) - 1;
401 for (i = 0; i < num; i++) {
402 for (j = i + 1; j < num; j++) {
403 if (streq(deps[i], deps[j])) {
404 memmove(&deps[j], &deps[j+1],
405 (num - j - 1) * sizeof(char *));
411 /* Make sure tal_count() works */
412 tal_resize(&deps, num + 1);
416 char **get_deps(const void *ctx, const char *dir, const char *style,
418 char *(*get_info)(const void *ctx, const char *dir))
423 ret = get_one_deps(ctx, dir, style, get_info);
425 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
427 return uniquify_deps(ret);
430 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
435 ret = get_one_safe_deps(ctx, dir, style, NULL);
437 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
439 return uniquify_deps(ret);