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