]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/build-coverage.c
f1010204d0447221244c4b6466b3568a728f2187
[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 #include "build-coverage.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 = run_command(m, &timeleft, "gcov -h");
23
24         if (output)
25                 return talloc_asprintf(m, "No gcov support: %s", output);
26         return NULL;
27 }
28
29 static char *build_module_objs_with_coverage(struct manifest *m, bool keep,
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                 err = compile_object(m, fullfile, ccan_dir, "",
41                                      i->cov_compiled);
42                 if (err) {
43                         talloc_free(i->cov_compiled);
44                         return err;
45                 }
46                 *modobjs = talloc_asprintf_append(*modobjs,
47                                                   " %s", i->cov_compiled);
48         }
49         return NULL;
50 }
51
52 static char *obj_list(const struct manifest *m, const char *modobjs)
53 {
54         char *list;
55         struct ccan_file *i;
56
57         /* We expect to be linked with tap, unless that's us. */
58         if (!streq(m->basename, "tap"))
59                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
60         else
61                 list = talloc_strdup(m, "");
62
63         /* Objects from any other C files. */
64         list_for_each(&m->other_test_c_files, i, list)
65                 list = talloc_asprintf_append(list, " %s", i->compiled);
66
67         if (modobjs)
68                 list = talloc_append_string(list, modobjs);
69
70         /* Other ccan modules (don't need coverage versions of those). */
71         list_for_each(&m->dep_dirs, i, list) {
72                 if (i->compiled)
73                         list = talloc_asprintf_append(list, " %s", i->compiled);
74         }
75
76         return list;
77 }
78
79 static char *lib_list(const struct manifest *m)
80 {
81         unsigned int i, num;
82         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
83         char *ret = talloc_strdup(m, "");
84
85         for (i = 0; i < num; i++)
86                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
87         return ret;
88 }
89
90 /* Grrr... gcov drops a turd in the current directory. */
91 void move_gcov_turd(const char *dir,
92                     struct ccan_file *file, const char *extension)
93 {
94         char *base, *gcovfile, *gcovdest;
95
96         base = talloc_basename(file, file->name);
97         gcovfile = talloc_asprintf(file, "%s/%.*s%s",
98                                    dir, strlen(base)-2, base, extension);
99         gcovdest = talloc_asprintf(file, "%s/%.*s%s",
100                                    talloc_dirname(base, file->cov_compiled),
101                                    strlen(base)-2, base, extension);
102         if (!move_file(gcovfile, gcovdest))
103                 err(1, "Could not move %s to %s", gcovfile, gcovdest);
104         talloc_free(base);
105 }
106
107 static char *cov_compile(const void *ctx,
108                          struct manifest *m,
109                          struct ccan_file *file,
110                          const char *modobjs,
111                          bool keep)
112 {
113         char *errmsg;
114         char path[PATH_MAX];
115
116         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
117         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
118                                   obj_list(m, modobjs),
119                                   COVERAGE_CFLAGS,
120                                   lib_list(m), file->cov_compiled);
121         if (errmsg) {
122                 talloc_free(file->cov_compiled);
123                 return errmsg;
124         }
125
126         move_gcov_turd(getcwd(path, sizeof(path)), file, ".gcno");
127         return NULL;
128 }
129
130 struct compile_tests_result {
131         struct list_node list;
132         const char *filename;
133         const char *description;
134         const char *output;
135 };
136
137 static void *do_compile_coverage_tests(struct manifest *m,
138                                        bool keep,
139                                        unsigned int *timeleft)
140 {
141         struct list_head *list = talloc(m, struct list_head);
142         char *cmdout, *modobjs = NULL;
143         struct ccan_file *i;
144         struct compile_tests_result *res;
145
146         list_head_init(list);
147
148         if (!list_empty(&m->api_tests)) {
149                 cmdout = build_module_objs_with_coverage(m, keep, &modobjs);
150                 if (cmdout) {
151                         res = talloc(list, struct compile_tests_result);
152                         res->filename = "Module objects with coverage";
153                         res->description = "failed to compile";
154                         res->output = talloc_steal(res, cmdout);
155                         list_add_tail(list, &res->list);
156                         return list;
157                 }
158         }
159
160         list_for_each(&m->run_tests, i, list) {
161                 compile_tests.total_score++;
162                 cmdout = cov_compile(m, m, i, NULL, keep);
163                 if (cmdout) {
164                         res = talloc(list, struct compile_tests_result);
165                         res->filename = i->name;
166                         res->description = "failed to compile";
167                         res->output = talloc_steal(res, cmdout);
168                         list_add_tail(list, &res->list);
169                 }
170         }
171
172         list_for_each(&m->api_tests, i, list) {
173                 compile_tests.total_score++;
174                 cmdout = cov_compile(m, m, i, modobjs, keep);
175                 if (cmdout) {
176                         res = talloc(list, struct compile_tests_result);
177                         res->filename = i->name;
178                         res->description = "failed to compile";
179                         res->output = talloc_steal(res, cmdout);
180                         list_add_tail(list, &res->list);
181                 }
182         }
183
184         if (list_empty(list)) {
185                 talloc_free(list);
186                 list = NULL;
187         }
188
189         return list;
190 }
191
192 static const char *describe_compile_coverage_tests(struct manifest *m,
193                                                    void *check_result)
194 {
195         struct list_head *list = check_result;
196         struct compile_tests_result *i;
197         char *descrip;
198
199         descrip = talloc_strdup(list,
200                                 "Compilation of tests for coverage failed:\n");
201
202         list_for_each(list, i, list)
203                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
204                                                  i->filename, i->description,
205                                                  i->output);
206         return descrip;
207 }
208
209 struct ccanlint compile_coverage_tests = {
210         .key = "compile-coverage-tests",
211         .name = "Module tests compile with profiling",
212         .check = do_compile_coverage_tests,
213         .describe = describe_compile_coverage_tests,
214         .can_run = can_run_coverage,
215 };
216
217 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);