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