]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/compile_tests.c
ccanlint: remove -Werror, use output of compile command to detect warnings.
[ccan] / tools / ccanlint / tests / compile_tests.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
18 static const char *can_build(struct manifest *m)
19 {
20         if (safe_mode)
21                 return "Safe mode enabled";
22         return NULL;
23 }
24
25 static char *obj_list(const struct manifest *m, bool link_with_module)
26 {
27         char *list;
28         struct ccan_file *i;
29
30         /* We expect to be linked with tap, unless that's us. */
31         if (!streq(m->basename, "tap"))
32                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
33         else
34                 list = talloc_strdup(m, "");
35
36         /* Objects from any other C files. */
37         list_for_each(&m->other_test_c_files, i, list)
38                 list = talloc_asprintf_append(list, " %s", i->compiled);
39
40         /* Our own object files. */
41         if (link_with_module)
42                 list_for_each(&m->c_files, i, list)
43                         list = talloc_asprintf_append(list, " %s", i->compiled);
44
45         /* Other ccan modules. */
46         list_for_each(&m->dep_dirs, i, list) {
47                 if (i->compiled)
48                         list = talloc_asprintf_append(list, " %s", i->compiled);
49         }
50
51         return list;
52 }
53
54 static char *lib_list(const struct manifest *m)
55 {
56         unsigned int i, num;
57         char **libs = get_libs(m, m->dir, &num, &m->info_file->compiled);
58         char *ret = talloc_strdup(m, "");
59
60         for (i = 0; i < num; i++)
61                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
62         return ret;
63 }
64
65 static bool compile(const void *ctx,
66                     struct manifest *m,
67                     struct ccan_file *file,
68                     bool fail,
69                     bool link_with_module,
70                     bool keep, char **output)
71 {
72         file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
73         if (!compile_and_link(ctx, file->fullname, ccan_dir,
74                               obj_list(m, link_with_module),
75                               fail ? "-DFAIL" : "",
76                               lib_list(m), file->compiled, output)) {
77                 talloc_free(file->compiled);
78                 return false;
79         }
80         return true;
81 }
82
83 static void do_compile_tests(struct manifest *m,
84                              bool keep,
85                              unsigned int *timeleft, struct score *score)
86 {
87         char *cmdout;
88         struct ccan_file *i;
89         struct list_head *list;
90         bool errors = false, warnings = false;
91
92         foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
93                 list_for_each(list, i, list) {
94                         if (!compile(score, m, i, false, list == &m->api_tests,
95                                      keep, &cmdout)) {
96                                 score->error = "Failed to compile tests";
97                                 score_file_error(score, i, 0, cmdout);
98                                 errors = true;
99                         } else if (!streq(cmdout, "")) {
100                                 score->error = "Test compiled with warnings";
101                                 score_file_error(score, i, 0, cmdout);
102                                 warnings = true;
103                         }
104                 }
105         }
106
107         /* The compile fail tests are a bit weird, handle them separately */
108         if (errors)
109                 return;
110
111         /* For historical reasons, "fail" often means "gives warnings" */
112         list_for_each(&m->compile_fail_tests, i, list) {
113                 if (!compile(score, m, i, false, false, false, &cmdout)) {
114                         score->error = "Failed to compile without -DFAIL";
115                         score_file_error(score, i, 0, cmdout);
116                         return;
117                 }
118                 if (!streq(cmdout, "")) {
119                         score->error = "Compile with warnigns without -DFAIL";
120                         score_file_error(score, i, 0, cmdout);
121                         return;
122                 }
123                 if (compile(score, m, i, true, false, false, &cmdout)
124                     && streq(cmdout, "")) {
125                         score->error = "Compiled successfully with -DFAIL?";
126                         score_file_error(score, i, 0, NULL);
127                         return;
128                 }
129         }
130
131         score->pass = true;
132         score->total = 2;
133         score->score = 1 + !warnings;
134 }
135
136 struct ccanlint compile_tests = {
137         .key = "compile-tests",
138         .name = "Module tests compile",
139         .check = do_compile_tests,
140         .can_run = can_build,
141 };
142
143 REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);