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