ccanlint: print coverage amount when -vv
[ccan] / tools / ccanlint / tests / tests_compile_coverage.c
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>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 /* Note: we already test safe_mode in run_tests.c */
19 static const char *can_run_coverage(struct manifest *m)
20 {
21         unsigned int timeleft = default_timeout_ms;
22         char *output;
23
24         if (!run_command(m, &timeleft, &output, "gcov -h"))
25                 return talloc_asprintf(m, "No gcov support: %s", output);
26         return NULL;
27 }
28
29 static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
30                                             struct score *score,
31                                             char **modobjs)
32 {
33         struct ccan_file *i;
34
35         *modobjs = talloc_strdup(m, "");
36         list_for_each(&m->c_files, i, list) {
37                 char *err;
38                 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
39
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;
46                         return false;
47                 }
48                 *modobjs = talloc_asprintf_append(*modobjs,
49                                                   " %s", i->cov_compiled);
50         }
51         return true;
52 }
53
54 /* FIXME: Merge this into one place. */
55 static char *obj_list(const struct manifest *m, const char *modobjs)
56 {
57         char *list = talloc_strdup(m, "");
58         struct ccan_file *i;
59         struct manifest *subm;
60
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);
64
65         if (modobjs)
66                 list = talloc_append_string(list, modobjs);
67
68         /* Other ccan modules (don't need coverage versions of those). */
69         list_for_each(&m->deps, subm, list) {
70                 if (subm->compiled)
71                         list = talloc_asprintf_append(list, " %s",
72                                                       subm->compiled);
73         }
74
75         return list;
76 }
77
78 static char *lib_list(const struct manifest *m)
79 {
80         unsigned int i, num;
81         char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
82         char *ret = talloc_strdup(m, "");
83
84         for (i = 0; i < num; i++)
85                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
86         return ret;
87 }
88
89 static char *cov_compile(const void *ctx,
90                          struct manifest *m,
91                          struct ccan_file *file,
92                          const char *modobjs,
93                          bool keep)
94 {
95         char *output;
96
97         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
98         if (!compile_and_link(ctx, file->fullname, ccan_dir,
99                               obj_list(m, modobjs),
100                               COVERAGE_CFLAGS,
101                               lib_list(m), file->cov_compiled, &output)) {
102                 talloc_free(file->cov_compiled);
103                 file->cov_compiled = NULL;
104                 return output;
105         }
106         talloc_free(output);
107         return NULL;
108 }
109
110 /* FIXME: Coverage from testable examples as well. */
111 static void do_compile_coverage_tests(struct manifest *m,
112                                       bool keep,
113                                       unsigned int *timeleft,
114                                       struct score *score)
115 {
116         char *cmdout, *modobjs = NULL;
117         struct ccan_file *i;
118         struct list_head *h;
119
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");
124                 return;
125         }
126
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);
130                         if (cmdout) {
131                                 score_file_error(score, i, 0,
132                                   "Failed to compile test with coverage: %s",
133                                   cmdout);
134                         }
135                 }
136         }
137         if (!score->error) {
138                 score->pass = true;
139                 score->score = score->total;
140         }
141 }
142
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"
149 };
150
151 REGISTER_TEST(tests_compile_coverage);