]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_compile_coverage.c
ccan: Correct some poor conventions in _info includes
[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 void cov_compile(const void *ctx,
35                         unsigned int time_ms,
36                         struct manifest *m,
37                         struct ccan_file *file,
38                         bool link_with_module)
39 {
40         char *flags = tal_fmt(ctx, "%s %s", cflags, COVERAGE_CFLAGS);
41
42         file->compiled[COMPILE_COVERAGE] = temp_file(ctx, "", file->fullname);
43         compile_and_link_async(file, time_ms, file->fullname, ccan_dir,
44                                test_obj_list(m, link_with_module,
45                                              COMPILE_NORMAL,
46                                              COMPILE_COVERAGE),
47                                compiler, flags,
48                                test_lib_list(m, COMPILE_NORMAL),
49                                file->compiled[COMPILE_COVERAGE]);
50 }
51
52 /* FIXME: Coverage from testable examples as well. */
53 static void do_compile_coverage_tests(struct manifest *m,
54                                       unsigned int *timeleft,
55                                       struct score *score)
56 {
57         char *cmdout;
58         struct ccan_file *i;
59         struct list_head *h;
60         bool ok;
61         char *f = tal_fmt(score, "%s %s", cflags, COVERAGE_CFLAGS);
62
63         /* For API tests, we need coverage version of module. */
64         if (!list_empty(&m->api_tests)) {
65                 build_objects(m, score, f, COMPILE_COVERAGE);
66                 if (!score->pass) {
67                         score->error = tal_strdup(score,
68                                                      "Failed to compile module objects with coverage");
69                         return;
70                 }
71         }
72
73         foreach_ptr(h, &m->run_tests, &m->api_tests) {
74                 list_for_each(h, i, list) {
75                         cov_compile(m, *timeleft, m, i, h == &m->api_tests);
76                 }
77         }
78
79         while ((i = collect_command(&ok, &cmdout)) != NULL) {
80                 if (!ok) {
81                         score_file_error(score, i, 0,
82                                          "Failed to compile test with coverage:"
83                                          " %s", cmdout);
84                 }
85         }
86         if (!score->error) {
87                 score->pass = true;
88                 score->score = score->total;
89         }
90 }
91
92 struct ccanlint tests_compile_coverage = {
93         .key = "tests_compile_coverage",
94         .name = "Module tests compile with " COVERAGE_CFLAGS,
95         .check = do_compile_coverage_tests,
96         .can_run = can_run_coverage,
97         .needs = "tests_compile"
98 };
99
100 REGISTER_TEST(tests_compile_coverage);