ccanlint: Add cflags support to _info
[ccan] / tools / ccanlint / tests / objects_build.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <ccan/tal/path/path.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 *cflags_list(const struct manifest *m)
26 {
27         unsigned int i;
28         char *ret = tal_strdup(m, cflags);
29
30         char **flags = get_cflags(m, m->dir, get_or_compile_info);
31         for (i = 0; flags[i]; i++)
32                 tal_append_fmt(&ret, " %s", flags[i]);
33         return ret;
34 }
35
36 void build_objects(struct manifest *m,
37                    struct score *score, const char *flags,
38                    enum compile_type ctype)
39 {
40         struct ccan_file *i;
41         bool errors = false, warnings = false;
42
43         if (list_empty(&m->c_files))
44                 score->total = 0;
45         else
46                 score->total = 2;
47
48         list_for_each(&m->c_files, i, list) {
49                 char *output;
50                 char *fullfile = path_join(m, m->dir, i->name);
51
52                 i->compiled[ctype] = temp_file(m, "", fullfile);
53                 if (!compile_object(score, fullfile, ccan_dir, compiler, flags,
54                                     i->compiled[ctype], &output)) {
55                         tal_free(i->compiled[ctype]);
56                         score_file_error(score, i, 0,
57                                          "Compiling object files:\n%s",
58                                          output);
59                         errors = true;
60                 } else if (!streq(output, "")) {
61                         score_file_error(score, i, 0,
62                                          "Compiling object files gave"
63                                          " warnings:\n%s",
64                                          output);
65                         warnings = true;
66                 }
67         }
68
69         if (!errors) {
70                 score->pass = true;
71                 score->score = score->total - warnings;
72         } else
73                 build_failed = true;
74 }
75
76 static void check_objs_build(struct manifest *m,
77                              unsigned int *timeleft, struct score *score)
78 {
79         const char *flags;
80
81         flags = cflags_list(m);
82         build_objects(m, score, flags, COMPILE_NORMAL);
83 }
84
85 struct ccanlint objects_build = {
86         .key = "objects_build",
87         .name = "Module object files can be built",
88         .compulsory = true,
89         .check = check_objs_build,
90         .can_run = can_build,
91         .needs = "depends_exist"
92 };
93
94 REGISTER_TEST(objects_build);