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. */
46 list_for_each(&m->deps, subm, list) {
47 if (subm->compiled[ctype])
48 list = talloc_asprintf_append(list, " %s",
49 subm->compiled[ctype]);
55 char *lib_list(const struct manifest *m, enum compile_type ctype)
58 char **libs = get_libs(m, m->dir, &num,
59 &m->info_file->compiled[ctype]);
60 char *ret = talloc_strdup(m, "");
62 for (i = 0; i < num; i++)
63 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
67 static bool compile(const void *ctx,
69 struct ccan_file *file,
71 bool link_with_module,
73 enum compile_type ctype,
78 flags = talloc_asprintf(ctx, "%s%s%s",
79 fail ? "-DFAIL " : "",
81 ctype == COMPILE_NOFEAT
82 ? " "REDUCE_FEATURES_FLAGS : "");
84 fname = maybe_temp_file(ctx, "", keep, file->fullname);
85 if (!compile_and_link(ctx, file->fullname, ccan_dir,
86 test_obj_list(m, link_with_module,
88 compiler, flags, lib_list(m, ctype), fname,
94 file->compiled[ctype] = fname;
98 static void compile_tests(struct manifest *m, bool keep,
100 enum compile_type ctype)
104 struct list_head *list;
105 bool errors = false, warnings = false;
107 foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
108 list_for_each(list, i, list) {
109 if (!compile(score, m, i, false,
110 list == &m->api_tests, keep,
112 score_file_error(score, i, 0,
113 "Compile failed:\n%s",
116 } else if (!streq(cmdout, "")) {
117 score_file_error(score, i, 0,
118 "Compile gave warnings:\n%s",
125 /* The compile fail tests are a bit weird, handle them separately */
129 /* For historical reasons, "fail" often means "gives warnings" */
130 list_for_each(&m->compile_fail_tests, i, list) {
131 if (!compile(score, m, i, false, false, false,
133 score_file_error(score, i, 0,
134 "Compile without -DFAIL failed:\n%s",
138 if (!streq(cmdout, "")) {
139 score_file_error(score, i, 0,
140 "Compile gave warnings"
141 " without -DFAIL:\n%s",
145 if (compile(score, m, i, true, false, false,
147 && streq(cmdout, "")) {
148 score_file_error(score, i, 0,
149 "Compiled successfully with -DFAIL?");
156 score->score = score->total - warnings;
159 static void do_compile_tests(struct manifest *m,
161 unsigned int *timeleft, struct score *score)
163 compile_tests(m, keep, score, COMPILE_NORMAL);
166 struct ccanlint tests_compile = {
167 .key = "tests_compile",
168 .name = "Module tests compile",
169 .check = do_compile_tests,
170 .can_run = can_build,
171 .needs = "tests_helpers_compile objects_build"
174 REGISTER_TEST(tests_compile);
176 static const char *features_reduced(struct manifest *m)
178 if (features_were_reduced)
180 return "No features to turn off";
183 static void do_compile_tests_without_features(struct manifest *m,
185 unsigned int *timeleft,
188 compile_tests(m, keep, score, COMPILE_NOFEAT);
191 struct ccanlint tests_compile_without_features = {
192 .key = "tests_compile_without_features",
193 .name = "Module tests compile (without features)",
194 .check = do_compile_tests_without_features,
195 .can_run = features_reduced,
196 .needs = "tests_helpers_compile_without_features objects_build_without_features"
198 REGISTER_TEST(tests_compile_without_features);