ccanlint: Add cflags support to _info
[ccan] / tools / ccanlint / tests / tests_compile_coverage.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/foreach/foreach.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.h"
17 #include "tests_compile.h"
18
19 /* Note: we already test safe_mode in run_tests.c */
20 static const char *can_run_coverage(struct manifest *m)
21 {
22 #ifdef __GNUC__
23         unsigned int timeleft = default_timeout_ms;
24         char *output;
25
26         if (!run_command(m, &timeleft, &output, "gcov -h"))
27                 return tal_fmt(m, "No gcov support: %s", output);
28         return NULL;
29 #else
30         return "No coverage support for this compiler";
31 #endif
32 }
33
34 static char *cflags_list(const struct manifest *m)
35 {
36         unsigned int i;
37         char *ret = tal_strdup(m, cflags);
38
39         char **flags = get_cflags(m, m->dir, get_or_compile_info);
40         for (i = 0; flags[i]; i++)
41                 tal_append_fmt(&ret, " %s", flags[i]);
42         return ret;
43 }
44
45 static char *cflags_list_append(const struct manifest *m, char *iflags)
46 {
47         unsigned int i;
48
49         char **flags = get_cflags(m, m->dir, get_or_compile_info);
50         for (i = 0; flags[i]; i++)
51                 tal_append_fmt(&iflags, " %s", flags[i]);
52         return iflags;
53 }
54
55 static void cov_compile(const void *ctx,
56                         unsigned int time_ms,
57                         struct manifest *m,
58                         struct ccan_file *file,
59                         bool link_with_module)
60 {
61         char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
62         flags = cflags_list_append(m, flags);
63
64         file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
65         compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
66                                test_obj_list(m, link_with_module,
67                                              COMPILE_NORMAL,
68                                              COMPILE_COVERAGE),
69                                compiler, flags,
70                                test_lib_list(m, COMPILE_NORMAL),
71                                file->compiled[COMPILE_COVERAGE]);
72 }
73
74 /* FIXME: Coverage from testable examples as well. */
75 static void do_compile_coverage_tests(struct manifest *m,
76                                       unsigned int *timeleft,
77                                       struct score *score)
78 {
79         char *cmdout;
80         struct ccan_file *i;
81         struct list_head *h;
82         bool ok;
83         char *f = cflags_list(m);
84         tal_append_fmt(&f, " %s", COVERAGE_CFLAGS);
85
86         /* For API tests, we need coverage version of module. */
87         if (!list_empty(&m->api_tests)) {
88                 build_objects(m, score, f, COMPILE_COVERAGE);
89                 if (!score->pass) {
90                         score->error = tal_strdup(score,
91                                                      "Failed to compile module objects with coverage");
92                         return;
93                 }
94         }
95
96         foreach_ptr(h, &m->run_tests, &m->api_tests) {
97                 list_for_each(h, i, list) {
98                         cov_compile(m, *timeleft, m, i, h == &m->api_tests);
99                 }
100         }
101
102         while ((i = collect_command(&ok, &cmdout)) != NULL) {
103                 if (!ok) {
104                         score_file_error(score, i, 0,
105                                          "Failed to compile test with coverage:"
106                                          " %s", cmdout);
107                 }
108         }
109         if (!score->error) {
110                 score->pass = true;
111                 score->score = score->total;
112         }
113 }
114
115 struct ccanlint tests_compile_coverage = {
116         .key = "tests_compile_coverage",
117         .name = "Module tests compile with " COVERAGE_CFLAGS,
118         .check = do_compile_coverage_tests,
119         .can_run = can_run_coverage,
120         .needs = "tests_compile"
121 };
122
123 REGISTER_TEST(tests_compile_coverage);