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;
21 char *output = run_command(m, &timeleft, "gcov -h");
24 return talloc_asprintf(m, "No gcov support: %s", output);
28 static char *build_module_objs_with_coverage(struct manifest *m, bool keep,
33 *modobjs = talloc_strdup(m, "");
34 list_for_each(&m->c_files, i, list) {
36 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
38 i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
39 err = compile_object(m, fullfile, ccan_dir, "",
42 talloc_free(i->cov_compiled);
45 *modobjs = talloc_asprintf_append(*modobjs,
46 " %s", i->cov_compiled);
51 static char *obj_list(const struct manifest *m, const char *modobjs)
56 /* We expect to be linked with tap, unless that's us. */
57 if (!streq(m->basename, "tap"))
58 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
60 list = talloc_strdup(m, "");
62 /* Objects from any other C files. */
63 list_for_each(&m->other_test_c_files, i, list)
64 list = talloc_asprintf_append(list, " %s", i->compiled);
67 list = talloc_append_string(list, modobjs);
69 /* Other ccan modules (don't need coverage versions of those). */
70 list_for_each(&m->dep_dirs, i, list) {
72 list = talloc_asprintf_append(list, " %s", i->compiled);
78 static char *lib_list(const struct manifest *m)
81 char **libs = get_libs(m, ".", &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 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
101 lib_list(m), file->cov_compiled);
103 talloc_free(file->cov_compiled);
110 struct compile_tests_result {
111 struct list_node list;
112 const char *filename;
113 const char *description;
117 static void *do_compile_coverage_tests(struct manifest *m,
119 unsigned int *timeleft)
121 struct list_head *list = talloc(m, struct list_head);
122 char *cmdout, *modobjs = NULL;
124 struct compile_tests_result *res;
126 list_head_init(list);
128 if (!list_empty(&m->api_tests)) {
129 cmdout = build_module_objs_with_coverage(m, keep, &modobjs);
131 res = talloc(list, struct compile_tests_result);
132 res->filename = "Module objects with coverage";
133 res->description = "failed to compile";
134 res->output = talloc_steal(res, cmdout);
135 list_add_tail(list, &res->list);
140 list_for_each(&m->run_tests, i, list) {
141 compile_tests.total_score++;
142 cmdout = cov_compile(m, m, i, NULL, keep);
144 res = talloc(list, struct compile_tests_result);
145 res->filename = i->name;
146 res->description = "failed to compile";
147 res->output = talloc_steal(res, cmdout);
148 list_add_tail(list, &res->list);
152 list_for_each(&m->api_tests, i, list) {
153 compile_tests.total_score++;
154 cmdout = cov_compile(m, m, i, modobjs, keep);
156 res = talloc(list, struct compile_tests_result);
157 res->filename = i->name;
158 res->description = "failed to compile";
159 res->output = talloc_steal(res, cmdout);
160 list_add_tail(list, &res->list);
164 if (list_empty(list)) {
172 static const char *describe_compile_coverage_tests(struct manifest *m,
175 struct list_head *list = check_result;
176 struct compile_tests_result *i;
179 descrip = talloc_strdup(list,
180 "Compilation of tests for coverage failed:\n");
182 list_for_each(list, i, list)
183 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
184 i->filename, i->description,
189 struct ccanlint compile_coverage_tests = {
190 .key = "compile-coverage-tests",
191 .name = "Module tests compile with profiling",
192 .check = do_compile_coverage_tests,
193 .describe = describe_compile_coverage_tests,
194 .can_run = can_run_coverage,
197 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);