]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/main_header_compiles.c
f54c6173a53578e76c6e805cf281ef7319a6c19a
[ccan] / tools / ccanlint / tests / main_header_compiles.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 static const char *can_build(struct manifest *m)
17 {
18         if (safe_mode)
19                 return "Safe mode enabled";
20         return NULL;
21 }
22
23 static struct ccan_file *main_header(struct manifest *m)
24 {
25         struct ccan_file *f;
26
27         list_for_each(&m->h_files, f, list) {
28                 if (strstarts(f->name, m->basename)
29                     && strlen(f->name) == strlen(m->basename) + 2)
30                         return f;
31         }
32         /* Should not happen: we depend on has_main_header */
33         abort();
34 }
35
36 static void check_includes_build(struct manifest *m,
37                                  unsigned int *timeleft, struct score *score)
38 {
39         char *contents;
40         char *tmpsrc, *tmpobj, *cmdout;
41         int fd;
42         struct ccan_file *mainh = main_header(m);
43
44         tmpsrc = temp_file(m, "-included.c", mainh->fullname);
45         tmpobj = temp_file(m, ".o", tmpsrc);
46
47         fd = open(tmpsrc, O_WRONLY | O_CREAT | O_EXCL, 0600);
48         if (fd < 0)
49                 err(1, "Creating temporary file %s", tmpsrc);
50
51         contents = tal_fmt(tmpsrc, "#include <ccan/%s/%s.h>\n",
52                            m->modname, m->basename);
53         if (write(fd, contents, strlen(contents)) != strlen(contents))
54                 err(1, "writing to temporary file %s", tmpsrc);
55         close(fd);
56
57         if (compile_object(score, tmpsrc, ccan_dir, compiler, cflags,
58                            tmpobj, &cmdout)) {
59                 score->pass = true;
60                 score->score = score->total;
61         } else {
62                 score->error = tal_fmt(score,
63                                        "#include of the main header file:\n%s",
64                                        cmdout);
65         }
66 }
67
68 struct ccanlint main_header_compiles = {
69         .key = "main_header_compiles",
70         .name = "Modules main header compiles",
71         .check = check_includes_build,
72         .can_run = can_build,
73         .needs = "depends_exist main_header_exists info_ported"
74 };
75
76 REGISTER_TEST(main_header_compiles);