]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/build-coverage.c
ccanlint: chdir to temporary dir so gcov files land there.
[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 = run_command(m, &timeleft, "gcov -h");
22
23         if (output)
24                 return talloc_asprintf(m, "No gcov support: %s", output);
25         return NULL;
26 }
27
28 static char *build_module_objs_with_coverage(struct manifest *m, bool keep,
29                                              char **modobjs)
30 {
31         struct ccan_file *i;
32
33         *modobjs = talloc_strdup(m, "");
34         list_for_each(&m->c_files, i, list) {
35                 char *err;
36                 char *fullfile = talloc_asprintf(m, "%s/%s", m->dir, i->name);
37
38                 i->cov_compiled = maybe_temp_file(m, "", keep, fullfile);
39                 err = compile_object(m, fullfile, ccan_dir, "",
40                                      i->cov_compiled);
41                 if (err) {
42                         talloc_free(i->cov_compiled);
43                         return err;
44                 }
45                 *modobjs = talloc_asprintf_append(*modobjs,
46                                                   " %s", i->cov_compiled);
47         }
48         return NULL;
49 }
50
51 static char *obj_list(const struct manifest *m, const char *modobjs)
52 {
53         char *list;
54         struct ccan_file *i;
55
56         /* We expect to be linked with tap, unless that's us. */
57         if (!streq(m->basename, "tap"))
58                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
59         else
60                 list = talloc_strdup(m, "");
61
62         /* Objects from any other C files. */
63         list_for_each(&m->other_test_c_files, i, list)
64                 list = talloc_asprintf_append(list, " %s", i->compiled);
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->dep_dirs, i, list) {
71                 if (i->compiled)
72                         list = talloc_asprintf_append(list, " %s", i->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, ".", &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 *errmsg;
96
97         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
98         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
99                                   obj_list(m, modobjs),
100                                   COVERAGE_CFLAGS,
101                                   lib_list(m), file->cov_compiled);
102         if (errmsg) {
103                 talloc_free(file->cov_compiled);
104                 return errmsg;
105         }
106
107         return NULL;
108 }
109
110 struct compile_tests_result {
111         struct list_node list;
112         const char *filename;
113         const char *description;
114         const char *output;
115 };
116
117 static void *do_compile_coverage_tests(struct manifest *m,
118                                        bool keep,
119                                        unsigned int *timeleft)
120 {
121         struct list_head *list = talloc(m, struct list_head);
122         char *cmdout, *modobjs = NULL;
123         struct ccan_file *i;
124         struct compile_tests_result *res;
125
126         list_head_init(list);
127
128         if (!list_empty(&m->api_tests)) {
129                 cmdout = build_module_objs_with_coverage(m, keep, &modobjs);
130                 if (cmdout) {
131                         res = talloc(list, struct compile_tests_result);
132                         res->filename = "Module objects with coverage";
133                         res->description = "failed to compile";
134                         res->output = talloc_steal(res, cmdout);
135                         list_add_tail(list, &res->list);
136                         return list;
137                 }
138         }
139
140         list_for_each(&m->run_tests, i, list) {
141                 compile_tests.total_score++;
142                 cmdout = cov_compile(m, m, i, NULL, keep);
143                 if (cmdout) {
144                         res = talloc(list, struct compile_tests_result);
145                         res->filename = i->name;
146                         res->description = "failed to compile";
147                         res->output = talloc_steal(res, cmdout);
148                         list_add_tail(list, &res->list);
149                 }
150         }
151
152         list_for_each(&m->api_tests, i, list) {
153                 compile_tests.total_score++;
154                 cmdout = cov_compile(m, m, i, modobjs, keep);
155                 if (cmdout) {
156                         res = talloc(list, struct compile_tests_result);
157                         res->filename = i->name;
158                         res->description = "failed to compile";
159                         res->output = talloc_steal(res, cmdout);
160                         list_add_tail(list, &res->list);
161                 }
162         }
163
164         if (list_empty(list)) {
165                 talloc_free(list);
166                 list = NULL;
167         }
168
169         return list;
170 }
171
172 static const char *describe_compile_coverage_tests(struct manifest *m,
173                                                    void *check_result)
174 {
175         struct list_head *list = check_result;
176         struct compile_tests_result *i;
177         char *descrip;
178
179         descrip = talloc_strdup(list,
180                                 "Compilation of tests for coverage failed:\n");
181
182         list_for_each(list, i, list)
183                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
184                                                  i->filename, i->description,
185                                                  i->output);
186         return descrip;
187 }
188
189 struct ccanlint compile_coverage_tests = {
190         .key = "compile-coverage-tests",
191         .name = "Module tests compile with profiling",
192         .check = do_compile_coverage_tests,
193         .describe = describe_compile_coverage_tests,
194         .can_run = can_run_coverage,
195 };
196
197 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);