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