]> git.ozlabs.org Git - ccan/blob - tools/manifest.c
024eaa42a0d42c469e08f2ccba78846f58e0f1d5
[ccan] / tools / manifest.c
1 #include "config.h"
2 #include "manifest.h"
3 #include "tools.h"
4 #include <ccan/talloc/talloc.h>
5 #include <ccan/str/str.h>
6 #include <ccan/str_talloc/str_talloc.h>
7 #include <ccan/talloc_link/talloc_link.h>
8 #include <ccan/hash/hash.h>
9 #include <ccan/htable/htable_type.h>
10 #include <ccan/grab_file/grab_file.h>
11 #include <ccan/noerr/noerr.h>
12 #include <ccan/foreach/foreach.h>
13 #include <ccan/asort/asort.h>
14 #include <ccan/array_size/array_size.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <err.h>
20 #include <errno.h>
21 #include <dirent.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <assert.h>
25
26 static size_t dir_hash(const char *name)
27 {
28         return hash(name, strlen(name), 0);
29 }
30
31 static const char *manifest_name(const struct manifest *m)
32 {
33         return m->dir;
34 }
35
36 static bool dir_cmp(const struct manifest *m, const char *dir)
37 {
38         return strcmp(m->dir, dir) == 0;
39 }
40
41 HTABLE_DEFINE_TYPE(struct manifest, manifest_name, dir_hash, dir_cmp,
42                    htable_manifest);
43 static struct htable_manifest *manifests;
44
45 const char *get_ccan_file_contents(struct ccan_file *f)
46 {
47         if (!f->contents) {
48                 f->contents = grab_file(f, f->fullname, &f->contents_size);
49                 if (!f->contents)
50                         err(1, "Reading file %s", f->fullname);
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 = strsplit(f, get_ccan_file_contents(f), "\n");
59
60         /* FIXME: is f->num_lines necessary? */
61         f->num_lines = talloc_array_length(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 = talloc(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 = talloc_steal(f, name);
79         f->fullname = talloc_asprintf(f, "%s/%s", 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 = NULL;
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                                   talloc_asprintf(m, "%s%s",
109                                                   dir, ent->d_name));
110                 if (lstat(f->name, &st) != 0)
111                         err(1, "lstat %s", f->name);
112
113                 if (S_ISDIR(st.st_mode)) {
114                         size_t len = talloc_array_length(subs);
115                         subs = talloc_realloc(m, subs, char *, len+1);
116                         subs[len] = talloc_append_string(f->name, "/");
117                         continue;
118                 }
119                 if (!S_ISREG(st.st_mode)) {
120                         talloc_free(f);
121                         continue;
122                 }
123
124                 if (streq(f->name, "_info")) {
125                         m->info_file = f;
126                         continue;
127                 }
128
129                 is_c_src = strends(f->name, ".c");
130                 if (!is_c_src && !strends(f->name, ".h")) {
131                         dest = &m->other_files;
132                 } else if (!strchr(f->name, '/')) {
133                         if (is_c_src)
134                                 dest = &m->c_files;
135                         else
136                                 dest = &m->h_files;
137                 } else if (strstarts(f->name, "test/")) {
138                         if (is_c_src) {
139                                 if (strstarts(f->name, "test/api"))
140                                         dest = &m->api_tests;
141                                 else if (strstarts(f->name, "test/run"))
142                                         dest = &m->run_tests;
143                                 else if (strstarts(f->name, "test/compile_ok"))
144                                         dest = &m->compile_ok_tests;
145                                 else if (strstarts(f->name, "test/compile_fail"))
146                                         dest = &m->compile_fail_tests;
147                                 else
148                                         dest = &m->other_test_c_files;
149                         } else
150                                 dest = &m->other_test_files;
151                 } else
152                         dest = &m->other_files;
153
154                 list_add(dest, &f->list);
155         }
156         closedir(d);
157
158         /* Before we recurse, sanity check this is a ccan module. */
159         if (!dir[0]) {
160                 size_t i;
161
162                 if (!m->info_file
163                     && list_empty(&m->c_files)
164                     && list_empty(&m->h_files))
165                         errx(1, "No _info, C or H files found here!");
166
167                 for (i = 0; i < talloc_array_length(subs); i++)
168                         add_files(m, subs[i]);
169         }
170         talloc_free(subs);
171 }
172
173 static int cmp_names(struct ccan_file *const *a, struct ccan_file *const *b,
174                      void *unused)
175 {
176         return strcmp((*a)->name, (*b)->name);
177 }
178
179 static void sort_files(struct list_head *list)
180 {
181         struct ccan_file **files = NULL, *f;
182         unsigned int i, num;
183
184         num = 0;
185         while ((f = list_top(list, struct ccan_file, list)) != NULL) {
186                 files = talloc_realloc(NULL, files, struct ccan_file *, num+1);
187                 files[num++] = f;
188                 list_del(&f->list);
189         }
190         asort(files, num, cmp_names, NULL);
191
192         for (i = 0; i < num; i++)
193                 list_add_tail(list, &files[i]->list);
194         talloc_free(files);
195 }
196
197 struct manifest *get_manifest(const void *ctx, const char *dir)
198 {
199         struct manifest *m;
200         char *olddir, *canon_dir;
201         unsigned int len;
202         struct list_head *list;
203
204         if (!manifests) {
205                 manifests = talloc(NULL, struct htable_manifest);
206                 htable_manifest_init(manifests);
207         }
208
209         olddir = talloc_getcwd(NULL);
210         if (!olddir)
211                 err(1, "Getting current directory");
212
213         if (chdir(dir) != 0)
214                 err(1, "Failed to chdir to %s", dir);
215
216         canon_dir = talloc_getcwd(olddir);
217         if (!canon_dir)
218                 err(1, "Getting current directory");
219
220         m = htable_manifest_get(manifests, canon_dir);
221         if (m)
222                 goto done;
223
224         m = talloc_linked(ctx, talloc(NULL, struct manifest));
225         m->info_file = NULL;
226         m->compiled[COMPILE_NORMAL] = m->compiled[COMPILE_NOFEAT] = NULL;
227         m->dir = talloc_steal(m, canon_dir);
228         list_head_init(&m->c_files);
229         list_head_init(&m->h_files);
230         list_head_init(&m->api_tests);
231         list_head_init(&m->run_tests);
232         list_head_init(&m->compile_ok_tests);
233         list_head_init(&m->compile_fail_tests);
234         list_head_init(&m->other_test_c_files);
235         list_head_init(&m->other_test_files);
236         list_head_init(&m->other_files);
237         list_head_init(&m->examples);
238         list_head_init(&m->mangled_examples);
239         list_head_init(&m->deps);
240
241         len = strlen(m->dir);
242         while (len && m->dir[len-1] == '/')
243                 m->dir[--len] = '\0';
244
245         m->basename = strrchr(m->dir, '/');
246         if (!m->basename)
247                 errx(1, "I don't expect to be run from the root directory");
248         m->basename++;
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, m);
258
259 done:
260         if (chdir(olddir) != 0)
261                 err(1, "Returning to original directory '%s'", olddir);
262         talloc_free(olddir);
263
264         return m;
265 }