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 bool compile(const void *ctx,
71 struct ccan_file *file,
73 bool link_with_module,
74 enum compile_type ctype,
79 flags = tal_fmt(ctx, "%s%s%s",
80 fail ? "-DFAIL " : "",
82 ctype == COMPILE_NOFEAT
83 ? " "REDUCE_FEATURES_FLAGS : "");
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,
89 compiler, flags, test_lib_list(m, ctype), fname,
95 file->compiled[ctype] = fname;
99 static void compile_async(const void *ctx,
101 struct ccan_file *file,
102 bool link_with_module,
103 enum compile_type ctype,
104 unsigned int time_ms)
108 file->compiled[ctype] = temp_file(ctx, "", file->fullname);
109 flags = tal_fmt(ctx, "%s%s",
111 ctype == COMPILE_NOFEAT
112 ? " "REDUCE_FEATURES_FLAGS : "");
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]);
120 static void compile_tests(struct manifest *m,
122 enum compile_type ctype,
123 unsigned int time_ms)
127 struct list_head *list;
128 bool errors = false, warnings = false, ok;
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,
138 while ((i = collect_command(&ok, &cmdout)) != NULL) {
140 score_file_error(score, i, 0,
141 "Compile failed:\n%s",
144 } else if (!streq(cmdout, "")) {
145 score_file_error(score, i, 0,
146 "Compile gave warnings:\n%s",
152 /* The compile fail tests are a bit weird, handle them separately */
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",
164 if (!streq(cmdout, "")) {
165 score_file_error(score, i, 0,
166 "Compile gave warnings"
167 " without -DFAIL:\n%s",
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?");
181 score->score = score->total - warnings;
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)
188 compile_tests(m, score, COMPILE_NORMAL, *timeleft);
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"
199 REGISTER_TEST(tests_compile);
201 static const char *features_reduced(struct manifest *m)
203 if (features_were_reduced)
205 return "No features to turn off";
208 static void do_compile_tests_without_features(struct manifest *m,
209 unsigned int *timeleft,
212 compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
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"
222 REGISTER_TEST(tests_compile_without_features);