7e0ab945f290dfaaa83e373b3255520a4e61fc20
[ccan] / tools / ccanlint / compulsory_tests / check_includes_build.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/grab_file/grab_file.h>
5 #include <ccan/str/str.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <err.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 static const char *can_build(struct manifest *m)
19 {
20         if (safe_mode)
21                 return "Safe mode enabled";
22         return NULL;
23 }
24
25 static void *check_includes_build(struct manifest *m)
26 {
27         char *contents;
28         char *tmpfile, *err;
29         int fd;
30
31         tmpfile = temp_file(m, ".c");
32
33         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
34         if (fd < 0)
35                 return talloc_asprintf(m, "Creating temporary file: %s",
36                                        strerror(errno));
37
38         contents = talloc_asprintf(tmpfile, "#include <ccan/%s/%s.h>\n",
39                                    m->basename, m->basename);
40         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
41                 close(fd);
42                 return "Failure writing to temporary file";
43         }
44         close(fd);
45
46         if (compile_object(m, tmpfile, &err))
47                 return NULL;
48         return err;
49 }
50
51 static const char *describe_includes_build(struct manifest *m,
52                                            void *check_result)
53 {
54         return talloc_asprintf(check_result, 
55                                "#include of the main header file:\n"
56                                "%s", (char *)check_result);
57 }
58
59 struct ccanlint includes_build = {
60         .name = "Can compile against main header",
61         .total_score = 1,
62         .check = check_includes_build,
63         .describe = describe_includes_build,
64         .can_run = can_build,
65 };
66
67 REGISTER_TEST(includes_build, &depends_exist, NULL);