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