ccanlint: add coverage variant of files.
[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 #include "../compulsory_tests/build.h"
18 #include "tests_compile.h"
19
20 /* Note: we already test safe_mode in run_tests.c */
21 static const char *can_run_coverage(struct manifest *m)
22 {
23         unsigned int timeleft = default_timeout_ms;
24         char *output;
25
26         if (!run_command(m, &timeleft, &output, "gcov -h"))
27                 return talloc_asprintf(m, "No gcov support: %s", output);
28         return NULL;
29 }
30
31 static char *cov_compile(const void *ctx,
32                          struct manifest *m,
33                          struct ccan_file *file,
34                          bool link_with_module,
35                          bool keep)
36 {
37         char *output;
38         char *flags = talloc_asprintf(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
39
40         file->compiled[COMPILE_COVERAGE]
41                 = maybe_temp_file(ctx, "", keep, file->fullname);
42         if (!compile_and_link(ctx, file->fullname, ccan_dir,
43                               test_obj_list(m, link_with_module,
44                                             COMPILE_NORMAL,
45                                             COMPILE_COVERAGE),
46                               compiler, flags,
47                               lib_list(m, COMPILE_NORMAL),
48                               file->compiled[COMPILE_COVERAGE], &output)) {
49                 talloc_free(file->compiled[COMPILE_COVERAGE]);
50                 file->compiled[COMPILE_COVERAGE] = NULL;
51                 return output;
52         }
53         talloc_free(output);
54         return NULL;
55 }
56
57 /* FIXME: Coverage from testable examples as well. */
58 static void do_compile_coverage_tests(struct manifest *m,
59                                       bool keep,
60                                       unsigned int *timeleft,
61                                       struct score *score)
62 {
63         char *cmdout;
64         struct ccan_file *i;
65         struct list_head *h;
66         char *f = talloc_asprintf(score, "%s %s", cflags, COVERAGE_CFLAGS);
67
68         /* For API tests, we need coverage version of module. */
69         if (!list_empty(&m->api_tests)) {
70                 build_objects(m, keep, score, f, COMPILE_COVERAGE);
71                 if (!score->pass) {
72                         score->error = talloc_strdup(score,
73                                                      "Failed to compile module objects with coverage");
74                         return;
75                 }
76         }
77
78         foreach_ptr(h, &m->run_tests, &m->api_tests) {
79                 list_for_each(h, i, list) {
80                         cmdout = cov_compile(m, m, i,
81                                              h == &m->api_tests,
82                                              keep);
83                         if (cmdout) {
84                                 score_file_error(score, i, 0,
85                                   "Failed to compile test with coverage: %s",
86                                   cmdout);
87                         }
88                 }
89         }
90         if (!score->error) {
91                 score->pass = true;
92                 score->score = score->total;
93         }
94 }
95
96 struct ccanlint tests_compile_coverage = {
97         .key = "tests_compile_coverage",
98         .name = "Module tests compile with " COVERAGE_CFLAGS,
99         .check = do_compile_coverage_tests,
100         .can_run = can_run_coverage,
101         .needs = "tests_compile"
102 };
103
104 REGISTER_TEST(tests_compile_coverage);