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