3bd82f830971ff4ec7eefcde5b4bb342d31b1390
[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 #include "build.h"
17
18 static const char *can_build(struct manifest *m)
19 {
20         if (safe_mode)
21                 return "Safe mode enabled";
22         return NULL;
23 }
24
25 static bool expect_obj_file(struct manifest *m)
26 {
27         /* If it has C files, we expect an object file built from them. */
28         return !list_empty(&m->c_files);
29 }
30
31 static char *build_subdir_objs(struct manifest *m)
32 {
33         struct ccan_file *i;
34
35         list_for_each(&m->c_files, i, list) {
36                 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
37                 char *output;
38
39                 i->compiled = maybe_temp_file(m, "", false, fullfile);
40                 if (!compile_object(m, fullfile, ccan_dir, "", i->compiled,
41                                     &output)) {
42                         talloc_free(i->compiled);
43                         i->compiled = NULL;
44                         return talloc_asprintf(m,
45                                                "Dependency %s"
46                                                " did not build:\n%s",
47                                                m->basename, output);
48                 }
49         }
50         return NULL;
51 }
52
53 static void check_depends_built(struct manifest *m,
54                                 bool keep,
55                                 unsigned int *timeleft, struct score *score)
56 {
57         struct manifest *i;
58         struct stat st;
59
60         list_for_each(&m->deps, i, list) {
61                 char *errstr;
62                 if (!expect_obj_file(i))
63                         continue;
64
65                 i->compiled = talloc_asprintf(i, "%s.o", i->dir);
66                 if (stat(i->compiled, &st) == 0)
67                         continue;
68
69                 if (verbose >= 2)
70                         printf("  Building dependency %s\n", i->dir);
71                 score->error = build_subdir_objs(i);
72                 if (score->error)
73                         return;
74                 i->compiled = build_module(i, keep, &errstr);
75                 if (!i->compiled) {
76                         score->error = talloc_asprintf(score,
77                                                        "Dependency %s"
78                                                        " did not build:\n%s",
79                                                        i->basename, errstr);
80                         return;
81                 }                       
82         }
83
84         if (!score->error) {
85                 score->pass = true;
86                 score->score = score->total;
87         }
88 }
89
90 struct ccanlint depends_built = {
91         .key = "depends-built",
92         .name = "Module's CCAN dependencies can be found or built",
93         .check = check_depends_built,
94         .can_run = can_build,
95 };
96
97 REGISTER_TEST(depends_built, &depends_exist, NULL);