tools: use tal/path instead of opencoding most paths.
[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         /* FIXME: is f->num_lines necessary? */
61         f->num_lines = tal_count(f->lines) - 1;
62         return f->lines;
63 }
64
65 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, 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_steal(f, name);
79         f->fullname = path_join(f, dir, f->name);
80         f->contents = NULL;
81         f->simplified = NULL;
82         return f;
83 }
84
85 static void add_files(struct manifest *m, const char *dir)
86 {
87         DIR *d;
88         struct dirent *ent;
89         char **subs = tal_arr(m, char *, 0);
90
91         if (dir[0])
92                 d = opendir(dir);
93         else
94                 d = opendir(".");
95         if (!d)
96                 err(1, "Opening directory %s", dir[0] ? dir : ".");
97
98         while ((ent = readdir(d)) != NULL) {
99                 struct stat st;
100                 struct ccan_file *f;
101                 struct list_head *dest;
102                 bool is_c_src;
103
104                 if (ent->d_name[0] == '.')
105                         continue;
106
107                 f = new_ccan_file(m, m->dir,
108                                   tal_fmt(m, "%s%s", dir, ent->d_name));
109                 if (lstat(f->name, &st) != 0)
110                         err(1, "lstat %s", f->name);
111
112                 if (S_ISDIR(st.st_mode)) {
113                         size_t len = tal_count(subs);
114                         tal_resize(&subs, len+1);
115                         subs[len] = tal_strcat(subs, f->name, "/");
116                         continue;
117                 }
118                 if (!S_ISREG(st.st_mode)) {
119                         tal_free(f);
120                         continue;
121                 }
122
123                 if (streq(f->name, "_info")) {
124                         m->info_file = f;
125                         continue;
126                 }
127
128                 is_c_src = strends(f->name, ".c");
129                 if (!is_c_src && !strends(f->name, ".h")) {
130                         dest = &m->other_files;
131                 } else if (!strchr(f->name, '/')) {
132                         if (is_c_src)
133                                 dest = &m->c_files;
134                         else
135                                 dest = &m->h_files;
136                 } else if (strstarts(f->name, "test/")) {
137                         if (is_c_src) {
138                                 if (strstarts(f->name, "test/api"))
139                                         dest = &m->api_tests;
140                                 else if (strstarts(f->name, "test/run"))
141                                         dest = &m->run_tests;
142                                 else if (strstarts(f->name, "test/compile_ok"))
143                                         dest = &m->compile_ok_tests;
144                                 else if (strstarts(f->name, "test/compile_fail"))
145                                         dest = &m->compile_fail_tests;
146                                 else
147                                         dest = &m->other_test_c_files;
148                         } else
149                                 dest = &m->other_test_files;
150                 } else
151                         dest = &m->other_files;
152
153                 list_add(dest, &f->list);
154         }
155         closedir(d);
156
157         /* Before we recurse, sanity check this is a ccan module. */
158         if (!dir[0]) {
159                 size_t i;
160
161                 if (!m->info_file
162                     && list_empty(&m->c_files)
163                     && list_empty(&m->h_files))
164                         errx(1, "No _info, C or H files found here!");
165
166                 for (i = 0; i < tal_count(subs); i++)
167                         add_files(m, subs[i]);
168         }
169         tal_free(subs);
170 }
171
172 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
173                      void *unused)
174 {
175         return strcmp((*a)->name, (*b)->name);
176 }
177
178 static void sort_files(struct list_head *list)
179 {
180         struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
181         unsigned int i;
182
183         while ((f = list_top(list, struct ccan_file, list)) != NULL) {
184                 tal_expand(&files, &f, 1);
185                 list_del(&f->list);
186         }
187         asort(files, tal_count(files), cmp_names, NULL);
188
189         for (i = 0; i < tal_count(files); i++)
190                 list_add_tail(list, &files[i]->list);
191         tal_free(files);
192 }
193
194 struct manifest *get_manifest(const void *ctx, const char *dir)
195 {
196         struct manifest *m;
197         char *canon_dir;
198         unsigned int len;
199         struct list_head *list;
200         struct path_pushd *old;
201
202         if (!manifests) {
203                 manifests = tal(NULL, struct htable_manifest);
204                 htable_manifest_init(manifests);
205         }
206
207         /* FIXME: Use path_canon, don't chdir! */
208         old = path_pushd(ctx, dir);
209         if (!old)
210                 err(1, "Failed to chdir to %s", dir);
211
212         canon_dir = path_cwd(old);
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 (!path_popd(old))
261                 err(1, "Returning to original directory");
262
263         return m;
264 }