1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/foreach/foreach.h>
17 #include "tests_compile.h"
19 /* Note: we already test safe_mode in run_tests.c */
20 static const char *can_run_coverage(struct manifest *m)
23 unsigned int timeleft = default_timeout_ms;
26 if (!run_command(m, &timeleft, &output, "gcov -h"))
27 return tal_fmt(m, "No gcov support: %s", output);
30 return "No coverage support for this compiler";
34 static char *cflags_list(const struct manifest *m)
37 char *ret = tal_strdup(m, cflags);
39 char **flags = get_cflags(m, m->dir, get_or_compile_info);
40 for (i = 0; flags[i]; i++)
41 tal_append_fmt(&ret, " %s", flags[i]);
45 static char *cflags_list_append(const struct manifest *m, char *iflags)
49 char **flags = get_cflags(m, m->dir, get_or_compile_info);
50 for (i = 0; flags[i]; i++)
51 tal_append_fmt(&iflags, " %s", flags[i]);
55 static void cov_compile(const void *ctx,
58 struct ccan_file *file,
59 bool link_with_module)
61 char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
62 flags = cflags_list_append(m, flags);
64 file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
65 compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
66 test_obj_list(m, link_with_module,
70 test_lib_list(m, COMPILE_NORMAL),
71 file->compiled[COMPILE_COVERAGE]);
74 /* FIXME: Coverage from testable examples as well. */
75 static void do_compile_coverage_tests(struct manifest *m,
76 unsigned int *timeleft,
83 char *f = cflags_list(m);
84 tal_append_fmt(&f, " %s", COVERAGE_CFLAGS);
86 /* For API tests, we need coverage version of module. */
87 if (!list_empty(&m->api_tests)) {
88 build_objects(m, score, f, COMPILE_COVERAGE);
90 score->error = tal_strdup(score,
91 "Failed to compile module objects with coverage");
96 foreach_ptr(h, &m->run_tests, &m->api_tests) {
97 list_for_each(h, i, list) {
98 cov_compile(m, *timeleft, m, i, h == &m->api_tests);
102 while ((i = collect_command(&ok, &cmdout)) != NULL) {
104 score_file_error(score, i, 0,
105 "Failed to compile test with coverage:"
111 score->score = score->total;
115 struct ccanlint tests_compile_coverage = {
116 .key = "tests_compile_coverage",
117 .name = "Module tests compile with " COVERAGE_CFLAGS,
118 .check = do_compile_coverage_tests,
119 .can_run = can_run_coverage,
120 .needs = "tests_compile"
123 REGISTER_TEST(tests_compile_coverage);