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