]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/compulsory_tests/build.c
557b708bf7144c2237caa0c44d93c9771cf8d04b
[ccan] / tools / ccanlint / compulsory_tests / 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 #include "build.h"
17
18 static const char *can_build(struct manifest *m)
19 {
20         if (safe_mode)
21                 return "Safe mode enabled";
22         return NULL;
23 }
24
25 static char *obj_list(const struct manifest *m)
26 {
27         char *list = talloc_strdup(m, "");
28         struct ccan_file *i;
29
30         /* Objects from all the C files. */
31         list_for_each(&m->c_files, i, list)
32                 list = talloc_asprintf_append(list, "%s ", i->compiled);
33
34         return list;
35 }
36
37 char *build_module(struct manifest *m, bool keep, char **errstr)
38 {
39         char *name = link_objects(m, m->basename, false, obj_list(m), errstr);
40         if (name) {
41                 if (keep) {
42                         char *realname = talloc_asprintf(m, "%s.o", m->dir);
43                         /* We leave this object file around, all built. */
44                         if (!move_file(name, realname))
45                                 err(1, "Renaming %s to %s", name, realname);
46                         name = realname;
47                 }
48         }
49         return name;
50 }
51
52 static void do_build(struct manifest *m,
53                      bool keep,
54                      unsigned int *timeleft,
55                      struct score *score)
56 {
57         char *errstr;
58
59         if (list_empty(&m->c_files)) {
60                 /* No files?  No score, but we "pass". */
61                 score->total = 0;
62                 score->pass = true;
63                 return;
64         }
65
66         m->compiled = build_module(m, keep, &errstr);
67         if (!m->compiled) {
68                 score_file_error(score, NULL, 0, errstr);
69                 return;
70         }
71
72         score->pass = true;
73         score->score = score->total;
74 }
75
76 struct ccanlint build = {
77         .key = "module_builds",
78         .name = "Module can be built from object files",
79         .check = do_build,
80         .can_run = can_build,
81 };
82
83 REGISTER_TEST(build, &build_objs, NULL);