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>
16 #include <sys/types.h>
25 static size_t dir_hash(const char *name)
27 return hash(name, strlen(name), 0);
30 static const char *manifest_name(const struct manifest *m)
35 static bool dir_cmp(const struct manifest *m, const char *dir)
37 return strcmp(m->dir, dir) == 0;
40 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
42 static struct htable_manifest *manifests;
44 const char *get_ccan_file_contents(struct ccan_file *f)
47 f->contents = grab_file(f, f->fullname);
49 err(1, "Reading file %s", f->fullname);
50 f->contents_size = tal_count(f->contents) - 1;
55 char **get_ccan_file_lines(struct ccan_file *f)
58 f->lines = tal_strsplit(f, get_ccan_file_contents(f), "\n",
64 struct ccan_file *new_ccan_file(const void *ctx, const char *dir,
70 assert(dir[0] == '/');
72 f = tal(ctx, struct ccan_file);
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);
82 f->idempotent_cond = NULL;
87 static void add_files(struct manifest *m, const char *base, const char *subdir)
91 char **subs = tal_arr(m, char *, 0);
97 thisdir = path_join(subs, base, subdir);
101 err(1, "Opening directory %s", thisdir);
103 while ((ent = readdir(d)) != NULL) {
106 struct list_head *dest;
109 if (ent->d_name[0] == '.')
112 f = new_ccan_file(m, m->dir,
113 subdir ? path_join(m, subdir, ent->d_name)
115 if (stat(f->fullname, &st) != 0)
116 err(1, "stat %s", f->fullname);
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, "/");
124 if (!S_ISREG(st.st_mode)) {
129 if (streq(f->name, "_info")) {
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, '/')) {
142 } else if (strstarts(f->name, "test/")) {
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;
153 dest = &m->other_test_c_files;
155 dest = &m->other_test_files;
157 dest = &m->other_files;
159 list_add(dest, &f->list);
163 /* Before we recurse, sanity check this is a ccan module. */
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);
172 /* Don't enter subdirs with _info: they're separate modules. */
173 for (i = 0; i < tal_count(subs); i++) {
175 char *subinfo = path_join(subs, base,
176 path_join(subs, subs[i],
178 if (lstat(subinfo, &st) != 0)
179 add_files(m, base, subs[i]);
185 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
186 void *unused UNNEEDED)
188 return strcmp((*a)->name, (*b)->name);
191 static void sort_files(struct list_head *list)
193 struct ccan_file **files = tal_arr(NULL, struct ccan_file *, 0), *f;
196 while ((f = list_top(list, struct ccan_file, list)) != NULL) {
197 tal_expand(&files, &f, 1);
200 asort(files, tal_count(files), cmp_names, NULL);
202 for (i = 0; i < tal_count(files); i++)
203 list_add_tail(list, &files[i]->list);
207 struct manifest *get_manifest(const void *ctx, const char *dir)
212 struct list_head *list;
215 manifests = tal(NULL, struct htable_manifest);
216 htable_manifest_init(manifests);
219 canon_dir = path_canon(ctx, dir);
221 err(1, "Getting canonical version of directory %s", dir);
223 m = htable_manifest_get(manifests, canon_dir);
227 m = tal_linkable(tal(NULL, struct manifest));
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);
245 /* Trim trailing /. */
246 len = strlen(m->dir);
247 while (len && m->dir[len-1] == '/')
248 m->dir[--len] = '\0';
250 m->basename = strrchr(m->dir, '/');
252 errx(1, "I don't expect to be run from the root directory");
255 assert(strstarts(m->dir, find_ccan_dir(m->dir)));
256 m->modname = m->dir + strlen(find_ccan_dir(m->dir)) + strlen("ccan/");
258 add_files(m, canon_dir, NULL);
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)
265 htable_manifest_add(manifests, tal_link(manifests, m));