ccanlint: only compile _info once; speeds tdb test from 18 to 16 seconds.
[ccan] / tools / ccanlint / 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, *objfile;
29         int fd;
30
31         tmpfile = temp_file(m, ".c");
32         objfile = temp_file(m, ".o");
33
34         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
35         if (fd < 0)
36                 return talloc_asprintf(m, "Creating temporary file: %s",
37                                        strerror(errno));
38
39         contents = talloc_asprintf(tmpfile, "#include <ccan/%s/%s.h>\n",
40                                    m->basename, m->basename);
41         if (write(fd, contents, strlen(contents)) != strlen(contents)) {
42                 close(fd);
43                 return "Failure writing to temporary file";
44         }
45         close(fd);
46
47         return compile_object(m, objfile, tmpfile);
48 }
49
50 static const char *describe_includes_build(struct manifest *m,
51                                            void *check_result)
52 {
53         return talloc_asprintf(check_result, 
54                                "#include of the main header file:\n"
55                                "%s", (char *)check_result);
56 }
57
58 struct ccanlint includes_build = {
59         .name = "Can compile against main header",
60         .total_score = 1,
61         .check = check_includes_build,
62         .describe = describe_includes_build,
63         .can_run = can_build,
64 };
65
66 REGISTER_TEST(includes_build, &depends_exist, NULL);