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))
63 compiled = temp_file(ctx, "", "info");
64 if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
65 compiler, cflags, "", compiled, &output))
70 static char **get_one_deps(const void *ctx, const char *dir, const char *style,
71 char *(*get_info)(const void *ctx, const char *dir))
74 char *infofile = get_info(ctx, dir);
79 cmd = tal_fmt(ctx, "%s %s", infofile, style);
80 deps = lines_from_cmd(cmd, "%s", cmd);
82 /* You must understand depends, maybe not testdepends. */
83 if (streq(style, "depends"))
84 err(1, "Could not run '%s'", cmd);
85 deps = tal(ctx, char *);
91 /* Make copy of src, replacing "from" with "to". */
92 static char *replace(const void *ctx, const char *src,
93 const char *from, const char *to)
95 char *ret = tal_strdup(ctx, "");
96 unsigned int rlen, len, add;
100 const char *next = strstr(src+len, from);
102 add = strlen(src+len) + 1;
104 add = next - (src+len);
106 tal_resize(&ret, rlen + add + strlen(to)+1);
107 memcpy(ret+rlen, src+len, add);
112 strcpy(ret+rlen, to);
118 /* This is a terrible hack. We scan for ccan/ strings. */
119 static char **get_one_safe_deps(const void *ctx,
122 char *(*unused)(const void *, const char *))
124 char **deps, **lines, *raw, *fname;
126 bool correct_style = false;
128 fname = path_join(ctx, dir, "_info");
129 raw = grab_file(fname, fname);
131 errx(1, "Could not open %s", fname);
133 /* Replace \n by actual line breaks, and split it. */
134 lines = tal_strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n",
137 deps = tal_arr(ctx, char *, tal_count(lines));
139 for (n = i = 0; lines[i]; i++) {
143 /* Ignore lines starting with # (e.g. #include) */
144 if (lines[i][0] == '#')
147 if (strstr(lines[i], "\"testdepends\""))
148 correct_style = streq(style, "testdepends");
149 else if (strstr(lines[i], "\"depends\""))
150 correct_style = streq(style, "depends");
155 /* Start of line, or after ". */
156 if (strstarts(lines[i], "ccan/"))
159 str = strstr(lines[i], "\"ccan/");
165 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
168 deps[n++] = tal_strndup(deps, str, len);
173 /* Make sure tal_array_length() works */
174 tal_resize(&deps, n + 1);
178 static bool have_dep(char **deps, const char *dep)
182 for (i = 0; deps[i]; i++)
183 if (streq(deps[i], dep))
190 /* Gets all the dependencies, recursively. */
192 get_all_deps(const void *ctx, const char *dir, const char *style,
193 char *(*get_info)(const void *ctx, const char *dir),
194 char **(*get_one)(const void *, const char *, const char *,
195 char *(*get_info)(const void *, const char *)))
200 deps = get_one(ctx, dir, style, get_info);
203 for (i = 0; i < tal_count(deps)-1; i++) {
208 if (!strstarts(deps[i], "ccan/"))
211 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
212 newdeps = get_one(ctx, subdir, "depends", get_info);
214 /* Should be short, so brute-force out dups. */
215 for (j = 0; j < tal_count(newdeps)-1; j++) {
218 if (have_dep(deps, newdeps[j]))
221 num = tal_count(deps)-1;
222 tal_resize(&deps, num + 2);
223 deps[num] = newdeps[j];
230 /* Can return NULL: _info may not support prop. */
231 static char **get_one_prop(const void *ctx, const char *dir, const char *prop,
232 char *(*get_info)(const void *ctx, const char *dir))
236 cmd = tal_fmt(ctx, "%s %s", get_info(ctx, dir), prop);
237 lines = lines_from_cmd(cmd, "%s", cmd);
238 /* Strip final NULL. */
240 tal_resize(&lines, tal_count(lines)-1);
244 static char **get_one_libs(const void *ctx, const char *dir,
245 char *(*get_info)(const void *ctx, const char *dir))
247 return get_one_prop(ctx, dir, "libs", get_info);
250 static char **get_one_cflags(const void *ctx, const char *dir,
251 char *(*get_info)(const void *ctx, const char *dir))
253 return get_one_prop(ctx, dir, "cflags", get_info);
256 /* O(n^2) but n is small. */
257 static char **add_deps(char **deps1, char **deps2)
261 len = tal_count(deps1);
263 for (i = 0; deps2[i]; i++) {
264 if (have_dep(deps1, deps2[i]))
266 tal_resize(&deps1, len + 1);
267 deps1[len-1] = tal_strdup(deps1, deps2[i]);
273 char **get_cflags(const void *ctx, const char *dir,
274 char *(*get_info)(const void *ctx, const char *dir))
278 flags = get_one_cflags(ctx, dir, get_info);
279 len = tal_count(flags);
280 tal_resize(&flags, len + 1);
285 static char *get_one_ported(const void *ctx, const char *dir,
286 char *(*get_info)(const void *ctx, const char *dir))
288 char **ported = get_one_prop(ctx, dir, "ported", get_info);
290 /* No news is good news. */
291 if (!ported || tal_count(ported) == 0)
294 if (tal_count(ported) != 1)
295 errx(1, "%s/_info ported gave %zu lines, not one",
296 dir, tal_count(ported));
298 if (streq(ported[0], ""))
304 char *get_ported(const void *ctx, const char *dir, bool recurse,
305 char *(*get_info)(const void *ctx, const char *dir))
309 msg = get_one_ported(ctx, dir, get_info);
315 char **deps = get_deps(ctx, dir, "depends", true, get_info);
316 for (i = 0; deps[i]; i++) {
318 if (!strstarts(deps[i], "ccan/"))
321 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
322 msg = get_one_ported(ctx, subdir, get_info);
330 char **get_libs(const void *ctx, const char *dir, const char *style,
331 char *(*get_info)(const void *ctx, const char *dir))
336 libs = get_one_libs(ctx, dir, get_info);
337 len = tal_count(libs);
340 deps = get_deps(ctx, dir, style, true, get_info);
341 if (streq(style, "testdepends"))
342 deps = add_deps(deps,
343 get_deps(ctx, dir, "depends", true,
346 for (i = 0; deps[i]; i++) {
347 char **newlibs, *subdir;
350 if (!strstarts(deps[i], "ccan/"))
353 subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
355 newlibs = get_one_libs(ctx, subdir, get_info);
356 newlen = tal_count(newlibs);
357 tal_resize(&libs, len + newlen);
358 memcpy(&libs[len], newlibs,
359 sizeof(newlibs[0])*newlen);
364 /* Append NULL entry. */
365 tal_resize(&libs, len + 1);
370 /* FIXME: This is O(n^2), which is dumb. */
371 static char **uniquify_deps(char **deps)
373 unsigned int i, j, num;
378 num = tal_count(deps) - 1;
379 for (i = 0; i < num; i++) {
380 for (j = i + 1; j < num; j++) {
381 if (streq(deps[i], deps[j])) {
382 memmove(&deps[j], &deps[j+1],
383 (num - j - 1) * sizeof(char *));
389 /* Make sure tal_count() works */
390 tal_resize(&deps, num + 1);
394 char **get_deps(const void *ctx, const char *dir, const char *style,
396 char *(*get_info)(const void *ctx, const char *dir))
401 ret = get_one_deps(ctx, dir, style, get_info);
403 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
405 return uniquify_deps(ret);
408 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
413 ret = get_one_safe_deps(ctx, dir, style, NULL);
415 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
417 return uniquify_deps(ret);