ccanlint: make tests non-compulsory, always print score.
[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 <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
17 static const char *can_build(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 static char *obj_list(const struct manifest *m, bool link_with_module)
25 {
26         char *list;
27         struct ccan_file *i;
28
29         /* We expect to be linked with tap, unless that's us. */
30         if (!streq(m->basename, "tap"))
31                 list = talloc_asprintf(m, "%s/ccan/tap.o", ccan_dir);
32         else
33                 list = talloc_strdup(m, "");
34
35         /* Objects from any other C files. */
36         list_for_each(&m->other_test_c_files, i, list)
37                 list = talloc_asprintf_append(list, " %s", i->compiled);
38
39         /* Our own object files. */
40         if (link_with_module)
41                 list_for_each(&m->c_files, i, list)
42                         list = talloc_asprintf_append(list, " %s", i->compiled);
43
44         /* Other ccan modules. */
45         list_for_each(&m->dep_dirs, i, list) {
46                 if (i->compiled)
47                         list = talloc_asprintf_append(list, " %s", i->compiled);
48         }
49
50         return list;
51 }
52
53 static char *lib_list(const struct manifest *m)
54 {
55         unsigned int i, num;
56         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
57         char *ret = talloc_strdup(m, "");
58
59         for (i = 0; i < num; i++)
60                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
61         return ret;
62 }
63
64 static char *compile(const void *ctx,
65                      struct manifest *m,
66                      struct ccan_file *file,
67                      bool fail,
68                      bool link_with_module,
69                      bool keep)
70 {
71         char *errmsg;
72
73         file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
74         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
75                                   obj_list(m, link_with_module),
76                                   fail ? "-DFAIL" : "",
77                                   lib_list(m), file->compiled);
78         if (errmsg) {
79                 talloc_free(file->compiled);
80                 return errmsg;
81         }
82         return NULL;
83 }
84
85 struct compile_tests_result {
86         struct list_node list;
87         const char *filename;
88         const char *description;
89         const char *output;
90 };
91
92 static void *do_compile_tests(struct manifest *m,
93                               bool keep,
94                               unsigned int *timeleft)
95 {
96         struct list_head *list = talloc(m, struct list_head);
97         char *cmdout;
98         struct ccan_file *i;
99         struct compile_tests_result *res;
100
101         list_head_init(list);
102
103         compile_tests.total_score = 0;
104         list_for_each(&m->compile_ok_tests, i, list) {
105                 compile_tests.total_score++;
106                 cmdout = compile(list, m, i, false, false, keep);
107                 if (cmdout) {
108                         res = talloc(list, struct compile_tests_result);
109                         res->filename = i->name;
110                         res->description = "failed to compile";
111                         res->output = talloc_steal(res, cmdout);
112                         list_add_tail(list, &res->list);
113                 }
114         }
115
116         list_for_each(&m->run_tests, i, list) {
117                 compile_tests.total_score++;
118                 cmdout = compile(m, m, i, false, false, keep);
119                 if (cmdout) {
120                         res = talloc(list, struct compile_tests_result);
121                         res->filename = i->name;
122                         res->description = "failed to compile";
123                         res->output = talloc_steal(res, cmdout);
124                         list_add_tail(list, &res->list);
125                 }
126         }
127
128         list_for_each(&m->api_tests, i, list) {
129                 compile_tests.total_score++;
130                 cmdout = compile(m, m, i, false, true, keep);
131                 if (cmdout) {
132                         res = talloc(list, struct compile_tests_result);
133                         res->filename = i->name;
134                         res->description = "failed to compile";
135                         res->output = talloc_steal(res, cmdout);
136                         list_add_tail(list, &res->list);
137                 }
138         }
139
140         list_for_each(&m->compile_fail_tests, i, list) {
141                 compile_tests.total_score++;
142                 cmdout = compile(list, m, i, false, false, false);
143                 if (cmdout) {
144                         res = talloc(list, struct compile_tests_result);
145                         res->filename = i->name;
146                         res->description = "failed to compile without -DFAIL";
147                         res->output = talloc_steal(res, cmdout);
148                         list_add_tail(list, &res->list);
149                 } else {
150                         cmdout = compile(list, m, i, true, false, false);
151                         if (!cmdout) {
152                                 res = talloc(list, struct compile_tests_result);
153                                 res->filename = i->name;
154                                 res->description = "compiled successfully"
155                                         " with -DFAIL";
156                                 res->output = "";
157                                 list_add_tail(list, &res->list);
158                         }
159                 }
160         }
161
162         if (list_empty(list)) {
163                 talloc_free(list);
164                 list = NULL;
165         }
166
167         return list;
168 }
169
170 static unsigned int score_compile_tests(struct manifest *m,
171                                         void *check_result)
172 {
173         struct list_head *list = check_result;
174         struct compile_tests_result *i;
175         unsigned int score = compile_tests.total_score;
176
177         list_for_each(list, i, list)
178                 score--;
179         return score;
180 }
181
182 static const char *describe_compile_tests(struct manifest *m,
183                                           void *check_result)
184 {
185         struct list_head *list = check_result;
186         struct compile_tests_result *i;
187         char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
188
189         list_for_each(list, i, list)
190                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
191                                                  i->filename, i->description,
192                                                  i->output);
193         return descrip;
194 }
195
196 struct ccanlint compile_tests = {
197         .key = "compile-tests",
198         .name = "Module tests compile",
199         .score = score_compile_tests,
200         .total_score = 1,
201         .check = do_compile_tests,
202         .describe = describe_compile_tests,
203         .can_run = can_build,
204 };
205
206 REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);