]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_compile.c
c939055857021e93c105c2da2352840902b45698
[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)
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)
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 bool compile(const void *ctx,
70                     struct manifest *m,
71                     struct ccan_file *file,
72                     bool fail,
73                     bool link_with_module,
74                     enum compile_type ctype,
75                     char **output)
76 {
77         char *fname, *flags;
78
79         flags = tal_fmt(ctx, "%s%s%s",
80                         fail ? "-DFAIL " : "",
81                         cflags,
82                         ctype == COMPILE_NOFEAT
83                         ? " "REDUCE_FEATURES_FLAGS : "");
84
85         fname = temp_file(ctx, "", file->fullname);
86         if (!compile_and_link(ctx, file->fullname, ccan_dir,
87                               test_obj_list(m, link_with_module,
88                                             ctype, ctype),
89                               compiler, flags, test_lib_list(m, ctype), fname,
90                               output)) {
91                 tal_free(fname);
92                 return false;
93         }
94
95         file->compiled[ctype] = fname;
96         return true;
97 }
98
99 static void compile_async(const void *ctx,
100                           struct manifest *m,
101                           struct ccan_file *file,
102                           bool link_with_module,
103                           enum compile_type ctype,
104                           unsigned int time_ms)
105 {
106         char *flags;
107
108         file->compiled[ctype] = temp_file(ctx, "", file->fullname);
109         flags = tal_fmt(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, test_lib_list(m, ctype),
117                                file->compiled[ctype]);
118 }
119
120 static void compile_tests(struct manifest *m,
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,
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, ctype, &cmdout)) {
159                         score_file_error(score, i, 0,
160                                          "Compile without -DFAIL failed:\n%s",
161                                          cmdout);
162                         return;
163                 }
164                 if (!streq(cmdout, "")) {
165                         score_file_error(score, i, 0,
166                                          "Compile gave warnings"
167                                          " without -DFAIL:\n%s",
168                                          cmdout);
169                         return;
170                 }
171                 if (compile(score, m, i, true, false, ctype, &cmdout)
172                     && streq(cmdout, "")) {
173                         score_file_error(score, i, 0,
174                                          "Compiled successfully with -DFAIL?");
175                         return;
176                 }
177                 score->total++;
178         }
179
180         score->pass = true;
181         score->score = score->total - warnings;
182 }
183
184 /* FIXME: If we time out, set *timeleft to 0 */
185 static void do_compile_tests(struct manifest *m,
186                              unsigned int *timeleft, struct score *score)
187 {
188         compile_tests(m, score, COMPILE_NORMAL, *timeleft);
189 }
190
191 struct ccanlint tests_compile = {
192         .key = "tests_compile",
193         .name = "Module tests compile",
194         .check = do_compile_tests,
195         .can_run = can_build,
196         .needs = "tests_helpers_compile objects_build"
197 };
198
199 REGISTER_TEST(tests_compile);
200
201 static const char *features_reduced(struct manifest *m)
202 {
203         if (features_were_reduced)
204                 return NULL;
205         return "No features to turn off";
206 }
207
208 static void do_compile_tests_without_features(struct manifest *m,
209                                               unsigned int *timeleft,
210                                               struct score *score)
211 {
212         compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
213 }
214
215 struct ccanlint tests_compile_without_features = {
216         .key = "tests_compile_without_features",
217         .name = "Module tests compile (without features)",
218         .check = do_compile_tests_without_features,
219         .can_run = features_reduced,
220         .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
221 };
222 REGISTER_TEST(tests_compile_without_features);