]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_build.c
69510a2969b69c6c6468d0085a89850b0428c3e2
[ccan] / tools / ccanlint / tests / depends_build.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/foreach/foreach.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                                const char *flags,
33                                enum compile_type ctype)
34 {
35         struct ccan_file *i;
36
37         list_for_each(&m->c_files, i, list) {
38                 char *fullfile = tal_fmt(m, "%s/%s", m->dir, i->name);
39                 char *output;
40
41                 i->compiled[ctype] = temp_file(m, "", fullfile);
42                 if (!compile_object(m, fullfile, ccan_dir, compiler, flags,
43                                     i->compiled[ctype], &output)) {
44                         tal_free(i->compiled[ctype]);
45                         i->compiled[ctype] = NULL;
46                         return tal_fmt(m,
47                                        "Dependency %s did not build:\n%s",
48                                        m->modname, output);
49                 }
50         }
51         return NULL;
52 }
53
54 char *build_submodule(struct manifest *m, const char *flags,
55                       enum compile_type ctype)
56 {
57         char *errstr;
58
59         if (m->compiled[ctype])
60                 return NULL;
61
62         if (!expect_obj_file(m))
63                 return NULL;
64
65         if (verbose >= 2)
66                 printf("  Building dependency %s\n", m->dir);
67
68         errstr = build_subdir_objs(m, flags, ctype);
69         if (errstr)
70                 return errstr;
71
72         m->compiled[ctype] = build_module(m, ctype, &errstr);
73         if (!m->compiled[ctype])
74                 return errstr;
75         return NULL;
76 }
77
78 static void check_depends_built(struct manifest *m,
79                                 unsigned int *timeleft, struct score *score)
80 {
81         struct list_head *list;
82
83         foreach_ptr(list, &m->deps, &m->test_deps) {
84                 struct manifest *i;
85                 list_for_each(list, i, list) {
86                         char *errstr;
87
88                         errstr = build_submodule(i, cflags, COMPILE_NORMAL);
89
90                         if (errstr) {
91                                 score->error = tal_fmt(score,
92                                                        "Dependency %s"
93                                                        " did not"
94                                                        " build:\n%s",
95                                                        i->modname,
96                                                        errstr);
97                                 return;
98                         }
99                 }
100         }
101
102         score->pass = true;
103         score->score = score->total;
104 }
105
106 struct ccanlint depends_build = {
107         .key = "depends_build",
108         .name = "Module's CCAN dependencies can be found or built",
109         .check = check_depends_built,
110         .can_run = can_build,
111         .needs = "depends_exist test_depends_exist"
112 };
113
114 REGISTER_TEST(depends_build);