]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/compile_tests.c
ccanlint: tweak example compilation.
[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;
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         /* 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", i->compiled);
43
44         /* Other ccan modules. */
45         list_for_each(&m->dep_dirs, i, list) {
46                 if (i->compiled)
47                         list = talloc_asprintf_append(list, " %s", i->compiled);
48         }
49
50         return list;
51 }
52
53 static char *lib_list(const struct manifest *m)
54 {
55         unsigned int i, num;
56         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
57         char *ret = talloc_strdup(m, "");
58
59         for (i = 0; i < num; i++)
60                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
61         return ret;
62 }
63
64 static char *compile(const void *ctx,
65                      struct manifest *m,
66                      struct ccan_file *file,
67                      bool fail,
68                      bool link_with_module,
69                      bool keep)
70 {
71         char *errmsg;
72
73         file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
74         errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
75                                   obj_list(m, link_with_module),
76                                   fail ? "-DFAIL" : "",
77                                   lib_list(m), file->compiled);
78         if (errmsg) {
79                 talloc_free(file->compiled);
80                 return errmsg;
81         }
82         return NULL;
83 }
84
85 static void do_compile_tests(struct manifest *m,
86                              bool keep,
87                              unsigned int *timeleft, struct score *score)
88 {
89         char *cmdout;
90         struct ccan_file *i;
91
92         list_for_each(&m->compile_ok_tests, i, list) {
93                 cmdout = compile(score, m, i, false, false, keep);
94                 if (cmdout) {
95                         score->error = "Failed to compile tests";
96                         score_file_error(score, i, 0, cmdout);
97                 }
98         }
99
100         list_for_each(&m->run_tests, i, list) {
101                 cmdout = compile(score, m, i, false, false, keep);
102                 if (cmdout) {
103                         score->error = "Failed to compile tests";
104                         score_file_error(score, i, 0, cmdout);
105                 }
106         }
107
108         list_for_each(&m->api_tests, i, list) {
109                 cmdout = compile(score, m, i, false, true, keep);
110                 if (cmdout) {
111                         score->error = "Failed to compile tests";
112                         score_file_error(score, i, 0, cmdout);
113                 }
114         }
115
116         /* The compile fail tests are a bit weird, handle them separately */
117         if (score->error)
118                 return;
119
120         list_for_each(&m->compile_fail_tests, i, list) {
121                 cmdout = compile(score, m, i, false, false, false);
122                 if (cmdout) {
123                         score->error = "Failed to compile without -DFAIL";
124                         score_file_error(score, i, 0, cmdout);
125                         return;
126                 }
127                 cmdout = compile(score, m, i, true, false, false);
128                 if (!cmdout) {
129                         score->error = "Compiled successfully with -DFAIL?";
130                         score_file_error(score, i, 0, NULL);
131                         return;
132                 }
133         }
134
135         score->pass = true;
136         score->score = score->total;
137 }
138
139 struct ccanlint compile_tests = {
140         .key = "compile-tests",
141         .name = "Module tests compile",
142         .check = do_compile_tests,
143         .can_run = can_build,
144 };
145
146 REGISTER_TEST(compile_tests, &compile_test_helpers, &build_objs, NULL);