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