ccanlint: clean up reduced feature handling.
[ccan] / tools / ccanlint / tests / tests_helpers_compile.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 "reduce_features.h"
17
18 static const char *can_run(struct manifest *m)
19 {
20         if (safe_mode)
21                 return "Safe mode enabled";
22         return NULL;
23 }
24
25 static bool compile(struct manifest *m,
26                     bool keep,
27                     struct ccan_file *cfile,
28                     const char *flags,
29                     enum compile_type ctype,
30                     char **output)
31 {
32         cfile->compiled[ctype] = maybe_temp_file(m, ".o", keep, cfile->fullname);
33         return compile_object(m, cfile->fullname, ccan_dir, compiler, flags,
34                               cfile->compiled[ctype], output);
35 }
36
37 static void compile_test_helpers(struct manifest *m,
38                                  bool keep,
39                                  unsigned int *timeleft,
40                                  struct score *score,
41                                  const char *flags,
42                                  enum compile_type ctype)
43 {
44         struct ccan_file *i;
45         bool errors = false, warnings = false;
46
47         if (list_empty(&m->other_test_c_files))
48                 score->total = 0;
49         else
50                 score->total = 2;
51
52         list_for_each(&m->other_test_c_files, i, list) {
53                 char *cmdout;
54
55                 if (!compile(m, keep, i, flags, ctype, &cmdout)) {
56                         errors = true;
57                         score_file_error(score, i, 0, "Compile failed:\n%s",
58                                          cmdout);
59                 } else if (!streq(cmdout, "")) {
60                         warnings = true;
61                         score_file_error(score, i, 0,
62                                          "Compile gave warnings:\n%s", cmdout);
63                 }
64         }
65
66         if (!errors) {
67                 score->pass = true;
68                 score->score = score->total - warnings;
69         }
70 }
71
72 static void do_compile_test_helpers(struct manifest *m,
73                                     bool keep,
74                                     unsigned int *timeleft,
75                                     struct score *score)
76 {
77         compile_test_helpers(m, keep, timeleft, score, cflags,
78                              COMPILE_NORMAL);
79 }
80
81 struct ccanlint tests_helpers_compile = {
82         .key = "tests_helpers_compile",
83         .name = "Module test helper objects compile",
84         .check = do_compile_test_helpers,
85         .can_run = can_run,
86         .needs = "depends_build tests_exist"
87 };
88
89 REGISTER_TEST(tests_helpers_compile);
90
91 static const char *features_reduced(struct manifest *m)
92 {
93         if (features_were_reduced)
94                 return NULL;
95         return "No features to turn off";
96 }
97
98 static void do_compile_test_helpers_without_features(struct manifest *m,
99                                                      bool keep,
100                                                      unsigned int *timeleft,
101                                                      struct score *score)
102 {
103         char *flags;
104
105         flags = talloc_asprintf(score, "%s %s", cflags,
106                                 REDUCE_FEATURES_FLAGS);
107
108         compile_test_helpers(m, keep, timeleft, score, flags,
109                              COMPILE_NOFEAT);
110 }
111
112 struct ccanlint tests_helpers_compile_without_features = {
113         .key = "tests_helpers_compile_without_features",
114         .name = "Module tests helpers compile (without features)",
115         .check = do_compile_test_helpers_without_features,
116         .can_run = features_reduced,
117         .needs = "depends_build_without_features tests_exist"
118 };
119 REGISTER_TEST(tests_helpers_compile_without_features);