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