]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_compile.c
8a833053e9d3c9e698aa573a228fb41a20bdf507
[ccan] / tools / ccanlint / tests / tests_compile.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 <ccan/foreach/foreach.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 "reduce_features.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 char *obj_list(const struct manifest *m, bool link_with_module,
27                       enum compile_type ctype)
28 {
29         char *list = talloc_strdup(m, "");
30         struct ccan_file *i;
31         struct manifest *subm;
32
33         /* Objects from any other C files. */
34         list_for_each(&m->other_test_c_files, i, list)
35                 list = talloc_asprintf_append(list, " %s",
36                                               i->compiled[ctype]);
37
38         /* Our own object files. */
39         if (link_with_module)
40                 list_for_each(&m->c_files, i, list)
41                         list = talloc_asprintf_append(list, " %s",
42                                                       i->compiled[ctype]);
43
44         /* Other ccan modules. */
45         list_for_each(&m->deps, subm, list) {
46                 if (subm->compiled[ctype])
47                         list = talloc_asprintf_append(list, " %s",
48                                                       subm->compiled[ctype]);
49         }
50
51         return list;
52 }
53
54 static char *lib_list(const struct manifest *m, enum compile_type ctype)
55 {
56         unsigned int i, num;
57         char **libs = get_libs(m, m->dir, &num,
58                                &m->info_file->compiled[ctype]);
59         char *ret = talloc_strdup(m, "");
60
61         for (i = 0; i < num; i++)
62                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
63         return ret;
64 }
65
66 static bool compile(const void *ctx,
67                     struct manifest *m,
68                     struct ccan_file *file,
69                     bool fail,
70                     bool link_with_module,
71                     bool keep,
72                     enum compile_type ctype,
73                     char **output)
74 {
75         char *fname, *flags;
76
77         flags = talloc_asprintf(ctx, "%s%s%s",
78                                 fail ? "-DFAIL " : "",
79                                 cflags,
80                                 ctype == COMPILE_NOFEAT ? " -I." : "");
81
82         fname = maybe_temp_file(ctx, "", keep, file->fullname);
83         if (!compile_and_link(ctx, file->fullname, ccan_dir,
84                               obj_list(m, link_with_module, ctype), compiler,
85                               flags, lib_list(m, ctype), fname, output)) {
86                 talloc_free(fname);
87                 return false;
88         }
89
90         file->compiled[ctype] = fname;
91         return true;
92 }
93
94 static void compile_tests(struct manifest *m, bool keep,
95                           struct score *score,
96                           enum compile_type ctype)
97 {
98         char *cmdout;
99         struct ccan_file *i;
100         struct list_head *list;
101         bool errors = false, warnings = false;
102
103         foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
104                 list_for_each(list, i, list) {
105                         if (!compile(score, m, i, false,
106                                      list == &m->api_tests, keep,
107                                      ctype, &cmdout)) {
108                                 score_file_error(score, i, 0,
109                                                  "Compile failed:\n%s",
110                                                  cmdout);
111                                 errors = true;
112                         } else if (!streq(cmdout, "")) {
113                                 score_file_error(score, i, 0,
114                                                  "Compile gave warnings:\n%s",
115                                                  cmdout);
116                                 warnings = true;
117                         }
118                 }
119         }
120
121         /* The compile fail tests are a bit weird, handle them separately */
122         if (errors)
123                 return;
124
125         /* For historical reasons, "fail" often means "gives warnings" */
126         list_for_each(&m->compile_fail_tests, i, list) {
127                 if (!compile(score, m, i, false, false, false,
128                              ctype, &cmdout)) {
129                         score_file_error(score, i, 0,
130                                          "Compile without -DFAIL failed:\n%s",
131                                          cmdout);
132                         return;
133                 }
134                 if (!streq(cmdout, "")) {
135                         score_file_error(score, i, 0,
136                                          "Compile gave warnings"
137                                          " without -DFAIL:\n%s",
138                                          cmdout);
139                         return;
140                 }
141                 if (compile(score, m, i, true, false, false,
142                             ctype, &cmdout)
143                     && streq(cmdout, "")) {
144                         score_file_error(score, i, 0,
145                                          "Compiled successfully with -DFAIL?");
146                         return;
147                 }
148                 score->total++;
149         }
150
151         score->pass = true;
152         score->score = score->total - warnings;
153 }
154
155 static void do_compile_tests(struct manifest *m,
156                              bool keep,
157                              unsigned int *timeleft, struct score *score)
158 {
159         compile_tests(m, keep, score, COMPILE_NORMAL);
160 }
161
162 struct ccanlint tests_compile = {
163         .key = "tests_compile",
164         .name = "Module tests compile",
165         .check = do_compile_tests,
166         .can_run = can_build,
167         .needs = "tests_helpers_compile objects_build"
168 };
169
170 REGISTER_TEST(tests_compile);
171
172 static const char *features_reduced(struct manifest *m)
173 {
174         if (features_were_reduced)
175                 return NULL;
176         return "No features to turn off";
177 }
178
179 static void do_compile_tests_without_features(struct manifest *m,
180                                               bool keep,
181                                               unsigned int *timeleft,
182                                               struct score *score)
183 {
184         compile_tests(m, keep, score, COMPILE_NOFEAT);
185 }
186
187 struct ccanlint tests_compile_without_features = {
188         .key = "tests_compile_without_features",
189         .name = "Module tests compile (without features)",
190         .check = do_compile_tests_without_features,
191         .can_run = features_reduced,
192         .needs = "tests_helpers_compile_without_features reduce_features"
193 };
194 REGISTER_TEST(tests_compile_without_features);