]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/build-coverage.c
Merge branch 'ronnie'
[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 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                 err = compile_object(m, fullfile, ccan_dir, "",
41                                      i->cov_compiled);
42                 if (err) {
43                         score_file_error(score, i, 0, 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 static char *obj_list(const struct manifest *m, const char *modobjs)
55 {
56         char *list;
57         struct ccan_file *i;
58
59         /* We expect to be linked with tap, unless that's us. */
60         if (!streq(m->basename, "tap"))
61                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
62         else
63                 list = talloc_strdup(m, "");
64
65         /* Objects from any other C files. */
66         list_for_each(&m->other_test_c_files, i, list)
67                 list = talloc_asprintf_append(list, " %s", i->compiled);
68
69         if (modobjs)
70                 list = talloc_append_string(list, modobjs);
71
72         /* Other ccan modules (don't need coverage versions of those). */
73         list_for_each(&m->dep_dirs, i, list) {
74                 if (i->compiled)
75                         list = talloc_asprintf_append(list, " %s", i->compiled);
76         }
77
78         return list;
79 }
80
81 static char *lib_list(const struct manifest *m)
82 {
83         unsigned int i, num;
84         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
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 *errmsg;
99
100         file->cov_compiled = maybe_temp_file(ctx, "", keep, file->fullname);
101         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
102                                   obj_list(m, modobjs),
103                                   COVERAGE_CFLAGS,
104                                   lib_list(m), file->cov_compiled);
105         if (errmsg) {
106                 talloc_free(file->cov_compiled);
107                 file->cov_compiled = NULL;
108                 return errmsg;
109         }
110
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
123         if (!list_empty(&m->api_tests)
124             && !build_module_objs_with_coverage(m, keep, score, &modobjs)) {
125                 score->error = "Failed to compile module objects with coverage";
126                 return;
127         }
128
129         list_for_each(&m->run_tests, i, list) {
130                 cmdout = cov_compile(m, m, i, NULL, keep);
131                 if (cmdout) {
132                         score->error = "Failed to compile test with coverage";
133                         score_file_error(score, i, 0, cmdout);
134                 }
135         }
136
137         list_for_each(&m->api_tests, i, list) {
138                 cmdout = cov_compile(m, m, i, modobjs, keep);
139                 if (cmdout) {
140                         score->error = "Failed to compile test with coverage";
141                         score_file_error(score, i, 0, cmdout);
142                 }
143         }
144         if (!score->error) {
145                 score->pass = true;
146                 score->score = score->total;
147         }
148 }
149
150 struct ccanlint compile_coverage_tests = {
151         .key = "compile-coverage-tests",
152         .name = "Module tests compile with profiling",
153         .check = do_compile_coverage_tests,
154         .can_run = can_run_coverage,
155 };
156
157 REGISTER_TEST(compile_coverage_tests, &compile_tests, NULL);