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>
17 #include "reduce_features.h"
18 #include "tests_compile.h"
20 static const char *can_build(struct manifest *m)
23 return "Safe mode enabled";
27 char *test_obj_list(const struct manifest *m, bool link_with_module,
28 enum compile_type ctype, enum compile_type own_ctype)
30 char *list = talloc_strdup(m, "");
32 struct manifest *subm;
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",
39 /* Our own object files. */
41 list_for_each(&m->c_files, i, list)
42 list = talloc_asprintf_append(list, " %s",
43 i->compiled[own_ctype]);
45 /* Other ccan modules (normal depends). */
46 list_for_each(&m->deps, subm, list) {
47 if (subm->compiled[ctype])
48 list = talloc_asprintf_append(list, " %s",
49 subm->compiled[ctype]);
52 /* Other ccan modules (test depends). */
53 list_for_each(&m->test_deps, subm, list) {
54 if (subm->compiled[ctype])
55 list = talloc_asprintf_append(list, " %s",
56 subm->compiled[ctype]);
62 char *test_lib_list(const struct manifest *m, enum compile_type ctype)
66 char *ret = talloc_strdup(m, "");
68 libs = get_libs(m, m->dir, "testdepends", get_or_compile_info);
69 for (i = 0; libs[i]; i++)
70 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
74 static bool compile(const void *ctx,
76 struct ccan_file *file,
78 bool link_with_module,
79 enum compile_type ctype,
84 flags = talloc_asprintf(ctx, "%s%s%s",
85 fail ? "-DFAIL " : "",
87 ctype == COMPILE_NOFEAT
88 ? " "REDUCE_FEATURES_FLAGS : "");
90 fname = temp_file(ctx, "", file->fullname);
91 if (!compile_and_link(ctx, file->fullname, ccan_dir,
92 test_obj_list(m, link_with_module,
94 compiler, flags, test_lib_list(m, ctype), fname,
100 file->compiled[ctype] = fname;
104 static void compile_async(const void *ctx,
106 struct ccan_file *file,
107 bool link_with_module,
108 enum compile_type ctype,
109 unsigned int time_ms)
113 file->compiled[ctype] = temp_file(ctx, "", file->fullname);
114 flags = talloc_asprintf(ctx, "%s%s",
116 ctype == COMPILE_NOFEAT
117 ? " "REDUCE_FEATURES_FLAGS : "");
119 compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
120 test_obj_list(m, link_with_module, ctype, ctype),
121 compiler, flags, test_lib_list(m, ctype),
122 file->compiled[ctype]);
125 static void compile_tests(struct manifest *m,
127 enum compile_type ctype,
128 unsigned int time_ms)
132 struct list_head *list;
133 bool errors = false, warnings = false, ok;
135 foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
136 list_for_each(list, i, list) {
137 compile_async(score, m, i,
138 list == &m->api_tests,
143 while ((i = collect_command(&ok, &cmdout)) != NULL) {
145 score_file_error(score, i, 0,
146 "Compile failed:\n%s",
149 } else if (!streq(cmdout, "")) {
150 score_file_error(score, i, 0,
151 "Compile gave warnings:\n%s",
157 /* The compile fail tests are a bit weird, handle them separately */
161 /* For historical reasons, "fail" often means "gives warnings" */
162 list_for_each(&m->compile_fail_tests, i, list) {
163 if (!compile(score, m, i, false, false, ctype, &cmdout)) {
164 score_file_error(score, i, 0,
165 "Compile without -DFAIL failed:\n%s",
169 if (!streq(cmdout, "")) {
170 score_file_error(score, i, 0,
171 "Compile gave warnings"
172 " without -DFAIL:\n%s",
176 if (compile(score, m, i, true, false, ctype, &cmdout)
177 && streq(cmdout, "")) {
178 score_file_error(score, i, 0,
179 "Compiled successfully with -DFAIL?");
186 score->score = score->total - warnings;
189 /* FIXME: If we time out, set *timeleft to 0 */
190 static void do_compile_tests(struct manifest *m,
191 unsigned int *timeleft, struct score *score)
193 compile_tests(m, score, COMPILE_NORMAL, *timeleft);
196 struct ccanlint tests_compile = {
197 .key = "tests_compile",
198 .name = "Module tests compile",
199 .check = do_compile_tests,
200 .can_run = can_build,
201 .needs = "tests_helpers_compile objects_build"
204 REGISTER_TEST(tests_compile);
206 static const char *features_reduced(struct manifest *m)
208 if (features_were_reduced)
210 return "No features to turn off";
213 static void do_compile_tests_without_features(struct manifest *m,
214 unsigned int *timeleft,
217 compile_tests(m, score, COMPILE_NOFEAT, *timeleft);
220 struct ccanlint tests_compile_without_features = {
221 .key = "tests_compile_without_features",
222 .name = "Module tests compile (without features)",
223 .check = do_compile_tests_without_features,
224 .can_run = features_reduced,
225 .needs = "module_builds tests_helpers_compile_without_features objects_build_without_features"
227 REGISTER_TEST(tests_compile_without_features);