]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.c
be6829c049d8420d5cefd76931ca09bf77a49825
[ccan] / tools / ccanlint / ccanlint.c
1 /*
2  * ccanlint: assorted checks and advice for a ccan package
3  * Copyright (C) 2008 Rusty Russell, Idris Soule
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  *   This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #include "ccanlint.h"
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <err.h>
27 #include <ctype.h>
28 #include <ccan/talloc/talloc.h>
29
30 static unsigned int verbose = 0;
31 static LIST_HEAD(compulsory_tests);
32 static LIST_HEAD(normal_tests);
33 static LIST_HEAD(finished_tests);
34 bool safe_mode = false;
35
36 static void usage(const char *name)
37 {
38         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
39                 "   -v: verbose mode\n"
40                 "   -s: simply give one line per FAIL and total score\n"
41                 "   -d: use this directory instead of the current one\n"
42                 "   -n: do not compile anything\n",
43                 name);
44         exit(1);
45 }
46
47 static void indent_print(const char *string)
48 {
49         while (*string) {
50                 unsigned int line = strcspn(string, "\n");
51                 printf("\t%.*s", line, string);
52                 if (string[line] == '\n') {
53                         printf("\n");
54                         line++;
55                 }
56                 string += line;
57         }
58 }
59
60 bool ask(const char *question)
61 {
62         char reply[2];
63
64         printf("%s ", question);
65         fflush(stdout);
66
67         return fgets(reply, sizeof(reply), stdin) != NULL
68                 && toupper(reply[0]) == 'Y';
69 }
70
71 static const char *should_skip(struct manifest *m, struct ccanlint *i)
72 {
73         if (i->skip_fail)
74                 return "dependency failed";
75
76         if (i->skip)
77                 return "dependency was skipped";
78
79         if (i->can_run)
80                 return i->can_run(m);
81         return NULL;
82 }
83
84 static bool run_test(struct ccanlint *i,
85                      bool summary,
86                      unsigned int *score,
87                      unsigned int *total_score,
88                      struct manifest *m)
89 {
90         void *result;
91         unsigned int this_score;
92         const struct dependent *d;
93         const char *skip;
94
95         //one less test to run through
96         list_for_each(&i->dependencies, d, node)
97                 d->dependent->num_depends--;
98
99         skip = should_skip(m, i);
100         if (skip) {
101                 if (verbose)
102                         printf("  %s: skipped (%s)\n", i->name, skip);
103
104                 /* If we're skipping this because a prereq failed, we fail. */
105                 if (i->skip_fail)
106                         *total_score += i->total_score;
107                         
108                 list_del(&i->list);
109                 list_add_tail(&finished_tests, &i->list);
110                 list_for_each(&i->dependencies, d, node) {
111                         d->dependent->skip = true;
112                         d->dependent->skip_fail = i->skip_fail;
113                 }
114                 return true;
115         }
116
117         result = i->check(m);
118         if (!result) {
119                 if (verbose)
120                         printf("  %s: OK\n", i->name);
121                 if (i->total_score) {
122                         *score += i->total_score;
123                         *total_score += i->total_score;
124                 }
125
126                 list_del(&i->list);
127                 list_add_tail(&finished_tests, &i->list);
128                 return true;
129         }
130
131         if (i->score)
132                 this_score = i->score(m, result);
133         else
134                 this_score = 0;
135
136         list_del(&i->list);
137         list_add_tail(&finished_tests, &i->list);
138
139         *total_score += i->total_score;
140         *score += this_score;
141         if (summary) {
142                 printf("%s FAILED (%u/%u)\n",
143                        i->name, this_score, i->total_score);
144
145                 if (verbose)
146                         indent_print(i->describe(m, result));
147         } else {
148                 printf("%s\n", i->describe(m, result));
149
150                 if (i->handle)
151                         i->handle(m, result);
152         }
153
154         /* Skip any tests which depend on this one. */
155         list_for_each(&i->dependencies, d, node) {
156                 d->dependent->skip = true;
157                 d->dependent->skip_fail = true;
158         }
159
160         return false;
161 }
162
163 static void register_test(struct ccanlint *test, ...)
164 {
165         va_list ap;
166         struct ccanlint *depends;
167         struct dependent *dchild;
168
169         if (!test->total_score)
170                 list_add(&compulsory_tests, &test->list);
171         else
172                 list_add(&normal_tests, &test->list);
173
174         va_start(ap, test);
175         /* Careful: we might have been initialized by a dependent. */
176         if (test->dependencies.n.next == NULL)
177                 list_head_init(&test->dependencies);
178
179         //dependent(s) args (if any), last one is NULL
180         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
181                 dchild = malloc(sizeof(*dchild));
182                 dchild->dependent = test;
183                 /* The thing we depend on might not be initialized yet! */
184                 if (depends->dependencies.n.next == NULL)
185                         list_head_init(&depends->dependencies);
186                 list_add_tail(&depends->dependencies, &dchild->node);
187                 test->num_depends++;
188         }
189         va_end(ap);
190 }
191
192 /**
193  * get_next_test - retrieves the next test to be processed
194  **/
195 static inline struct ccanlint *get_next_test(struct list_head *test)
196 {
197         struct ccanlint *i;
198
199         if (list_empty(test))
200                 return NULL;
201
202         list_for_each(test, i, list) {
203                 if (i->num_depends == 0)
204                         return i;
205         }
206         errx(1, "Can't make process; test dependency cycle");
207 }
208
209 static void init_tests(void)
210 {
211         const struct ccanlint *i;
212
213 #undef REGISTER_TEST
214 #define REGISTER_TEST(name, ...) register_test(&name, __VA_ARGS__)
215 #include "generated-init-tests"
216
217         if (!verbose)
218                 return;
219
220         printf("\nCompulsory Tests\n");
221         list_for_each(&compulsory_tests, i, list) {
222                 printf("%s depends on %u others\n", i->name, i->num_depends);
223                 if (!list_empty(&i->dependencies)) {
224                         const struct dependent *d;
225                         printf("These depend on us:\n");
226                         list_for_each(&i->dependencies, d, node)
227                                 printf("\t%s\n", d->dependent->name);
228                 }
229         }
230
231         printf("\nNormal Tests\n");
232         list_for_each(&normal_tests, i, list) {
233                 printf("%s depends on %u others\n", i->name, i->num_depends);
234                 if (!list_empty(&i->dependencies)) {
235                         const struct dependent *d;
236                         printf("These depend on us:\n");
237                         list_for_each(&i->dependencies, d, node)
238                                 printf("\t%s\n", d->dependent->name);
239                 }
240         }
241 }
242
243 int main(int argc, char *argv[])
244 {
245         int c;
246         bool summary = false;
247         unsigned int score, total_score;
248         struct manifest *m;
249         struct ccanlint *i;
250
251         /* I'd love to use long options, but that's not standard. */
252         /* FIXME: getopt_long ccan package? */
253         while ((c = getopt(argc, argv, "sd:vn")) != -1) {
254                 switch (c) {
255                 case 'd':
256                         if (chdir(optarg) != 0)
257                                 err(1, "Changing into directory '%s'", optarg);
258                         break;
259                 case 's':
260                         summary = true;
261                         break;
262                 case 'v':
263                         verbose++;
264                         break;
265                 case 'n':
266                         safe_mode = true;
267                         break;
268                 default:
269                         usage(argv[0]);
270                 }
271         }
272
273         if (optind < argc)
274                 usage(argv[0]);
275
276         m = get_manifest(talloc_autofree_context());
277
278         init_tests();
279
280         /* If you don't pass the compulsory tests, you don't even get a score */
281         if (verbose)
282                 printf("Compulsory tests:\n");
283
284         while ((i = get_next_test(&compulsory_tests)) != NULL) {
285                 if (!run_test(i, summary, &score, &total_score, m))
286                         exit(1);
287         }
288
289         if (verbose)
290                 printf("\nNormal tests:\n");
291         score = total_score = 0;
292         while ((i = get_next_test(&normal_tests)) != NULL) {
293                 if (i->total_score)
294                         run_test(i, summary, &score, &total_score, m);
295         }
296         printf("Total score: %u/%u\n", score, total_score);
297         return 0;
298 }