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>
10 static char ** __attribute__((format(printf, 3, 4)))
11 lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
18 cmd = talloc_vasprintf(ctx, format, ap);
23 err(1, "Executing '%s'", cmd);
25 buffer = grab_fd(ctx, fileno(p), NULL);
27 err(1, "Reading from '%s'", cmd);
30 return strsplit(ctx, buffer, "\n", num);
33 static int unlink_info(char *infofile)
39 /* Be careful about trying to compile over running programs (parallel make) */
40 static char *compile_info(const void *ctx, const char *dir)
42 char *infofile = talloc_asprintf(ctx, "%s/info.%u", dir, getpid());
43 char *cmd = talloc_asprintf(ctx, "cc " CFLAGS " -o %s -x c %s/_info",
45 talloc_set_destructor(infofile, unlink_info);
52 static char **get_one_deps(const void *ctx, const char *dir, unsigned int *num)
54 char **deps, *cmd, *infofile;
56 infofile = compile_info(ctx, dir);
58 errx(1, "Could not compile _info for '%s'", dir);
60 cmd = talloc_asprintf(ctx, "%s depends", infofile);
61 deps = lines_from_cmd(cmd, num, "%s", cmd);
63 err(1, "Could not run '%s'", cmd);
67 /* Make copy of src, replacing "from" with "to". */
68 static char *replace(const void *ctx, const char *src,
69 const char *from, const char *to)
71 char *ret = talloc_strdup(ctx, "");
72 unsigned int rlen, len, add;
76 const char *next = strstr(src+len, from);
78 add = strlen(src+len) + 1;
80 add = next - (src+len);
82 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
83 memcpy(ret+rlen, src+len, add);
94 /* This is a terrible hack. We scan for ccan/ strings. */
95 static char **get_one_safe_deps(const void *ctx,
96 const char *dir, unsigned int *num)
98 char **deps, **lines, *raw, *fname;
99 unsigned int i, n = 0;
101 fname = talloc_asprintf(ctx, "%s/_info", dir);
102 raw = grab_file(fname, fname, NULL);
104 errx(1, "Could not open %s", fname);
106 /* Replace \n by actual line breaks, and split it. */
107 lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
109 deps = talloc_array(ctx, char *, n+1);
111 for (n = i = 0; lines[i]; i++) {
115 /* Ignore lines starting with # (e.g. #include) */
116 if (lines[i][0] == '#')
119 /* Start of line, or after ". */
120 if (strstarts(lines[i], "ccan/"))
123 str = strstr(lines[i], "\"ccan/");
129 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
132 deps[n++] = talloc_strndup(deps, str, len);
141 static bool have_dep(char **deps, unsigned int num, const char *dep)
145 for (i = 0; i < num; i++)
146 if (streq(deps[i], dep))
151 /* Gets all the dependencies, recursively. */
153 get_all_deps(const void *ctx, const char *dir,
154 char **(*get_one)(const void *, const char *, unsigned int *))
159 deps = get_one(ctx, dir, &num);
160 for (i = 0; i < num; i++) {
162 unsigned int j, newnum;
164 if (!strstarts(deps[i], "ccan/"))
167 newdeps = get_one(ctx, deps[i], &newnum);
169 /* Should be short, so brute-force out dups. */
170 for (j = 0; j < newnum; j++) {
171 if (have_dep(deps, num, newdeps[j]))
174 deps = talloc_realloc(NULL, deps, char *, num + 2);
175 deps[num++] = newdeps[j];
182 char **get_deps(const void *ctx, const char *dir, bool recurse)
186 return get_one_deps(ctx, dir, &num);
188 return get_all_deps(ctx, dir, get_one_deps);
191 char **get_safe_ccan_deps(const void *ctx, const char *dir, bool recurse)
195 return get_one_safe_deps(ctx, dir, &num);
197 return get_all_deps(ctx, dir, get_one_safe_deps);