1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
17 static const char *can_build(struct manifest *m)
20 return "Safe mode enabled";
24 static char *obj_list(const struct manifest *m, bool link_with_module)
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);
33 list = talloc_strdup(m, "");
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);
40 list = talloc_asprintf_append(list, " %s.o", m->dir);
42 /* Other ccan modules. */
43 list_for_each(&m->dep_objs, i, list)
44 list = talloc_asprintf_append(list, " %s", i->name);
49 static char *lib_list(const struct manifest *m)
52 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
53 char *ret = talloc_strdup(m, "");
55 for (i = 0; i < num; i++)
56 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
60 static char *compile(const void *ctx,
61 struct manifest *m, struct ccan_file *file, bool fail,
62 bool link_with_module)
66 file->compiled = compile_and_link(ctx, file->fullname, ccan_dir,
67 obj_list(m, link_with_module),
69 lib_list(m), &errmsg);
72 talloc_steal(ctx, file->compiled);
76 struct compile_tests_result {
77 struct list_node list;
79 const char *description;
83 static void *do_compile_tests(struct manifest *m, unsigned int *timeleft)
85 struct list_head *list = talloc(m, struct list_head);
88 struct compile_tests_result *res;
92 list_for_each(&m->compile_ok_tests, i, list) {
93 compile_tests.total_score++;
94 cmdout = compile(list, m, i, false, false);
96 res = talloc(list, struct compile_tests_result);
97 res->filename = i->name;
98 res->description = "failed to compile";
99 res->output = talloc_steal(res, cmdout);
100 list_add_tail(list, &res->list);
104 list_for_each(&m->run_tests, i, list) {
105 compile_tests.total_score++;
106 cmdout = compile(m, m, i, false, false);
108 res = talloc(list, struct compile_tests_result);
109 res->filename = i->name;
110 res->description = "failed to compile";
111 res->output = talloc_steal(res, cmdout);
112 list_add_tail(list, &res->list);
116 list_for_each(&m->api_tests, i, list) {
117 compile_tests.total_score++;
118 cmdout = compile(m, m, i, false, true);
120 res = talloc(list, struct compile_tests_result);
121 res->filename = i->name;
122 res->description = "failed to compile";
123 res->output = talloc_steal(res, cmdout);
124 list_add_tail(list, &res->list);
128 list_for_each(&m->compile_fail_tests, i, list) {
129 compile_tests.total_score++;
130 cmdout = compile(list, m, i, false, false);
132 res = talloc(list, struct compile_tests_result);
133 res->filename = i->name;
134 res->description = "failed to compile without -DFAIL";
135 res->output = talloc_steal(res, cmdout);
136 list_add_tail(list, &res->list);
138 cmdout = compile(list, m, i, true, false);
140 res = talloc(list, struct compile_tests_result);
141 res->filename = i->name;
142 res->description = "compiled successfully"
145 list_add_tail(list, &res->list);
150 if (list_empty(list)) {
158 static unsigned int score_compile_tests(struct manifest *m,
161 struct list_head *list = check_result;
162 struct compile_tests_result *i;
163 unsigned int score = compile_tests.total_score;
165 list_for_each(list, i, list)
170 static const char *describe_compile_tests(struct manifest *m,
173 struct list_head *list = check_result;
174 struct compile_tests_result *i;
175 char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
177 list_for_each(list, i, list)
178 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
179 i->filename, i->description,
184 struct ccanlint compile_tests = {
186 .name = "Module tests compile",
187 .score = score_compile_tests,
188 .check = do_compile_tests,
189 .describe = describe_compile_tests,
190 .can_run = can_build,
193 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);