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)
26 char *list = talloc_strdup(m, "../tap.o");
29 /* Objects from any other C files. */
30 list_for_each(&m->other_test_c_files, i, list)
31 list = talloc_asprintf_append(list, " %.*s.o",
32 strlen(i->name) - 2, i->name);
35 list = talloc_asprintf_append(list, " ../%s.o", m->basename);
40 static char *lib_list(const struct manifest *m)
43 char **libs = get_libs(m, ".", ".", &num, &m->info_file->compiled);
44 char *ret = talloc_strdup(m, "");
46 for (i = 0; i < num; i++)
47 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
51 static char *compile(const void *ctx,
52 struct manifest *m, struct ccan_file *file, bool fail,
53 bool link_with_module)
57 file->compiled = compile_and_link(ctx, file->name,
58 obj_list(m, link_with_module),
60 lib_list(m), &errmsg);
63 talloc_steal(ctx, file->compiled);
67 struct compile_tests_result {
68 struct list_node list;
70 const char *description;
74 static void *do_compile_tests(struct manifest *m)
76 struct list_head *list = talloc(m, struct list_head);
79 struct compile_tests_result *res;
83 list_for_each(&m->compile_ok_tests, i, list) {
84 compile_tests.total_score++;
85 cmdout = compile(list, m, i, false, false);
87 res = talloc(list, struct compile_tests_result);
88 res->filename = i->name;
89 res->description = "failed to compile";
90 res->output = talloc_steal(res, cmdout);
91 list_add_tail(list, &res->list);
95 list_for_each(&m->run_tests, i, list) {
96 compile_tests.total_score++;
97 cmdout = compile(m, m, i, false, false);
99 res = talloc(list, struct compile_tests_result);
100 res->filename = i->name;
101 res->description = "failed to compile";
102 res->output = talloc_steal(res, cmdout);
103 list_add_tail(list, &res->list);
107 list_for_each(&m->api_tests, i, list) {
108 compile_tests.total_score++;
109 cmdout = compile(m, m, i, false, true);
111 res = talloc(list, struct compile_tests_result);
112 res->filename = i->name;
113 res->description = "failed to compile";
114 res->output = talloc_steal(res, cmdout);
115 list_add_tail(list, &res->list);
119 list_for_each(&m->compile_fail_tests, i, list) {
120 compile_tests.total_score++;
121 cmdout = compile(list, m, i, true, false);
123 res = talloc(list, struct compile_tests_result);
124 res->filename = i->name;
125 res->description = "failed to compile without -DFAIL";
126 res->output = talloc_steal(res, cmdout);
127 list_add_tail(list, &res->list);
129 cmdout = compile(list, m, i, false, false);
131 res = talloc(list, struct compile_tests_result);
132 res->filename = i->name;
133 res->description = "compiled successfully"
136 list_add_tail(list, &res->list);
141 if (list_empty(list)) {
149 static unsigned int score_compile_tests(struct manifest *m,
152 struct list_head *list = check_result;
153 struct compile_tests_result *i;
154 unsigned int score = compile_tests.total_score;
156 list_for_each(list, i, list)
161 static const char *describe_compile_tests(struct manifest *m,
164 struct list_head *list = check_result;
165 struct compile_tests_result *i;
166 char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
168 list_for_each(list, i, list)
169 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
170 i->filename, i->description,
175 struct ccanlint compile_tests = {
176 .name = "Compile tests succeed",
177 .score = score_compile_tests,
178 .check = do_compile_tests,
179 .describe = describe_compile_tests,
180 .can_run = can_build,
183 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);