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