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_dirs, i, list) {
45 list = talloc_asprintf_append(list, " %s", i->compiled);
51 static char *lib_list(const struct manifest *m)
54 char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
55 char *ret = talloc_strdup(m, "");
57 for (i = 0; i < num; i++)
58 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
62 static char *compile(const void *ctx,
64 struct ccan_file *file,
66 bool link_with_module,
71 file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
72 errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
73 obj_list(m, link_with_module),
75 lib_list(m), file->compiled);
77 talloc_free(file->compiled);
83 struct compile_tests_result {
84 struct list_node list;
86 const char *description;
90 static void *do_compile_tests(struct manifest *m,
92 unsigned int *timeleft)
94 struct list_head *list = talloc(m, struct list_head);
97 struct compile_tests_result *res;
101 list_for_each(&m->compile_ok_tests, i, list) {
102 compile_tests.total_score++;
103 cmdout = compile(list, m, i, false, false, keep);
105 res = talloc(list, struct compile_tests_result);
106 res->filename = i->name;
107 res->description = "failed to compile";
108 res->output = talloc_steal(res, cmdout);
109 list_add_tail(list, &res->list);
113 list_for_each(&m->run_tests, i, list) {
114 compile_tests.total_score++;
115 cmdout = compile(m, m, i, false, false, keep);
117 res = talloc(list, struct compile_tests_result);
118 res->filename = i->name;
119 res->description = "failed to compile";
120 res->output = talloc_steal(res, cmdout);
121 list_add_tail(list, &res->list);
125 list_for_each(&m->api_tests, i, list) {
126 compile_tests.total_score++;
127 cmdout = compile(m, m, i, false, true, keep);
129 res = talloc(list, struct compile_tests_result);
130 res->filename = i->name;
131 res->description = "failed to compile";
132 res->output = talloc_steal(res, cmdout);
133 list_add_tail(list, &res->list);
137 list_for_each(&m->compile_fail_tests, i, list) {
138 compile_tests.total_score++;
139 cmdout = compile(list, m, i, false, false, false);
141 res = talloc(list, struct compile_tests_result);
142 res->filename = i->name;
143 res->description = "failed to compile without -DFAIL";
144 res->output = talloc_steal(res, cmdout);
145 list_add_tail(list, &res->list);
147 cmdout = compile(list, m, i, true, false, false);
149 res = talloc(list, struct compile_tests_result);
150 res->filename = i->name;
151 res->description = "compiled successfully"
154 list_add_tail(list, &res->list);
159 if (list_empty(list)) {
167 static unsigned int score_compile_tests(struct manifest *m,
170 struct list_head *list = check_result;
171 struct compile_tests_result *i;
172 unsigned int score = compile_tests.total_score;
174 list_for_each(list, i, list)
179 static const char *describe_compile_tests(struct manifest *m,
182 struct list_head *list = check_result;
183 struct compile_tests_result *i;
184 char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
186 list_for_each(list, i, list)
187 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
188 i->filename, i->description,
193 struct ccanlint compile_tests = {
194 .key = "compile-tests",
195 .name = "Module tests compile",
196 .score = score_compile_tests,
197 .check = do_compile_tests,
198 .describe = describe_compile_tests,
199 .can_run = can_build,
202 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);