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"
19 static const char *can_build(struct manifest *m)
22 return "Safe mode enabled";
26 static char *obj_list(const struct manifest *m, bool link_with_module,
27 enum compile_type ctype)
29 char *list = talloc_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 list = talloc_asprintf_append(list, " %s",
38 /* Our own object files. */
40 list_for_each(&m->c_files, i, list)
41 list = talloc_asprintf_append(list, " %s",
44 /* Other ccan modules. */
45 list_for_each(&m->deps, subm, list) {
46 if (subm->compiled[ctype])
47 list = talloc_asprintf_append(list, " %s",
48 subm->compiled[ctype]);
54 static char *lib_list(const struct manifest *m, enum compile_type ctype)
57 char **libs = get_libs(m, m->dir, &num,
58 &m->info_file->compiled[ctype]);
59 char *ret = talloc_strdup(m, "");
61 for (i = 0; i < num; i++)
62 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
66 static bool compile(const void *ctx,
68 struct ccan_file *file,
70 bool link_with_module,
72 enum compile_type ctype,
77 flags = talloc_asprintf(ctx, "%s%s%s",
78 fail ? "-DFAIL " : "",
80 ctype == COMPILE_NOFEAT ? " -I." : "");
82 fname = maybe_temp_file(ctx, "", keep, file->fullname);
83 if (!compile_and_link(ctx, file->fullname, ccan_dir,
84 obj_list(m, link_with_module, ctype), compiler,
85 flags, lib_list(m, ctype), fname, output)) {
90 file->compiled[ctype] = fname;
94 static void compile_tests(struct manifest *m, bool keep,
96 enum compile_type ctype)
100 struct list_head *list;
101 bool errors = false, warnings = false;
103 foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
104 list_for_each(list, i, list) {
105 if (!compile(score, m, i, false,
106 list == &m->api_tests, keep,
108 score_file_error(score, i, 0,
109 "Compile failed:\n%s",
112 } else if (!streq(cmdout, "")) {
113 score_file_error(score, i, 0,
114 "Compile gave warnings:\n%s",
121 /* The compile fail tests are a bit weird, handle them separately */
125 /* For historical reasons, "fail" often means "gives warnings" */
126 list_for_each(&m->compile_fail_tests, i, list) {
127 if (!compile(score, m, i, false, false, false,
129 score_file_error(score, i, 0,
130 "Compile without -DFAIL failed:\n%s",
134 if (!streq(cmdout, "")) {
135 score_file_error(score, i, 0,
136 "Compile gave warnings"
137 " without -DFAIL:\n%s",
141 if (compile(score, m, i, true, false, false,
143 && streq(cmdout, "")) {
144 score_file_error(score, i, 0,
145 "Compiled successfully with -DFAIL?");
152 score->score = score->total - warnings;
155 static void do_compile_tests(struct manifest *m,
157 unsigned int *timeleft, struct score *score)
159 compile_tests(m, keep, score, COMPILE_NORMAL);
162 struct ccanlint tests_compile = {
163 .key = "tests_compile",
164 .name = "Module tests compile",
165 .check = do_compile_tests,
166 .can_run = can_build,
167 .needs = "tests_helpers_compile objects_build"
170 REGISTER_TEST(tests_compile);
172 static const char *features_reduced(struct manifest *m)
174 if (features_were_reduced)
176 return "No features to turn off";
179 static void do_compile_tests_without_features(struct manifest *m,
181 unsigned int *timeleft,
184 compile_tests(m, keep, score, COMPILE_NOFEAT);
187 struct ccanlint tests_compile_without_features = {
188 .key = "tests_compile_without_features",
189 .name = "Module tests compile (without features)",
190 .check = do_compile_tests_without_features,
191 .can_run = features_reduced,
192 .needs = "tests_helpers_compile_without_features reduce_features"
194 REGISTER_TEST(tests_compile_without_features);