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>
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, *ccandir, *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 ccandir = talloc_dirname(ctx, dir);
63 if (strrchr(ccandir, '/'))
64 *strrchr(ccandir, '/') = '\0';
66 compiled = temp_file(ctx, "", "info");
67 if (compile_and_link(ctx, info_c_file, ccandir, "",
68 CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
74 static char **get_one_deps(const void *ctx, const char *dir,
75 char *(*get_info)(const void *ctx, const char *dir))
78 char *infofile = get_info(ctx, dir);
83 cmd = talloc_asprintf(ctx, "%s depends", infofile);
84 deps = lines_from_cmd(cmd, "%s", cmd);
86 err(1, "Could not run '%s'", cmd);
90 /* Make copy of src, replacing "from" with "to". */
91 static char *replace(const void *ctx, const char *src,
92 const char *from, const char *to)
94 char *ret = talloc_strdup(ctx, "");
95 unsigned int rlen, len, add;
99 const char *next = strstr(src+len, from);
101 add = strlen(src+len) + 1;
103 add = next - (src+len);
105 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
106 memcpy(ret+rlen, src+len, add);
111 strcpy(ret+rlen, to);
117 /* This is a terrible hack. We scan for ccan/ strings. */
118 static char **get_one_safe_deps(const void *ctx,
120 char *(*unused)(const void *, const char *))
122 char **deps, **lines, *raw, *fname;
125 fname = talloc_asprintf(ctx, "%s/_info", dir);
126 raw = grab_file(fname, fname, NULL);
128 errx(1, "Could not open %s", fname);
130 /* Replace \n by actual line breaks, and split it. */
131 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
133 deps = talloc_array(ctx, char *, talloc_array_length(lines));
135 for (n = i = 0; lines[i]; i++) {
139 /* Ignore lines starting with # (e.g. #include) */
140 if (lines[i][0] == '#')
143 /* Start of line, or after ". */
144 if (strstarts(lines[i], "ccan/"))
147 str = strstr(lines[i], "\"ccan/");
153 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
156 deps[n++] = talloc_strndup(deps, str, len);
161 /* Make sure talloc_array_length() works */
162 return talloc_realloc(NULL, deps, char *, n + 1);
165 static bool have_dep(char **deps, const char *dep)
169 for (i = 0; deps[i]; i++)
170 if (streq(deps[i], dep))
177 /* Gets all the dependencies, recursively. */
179 get_all_deps(const void *ctx, const char *dir,
180 char *(*get_info)(const void *ctx, const char *dir),
181 char **(*get_one)(const void *, const char *,
182 char *(*get_info)(const void *, const char *)))
187 deps = get_one(ctx, dir, get_info);
188 for (i = 0; i < talloc_array_length(deps)-1; i++) {
193 if (!strstarts(deps[i], "ccan/"))
196 subdir = talloc_asprintf(ctx, "%s/%s",
197 talloc_dirname(ctx, dir),
198 deps[i] + strlen("ccan/"));
199 newdeps = get_one(ctx, subdir, get_info);
201 /* Should be short, so brute-force out dups. */
202 for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
205 if (have_dep(deps, newdeps[j]))
208 num = talloc_array_length(deps)-1;
209 deps = talloc_realloc(NULL, deps, char *, num + 2);
210 deps[num] = newdeps[j];
217 char **get_libs(const void *ctx, const char *dir,
218 unsigned int *num, char **infofile)
223 *infofile = compile_info(ctx, dir);
225 errx(1, "Could not compile _info for '%s'", dir);
228 cmd = talloc_asprintf(ctx, "%s libs", *infofile);
229 libs = lines_from_cmd(cmd, "%s", cmd);
231 err(1, "Could not run '%s'", cmd);
232 /* FIXME: Do we need num arg? */
233 *num = talloc_array_length(libs) - 1;
237 /* FIXME: This is O(n^2), which is dumb. */
238 static char **uniquify_deps(char **deps)
240 unsigned int i, j, num;
245 num = talloc_array_length(deps) - 1;
246 for (i = 0; i < num; i++) {
247 for (j = i + 1; j < num; j++) {
248 if (streq(deps[i], deps[j])) {
249 memmove(&deps[j], &deps[j+1],
250 (num - j - 1) * sizeof(char *));
256 /* Make sure talloc_array_length() works */
257 return talloc_realloc(NULL, deps, char *, num + 1);
260 char **get_deps(const void *ctx, const char *dir, bool recurse,
261 char *(*get_info)(const void *ctx, const char *dir))
266 ret = get_one_deps(ctx, dir, get_info);
268 ret = get_all_deps(ctx, dir, get_info, get_one_deps);
270 return uniquify_deps(ret);
273 char **get_safe_ccan_deps(const void *ctx, const char *dir,
278 ret = get_one_safe_deps(ctx, dir, NULL);
280 ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps);
282 return uniquify_deps(ret);