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/talloc/talloc.h>
6 #include <ccan/str/str.h>
7 #include <ccan/str_talloc/str_talloc.h>
8 #include <ccan/hash/hash.h>
9 #include <ccan/read_write_all/read_write_all.h>
12 #include <sys/types.h>
16 #include "reduce_features.h"
18 bool features_were_reduced;
20 static const char *can_run(struct manifest *m)
23 return talloc_strdup(m, "Could not read config.h");
27 static size_t option_hash(const char *name)
29 return hash(name, strlen(name), 0);
32 static const char *option_name(const char *name)
37 static bool option_cmp(const char *name1, const char *name2)
39 return streq(name1, name2);
42 HTABLE_DEFINE_TYPE(char, option_name, option_hash, option_cmp, htable_option);
44 static struct htable_option *htable_option_new(void)
46 struct htable_option *opts = malloc(sizeof(*opts));
47 htable_option_init(opts);
51 static void htable_option_free(struct htable_option *opts)
53 htable_option_clear(opts);
57 static unsigned int add_options(struct htable_option *opts,
58 struct pp_conditions *cond)
62 num += add_options(opts, cond->parent);
63 if (cond->type == PP_COND_IF || cond->type == PP_COND_IFDEF) {
64 if (strstarts(cond->symbol, "HAVE_")) {
65 if (!htable_option_get(opts, cond->symbol)) {
66 htable_option_add(opts, cond->symbol);
74 static struct htable_option *get_used_options(struct manifest *m)
76 struct list_head *list;
79 struct htable_option *opts = htable_option_new();
80 struct line_info *info;
83 foreach_ptr(list, &m->c_files, &m->h_files) {
84 list_for_each(list, f, list) {
85 info = get_ccan_line_info(f);
86 struct pp_conditions *prev = NULL;
88 for (i = 0; i < f->num_lines; i++) {
89 if (info[i].cond && info[i].cond != prev) {
90 num += add_options(opts, info[i].cond);
98 htable_option_free(opts);
104 static struct htable_option *get_config_options(struct manifest *m)
106 const char **lines = (const char **)strsplit(m, config_header, "\n");
108 struct htable_option *opts = htable_option_new();
110 for (i = 0; i < talloc_array_length(lines) - 1; i++) {
113 if (!get_token(&lines[i], "#"))
115 if (!get_token(&lines[i], "define"))
117 sym = get_symbol_token(lines, &lines[i]);
118 if (!strstarts(sym, "HAVE_"))
120 /* Don't override endian... */
121 if (strends(sym, "_ENDIAN"))
123 /* Don't override HAVE_STRUCT_TIMESPEC. */
124 if (streq(sym, "HAVE_STRUCT_TIMESPEC"))
126 if (!get_token(&lines[i], "1"))
128 htable_option_add(opts, sym);
133 static void do_reduce_features(struct manifest *m,
134 unsigned int *timeleft, struct score *score)
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 = talloc_strdup(m, "/* Modified by reduce_features */\n");
170 hdr = talloc_append_string(hdr, config_header);
171 for (sym = htable_option_first(options, &i);
173 sym = htable_option_next(options, &i)) {
174 hdr = talloc_asprintf_append
175 (hdr, "#undef %s\n#define %s 0\n", sym, sym);
177 if (mkdir("reduced-features", 0700) != 0 && errno != EEXIST)
178 err(1, "Creating reduced-features directory");
180 fd = open("reduced-features/config.h", O_TRUNC|O_CREAT|O_RDWR, 0600);
182 err(1, "Creating reduced-features/config.h");
183 if (!write_all(fd, hdr, strlen(hdr)))
184 err(1, "Writing reduced-features/config.h");
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);