]> git.ozlabs.org Git - ccan/blob - tools/depends.c
203788fe9bed29dff55021eb6cf23bef442bfa54
[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 "tools.h"
6 #include <err.h>
7 #include <stdbool.h>
8 #include <unistd.h>
9 #include <errno.h>
10
11 static char ** __attribute__((format(printf, 3, 4)))
12 lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
13 {
14         va_list ap;
15         char *cmd, *buffer;
16         FILE *p;
17
18         va_start(ap, format);
19         cmd = talloc_vasprintf(ctx, format, ap);
20         va_end(ap);
21
22         p = popen(cmd, "r");
23         if (!p)
24                 err(1, "Executing '%s'", cmd);
25
26         buffer = grab_fd(ctx, fileno(p), NULL);
27         if (!buffer)
28                 err(1, "Reading from '%s'", cmd);
29         pclose(p);
30
31         return strsplit(ctx, buffer, "\n", num);
32 }
33
34 static int unlink_info(char *infofile)
35 {
36         unlink(infofile);
37         return 0;
38 }
39
40 /* Be careful about trying to compile over running programs (parallel make) */
41 static char *compile_info(const void *ctx, const char *dir, const char *name)
42 {
43         char *infofile = talloc_asprintf(ctx, "%s/info.%u", dir, getpid());
44         char *cmd = talloc_asprintf(ctx, "cc " CFLAGS
45                                     " -o %s -x c %s/%s/_info",
46                                     infofile, dir, name);
47         talloc_set_destructor(infofile, unlink_info);
48         if (system(cmd) != 0)
49                 return NULL;
50
51         return infofile;
52 }
53
54 static char **get_one_deps(const void *ctx, const char *dir,
55                            const char *name, unsigned int *num)
56 {
57         char **deps, *cmd, *infofile;
58
59         infofile = compile_info(ctx, dir, name);
60         if (!infofile)
61                 errx(1, "Could not compile _info for '%s'", name);
62
63         cmd = talloc_asprintf(ctx, "%s depends", infofile);
64         deps = lines_from_cmd(cmd, num, "%s", cmd);
65         if (!deps)
66                 err(1, "Could not run '%s'", cmd);
67         return deps;
68 }
69
70 /* Make copy of src, replacing "from" with "to". */
71 static char *replace(const void *ctx, const char *src,
72                      const char *from, const char *to)
73 {
74         char *ret = talloc_strdup(ctx, "");
75         unsigned int rlen, len, add;
76
77         rlen = len = 0;
78         for (;;) {
79                 const char *next = strstr(src+len, from);
80                 if (!next)
81                         add = strlen(src+len) + 1;
82                 else
83                         add = next - (src+len);
84
85                 ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
86                 memcpy(ret+rlen, src+len, add);
87                 if (!next)
88                         return ret;
89                 len += add;
90                 rlen += add;
91                 strcpy(ret+rlen, to);
92                 rlen += strlen(to);
93                 len += strlen(from);
94         }
95 }
96
97 /* This is a terrible hack.  We scan for ccan/ strings. */
98 static char **get_one_safe_deps(const void *ctx,
99                                 const char *dir, const char *name,
100                                 unsigned int *num)
101 {
102         char **deps, **lines, *raw, *fname;
103         unsigned int i, n = 0;
104
105         fname = talloc_asprintf(ctx, "%s/%s/_info", dir, name);
106         raw = grab_file(fname, fname, NULL);
107         if (!raw)
108                 errx(1, "Could not open %s", fname);
109
110         /* Replace \n by actual line breaks, and split it. */
111         lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
112
113         deps = talloc_array(ctx, char *, n+1);
114
115         for (n = i = 0; lines[i]; i++) {
116                 char *str;
117                 unsigned int len;
118
119                 /* Ignore lines starting with # (e.g. #include) */
120                 if (lines[i][0] == '#')
121                         continue;
122
123                 /* Start of line, or after ". */
124                 if (strstarts(lines[i], "ccan/"))
125                         str = lines[i];
126                 else {
127                         str = strstr(lines[i], "\"ccan/");
128                         if (!str)
129                                 continue;
130                         str++;
131                 }
132                 
133                 len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
134                 if (len == 5)
135                         continue;
136                 deps[n++] = talloc_strndup(deps, str, len);
137         }
138         deps[n] = NULL;
139         talloc_free(fname);
140         if (num)
141                 *num = n;
142         return deps;
143 }
144
145 static bool have_dep(char **deps, unsigned int num, const char *dep)
146 {
147         unsigned int i;
148
149         for (i = 0; i < num; i++)
150                 if (streq(deps[i], dep))
151                         return true;
152         return false;
153 }
154
155 /* Gets all the dependencies, recursively. */
156 static char **
157 get_all_deps(const void *ctx, const char *dir, const char *name,
158              char **(*get_one)(const void *, const char *, const char *,
159                                unsigned int *))
160 {
161         char **deps;
162         unsigned int i, num;
163
164         deps = get_one(ctx, dir, name, &num);
165         for (i = 0; i < num; i++) {
166                 char **newdeps;
167                 unsigned int j, newnum;
168
169                 if (!strstarts(deps[i], "ccan/"))
170                         continue;
171
172                 newdeps = get_one(ctx, dir, deps[i] + strlen("ccan/"),
173                                   &newnum);
174
175                 /* Should be short, so brute-force out dups. */
176                 for (j = 0; j < newnum; j++) {
177                         if (have_dep(deps, num, newdeps[j]))
178                                 continue;
179
180                         deps = talloc_realloc(NULL, deps, char *, num + 2);
181                         deps[num++] = newdeps[j];
182                         deps[num] = NULL;
183                 }
184         }
185         return deps;
186 }
187
188 char **get_libs(const void *ctx, const char *dir,
189                 const char *name, unsigned int *num)
190 {
191         char **libs, *cmd, *infofile;
192
193         infofile = compile_info(ctx, dir, name);
194         if (!infofile)
195                 errx(1, "Could not compile _info for '%s'", name);
196
197         cmd = talloc_asprintf(ctx, "%s libs", infofile);
198         libs = lines_from_cmd(cmd, num, "%s", cmd);
199         if (!libs)
200                 err(1, "Could not run '%s'", cmd);
201         return libs;
202 }
203
204 char **get_deps(const void *ctx, const char *dir, const char *name,
205                 bool recurse)
206 {
207         if (!recurse) {
208                 unsigned int num;
209                 return get_one_deps(ctx, dir, name, &num);
210         }
211         return get_all_deps(ctx, dir, name, get_one_deps);
212 }
213
214 char **get_safe_ccan_deps(const void *ctx, const char *dir,
215                           const char *name, bool recurse)
216 {
217         if (!recurse) {
218                 unsigned int num;
219                 return get_one_safe_deps(ctx, dir, name, &num);
220         }
221         return get_all_deps(ctx, dir, name, get_one_safe_deps);
222 }