1 #include <ccan/str/str.h>
2 #include <ccan/talloc/talloc.h>
3 #include <ccan/str_talloc/str_talloc.h>
4 #include <ccan/read_write_all/read_write_all.h>
5 #include <ccan/rbuf/rbuf.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 = talloc_vasprintf(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), talloc_array(ctx, char, 4096), 4096);
34 if (!rbuf_read_str(&in, 0, do_talloc_realloc) && errno)
35 err(1, "Reading from '%s'", cmd);
38 return strsplit(ctx, in.buf, "\n");
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;
49 /* Copy it to a file with proper .c suffix. */
50 info = talloc_grab_file(ctx, talloc_asprintf(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, len))
65 compiled = temp_file(ctx, "", "info");
66 if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
67 CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
73 static char **get_one_deps(const void *ctx, const char *dir, const char *style,
74 char *(*get_info)(const void *ctx, const char *dir))
77 char *infofile = get_info(ctx, dir);
82 cmd = talloc_asprintf(ctx, "%s %s", infofile, style);
83 deps = lines_from_cmd(cmd, "%s", cmd);
85 /* You must understand depends, maybe not testdepends. */
86 if (streq(style, "depends"))
87 err(1, "Could not run '%s'", cmd);
88 deps = talloc(ctx, char *);
94 /* Make copy of src, replacing "from" with "to". */
95 static char *replace(const void *ctx, const char *src,
96 const char *from, const char *to)
98 char *ret = talloc_strdup(ctx, "");
99 unsigned int rlen, len, add;
103 const char *next = strstr(src+len, from);
105 add = strlen(src+len) + 1;
107 add = next - (src+len);
109 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
110 memcpy(ret+rlen, src+len, add);
115 strcpy(ret+rlen, to);
121 /* This is a terrible hack. We scan for ccan/ strings. */
122 static char **get_one_safe_deps(const void *ctx,
125 char *(*unused)(const void *, const char *))
127 char **deps, **lines, *raw, *fname;
129 bool correct_style = false;
131 fname = talloc_asprintf(ctx, "%s/_info", dir);
132 raw = talloc_grab_file(fname, fname, NULL);
134 errx(1, "Could not open %s", fname);
136 /* Replace \n by actual line breaks, and split it. */
137 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
139 deps = talloc_array(ctx, char *, talloc_array_length(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++] = talloc_strndup(deps, str, len);
175 /* Make sure talloc_array_length() works */
176 return talloc_realloc(NULL, deps, char *, n + 1);
179 static bool have_dep(char **deps, const char *dep)
183 for (i = 0; deps[i]; i++)
184 if (streq(deps[i], dep))
191 /* Gets all the dependencies, recursively. */
193 get_all_deps(const void *ctx, const char *dir, const char *style,
194 char *(*get_info)(const void *ctx, const char *dir),
195 char **(*get_one)(const void *, const char *, const char *,
196 char *(*get_info)(const void *, const char *)))
201 deps = get_one(ctx, dir, style, get_info);
202 for (i = 0; i < talloc_array_length(deps)-1; i++) {
207 if (!strstarts(deps[i], "ccan/"))
210 subdir = talloc_asprintf(ctx, "%s/%s",
211 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 < talloc_array_length(newdeps)-1; j++) {
218 if (have_dep(deps, newdeps[j]))
221 num = talloc_array_length(deps)-1;
222 deps = talloc_realloc(NULL, deps, char *, num + 2);
223 deps[num] = newdeps[j];
230 /* Can return NULL: _info may not support 'libs'. */
231 static char **get_one_libs(const void *ctx, const char *dir,
232 char *(*get_info)(const void *ctx, const char *dir))
236 cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
237 lines = lines_from_cmd(cmd, "%s", cmd);
238 /* Strip final NULL. */
240 lines = talloc_realloc(NULL, lines, char *,
241 talloc_array_length(lines)-1);
245 /* O(n^2) but n is small. */
246 static char **add_deps(char **deps1, char **deps2)
250 len = talloc_array_length(deps1);
252 for (i = 0; deps2[i]; i++) {
253 if (have_dep(deps1, deps2[i]))
255 deps1 = talloc_realloc(NULL, deps1, char *, len + 1);
256 deps1[len-1] = talloc_steal(deps1, deps2[i]);
262 char **get_libs(const void *ctx, const char *dir, const char *style,
263 char *(*get_info)(const void *ctx, const char *dir))
268 libs = get_one_libs(ctx, dir, get_info);
269 len = talloc_array_length(libs);
272 deps = get_deps(ctx, dir, style, true, get_info);
273 if (streq(style, "testdepends"))
274 deps = add_deps(deps,
275 get_deps(ctx, dir, "depends", true,
278 for (i = 0; deps[i]; i++) {
279 char **newlibs, *subdir;
282 if (!strstarts(deps[i], "ccan/"))
285 subdir = talloc_asprintf(ctx, "%s/%s",
286 find_ccan_dir(dir), deps[i]);
288 newlibs = get_one_libs(ctx, subdir, get_info);
289 newlen = talloc_array_length(newlibs);
290 libs = talloc_realloc(ctx, libs, char *, len + newlen);
291 memcpy(&libs[len], newlibs,
292 sizeof(newlibs[0])*newlen);
297 /* Append NULL entry. */
298 libs = talloc_realloc(ctx, libs, char *, len + 1);
303 /* FIXME: This is O(n^2), which is dumb. */
304 static char **uniquify_deps(char **deps)
306 unsigned int i, j, num;
311 num = talloc_array_length(deps) - 1;
312 for (i = 0; i < num; i++) {
313 for (j = i + 1; j < num; j++) {
314 if (streq(deps[i], deps[j])) {
315 memmove(&deps[j], &deps[j+1],
316 (num - j - 1) * sizeof(char *));
322 /* Make sure talloc_array_length() works */
323 return talloc_realloc(NULL, deps, char *, num + 1);
326 char **get_deps(const void *ctx, const char *dir, const char *style,
328 char *(*get_info)(const void *ctx, const char *dir))
333 ret = get_one_deps(ctx, dir, style, get_info);
335 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
337 return uniquify_deps(ret);
340 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
345 ret = get_one_safe_deps(ctx, dir, style, NULL);
347 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
349 return uniquify_deps(ret);