]> git.ozlabs.org Git - ccan/blob - tools/depends.c
tools: add testdepends handling in _info.
[ccan] / tools / depends.c
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>
7 #include "tools.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <err.h>
12 #include <stdbool.h>
13 #include <unistd.h>
14 #include <errno.h>
15
16 static char ** PRINTF_FMT(2, 3)
17 lines_from_cmd(const void *ctx, const char *format, ...)
18 {
19         va_list ap;
20         char *cmd, *buffer;
21         FILE *p;
22
23         va_start(ap, format);
24         cmd = talloc_vasprintf(ctx, format, ap);
25         va_end(ap);
26
27         p = popen(cmd, "r");
28         if (!p)
29                 err(1, "Executing '%s'", cmd);
30
31         buffer = grab_fd(ctx, fileno(p), NULL);
32         if (!buffer)
33                 err(1, "Reading from '%s'", cmd);
34         pclose(p);
35
36         return strsplit(ctx, buffer, "\n");
37 }
38
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)
42 {
43         char *info_c_file, *info, *ccandir, *compiled, *output;
44         size_t len;
45         int fd;
46
47         /* Copy it to a file with proper .c suffix. */
48         info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
49         if (!info)
50                 return NULL;
51
52         info_c_file = temp_file(ctx, ".c", "_info");
53         fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
54         if (fd < 0)
55                 return NULL;
56         if (!write_all(fd, info, len))
57                 return NULL;
58
59         if (close(fd) != 0)
60                 return NULL;
61
62         ccandir = talloc_dirname(ctx, dir);
63         if (strrchr(ccandir, '/'))
64                 *strrchr(ccandir, '/') = '\0';
65
66         compiled = temp_file(ctx, "", "info");
67         if (compile_and_link(ctx, info_c_file, ccandir, "",
68                              CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
69                              compiled, &output))
70                 return compiled;
71         return NULL;
72 }
73
74 static char **get_one_deps(const void *ctx, const char *dir, const char *style,
75                            char *(*get_info)(const void *ctx, const char *dir))
76 {
77         char **deps, *cmd;
78         char *infofile = get_info(ctx, dir);
79
80         if (!infofile)
81                 return NULL;
82
83         cmd = talloc_asprintf(ctx, "%s %s", infofile, style);
84         deps = lines_from_cmd(cmd, "%s", cmd);
85         if (!deps) {
86                 /* You must understand depends, maybe not testdepends. */
87                 if (streq(style, "depends"))
88                         err(1, "Could not run '%s'", cmd);
89                 deps = talloc(ctx, char *);
90                 deps[0] = NULL;
91         }
92         return deps;
93 }
94
95 /* Make copy of src, replacing "from" with "to". */
96 static char *replace(const void *ctx, const char *src,
97                      const char *from, const char *to)
98 {
99         char *ret = talloc_strdup(ctx, "");
100         unsigned int rlen, len, add;
101
102         rlen = len = 0;
103         for (;;) {
104                 const char *next = strstr(src+len, from);
105                 if (!next)
106                         add = strlen(src+len) + 1;
107                 else
108                         add = next - (src+len);
109
110                 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
111                 memcpy(ret+rlen, src+len, add);
112                 if (!next)
113                         return ret;
114                 len += add;
115                 rlen += add;
116                 strcpy(ret+rlen, to);
117                 rlen += strlen(to);
118                 len += strlen(from);
119         }
120 }
121
122 /* This is a terrible hack.  We scan for ccan/ strings. */
123 static char **get_one_safe_deps(const void *ctx,
124                                 const char *dir,
125                                 const char *style,
126                                 char *(*unused)(const void *, const char *))
127 {
128         char **deps, **lines, *raw, *fname;
129         unsigned int i, n;
130         bool correct_style = false;
131
132         fname = talloc_asprintf(ctx, "%s/_info", dir);
133         raw = grab_file(fname, fname, NULL);
134         if (!raw)
135                 errx(1, "Could not open %s", fname);
136
137         /* Replace \n by actual line breaks, and split it. */
138         lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
139
140         deps = talloc_array(ctx, char *, talloc_array_length(lines));
141
142         for (n = i = 0; lines[i]; i++) {
143                 char *str;
144                 unsigned int len;
145
146                 /* Ignore lines starting with # (e.g. #include) */
147                 if (lines[i][0] == '#')
148                         continue;
149
150                 if (strstr(lines[i], "\"testdepends\""))
151                         correct_style = streq(style, "testdepends");
152                 else if (strstr(lines[i], "\"depends\""))
153                         correct_style = streq(style, "depends");
154
155                 if (!correct_style)
156                         continue;
157
158                 /* Start of line, or after ". */
159                 if (strstarts(lines[i], "ccan/"))
160                         str = lines[i];
161                 else {
162                         str = strstr(lines[i], "\"ccan/");
163                         if (!str)
164                                 continue;
165                         str++;
166                 }
167                 
168                 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
169                 if (len == 5)
170                         continue;
171                 deps[n++] = talloc_strndup(deps, str, len);
172         }
173         deps[n] = NULL;
174         talloc_free(fname);
175
176         /* Make sure talloc_array_length() works */
177         return talloc_realloc(NULL, deps, char *, n + 1);
178 }
179
180 static bool have_dep(char **deps, const char *dep)
181 {
182         unsigned int i;
183
184         for (i = 0; deps[i]; i++)
185                 if (streq(deps[i], dep))
186                         return true;
187         return false;
188 }
189
190
191
192 /* Gets all the dependencies, recursively. */
193 static char **
194 get_all_deps(const void *ctx, const char *dir, const char *style,
195              char *(*get_info)(const void *ctx, const char *dir),
196              char **(*get_one)(const void *, const char *, const char *,
197                                char *(*get_info)(const void *, const char *)))
198 {
199         char **deps;
200         unsigned int i;
201
202         deps = get_one(ctx, dir, style, get_info);
203         for (i = 0; i < talloc_array_length(deps)-1; i++) {
204                 char **newdeps;
205                 unsigned int j;
206                 char *subdir;
207
208                 if (!strstarts(deps[i], "ccan/"))
209                         continue;
210
211                 subdir = talloc_asprintf(ctx, "%s/%s",
212                                          talloc_dirname(ctx, dir),
213                                          deps[i] + strlen("ccan/"));
214                 newdeps = get_one(ctx, subdir, "depends", get_info);
215
216                 /* Should be short, so brute-force out dups. */
217                 for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
218                         unsigned int num;
219
220                         if (have_dep(deps, newdeps[j]))
221                                 continue;
222
223                         num = talloc_array_length(deps)-1;
224                         deps = talloc_realloc(NULL, deps, char *, num + 2);
225                         deps[num] = newdeps[j];
226                         deps[num+1] = NULL;
227                 }
228         }
229         return deps;
230 }
231
232 /* Can return NULL: _info may not support 'libs'. */
233 static char **get_one_libs(const void *ctx, const char *dir,
234                            char *(*get_info)(const void *ctx, const char *dir))
235 {
236         char *cmd, **lines;
237
238         cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
239         lines = lines_from_cmd(cmd, "%s", cmd);
240         /* Strip final NULL. */
241         if (lines)
242                 lines = talloc_realloc(NULL, lines, char *,
243                                        talloc_array_length(lines)-1);
244         return lines;
245 }
246
247 char **get_libs(const void *ctx, const char *dir, bool recurse,
248                 char *(*get_info)(const void *ctx, const char *dir))
249 {
250         char **deps, **libs;
251         unsigned int i, len;
252
253         libs = get_one_libs(ctx, dir, get_info);
254         len = talloc_array_length(libs);
255
256         if (recurse) {
257                 deps = get_deps(ctx, dir, "depends", true, get_info);
258                 for (i = 0; deps[i]; i++) {
259                         char **newlibs, *subdir;
260                         size_t newlen;
261
262                         if (!strstarts(deps[i], "ccan/"))
263                                 continue;
264
265                         subdir = talloc_asprintf(ctx, "%s/%s",
266                                                  talloc_dirname(ctx, dir),
267                                                  deps[i] + strlen("ccan/"));
268
269                         newlibs = get_one_libs(ctx, subdir, get_info);
270                         newlen = talloc_array_length(newlibs);
271                         libs = talloc_realloc(ctx, libs, char *, len + newlen);
272                         memcpy(&libs[len], newlibs,
273                                sizeof(newlibs[0])*newlen);
274                         len += newlen;
275                 }
276         }
277
278         /* Append NULL entry. */
279         libs = talloc_realloc(ctx, libs, char *, len + 1);
280         libs[len] = NULL;
281         return libs;
282 }
283
284 /* FIXME: This is O(n^2), which is dumb. */
285 static char **uniquify_deps(char **deps)
286 {
287         unsigned int i, j, num;
288
289         if (!deps)
290                 return NULL;
291
292         num = talloc_array_length(deps) - 1;
293         for (i = 0; i < num; i++) {
294                 for (j = i + 1; j < num; j++) {
295                         if (streq(deps[i], deps[j])) {
296                                 memmove(&deps[j], &deps[j+1],
297                                         (num - j - 1) * sizeof(char *));
298                                 num--;
299                         }
300                 }
301         }
302         deps[num] = NULL;
303         /* Make sure talloc_array_length() works */
304         return talloc_realloc(NULL, deps, char *, num + 1);
305 }
306
307 char **get_deps(const void *ctx, const char *dir, const char *style,
308                 bool recurse,
309                 char *(*get_info)(const void *ctx, const char *dir))
310 {
311         char **ret;
312
313         if (!recurse) {
314                 ret = get_one_deps(ctx, dir, style, get_info);
315         } else
316                 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
317
318         return uniquify_deps(ret);
319 }
320
321 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
322                           bool recurse)
323 {
324         char **ret;
325         if (!recurse) {
326                 ret = get_one_safe_deps(ctx, dir, style, NULL);
327         } else {
328                 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
329         }
330         return uniquify_deps(ret);
331 }