66f8954f4be381d982e8068cf3917b653682e1c8
[ccan] / tools / ccanlint / tests / module_links.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 manifest *i;
28
29         /* Other CCAN deps. */
30         list_for_each(&m->deps, i, list) {
31                 if (i->compiled[COMPILE_NORMAL])
32                         list = talloc_asprintf_append(list, "%s ",
33                                                       i->compiled
34                                                       [COMPILE_NORMAL]);
35         }
36         return list;
37 }
38
39 static char *lib_list(const struct manifest *m)
40 {
41         unsigned int i, num;
42         char **libs = get_libs(m, ".",
43                                &num, &m->info_file->compiled[COMPILE_NORMAL]);
44         char *ret = talloc_strdup(m, "");
45
46         for (i = 0; i < num; i++)
47                 ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
48         return ret;
49 }
50
51 static void check_use_build(struct manifest *m,
52                             unsigned int *timeleft, struct score *score)
53 {
54         char *contents;
55         char *tmpfile, *cmdout;
56         char *basename = talloc_asprintf(m, "%s/example.c", m->dir);
57         int fd;
58
59         tmpfile = temp_file(m, ".c", basename);
60
61         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
62         if (fd < 0)
63                 err(1, "Creating temporary file %s", tmpfile);
64
65         contents = talloc_asprintf(tmpfile,
66                                    "#include <ccan/%s/%s.h>\n"
67                                    "int main(void)\n"
68                                    "{\n"
69                                    "    return 0;\n"
70                                    "}\n",
71                                    m->basename, m->basename);
72         if (write(fd, contents, strlen(contents)) != strlen(contents))
73                 err(1, "Failure writing to temporary file %s", tmpfile);
74         close(fd);
75
76         if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m),
77                              compiler, cflags, lib_list(m),
78                              temp_file(m, "", tmpfile),
79                              &cmdout)) {
80                 score->pass = true;
81                 score->score = score->total;
82         } else {
83                 score->error = cmdout;
84         }
85 }
86
87 struct ccanlint module_links = {
88         .key = "module_links",
89         .name = "Module can be linked against trivial program",
90         .check = check_use_build,
91         .can_run = can_build,
92         .needs = "module_builds depends_build"
93 };
94
95 REGISTER_TEST(module_links);