ccanlint: make sure fullname is always full path name.
[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, struct ccan_file *file, bool fail,
64                      bool link_with_module)
65 {
66         char *errmsg;
67
68         file->compiled = compile_and_link(ctx, file->fullname, ccan_dir,
69                                           obj_list(m, link_with_module),
70                                           fail ? "-DFAIL" : "",
71                                           lib_list(m), &errmsg);
72         if (!file->compiled)
73                 return errmsg;
74         talloc_steal(ctx, file->compiled);
75         return NULL;
76 }
77
78 struct compile_tests_result {
79         struct list_node list;
80         const char *filename;
81         const char *description;
82         const char *output;
83 };
84
85 static void *do_compile_tests(struct manifest *m, unsigned int *timeleft)
86 {
87         struct list_head *list = talloc(m, struct list_head);
88         char *cmdout;
89         struct ccan_file *i;
90         struct compile_tests_result *res;
91
92         list_head_init(list);
93
94         list_for_each(&m->compile_ok_tests, i, list) {
95                 compile_tests.total_score++;
96                 cmdout = compile(list, m, i, false, false);
97                 if (cmdout) {
98                         res = talloc(list, struct compile_tests_result);
99                         res->filename = i->name;
100                         res->description = "failed to compile";
101                         res->output = talloc_steal(res, cmdout);
102                         list_add_tail(list, &res->list);
103                 }
104         }
105
106         list_for_each(&m->run_tests, i, list) {
107                 compile_tests.total_score++;
108                 cmdout = compile(m, m, i, false, false);
109                 if (cmdout) {
110                         res = talloc(list, struct compile_tests_result);
111                         res->filename = i->name;
112                         res->description = "failed to compile";
113                         res->output = talloc_steal(res, cmdout);
114                         list_add_tail(list, &res->list);
115                 }
116         }
117
118         list_for_each(&m->api_tests, i, list) {
119                 compile_tests.total_score++;
120                 cmdout = compile(m, m, i, false, true);
121                 if (cmdout) {
122                         res = talloc(list, struct compile_tests_result);
123                         res->filename = i->name;
124                         res->description = "failed to compile";
125                         res->output = talloc_steal(res, cmdout);
126                         list_add_tail(list, &res->list);
127                 }
128         }
129
130         list_for_each(&m->compile_fail_tests, i, list) {
131                 compile_tests.total_score++;
132                 cmdout = compile(list, m, i, false, false);
133                 if (cmdout) {
134                         res = talloc(list, struct compile_tests_result);
135                         res->filename = i->name;
136                         res->description = "failed to compile without -DFAIL";
137                         res->output = talloc_steal(res, cmdout);
138                         list_add_tail(list, &res->list);
139                 } else {
140                         cmdout = compile(list, m, i, true, false);
141                         if (!cmdout) {
142                                 res = talloc(list, struct compile_tests_result);
143                                 res->filename = i->name;
144                                 res->description = "compiled successfully"
145                                         " with -DFAIL";
146                                 res->output = "";
147                                 list_add_tail(list, &res->list);
148                         }
149                 }
150         }
151
152         if (list_empty(list)) {
153                 talloc_free(list);
154                 list = NULL;
155         }
156
157         return list;
158 }
159
160 static unsigned int score_compile_tests(struct manifest *m,
161                                         void *check_result)
162 {
163         struct list_head *list = check_result;
164         struct compile_tests_result *i;
165         unsigned int score = compile_tests.total_score;
166
167         list_for_each(list, i, list)
168                 score--;
169         return score;
170 }
171
172 static const char *describe_compile_tests(struct manifest *m,
173                                           void *check_result)
174 {
175         struct list_head *list = check_result;
176         struct compile_tests_result *i;
177         char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
178
179         list_for_each(list, i, list)
180                 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
181                                                  i->filename, i->description,
182                                                  i->output);
183         return descrip;
184 }
185
186 struct ccanlint compile_tests = {
187         .key = "compile",
188         .name = "Module tests compile",
189         .score = score_compile_tests,
190         .check = do_compile_tests,
191         .describe = describe_compile_tests,
192         .can_run = can_build,
193 };
194
195 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);