tools: enhance get_libs to get libraries for tests, too.
[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;
58         char **libs;
59         char *ret = talloc_strdup(m, "");
60
61         libs = get_libs(m, m->dir, "depends", get_or_compile_info);
62         for (i = 0; libs[i]; 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                     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
81                                 ? " "REDUCE_FEATURES_FLAGS : "");
82
83         fname = temp_file(ctx, "", file->fullname);
84         if (!compile_and_link(ctx, file->fullname, ccan_dir,
85                               test_obj_list(m, link_with_module,
86                                             ctype, ctype),
87                               compiler, flags, lib_list(m, ctype), fname,
88                               output)) {
89                 talloc_free(fname);
90                 return false;
91         }
92
93         file->compiled[ctype] = fname;
94         return true;
95 }
96
97 static void compile_async(const void *ctx,
98                           struct manifest *m,
99                           struct ccan_file *file,
100                           bool link_with_module,
101                           enum compile_type ctype,
102                           unsigned int time_ms)
103 {
104         char *flags;
105
106         file->compiled[ctype] = temp_file(ctx, "", file->fullname);
107         flags = talloc_asprintf(ctx, "%s%s",
108                                 cflags,
109                                 ctype == COMPILE_NOFEAT
110                                 ? " "REDUCE_FEATURES_FLAGS : "");
111
112         compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
113                                test_obj_list(m, link_with_module, ctype, ctype),
114                                compiler, flags, lib_list(m, ctype),
115                                file->compiled[ctype]);
116 }
117
118 static void compile_tests(struct manifest *m,
119                           struct score *score,
120                           enum compile_type ctype,
121                           unsigned int time_ms)
122 {
123         char *cmdout;
124         struct ccan_file *i;
125         struct list_head *list;
126         bool errors = false, warnings = false, ok;
127
128         foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
129                 list_for_each(list, i, list) {
130                         compile_async(score, m, i,
131                                       list == &m->api_tests,
132                                       ctype, time_ms);
133                 }
134         }
135
136         while ((i = collect_command(&ok, &cmdout)) != NULL) {
137                 if (!ok) {
138                         score_file_error(score, i, 0,
139                                          "Compile failed:\n%s",
140                                          cmdout);
141                         errors = true;
142                 } else if (!streq(cmdout, "")) {
143                         score_file_error(score, i, 0,
144                                          "Compile gave warnings:\n%s",
145                                          cmdout);
146                         warnings = true;
147                 }
148         }
149
150         /* The compile fail tests are a bit weird, handle them separately */
151         if (errors)
152                 return;
153
154         /* For historical reasons, "fail" often means "gives warnings" */
155         list_for_each(&m->compile_fail_tests, i, list) {
156                 if (!compile(score, m, i, false, false, ctype, &cmdout)) {
157                         score_file_error(score, i, 0,
158                                          "Compile without -DFAIL failed:\n%s",
159                                          cmdout);
160                         return;
161                 }
162                 if (!streq(cmdout, "")) {
163                         score_file_error(score, i, 0,
164                                          "Compile gave warnings"
165                                          " without -DFAIL:\n%s",
166                                          cmdout);
167                         return;
168                 }
169                 if (compile(score, m, i, true, false, ctype, &cmdout)
170                     && streq(cmdout, "")) {
171                         score_file_error(score, i, 0,
172                                          "Compiled successfully with -DFAIL?");
173                         return;
174                 }
175                 score->total++;
176         }
177
178         score->pass = true;
179         score->score = score->total - warnings;
180 }
181
182 /* FIXME: If we time out, set *timeleft to 0 */
183 static void do_compile_tests(struct manifest *m,
184                              unsigned int *timeleft, struct score *score)
185 {
186         compile_tests(m, score, COMPILE_NORMAL, *timeleft);
187 }
188
189 struct ccanlint tests_compile = {
190         .key = "tests_compile",
191         .name = "Module tests compile",
192         .check = do_compile_tests,
193         .can_run = can_build,
194         .needs = "tests_helpers_compile objects_build"
195 };
196
197 REGISTER_TEST(tests_compile);
198
199 static const char *features_reduced(struct manifest *m)
200 {
201         if (features_were_reduced)
202                 return NULL;
203         return "No features to turn off";
204 }
205
206 static void do_compile_tests_without_features(struct manifest *m,
207                                               unsigned int *timeleft,
208                                               struct score *score)
209 {
210         compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
211 }
212
213 struct ccanlint tests_compile_without_features = {
214         .key = "tests_compile_without_features",
215         .name = "Module tests compile (without features)",
216         .check = do_compile_tests_without_features,
217         .can_run = features_reduced,
218         .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
219 };
220 REGISTER_TEST(tests_compile_without_features);