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