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 list_for_each(&m->compile_ok_tests, i, list) {
87 compile_tests.total_score++;
88 cmdout = compile(list, m, i, false, false);
90 res = talloc(list, struct compile_tests_result);
91 res->filename = i->name;
92 res->description = "failed to compile";
93 res->output = talloc_steal(res, cmdout);
94 list_add_tail(list, &res->list);
98 list_for_each(&m->run_tests, i, list) {
99 compile_tests.total_score++;
100 cmdout = compile(m, m, i, false, false);
102 res = talloc(list, struct compile_tests_result);
103 res->filename = i->name;
104 res->description = "failed to compile";
105 res->output = talloc_steal(res, cmdout);
106 list_add_tail(list, &res->list);
110 list_for_each(&m->api_tests, i, list) {
111 compile_tests.total_score++;
112 cmdout = compile(m, m, i, false, true);
114 res = talloc(list, struct compile_tests_result);
115 res->filename = i->name;
116 res->description = "failed to compile";
117 res->output = talloc_steal(res, cmdout);
118 list_add_tail(list, &res->list);
122 list_for_each(&m->compile_fail_tests, i, list) {
123 compile_tests.total_score++;
124 cmdout = compile(list, m, i, true, false);
126 res = talloc(list, struct compile_tests_result);
127 res->filename = i->name;
128 res->description = "failed to compile without -DFAIL";
129 res->output = talloc_steal(res, cmdout);
130 list_add_tail(list, &res->list);
132 cmdout = compile(list, m, i, false, false);
134 res = talloc(list, struct compile_tests_result);
135 res->filename = i->name;
136 res->description = "compiled successfully"
139 list_add_tail(list, &res->list);
144 if (list_empty(list)) {
152 static unsigned int score_compile_tests(struct manifest *m,
155 struct list_head *list = check_result;
156 struct compile_tests_result *i;
157 unsigned int score = compile_tests.total_score;
159 list_for_each(list, i, list)
164 static const char *describe_compile_tests(struct manifest *m,
167 struct list_head *list = check_result;
168 struct compile_tests_result *i;
169 char *descrip = talloc_strdup(list, "Compilation tests failed:\n");
171 list_for_each(list, i, list)
172 descrip = talloc_asprintf_append(descrip, "%s %s\n%s",
173 i->filename, i->description,
178 struct ccanlint compile_tests = {
179 .name = "Compile tests succeed",
180 .score = score_compile_tests,
181 .check = do_compile_tests,
182 .describe = describe_compile_tests,
183 .can_run = can_build,
186 REGISTER_TEST(compile_tests, &compile_test_helpers, NULL);