]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/compulsory_tests/check_build.c
Fix Joey's report of rename failing across moint points.
[ccan] / tools / ccanlint / compulsory_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 char *obj_list(const struct manifest *m)
25 {
26         char *list = talloc_strdup(m, "");
27         struct ccan_file *i;
28
29         /* Other CCAN deps. */
30         list_for_each(&m->dep_objs, i, list)
31                 list = talloc_asprintf_append(list, "%s ", i->name);
32
33         return list;
34 }
35
36 static char *lib_list(const struct manifest *m)
37 {
38         unsigned int i, num;
39         char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
40         char *ret = talloc_strdup(m, "");
41
42         for (i = 0; i < num; i++)
43                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
44         return ret;
45 }
46
47 static void *check_use_build(struct manifest *m)
48 {
49         char *contents;
50         char *tmpfile, *err;
51         int fd;
52
53         tmpfile = temp_file(m, ".c");
54
55         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
56         if (fd < 0)
57                 return talloc_asprintf(m, "Creating temporary file: %s",
58                                        strerror(errno));
59
60         contents = talloc_asprintf(tmpfile,
61                                    "#include <ccan/%s/%s.h>\n"
62                                    "int main(void)\n"
63                                    "{\n"
64                                    "    return 0;\n"
65                                    "}\n",
66                                    m->basename, m->basename);
67         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
68                 close(fd);
69                 return "Failure writing to temporary file";
70         }
71         close(fd);
72
73         if (!compile_and_link(m, tmpfile, ccan_dir, obj_list(m), "",
74                               lib_list(m), &err))
75                 return err;
76         return NULL;
77 }
78
79 static const char *describe_use_build(struct manifest *m, void *check_result)
80 {
81         return talloc_asprintf(check_result, 
82                                "Linking against module:\n"
83                                "%s", (char *)check_result);
84 }
85
86 struct ccanlint check_build = {
87         .name = "Module can be used",
88         .total_score = 1,
89         .check = check_use_build,
90         .describe = describe_use_build,
91         .can_run = can_build,
92 };
93
94 REGISTER_TEST(check_build, &build, NULL);