2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell, Idris Soule
4 * Copyright (C) 2010 Rusty Russell, Idris Soule
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <ccan/btree/btree.h>
29 #include <ccan/str/str.h>
30 #include <ccan/str_talloc/str_talloc.h>
31 #include <ccan/talloc/talloc.h>
32 #include <ccan/opt/opt.h>
33 #include <ccan/foreach/foreach.h>
34 #include <ccan/grab_file/grab_file.h>
37 static LIST_HEAD(compulsory_tests);
38 static LIST_HEAD(normal_tests);
39 static LIST_HEAD(finished_tests);
40 bool safe_mode = false;
41 static struct btree *cmdline_exclude;
42 static struct btree *info_exclude;
43 static unsigned int timeout;
45 /* These are overridden at runtime if we can find config.h */
46 char *compiler = NULL;
49 const char *config_header;
52 static void indent_print(const char *string)
55 unsigned int line = strcspn(string, "\n");
56 printf("\t%.*s", line, string);
57 if (string[line] == '\n') {
66 bool ask(const char *question)
70 printf("%s ", question);
73 return fgets(reply, sizeof(reply), stdin) != NULL
74 && toupper(reply[0]) == 'Y';
77 static const char *should_skip(struct manifest *m, struct ccanlint *i)
79 if (btree_lookup(cmdline_exclude, i->key))
80 return "excluded on command line";
82 if (btree_lookup(info_exclude, i->key))
83 return "excluded in _info file";
89 return "dependency failed";
96 static bool run_test(struct ccanlint *i,
98 unsigned int *running_score,
99 unsigned int *running_total,
102 unsigned int timeleft;
103 const struct dependent *d;
107 //one less test to run through
108 list_for_each(&i->dependencies, d, node)
109 d->dependent->num_depends--;
111 score = talloc(m, struct score);
112 list_head_init(&score->per_file_errors);
118 skip = should_skip(m, i);
122 if (verbose && !streq(skip, "not relevant to target"))
123 printf("%s: skipped (%s)\n", i->name, skip);
125 /* If we're skipping this because a prereq failed, we fail:
126 * count it as a score of 1. */
131 list_add_tail(&finished_tests, &i->list);
132 list_for_each(&i->dependencies, d, node) {
133 if (d->dependent->skip)
135 d->dependent->skip = "dependency was skipped";
136 d->dependent->skip_fail = i->skip_fail;
138 return i->skip_fail ? false : true;
141 timeleft = timeout ? timeout : default_timeout_ms;
142 i->check(m, i->keep_results, &timeleft, score);
143 if (timeout && timeleft == 0) {
148 assert(score->score <= score->total);
149 if ((!score->pass && !quiet)
150 || (score->score < score->total && verbose)
152 printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
153 if (score->total > 1)
154 printf(" (+%u/%u)", score->score, score->total);
158 if ((!quiet && !score->pass) || verbose) {
160 printf("%s%s", score->error,
161 strends(score->error, "\n") ? "" : "\n");
163 if (!quiet && !score->pass && i->handle)
167 *running_score += score->score;
168 *running_total += score->total;
171 list_add_tail(&finished_tests, &i->list);
174 /* Skip any tests which depend on this one. */
175 list_for_each(&i->dependencies, d, node) {
176 if (d->dependent->skip)
178 d->dependent->skip = "dependency failed";
179 d->dependent->skip_fail = true;
185 static void register_test(struct list_head *h, struct ccanlint *test)
187 list_add(h, &test->list);
191 * get_next_test - retrieves the next test to be processed
193 static inline struct ccanlint *get_next_test(struct list_head *test)
197 if (list_empty(test))
200 list_for_each(test, i, list) {
201 if (i->num_depends == 0)
204 errx(1, "Can't make process; test dependency cycle");
207 static struct ccanlint *find_test(const char *key)
211 list_for_each(&compulsory_tests, i, list)
212 if (streq(i->key, key))
215 list_for_each(&normal_tests, i, list)
216 if (streq(i->key, key))
223 #define REGISTER_TEST(name, ...) extern struct ccanlint name
224 #include "generated-normal-tests"
225 #include "generated-compulsory-tests"
227 static void init_tests(void)
230 struct btree *keys, *names;
231 struct list_head *list;
234 #define REGISTER_TEST(name) register_test(&normal_tests, &name)
235 #include "generated-normal-tests"
237 #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
238 #include "generated-compulsory-tests"
240 /* Initialize dependency lists. */
241 foreach_ptr(list, &compulsory_tests, &normal_tests) {
242 list_for_each(list, c, list) {
243 list_head_init(&c->dependencies);
247 /* Resolve dependencies. */
248 foreach_ptr(list, &compulsory_tests, &normal_tests) {
249 list_for_each(list, c, list) {
250 char **deps = strsplit(NULL, c->needs, " ");
253 for (i = 0; deps[i]; i++) {
254 struct ccanlint *dep;
255 struct dependent *dchild;
257 dep = find_test(deps[i]);
259 errx(1, "BUG: unknown dep '%s' for %s",
261 dchild = talloc(NULL, struct dependent);
262 dchild->dependent = c;
263 list_add_tail(&dep->dependencies,
271 /* Self-consistency check: make sure no two tests
272 have the same key or name. */
273 keys = btree_new(btree_strcmp);
274 names = btree_new(btree_strcmp);
275 foreach_ptr(list, &compulsory_tests, &normal_tests) {
276 list_for_each(list, c, list) {
277 if (!btree_insert(keys, c->key))
278 errx(1, "BUG: Duplicate test key '%s'",
280 if (!btree_insert(names, c->name))
281 errx(1, "BUG: Duplicate test name '%s'",
291 foreach_ptr(list, &compulsory_tests, &normal_tests) {
292 printf("\%s Tests\n",
293 list == &compulsory_tests ? "Compulsory" : "Normal");
295 if (!list_empty(&c->dependencies)) {
296 const struct dependent *d;
297 printf("These depend on us:\n");
298 list_for_each(&c->dependencies, d, node)
299 printf("\t%s\n", d->dependent->name);
304 static int show_tmpdir(char *dir)
306 printf("You can find ccanlint working files in '%s'\n", dir);
310 static char *keep_test(const char *testname, void *unused)
314 if (streq(testname, "all")) {
315 struct list_head *list;
316 foreach_ptr(list, &compulsory_tests, &normal_tests) {
317 list_for_each(list, i, list)
318 i->keep_results = true;
321 i = find_test(testname);
323 errx(1, "No test %s to --keep", testname);
324 i->keep_results = true;
327 /* Don't automatically destroy temporary dir. */
328 talloc_set_destructor(temp_dir(NULL), show_tmpdir);
332 static char *skip_test(const char *testname, void *unused)
334 btree_insert(cmdline_exclude, testname);
338 static void print_tests(struct list_head *tests, const char *type)
342 printf("%s tests:\n", type);
343 /* This makes them print in topological order. */
344 while ((i = get_next_test(tests)) != NULL) {
345 const struct dependent *d;
346 printf(" %-25s %s\n", i->key, i->name);
348 list_for_each(&i->dependencies, d, node)
349 d->dependent->num_depends--;
353 static char *list_tests(void *arg)
355 print_tests(&compulsory_tests, "Compulsory");
356 print_tests(&normal_tests, "Normal");
360 static void test_dgraph_vertices(struct list_head *tests, const char *style)
362 const struct ccanlint *i;
364 list_for_each(tests, i, list) {
366 * todo: escape labels in case ccanlint test keys have
367 * characters interpreted as GraphViz syntax.
369 printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
373 static void test_dgraph_edges(struct list_head *tests)
375 const struct ccanlint *i;
376 const struct dependent *d;
378 list_for_each(tests, i, list)
379 list_for_each(&i->dependencies, d, node)
380 printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
383 static char *test_dependency_graph(void *arg)
387 test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
388 test_dgraph_vertices(&normal_tests, "");
390 test_dgraph_edges(&compulsory_tests);
391 test_dgraph_edges(&normal_tests);
398 /* Remove empty lines. */
399 static char **collapse(char **lines, unsigned int *nump)
402 for (i = j = 0; lines[i]; i++) {
404 lines[j++] = lines[i];
411 static void add_info_options(struct ccan_file *info, bool mark_fails)
413 struct doc_section *d;
415 struct ccanlint *test;
417 list_for_each(get_ccan_file_docs(info), d, list) {
418 if (!streq(d->type, "ccanlint"))
421 for (i = 0; i < d->num_lines; i++) {
422 char **words = collapse(strsplit(d, d->lines[i], " \t"),
427 if (strncmp(words[0], "//", 2) == 0)
430 test = find_test(words[0]);
432 warnx("%s: unknown ccanlint test '%s'",
433 info->fullname, words[0]);
438 warnx("%s: no argument to test '%s'",
439 info->fullname, words[0]);
444 if (strcasecmp(words[1], "FAIL") == 0) {
446 btree_insert(info_exclude, words[0]);
448 if (!test->takes_options)
449 warnx("%s: %s doesn't take options",
450 info->fullname, words[0]);
451 /* Copy line exactly into options. */
452 test->options = strstr(d->lines[i], words[0])
459 static bool depends_on(struct ccanlint *i, struct ccanlint *target)
461 const struct dependent *d;
466 list_for_each(&i->dependencies, d, node) {
467 if (depends_on(d->dependent, target))
473 /* O(N^2), who cares? */
474 static void skip_unrelated_tests(struct ccanlint *target)
477 struct list_head *list;
479 foreach_ptr(list, &compulsory_tests, &normal_tests)
480 list_for_each(list, i, list)
481 if (!depends_on(i, target))
482 i->skip = "not relevant to target";
485 static char *demangle_string(char *string)
488 const char mapfrom[] = "abfnrtv";
489 const char mapto[] = "\a\b\f\n\r\t\v";
491 if (!strchr(string, '"'))
493 string = strchr(string, '"') + 1;
494 if (!strrchr(string, '"'))
496 *strrchr(string, '"') = '\0';
498 for (i = 0; i < strlen(string); i++) {
499 if (string[i] == '\\') {
502 char *p = strchr(mapfrom, string[i+1]);
504 repl = mapto[p - mapfrom];
506 } else if (strlen(string+i+1) >= 3) {
507 if (string[i+1] == 'x') {
508 repl = (string[i+2]-'0')*16
511 } else if (isdigit(string[i+1])) {
512 repl = (string[i+2]-'0')*8*8
513 + (string[i+3]-'0')*8
524 memmove(string + i + 1, string + i + len + 1,
525 strlen(string + i + len + 1) + 1);
533 static void read_config_header(void)
535 char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
539 config_header = grab_file(NULL, fname, NULL);
540 if (!config_header) {
545 lines = strsplit(config_header, config_header, "\n");
546 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
548 const char **line = (const char **)&lines[i];
550 if (!get_token(line, "#"))
552 if (!get_token(line, "define"))
554 sym = get_symbol_token(lines, line);
555 if (streq(sym, "CCAN_COMPILER") && !compiler) {
556 compiler = demangle_string(lines[i]);
558 errx(1, "%s:%u:could not parse CCAN_COMPILER",
561 printf("%s: compiler set to '%s'\n",
563 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
564 cflags = demangle_string(lines[i]);
566 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
569 printf("%s: compiler flags set to '%s'\n",
574 compiler = CCAN_COMPILER;
576 compiler = CCAN_CFLAGS;
579 int main(int argc, char *argv[])
581 bool summary = false;
582 unsigned int score = 0, total_score = 0;
585 const char *prefix = "";
586 char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
590 cmdline_exclude = btree_new(btree_strcmp);
591 info_exclude = btree_new(btree_strcmp);
593 opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
594 "use this directory");
595 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
596 "do not compile anything");
597 opt_register_noarg("-l|--list-tests", list_tests, NULL,
598 "list tests ccanlint performs (and exit)");
599 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
600 "print dependency graph of tests in Graphviz .dot format");
601 opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
602 "keep results of <testname>"
603 " (can be used multiple times, or 'all')");
604 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
605 "simply give one line summary");
606 opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
607 "verbose mode (up to -vvvv)");
608 opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
609 "exclude <testname> (can be used multiple times)");
610 opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
612 "ignore (terminate) tests that are slower than this");
613 opt_register_arg("--target <testname>", opt_set_charp,
615 "only run one test (and its prerequisites)");
616 opt_register_arg("--compiler <compiler>", opt_set_charp,
617 NULL, &compiler, "set the compiler");
618 opt_register_arg("--cflags <flags>", opt_set_charp,
619 NULL, &cflags, "set the compiler flags");
620 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
621 "\nA program for checking and guiding development"
623 "This usage message");
625 /* We move into temporary directory, so gcov dumps its files there. */
626 if (chdir(temp_dir(talloc_autofree_context())) != 0)
627 err(1, "Error changing to %s temporary dir", temp_dir(NULL));
629 opt_parse(&argc, argv, opt_log_stderr_exit);
632 dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
633 while (strends(dir, "/"))
634 dir[strlen(dir)-1] = '\0';
636 prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
638 compile_verbose = true;
640 tools_verbose = true;
642 m = get_manifest(talloc_autofree_context(), dir);
643 read_config_header();
645 /* Create a symlink from temp dir back to src dir's test directory. */
646 if (symlink(talloc_asprintf(m, "%s/test", dir),
647 talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
648 err(1, "Creating test symlink in %s", temp_dir(NULL));
651 struct ccanlint *test;
653 test = find_test(target);
655 errx(1, "Unknown test to run '%s'", target);
656 skip_unrelated_tests(test);
659 /* If you don't pass the compulsory tests, you get a score of 0. */
660 while ((i = get_next_test(&compulsory_tests)) != NULL) {
661 if (!run_test(i, summary, &score, &total_score, m)) {
662 printf("%sTotal score: 0/%u\n", prefix, total_score);
663 errx(1, "%s%s failed", prefix, i->name);
667 /* --target overrides known FAIL from _info */
669 add_info_options(m->info_file, !target);
671 while ((i = get_next_test(&normal_tests)) != NULL)
672 run_test(i, summary, &score, &total_score, m);
674 printf("%sTotal score: %u/%u\n", prefix, score, total_score);