]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/module_links.c
ccanlint: make _info ported an empty string on success.
[ccan] / tools / ccanlint / tests / module_links.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/take/take.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 = tal_strdup(m, m->compiled[COMPILE_NORMAL]);
31         else
32                 list = tal_strdup(m, "");
33
34         /* Other CCAN deps. */
35         list_for_each(&m->deps, i, list) {
36                 if (!i->compiled[COMPILE_NORMAL])
37                         continue;
38                 list = tal_strcat(m, take(list), " ");
39                 list = tal_strcat(m, take(list), i->compiled[COMPILE_NORMAL]);
40         }
41         return list;
42 }
43
44 static char *cflags_list(const struct manifest *m)
45 {
46         unsigned int i;
47         char *ret = tal_strdup(m, cflags);
48
49         char **flags = get_cflags(m, m->dir, get_or_compile_info);
50         for (i = 0; flags[i]; i++)
51                 tal_append_fmt(&ret, " %s", flags[i]);
52         return ret;
53 }
54
55 static char *lib_list(const struct manifest *m)
56 {
57         unsigned int i;
58         char **libs;
59         char *ret = tal_strdup(m, "");
60
61         libs = get_libs(m, m->dir, "depends", get_or_compile_info);
62         for (i = 0; libs[i]; i++)
63                 tal_append_fmt(&ret, "-l%s ", libs[i]);
64         return ret;
65 }
66
67 static void check_use_build(struct manifest *m,
68                             unsigned int *timeleft, struct score *score)
69 {
70         char *contents;
71         char *tmpfile, *cmdout;
72         int fd;
73         char *flags;
74
75         tmpfile = temp_file(m, ".c", "example.c");
76
77         fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
78         if (fd < 0)
79                 err(1, "Creating temporary file %s", tmpfile);
80
81         contents = tal_fmt(tmpfile,
82                            "#include <ccan/%s/%s.h>\n"
83                            "int main(void)\n"
84                            "{\n"
85                            "    return 0;\n"
86                            "}\n",
87                            m->modname, m->basename);
88         if (write(fd, contents, strlen(contents)) != strlen(contents))
89                 err(1, "Failure writing to temporary file %s", tmpfile);
90         close(fd);
91
92         flags = cflags_list(m);
93
94         if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m),
95                              compiler, flags, lib_list(m),
96                              temp_file(m, "", tmpfile),
97                              &cmdout)) {
98                 score->pass = true;
99                 score->score = score->total;
100         } else {
101                 score->error = cmdout;
102         }
103 }
104
105 struct ccanlint module_links = {
106         .key = "module_links",
107         .name = "Module can be linked against trivial program",
108         .check = check_use_build,
109         .can_run = can_build,
110         .needs = "module_builds depends_build"
111 };
112
113 REGISTER_TEST(module_links);