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