]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/check_depends_exist.c
0978340f8c8a117680e04eaff74c5a72f221614b
[ccan] / tools / ccanlint / tests / check_depends_exist.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.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 char *add_dep(char *sofar, struct manifest *m, const char *dep)
18 {
19         char *dir;
20         struct stat st;
21         struct ccan_file *f;
22
23         dir = talloc_asprintf(m, "../%s", dep);
24         if (stat(dir, &st) != 0) {
25                 return talloc_asprintf_append(sofar,
26                                               "ccan/%s: expected it in"
27                                               " directory %s\n",
28                                               dep, dir);
29         }
30
31         f = new_ccan_file(m, dir);
32         list_add_tail(&m->dep_dirs, &f->list);
33         return sofar;
34 }
35
36 static void *check_depends_exist(struct manifest *m)
37 {
38         unsigned int i;
39         char *report = NULL;
40         char **deps;
41
42         if (safe_mode)
43                 deps = get_safe_ccan_deps(m, "..", m->basename, true);
44         else
45                 deps = get_deps(m, "..", m->basename, true);
46
47         for (i = 0; deps[i]; i++) {
48                 if (!strstarts(deps[i], "ccan/"))
49                         continue;
50
51                 report = add_dep(report, m, deps[i] + strlen("ccan/"));
52         }
53         return report;
54 }
55
56 static const char *describe_depends_exist(struct manifest *m,
57                                           void *check_result)
58 {
59         return talloc_asprintf(check_result,
60                                "The following dependencies are are expected:\n"
61                                "%s", (char *)check_result);
62 }
63
64 struct ccanlint depends_exist = {
65         .name = "CCAN dependencies are present",
66         .total_score = 1,
67         .check = check_depends_exist,
68         .describe = describe_depends_exist,
69 };
70
71 REGISTER_TEST(depends_exist, NULL);