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