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 /* FIXME: Merge this into one place. */
27 static char *obj_list(const struct manifest *m, bool link_with_module)
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", i->compiled);
37 /* Our own object files. */
39 list_for_each(&m->c_files, i, list)
40 list = talloc_asprintf_append(list, " %s", i->compiled);
42 /* Other ccan modules. */
43 list_for_each(&m->deps, subm, list) {
45 list = talloc_asprintf_append(list, " %s",
52 static char *lib_list(const struct manifest *m)
55 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
56 char *ret = talloc_strdup(m, "");
58 for (i = 0; i < num; i++)
59 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
63 static bool compile(const void *ctx,
65 struct ccan_file *file,
68 bool link_with_module,
69 bool keep, char **output)
71 char *f = talloc_asprintf(ctx, "%s%s%s",
72 flags, fail ? "-DFAIL " : "", cflags);
74 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
75 if (!compile_and_link(ctx, file->fullname, ccan_dir,
76 obj_list(m, link_with_module), compiler, f,
77 lib_list(m), file->compiled, output)) {
78 talloc_free(file->compiled);
84 static void compile_tests(struct manifest *m, bool keep,
85 struct score *score, const char *incl)
89 struct list_head *list;
90 bool errors = false, warnings = false;
92 foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
93 list_for_each(list, i, list) {
94 if (!compile(score, m, i, incl, false,
95 list == &m->api_tests, keep, &cmdout)) {
96 score_file_error(score, i, 0,
97 "Compile failed:\n%s",
100 } else if (!streq(cmdout, "")) {
101 score_file_error(score, i, 0,
102 "Compile gave warnings:\n%s",
109 /* The compile fail tests are a bit weird, handle them separately */
113 /* For historical reasons, "fail" often means "gives warnings" */
114 list_for_each(&m->compile_fail_tests, i, list) {
115 if (!compile(score, m, i, incl, false, false, false, &cmdout)) {
116 score_file_error(score, i, 0,
117 "Compile without -DFAIL failed:\n%s",
121 if (!streq(cmdout, "")) {
122 score_file_error(score, i, 0,
123 "Compile gave warnings"
124 " without -DFAIL:\n%s",
128 if (compile(score, m, i, incl, true, false, false, &cmdout)
129 && streq(cmdout, "")) {
130 score_file_error(score, i, 0,
131 "Compiled successfully with -DFAIL?");
138 score->score = 1 + !warnings;
141 static void do_compile_tests(struct manifest *m,
143 unsigned int *timeleft, struct score *score)
145 return compile_tests(m, keep, score, "");
148 struct ccanlint tests_compile = {
149 .key = "tests_compile",
150 .name = "Module tests compile",
151 .check = do_compile_tests,
152 .can_run = can_build,
153 .needs = "tests_helpers_compile objects_build"
156 REGISTER_TEST(tests_compile);
158 static const char *features_reduced(struct manifest *m)
160 if (features_were_reduced)
162 return "No features to turn off";
165 static void do_compile_tests_without_features(struct manifest *m,
167 unsigned int *timeleft,
170 return compile_tests(m, keep, score, "-I. ");
173 struct ccanlint tests_compile_without_features = {
174 .key = "tests_compile_without_features",
175 .name = "Module tests compile (without features)",
176 .check = do_compile_tests_without_features,
177 .can_run = features_reduced,
178 .needs = "reduce_features"
180 REGISTER_TEST(tests_compile_without_features);