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