utf8: don't allow NUL in decoded strings.
[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 UNNEEDED)
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 UNNEEDED,
80                                 struct score *score)
81 {
82         struct list_head *list;
83
84         foreach_ptr(list, &m->deps, &m->test_deps) {
85                 struct manifest *i;
86                 list_for_each(list, i, list) {
87                         char *errstr;
88
89                         errstr = build_submodule(i, cflags, COMPILE_NORMAL);
90
91                         if (errstr) {
92                                 score->error = tal_fmt(score,
93                                                        "Dependency %s"
94                                                        " did not"
95                                                        " build:\n%s",
96                                                        i->modname,
97                                                        errstr);
98                                 return;
99                         }
100                 }
101         }
102
103         score->pass = true;
104         score->score = score->total;
105 }
106
107 struct ccanlint depends_build = {
108         .key = "depends_build",
109         .name = "Module's CCAN dependencies can be found or built",
110         .check = check_depends_built,
111         .can_run = can_build,
112         .needs = "depends_exist test_depends_exist info_ported"
113 };
114
115 REGISTER_TEST(depends_build);