]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/check_build.c
ccanlint: compile and run tests.
[ccan] / tools / ccanlint / tests / check_build.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 *obj_list(const struct manifest *m)
31 {
32         char *list = talloc_strdup(m, "");
33         struct ccan_file *i;
34
35         /* Other CCAN deps. */
36         list_for_each(&m->dep_objs, i, list)
37                 list = talloc_asprintf_append(list, "%s ", i->name);
38
39         return list;
40 }
41
42 static char *lib_list(const struct manifest *m)
43 {
44         unsigned int i, num;
45         char **libs = get_libs(m, ".", ".", &num);
46         char *ret = talloc_strdup(m, "");
47
48         for (i = 0; i < num; i++)
49                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
50         return ret;
51 }
52
53 static void *check_use_build(struct manifest *m)
54 {
55         char *contents;
56         char *tmpfile, *outfile;
57         int fd;
58
59         tmpfile = talloc_strdup(m, tempnam("/tmp", "ccanlint"));
60         talloc_set_destructor(tmpfile, cleanup_testfile);
61         outfile = talloc_strdup(m, tempnam("/tmp", "ccanlint"));
62         talloc_set_destructor(outfile, cleanup_testfile);
63
64         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
65         if (fd < 0)
66                 return talloc_asprintf(m, "Creating temporary file: %s",
67                                        strerror(errno));
68
69         contents = talloc_asprintf(tmpfile,
70                                    "#include <ccan/%s/%s.h>\n"
71                                    "int main(void)\n"
72                                    "{\n"
73                                    "    return 0;\n"
74                                    "}\n",
75                                    m->basename, m->basename);
76         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
77                 close(fd);
78                 return "Failure writing to temporary file";
79         }
80         close(fd);
81
82         return run_command(m, "cc " CFLAGS " -o %s -x c %s -x none %s %s",
83                            outfile, tmpfile, obj_list(m), lib_list(m));
84 }
85
86 static const char *describe_use_build(struct manifest *m, void *check_result)
87 {
88         return talloc_asprintf(check_result, 
89                                "Linking against module:\n"
90                                "%s", (char *)check_result);
91 }
92
93 struct ccanlint check_build = {
94         .name = "Module can be used",
95         .total_score = 1,
96         .check = check_use_build,
97         .describe = describe_use_build,
98         .can_run = can_build,
99 };
100
101 REGISTER_TEST(check_build, &build, NULL);