]> git.ozlabs.org Git - ccan/blob - tools/depends.c
Makefile: remove FASTTIMEOUT
[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, *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 /* O(n^2) but n is small. */
248 static char **add_deps(char **deps1, char **deps2)
249 {
250         unsigned int i, len;
251
252         len = talloc_array_length(deps1);
253
254         for (i = 0; deps2[i]; i++) {
255                 if (have_dep(deps1, deps2[i]))
256                         continue;
257                 deps1 = talloc_realloc(NULL, deps1, char *, len + 1);
258                 deps1[len-1] = talloc_steal(deps1, deps2[i]);
259                 deps1[len++] = NULL;
260         }
261         return deps1;
262 }
263
264 char **get_libs(const void *ctx, const char *dir, const char *style,
265                 char *(*get_info)(const void *ctx, const char *dir))
266 {
267         char **deps, **libs;
268         unsigned int i, len;
269
270         libs = get_one_libs(ctx, dir, get_info);
271         len = talloc_array_length(libs);
272
273         if (style) {
274                 deps = get_deps(ctx, dir, style, true, get_info);
275                 if (streq(style, "testdepends"))
276                         deps = add_deps(deps,
277                                         get_deps(ctx, dir, "depends", true,
278                                                  get_info));
279
280                 for (i = 0; deps[i]; i++) {
281                         char **newlibs, *subdir;
282                         size_t newlen;
283
284                         if (!strstarts(deps[i], "ccan/"))
285                                 continue;
286
287                         subdir = talloc_asprintf(ctx, "%s/%s",
288                                                  talloc_dirname(ctx, dir),
289                                                  deps[i] + strlen("ccan/"));
290
291                         newlibs = get_one_libs(ctx, subdir, get_info);
292                         newlen = talloc_array_length(newlibs);
293                         libs = talloc_realloc(ctx, libs, char *, len + newlen);
294                         memcpy(&libs[len], newlibs,
295                                sizeof(newlibs[0])*newlen);
296                         len += newlen;
297                 }
298         }
299
300         /* Append NULL entry. */
301         libs = talloc_realloc(ctx, libs, char *, len + 1);
302         libs[len] = NULL;
303         return libs;
304 }
305
306 /* FIXME: This is O(n^2), which is dumb. */
307 static char **uniquify_deps(char **deps)
308 {
309         unsigned int i, j, num;
310
311         if (!deps)
312                 return NULL;
313
314         num = talloc_array_length(deps) - 1;
315         for (i = 0; i < num; i++) {
316                 for (j = i + 1; j < num; j++) {
317                         if (streq(deps[i], deps[j])) {
318                                 memmove(&deps[j], &deps[j+1],
319                                         (num - j - 1) * sizeof(char *));
320                                 num--;
321                         }
322                 }
323         }
324         deps[num] = NULL;
325         /* Make sure talloc_array_length() works */
326         return talloc_realloc(NULL, deps, char *, num + 1);
327 }
328
329 char **get_deps(const void *ctx, const char *dir, const char *style,
330                 bool recurse,
331                 char *(*get_info)(const void *ctx, const char *dir))
332 {
333         char **ret;
334
335         if (!recurse) {
336                 ret = get_one_deps(ctx, dir, style, get_info);
337         } else
338                 ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
339
340         return uniquify_deps(ret);
341 }
342
343 char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
344                           bool recurse)
345 {
346         char **ret;
347         if (!recurse) {
348                 ret = get_one_safe_deps(ctx, dir, style, NULL);
349         } else {
350                 ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
351         }
352         return uniquify_deps(ret);
353 }