]> git.ozlabs.org Git - ccan/blob - tools/depends.c
configurator: HAVE_SECTION_START_STOP
[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 = temp_file(ctx, ".c", "_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 = temp_file(ctx, "", "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, char **infofile)
74 {
75         char **deps, *cmd;
76
77         if (!*infofile) {
78                 *infofile = compile_info(ctx, dir);
79                 if (!*infofile)
80                         errx(1, "Could not compile _info for '%s'", dir);
81         }
82
83         cmd = talloc_asprintf(ctx, "%s depends", *infofile);
84         deps = lines_from_cmd(cmd, "%s", cmd);
85         if (!deps)
86                 err(1, "Could not run '%s'", cmd);
87         return deps;
88 }
89
90 /* Make copy of src, replacing "from" with "to". */
91 static char *replace(const void *ctx, const char *src,
92                      const char *from, const char *to)
93 {
94         char *ret = talloc_strdup(ctx, "");
95         unsigned int rlen, len, add;
96
97         rlen = len = 0;
98         for (;;) {
99                 const char *next = strstr(src+len, from);
100                 if (!next)
101                         add = strlen(src+len) + 1;
102                 else
103                         add = next - (src+len);
104
105                 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
106                 memcpy(ret+rlen, src+len, add);
107                 if (!next)
108                         return ret;
109                 len += add;
110                 rlen += add;
111                 strcpy(ret+rlen, to);
112                 rlen += strlen(to);
113                 len += strlen(from);
114         }
115 }
116
117 /* This is a terrible hack.  We scan for ccan/ strings. */
118 static char **get_one_safe_deps(const void *ctx,
119                                 const char *dir,
120                                 char **infofile)
121 {
122         char **deps, **lines, *raw, *fname;
123         unsigned int i, n;
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");
132
133         deps = talloc_array(ctx, char *, talloc_array_length(lines));
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
161         /* Make sure talloc_array_length() works */
162         return talloc_realloc(NULL, deps, char *, n + 1);
163 }
164
165 static bool have_dep(char **deps, const char *dep)
166 {
167         unsigned int i;
168
169         for (i = 0; deps[i]; i++)
170                 if (streq(deps[i], dep))
171                         return true;
172         return false;
173 }
174
175
176
177 /* Gets all the dependencies, recursively. */
178 static char **
179 get_all_deps(const void *ctx, const char *dir,
180              char **infofile,
181              char **(*get_one)(const void *, const char *, char **))
182 {
183         char **deps;
184         unsigned int i;
185
186         deps = get_one(ctx, dir, infofile);
187         for (i = 0; i < talloc_array_length(deps)-1; i++) {
188                 char **newdeps;
189                 unsigned int j;
190                 char *subinfo = NULL;
191                 char *subdir;
192
193                 if (!strstarts(deps[i], "ccan/"))
194                         continue;
195
196                 subdir = talloc_asprintf(ctx, "%s/%s",
197                                          talloc_dirname(ctx, dir),
198                                          deps[i] + strlen("ccan/"));
199                 newdeps = get_one(ctx, subdir, &subinfo);
200
201                 /* Should be short, so brute-force out dups. */
202                 for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
203                         unsigned int num;
204
205                         if (have_dep(deps, newdeps[j]))
206                                 continue;
207
208                         num = talloc_array_length(deps)-1;
209                         deps = talloc_realloc(NULL, deps, char *, num + 2);
210                         deps[num] = newdeps[j];
211                         deps[num+1] = 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 /* FIXME: This is O(n^2), which is dumb. */
238 static char **uniquify_deps(char **deps)
239 {
240         unsigned int i, j, num;
241
242         num = talloc_array_length(deps) - 1;
243         for (i = 0; i < num; i++) {
244                 for (j = i + 1; j < num; j++) {
245                         if (streq(deps[i], deps[j])) {
246                                 memmove(&deps[j], &deps[j+1],
247                                         (num - j - 1) * sizeof(char *));
248                                 num--;
249                         }
250                 }
251         }
252         deps[num] = NULL;
253         /* Make sure talloc_array_length() works */
254         return talloc_realloc(NULL, deps, char *, num + 1);
255 }
256
257 char **get_deps(const void *ctx, const char *dir,
258                 bool recurse, char **infofile)
259 {
260         char *temp = NULL, **ret;
261         if (!infofile)
262                 infofile = &temp;
263
264         if (!recurse) {
265                 ret = get_one_deps(ctx, dir, infofile);
266         } else
267                 ret = get_all_deps(ctx, dir, infofile, get_one_deps);
268
269         if (infofile == &temp && temp) {
270                 unlink(temp);
271                 talloc_free(temp);
272         }
273         return uniquify_deps(ret);
274 }
275
276 char **get_safe_ccan_deps(const void *ctx, const char *dir,
277                           bool recurse)
278 {
279         char **ret;
280         if (!recurse) {
281                 ret = get_one_safe_deps(ctx, dir, NULL);
282         } else {
283                 ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps);
284         }
285         return uniquify_deps(ret);
286 }