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>
18 static const char *can_build(struct manifest *m)
21 return "Safe mode enabled";
25 static char *obj_list(const struct manifest *m, bool link_with_module)
30 /* We expect to be linked with tap, unless that's us. */
31 if (!streq(m->basename, "tap"))
32 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
34 list = talloc_strdup(m, "");
36 /* Objects from any other C files. */
37 list_for_each(&m->other_test_c_files, i, list)
38 list = talloc_asprintf_append(list, " %s", i->compiled);
40 /* Our own object files. */
42 list_for_each(&m->c_files, i, list)
43 list = talloc_asprintf_append(list, " %s", i->compiled);
45 /* Other ccan modules. */
46 list_for_each(&m->dep_dirs, i, list) {
48 list = talloc_asprintf_append(list, " %s", i->compiled);
54 static char *lib_list(const struct manifest *m)
57 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
58 char *ret = talloc_strdup(m, "");
60 for (i = 0; i < num; i++)
61 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
65 static bool compile(const void *ctx,
67 struct ccan_file *file,
69 bool link_with_module,
70 bool keep, char **output)
72 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
73 if (!compile_and_link(ctx, file->fullname, ccan_dir,
74 obj_list(m, link_with_module),
76 lib_list(m), file->compiled, output)) {
77 talloc_free(file->compiled);
83 static void do_compile_tests(struct manifest *m,
85 unsigned int *timeleft, struct score *score)
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, false, list == &m->api_tests,
96 score->error = "Failed to compile tests";
97 score_file_error(score, i, 0, cmdout);
99 } else if (!streq(cmdout, "")) {
100 score->error = "Test compiled with warnings";
101 score_file_error(score, i, 0, cmdout);
107 /* The compile fail tests are a bit weird, handle them separately */
111 /* For historical reasons, "fail" often means "gives warnings" */
112 list_for_each(&m->compile_fail_tests, i, list) {
113 if (!compile(score, m, i, false, false, false, &cmdout)) {
114 score->error = "Failed to compile without -DFAIL";
115 score_file_error(score, i, 0, cmdout);
118 if (!streq(cmdout, "")) {
119 score->error = "Compile with warnigns without -DFAIL";
120 score_file_error(score, i, 0, cmdout);
123 if (compile(score, m, i, true, false, false, &cmdout)
124 && streq(cmdout, "")) {
125 score->error = "Compiled successfully with -DFAIL?";
126 score_file_error(score, i, 0, NULL);
133 score->score = 1 + !warnings;
136 struct ccanlint compile_tests = {
137 .key = "compile-tests",
138 .name = "Module tests compile",
139 .check = do_compile_tests,
140 .can_run = can_build,
143 REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);