ccanlint: with -vv, give details on attempts to compile examples.
[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         /* 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         list_for_each(&m->compile_ok_tests, i, list) {
104                 compile_tests.total_score++;
105                 cmdout = compile(list, m, i, false, false, keep);
106                 if (cmdout) {
107                         res = talloc(list, struct compile_tests_result);
108                         res->filename = i->name;
109                         res->description = "failed to compile";
110                         res->output = talloc_steal(res, cmdout);
111                         list_add_tail(list, &res->list);
112                 }
113         }
114
115         list_for_each(&m->run_tests, i, list) {
116                 compile_tests.total_score++;
117                 cmdout = compile(m, m, i, false, false, keep);
118                 if (cmdout) {
119                         res = talloc(list, struct compile_tests_result);
120                         res->filename = i->name;
121                         res->description = "failed to compile";
122                         res->output = talloc_steal(res, cmdout);
123                         list_add_tail(list, &res->list);
124                 }
125         }
126
127         list_for_each(&m->api_tests, i, list) {
128                 compile_tests.total_score++;
129                 cmdout = compile(m, m, i, false, true, keep);
130                 if (cmdout) {
131                         res = talloc(list, struct compile_tests_result);
132                         res->filename = i->name;
133                         res->description = "failed to compile";
134                         res->output = talloc_steal(res, cmdout);
135                         list_add_tail(list, &res->list);
136                 }
137         }
138
139         list_for_each(&m->compile_fail_tests, i, list) {
140                 compile_tests.total_score++;
141                 cmdout = compile(list, m, i, false, false, false);
142                 if (cmdout) {
143                         res = talloc(list, struct compile_tests_result);
144                         res->filename = i->name;
145                         res->description = "failed to compile without -DFAIL";
146                         res->output = talloc_steal(res, cmdout);
147                         list_add_tail(list, &res->list);
148                 } else {
149                         cmdout = compile(list, m, i, true, false, false);
150                         if (!cmdout) {
151                                 res = talloc(list, struct compile_tests_result);
152                                 res->filename = i->name;
153                                 res->description = "compiled successfully"
154                                         " with -DFAIL";
155                                 res->output = "";
156                                 list_add_tail(list, &res->list);
157                         }
158                 }
159         }
160
161         if (list_empty(list)) {
162                 talloc_free(list);
163                 list = NULL;
164         }
165
166         return list;
167 }
168
169 static unsigned int score_compile_tests(struct manifest *m,
170                                         void *check_result)
171 {
172         struct list_head *list = check_result;
173         struct compile_tests_result *i;
174         unsigned int score = compile_tests.total_score;
175
176         list_for_each(list, i, list)
177                 score--;
178         return score;
179 }
180
181 static const char *describe_compile_tests(struct manifest *m,
182                                           void *check_result)
183 {
184         struct list_head *list = check_result;
185         struct compile_tests_result *i;
186         char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
187
188         list_for_each(list, i, list)
189                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
190                                                  i->filename, i->description,
191                                                  i->output);
192         return descrip;
193 }
194
195 struct ccanlint compile_tests = {
196         .key = "compile-tests",
197         .name = "Module tests compile",
198         .score = score_compile_tests,
199         .check = do_compile_tests,
200         .describe = describe_compile_tests,
201         .can_run = can_build,
202 };
203
204 REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);