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