]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/depends_build.c
ccanlint: make depends_accurate test more accurate.
[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 "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] = temp_file(m, "", 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, 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                                 unsigned int *timeleft, struct score *score)
81 {
82         struct manifest *i;
83
84         list_for_each(&m->deps, i, list) {
85                 char *errstr = build_submodule(i, cflags, COMPILE_NORMAL);
86
87                 if (errstr) {
88                         score->error = talloc_asprintf(score,
89                                                        "Dependency %s"
90                                                        " did not build:\n%s",
91                                                        i->basename, errstr);
92                         return;
93                 }
94         }
95
96         score->pass = true;
97         score->score = score->total;
98 }
99
100 struct ccanlint depends_build = {
101         .key = "depends_build",
102         .name = "Module's CCAN dependencies can be found or built",
103         .check = check_depends_built,
104         .can_run = can_build,
105         .needs = "depends_exist"
106 };
107
108 REGISTER_TEST(depends_build);