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