]> git.ozlabs.org Git - ccan/blob - tools/depends.c
Fix "make check": this is temporary until we use ccanlint for it.
[ccan] / tools / depends.c
1 #include <ccan/talloc/talloc.h>
2 #include <ccan/str/str.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, 3, 4)))
16 lines_from_cmd(const void *ctx, unsigned int *num, 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", num);
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, const char *name)
41 {
42         char *info_c_file, *info, *errmsg;
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/%s/_info", dir, name),
48                          &len);
49         if (!info)
50                 return NULL;
51
52         info_c_file = temp_file(ctx, ".c");
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         return compile_and_link(ctx, info_c_file, "", "", "", &errmsg);
63 }
64
65 static char **get_one_deps(const void *ctx, const char *dir,
66                            const char *name, unsigned int *num,
67                            char **infofile)
68 {
69         char **deps, *cmd;
70
71         if (!*infofile) {
72                 *infofile = compile_info(ctx, dir, name);
73                 if (!*infofile)
74                         errx(1, "Could not compile _info for '%s'", name);
75         }
76
77         cmd = talloc_asprintf(ctx, "%s depends", *infofile);
78         deps = lines_from_cmd(cmd, num, "%s", cmd);
79         if (!deps)
80                 err(1, "Could not run '%s'", cmd);
81         return deps;
82 }
83
84 /* Make copy of src, replacing "from" with "to". */
85 static char *replace(const void *ctx, const char *src,
86                      const char *from, const char *to)
87 {
88         char *ret = talloc_strdup(ctx, "");
89         unsigned int rlen, len, add;
90
91         rlen = len = 0;
92         for (;;) {
93                 const char *next = strstr(src+len, from);
94                 if (!next)
95                         add = strlen(src+len) + 1;
96                 else
97                         add = next - (src+len);
98
99                 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
100                 memcpy(ret+rlen, src+len, add);
101                 if (!next)
102                         return ret;
103                 len += add;
104                 rlen += add;
105                 strcpy(ret+rlen, to);
106                 rlen += strlen(to);
107                 len += strlen(from);
108         }
109 }
110
111 /* This is a terrible hack.  We scan for ccan/ strings. */
112 static char **get_one_safe_deps(const void *ctx,
113                                 const char *dir, const char *name,
114                                 unsigned int *num,
115                                 char **infofile)
116 {
117         char **deps, **lines, *raw, *fname;
118         unsigned int i, n = 0;
119
120         fname = talloc_asprintf(ctx, "%s/%s/_info", dir, name);
121         raw = grab_file(fname, fname, NULL);
122         if (!raw)
123                 errx(1, "Could not open %s", fname);
124
125         /* Replace \n by actual line breaks, and split it. */
126         lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
127
128         deps = talloc_array(ctx, char *, n+1);
129
130         for (n = i = 0; lines[i]; i++) {
131                 char *str;
132                 unsigned int len;
133
134                 /* Ignore lines starting with # (e.g. #include) */
135                 if (lines[i][0] == '#')
136                         continue;
137
138                 /* Start of line, or after ". */
139                 if (strstarts(lines[i], "ccan/"))
140                         str = lines[i];
141                 else {
142                         str = strstr(lines[i], "\"ccan/");
143                         if (!str)
144                                 continue;
145                         str++;
146                 }
147                 
148                 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
149                 if (len == 5)
150                         continue;
151                 deps[n++] = talloc_strndup(deps, str, len);
152         }
153         deps[n] = NULL;
154         talloc_free(fname);
155         if (num)
156                 *num = n;
157         return deps;
158 }
159
160 static bool have_dep(char **deps, unsigned int num, const char *dep)
161 {
162         unsigned int i;
163
164         for (i = 0; i < num; i++)
165                 if (streq(deps[i], dep))
166                         return true;
167         return false;
168 }
169
170 /* Gets all the dependencies, recursively. */
171 static char **
172 get_all_deps(const void *ctx, const char *dir, const char *name,
173              char **infofile,
174              char **(*get_one)(const void *, const char *, const char *,
175                                unsigned int *, char **))
176 {
177         char **deps;
178         unsigned int i, num;
179
180         deps = get_one(ctx, dir, name, &num, infofile);
181         for (i = 0; i < num; i++) {
182                 char **newdeps;
183                 unsigned int j, newnum;
184
185                 if (!strstarts(deps[i], "ccan/"))
186                         continue;
187
188                 newdeps = get_one(ctx, dir, deps[i] + strlen("ccan/"),
189                                   &newnum, infofile);
190
191                 /* Should be short, so brute-force out dups. */
192                 for (j = 0; j < newnum; j++) {
193                         if (have_dep(deps, num, newdeps[j]))
194                                 continue;
195
196                         deps = talloc_realloc(NULL, deps, char *, num + 2);
197                         deps[num++] = newdeps[j];
198                         deps[num] = NULL;
199                 }
200         }
201         return deps;
202 }
203
204 char **get_libs(const void *ctx, const char *dir,
205                 const char *name, unsigned int *num,
206                 char **infofile)
207 {
208         char **libs, *cmd;
209
210         if (!*infofile) {
211                 *infofile = compile_info(ctx, dir, name);
212                 if (!*infofile)
213                         errx(1, "Could not compile _info for '%s'", name);
214         }
215
216         cmd = talloc_asprintf(ctx, "%s libs", *infofile);
217         libs = lines_from_cmd(cmd, num, "%s", cmd);
218         if (!libs)
219                 err(1, "Could not run '%s'", cmd);
220         return libs;
221 }
222
223 char **get_deps(const void *ctx, const char *dir, const char *name,
224                 bool recurse, char **infofile)
225 {
226         char *temp = NULL, **ret;
227         if (!infofile)
228                 infofile = &temp;
229
230         if (!recurse) {
231                 unsigned int num;
232                 ret = get_one_deps(ctx, dir, name, &num, infofile);
233         } else
234                 ret = get_all_deps(ctx, dir, name, infofile, get_one_deps);
235
236         if (infofile == &temp && temp) {
237                 unlink(temp);
238                 talloc_free(temp);
239         }
240         return ret;
241 }
242
243 char **get_safe_ccan_deps(const void *ctx, const char *dir,
244                           const char *name, bool recurse, char **infofile)
245 {
246         if (!recurse) {
247                 unsigned int num;
248                 return get_one_safe_deps(ctx, dir, name, &num, infofile);
249         }
250         return get_all_deps(ctx, dir, name, infofile, get_one_safe_deps);
251 }