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, compiler, cflags,
42 i->cov_compiled, &err)) {
43 score_file_error(score, i, 0, "%s", 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",
64 i->compiled[COMPILE_NORMAL]);
67 list = talloc_append_string(list, modobjs);
69 /* Other ccan modules (don't need coverage versions of those). */
70 list_for_each(&m->deps, subm, list) {
71 if (subm->compiled[COMPILE_NORMAL])
72 list = talloc_asprintf_append(list, " %s",
80 static char *lib_list(const struct manifest *m)
83 char **libs = get_libs(m, m->dir, &num,
84 &m->info_file->compiled[COMPILE_NORMAL]);
85 char *ret = talloc_strdup(m, "");
87 for (i = 0; i < num; i++)
88 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
92 static char *cov_compile(const void *ctx,
94 struct ccan_file *file,
99 char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
101 file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
102 if (!compile_and_link(ctx, file->fullname, ccan_dir,
103 obj_list(m, modobjs),
105 lib_list(m), file->cov_compiled, &output)) {
106 talloc_free(file->cov_compiled);
107 file->cov_compiled = NULL;
114 /* FIXME: Coverage from testable examples as well. */
115 static void do_compile_coverage_tests(struct manifest *m,
117 unsigned int *timeleft,
120 char *cmdout, *modobjs = NULL;
124 if (!list_empty(&m->api_tests)
125 && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
126 score->error = talloc_strdup(score,
127 "Failed to compile module objects with coverage");
131 foreach_ptr(h, &m->run_tests, &m->api_tests) {
132 list_for_each(h, i, list) {
133 cmdout = cov_compile(m, m, i,
138 score_file_error(score, i, 0,
139 "Failed to compile test with coverage: %s",
146 score->score = score->total;
150 struct ccanlint tests_compile_coverage = {
151 .key = "tests_compile_coverage",
152 .name = "Module tests compile with " COVERAGE_CFLAGS,
153 .check = do_compile_coverage_tests,
154 .can_run = can_run_coverage,
155 .needs = "tests_compile"
158 REGISTER_TEST(tests_compile_coverage);