1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
17 /* Note: we already test safe_mode in run_tests.c */
18 static const char *can_run_coverage(struct manifest *m)
20 unsigned int timeleft = default_timeout_ms;
23 if (!run_command(m, &timeleft, &output, "gcov -h"))
24 return talloc_asprintf(m, "No gcov support: %s", output);
28 static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
34 *modobjs = talloc_strdup(m, "");
35 list_for_each(&m->c_files, i, list) {
37 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
39 i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
40 if (!compile_object(m, fullfile, ccan_dir, "",
41 i->cov_compiled, &err)) {
42 score_file_error(score, i, 0, err);
43 talloc_free(i->cov_compiled);
44 i->cov_compiled = NULL;
47 *modobjs = talloc_asprintf_append(*modobjs,
48 " %s", i->cov_compiled);
53 /* FIXME: Merge this into one place. */
54 static char *obj_list(const struct manifest *m, const char *modobjs)
56 char *list = talloc_strdup(m, "");
58 struct manifest *subm;
60 /* Objects from any other C files. */
61 list_for_each(&m->other_test_c_files, i, list)
62 list = talloc_asprintf_append(list, " %s", i->compiled);
65 list = talloc_append_string(list, modobjs);
67 /* Other ccan modules (don't need coverage versions of those). */
68 list_for_each(&m->deps, subm, list) {
70 list = talloc_asprintf_append(list, " %s",
77 static char *lib_list(const struct manifest *m)
80 char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
81 char *ret = talloc_strdup(m, "");
83 for (i = 0; i < num; i++)
84 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
88 static char *cov_compile(const void *ctx,
90 struct ccan_file *file,
96 file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
97 if (!compile_and_link(ctx, file->fullname, ccan_dir,
100 lib_list(m), file->cov_compiled, &output)) {
101 talloc_free(file->cov_compiled);
102 file->cov_compiled = NULL;
109 /* FIXME: Coverage from testable examples as well. */
110 static void do_compile_coverage_tests(struct manifest *m,
112 unsigned int *timeleft,
115 char *cmdout, *modobjs = NULL;
118 if (!list_empty(&m->api_tests)
119 && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
120 score->error = "Failed to compile module objects with coverage";
124 list_for_each(&m->run_tests, i, list) {
125 cmdout = cov_compile(m, m, i, NULL, keep);
127 score->error = "Failed to compile test with coverage";
128 score_file_error(score, i, 0, cmdout);
132 list_for_each(&m->api_tests, i, list) {
133 cmdout = cov_compile(m, m, i, modobjs, keep);
135 score->error = "Failed to compile test with coverage";
136 score_file_error(score, i, 0, cmdout);
141 score->score = score->total;
145 struct ccanlint tests_compile_coverage = {
146 .key = "tests_compile_coverage",
147 .name = "Module tests compile with " COVERAGE_CFLAGS,
148 .check = do_compile_coverage_tests,
149 .can_run = can_run_coverage,
150 .needs = "tests_compile"
153 REGISTER_TEST(tests_compile_coverage);