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>
15 #include <sys/types.h>
24 static size_t dir_hash(const char *name)
26 return hash(name, strlen(name), 0);
29 static const char *manifest_name(const struct manifest *m)
34 static bool dir_cmp(const struct manifest *m, const char *dir)
36 return strcmp(m->dir, dir) == 0;
39 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
41 static struct htable_manifest *manifests;
43 const char *get_ccan_file_contents(struct ccan_file *f)
46 f->contents = tal_grab_file(f, f->fullname,
49 err(1, "Reading file %s", f->fullname);
54 char **get_ccan_file_lines(struct ccan_file *f)
57 f->lines = tal_strsplit(f, get_ccan_file_contents(f), "\n",
63 struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name)
68 assert(dir[0] == '/');
70 f = tal(ctx, struct ccan_file);
73 f->doc_sections = NULL;
74 for (i = 0; i < ARRAY_SIZE(f->compiled); i++)
75 f->compiled[i] = NULL;
76 f->name = tal_steal(f, name);
77 f->fullname = path_join(f, dir, f->name);
83 static void add_files(struct manifest *m, const char *dir)
87 char **subs = tal_arr(m, char *, 0);
94 err(1, "Opening directory %s", dir[0] ? dir : ".");
96 while ((ent = readdir(d)) != NULL) {
99 struct list_head *dest;
102 if (ent->d_name[0] == '.')
105 f = new_ccan_file(m, m->dir,
106 tal_fmt(m, "%s%s", dir, ent->d_name));
107 if (lstat(f->name, &st) != 0)
108 err(1, "lstat %s", f->name);
110 if (S_ISDIR(st.st_mode)) {
111 size_t len = tal_count(subs);
112 tal_resize(&subs, len+1);
113 subs[len] = tal_strcat(subs, f->name, "/");
116 if (!S_ISREG(st.st_mode)) {
121 if (streq(f->name, "_info")) {
126 is_c_src = strends(f->name, ".c");
127 if (!is_c_src && !strends(f->name, ".h")) {
128 dest = &m->other_files;
129 } else if (!strchr(f->name, '/')) {
134 } else if (strstarts(f->name, "test/")) {
136 if (strstarts(f->name, "test/api"))
137 dest = &m->api_tests;
138 else if (strstarts(f->name, "test/run"))
139 dest = &m->run_tests;
140 else if (strstarts(f->name, "test/compile_ok"))
141 dest = &m->compile_ok_tests;
142 else if (strstarts(f->name, "test/compile_fail"))
143 dest = &m->compile_fail_tests;
145 dest = &m->other_test_c_files;
147 dest = &m->other_test_files;
149 dest = &m->other_files;
151 list_add(dest, &f->list);
155 /* Before we recurse, sanity check this is a ccan module. */
160 && list_empty(&m->c_files)
161 && list_empty(&m->h_files))
162 errx(1, "No _info, C or H files found here!");
164 for (i = 0; i < tal_count(subs); i++)
165 add_files(m, subs[i]);
170 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
173 return strcmp((*a)->name, (*b)->name);
176 static void sort_files(struct list_head *list)
178 struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
181 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
182 tal_expand(&files, &f, 1);
185 asort(files, tal_count(files), cmp_names, NULL);
187 for (i = 0; i < tal_count(files); i++)
188 list_add_tail(list, &files[i]->list);
192 struct manifest *get_manifest(const void *ctx, const char *dir)
197 struct list_head *list;
198 struct path_pushd *old;
201 manifests = tal(NULL, struct htable_manifest);
202 htable_manifest_init(manifests);
205 /* FIXME: Use path_canon, don't chdir! */
206 old = path_pushd(ctx, dir);
208 err(1, "Failed to chdir to %s", dir);
210 canon_dir = path_cwd(old);
212 err(1, "Getting current directory");
214 m = htable_manifest_get(manifests, canon_dir);
217 m = tal_linkable(tal(NULL, struct manifest));
219 m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
220 m->dir = tal_steal(m, canon_dir);
221 list_head_init(&m->c_files);
222 list_head_init(&m->h_files);
223 list_head_init(&m->api_tests);
224 list_head_init(&m->run_tests);
225 list_head_init(&m->compile_ok_tests);
226 list_head_init(&m->compile_fail_tests);
227 list_head_init(&m->other_test_c_files);
228 list_head_init(&m->other_test_files);
229 list_head_init(&m->other_files);
230 list_head_init(&m->examples);
231 list_head_init(&m->mangled_examples);
232 list_head_init(&m->deps);
233 list_head_init(&m->test_deps);
235 /* Trim trailing /. */
236 len = strlen(m->dir);
237 while (len && m->dir[len-1] == '/')
238 m->dir[--len] = '\0';
240 m->basename = strrchr(m->dir, '/');
242 errx(1, "I don't expect to be run from the root directory");
245 assert(strstarts(m->dir, find_ccan_dir(m->dir)));
246 m->modname = m->dir + strlen(find_ccan_dir(m->dir)) + strlen("ccan/");
250 /* Nicer to run tests in a predictable order. */
251 foreach_ptr(list, &m->api_tests, &m->run_tests, &m->compile_ok_tests,
252 &m->compile_fail_tests)
255 htable_manifest_add(manifests, tal_link(manifests, m));
259 err(1, "Returning to original directory");