1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
16 #include "build-coverage.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;
22 char *output = run_command(m, &timeleft, "gcov -h");
25 return talloc_asprintf(m, "No gcov support: %s", output);
29 static char *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 err = compile_object(m, fullfile, ccan_dir, "",
43 talloc_free(i->cov_compiled);
46 *modobjs = talloc_asprintf_append(*modobjs,
47 " %s", i->cov_compiled);
52 static char *obj_list(const struct manifest *m, const char *modobjs)
57 /* We expect to be linked with tap, unless that's us. */
58 if (!streq(m->basename, "tap"))
59 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
61 list = talloc_strdup(m, "");
63 /* Objects from any other C files. */
64 list_for_each(&m->other_test_c_files, i, list)
65 list = talloc_asprintf_append(list, " %s", i->compiled);
68 list = talloc_append_string(list, modobjs);
70 /* Other ccan modules (don't need coverage versions of those). */
71 list_for_each(&m->dep_dirs, i, list) {
73 list = talloc_asprintf_append(list, " %s", i->compiled);
79 static char *lib_list(const struct manifest *m)
82 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
83 char *ret = talloc_strdup(m, "");
85 for (i = 0; i < num; i++)
86 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
90 /* Grrr... gcov drops a turd in the current directory. */
91 void move_gcov_turd(const char *dir,
92 struct ccan_file *file, const char *extension)
94 char *base, *gcovfile, *gcovdest;
96 base = talloc_basename(file, file->name);
97 gcovfile = talloc_asprintf(file, "%s/%.*s%s",
98 dir, strlen(base)-2, base, extension);
99 gcovdest = talloc_asprintf(file, "%s/%.*s%s",
100 talloc_dirname(base, file->cov_compiled),
101 strlen(base)-2, base, extension);
102 if (!move_file(gcovfile, gcovdest))
103 err(1, "Could not move %s to %s", gcovfile, gcovdest);
107 static char *cov_compile(const void *ctx,
109 struct ccan_file *file,
116 file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
117 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
118 obj_list(m, modobjs),
120 lib_list(m), file->cov_compiled);
122 talloc_free(file->cov_compiled);
126 move_gcov_turd(getcwd(path, sizeof(path)), file, ".gcno");
130 struct compile_tests_result {
131 struct list_node list;
132 const char *filename;
133 const char *description;
137 static void *do_compile_coverage_tests(struct manifest *m,
139 unsigned int *timeleft)
141 struct list_head *list = talloc(m, struct list_head);
142 char *cmdout, *modobjs = NULL;
144 struct compile_tests_result *res;
146 list_head_init(list);
148 if (!list_empty(&m->api_tests)) {
149 cmdout = build_module_objs_with_coverage(m, keep, &modobjs);
151 res = talloc(list, struct compile_tests_result);
152 res->filename = "Module objects with coverage";
153 res->description = "failed to compile";
154 res->output = talloc_steal(res, cmdout);
155 list_add_tail(list, &res->list);
160 list_for_each(&m->run_tests, i, list) {
161 compile_tests.total_score++;
162 cmdout = cov_compile(m, m, i, NULL, keep);
164 res = talloc(list, struct compile_tests_result);
165 res->filename = i->name;
166 res->description = "failed to compile";
167 res->output = talloc_steal(res, cmdout);
168 list_add_tail(list, &res->list);
172 list_for_each(&m->api_tests, i, list) {
173 compile_tests.total_score++;
174 cmdout = cov_compile(m, m, i, modobjs, keep);
176 res = talloc(list, struct compile_tests_result);
177 res->filename = i->name;
178 res->description = "failed to compile";
179 res->output = talloc_steal(res, cmdout);
180 list_add_tail(list, &res->list);
184 if (list_empty(list)) {
192 static const char *describe_compile_coverage_tests(struct manifest *m,
195 struct list_head *list = check_result;
196 struct compile_tests_result *i;
199 descrip = talloc_strdup(list,
200 "Compilation of tests for coverage failed:\n");
202 list_for_each(list, i, list)
203 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
204 i->filename, i->description,
209 struct ccanlint compile_coverage_tests = {
210 .key = "compile-coverage-tests",
211 .name = "Module tests compile with profiling",
212 .check = do_compile_coverage_tests,
213 .describe = describe_compile_coverage_tests,
214 .can_run = can_run_coverage,
217 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);