]> git.ozlabs.org Git - ccan/blob - tools/manifest.c
tools: determing ccan directory differently
[ccan] / tools / manifest.c
1 #include "config.h"
2 #include "manifest.h"
3 #include "tools.h"
4 #include <ccan/talloc/talloc.h>
5 #include <ccan/str/str.h>
6 #include <ccan/str_talloc/str_talloc.h>
7 #include <ccan/talloc_link/talloc_link.h>
8 #include <ccan/hash/hash.h>
9 #include <ccan/htable/htable_type.h>
10 #include <ccan/grab_file/grab_file.h>
11 #include <ccan/noerr/noerr.h>
12 #include <ccan/foreach/foreach.h>
13 #include <ccan/asort/asort.h>
14 #include <ccan/array_size/array_size.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <err.h>
20 #include <errno.h>
21 #include <dirent.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <assert.h>
25
26 const char *ccan_dir;
27
28 static size_t dir_hash(const char *name)
29 {
30         return hash(name, strlen(name), 0);
31 }
32
33 static const char *manifest_name(const struct manifest *m)
34 {
35         return m->dir;
36 }
37
38 static bool dir_cmp(const struct manifest *m, const char *dir)
39 {
40         return strcmp(m->dir, dir) == 0;
41 }
42
43 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
44                    htable_manifest);
45 static struct htable_manifest *manifests;
46
47 const char *get_ccan_file_contents(struct ccan_file *f)
48 {
49         if (!f->contents) {
50                 f->contents = grab_file(f, f->fullname, &f->contents_size);
51                 if (!f->contents)
52                         err(1, "Reading file %s", f->fullname);
53         }
54         return f->contents;
55 }
56
57 char **get_ccan_file_lines(struct ccan_file *f)
58 {
59         if (!f->lines)
60                 f->lines = strsplit(f, get_ccan_file_contents(f), "\n");
61
62         /* FIXME: is f->num_lines necessary? */
63         f->num_lines = talloc_array_length(f->lines) - 1;
64         return f->lines;
65 }
66
67 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
68 {
69         struct ccan_file *f;
70         unsigned int i;
71
72         assert(dir[0] == '/');
73
74         f = talloc(ctx, struct ccan_file);
75         f->lines = NULL;
76         f->line_info = NULL;
77         f->doc_sections = NULL;
78         for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
79                 f->compiled[i] = NULL;
80         f->name = talloc_steal(f, name);
81         f->fullname = talloc_asprintf(f, "%s/%s", dir, f->name);
82         f->contents = NULL;
83         f->simplified = NULL;
84         return f;
85 }
86
87 static void add_files(struct manifest *m, const char *dir)
88 {
89         DIR *d;
90         struct dirent *ent;
91         char **subs = NULL;
92
93         if (dir[0])
94                 d = opendir(dir);
95         else
96                 d = opendir(".");
97         if (!d)
98                 err(1, "Opening directory %s", dir[0] ? dir : ".");
99
100         while ((ent = readdir(d)) != NULL) {
101                 struct stat st;
102                 struct ccan_file *f;
103                 struct list_head *dest;
104                 bool is_c_src;
105
106                 if (ent->d_name[0] == '.')
107                         continue;
108
109                 f = new_ccan_file(m, m->dir,
110                                   talloc_asprintf(m, "%s%s",
111                                                   dir, ent->d_name));
112                 if (lstat(f->name, &st) != 0)
113                         err(1, "lstat %s", f->name);
114
115                 if (S_ISDIR(st.st_mode)) {
116                         size_t len = talloc_array_length(subs);
117                         subs = talloc_realloc(m, subs, char *, len+1);
118                         subs[len] = talloc_append_string(f->name, "/");
119                         continue;
120                 }
121                 if (!S_ISREG(st.st_mode)) {
122                         talloc_free(f);
123                         continue;
124                 }
125
126                 if (streq(f->name, "_info")) {
127                         m->info_file = f;
128                         continue;
129                 }
130
131                 is_c_src = strends(f->name, ".c");
132                 if (!is_c_src && !strends(f->name, ".h")) {
133                         dest = &m->other_files;
134                 } else if (!strchr(f->name, '/')) {
135                         if (is_c_src)
136                                 dest = &m->c_files;
137                         else
138                                 dest = &m->h_files;
139                 } else if (strstarts(f->name, "test/")) {
140                         if (is_c_src) {
141                                 if (strstarts(f->name, "test/api"))
142                                         dest = &m->api_tests;
143                                 else if (strstarts(f->name, "test/run"))
144                                         dest = &m->run_tests;
145                                 else if (strstarts(f->name, "test/compile_ok"))
146                                         dest = &m->compile_ok_tests;
147                                 else if (strstarts(f->name, "test/compile_fail"))
148                                         dest = &m->compile_fail_tests;
149                                 else
150                                         dest = &m->other_test_c_files;
151                         } else
152                                 dest = &m->other_test_files;
153                 } else
154                         dest = &m->other_files;
155
156                 list_add(dest, &f->list);
157         }
158         closedir(d);
159
160         /* Before we recurse, sanity check this is a ccan module. */
161         if (!dir[0]) {
162                 size_t i;
163
164                 if (!m->info_file
165                     && list_empty(&m->c_files)
166                     && list_empty(&m->h_files))
167                         errx(1, "No _info, C or H files found here!");
168
169                 for (i = 0; i < talloc_array_length(subs); i++)
170                         add_files(m, subs[i]);
171         }
172         talloc_free(subs);
173 }
174
175 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
176                      void *unused)
177 {
178         return strcmp((*a)->name, (*b)->name);
179 }
180
181 static void sort_files(struct list_head *list)
182 {
183         struct ccan_file **files = NULL, *f;
184         unsigned int i, num;
185
186         num = 0;
187         while ((f = list_top(list, struct ccan_file, list)) != NULL) {
188                 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
189                 files[num++] = f;
190                 list_del(&f->list);
191         }
192         asort(files, num, cmp_names, NULL);
193
194         for (i = 0; i < num; i++)
195                 list_add_tail(list, &files[i]->list);
196         talloc_free(files);
197 }
198
199 /* Walk up tp find /ccan/ => ccan directory. */
200 static unsigned int ccan_dir_prefix(const char *fulldir)
201 {
202         unsigned int i;
203
204         assert(fulldir[0] == '/');
205         for (i = strlen(fulldir) - 1; i > 0; i--) {
206                 if (strncmp(fulldir+i, "/ccan", 5) != 0)
207                         continue;
208                 if (fulldir[i+5] != '\0' && fulldir[i+5] != '/')
209                         continue;
210                 return i + 1;
211         }
212         errx(1, "Could not find /ccan/ dir in %s", fulldir);
213 }
214
215 struct manifest *get_manifest(const void *ctx, const char *dir)
216 {
217         struct manifest *m;
218         char *olddir, *canon_dir;
219         unsigned int len;
220         struct list_head *list;
221
222         if (!manifests) {
223                 manifests = talloc(NULL, struct htable_manifest);
224                 htable_manifest_init(manifests);
225         }
226
227         olddir = talloc_getcwd(NULL);
228         if (!olddir)
229                 err(1, "Getting current directory");
230
231         if (chdir(dir) != 0)
232                 err(1, "Failed to chdir to %s", dir);
233
234         canon_dir = talloc_getcwd(olddir);
235         if (!canon_dir)
236                 err(1, "Getting current directory");
237
238         m = htable_manifest_get(manifests, canon_dir);
239         if (m)
240                 goto done;
241
242         m = talloc_linked(ctx, talloc(NULL, struct manifest));
243         m->info_file = NULL;
244         m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
245         m->dir = talloc_steal(m, canon_dir);
246         list_head_init(&m->c_files);
247         list_head_init(&m->h_files);
248         list_head_init(&m->api_tests);
249         list_head_init(&m->run_tests);
250         list_head_init(&m->compile_ok_tests);
251         list_head_init(&m->compile_fail_tests);
252         list_head_init(&m->other_test_c_files);
253         list_head_init(&m->other_test_files);
254         list_head_init(&m->other_files);
255         list_head_init(&m->examples);
256         list_head_init(&m->mangled_examples);
257         list_head_init(&m->deps);
258
259         len = strlen(m->dir);
260         while (len && m->dir[len-1] == '/')
261                 m->dir[--len] = '\0';
262
263         m->basename = strrchr(m->dir, '/');
264         if (!m->basename)
265                 errx(1, "I don't expect to be run from the root directory");
266         m->basename++;
267
268         if (!ccan_dir) {
269                 unsigned int prefix = ccan_dir_prefix(m->dir);
270
271                 ccan_dir = talloc_strndup(NULL, m->dir, prefix);
272         }
273
274         add_files(m, "");
275
276         /* Nicer to run tests in a predictable order. */
277         foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
278                     &m->compile_fail_tests)
279                 sort_files(list);
280
281         htable_manifest_add(manifests, m);
282
283 done:
284         if (chdir(olddir) != 0)
285                 err(1, "Returning to original directory '%s'", olddir);
286         talloc_free(olddir);
287
288         return m;
289 }