]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/build_objs.c
e0ea222350bc981ed3075270a0db3ee7459cf1f1
[ccan] / tools / ccanlint / tests / build_objs.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 bool compile_obj(struct ccan_file *c_file, char *objfile, char **report)
25 {
26         char *err;
27
28         err = compile_object(objfile, objfile, c_file->name);
29         if (err) {
30                 if (*report)
31                         *report = talloc_append_string(*report, err);
32                 else
33                         *report = err;
34                 return false;
35         }
36         return true;
37 }
38
39 static int cleanup_obj(char *objfile)
40 {
41         unlink(objfile);
42         return 0;
43 }
44
45 static void *check_objs_build(struct manifest *m)
46 {
47         char *report = NULL;
48         struct ccan_file *i;
49
50         /* One point for each obj file. */
51         list_for_each(&m->c_files, i, list)
52                 build_objs.total_score++;
53
54         list_for_each(&m->c_files, i, list) {
55                 char *objfile = talloc_strdup(m, i->name);
56                 objfile[strlen(objfile)-1] = 'o';
57
58                 if (compile_obj(i, objfile, &report))
59                         talloc_set_destructor(objfile, cleanup_obj);
60         }
61         return report;
62 }
63
64 static const char *describe_objs_build(struct manifest *m, void *check_result)
65 {
66         return talloc_asprintf(check_result, 
67                                "%s", (char *)check_result);
68 }
69
70 struct ccanlint build_objs = {
71         .name = "Module object files can be built",
72         .check = check_objs_build,
73         .describe = describe_objs_build,
74         .can_run = can_build,
75 };
76
77 REGISTER_TEST(build_objs, &depends_exist, NULL);