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 ? " -I." : "");
83 fname = maybe_temp_file(ctx, "", keep, file->fullname);
84 if (!compile_and_link(ctx, file->fullname, ccan_dir,
85 test_obj_list(m, link_with_module,
87 compiler, flags, lib_list(m, ctype), fname,
93 file->compiled[ctype] = fname;
97 static void compile_tests(struct manifest *m, bool keep,
99 enum compile_type ctype)
103 struct list_head *list;
104 bool errors = false, warnings = false;
106 foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
107 list_for_each(list, i, list) {
108 if (!compile(score, m, i, false,
109 list == &m->api_tests, keep,
111 score_file_error(score, i, 0,
112 "Compile failed:\n%s",
115 } else if (!streq(cmdout, "")) {
116 score_file_error(score, i, 0,
117 "Compile gave warnings:\n%s",
124 /* The compile fail tests are a bit weird, handle them separately */
128 /* For historical reasons, "fail" often means "gives warnings" */
129 list_for_each(&m->compile_fail_tests, i, list) {
130 if (!compile(score, m, i, false, false, false,
132 score_file_error(score, i, 0,
133 "Compile without -DFAIL failed:\n%s",
137 if (!streq(cmdout, "")) {
138 score_file_error(score, i, 0,
139 "Compile gave warnings"
140 " without -DFAIL:\n%s",
144 if (compile(score, m, i, true, false, false,
146 && streq(cmdout, "")) {
147 score_file_error(score, i, 0,
148 "Compiled successfully with -DFAIL?");
155 score->score = score->total - warnings;
158 static void do_compile_tests(struct manifest *m,
160 unsigned int *timeleft, struct score *score)
162 compile_tests(m, keep, score, COMPILE_NORMAL);
165 struct ccanlint tests_compile = {
166 .key = "tests_compile",
167 .name = "Module tests compile",
168 .check = do_compile_tests,
169 .can_run = can_build,
170 .needs = "tests_helpers_compile objects_build"
173 REGISTER_TEST(tests_compile);
175 static const char *features_reduced(struct manifest *m)
177 if (features_were_reduced)
179 return "No features to turn off";
182 static void do_compile_tests_without_features(struct manifest *m,
184 unsigned int *timeleft,
187 compile_tests(m, keep, score, COMPILE_NOFEAT);
190 struct ccanlint tests_compile_without_features = {
191 .key = "tests_compile_without_features",
192 .name = "Module tests compile (without features)",
193 .check = do_compile_tests_without_features,
194 .can_run = features_reduced,
195 .needs = "tests_helpers_compile_without_features reduce_features"
197 REGISTER_TEST(tests_compile_without_features);