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>
7 #include <ccan/err/err.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, *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 compiled = temp_file(ctx, "", "info");
63 if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
64 CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
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 = talloc_asprintf(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 = talloc(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 = talloc_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 ret = talloc_realloc(ctx, ret, char, 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 = talloc_asprintf(ctx, "%s/_info", dir);
129 raw = grab_file(fname, fname, NULL);
131 errx(1, "Could not open %s", fname);
133 /* Replace \n by actual line breaks, and split it. */
134 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
136 deps = talloc_array(ctx, char *, talloc_array_length(lines));
138 for (n = i = 0; lines[i]; i++) {
142 /* Ignore lines starting with # (e.g. #include) */
143 if (lines[i][0] == '#')
146 if (strstr(lines[i], "\"testdepends\""))
147 correct_style = streq(style, "testdepends");
148 else if (strstr(lines[i], "\"depends\""))
149 correct_style = streq(style, "depends");
154 /* Start of line, or after ". */
155 if (strstarts(lines[i], "ccan/"))
158 str = strstr(lines[i], "\"ccan/");
164 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
167 deps[n++] = talloc_strndup(deps, str, len);
172 /* Make sure talloc_array_length() works */
173 return talloc_realloc(NULL, deps, char *, n + 1);
176 static bool have_dep(char **deps, const char *dep)
180 for (i = 0; deps[i]; i++)
181 if (streq(deps[i], dep))
188 /* Gets all the dependencies, recursively. */
190 get_all_deps(const void *ctx, const char *dir, const char *style,
191 char *(*get_info)(const void *ctx, const char *dir),
192 char **(*get_one)(const void *, const char *, const char *,
193 char *(*get_info)(const void *, const char *)))
198 deps = get_one(ctx, dir, style, get_info);
199 for (i = 0; i < talloc_array_length(deps)-1; i++) {
204 if (!strstarts(deps[i], "ccan/"))
207 subdir = talloc_asprintf(ctx, "%s/%s",
208 find_ccan_dir(dir), deps[i]);
209 newdeps = get_one(ctx, subdir, "depends", get_info);
211 /* Should be short, so brute-force out dups. */
212 for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
215 if (have_dep(deps, newdeps[j]))
218 num = talloc_array_length(deps)-1;
219 deps = talloc_realloc(NULL, deps, char *, num + 2);
220 deps[num] = newdeps[j];
227 /* Can return NULL: _info may not support 'libs'. */
228 static char **get_one_libs(const void *ctx, const char *dir,
229 char *(*get_info)(const void *ctx, const char *dir))
233 cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
234 lines = lines_from_cmd(cmd, "%s", cmd);
235 /* Strip final NULL. */
237 lines = talloc_realloc(NULL, lines, char *,
238 talloc_array_length(lines)-1);
242 /* O(n^2) but n is small. */
243 static char **add_deps(char **deps1, char **deps2)
247 len = talloc_array_length(deps1);
249 for (i = 0; deps2[i]; i++) {
250 if (have_dep(deps1, deps2[i]))
252 deps1 = talloc_realloc(NULL, deps1, char *, len + 1);
253 deps1[len-1] = talloc_steal(deps1, deps2[i]);
259 char **get_libs(const void *ctx, const char *dir, const char *style,
260 char *(*get_info)(const void *ctx, const char *dir))
265 libs = get_one_libs(ctx, dir, get_info);
266 len = talloc_array_length(libs);
269 deps = get_deps(ctx, dir, style, true, get_info);
270 if (streq(style, "testdepends"))
271 deps = add_deps(deps,
272 get_deps(ctx, dir, "depends", true,
275 for (i = 0; deps[i]; i++) {
276 char **newlibs, *subdir;
279 if (!strstarts(deps[i], "ccan/"))
282 subdir = talloc_asprintf(ctx, "%s/%s",
283 find_ccan_dir(dir), deps[i]);
285 newlibs = get_one_libs(ctx, subdir, get_info);
286 newlen = talloc_array_length(newlibs);
287 libs = talloc_realloc(ctx, libs, char *, len + newlen);
288 memcpy(&libs[len], newlibs,
289 sizeof(newlibs[0])*newlen);
294 /* Append NULL entry. */
295 libs = talloc_realloc(ctx, libs, char *, len + 1);
300 /* FIXME: This is O(n^2), which is dumb. */
301 static char **uniquify_deps(char **deps)
303 unsigned int i, j, num;
308 num = talloc_array_length(deps) - 1;
309 for (i = 0; i < num; i++) {
310 for (j = i + 1; j < num; j++) {
311 if (streq(deps[i], deps[j])) {
312 memmove(&deps[j], &deps[j+1],
313 (num - j - 1) * sizeof(char *));
319 /* Make sure talloc_array_length() works */
320 return talloc_realloc(NULL, deps, char *, num + 1);
323 char **get_deps(const void *ctx, const char *dir, const char *style,
325 char *(*get_info)(const void *ctx, const char *dir))
330 ret = get_one_deps(ctx, dir, style, get_info);
332 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
334 return uniquify_deps(ret);
337 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
342 ret = get_one_safe_deps(ctx, dir, style, NULL);
344 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
346 return uniquify_deps(ret);