2e7cd2640eec2a1022e50d00906294aee5825077
[ccan] / tools / ccanlint / tests / tests_compile.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 <ccan/foreach/foreach.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include "reduce_features.h"
18 #include "tests_compile.h"
19
20 static const char *can_build(struct manifest *m)
21 {
22         if (safe_mode)
23                 return "Safe mode enabled";
24         return NULL;
25 }
26
27 char *test_obj_list(const struct manifest *m, bool link_with_module,
28                     enum compile_type ctype, enum compile_type own_ctype)
29 {
30         char *list = talloc_strdup(m, "");
31         struct ccan_file *i;
32         struct manifest *subm;
33
34         /* Objects from any other C files. */
35         list_for_each(&m->other_test_c_files, i, list)
36                 list = talloc_asprintf_append(list, " %s",
37                                               i->compiled[ctype]);
38
39         /* Our own object files. */
40         if (link_with_module)
41                 list_for_each(&m->c_files, i, list)
42                         list = talloc_asprintf_append(list, " %s",
43                                                       i->compiled[own_ctype]);
44
45         /* Other ccan modules. */
46         list_for_each(&m->deps, subm, list) {
47                 if (subm->compiled[ctype])
48                         list = talloc_asprintf_append(list, " %s",
49                                                       subm->compiled[ctype]);
50         }
51
52         return list;
53 }
54
55 char *lib_list(const struct manifest *m, enum compile_type ctype)
56 {
57         unsigned int i, num;
58         char **libs = get_libs(m, m->dir, &num,
59                                &m->info_file->compiled[ctype]);
60         char *ret = talloc_strdup(m, "");
61
62         for (i = 0; i < num; i++)
63                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
64         return ret;
65 }
66
67 static bool compile(const void *ctx,
68                     struct manifest *m,
69                     struct ccan_file *file,
70                     bool fail,
71                     bool link_with_module,
72                     bool keep,
73                     enum compile_type ctype,
74                     char **output)
75 {
76         char *fname, *flags;
77
78         flags = talloc_asprintf(ctx, "%s%s%s",
79                                 fail ? "-DFAIL " : "",
80                                 cflags,
81                                 ctype == COMPILE_NOFEAT ? " -I." : "");
82
83         fname = maybe_temp_file(ctx, "", keep, file->fullname);
84         if (!compile_and_link(ctx, file->fullname, ccan_dir,
85                               test_obj_list(m, link_with_module,
86                                             ctype, ctype),
87                               compiler, flags, lib_list(m, ctype), fname,
88                               output)) {
89                 talloc_free(fname);
90                 return false;
91         }
92
93         file->compiled[ctype] = fname;
94         return true;
95 }
96
97 static void compile_tests(struct manifest *m, bool keep,
98                           struct score *score,
99                           enum compile_type ctype)
100 {
101         char *cmdout;
102         struct ccan_file *i;
103         struct list_head *list;
104         bool errors = false, warnings = false;
105
106         foreach_ptr(list, &m->compile_ok_tests, &m->run_tests, &m->api_tests) {
107                 list_for_each(list, i, list) {
108                         if (!compile(score, m, i, false,
109                                      list == &m->api_tests, keep,
110                                      ctype, &cmdout)) {
111                                 score_file_error(score, i, 0,
112                                                  "Compile failed:\n%s",
113                                                  cmdout);
114                                 errors = true;
115                         } else if (!streq(cmdout, "")) {
116                                 score_file_error(score, i, 0,
117                                                  "Compile gave warnings:\n%s",
118                                                  cmdout);
119                                 warnings = true;
120                         }
121                 }
122         }
123
124         /* The compile fail tests are a bit weird, handle them separately */
125         if (errors)
126                 return;
127
128         /* For historical reasons, "fail" often means "gives warnings" */
129         list_for_each(&m->compile_fail_tests, i, list) {
130                 if (!compile(score, m, i, false, false, false,
131                              ctype, &cmdout)) {
132                         score_file_error(score, i, 0,
133                                          "Compile without -DFAIL failed:\n%s",
134                                          cmdout);
135                         return;
136                 }
137                 if (!streq(cmdout, "")) {
138                         score_file_error(score, i, 0,
139                                          "Compile gave warnings"
140                                          " without -DFAIL:\n%s",
141                                          cmdout);
142                         return;
143                 }
144                 if (compile(score, m, i, true, false, false,
145                             ctype, &cmdout)
146                     && streq(cmdout, "")) {
147                         score_file_error(score, i, 0,
148                                          "Compiled successfully with -DFAIL?");
149                         return;
150                 }
151                 score->total++;
152         }
153
154         score->pass = true;
155         score->score = score->total - warnings;
156 }
157
158 static void do_compile_tests(struct manifest *m,
159                              bool keep,
160                              unsigned int *timeleft, struct score *score)
161 {
162         compile_tests(m, keep, score, COMPILE_NORMAL);
163 }
164
165 struct ccanlint tests_compile = {
166         .key = "tests_compile",
167         .name = "Module tests compile",
168         .check = do_compile_tests,
169         .can_run = can_build,
170         .needs = "tests_helpers_compile objects_build"
171 };
172
173 REGISTER_TEST(tests_compile);
174
175 static const char *features_reduced(struct manifest *m)
176 {
177         if (features_were_reduced)
178                 return NULL;
179         return "No features to turn off";
180 }
181
182 static void do_compile_tests_without_features(struct manifest *m,
183                                               bool keep,
184                                               unsigned int *timeleft,
185                                               struct score *score)
186 {
187         compile_tests(m, keep, score, COMPILE_NOFEAT);
188 }
189
190 struct ccanlint tests_compile_without_features = {
191         .key = "tests_compile_without_features",
192         .name = "Module tests compile (without features)",
193         .check = do_compile_tests_without_features,
194         .can_run = features_reduced,
195         .needs = "tests_helpers_compile_without_features reduce_features"
196 };
197 REGISTER_TEST(tests_compile_without_features);