1 #include <ccan/talloc/talloc.h>
2 #include <ccan/str/str.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>
15 static char ** __attribute__((format(printf, 3, 4)))
16 lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
23 cmd = talloc_vasprintf(ctx, format, ap);
28 err(1, "Executing '%s'", cmd);
30 buffer = grab_fd(ctx, fileno(p), NULL);
32 err(1, "Reading from '%s'", cmd);
35 return strsplit(ctx, buffer, "\n", num);
38 /* Be careful about trying to compile over running programs (parallel make).
39 * temp_file helps here. */
40 static char *compile_info(const void *ctx, const char *dir, const char *name)
42 char *info_c_file, *info, *errmsg;
46 /* Copy it to a file with proper .c suffix. */
47 info = grab_file(ctx, talloc_asprintf(ctx, "%s/%s/_info", dir, name),
52 info_c_file = temp_file(ctx, ".c");
53 fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
56 if (!write_all(fd, info, len))
62 return compile_and_link(ctx, info_c_file, "", "", "", &errmsg);
65 static char **get_one_deps(const void *ctx, const char *dir,
66 const char *name, unsigned int *num,
72 *infofile = compile_info(ctx, dir, name);
74 errx(1, "Could not compile _info for '%s'", name);
77 cmd = talloc_asprintf(ctx, "%s depends", *infofile);
78 deps = lines_from_cmd(cmd, num, "%s", cmd);
80 err(1, "Could not run '%s'", cmd);
84 /* Make copy of src, replacing "from" with "to". */
85 static char *replace(const void *ctx, const char *src,
86 const char *from, const char *to)
88 char *ret = talloc_strdup(ctx, "");
89 unsigned int rlen, len, add;
93 const char *next = strstr(src+len, from);
95 add = strlen(src+len) + 1;
97 add = next - (src+len);
99 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
100 memcpy(ret+rlen, src+len, add);
105 strcpy(ret+rlen, to);
111 /* This is a terrible hack. We scan for ccan/ strings. */
112 static char **get_one_safe_deps(const void *ctx,
113 const char *dir, const char *name,
117 char **deps, **lines, *raw, *fname;
118 unsigned int i, n = 0;
120 fname = talloc_asprintf(ctx, "%s/%s/_info", dir, name);
121 raw = grab_file(fname, fname, NULL);
123 errx(1, "Could not open %s", fname);
125 /* Replace \n by actual line breaks, and split it. */
126 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
128 deps = talloc_array(ctx, char *, n+1);
130 for (n = i = 0; lines[i]; i++) {
134 /* Ignore lines starting with # (e.g. #include) */
135 if (lines[i][0] == '#')
138 /* Start of line, or after ". */
139 if (strstarts(lines[i], "ccan/"))
142 str = strstr(lines[i], "\"ccan/");
148 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
151 deps[n++] = talloc_strndup(deps, str, len);
160 static bool have_dep(char **deps, unsigned int num, const char *dep)
164 for (i = 0; i < num; i++)
165 if (streq(deps[i], dep))
170 /* Gets all the dependencies, recursively. */
172 get_all_deps(const void *ctx, const char *dir, const char *name,
174 char **(*get_one)(const void *, const char *, const char *,
175 unsigned int *, char **))
180 deps = get_one(ctx, dir, name, &num, infofile);
181 for (i = 0; i < num; i++) {
183 unsigned int j, newnum;
185 if (!strstarts(deps[i], "ccan/"))
188 newdeps = get_one(ctx, dir, deps[i] + strlen("ccan/"),
191 /* Should be short, so brute-force out dups. */
192 for (j = 0; j < newnum; j++) {
193 if (have_dep(deps, num, newdeps[j]))
196 deps = talloc_realloc(NULL, deps, char *, num + 2);
197 deps[num++] = newdeps[j];
204 char **get_libs(const void *ctx, const char *dir,
205 const char *name, unsigned int *num,
211 *infofile = compile_info(ctx, dir, name);
213 errx(1, "Could not compile _info for '%s'", name);
216 cmd = talloc_asprintf(ctx, "%s libs", *infofile);
217 libs = lines_from_cmd(cmd, num, "%s", cmd);
219 err(1, "Could not run '%s'", cmd);
223 char **get_deps(const void *ctx, const char *dir, const char *name,
224 bool recurse, char **infofile)
226 char *temp = NULL, **ret;
232 ret = get_one_deps(ctx, dir, name, &num, infofile);
234 ret = get_all_deps(ctx, dir, name, infofile, get_one_deps);
236 if (infofile == &temp && temp) {
243 char **get_safe_ccan_deps(const void *ctx, const char *dir,
244 const char *name, bool recurse, char **infofile)
248 return get_one_safe_deps(ctx, dir, name, &num, infofile);
250 return get_all_deps(ctx, dir, name, infofile, get_one_safe_deps);