]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/build-coverage.c
ccanlint: make compile commands return output.
[ccan] / tools / ccanlint / tests / build-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 <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 /* Note: we already test safe_mode in run_tests.c */
18 static const char *can_run_coverage(struct manifest *m)
19 {
20         unsigned int timeleft = default_timeout_ms;
21         char *output;
22
23         if (!run_command(m, &timeleft, &output, "gcov -h"))
24                 return talloc_asprintf(m, "No gcov support: %s", output);
25         return NULL;
26 }
27
28 static bool build_module_objs_with_coverage(struct manifest *m, bool keep,
29                                             struct score *score,
30                                             char **modobjs)
31 {
32         struct ccan_file *i;
33
34         *modobjs = talloc_strdup(m, "");
35         list_for_each(&m->c_files, i, list) {
36                 char *err;
37                 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
38
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;
45                         return false;
46                 }
47                 *modobjs = talloc_asprintf_append(*modobjs,
48                                                   " %s", i->cov_compiled);
49         }
50         return true;
51 }
52
53 static char *obj_list(const struct manifest *m, const char *modobjs)
54 {
55         char *list;
56         struct ccan_file *i;
57
58         /* We expect to be linked with tap, unless that's us. */
59         if (!streq(m->basename, "tap"))
60                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
61         else
62                 list = talloc_strdup(m, "");
63
64         /* Objects from any other C files. */
65         list_for_each(&m->other_test_c_files, i, list)
66                 list = talloc_asprintf_append(list, " %s", i->compiled);
67
68         if (modobjs)
69                 list = talloc_append_string(list, modobjs);
70
71         /* Other ccan modules (don't need coverage versions of those). */
72         list_for_each(&m->dep_dirs, i, list) {
73                 if (i->compiled)
74                         list = talloc_asprintf_append(list, " %s", i->compiled);
75         }
76
77         return list;
78 }
79
80 static char *lib_list(const struct manifest *m)
81 {
82         unsigned int i, num;
83         char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
84         char *ret = talloc_strdup(m, "");
85
86         for (i = 0; i < num; i++)
87                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
88         return ret;
89 }
90
91 static char *cov_compile(const void *ctx,
92                          struct manifest *m,
93                          struct ccan_file *file,
94                          const char *modobjs,
95                          bool keep)
96 {
97         char *output;
98
99         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
100         if (!compile_and_link(ctx, file->fullname, ccan_dir,
101                               obj_list(m, modobjs),
102                               COVERAGE_CFLAGS,
103                               lib_list(m), file->cov_compiled, &output)) {
104                 talloc_free(file->cov_compiled);
105                 file->cov_compiled = NULL;
106                 return output;
107         }
108         talloc_free(output);
109         return NULL;
110 }
111
112 /* FIXME: Coverage from testable examples as well. */
113 static void do_compile_coverage_tests(struct manifest *m,
114                                       bool keep,
115                                       unsigned int *timeleft,
116                                       struct score *score)
117 {
118         char *cmdout, *modobjs = NULL;
119         struct ccan_file *i;
120
121         if (!list_empty(&m->api_tests)
122             && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
123                 score->error = "Failed to compile module objects with coverage";
124                 return;
125         }
126
127         list_for_each(&m->run_tests, i, list) {
128                 cmdout = cov_compile(m, m, i, NULL, keep);
129                 if (cmdout) {
130                         score->error = "Failed to compile test with coverage";
131                         score_file_error(score, i, 0, cmdout);
132                 }
133         }
134
135         list_for_each(&m->api_tests, i, list) {
136                 cmdout = cov_compile(m, m, i, modobjs, keep);
137                 if (cmdout) {
138                         score->error = "Failed to compile test with coverage";
139                         score_file_error(score, i, 0, cmdout);
140                 }
141         }
142         if (!score->error) {
143                 score->pass = true;
144                 score->score = score->total;
145         }
146 }
147
148 struct ccanlint compile_coverage_tests = {
149         .key = "compile-coverage-tests",
150         .name = "Module tests compile with profiling",
151         .check = do_compile_coverage_tests,
152         .can_run = can_run_coverage,
153 };
154
155 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);