1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/foreach/foreach.h>
16 #include "reduce_features.h"
17 #include "tests_compile.h"
19 static const char *can_build(struct manifest *m)
22 return "Safe mode enabled";
26 char *test_obj_list(const struct manifest *m, bool link_with_module,
27 enum compile_type ctype, enum compile_type own_ctype)
29 char *list = tal_strdup(m, "");
31 struct manifest *subm;
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]);
37 /* Our own object files. */
39 list_for_each(&m->c_files, i, list)
40 tal_append_fmt(&list, " %s", i->compiled[own_ctype]);
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]);
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]);
57 char *test_lib_list(const struct manifest *m, enum compile_type ctype)
61 char *ret = tal_strdup(m, "");
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]);
69 static char *cflags_list(const struct manifest *m, const char *iflags)
72 char *ret = tal_strdup(m, iflags);
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]);
80 static bool compile(const void *ctx,
82 struct ccan_file *file,
84 bool link_with_module,
85 enum compile_type ctype,
90 flags = tal_fmt(ctx, "%s%s%s",
91 fail ? "-DFAIL " : "",
93 ctype == COMPILE_NOFEAT
94 ? " "REDUCE_FEATURES_FLAGS : "");
95 flags = cflags_list(m, flags);
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,
101 compiler, flags, test_lib_list(m, ctype), fname,
107 file->compiled[ctype] = fname;
111 static void compile_async(const void *ctx,
113 struct ccan_file *file,
114 bool link_with_module,
115 enum compile_type ctype,
116 unsigned int time_ms)
120 file->compiled[ctype] = temp_file(ctx, "", file->fullname);
121 flags = tal_fmt(ctx, "%s%s",
123 ctype == COMPILE_NOFEAT
124 ? " "REDUCE_FEATURES_FLAGS : "");
125 flags = cflags_list(m, flags);
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]);
133 static void compile_tests(struct manifest *m,
135 enum compile_type ctype,
136 unsigned int time_ms)
140 struct list_head *list;
141 bool errors = false, warnings = false, ok;
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,
151 while ((i = collect_command(&ok, &cmdout)) != NULL) {
153 score_file_error(score, i, 0,
154 "Compile failed:\n%s",
157 } else if (!streq(cmdout, "")) {
158 score_file_error(score, i, 0,
159 "Compile gave warnings:\n%s",
165 /* The compile fail tests are a bit weird, handle them separately */
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",
177 if (!streq(cmdout, "")) {
178 score_file_error(score, i, 0,
179 "Compile gave warnings"
180 " without -DFAIL:\n%s",
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?");
194 score->score = score->total - warnings;
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)
201 compile_tests(m, score, COMPILE_NORMAL, *timeleft);
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"
212 REGISTER_TEST(tests_compile);
214 static const char *features_reduced(struct manifest *m)
216 if (features_were_reduced)
218 return "No features to turn off";
221 static void do_compile_tests_without_features(struct manifest *m,
222 unsigned int *timeleft,
225 compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
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"
235 REGISTER_TEST(tests_compile_without_features);