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