ccanlint: keep separate array of compiled versions.
[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",
64                                               i->compiled[COMPILE_NORMAL]);
65
66         if (modobjs)
67                 list = talloc_append_string(list, modobjs);
68
69         /* Other ccan modules (don't need coverage versions of those). */
70         list_for_each(&m->deps, subm, list) {
71                 if (subm->compiled[COMPILE_NORMAL])
72                         list = talloc_asprintf_append(list, " %s",
73                                                       subm->compiled
74                                                       [COMPILE_NORMAL]);
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,
84                                &m->info_file->compiled[COMPILE_NORMAL]);
85         char *ret = talloc_strdup(m, "");
86
87         for (i = 0; i < num; i++)
88                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
89         return ret;
90 }
91
92 static char *cov_compile(const void *ctx,
93                          struct manifest *m,
94                          struct ccan_file *file,
95                          const char *modobjs,
96                          bool keep)
97 {
98         char *output;
99         char *f = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
100
101         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
102         if (!compile_and_link(ctx, file->fullname, ccan_dir,
103                               obj_list(m, modobjs),
104                               compiler, f,
105                               lib_list(m), file->cov_compiled, &output)) {
106                 talloc_free(file->cov_compiled);
107                 file->cov_compiled = NULL;
108                 return output;
109         }
110         talloc_free(output);
111         return NULL;
112 }
113
114 /* FIXME: Coverage from testable examples as well. */
115 static void do_compile_coverage_tests(struct manifest *m,
116                                       bool keep,
117                                       unsigned int *timeleft,
118                                       struct score *score)
119 {
120         char *cmdout, *modobjs = NULL;
121         struct ccan_file *i;
122         struct list_head *h;
123
124         if (!list_empty(&m->api_tests)
125             && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
126                 score->error = talloc_strdup(score,
127                              "Failed to compile module objects with coverage");
128                 return;
129         }
130
131         foreach_ptr(h, &m->run_tests, &m->api_tests) {
132                 list_for_each(h, i, list) {
133                         cmdout = cov_compile(m, m, i,
134                                              h == &m->api_tests
135                                              ? modobjs : NULL,
136                                              keep);
137                         if (cmdout) {
138                                 score_file_error(score, i, 0,
139                                   "Failed to compile test with coverage: %s",
140                                   cmdout);
141                         }
142                 }
143         }
144         if (!score->error) {
145                 score->pass = true;
146                 score->score = score->total;
147         }
148 }
149
150 struct ccanlint tests_compile_coverage = {
151         .key = "tests_compile_coverage",
152         .name = "Module tests compile with " COVERAGE_CFLAGS,
153         .check = do_compile_coverage_tests,
154         .can_run = can_run_coverage,
155         .needs = "tests_compile"
156 };
157
158 REGISTER_TEST(tests_compile_coverage);