]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/info_compiles.c
4ec07a0e0486b916c568c0d421f9161e86b5be9b
[ccan] / tools / ccanlint / tests / info_compiles.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/tal/grab_file/grab_file.h>
4 #include <ccan/read_write_all/read_write_all.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 void check_info_compiles(struct manifest *m,
19                                 unsigned int *timeleft, struct score *score)
20 {
21         char *info_c_file, *info, *output;
22         int fd;
23
24         /* We don't really fail if we're in safe mode: our dependencies
25          * still run. */
26         if (safe_mode) {
27                 score->pass = true;
28                 score->score = 0;
29                 return;
30         }
31
32         /* Copy it to a file with proper .c suffix. */
33         info = grab_file(score, m->info_file->fullname);
34         if (!info) {
35                 score_file_error(score, m->info_file, 0,
36                                  "could not be read: %s", strerror(errno));
37                 return;
38         }
39
40         info_c_file = temp_file(info, ".c", "_info");
41         fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
42         if (fd < 0 || !write_all(fd, info, tal_count(info)-1))
43                 err(1, "Copying _info file");
44
45         if (close(fd) != 0)
46                 err(1, "Closing _info file");
47
48         m->info_file->compiled[COMPILE_NORMAL] = temp_file(m, "", "info");
49         if (!compile_and_link(score, info_c_file, find_ccan_dir(m->dir), "",
50                               compiler, cflags, "",
51                               m->info_file->compiled[COMPILE_NORMAL],
52                               &output)) {
53                 score_file_error(score, m->info_file, 0,
54                                  "Errors compiling _info:\n%s", output);
55                 return;
56         }
57
58         score->pass = true;
59         score->score = 1;
60 }
61
62 struct ccanlint info_compiles = {
63         .key = "info_compiles",
64         .name = "_info compiles",
65         .check = check_info_compiles,
66         .needs = "info_exists",
67         .compulsory = true
68 };
69
70 REGISTER_TEST(info_compiles);