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