ccanlint: make fewer tests compulsory.
[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/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 "../compulsory_tests/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 = talloc_asprintf(m, "%s/%s", m->dir, i->name);
39                 char *output;
40
41                 i->compiled[ctype] = maybe_temp_file(m, "", false, fullfile);
42                 if (!compile_object(m, fullfile, ccan_dir, compiler, flags,
43                                     i->compiled[ctype], &output)) {
44                         talloc_free(i->compiled[ctype]);
45                         i->compiled[ctype] = NULL;
46                         return talloc_asprintf(m,
47                                                "Dependency %s"
48                                                " did not build:\n%s",
49                                                m->basename, output);
50                 }
51         }
52         return NULL;
53 }
54
55 char *build_submodule(struct manifest *m, const char *flags,
56                       enum compile_type ctype)
57 {
58         char *errstr;
59
60         if (m->compiled[ctype])
61                 return NULL;
62
63         if (!expect_obj_file(m))
64                 return NULL;
65
66         if (verbose >= 2)
67                 printf("  Building dependency %s\n", m->dir);
68
69         errstr = build_subdir_objs(m, flags, ctype);
70         if (errstr)
71                 return errstr;
72
73         m->compiled[ctype] = build_module(m, false, ctype, &errstr);
74         if (!m->compiled[ctype])
75                 return errstr;
76         return NULL;
77 }
78
79 static void check_depends_built(struct manifest *m,
80                                 bool keep,
81                                 unsigned int *timeleft, struct score *score)
82 {
83         struct manifest *i;
84
85         list_for_each(&m->deps, i, list) {
86                 char *errstr = build_submodule(i, cflags, COMPILE_NORMAL);
87
88                 if (errstr) {
89                         score->error = talloc_asprintf(score,
90                                                        "Dependency %s"
91                                                        " did not build:\n%s",
92                                                        i->basename, errstr);
93                         return;
94                 }
95         }
96
97         score->pass = true;
98         score->score = score->total;
99 }
100
101 struct ccanlint depends_build = {
102         .key = "depends_build",
103         .name = "Module's CCAN dependencies can be found or built",
104         .check = check_depends_built,
105         .can_run = can_build,
106         .needs = "depends_exist"
107 };
108
109 REGISTER_TEST(depends_build);