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