]> git.ozlabs.org Git - ccan/blob - tools/manifest.c
tools/ccanlint: detect more unmentioned dependencies.
[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/path/path.h>
7 #include <ccan/hash/hash.h>
8 #include <ccan/htable/htable_type.h>
9 #include <ccan/noerr/noerr.h>
10 #include <ccan/foreach/foreach.h>
11 #include <ccan/asort/asort.h>
12 #include <ccan/array_size/array_size.h>
13 #include <ccan/err/err.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <dirent.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <assert.h>
23
24 static size_t dir_hash(const char *name)
25 {
26         return hash(name, strlen(name), 0);
27 }
28
29 static const char *manifest_name(const struct manifest *m)
30 {
31         return m->dir;
32 }
33
34 static bool dir_cmp(const struct manifest *m, const char *dir)
35 {
36         return strcmp(m->dir, dir) == 0;
37 }
38
39 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
40                    htable_manifest);
41 static struct htable_manifest *manifests;
42
43 const char *get_ccan_file_contents(struct ccan_file *f)
44 {
45         if (!f->contents) {
46                 f->contents = tal_grab_file(f, f->fullname,
47                                                &f->contents_size);
48                 if (!f->contents)
49                         err(1, "Reading file %s", f->fullname);
50         }
51         return f->contents;
52 }
53
54 char **get_ccan_file_lines(struct ccan_file *f)
55 {
56         if (!f->lines)
57                 f->lines = tal_strsplit(f, get_ccan_file_contents(f), "\n",
58                                         STR_EMPTY_OK);
59
60         return f->lines;
61 }
62
63 struct ccan_file *new_ccan_file(const void *ctx, const char *dir,
64                                 const 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_strdup(f, name);
78         f->fullname = path_join(f, dir, f->name);
79         f->contents = NULL;
80         f->simplified = NULL;
81         f->idempotent_cond = NULL;
82
83         return f;
84 }
85
86 static void add_files(struct manifest *m, const char *base, const char *subdir)
87 {
88         DIR *d;
89         struct dirent *ent;
90         char **subs = tal_arr(m, char *, 0);
91         const char *thisdir;
92
93         if (!subdir)
94                 thisdir = base;
95         else
96                 thisdir = path_join(subs, base, subdir);
97
98         d = opendir(thisdir);
99         if (!d)
100                 err(1, "Opening directory %s", thisdir);
101
102         while ((ent = readdir(d)) != NULL) {
103                 struct stat st;
104                 struct ccan_file *f;
105                 struct list_head *dest;
106                 bool is_c_src;
107
108                 if (ent->d_name[0] == '.')
109                         continue;
110
111                 f = new_ccan_file(m, m->dir,
112                                   subdir ? path_join(m, subdir, ent->d_name)
113                                   : ent->d_name);
114                 if (lstat(f->fullname, &st) != 0)
115                         err(1, "lstat %s", f->fullname);
116
117                 if (S_ISDIR(st.st_mode)) {
118                         size_t len = tal_count(subs);
119                         tal_resize(&subs, len+1);
120                         subs[len] = tal_strcat(subs, f->name, "/");
121                         continue;
122                 }
123                 if (!S_ISREG(st.st_mode)) {
124                         tal_free(f);
125                         continue;
126                 }
127
128                 if (streq(f->name, "_info")) {
129                         m->info_file = f;
130                         continue;
131                 }
132
133                 is_c_src = strends(f->name, ".c");
134                 if (!is_c_src && !strends(f->name, ".h")) {
135                         dest = &m->other_files;
136                 } else if (!strchr(f->name, '/')) {
137                         if (is_c_src)
138                                 dest = &m->c_files;
139                         else
140                                 dest = &m->h_files;
141                 } else if (strstarts(f->name, "test/")) {
142                         if (is_c_src) {
143                                 if (strstarts(f->name, "test/api"))
144                                         dest = &m->api_tests;
145                                 else if (strstarts(f->name, "test/run"))
146                                         dest = &m->run_tests;
147                                 else if (strstarts(f->name, "test/compile_ok"))
148                                         dest = &m->compile_ok_tests;
149                                 else if (strstarts(f->name, "test/compile_fail"))
150                                         dest = &m->compile_fail_tests;
151                                 else
152                                         dest = &m->other_test_c_files;
153                         } else
154                                 dest = &m->other_test_files;
155                 } else
156                         dest = &m->other_files;
157
158                 list_add(dest, &f->list);
159         }
160         closedir(d);
161
162         /* Before we recurse, sanity check this is a ccan module. */
163         if (!subdir) {
164                 size_t i;
165
166                 if (!m->info_file
167                     && list_empty(&m->c_files)
168                     && list_empty(&m->h_files))
169                         errx(1, "No _info, C or H files found here!");
170
171                 for (i = 0; i < tal_count(subs); i++)
172                         add_files(m, base, subs[i]);
173         }
174         tal_free(subs);
175 }
176
177 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
178                      void *unused)
179 {
180         return strcmp((*a)->name, (*b)->name);
181 }
182
183 static void sort_files(struct list_head *list)
184 {
185         struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
186         unsigned int i;
187
188         while ((f = list_top(list, struct ccan_file, list)) != NULL) {
189                 tal_expand(&files, &f, 1);
190                 list_del(&f->list);
191         }
192         asort(files, tal_count(files), cmp_names, NULL);
193
194         for (i = 0; i < tal_count(files); i++)
195                 list_add_tail(list, &files[i]->list);
196         tal_free(files);
197 }
198
199 struct manifest *get_manifest(const void *ctx, const char *dir)
200 {
201         struct manifest *m;
202         char *canon_dir;
203         unsigned int len;
204         struct list_head *list;
205
206         if (!manifests) {
207                 manifests = tal(NULL, struct htable_manifest);
208                 htable_manifest_init(manifests);
209         }
210
211         canon_dir = path_canon(ctx, dir);
212         if (!canon_dir)
213                 err(1, "Getting canonical version of directory %s", dir);
214
215         m = htable_manifest_get(manifests, canon_dir);
216         if (m)
217                 return m;
218
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, canon_dir, NULL);
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         return m;
260 }