ccanlint: get rid of separate class of compulsory tests.
[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 #include "tests_compile.h"
19
20 static const char *can_build(struct manifest *m)
21 {
22         if (safe_mode)
23                 return "Safe mode enabled";
24         return NULL;
25 }
26
27 char *test_obj_list(const struct manifest *m, bool link_with_module,
28                     enum compile_type ctype, enum compile_type own_ctype)
29 {
30         char *list = talloc_strdup(m, "");
31         struct ccan_file *i;
32         struct manifest *subm;
33
34         /* Objects from any other C files. */
35         list_for_each(&m->other_test_c_files, i, list)
36                 list = talloc_asprintf_append(list, " %s",
37                                               i->compiled[ctype]);
38
39         /* Our own object files. */
40         if (link_with_module)
41                 list_for_each(&m->c_files, i, list)
42                         list = talloc_asprintf_append(list, " %s",
43                                                       i->compiled[own_ctype]);
44
45         /* Other ccan modules. */
46         list_for_each(&m->deps, subm, list) {
47                 if (subm->compiled[ctype])
48                         list = talloc_asprintf_append(list, " %s",
49                                                       subm->compiled[ctype]);
50         }
51
52         return list;
53 }
54
55 char *lib_list(const struct manifest *m, enum compile_type ctype)
56 {
57         unsigned int i, num;
58         char **libs = get_libs(m, m->dir, &num,
59                                &m->info_file->compiled[ctype]);
60         char *ret = talloc_strdup(m, "");
61
62         for (i = 0; i < num; i++)
63                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
64         return ret;
65 }
66
67 static bool compile(const void *ctx,
68                     struct manifest *m,
69                     struct ccan_file *file,
70                     bool fail,
71                     bool link_with_module,
72                     bool keep,
73                     enum compile_type ctype,
74                     char **output)
75 {
76         char *fname, *flags;
77
78         flags = talloc_asprintf(ctx, "%s%s%s",
79                                 fail ? "-DFAIL " : "",
80                                 cflags,
81                                 ctype == COMPILE_NOFEAT
82                                 ? " "REDUCE_FEATURES_FLAGS : "");
83
84         fname = maybe_temp_file(ctx, "", keep, file->fullname);
85         if (!compile_and_link(ctx, file->fullname, ccan_dir,
86                               test_obj_list(m, link_with_module,
87                                             ctype, ctype),
88                               compiler, flags, lib_list(m, ctype), fname,
89                               output)) {
90                 talloc_free(fname);
91                 return false;
92         }
93
94         file->compiled[ctype] = fname;
95         return true;
96 }
97
98 static void compile_async(const void *ctx,
99                           struct manifest *m,
100                           struct ccan_file *file,
101                           bool link_with_module,
102                           bool keep,
103                           enum compile_type ctype,
104                           unsigned int time_ms)
105 {
106         char *flags;
107
108         file->compiled[ctype] = maybe_temp_file(ctx, "", keep, file->fullname);
109         flags = talloc_asprintf(ctx, "%s%s",
110                                 cflags,
111                                 ctype == COMPILE_NOFEAT
112                                 ? " "REDUCE_FEATURES_FLAGS : "");
113
114         compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
115                                test_obj_list(m, link_with_module, ctype, ctype),
116                                compiler, flags, lib_list(m, ctype),
117                                file->compiled[ctype]);
118 }
119
120 static void compile_tests(struct manifest *m, bool keep,
121                           struct score *score,
122                           enum compile_type ctype,
123                           unsigned int time_ms)
124 {
125         char *cmdout;
126         struct ccan_file *i;
127         struct list_head *list;
128         bool errors = false, warnings = false, ok;
129
130         foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
131                 list_for_each(list, i, list) {
132                         compile_async(score, m, i,
133                                       list == &m->api_tests, keep,
134                                       ctype, time_ms);
135                 }
136         }
137
138         while ((i = collect_command(&ok, &cmdout)) != NULL) {
139                 if (!ok) {
140                         score_file_error(score, i, 0,
141                                          "Compile failed:\n%s",
142                                          cmdout);
143                         errors = true;
144                 } else if (!streq(cmdout, "")) {
145                         score_file_error(score, i, 0,
146                                          "Compile gave warnings:\n%s",
147                                          cmdout);
148                         warnings = true;
149                 }
150         }
151
152         /* The compile fail tests are a bit weird, handle them separately */
153         if (errors)
154                 return;
155
156         /* For historical reasons, "fail" often means "gives warnings" */
157         list_for_each(&m->compile_fail_tests, i, list) {
158                 if (!compile(score, m, i, false, false, false,
159                              ctype, &cmdout)) {
160                         score_file_error(score, i, 0,
161                                          "Compile without -DFAIL failed:\n%s",
162                                          cmdout);
163                         return;
164                 }
165                 if (!streq(cmdout, "")) {
166                         score_file_error(score, i, 0,
167                                          "Compile gave warnings"
168                                          " without -DFAIL:\n%s",
169                                          cmdout);
170                         return;
171                 }
172                 if (compile(score, m, i, true, false, false,
173                             ctype, &cmdout)
174                     && streq(cmdout, "")) {
175                         score_file_error(score, i, 0,
176                                          "Compiled successfully with -DFAIL?");
177                         return;
178                 }
179                 score->total++;
180         }
181
182         score->pass = true;
183         score->score = score->total - warnings;
184 }
185
186 /* FIXME: If we time out, set *timeleft to 0 */
187 static void do_compile_tests(struct manifest *m,
188                              bool keep,
189                              unsigned int *timeleft, struct score *score)
190 {
191         compile_tests(m, keep, score, COMPILE_NORMAL, *timeleft);
192 }
193
194 struct ccanlint tests_compile = {
195         .key = "tests_compile",
196         .name = "Module tests compile",
197         .check = do_compile_tests,
198         .can_run = can_build,
199         .needs = "tests_helpers_compile objects_build"
200 };
201
202 REGISTER_TEST(tests_compile);
203
204 static const char *features_reduced(struct manifest *m)
205 {
206         if (features_were_reduced)
207                 return NULL;
208         return "No features to turn off";
209 }
210
211 static void do_compile_tests_without_features(struct manifest *m,
212                                               bool keep,
213                                               unsigned int *timeleft,
214                                               struct score *score)
215 {
216         compile_tests(m, keep, score, COMPILE_NOFEAT, *timeleft);
217 }
218
219 struct ccanlint tests_compile_without_features = {
220         .key = "tests_compile_without_features",
221         .name = "Module tests compile (without features)",
222         .check = do_compile_tests_without_features,
223         .can_run = features_reduced,
224         .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
225 };
226 REGISTER_TEST(tests_compile_without_features);