]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/ccanlint.c
064e40d869d6703da46270ce16e67a95488d3895
[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 "../tools.h"
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <err.h>
28 #include <ctype.h>
29 #include <ccan/talloc/talloc.h>
30
31 static unsigned int verbose = 0;
32 static LIST_HEAD(compulsory_tests);
33 static LIST_HEAD(normal_tests);
34 static LIST_HEAD(finished_tests);
35 bool safe_mode = false;
36
37 static void usage(const char *name)
38 {
39         fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
40                 "   -v: verbose mode\n"
41                 "   -s: simply give one line summary\n"
42                 "   -d: use this directory instead of the current one\n"
43                 "   -n: do not compile anything\n",
44                 name);
45         exit(1);
46 }
47
48 #if 0
49 static void indent_print(const char *string)
50 {
51         while (*string) {
52                 unsigned int line = strcspn(string, "\n");
53                 printf("\t%.*s", line, string);
54                 if (string[line] == '\n') {
55                         printf("\n");
56                         line++;
57                 }
58                 string += line;
59         }
60 }
61 #endif
62
63 bool ask(const char *question)
64 {
65         char reply[2];
66
67         printf("%s ", question);
68         fflush(stdout);
69
70         return fgets(reply, sizeof(reply), stdin) != NULL
71                 && toupper(reply[0]) == 'Y';
72 }
73
74 static const char *should_skip(struct manifest *m, struct ccanlint *i)
75 {
76         if (i->skip_fail)
77                 return "dependency failed";
78
79         if (i->skip)
80                 return "dependency was skipped";
81
82         if (i->can_run)
83                 return i->can_run(m);
84         return NULL;
85 }
86
87 static bool run_test(struct ccanlint *i,
88                      bool quiet,
89                      unsigned int *score,
90                      unsigned int *total_score,
91                      struct manifest *m)
92 {
93         void *result;
94         unsigned int this_score;
95         const struct dependent *d;
96         const char *skip;
97
98         //one less test to run through
99         list_for_each(&i->dependencies, d, node)
100                 d->dependent->num_depends--;
101
102         skip = should_skip(m, i);
103         if (skip) {
104                 if (verbose)
105                         printf("  %s: skipped (%s)\n", i->name, skip);
106
107                 /* If we're skipping this because a prereq failed, we fail. */
108                 if (i->skip_fail)
109                         *total_score += i->total_score;
110                         
111                 list_del(&i->list);
112                 list_add_tail(&finished_tests, &i->list);
113                 list_for_each(&i->dependencies, d, node) {
114                         d->dependent->skip = true;
115                         d->dependent->skip_fail = i->skip_fail;
116                 }
117                 return true;
118         }
119
120         result = i->check(m);
121         if (!result) {
122                 if (verbose)
123                         printf("  %s: OK\n", i->name);
124                 if (i->total_score) {
125                         *score += i->total_score;
126                         *total_score += i->total_score;
127                 }
128
129                 list_del(&i->list);
130                 list_add_tail(&finished_tests, &i->list);
131                 return true;
132         }
133
134         if (i->score)
135                 this_score = i->score(m, result);
136         else
137                 this_score = 0;
138
139         list_del(&i->list);
140         list_add_tail(&finished_tests, &i->list);
141
142         *total_score += i->total_score;
143         *score += this_score;
144         if (!quiet) {
145                 printf("%s\n", i->describe(m, result));
146
147                 if (i->handle)
148                         i->handle(m, result);
149         }
150
151         /* Skip any tests which depend on this one. */
152         list_for_each(&i->dependencies, d, node) {
153                 d->dependent->skip = true;
154                 d->dependent->skip_fail = true;
155         }
156
157         return false;
158 }
159
160 static void register_test(struct list_head *h, struct ccanlint *test, ...)
161 {
162         va_list ap;
163         struct ccanlint *depends;
164         struct dependent *dchild;
165
166         list_add(h, &test->list);
167
168         va_start(ap, test);
169         /* Careful: we might have been initialized by a dependent. */
170         if (test->dependencies.n.next == NULL)
171                 list_head_init(&test->dependencies);
172
173         //dependent(s) args (if any), last one is NULL
174         while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
175                 dchild = malloc(sizeof(*dchild));
176                 dchild->dependent = test;
177                 /* The thing we depend on might not be initialized yet! */
178                 if (depends->dependencies.n.next == NULL)
179                         list_head_init(&depends->dependencies);
180                 list_add_tail(&depends->dependencies, &dchild->node);
181                 test->num_depends++;
182         }
183         va_end(ap);
184 }
185
186 /**
187  * get_next_test - retrieves the next test to be processed
188  **/
189 static inline struct ccanlint *get_next_test(struct list_head *test)
190 {
191         struct ccanlint *i;
192
193         if (list_empty(test))
194                 return NULL;
195
196         list_for_each(test, i, list) {
197                 if (i->num_depends == 0)
198                         return i;
199         }
200         errx(1, "Can't make process; test dependency cycle");
201 }
202
203 static void init_tests(void)
204 {
205         const struct ccanlint *i;
206
207 #undef REGISTER_TEST
208 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
209 #include "generated-normal-tests"
210 #undef REGISTER_TEST
211 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
212 #include "generated-compulsory-tests"
213
214         if (!verbose)
215                 return;
216
217         printf("\nCompulsory Tests\n");
218         list_for_each(&compulsory_tests, i, list) {
219                 printf("%s depends on %u others\n", i->name, i->num_depends);
220                 if (!list_empty(&i->dependencies)) {
221                         const struct dependent *d;
222                         printf("These depend on us:\n");
223                         list_for_each(&i->dependencies, d, node)
224                                 printf("\t%s\n", d->dependent->name);
225                 }
226         }
227
228         printf("\nNormal Tests\n");
229         list_for_each(&normal_tests, i, list) {
230                 printf("%s depends on %u others\n", i->name, i->num_depends);
231                 if (!list_empty(&i->dependencies)) {
232                         const struct dependent *d;
233                         printf("These depend on us:\n");
234                         list_for_each(&i->dependencies, d, node)
235                                 printf("\t%s\n", d->dependent->name);
236                 }
237         }
238 }
239
240 int main(int argc, char *argv[])
241 {
242         int c;
243         bool summary = false;
244         unsigned int score = 0, total_score = 0;
245         struct manifest *m;
246         struct ccanlint *i;
247         const char *prefix = "", *dir = ".";
248
249         /* I'd love to use long options, but that's not standard. */
250         /* FIXME: getopt_long ccan package? */
251         while ((c = getopt(argc, argv, "sd:vn")) != -1) {
252                 switch (c) {
253                 case 'd':
254                         dir = optarg;
255                         prefix = talloc_append_string(talloc_basename(NULL,
256                                                                       optarg),
257                                                       ": ");
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(), dir);
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                         errx(1, "%s%s failed", prefix, i->name);
287                 }
288         }
289
290         if (verbose)
291                 printf("\nNormal tests:\n");
292         while ((i = get_next_test(&normal_tests)) != NULL)
293                 run_test(i, summary, &score, &total_score, m);
294
295         printf("%sTotal score: %u/%u\n", prefix, score, total_score);
296         return 0;
297 }