ccanlint: add testdepends support.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 12 Nov 2012 06:34:24 +0000 (17:04 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 12 Nov 2012 06:34:24 +0000 (17:04 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
tools/ccanlint/tests/depends_accurate.c
tools/ccanlint/tests/depends_build.c
tools/ccanlint/tests/depends_build_without_features.c
tools/ccanlint/tests/depends_exist.c
tools/ccanlint/tests/tests_compile.c
tools/ccanlint/tests/tests_compile.h
tools/ccanlint/tests/tests_compile_coverage.c
tools/manifest.c
tools/manifest.h

index 986fb3bcc398df429e3b09d05cd081dc22e9d039..0ed1e654cd9324199e045a867b41b4d219593a0f 100644 (file)
@@ -16,7 +16,7 @@
 #include <string.h>
 #include <ctype.h>
 
-static bool has_dep(struct manifest *m, const char *depname)
+static bool has_dep(struct manifest *m, bool test_depend, const char *depname)
 {
        struct manifest *i;
 
@@ -28,45 +28,63 @@ static bool has_dep(struct manifest *m, const char *depname)
                if (streq(i->basename, depname))
                        return true;
        }
+
+       if (test_depend) {
+               list_for_each(&m->test_deps, i, list) {
+                       if (streq(i->basename, depname))
+                               return true;
+               }
+       }
+
        return false;
 }
 
+static void check_dep_includes(struct manifest *m, struct score *score,
+                              struct ccan_file *f, bool test_depend)
+{
+       unsigned int i;
+       char **lines = get_ccan_file_lines(f);
+       struct line_info *li = get_ccan_line_info(f);
+
+       for (i = 0; lines[i]; i++) {
+               char *mod;
+               if (!strreg(f, lines[i],
+                           "^[ \t]*#[ \t]*include[ \t]*[<\"]"
+                           "ccan/+([^/]+)/", &mod))
+                       continue;
+
+               if (has_dep(m, test_depend, mod))
+                       continue;
+
+               /* FIXME: we can't be sure about
+                * conditional includes, so don't
+                * complain. */
+               if (!li[i].cond) {
+                       score_file_error(score, f, i+1,
+                                        "%s not listed in _info", mod);
+               }
+       }
+}
+
 static void check_depends_accurate(struct manifest *m,
                                   unsigned int *timeleft, struct score *score)
 {
        struct list_head *list;
 
-       foreach_ptr(list, &m->c_files, &m->h_files,
-                   &m->run_tests, &m->api_tests,
+       foreach_ptr(list, &m->c_files, &m->h_files) {
+               struct ccan_file *f;
+
+               list_for_each(list, f, list)
+                       check_dep_includes(m, score, f, false);
+       }
+
+       foreach_ptr(list, &m->run_tests, &m->api_tests,
                    &m->compile_ok_tests, &m->compile_fail_tests,
                    &m->other_test_c_files) {
                struct ccan_file *f;
 
-               list_for_each(list, f, list) {
-                       unsigned int i;
-                       char **lines = get_ccan_file_lines(f);
-                       struct line_info *li = get_ccan_line_info(f);
-
-                       for (i = 0; lines[i]; i++) {
-                               char *mod;
-                               if (!strreg(f, lines[i],
-                                           "^[ \t]*#[ \t]*include[ \t]*[<\"]"
-                                           "ccan/+([^/]+)/", &mod))
-                                       continue;
-
-                               if (has_dep(m, mod))
-                                       continue;
-
-                               /* FIXME: we can't be sure about
-                                * conditional includes, so don't
-                                * complain. */
-                               if (!li[i].cond) {
-                                       score_file_error(score, f, i+1,
-                                                        "%s not listed in _info",
-                                                        mod);
-                               }
-                       }
-               }
+               list_for_each(list, f, list)
+                       check_dep_includes(m, score, f, true);
        }
 
        if (!score->error) {
@@ -79,7 +97,7 @@ struct ccanlint depends_accurate = {
        .key = "depends_accurate",
        .name = "Module's CCAN dependencies are the only CCAN files #included",
        .check = check_depends_accurate,
-       .needs = "depends_exist"
+       .needs = "depends_exist test_depends_exist"
 };
 
 REGISTER_TEST(depends_accurate);
index e6300a19502559fc3a69effa3e6b6218cae75c2b..b578d4b597b7f5b62d98dc5d6d5218b6be89a85b 100644 (file)
@@ -1,6 +1,7 @@
 #include <tools/ccanlint/ccanlint.h>
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
+#include <ccan/foreach/foreach.h>
 #include <ccan/str/str.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -79,17 +80,24 @@ char *build_submodule(struct manifest *m, const char *flags,
 static void check_depends_built(struct manifest *m,
                                unsigned int *timeleft, struct score *score)
 {
-       struct manifest *i;
-
-       list_for_each(&m->deps, i, list) {
-               char *errstr = build_submodule(i, cflags, COMPILE_NORMAL);
-
-               if (errstr) {
-                       score->error = talloc_asprintf(score,
-                                                      "Dependency %s"
-                                                      " did not build:\n%s",
-                                                      i->basename, errstr);
-                       return;
+       struct list_head *list;
+
+       foreach_ptr(list, &m->deps, &m->test_deps) {
+               struct manifest *i;
+               list_for_each(list, i, list) {
+                       char *errstr;
+
+                       errstr = build_submodule(i, cflags, COMPILE_NORMAL);
+
+                       if (errstr) {
+                               score->error = talloc_asprintf(score,
+                                                              "Dependency %s"
+                                                              " did not"
+                                                              " build:\n%s",
+                                                              i->basename,
+                                                              errstr);
+                               return;
+                       }
                }
        }
 
@@ -102,7 +110,7 @@ struct ccanlint depends_build = {
        .name = "Module's CCAN dependencies can be found or built",
        .check = check_depends_built,
        .can_run = can_build,
-       .needs = "depends_exist"
+       .needs = "depends_exist test_depends_exist"
 };
 
 REGISTER_TEST(depends_build);
index 533f4f318ce869cfb48f83882aa713bea5457dfa..faa3b21d115b28fc31066d7456f9b0f5e3eefbee 100644 (file)
@@ -2,6 +2,7 @@
 #include <tools/tools.h>
 #include <ccan/talloc/talloc.h>
 #include <ccan/str/str.h>
+#include <ccan/foreach/foreach.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -27,21 +28,27 @@ static void check_depends_built_without_features(struct manifest *m,
                                                 unsigned int *timeleft,
                                                 struct score *score)
 {
+       struct list_head *list;
        struct manifest *i;
        char *flags;
 
        flags = talloc_asprintf(score, "%s %s", cflags,
                                REDUCE_FEATURES_FLAGS);
 
-       list_for_each(&m->deps, i, list) {
-               char *errstr = build_submodule(i, flags, COMPILE_NOFEAT);
+       foreach_ptr(list, &m->deps, &m->test_deps) {
+               list_for_each(list, i, list) {
+                       char *errstr = build_submodule(i, flags,
+                                                      COMPILE_NOFEAT);
 
-               if (errstr) {
-                       score->error = talloc_asprintf(score,
-                                                      "Dependency %s"
-                                                      " did not build:\n%s",
-                                                      i->basename, errstr);
-                       return;
+                       if (errstr) {
+                               score->error = talloc_asprintf(score,
+                                                              "Dependency %s"
+                                                              " did not"
+                                                              " build:\n%s",
+                                                              i->basename,
+                                                              errstr);
+                               return;
+                       }
                }
        }
 
index d869cbb7f38cb53a539359e21f8d5f212cfaef07..76750fb1236f7bab79e7ff2a73412aa74185d919 100644 (file)
 #include <string.h>
 #include <ctype.h>
 
-static bool add_dep(struct manifest *m, const char *dep, struct score *score)
+static bool have_dep(struct manifest *m, const char *dep)
+{
+       struct manifest *i;
+
+       list_for_each(&m->deps, i, list)
+               if (streq(i->basename, dep + strlen("ccan/")))
+                       return true;
+
+       return false;
+}
+
+static bool add_dep(struct manifest *m,
+                   struct list_head *deplist,
+                   const char *dep, struct score *score)
 {
        struct stat st;
        struct manifest *subm;
@@ -29,7 +42,7 @@ static bool add_dep(struct manifest *m, const char *dep, struct score *score)
                return false;
        }
        subm = get_manifest(m, dir);
-       list_add_tail(&m->deps, &subm->list);
+       list_add_tail(deplist, &subm->list);
        return true;
 }
 
@@ -39,11 +52,32 @@ static void check_depends_exist(struct manifest *m,
 {
        unsigned int i;
        char **deps;
-       char *updir = talloc_strdup(m, m->dir);
-       bool needs_tap;
 
-       if (strrchr(updir, '/'))
-               *strrchr(updir, '/') = '\0';
+       if (safe_mode)
+               deps = get_safe_ccan_deps(m, m->dir, "depends", true);
+       else
+               deps = get_deps(m, m->dir, "depends", true,
+                               get_or_compile_info);
+
+       for (i = 0; deps[i]; i++) {
+               if (!strstarts(deps[i], "ccan/"))
+                       continue;
+
+               if (!add_dep(m, &m->deps, deps[i], score))
+                       return;
+       }
+
+       score->pass = true;
+       score->score = score->total;
+}
+
+static void check_test_depends_exist(struct manifest *m,
+                                    unsigned int *timeleft,
+                                    struct score *score)
+{
+       unsigned int i;
+       char **deps;
+       bool needs_tap;
 
        /* We may need libtap for testing, unless we're "tap" */
        if (streq(m->basename, "tap")) {
@@ -55,16 +89,18 @@ static void check_depends_exist(struct manifest *m,
        }
 
        if (safe_mode)
-               deps = get_safe_ccan_deps(m, m->dir, "depends", true);
+               deps = get_safe_ccan_deps(m, m->dir, "testdepends", true);
        else
-               deps = get_deps(m, m->dir, "depends", true,
+               deps = get_deps(m, m->dir, "testdepends", true,
                                get_or_compile_info);
 
        for (i = 0; deps[i]; i++) {
                if (!strstarts(deps[i], "ccan/"))
                        continue;
 
-               if (!add_dep(m, deps[i], score))
+               /* Don't add dependency twice: we can only be on one list! */
+               if (!have_dep(m, deps[i])
+                   && !add_dep(m, &m->test_deps, deps[i], score))
                        return;
 
                if (streq(deps[i], "ccan/tap")) {
@@ -72,7 +108,8 @@ static void check_depends_exist(struct manifest *m,
                }
        }
 
-       if (needs_tap && !add_dep(m, "ccan/tap", score)) {
+       if (needs_tap && !have_dep(m, "ccan/tap")
+           && !add_dep(m, &m->test_deps, "ccan/tap", score)) {
                return;
        }
 
@@ -89,3 +126,13 @@ struct ccanlint depends_exist = {
 };
 
 REGISTER_TEST(depends_exist);
+
+struct ccanlint test_depends_exist = {
+       .key = "test_depends_exist",
+       .name = "Module's CCAN test dependencies can be found",
+       .compulsory = false,
+       .check = check_test_depends_exist,
+       .needs = "depends_exist"
+};
+
+REGISTER_TEST(test_depends_exist);
index 3114f2f1bc6de31ea7b7afdf4a382a36cfdde72e..73b1cb24aed025c570e766961e965ab6ebf1c92a 100644 (file)
@@ -42,23 +42,30 @@ char *test_obj_list(const struct manifest *m, bool link_with_module,
                        list = talloc_asprintf_append(list, " %s",
                                                      i->compiled[own_ctype]);
 
-       /* Other ccan modules. */
+       /* Other ccan modules (normal depends). */
        list_for_each(&m->deps, subm, list) {
                if (subm->compiled[ctype])
                        list = talloc_asprintf_append(list, " %s",
                                                      subm->compiled[ctype]);
        }
 
+       /* Other ccan modules (test depends). */
+       list_for_each(&m->test_deps, subm, list) {
+               if (subm->compiled[ctype])
+                       list = talloc_asprintf_append(list, " %s",
+                                                     subm->compiled[ctype]);
+       }
+
        return list;
 }
 
-char *lib_list(const struct manifest *m, enum compile_type ctype)
+char *test_lib_list(const struct manifest *m, enum compile_type ctype)
 {
        unsigned int i;
        char **libs;
        char *ret = talloc_strdup(m, "");
 
-       libs = get_libs(m, m->dir, "depends", get_or_compile_info);
+       libs = get_libs(m, m->dir, "testdepends", get_or_compile_info);
        for (i = 0; libs[i]; i++)
                ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
        return ret;
@@ -84,7 +91,7 @@ static bool compile(const void *ctx,
        if (!compile_and_link(ctx, file->fullname, ccan_dir,
                              test_obj_list(m, link_with_module,
                                            ctype, ctype),
-                             compiler, flags, lib_list(m, ctype), fname,
+                             compiler, flags, test_lib_list(m, ctype), fname,
                              output)) {
                talloc_free(fname);
                return false;
@@ -111,7 +118,7 @@ static void compile_async(const void *ctx,
 
        compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
                               test_obj_list(m, link_with_module, ctype, ctype),
-                              compiler, flags, lib_list(m, ctype),
+                              compiler, flags, test_lib_list(m, ctype),
                               file->compiled[ctype]);
 }
 
index 590eddedebf15736dec6ac18ee9853f67ab760b6..9c010e3161bcf22f495ad2b95bcbf0cf807bd233 100644 (file)
@@ -3,4 +3,4 @@
 char *test_obj_list(const struct manifest *m, bool link_with_module,
                    enum compile_type ctype, enum compile_type own_ctype);
 /* Library list as specified by ctype variant of _info. */
-char *lib_list(const struct manifest *m, enum compile_type ctype);
+char *test_lib_list(const struct manifest *m, enum compile_type ctype);
index 19ba1ef2853fe277ab31709add902ac447d513d3..9e13ad938a403f2d25f15c78d91e05e931e4bc98 100644 (file)
@@ -46,7 +46,7 @@ static void cov_compile(const void *ctx,
                                             COMPILE_NORMAL,
                                             COMPILE_COVERAGE),
                               compiler, flags,
-                              lib_list(m, COMPILE_NORMAL),
+                              test_lib_list(m, COMPILE_NORMAL),
                               file->compiled[COMPILE_COVERAGE]);
 }
 
index 024eaa42a0d42c469e08f2ccba78846f58e0f1d5..e368b4ed1e2a8b0470218cc30a42aa7f5da3547d 100644 (file)
@@ -237,6 +237,7 @@ struct manifest *get_manifest(const void *ctx, const char *dir)
        list_head_init(&m->examples);
        list_head_init(&m->mangled_examples);
        list_head_init(&m->deps);
+       list_head_init(&m->test_deps);
 
        len = strlen(m->dir);
        while (len && m->dir[len-1] == '/')
index ee5ae9dfcd6aaa1651753408147a28173284152a..04f88d9e084ee7f5e14397e1ba5e8596b7bd822e 100644 (file)
@@ -38,6 +38,7 @@ struct manifest {
 
        /* From tests/check_depends_exist.c */
        struct list_head deps;
+       struct list_head test_deps;
 
        /* From tests/license_exists.c */
        enum license license;