]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_exist.c
tools: use tal instead of talloc.
[ccan] / tools / ccanlint / tests / depends_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 static bool have_dep(struct manifest *m, const char *dep)
17 {
18         struct manifest *i;
19
20         list_for_each(&m->deps, i, list)
21                 if (streq(i->modname, dep + strlen("ccan/")))
22                         return true;
23
24         return false;
25 }
26
27 static bool add_dep(struct manifest *m,
28                     struct list_head *deplist,
29                     const char *dep, struct score *score)
30 {
31         struct stat st;
32         struct manifest *subm;
33         char *dir = tal_fmt(m, "%s/%s", ccan_dir, dep);
34
35         /* FIXME: get_manifest has a tendency to exit. */
36         if (stat(dir, &st) != 0) {
37                 score->error = tal_fmt(m, "Could not stat dependency %s: %s",
38                                        dir, strerror(errno));
39                 return false;
40         }
41         subm = get_manifest(m, dir);
42         list_add_tail(deplist, &subm->list);
43         return true;
44 }
45
46 /* FIXME: check this is still true once we reduce features. */
47 static void check_depends_exist(struct manifest *m,
48                                 unsigned int *timeleft, struct score *score)
49 {
50         unsigned int i;
51         char **deps;
52
53         if (safe_mode)
54                 deps = get_safe_ccan_deps(m, m->dir, "depends", true);
55         else
56                 deps = get_deps(m, m->dir, "depends", true,
57                                 get_or_compile_info);
58
59         for (i = 0; deps[i]; i++) {
60                 if (!strstarts(deps[i], "ccan/"))
61                         continue;
62
63                 if (!add_dep(m, &m->deps, deps[i], score))
64                         return;
65         }
66
67         score->pass = true;
68         score->score = score->total;
69 }
70
71 static void check_test_depends_exist(struct manifest *m,
72                                      unsigned int *timeleft,
73                                      struct score *score)
74 {
75         unsigned int i;
76         char **deps;
77         bool needs_tap;
78
79         /* We may need libtap for testing, unless we're "tap" */
80         if (streq(m->modname, "tap")) {
81                 needs_tap = false;
82         } else if (list_empty(&m->run_tests) && list_empty(&m->api_tests)) {
83                 needs_tap = false;
84         } else {
85                 needs_tap = true;
86         }
87
88         if (safe_mode)
89                 deps = get_safe_ccan_deps(m, m->dir, "testdepends", true);
90         else
91                 deps = get_deps(m, m->dir, "testdepends", true,
92                                 get_or_compile_info);
93
94         for (i = 0; deps[i]; i++) {
95                 if (!strstarts(deps[i], "ccan/"))
96                         continue;
97
98                 /* Don't add dependency twice: we can only be on one list! */
99                 if (!have_dep(m, deps[i])
100                     && !add_dep(m, &m->test_deps, deps[i], score))
101                         return;
102
103                 if (streq(deps[i], "ccan/tap")) {
104                         needs_tap = false;
105                 }
106         }
107
108         if (needs_tap && !have_dep(m, "ccan/tap")
109             && !add_dep(m, &m->test_deps, "ccan/tap", score)) {
110                 return;
111         }
112
113         score->pass = true;
114         score->score = score->total;
115 }
116
117 struct ccanlint depends_exist = {
118         .key = "depends_exist",
119         .name = "Module's CCAN dependencies can be found",
120         .compulsory = true,
121         .check = check_depends_exist,
122         .needs = "info_exists"
123 };
124
125 REGISTER_TEST(depends_exist);
126
127 struct ccanlint test_depends_exist = {
128         .key = "test_depends_exist",
129         .name = "Module's CCAN test dependencies can be found",
130         .compulsory = false,
131         .check = check_test_depends_exist,
132         .needs = "depends_exist"
133 };
134
135 REGISTER_TEST(test_depends_exist);