]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/compile_test_helpers.c
ccanlint: compile and run tests.
[ccan] / tools / ccanlint / tests / compile_test_helpers.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 static const char *can_build(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 static int cleanup_testfile(char *testfile)
25 {
26         unlink(testfile);
27         return 0;
28 }
29
30 static char *objname(const void *ctx, const char *cfile)
31 {
32         return talloc_asprintf(ctx, "%.*s.o ", strlen(cfile) - 2, cfile);
33 }
34
35 static char *compile(struct manifest *m, const char *cfile)
36 {
37         char *obj;
38
39         obj = objname(m, cfile);
40         talloc_set_destructor(obj, cleanup_testfile);
41         return run_command(m, "cc " CFLAGS " -c -o %s %s", obj, cfile);
42 }
43
44 static void *do_compile_test_helpers(struct manifest *m)
45 {
46         char *cmdout = NULL;
47         struct ccan_file *i;
48
49         list_for_each(&m->other_test_c_files, i, list) {
50                 compile_tests.total_score++;
51                 cmdout = compile(m, i->name);
52                 if (cmdout)
53                         return talloc_asprintf(m,
54                                                "Failed to compile helper C"
55                                                " code file %s:\n%s",
56                                                i->name, cmdout);
57         }
58         return NULL;
59 }
60
61 static const char *describe_compile_test_helpers(struct manifest *m,
62                                                  void *check_result)
63 {
64         return check_result;
65 }
66
67 struct ccanlint compile_test_helpers = {
68         .name = "Compiling test helper files",
69         .total_score = 1,
70         .check = do_compile_test_helpers,
71         .describe = describe_compile_test_helpers,
72         .can_run = can_build,
73 };
74
75 REGISTER_TEST(compile_test_helpers, &depends_built);