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