+static bool should_run_ccanlint(const char *name, struct ccanlint *c,
+ void *unused)
+{
+ c->should_run = true;
+ return true;
+}
+
+static bool should_run_node(struct dgraph_node *to, void *unused)
+{
+ should_run_ccanlint(NULL, container_of(to, struct ccanlint, node),
+ unused);
+ return true;
+}
+
+static char *add_target(const char *arg, bool *targeting)
+{
+ struct ccanlint *c = find_test(arg);
+ if (!c)
+ return talloc_asprintf(NULL, "Unknown test %s", arg);
+
+ dgraph_traverse_to(&c->node, should_run_node, NULL);
+ *targeting = true;
+ return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+ bool summary = false, pass = true, targeting = false;
+ unsigned int i;
+ struct manifest *m;
+ struct ccanlint *t;
+ const char *prefix = "";
+ char *dir = talloc_getcwd(NULL), *base_dir = dir, *testlink;
+
+ cmdline_exclude = btree_new(btree_strcmp);
+ info_exclude = btree_new(btree_strcmp);
+
+ opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
+ "verbose mode (up to -vvvv)");
+ opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
+ "do not compile anything");
+ opt_register_noarg("-l|--list-tests", list_tests, NULL,
+ "list tests ccanlint performs (and exit)");
+ opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
+ "print dependency graph of tests in Graphviz .dot format");
+ opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
+ "keep results of <testname>"
+ " (can be used multiple times, or 'all')");
+ opt_register_noarg("--summary|-s", opt_set_bool, &summary,
+ "simply give one line summary");
+ opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
+ "exclude <testname> (can be used multiple times)");
+ opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
+ NULL, &timeout,
+ "ignore (terminate) tests that are slower than this");
+ opt_register_arg("--target <testname>", add_target, NULL, &targeting,
+ "run this test and prerequisites (can specify multiple times)");
+ opt_register_arg("--compiler <compiler>", opt_set_const_charp,
+ NULL, &compiler, "set the compiler");
+ opt_register_arg("--cflags <flags>", opt_set_const_charp,
+ NULL, &cflags, "set the compiler flags");
+ opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
+ "\nA program for checking and guiding development"
+ " of CCAN modules.",
+ "This usage message");
+
+ /* Do verbose before anything else... */
+ opt_early_parse(argc, argv, opt_log_stderr_exit);
+
+ /* We move into temporary directory, so gcov dumps its files there. */
+ if (chdir(temp_dir(talloc_autofree_context())) != 0)
+ err(1, "Error changing to %s temporary dir", temp_dir(NULL));
+
+ init_tests();
+
+ if (verbose >= 3) {
+ compile_verbose = true;
+ print_test_depends();
+ }
+ if (verbose >= 4)
+ tools_verbose = true;
+
+ opt_parse(&argc, argv, opt_log_stderr_exit);
+
+ /* This links back to the module's test dir. */
+ testlink = talloc_asprintf(NULL, "%s/test", temp_dir(NULL));
+
+ /* If no --target, run all tests. */
+ if (!targeting)
+ strmap_iterate(&tests, should_run_ccanlint, NULL);
+
+ /* Defaults to pwd. */
+ if (argc == 1) {
+ i = 1;
+ goto got_dir;
+ }
+
+ for (i = 1; i < argc; i++) {
+ unsigned int score, total_score;
+
+ dir = argv[i];
+
+ if (dir[0] != '/')
+ dir = talloc_asprintf_append(NULL, "%s/%s",
+ base_dir, dir);
+ while (strends(dir, "/"))
+ dir[strlen(dir)-1] = '\0';
+
+ got_dir:
+ if (dir != base_dir)
+ prefix = talloc_append_string(talloc_basename(NULL,dir),
+ ": ");
+
+ init_tests();
+
+ m = get_manifest(talloc_autofree_context(), dir);
+
+ /* FIXME: This has to come after we've got manifest. */
+ if (i == 1)
+ read_config_header();
+
+ /* Create a symlink from temp dir back to src dir's
+ * test directory. */
+ unlink(testlink);
+ if (symlink(talloc_asprintf(m, "%s/test", dir), testlink) != 0)
+ err(1, "Creating test symlink in %s", temp_dir(NULL));
+
+ score = total_score = 0;
+ while ((t = get_next_test()) != NULL) {
+ if (!run_test(t, summary, &score, &total_score, m,
+ prefix)) {
+ pass = false;
+ if (t->compulsory) {
+ warnx("%s%s failed", prefix, t->name);
+ printf("%sTotal score: 0/%u\n",
+ prefix, total_score);
+ goto next;
+ }
+ }
+ }
+
+ printf("%sTotal score: %u/%u\n", prefix, score, total_score);
+ next: ;
+ }
+ return pass ? 0 : 1;