]> git.ozlabs.org Git - ccan/blob - tools/depends.c
ccanlint: run tests under valgrind initially.
[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 "tools.h"
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <err.h>
11 #include <stdbool.h>
12 #include <unistd.h>
13 #include <errno.h>
14
15 static char ** __attribute__((format(printf, 2, 3)))
16 lines_from_cmd(const void *ctx, const char *format, ...)
17 {
18         va_list ap;
19         char *cmd, *buffer;
20         FILE *p;
21
22         va_start(ap, format);
23         cmd = talloc_vasprintf(ctx, format, ap);
24         va_end(ap);
25
26         p = popen(cmd, "r");
27         if (!p)
28                 err(1, "Executing '%s'", cmd);
29
30         buffer = grab_fd(ctx, fileno(p), NULL);
31         if (!buffer)
32                 err(1, "Reading from '%s'", cmd);
33         pclose(p);
34
35         return strsplit(ctx, buffer, "\n");
36 }
37
38 /* Be careful about trying to compile over running programs (parallel make).
39  * temp_file helps here. */
40 static char *compile_info(const void *ctx, const char *dir)
41 {
42         char *info_c_file, *info, *ccandir, *compiled, *output;
43         size_t len;
44         int fd;
45
46         /* Copy it to a file with proper .c suffix. */
47         info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
48         if (!info)
49                 return NULL;
50
51         info_c_file = maybe_temp_file(ctx, ".c", false, "_info");
52         fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
53         if (fd < 0)
54                 return NULL;
55         if (!write_all(fd, info, len))
56                 return NULL;
57
58         if (close(fd) != 0)
59                 return NULL;
60
61         ccandir = talloc_dirname(ctx, dir);
62         if (strrchr(ccandir, '/'))
63                 *strrchr(ccandir, '/') = '\0';
64
65         compiled = maybe_temp_file(ctx, "", false, "info");
66         if (compile_and_link(ctx, info_c_file, ccandir, "",
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,
74                            unsigned int *num, char **infofile)
75 {
76         char **deps, *cmd;
77
78         if (!*infofile) {
79                 *infofile = compile_info(ctx, dir);
80                 if (!*infofile)
81                         errx(1, "Could not compile _info for '%s'", dir);
82         }
83
84         cmd = talloc_asprintf(ctx, "%s depends", *infofile);
85         deps = lines_from_cmd(cmd, "%s", cmd);
86         if (!deps)
87                 err(1, "Could not run '%s'", cmd);
88         /* FIXME: Do we need num arg? */
89         *num = talloc_array_length(deps) - 1;
90         return deps;
91 }
92
93 /* Make copy of src, replacing "from" with "to". */
94 static char *replace(const void *ctx, const char *src,
95                      const char *from, const char *to)
96 {
97         char *ret = talloc_strdup(ctx, "");
98         unsigned int rlen, len, add;
99
100         rlen = len = 0;
101         for (;;) {
102                 const char *next = strstr(src+len, from);
103                 if (!next)
104                         add = strlen(src+len) + 1;
105                 else
106                         add = next - (src+len);
107
108                 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
109                 memcpy(ret+rlen, src+len, add);
110                 if (!next)
111                         return ret;
112                 len += add;
113                 rlen += add;
114                 strcpy(ret+rlen, to);
115                 rlen += strlen(to);
116                 len += strlen(from);
117         }
118 }
119
120 /* This is a terrible hack.  We scan for ccan/ strings. */
121 static char **get_one_safe_deps(const void *ctx,
122                                 const char *dir,
123                                 unsigned int *num,
124                                 char **infofile)
125 {
126         char **deps, **lines, *raw, *fname;
127         unsigned int i, n;
128
129         fname = talloc_asprintf(ctx, "%s/_info", dir);
130         raw = grab_file(fname, fname, NULL);
131         if (!raw)
132                 errx(1, "Could not open %s", fname);
133
134         /* Replace \n by actual line breaks, and split it. */
135         lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
136
137         deps = talloc_array(ctx, char *, talloc_array_length(lines));
138
139         for (n = i = 0; lines[i]; i++) {
140                 char *str;
141                 unsigned int len;
142
143                 /* Ignore lines starting with # (e.g. #include) */
144                 if (lines[i][0] == '#')
145                         continue;
146
147                 /* Start of line, or after ". */
148                 if (strstarts(lines[i], "ccan/"))
149                         str = lines[i];
150                 else {
151                         str = strstr(lines[i], "\"ccan/");
152                         if (!str)
153                                 continue;
154                         str++;
155                 }
156                 
157                 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
158                 if (len == 5)
159                         continue;
160                 deps[n++] = talloc_strndup(deps, str, len);
161         }
162         deps[n] = NULL;
163         talloc_free(fname);
164         if (num)
165                 *num = n;
166         return deps;
167 }
168
169 static bool have_dep(char **deps, unsigned int num, const char *dep)
170 {
171         unsigned int i;
172
173         for (i = 0; i < num; i++)
174                 if (streq(deps[i], dep))
175                         return true;
176         return false;
177 }
178
179 /* Gets all the dependencies, recursively. */
180 static char **
181 get_all_deps(const void *ctx, const char *dir,
182              char **infofile,
183              char **(*get_one)(const void *, const char *,
184                                unsigned int *, char **))
185 {
186         char **deps;
187         unsigned int i, num;
188
189         deps = get_one(ctx, dir, &num, infofile);
190         for (i = 0; i < num; i++) {
191                 char **newdeps;
192                 unsigned int j, newnum;
193                 char *subinfo = NULL;
194                 char *subdir;
195
196                 if (!strstarts(deps[i], "ccan/"))
197                         continue;
198
199                 subdir = talloc_asprintf(ctx, "%s/%s",
200                                          talloc_dirname(ctx, dir),
201                                          deps[i] + strlen("ccan/"));
202                 newdeps = get_one(ctx, subdir, &newnum, &subinfo);
203
204                 /* Should be short, so brute-force out dups. */
205                 for (j = 0; j < newnum; j++) {
206                         if (have_dep(deps, num, newdeps[j]))
207                                 continue;
208
209                         deps = talloc_realloc(NULL, deps, char *, num + 2);
210                         deps[num++] = newdeps[j];
211                         deps[num] = NULL;
212                 }
213         }
214         return deps;
215 }
216
217 char **get_libs(const void *ctx, const char *dir,
218                 unsigned int *num, char **infofile)
219 {
220         char **libs, *cmd;
221
222         if (!*infofile) {
223                 *infofile = compile_info(ctx, dir);
224                 if (!*infofile)
225                         errx(1, "Could not compile _info for '%s'", dir);
226         }
227
228         cmd = talloc_asprintf(ctx, "%s libs", *infofile);
229         libs = lines_from_cmd(cmd, "%s", cmd);
230         if (!libs)
231                 err(1, "Could not run '%s'", cmd);
232         /* FIXME: Do we need num arg? */
233         *num = talloc_array_length(libs) - 1;
234         return libs;
235 }
236
237 char **get_deps(const void *ctx, const char *dir,
238                 bool recurse, char **infofile)
239 {
240         char *temp = NULL, **ret;
241         if (!infofile)
242                 infofile = &temp;
243
244         if (!recurse) {
245                 unsigned int num;
246                 ret = get_one_deps(ctx, dir, &num, infofile);
247         } else
248                 ret = get_all_deps(ctx, dir, infofile, get_one_deps);
249
250         if (infofile == &temp && temp) {
251                 unlink(temp);
252                 talloc_free(temp);
253         }
254         return ret;
255 }
256
257 char **get_safe_ccan_deps(const void *ctx, const char *dir,
258                           bool recurse)
259 {
260         if (!recurse) {
261                 unsigned int num;
262                 return get_one_safe_deps(ctx, dir, &num, NULL);
263         }
264         return get_all_deps(ctx, dir, NULL, get_one_safe_deps);
265 }