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, ...)
25 cmd = tal_vfmt(ctx, format, ap);
30 err(1, "Executing '%s'", cmd);
32 /* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */
33 rbuf_init(&in, fileno(p), tal_arr(ctx, char, 0), 0);
34 if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
35 err(1, "Reading from '%s'", cmd);
38 return tal_strsplit(ctx, in.buf, "\n", STR_EMPTY_OK);
41 /* Be careful about trying to compile over running programs (parallel make).
42 * temp_file helps here. */
43 char *compile_info(const void *ctx, const char *dir)
45 char *info_c_file, *info, *compiled, *output;
48 /* Copy it to a file with proper .c suffix. */
49 info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
53 info_c_file = temp_file(ctx, ".c", "_info");
54 fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
57 if (!write_all(fd, info, tal_count(info)-1)) {
65 compiled = temp_file(ctx, "", "info");
66 if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
67 compiler, cflags, "", compiled, &output))
72 static char **get_one_deps(const void *ctx, const char *dir, const char *style,
73 char *(*get_info)(const void *ctx, const char *dir))
76 char *infofile = get_info(ctx, dir);
81 cmd = tal_fmt(ctx, "%s %s", infofile, style);
82 deps = lines_from_cmd(cmd, "%s", cmd);
84 /* You must understand depends, maybe not testdepends. */
85 if (streq(style, "depends"))
86 err(1, "Could not run '%s'", cmd);
87 deps = tal(ctx, char *);
93 /* Make copy of src, replacing "from" with "to". */
94 static char *replace(const void *ctx, const char *src,
95 const char *from, const char *to)
97 char *ret = tal_strdup(ctx, "");
98 unsigned int rlen, len, add;
102 const char *next = strstr(src+len, from);
104 add = strlen(src+len) + 1;
106 add = next - (src+len);
108 tal_resize(&ret, rlen + add + strlen(to)+1);
109 memcpy(ret+rlen, src+len, add);
114 strcpy(ret+rlen, to);
120 /* This is a terrible hack. We scan for ccan/ strings. */
121 static char **get_one_safe_deps(const void *ctx,
124 char *(*unused)(const void *, const char *))
126 char **deps, **lines, *raw, *fname;
128 bool correct_style = false;
130 fname = path_join(ctx, dir, "_info");
131 raw = grab_file(fname, fname);
133 errx(1, "Could not open %s", fname);
135 /* Replace \n by actual line breaks, and split it. */
136 lines = tal_strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n",
139 deps = tal_arr(ctx, char *, tal_count(lines));
141 for (n = i = 0; lines[i]; i++) {
145 /* Ignore lines starting with # (e.g. #include) */
146 if (lines[i][0] == '#')
149 if (strstr(lines[i], "\"testdepends\""))
150 correct_style = streq(style, "testdepends");
151 else if (strstr(lines[i], "\"depends\""))
152 correct_style = streq(style, "depends");
157 /* Start of line, or after ". */
158 if (strstarts(lines[i], "ccan/"))
161 str = strstr(lines[i], "\"ccan/");
167 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
170 deps[n++] = tal_strndup(deps, str, len);
175 /* Make sure tal_array_length() works */
176 tal_resize(&deps, 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);
205 for (i = 0; i < tal_count(deps)-1; i++) {
210 if (!strstarts(deps[i], "ccan/"))
213 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
214 newdeps = get_one(ctx, subdir, "depends", get_info);
216 /* Should be short, so brute-force out dups. */
217 for (j = 0; j < tal_count(newdeps)-1; j++) {
220 if (have_dep(deps, newdeps[j]))
223 num = tal_count(deps)-1;
224 tal_resize(&deps, num + 2);
225 deps[num] = newdeps[j];
232 /* Can return NULL: _info may not support prop. */
233 static char **get_one_prop(const void *ctx, const char *dir, const char *prop,
234 char *(*get_info)(const void *ctx, const char *dir))
238 cmd = tal_fmt(ctx, "%s %s", get_info(ctx, dir), prop);
239 lines = lines_from_cmd(cmd, "%s", cmd);
240 /* Strip final NULL. */
242 tal_resize(&lines, tal_count(lines)-1);
246 static char **get_one_libs(const void *ctx, const char *dir,
247 char *(*get_info)(const void *ctx, const char *dir))
249 return get_one_prop(ctx, dir, "libs", get_info);
252 static char **get_one_cflags(const void *ctx, const char *dir,
253 char *(*get_info)(const void *ctx, const char *dir))
255 return get_one_prop(ctx, dir, "cflags", get_info);
258 static char **get_one_ccanlint(const void *ctx, const char *dir,
259 char *(*get_info)(const void *ctx, const char *dir))
261 return get_one_prop(ctx, dir, "ccanlint", get_info);
264 /* O(n^2) but n is small. */
265 static char **add_deps(char **deps1, char **deps2)
269 len = tal_count(deps1);
271 for (i = 0; deps2[i]; i++) {
272 if (have_dep(deps1, deps2[i]))
274 tal_resize(&deps1, len + 1);
275 deps1[len-1] = tal_strdup(deps1, deps2[i]);
281 char **get_cflags(const void *ctx, const char *dir,
282 char *(*get_info)(const void *ctx, const char *dir))
286 flags = get_one_cflags(ctx, dir, get_info);
287 len = tal_count(flags);
288 tal_resize(&flags, len + 1);
293 char **get_ccanlint(const void *ctx, const char *dir,
294 char *(*get_info)(const void *ctx, const char *dir))
298 ccanlint = get_one_ccanlint(ctx, dir, get_info);
299 len = tal_count(ccanlint);
300 tal_resize(&ccanlint, len + 1);
301 ccanlint[len] = NULL;
305 static char *get_one_ported(const void *ctx, const char *dir,
306 char *(*get_info)(const void *ctx, const char *dir))
308 char **ported = get_one_prop(ctx, dir, "ported", get_info);
310 /* No news is good news. */
311 if (tal_count(ported) == 0)
314 if (tal_count(ported) != 1)
315 errx(1, "%s/_info ported gave %zu lines, not one",
316 dir, tal_count(ported));
318 if (streq(ported[0], ""))
324 char *get_ported(const void *ctx, const char *dir, bool recurse,
325 char *(*get_info)(const void *ctx, const char *dir))
329 msg = get_one_ported(ctx, dir, get_info);
335 char **deps = get_deps(ctx, dir, "depends", true, get_info);
336 for (i = 0; deps[i]; i++) {
338 if (!strstarts(deps[i], "ccan/"))
341 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
342 msg = get_one_ported(ctx, subdir, get_info);
350 char **get_libs(const void *ctx, const char *dir, const char *style,
351 char *(*get_info)(const void *ctx, const char *dir))
356 libs = get_one_libs(ctx, dir, get_info);
357 len = tal_count(libs);
360 deps = get_deps(ctx, dir, style, true, get_info);
361 if (streq(style, "testdepends"))
362 deps = add_deps(deps,
363 get_deps(ctx, dir, "depends", true,
366 for (i = 0; deps[i]; i++) {
367 char **newlibs, *subdir;
370 if (!strstarts(deps[i], "ccan/"))
373 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
375 newlibs = get_one_libs(ctx, subdir, get_info);
376 newlen = tal_count(newlibs);
377 tal_resize(&libs, len + newlen);
378 memcpy(&libs[len], newlibs,
379 sizeof(newlibs[0])*newlen);
384 /* Append NULL entry. */
385 tal_resize(&libs, len + 1);
390 /* FIXME: This is O(n^2), which is dumb. */
391 static char **uniquify_deps(char **deps)
393 unsigned int i, j, num;
398 num = tal_count(deps) - 1;
399 for (i = 0; i < num; i++) {
400 for (j = i + 1; j < num; j++) {
401 if (streq(deps[i], deps[j])) {
402 memmove(&deps[j], &deps[j+1],
403 (num - j - 1) * sizeof(char *));
409 /* Make sure tal_count() works */
410 tal_resize(&deps, num + 1);
414 char **get_deps(const void *ctx, const char *dir, const char *style,
416 char *(*get_info)(const void *ctx, const char *dir))
421 ret = get_one_deps(ctx, dir, style, get_info);
423 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
425 return uniquify_deps(ret);
428 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
433 ret = get_one_safe_deps(ctx, dir, style, NULL);
435 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
437 return uniquify_deps(ret);