1 #include "talloc/talloc.h"
2 #include "string/string.h"
8 static char ** __attribute__((format(printf, 3, 4)))
9 lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
16 cmd = talloc_vasprintf(ctx, format, ap);
21 err(1, "Executing '%s'", cmd);
23 buffer = grab_fd(ctx, fileno(p), NULL);
25 err(1, "Reading from '%s'", cmd);
28 return strsplit(ctx, buffer, "\n", num);
31 static int unlink_info(char *infofile)
37 /* Be careful about trying to compile over running programs (parallel make) */
38 static char *compile_info(const void *ctx, const char *dir)
40 char *infofile = talloc_asprintf(ctx, "%s/_info.%u", dir, getpid());
41 char *cmd = talloc_asprintf(ctx, "cc " CFLAGS " -o %s %s/_info.c",
43 talloc_set_destructor(infofile, unlink_info);
50 static char **get_one_deps(const void *ctx, const char *dir, unsigned int *num)
52 char **deps, *cmd, *infofile;
54 infofile = compile_info(ctx, dir);
56 errx(1, "Could not compile _info for '%s'", dir);
58 cmd = talloc_asprintf(ctx, "%s depends", infofile);
59 deps = lines_from_cmd(cmd, num, "%s", cmd);
61 err(1, "Could not run '%s'", cmd);
65 /* Make copy of src, replacing "from" with "to". */
66 static char *replace(const void *ctx, const char *src,
67 const char *from, const char *to)
69 char *ret = talloc_strdup(ctx, "");
70 unsigned int rlen, len, add;
74 const char *next = strstr(src+len, from);
76 add = strlen(src+len) + 1;
78 add = next - (src+len);
80 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
81 memcpy(ret+rlen, src+len, add);
92 /* This is a terrible hack. We scan for ccan/ strings. */
93 static char **get_one_safe_deps(const void *ctx,
94 const char *dir, unsigned int *num)
96 char **deps, **lines, *raw, *fname;
97 unsigned int i, n = 0;
99 fname = talloc_asprintf(ctx, "%s/_info.c", dir);
100 raw = grab_file(fname, fname, NULL);
102 errx(1, "Could not open %s", fname);
104 /* Replace \n by actual line breaks, and split it. */
105 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
107 deps = talloc_array(ctx, char *, n+1);
109 for (n = i = 0; lines[i]; i++) {
113 /* Start of line, or after ". */
114 if (strstarts(lines[i], "ccan/"))
117 str = strstr(lines[i], "\"ccan/");
123 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
126 deps[n++] = talloc_strndup(deps, str, len);
135 static bool have_dep(char **deps, unsigned int num, const char *dep)
139 for (i = 0; i < num; i++)
140 if (streq(deps[i], dep))
145 /* Gets all the dependencies, recursively. */
147 get_all_deps(const void *ctx, const char *dir,
148 char **(*get_one)(const void *, const char *, unsigned int *))
153 deps = get_one(ctx, dir, &num);
154 for (i = 0; i < num; i++) {
156 unsigned int j, newnum;
158 if (!strstarts(deps[i], "ccan/"))
161 newdeps = get_one(ctx, deps[i], &newnum);
163 /* Should be short, so brute-force out dups. */
164 for (j = 0; j < newnum; j++) {
165 if (have_dep(deps, num, newdeps[j]))
168 deps = talloc_realloc(NULL, deps, char *, num + 2);
169 deps[num++] = newdeps[j];
176 char **get_deps(const void *ctx, const char *dir)
178 return get_all_deps(ctx, dir, get_one_deps);
181 char **get_safe_ccan_deps(const void *ctx, const char *dir)
183 return get_all_deps(ctx, dir, get_one_safe_deps);