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