]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_compile_coverage.c
ccanlint: Add more C++ keywords.
[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, compiler, cflags,
42                                     i->cov_compiled, &err)) {
43                         score_file_error(score, i, 0, "%s", 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         char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
97
98         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
99         if (!compile_and_link(ctx, file->fullname, ccan_dir,
100                               obj_list(m, modobjs),
101                               compiler, f,
102                               lib_list(m), file->cov_compiled, &output)) {
103                 talloc_free(file->cov_compiled);
104                 file->cov_compiled = NULL;
105                 return output;
106         }
107         talloc_free(output);
108         return NULL;
109 }
110
111 /* FIXME: Coverage from testable examples as well. */
112 static void do_compile_coverage_tests(struct manifest *m,
113                                       bool keep,
114                                       unsigned int *timeleft,
115                                       struct score *score)
116 {
117         char *cmdout, *modobjs = NULL;
118         struct ccan_file *i;
119         struct list_head *h;
120
121         if (!list_empty(&m->api_tests)
122             && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
123                 score->error = talloc_strdup(score,
124                              "Failed to compile module objects with coverage");
125                 return;
126         }
127
128         foreach_ptr(h, &m->run_tests, &m->api_tests) {
129                 list_for_each(h, i, list) {
130                         cmdout = cov_compile(m, m, i,
131                                              h == &m->api_tests
132                                              ? modobjs : NULL,
133                                              keep);
134                         if (cmdout) {
135                                 score_file_error(score, i, 0,
136                                   "Failed to compile test with coverage: %s",
137                                   cmdout);
138                         }
139                 }
140         }
141         if (!score->error) {
142                 score->pass = true;
143                 score->score = score->total;
144         }
145 }
146
147 struct ccanlint tests_compile_coverage = {
148         .key = "tests_compile_coverage",
149         .name = "Module tests compile with " COVERAGE_CFLAGS,
150         .check = do_compile_coverage_tests,
151         .can_run = can_run_coverage,
152         .needs = "tests_compile"
153 };
154
155 REGISTER_TEST(tests_compile_coverage);