ccanlint: rename files to match keys
[ccan] / tools / ccanlint / compulsory_tests / depends_build.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 char *build_submodule(struct manifest *m)
54 {
55         char *errstr;
56         struct stat st;
57
58         if (m->compiled)
59                 return NULL;
60
61         if (!expect_obj_file(m))
62                 return NULL;
63
64         m->compiled = talloc_asprintf(m, "%s.o", m->dir);
65         if (stat(m->compiled, &st) == 0)
66                 return NULL;
67
68         if (verbose >= 2)
69                 printf("  Building dependency %s\n", m->dir);
70
71         errstr = build_subdir_objs(m);
72         if (errstr)
73                 return errstr;
74
75         m->compiled = build_module(m, false, &errstr);
76         if (!m->compiled)
77                 return errstr;
78         return NULL;
79 }
80
81 static void check_depends_built(struct manifest *m,
82                                 bool keep,
83                                 unsigned int *timeleft, struct score *score)
84 {
85         struct manifest *i;
86
87         list_for_each(&m->deps, i, list) {
88                 char *errstr = build_submodule(i);
89
90                 if (errstr) {
91                         score->error = talloc_asprintf(score,
92                                                        "Dependency %s"
93                                                        " did not build:\n%s",
94                                                        i->basename, errstr);
95                         return;
96                 }
97         }
98
99         score->pass = true;
100         score->score = score->total;
101 }
102
103 struct ccanlint depends_build = {
104         .key = "depends_build",
105         .name = "Module's CCAN dependencies can be found or built",
106         .check = check_depends_built,
107         .can_run = can_build,
108         .needs = "depends_exist"
109 };
110
111 REGISTER_TEST(depends_build);