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.
22 #include "../read_config_header.h"
29 #include <ccan/str/str.h>
30 #include <ccan/take/take.h>
31 #include <ccan/opt/opt.h>
32 #include <ccan/foreach/foreach.h>
33 #include <ccan/cast/cast.h>
34 #include <ccan/tlist/tlist.h>
35 #include <ccan/tal/path/path.h>
36 #include <ccan/strmap/strmap.h>
38 typedef STRMAP(struct ccanlint *) ccanlint_map_t;
41 static ccanlint_map_t tests;
42 bool safe_mode = false;
43 bool keep_results = false;
44 bool non_ccan_deps = false;
45 bool build_failed = false;
46 static bool targeting = false;
47 static unsigned int timeout;
49 const char *config_header;
54 static void indent_print(const char *string)
57 unsigned int line = strcspn(string, "\n");
58 printf("\t%.*s", line, string);
59 if (string[line] == '\n') {
68 bool ask(const char *question)
72 printf("%s ", question);
75 return fgets(reply, sizeof(reply), stdin) != NULL
76 && toupper(reply[0]) == 'Y';
79 /* Skip, but don't remove. */
80 static bool skip_test(struct dgraph_node *node, const char *why)
82 struct ccanlint *c = container_of(node, struct ccanlint, node);
87 static const char *dep_failed(struct manifest *m UNNEEDED)
89 return "dependency couldn't run";
92 static bool cannot_run(struct dgraph_node *node, void *all UNNEEDED)
94 struct ccanlint *c = container_of(node, struct ccanlint, node);
95 c->can_run = dep_failed;
102 unsigned int score, total;
108 static bool run_test(struct dgraph_node *n, struct run_info *run)
110 struct ccanlint *i = container_of(n, struct ccanlint, node);
111 unsigned int timeleft;
117 score = tal(run->m, struct score);
118 list_head_init(&score->per_file_errors);
124 /* We can see skipped things in two cases:
125 * (1) _info excluded them (presumably because they fail).
126 * (2) A prerequisite failed.
130 printf("%s%s: skipped (%s)\n",
131 run->prefix, i->name, i->skip);
132 /* Pass us up to the test which failed, not us. */
138 i->skip = i->can_run(run->m);
140 /* Test doesn't apply, or can't run? That's OK. */
142 printf("%s%s: skipped (%s)\n",
143 run->prefix, i->name, i->skip);
144 /* Mark our dependencies to skip. */
145 dgraph_traverse_from(&i->node, cannot_run, NULL);
152 timeleft = timeout ? timeout : default_timeout_ms;
153 i->check(run->m, &timeleft, score);
154 if (timeout && timeleft == 0) {
157 printf("%s%s: skipped (%s)\n",
158 run->prefix, i->name, i->skip);
159 /* Mark our dependencies to skip. */
160 dgraph_traverse_from(&i->node, skip_test,
161 "dependency timed out");
167 assert(score->score <= score->total);
169 || (score->score < score->total && verbose)
171 printf("%s%s (%s): %s",
172 run->prefix, i->name, i->key,
173 score->pass ? "PASS" : "FAIL");
174 if (score->total > 1)
175 printf(" (+%u/%u)", score->score, score->total);
179 if (!score->pass || verbose) {
181 printf("%s%s", score->error,
182 strends(score->error, "\n") ? "" : "\n");
185 if (!run->noninteractive && score->score < score->total && i->handle)
186 i->handle(run->m, score);
189 /* Skip any tests which depend on this one. */
190 dgraph_traverse_from(&i->node, skip_test, "dependency failed");
194 run->score += score->score;
195 run->total += score->total;
197 /* FIXME: Free score. */
198 run->pass &= score->pass;
201 if (!score->pass && i->compulsory) {
202 warnx("%s%s failed", run->prefix, i->name);
209 static void register_test(struct ccanlint *test)
211 if (!strmap_add(&tests, test->key, test))
212 err(1, "Adding test %s", test->key);
213 test->options = tal_arr(NULL, char *, 1);
214 test->options[0] = NULL;
215 dgraph_init_node(&test->node);
218 static bool get_test(const char *member UNNEEDED, struct ccanlint *i,
219 struct ccanlint **ret)
221 if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
229 * get_next_test - retrieves the next test to be processed
231 static inline struct ccanlint *get_next_test(void)
233 struct ccanlint *i = NULL;
235 strmap_iterate(&tests, get_test, &i);
239 if (strmap_empty(&tests))
242 errx(1, "Can't make process; test dependency cycle");
245 static struct ccanlint *find_test(const char *key)
247 return strmap_get(&tests, key);
250 bool is_excluded(const char *name)
252 return find_test(name)->skip != NULL;
255 static bool init_deps(const char *member UNNEEDED,
256 struct ccanlint *c, void *unused UNNEEDED)
258 char **deps = tal_strsplit(NULL, c->needs, " ", STR_EMPTY_OK);
261 for (i = 0; deps[i]; i++) {
262 struct ccanlint *dep;
264 dep = find_test(deps[i]);
266 errx(1, "BUG: unknown dep '%s' for %s",
268 dgraph_add_edge(&dep->node, &c->node);
274 static bool check_names(const char *member UNNEEDED, struct ccanlint *c,
275 ccanlint_map_t *names)
277 if (!strmap_add(names, c->name, c))
278 err(1, "Duplicate name %s", c->name);
282 static void init_tests(void)
284 ccanlint_map_t names;
285 struct ccanlint **table;
290 table = autodata_get(ccanlint_tests, &num);
291 for (i = 0; i < num; i++)
292 register_test(table[i]);
293 autodata_free(table);
295 strmap_iterate(&tests, init_deps, NULL);
297 /* Check for duplicate names. */
299 strmap_iterate(&tests, check_names, &names);
300 strmap_clear(&names);
303 static bool reset_test(struct dgraph_node *node, void *unused UNNEEDED)
305 struct ccanlint *c = container_of(node, struct ccanlint, node);
311 static void reset_tests(struct dgraph_node *all)
313 dgraph_traverse_to(all, reset_test, NULL);
316 static bool print_deps(const char *member UNNEEDED,
317 struct ccanlint *c, void *unused UNNEEDED)
319 if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
320 struct dgraph_edge *e;
322 printf("These depend on %s:\n", c->key);
323 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
324 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
327 printf("\t%s\n", to->key);
333 static void print_test_depends(void)
337 strmap_iterate(&tests, print_deps, NULL);
341 static void show_tmpdir(const char *dir)
343 printf("You can find ccanlint working files in '%s'\n", dir);
346 static char *keep_tests(void *unused UNNEEDED)
350 /* Don't automatically destroy temporary dir. */
352 tal_add_destructor(temp_dir(), show_tmpdir);
356 static bool remove_test(struct dgraph_node *node, const char *why)
358 struct ccanlint *c = container_of(node, struct ccanlint, node);
360 dgraph_clear_node(node);
364 static char *exclude_test(const char *testname, void *unused UNNEEDED)
366 struct ccanlint *i = find_test(testname);
368 return tal_fmt(NULL, "No test %s to --exclude", testname);
370 /* Remove this, and everything which depends on it. */
371 dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
372 remove_test(&i->node, "excluded on command line");
376 static void skip_test_and_deps(struct ccanlint *c, const char *why)
378 /* Skip this, and everything which depends on us. */
379 dgraph_traverse_from(&c->node, skip_test, why);
380 skip_test(&c->node, why);
383 static char *list_tests(void *arg UNNEEDED)
388 /* This makes them print in topological order. */
389 while ((i = get_next_test()) != NULL) {
390 printf(" %-25s %s\n", i->key, i->name);
391 dgraph_clear_node(&i->node);
392 strmap_del(&tests, i->key, NULL);
397 static bool draw_test(const char *member UNNEEDED,
398 struct ccanlint *c, const char *style)
401 * todo: escape labels in case ccanlint test keys have
402 * characters interpreted as GraphViz syntax.
404 printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
408 static void test_dgraph_vertices(const char *style)
410 strmap_iterate(&tests, draw_test, style);
413 static bool draw_edges(const char *member UNNEEDED,
414 struct ccanlint *c, void *unused UNNEEDED)
416 struct dgraph_edge *e;
418 dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
419 struct ccanlint *to = container_of(e->n[DGRAPH_TO],
422 printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
427 static void test_dgraph_edges(void)
429 strmap_iterate(&tests, draw_edges, NULL);
432 static char *test_dependency_graph(void *arg UNNEEDED)
436 test_dgraph_vertices("");
444 static void add_options(struct ccanlint *test, char **options,
445 unsigned int num_options)
452 /* -1, because last one is NULL. */
453 num = tal_count(test->options) - 1;
455 tal_resize(&test->options, num + num_options + 1);
456 memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
459 void add_info_options(struct manifest *m)
462 char **info_options = get_ccanlint(m, m->dir, get_or_compile_info);
464 for (i = 0; info_options[i]; i++) {
465 char **words = tal_strsplit(m, info_options[i], " \t",
467 struct ccanlint *test;
472 test = find_test(words[0]);
474 warnx("%s: unknown ccanlint test '%s'",
475 m->info_file->fullname, words[0]);
480 warnx("%s: no argument to test '%s'",
481 m->info_file->fullname, words[0]);
486 if (strcasecmp(words[1], "FAIL") == 0) {
488 skip_test_and_deps(test,
492 if (!test->takes_options)
493 warnx("%s: %s doesn't take options",
494 m->info_file->fullname, words[0]);
495 add_options(test, words+1, tal_count(words)-1);
500 /* If options are of form "filename:<option>" they only apply to that file */
501 char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
504 unsigned int i, j = 0;
507 if (!test->options[0])
508 return test->options;
510 ret = tal_arr(f, char *, tal_count(test->options));
511 for (i = 0; test->options[i]; i++) {
514 if (!test->options[i] || !strchr(test->options[i], ':')) {
515 optname = test->options[i];
516 } else if (strstarts(test->options[i], f->name)
517 && test->options[i][strlen(f->name)] == ':') {
518 optname = test->options[i] + strlen(f->name) + 1;
522 /* FAIL overrides anything else. */
523 if (streq(optname, "FAIL")) {
524 ret = tal_arr(f, char *, 2);
525 ret[0] = (char *)"FAIL";
533 /* Shrink it to size so tal_array_length() works as expected. */
534 tal_resize(&ret, j + 1);
538 static char *opt_set_const_charp(const char *arg, const char **p)
540 return opt_set_charp(arg, cast_const2(char **, p));
543 static char *opt_set_target(const char *arg, struct dgraph_node *all)
545 struct ccanlint *t = find_test(arg);
547 return tal_fmt(NULL, "unknown --target %s", arg);
550 dgraph_add_edge(&t->node, all);
554 static bool run_tests(struct dgraph_node *all,
556 bool deps_fail_ignore,
561 const char *comment = "";
563 run.noninteractive = summary;
566 run.score = run.total = 0;
569 non_ccan_deps = build_failed = false;
571 dgraph_traverse_to(all, run_test, &run);
573 /* We can completely fail if we're missing external stuff: ignore */
574 if (deps_fail_ignore && non_ccan_deps && build_failed) {
575 comment = " (missing non-ccan dependencies?)";
577 } else if (!run.pass) {
580 printf("%sTotal score: %u/%u%s\n",
581 prefix, run.score, run.total, comment);
586 static bool add_to_all(const char *member UNNEEDED, struct ccanlint *c,
587 struct dgraph_node *all)
589 /* If we're excluded on cmdline, don't add. */
591 dgraph_add_edge(&c->node, all);
595 static bool test_module(struct dgraph_node *all,
596 const char *dir, const char *prefix, bool summary,
597 bool deps_fail_ignore)
599 struct manifest *m = get_manifest(autofree(), dir);
600 char *testlink = path_join(NULL, temp_dir(), "test");
602 /* Create a symlink from temp dir back to src dir's
605 if (symlink(path_join(m, dir, "test"), testlink) != 0)
606 err(1, "Creating test symlink in %s", temp_dir());
608 return run_tests(all, summary, deps_fail_ignore, m, prefix);
611 int main(int argc, char *argv[])
613 bool summary = false, pass = true, deps_fail_ignore = false;
615 const char *prefix = "";
616 char *cwd = path_cwd(NULL), *dir;
617 struct ccanlint top; /* cannot_run may try to set ->can_run */
618 const char *override_compiler = NULL, *override_cflags = NULL;
619 const char *override_gcov = NULL;
621 /* Empty graph node to which we attach everything else. */
622 dgraph_init_node(&top.node);
624 opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
625 "verbose mode (up to -vvvv)");
626 opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
627 "do not compile anything");
628 opt_register_noarg("-l|--list-tests", list_tests, NULL,
629 "list tests ccanlint performs (and exit)");
630 opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
631 "print dependency graph of tests in Graphviz .dot format");
632 opt_register_noarg("-k|--keep", keep_tests, NULL,
633 "do not delete ccanlint working files");
634 opt_register_noarg("--summary|-s", opt_set_bool, &summary,
635 "give results only, no interactive correction");
636 opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
637 "exclude <testname> (can be used multiple times)");
638 opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
640 "ignore (terminate) tests that are slower than this");
641 opt_register_arg("-t|--target <testname>", opt_set_target, NULL,
643 "only run one test (and its prerequisites)");
644 opt_register_arg("--compiler <compiler>", opt_set_const_charp,
645 NULL, &override_compiler, "set the compiler");
646 opt_register_arg("--cflags <flags>", opt_set_const_charp,
647 NULL, &override_cflags, "set the compiler flags");
648 opt_register_arg("--gcov <coverage tool>", opt_set_const_charp,
649 NULL, &override_gcov, "set the coverage tool");
650 opt_register_noarg("--deps-fail-ignore", opt_set_bool,
652 "don't fail if external dependencies are missing");
653 opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
654 "\nA program for checking and guiding development"
656 "This usage message");
658 /* Do verbose before anything else... */
659 opt_early_parse(argc, argv, opt_log_stderr_exit);
661 /* We move into temporary directory, so gcov dumps its files there. */
662 if (chdir(temp_dir()) != 0)
663 err(1, "Error changing to %s temporary dir", temp_dir());
668 compile_verbose = true;
669 print_test_depends();
672 tools_verbose = true;
674 opt_parse(&argc, argv, opt_log_stderr_exit);
677 strmap_iterate(&tests, add_to_all, &top.node);
682 dir = path_simplify(NULL, take(path_join(NULL, cwd, argv[1])));
684 ccan_dir = find_ccan_dir(dir);
686 errx(1, "Cannot find ccan/ base directory in %s", dir);
687 config_header = read_config_header(ccan_dir, verbose > 1);
689 /* We do this after read_config_header has set compiler & cflags */
691 cflags = override_cflags;
692 if (override_compiler)
693 compiler = override_compiler;
695 gcov = override_gcov;
698 pass = test_module(&top.node, cwd, "",
699 summary, deps_fail_ignore);
701 for (i = 1; i < argc; i++) {
702 dir = path_canon(NULL,
703 take(path_join(NULL, cwd, argv[i])));
705 err(1, "Cannot get canonical name of '%s'",
708 prefix = path_join(NULL, ccan_dir, "ccan");
709 prefix = path_rel(NULL, take(prefix), dir);
710 prefix = tal_strcat(NULL, take(prefix), ": ");
712 pass &= test_module(&top.node, dir, prefix, summary,
714 reset_tests(&top.node);