1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/htable/htable_type.h>
4 #include <ccan/foreach/foreach.h>
5 #include <ccan/str/str.h>
6 #include <ccan/hash/hash.h>
7 #include <ccan/read_write_all/read_write_all.h>
10 #include <sys/types.h>
14 #include "reduce_features.h"
16 bool features_were_reduced;
18 static const char *can_run(struct manifest *m)
21 return tal_strdup(m, "Could not read config.h");
25 static size_t option_hash(const char *name)
27 return hash(name, strlen(name), 0);
30 static const char *option_name(const char *name)
35 static bool option_cmp(const char *name1, const char *name2)
37 return streq(name1, name2);
40 HTABLE_DEFINE_TYPE(char, option_name, option_hash, option_cmp, htable_option);
42 static struct htable_option *htable_option_new(void)
44 struct htable_option *opts = malloc(sizeof(*opts));
45 htable_option_init(opts);
49 static void htable_option_free(struct htable_option *opts)
51 htable_option_clear(opts);
55 static unsigned int add_options(struct htable_option *opts,
56 struct pp_conditions *cond)
60 num += add_options(opts, cond->parent);
61 if (cond->type == PP_COND_IF || cond->type == PP_COND_IFDEF) {
62 if (strstarts(cond->symbol, "HAVE_")) {
63 if (!htable_option_get(opts, cond->symbol)) {
64 htable_option_add(opts, cond->symbol);
72 static struct htable_option *get_used_options(struct manifest *m)
74 struct list_head *list;
77 struct htable_option *opts = htable_option_new();
78 struct line_info *info;
81 foreach_ptr(list, &m->c_files, &m->h_files) {
82 list_for_each(list, f, list) {
83 info = get_ccan_line_info(f);
84 struct pp_conditions *prev = NULL;
86 for (i = 0; f->lines[i]; i++) {
87 if (info[i].cond && info[i].cond != prev) {
88 num += add_options(opts, info[i].cond);
96 htable_option_free(opts);
102 static struct htable_option *get_config_options(struct manifest *m)
104 const char **lines = (const char **)tal_strsplit(m, config_header, "\n",
107 struct htable_option *opts = htable_option_new();
109 for (i = 0; i < tal_count(lines) - 1; i++) {
112 if (!get_token(&lines[i], "#"))
114 if (!get_token(&lines[i], "define"))
116 sym = get_symbol_token(lines, &lines[i]);
117 if (!strstarts(sym, "HAVE_"))
119 /* Don't override endian... */
120 if (strends(sym, "_ENDIAN"))
122 /* Don't override HAVE_STRUCT_TIMESPEC. */
123 if (streq(sym, "HAVE_STRUCT_TIMESPEC"))
125 if (!get_token(&lines[i], "1"))
127 htable_option_add(opts, sym);
132 static void do_reduce_features(struct manifest *m,
133 unsigned int *timeleft UNNEEDED,
136 struct htable_option *options_used, *options_avail, *options;
137 struct htable_option_iter i;
142 /* This isn't really a test, as such. */
146 options_used = get_used_options(m);
150 options_avail = get_config_options(m);
153 for (sym = htable_option_first(options_used, &i);
155 sym = htable_option_next(options_used, &i)) {
156 if (htable_option_get(options_avail, sym)) {
158 options = htable_option_new();
159 htable_option_add(options, sym);
162 htable_option_free(options_avail);
163 htable_option_free(options_used);
168 /* Now make our own config.h variant, with our own options. */
169 hdr = tal_strcat(m, "/* Modified by reduce_features */\n",
171 for (sym = htable_option_first(options, &i);
173 sym = htable_option_next(options, &i)) {
174 tal_append_fmt(&hdr, "#undef %s\n#define %s 0\n", sym, sym);
176 if (mkdir("reduced-features", 0700) != 0 && errno != EEXIST)
177 err(1, "Creating reduced-features directory");
179 fd = open("reduced-features/config.h", O_TRUNC|O_CREAT|O_RDWR, 0600);
181 err(1, "Creating reduced-features/config.h");
182 if (!write_all(fd, hdr, strlen(hdr)))
183 err(1, "Writing reduced-features/config.h");
184 htable_option_free(options);
186 features_were_reduced = true;
189 struct ccanlint reduce_features = {
190 .key = "reduce_features",
191 .name = "Produce config.h with reduced features",
193 .check = do_reduce_features,
194 /* We only want to compile up versions with reduced featuress once
195 * objects for normal tests are built. */
196 .needs = "tests_compile"
198 REGISTER_TEST(reduce_features);