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