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