b3332a9fc1eba3ccb98154d1c784695fd363f1d1
[ccan] / tools / ccanlint / compulsory_tests / check_depends_built.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 const char *can_build(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 /* FIXME: recursive ccanlint if they ask for it. */
25 static bool expect_obj_file(const char *dir)
26 {
27         struct manifest *dep_man;
28         bool has_c_files;
29
30         dep_man = get_manifest(dir, dir);
31
32         /* If it has C files, we expect an object file built from them. */
33         has_c_files = !list_empty(&dep_man->c_files);
34         talloc_free(dep_man);
35         return has_c_files;
36 }
37
38 static void *check_depends_built(struct manifest *m)
39 {
40         struct ccan_file *i;
41         struct stat st;
42         char *report = NULL;
43
44         list_for_each(&m->dep_dirs, i, list) {
45                 char *objfile;
46
47                 if (!expect_obj_file(i->name))
48                         continue;
49
50                 objfile = talloc_asprintf(m, "%s.o", i->name);
51                 if (stat(objfile, &st) != 0) {
52                         report = talloc_asprintf_append(report,
53                                                         "object file %s\n",
54                                                         objfile);
55                 } else {
56                         struct ccan_file *f = new_ccan_file(m, objfile);
57                         list_add_tail(&m->dep_objs, &f->list);
58                 }
59                         
60         }
61
62         /* We may need libtap for testing, unless we're "tap" */
63         if (!streq(m->basename, "tap")
64             && (!list_empty(&m->run_tests) || !list_empty(&m->api_tests))) {
65                 if (stat("../tap.o", &st) != 0) {
66                         report = talloc_asprintf_append(report,
67                                                         "object file ../tap.o"
68                                                         " (for tests)\n");
69                 }
70         }
71
72         return talloc_steal(m, report);
73 }
74
75 static const char *describe_depends_built(struct manifest *m,
76                                           void *check_result)
77 {
78         return talloc_asprintf(check_result, 
79                                "The following dependencies are not built:\n"
80                                "%s", (char *)check_result);
81 }
82
83 struct ccanlint depends_built = {
84         .name = "CCAN dependencies are built",
85         .total_score = 1,
86         .check = check_depends_built,
87         .describe = describe_depends_built,
88         .can_run = can_build,
89 };
90
91 REGISTER_TEST(depends_built, &depends_exist, NULL);