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 /* Note: we already test safe_mode in run_tests.c */
19 static const char *can_run_coverage(struct manifest *m)
21 unsigned int timeleft = default_timeout_ms;
24 if (!run_command(m, &timeleft, &output, "gcov -h"))
25 return talloc_asprintf(m, "No gcov support: %s", output);
29 static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
35 *modobjs = talloc_strdup(m, "");
36 list_for_each(&m->c_files, i, list) {
38 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
40 i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
41 if (!compile_object(m, fullfile, ccan_dir, "",
42 i->cov_compiled, &err)) {
43 score_file_error(score, i, 0, err);
44 talloc_free(i->cov_compiled);
45 i->cov_compiled = NULL;
48 *modobjs = talloc_asprintf_append(*modobjs,
49 " %s", i->cov_compiled);
54 /* FIXME: Merge this into one place. */
55 static char *obj_list(const struct manifest *m, const char *modobjs)
57 char *list = talloc_strdup(m, "");
59 struct manifest *subm;
61 /* Objects from any other C files. */
62 list_for_each(&m->other_test_c_files, i, list)
63 list = talloc_asprintf_append(list, " %s", i->compiled);
66 list = talloc_append_string(list, modobjs);
68 /* Other ccan modules (don't need coverage versions of those). */
69 list_for_each(&m->deps, subm, list) {
71 list = talloc_asprintf_append(list, " %s",
78 static char *lib_list(const struct manifest *m)
81 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
82 char *ret = talloc_strdup(m, "");
84 for (i = 0; i < num; i++)
85 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
89 static char *cov_compile(const void *ctx,
91 struct ccan_file *file,
97 file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
98 if (!compile_and_link(ctx, file->fullname, ccan_dir,
101 lib_list(m), file->cov_compiled, &output)) {
102 talloc_free(file->cov_compiled);
103 file->cov_compiled = NULL;
110 /* FIXME: Coverage from testable examples as well. */
111 static void do_compile_coverage_tests(struct manifest *m,
113 unsigned int *timeleft,
116 char *cmdout, *modobjs = NULL;
120 if (!list_empty(&m->api_tests)
121 && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
122 score->error = talloc_strdup(score,
123 "Failed to compile module objects with coverage");
127 foreach_ptr(h, &m->run_tests, &m->api_tests) {
128 list_for_each(h, i, list) {
129 cmdout = cov_compile(m, m, i, NULL, keep);
131 score_file_error(score, i, 0,
132 "Failed to compile test with coverage: %s",
139 score->score = score->total;
143 struct ccanlint tests_compile_coverage = {
144 .key = "tests_compile_coverage",
145 .name = "Module tests compile with " COVERAGE_CFLAGS,
146 .check = do_compile_coverage_tests,
147 .can_run = can_run_coverage,
148 .needs = "tests_compile"
151 REGISTER_TEST(tests_compile_coverage);