+static void register_test(struct list_head *h, struct ccanlint *test)
+{
+ list_add(h, &test->list);
+}
+
+/**
+ * get_next_test - retrieves the next test to be processed
+ **/
+static inline struct ccanlint *get_next_test(struct list_head *test)
+{
+ struct ccanlint *i;
+
+ if (list_empty(test))
+ return NULL;
+
+ list_for_each(test, i, list) {
+ if (i->num_depends == 0)
+ return i;
+ }
+ errx(1, "Can't make process; test dependency cycle");
+}
+
+static struct ccanlint *find_test(const char *key)
+{
+ struct ccanlint *i;
+
+ list_for_each(&compulsory_tests, i, list)
+ if (streq(i->key, key))
+ return i;
+
+ list_for_each(&normal_tests, i, list)
+ if (streq(i->key, key))
+ return i;
+
+ return NULL;
+}
+
+#undef REGISTER_TEST
+#define REGISTER_TEST(name, ...) extern struct ccanlint name
+#include "generated-normal-tests"
+#include "generated-compulsory-tests"
+
+static void init_tests(void)
+{
+ struct ccanlint *c;
+ struct btree *keys, *names;
+ struct list_head *list;
+
+#undef REGISTER_TEST
+#define REGISTER_TEST(name) register_test(&normal_tests, &name)
+#include "generated-normal-tests"
+#undef REGISTER_TEST
+#define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
+#include "generated-compulsory-tests"
+
+ /* Initialize dependency lists. */
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ list_head_init(&c->dependencies);
+ }
+ }
+
+ /* Resolve dependencies. */
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ char **deps = strsplit(NULL, c->needs, " ", NULL);
+ unsigned int i;
+
+ for (i = 0; deps[i]; i++) {
+ struct ccanlint *dep;
+ struct dependent *dchild;
+
+ dep = find_test(deps[i]);
+ if (!dep)
+ errx(1, "BUG: unknown dep '%s' for %s",
+ deps[i], c->key);
+ dchild = talloc(NULL, struct dependent);
+ dchild->dependent = c;
+ list_add_tail(&dep->dependencies,
+ &dchild->node);
+ c->num_depends++;
+ }
+ talloc_free(deps);
+ }
+ }
+
+ /* Self-consistency check: make sure no two tests
+ have the same key or name. */
+ keys = btree_new(btree_strcmp);
+ names = btree_new(btree_strcmp);
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, c, list) {
+ if (!btree_insert(keys, c->key))
+ errx(1, "BUG: Duplicate test key '%s'",
+ c->key);
+ if (!btree_insert(names, c->name))
+ errx(1, "BUG: Duplicate test name '%s'",
+ c->name);
+ }
+ }
+ btree_delete(keys);
+ btree_delete(names);
+
+ if (!verbose)
+ return;
+
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ printf("\%s Tests\n",
+ list == &compulsory_tests ? "Compulsory" : "Normal");
+
+ if (!list_empty(&c->dependencies)) {
+ const struct dependent *d;
+ printf("These depend on us:\n");
+ list_for_each(&c->dependencies, d, node)
+ printf("\t%s\n", d->dependent->name);
+ }
+ }
+}
+
+static int show_tmpdir(char *dir)
+{
+ printf("You can find ccanlint working files in '%s'\n", dir);
+ return 0;
+}
+
+static char *keep_test(const char *testname, void *unused)
+{
+ struct ccanlint *i;
+
+ if (streq(testname, "all")) {
+ struct list_head *list;
+ foreach_ptr(list, &compulsory_tests, &normal_tests) {
+ list_for_each(list, i, list)
+ i->keep_results = true;
+ }
+ } else {
+ i = find_test(testname);
+ if (!i)
+ errx(1, "No test %s to --keep", testname);
+ i->keep_results = true;
+ }
+
+ /* Don't automatically destroy temporary dir. */
+ talloc_set_destructor(temp_dir(NULL), show_tmpdir);
+ return NULL;
+}
+
+static char *skip_test(const char *testname, void *unused)
+{
+ btree_insert(cmdline_exclude, testname);
+ return NULL;
+}
+
+static void print_tests(struct list_head *tests, const char *type)
+{
+ struct ccanlint *i;
+
+ printf("%s tests:\n", type);
+ /* This makes them print in topological order. */
+ while ((i = get_next_test(tests)) != NULL) {
+ const struct dependent *d;
+ printf(" %-25s %s\n", i->key, i->name);
+ list_del(&i->list);
+ list_for_each(&i->dependencies, d, node)
+ d->dependent->num_depends--;
+ }
+}
+
+static char *list_tests(void *arg)
+{
+ print_tests(&compulsory_tests, "Compulsory");
+ print_tests(&normal_tests, "Normal");
+ exit(0);
+}
+
+static void test_dgraph_vertices(struct list_head *tests, const char *style)