]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/tests_helpers_compile.c
ccan: Correct some poor conventions in _info includes
[ccan] / tools / ccanlint / tests / tests_helpers_compile.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/str/str.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <err.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include "reduce_features.h"
16
17 static const char *can_run(struct manifest *m)
18 {
19         if (safe_mode)
20                 return "Safe mode enabled";
21         return NULL;
22 }
23
24 static bool compile(struct manifest *m,
25                     struct ccan_file *cfile,
26                     const char *flags,
27                     enum compile_type ctype,
28                     char **output)
29 {
30         cfile->compiled[ctype] = temp_file(m, ".o", cfile->fullname);
31         return compile_object(m, cfile->fullname, ccan_dir, compiler, flags,
32                               cfile->compiled[ctype], output);
33 }
34
35 static void compile_test_helpers(struct manifest *m,
36                                  unsigned int *timeleft,
37                                  struct score *score,
38                                  const char *flags,
39                                  enum compile_type ctype)
40 {
41         struct ccan_file *i;
42         bool errors = false, warnings = false;
43
44         if (list_empty(&m->other_test_c_files))
45                 score->total = 0;
46         else
47                 score->total = 2;
48
49         list_for_each(&m->other_test_c_files, i, list) {
50                 char *cmdout;
51
52                 if (!compile(m, i, flags, ctype, &cmdout)) {
53                         errors = true;
54                         score_file_error(score, i, 0, "Compile failed:\n%s",
55                                          cmdout);
56                 } else if (!streq(cmdout, "")) {
57                         warnings = true;
58                         score_file_error(score, i, 0,
59                                          "Compile gave warnings:\n%s", cmdout);
60                 }
61         }
62
63         if (!errors) {
64                 score->pass = true;
65                 score->score = score->total - warnings;
66         }
67 }
68
69 static void do_compile_test_helpers(struct manifest *m,
70                                     unsigned int *timeleft,
71                                     struct score *score)
72 {
73         compile_test_helpers(m, timeleft, score, cflags, COMPILE_NORMAL);
74 }
75
76 struct ccanlint tests_helpers_compile = {
77         .key = "tests_helpers_compile",
78         .name = "Module test helper objects compile",
79         .check = do_compile_test_helpers,
80         .can_run = can_run,
81         .needs = "depends_build tests_exist"
82 };
83
84 REGISTER_TEST(tests_helpers_compile);
85
86 static const char *features_reduced(struct manifest *m)
87 {
88         if (features_were_reduced)
89                 return NULL;
90         return "No features to turn off";
91 }
92
93 static void do_compile_test_helpers_without_features(struct manifest *m,
94                                                      unsigned int *timeleft,
95                                                      struct score *score)
96 {
97         char *flags;
98
99         flags = tal_fmt(score, "%s %s", cflags, REDUCE_FEATURES_FLAGS);
100
101         compile_test_helpers(m, timeleft, score, flags, COMPILE_NOFEAT);
102 }
103
104 struct ccanlint tests_helpers_compile_without_features = {
105         .key = "tests_helpers_compile_without_features",
106         .name = "Module tests helpers compile (without features)",
107         .check = do_compile_test_helpers_without_features,
108         .can_run = features_reduced,
109         .needs = "depends_build_without_features tests_exist"
110 };
111 REGISTER_TEST(tests_helpers_compile_without_features);