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);
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 int cleanup_testfile(const char *testfile)
57 static char *compile(const void *ctx,
58 struct manifest *m, struct ccan_file *file, bool fail,
59 bool link_with_module)
61 file->compiled = talloc_strdup(ctx, tempnam("/tmp", "ccanlint"));
62 talloc_set_destructor(file->compiled, cleanup_testfile);
64 return run_command(m, "cc " CFLAGS " %s -o %s %s %s %s",
66 file->compiled, file->name,
67 obj_list(m, link_with_module), lib_list(m));
70 struct compile_tests_result {
71 struct list_node list;
73 const char *description;
77 static void *do_compile_tests(struct manifest *m)
79 struct list_head *list = talloc(m, struct list_head);
82 struct compile_tests_result *res;
86 compile_tests.total_score = 0;
87 list_for_each(&m->compile_ok_tests, i, list) {
88 compile_tests.total_score++;
89 cmdout = compile(list, m, i, false, false);
91 res = talloc(list, struct compile_tests_result);
92 res->filename = i->name;
93 res->description = "failed to compile";
94 res->output = talloc_steal(res, cmdout);
95 list_add_tail(list, &res->list);
99 list_for_each(&m->run_tests, i, list) {
100 compile_tests.total_score++;
101 cmdout = compile(m, m, i, false, false);
103 res = talloc(list, struct compile_tests_result);
104 res->filename = i->name;
105 res->description = "failed to compile";
106 res->output = talloc_steal(res, cmdout);
107 list_add_tail(list, &res->list);
111 list_for_each(&m->api_tests, i, list) {
112 compile_tests.total_score++;
113 cmdout = compile(m, m, i, false, true);
115 res = talloc(list, struct compile_tests_result);
116 res->filename = i->name;
117 res->description = "failed to compile";
118 res->output = talloc_steal(res, cmdout);
119 list_add_tail(list, &res->list);
123 list_for_each(&m->compile_fail_tests, i, list) {
124 compile_tests.total_score++;
125 cmdout = compile(list, m, i, true, false);
127 res = talloc(list, struct compile_tests_result);
128 res->filename = i->name;
129 res->description = "failed to compile without -DFAIL";
130 res->output = talloc_steal(res, cmdout);
131 list_add_tail(list, &res->list);
133 cmdout = compile(list, m, i, false, false);
135 res = talloc(list, struct compile_tests_result);
136 res->filename = i->name;
137 res->description = "compiled successfully"
140 list_add_tail(list, &res->list);
145 if (list_empty(list)) {
153 static unsigned int score_compile_tests(struct manifest *m,
156 struct list_head *list = check_result;
157 struct compile_tests_result *i;
158 unsigned int score = compile_tests.total_score;
160 list_for_each(list, i, list)
165 static const char *describe_compile_tests(struct manifest *m,
168 struct list_head *list = check_result;
169 struct compile_tests_result *i;
170 char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
172 list_for_each(list, i, list)
173 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
174 i->filename, i->description,
179 struct ccanlint compile_tests = {
180 .name = "Compile tests succeed",
182 .score = score_compile_tests,
183 .check = do_compile_tests,
184 .describe = describe_compile_tests,
185 .can_run = can_build,
188 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);