tools: don't assume modules are immediately below ccan/ dir.
[ccan] / tools / ccanlint / tests / depends_build.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/foreach/foreach.h>
5 #include <ccan/str/str.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "build.h"
18
19 static const char *can_build(struct manifest *m)
20 {
21         if (safe_mode)
22                 return "Safe mode enabled";
23         return NULL;
24 }
25
26 static bool expect_obj_file(struct manifest *m)
27 {
28         /* If it has C files, we expect an object file built from them. */
29         return !list_empty(&m->c_files);
30 }
31
32 static char *build_subdir_objs(struct manifest *m,
33                                const char *flags,
34                                enum compile_type ctype)
35 {
36         struct ccan_file *i;
37
38         list_for_each(&m->c_files, i, list) {
39                 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
40                 char *output;
41
42                 i->compiled[ctype] = temp_file(m, "", fullfile);
43                 if (!compile_object(m, fullfile, ccan_dir, compiler, flags,
44                                     i->compiled[ctype], &output)) {
45                         talloc_free(i->compiled[ctype]);
46                         i->compiled[ctype] = NULL;
47                         return talloc_asprintf(m,
48                                                "Dependency %s"
49                                                " did not build:\n%s",
50                                                m->modname, output);
51                 }
52         }
53         return NULL;
54 }
55
56 char *build_submodule(struct manifest *m, const char *flags,
57                       enum compile_type ctype)
58 {
59         char *errstr;
60
61         if (m->compiled[ctype])
62                 return NULL;
63
64         if (!expect_obj_file(m))
65                 return NULL;
66
67         if (verbose >= 2)
68                 printf("  Building dependency %s\n", m->dir);
69
70         errstr = build_subdir_objs(m, flags, ctype);
71         if (errstr)
72                 return errstr;
73
74         m->compiled[ctype] = build_module(m, ctype, &errstr);
75         if (!m->compiled[ctype])
76                 return errstr;
77         return NULL;
78 }
79
80 static void check_depends_built(struct manifest *m,
81                                 unsigned int *timeleft, struct score *score)
82 {
83         struct list_head *list;
84
85         foreach_ptr(list, &m->deps, &m->test_deps) {
86                 struct manifest *i;
87                 list_for_each(list, i, list) {
88                         char *errstr;
89
90                         errstr = build_submodule(i, cflags, COMPILE_NORMAL);
91
92                         if (errstr) {
93                                 score->error = talloc_asprintf(score,
94                                                                "Dependency %s"
95                                                                " did not"
96                                                                " build:\n%s",
97                                                                i->modname,
98                                                                errstr);
99                                 return;
100                         }
101                 }
102         }
103
104         score->pass = true;
105         score->score = score->total;
106 }
107
108 struct ccanlint depends_build = {
109         .key = "depends_build",
110         .name = "Module's CCAN dependencies can be found or built",
111         .check = check_depends_built,
112         .can_run = can_build,
113         .needs = "depends_exist test_depends_exist"
114 };
115
116 REGISTER_TEST(depends_build);