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