blob: 149e27aec564a3acd102ab2d8f90c34db00094f9 [file] [log] [blame]
Patrick Georgi0588d192009-08-12 15:00:51 +00001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <sys/stat.h>
7#include <ctype.h>
Patrick Georgid5208402014-04-11 20:24:06 +02008#include <errno.h>
Patrick Georgi0588d192009-08-12 15:00:51 +00009#include <fcntl.h>
Patrick Georgid5208402014-04-11 20:24:06 +020010#include <stdarg.h>
Patrick Georgi0588d192009-08-12 15:00:51 +000011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <unistd.h>
16
Patrick Georgi0588d192009-08-12 15:00:51 +000017#include "lkc.h"
18
19static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
21
Patrick Georgid5208402014-04-11 20:24:06 +020022static void conf_message(const char *fmt, ...)
23 __attribute__ ((format (printf, 1, 2)));
24
Patrick Georgi0588d192009-08-12 15:00:51 +000025static const char *conf_filename;
26static int conf_lineno, conf_warnings, conf_unsaved;
27
28const char conf_defname[] = "arch/$ARCH/defconfig";
29
30static void conf_warning(const char *fmt, ...)
31{
32 va_list ap;
33 va_start(ap, fmt);
34 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
35 vfprintf(stderr, fmt, ap);
36 fprintf(stderr, "\n");
37 va_end(ap);
38 conf_warnings++;
39}
40
Patrick Georgid5208402014-04-11 20:24:06 +020041static void conf_default_message_callback(const char *fmt, va_list ap)
42{
43 printf("#\n# ");
44 vprintf(fmt, ap);
45 printf("\n#\n");
46}
47
48static void (*conf_message_callback) (const char *fmt, va_list ap) =
49 conf_default_message_callback;
50void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
51{
52 conf_message_callback = fn;
53}
54
55static void conf_message(const char *fmt, ...)
56{
57 va_list ap;
58
59 va_start(ap, fmt);
60 if (conf_message_callback)
61 conf_message_callback(fmt, ap);
62}
63
Patrick Georgi0588d192009-08-12 15:00:51 +000064const char *conf_get_configname(void)
65{
66 char *name = getenv("KCONFIG_CONFIG");
67
68 return name ? name : ".config";
69}
70
Patrick Georgid5208402014-04-11 20:24:06 +020071const char *conf_get_autoconfig_name(void)
72{
73 char *name = getenv("KCONFIG_AUTOCONFIG");
74
75 return name ? name : "include/config/auto.conf";
76}
77
Patrick Georgi0588d192009-08-12 15:00:51 +000078static char *conf_expand_value(const char *in)
79{
80 struct symbol *sym;
81 const char *src;
82 static char res_value[SYMBOL_MAXLENGTH];
83 char *dst, name[SYMBOL_MAXLENGTH];
84
85 res_value[0] = 0;
86 dst = name;
87 while ((src = strchr(in, '$'))) {
88 strncat(res_value, in, src - in);
89 src++;
90 dst = name;
91 while (isalnum(*src) || *src == '_')
92 *dst++ = *src++;
93 *dst = 0;
94 sym = sym_lookup(name, 0);
95 sym_calc_value(sym);
96 strcat(res_value, sym_get_string_value(sym));
97 in = src;
98 }
99 strcat(res_value, in);
100
101 return res_value;
102}
103
104char *conf_get_default_confname(void)
105{
106 struct stat buf;
107 static char fullname[PATH_MAX+1];
108 char *env, *name;
109
110 name = conf_expand_value(conf_defname);
111 env = getenv(SRCTREE);
112 if (env) {
113 sprintf(fullname, "%s/%s", env, name);
114 if (!stat(fullname, &buf))
115 return fullname;
116 }
117 return name;
118}
119
120static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
121{
122 char *p2;
123
124 switch (sym->type) {
125 case S_TRISTATE:
126 if (p[0] == 'm') {
127 sym->def[def].tri = mod;
128 sym->flags |= def_flags;
129 break;
130 }
Patrick Georgid5208402014-04-11 20:24:06 +0200131 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000132 case S_BOOLEAN:
133 if (p[0] == 'y') {
134 sym->def[def].tri = yes;
135 sym->flags |= def_flags;
136 break;
137 }
138 if (p[0] == 'n') {
139 sym->def[def].tri = no;
140 sym->flags |= def_flags;
141 break;
142 }
Patrick Georgid5208402014-04-11 20:24:06 +0200143 if (def != S_DEF_AUTO)
144 conf_warning("symbol value '%s' invalid for %s",
145 p, sym->name);
146 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000147 case S_OTHER:
148 if (*p != '"') {
149 for (p2 = p; *p2 && !isspace(*p2); p2++)
150 ;
151 sym->type = S_STRING;
152 goto done;
153 }
Patrick Georgid5208402014-04-11 20:24:06 +0200154 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000155 case S_STRING:
156 if (*p++ != '"')
157 break;
158 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
159 if (*p2 == '"') {
160 *p2 = 0;
161 break;
162 }
163 memmove(p2, p2 + 1, strlen(p2));
164 }
165 if (!p2) {
Patrick Georgid5208402014-04-11 20:24:06 +0200166 if (def != S_DEF_AUTO)
167 conf_warning("invalid string found");
Patrick Georgi0588d192009-08-12 15:00:51 +0000168 return 1;
169 }
Patrick Georgid5208402014-04-11 20:24:06 +0200170 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000171 case S_INT:
172 case S_HEX:
173 done:
174 if (sym_string_valid(sym, p)) {
175 sym->def[def].val = strdup(p);
176 sym->flags |= def_flags;
177 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200178 if (def != S_DEF_AUTO)
179 conf_warning("symbol value '%s' invalid for %s",
180 p, sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000181 return 1;
182 }
183 break;
184 default:
185 ;
186 }
187 return 0;
188}
189
Patrick Georgid5208402014-04-11 20:24:06 +0200190#define LINE_GROWTH 16
191static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
192{
193 char *nline;
194 size_t new_size = slen + 1;
195 if (new_size > *n) {
196 new_size += LINE_GROWTH - 1;
197 new_size *= 2;
198 nline = realloc(*lineptr, new_size);
199 if (!nline)
200 return -1;
201
202 *lineptr = nline;
203 *n = new_size;
204 }
205
206 (*lineptr)[slen] = c;
207
208 return 0;
209}
210
211static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
212{
213 char *line = *lineptr;
214 size_t slen = 0;
215
216 for (;;) {
217 int c = getc(stream);
218
219 switch (c) {
220 case '\n':
221 if (add_byte(c, &line, slen, n) < 0)
222 goto e_out;
223 slen++;
224 /* fall through */
225 case EOF:
226 if (add_byte('\0', &line, slen, n) < 0)
227 goto e_out;
228 *lineptr = line;
229 if (slen == 0)
230 return -1;
231 return slen;
232 default:
233 if (add_byte(c, &line, slen, n) < 0)
234 goto e_out;
235 slen++;
236 }
237 }
238
239e_out:
240 line[slen-1] = '\0';
241 *lineptr = line;
242 return -1;
243}
244
Patrick Georgi0588d192009-08-12 15:00:51 +0000245int conf_read_simple(const char *name, int def)
246{
247 FILE *in = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200248 char *line = NULL;
249 size_t line_asize = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000250 char *p, *p2;
251 struct symbol *sym;
252 int i, def_flags;
253
254 if (name) {
255 in = zconf_fopen(name);
256 } else {
257 struct property *prop;
258
259 name = conf_get_configname();
260 in = zconf_fopen(name);
261 if (in)
262 goto load;
263 sym_add_change_count(1);
Patrick Georgid5208402014-04-11 20:24:06 +0200264 if (!sym_defconfig_list) {
265 if (modules_sym)
266 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000267 return 1;
Patrick Georgid5208402014-04-11 20:24:06 +0200268 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000269
270 for_all_defaults(sym_defconfig_list, prop) {
271 if (expr_calc_value(prop->visible.expr) == no ||
272 prop->expr->type != E_SYMBOL)
273 continue;
274 name = conf_expand_value(prop->expr->left.sym->name);
275 in = zconf_fopen(name);
276 if (in) {
Patrick Georgid5208402014-04-11 20:24:06 +0200277 conf_message(_("using defaults found in %s"),
278 name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000279 goto load;
280 }
281 }
282 }
283 if (!in)
284 return 1;
285
286load:
287 conf_filename = name;
288 conf_lineno = 0;
289 conf_warnings = 0;
290 conf_unsaved = 0;
291
292 def_flags = SYMBOL_DEF << def;
293 for_all_symbols(i, sym) {
294 sym->flags |= SYMBOL_CHANGED;
295 sym->flags &= ~(def_flags|SYMBOL_VALID);
296 if (sym_is_choice(sym))
297 sym->flags |= def_flags;
298 switch (sym->type) {
299 case S_INT:
300 case S_HEX:
301 case S_STRING:
302 if (sym->def[def].val)
303 free(sym->def[def].val);
Patrick Georgid5208402014-04-11 20:24:06 +0200304 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000305 default:
306 sym->def[def].val = NULL;
307 sym->def[def].tri = no;
308 }
309 }
310
Patrick Georgid5208402014-04-11 20:24:06 +0200311 while (compat_getline(&line, &line_asize, in) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000312 conf_lineno++;
313 sym = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200314 if (line[0] == '#') {
315 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Patrick Georgi0588d192009-08-12 15:00:51 +0000316 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200317 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Patrick Georgi0588d192009-08-12 15:00:51 +0000318 if (!p)
319 continue;
320 *p++ = 0;
321 if (strncmp(p, "is not set", 10))
322 continue;
323 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200324 sym = sym_find(line + 2 + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000325 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200326 sym_add_change_count(1);
327 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000328 }
329 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200330 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000331 if (sym->type == S_UNKNOWN)
332 sym->type = S_BOOLEAN;
333 }
334 if (sym->flags & def_flags) {
335 conf_warning("override: reassigning to symbol %s", sym->name);
336 }
337 switch (sym->type) {
338 case S_BOOLEAN:
339 case S_TRISTATE:
340 sym->def[def].tri = no;
341 sym->flags |= def_flags;
342 break;
343 default:
344 ;
345 }
Patrick Georgid5208402014-04-11 20:24:06 +0200346 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
347 p = strchr(line + strlen(CONFIG_), '=');
Patrick Georgi0588d192009-08-12 15:00:51 +0000348 if (!p)
349 continue;
350 *p++ = 0;
351 p2 = strchr(p, '\n');
352 if (p2) {
353 *p2-- = 0;
354 if (*p2 == '\r')
355 *p2 = 0;
356 }
357 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200358 sym = sym_find(line + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000359 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200360 sym_add_change_count(1);
361 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000362 }
363 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200364 sym = sym_lookup(line + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000365 if (sym->type == S_UNKNOWN)
366 sym->type = S_OTHER;
367 }
368 if (sym->flags & def_flags) {
369 conf_warning("override: reassigning to symbol %s", sym->name);
370 }
371 if (conf_set_sym_val(sym, def, def_flags, p))
372 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200373 } else {
374 if (line[0] != '\r' && line[0] != '\n')
375 conf_warning("unexpected data");
Patrick Georgi0588d192009-08-12 15:00:51 +0000376 continue;
377 }
Patrick Georgid5208402014-04-11 20:24:06 +0200378setsym:
Patrick Georgi0588d192009-08-12 15:00:51 +0000379 if (sym && sym_is_choice_value(sym)) {
380 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
381 switch (sym->def[def].tri) {
382 case no:
383 break;
384 case mod:
385 if (cs->def[def].tri == yes) {
386 conf_warning("%s creates inconsistent choice state", sym->name);
387 cs->flags &= ~def_flags;
388 }
389 break;
390 case yes:
391 if (cs->def[def].tri != no)
392 conf_warning("override: %s changes choice state", sym->name);
393 cs->def[def].val = sym;
394 break;
395 }
396 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
397 }
398 }
Patrick Georgid5208402014-04-11 20:24:06 +0200399 free(line);
Patrick Georgi0588d192009-08-12 15:00:51 +0000400 fclose(in);
401
402 if (modules_sym)
403 sym_calc_value(modules_sym);
404 return 0;
405}
406
407int conf_read(const char *name)
408{
Patrick Georgid5208402014-04-11 20:24:06 +0200409 struct symbol *sym;
410 int i;
Patrick Georgi0588d192009-08-12 15:00:51 +0000411
412 sym_set_change_count(0);
413
414 if (conf_read_simple(name, S_DEF_USER))
415 return 1;
416
417 for_all_symbols(i, sym) {
418 sym_calc_value(sym);
419 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
Patrick Georgid5208402014-04-11 20:24:06 +0200420 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000421 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
422 /* check that calculated value agrees with saved value */
423 switch (sym->type) {
424 case S_BOOLEAN:
425 case S_TRISTATE:
426 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
427 break;
428 if (!sym_is_choice(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200429 continue;
430 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000431 default:
432 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Patrick Georgid5208402014-04-11 20:24:06 +0200433 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000434 break;
435 }
436 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
437 /* no previous value and not saved */
Patrick Georgid5208402014-04-11 20:24:06 +0200438 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000439 conf_unsaved++;
440 /* maybe print value in verbose mode... */
Patrick Georgi0588d192009-08-12 15:00:51 +0000441 }
442
443 for_all_symbols(i, sym) {
444 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
445 /* Reset values of generates values, so they'll appear
446 * as new, if they should become visible, but that
447 * doesn't quite work if the Kconfig and the saved
448 * configuration disagree.
449 */
450 if (sym->visible == no && !conf_unsaved)
451 sym->flags &= ~SYMBOL_DEF_USER;
452 switch (sym->type) {
453 case S_STRING:
454 case S_INT:
455 case S_HEX:
456 /* Reset a string value if it's out of range */
457 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
458 break;
459 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
460 conf_unsaved++;
461 break;
462 default:
463 break;
464 }
465 }
466 }
467
468 sym_add_change_count(conf_warnings || conf_unsaved);
469
470 return 0;
471}
472
Patrick Georgid5208402014-04-11 20:24:06 +0200473/*
474 * Kconfig configuration printer
475 *
476 * This printer is used when generating the resulting configuration after
477 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
478 * passing a non-NULL argument to the printer.
479 *
480 */
481static void
482kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
483{
484
485 switch (sym->type) {
486 case S_BOOLEAN:
487 case S_TRISTATE:
488 if (*value == 'n') {
489 bool skip_unset = (arg != NULL);
490
491 if (!skip_unset)
492 fprintf(fp, "# %s%s is not set\n",
493 CONFIG_, sym->name);
494 return;
495 }
496 break;
497 default:
498 break;
499 }
500
501 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
502}
503
504static void
505kconfig_print_comment(FILE *fp, const char *value, void *arg)
506{
507 const char *p = value;
508 size_t l;
509
510 for (;;) {
511 l = strcspn(p, "\n");
512 fprintf(fp, "#");
513 if (l) {
514 fprintf(fp, " ");
515 xfwrite(p, l, 1, fp);
516 p += l;
517 }
518 fprintf(fp, "\n");
519 if (*p++ == '\0')
520 break;
521 }
522}
523
524static struct conf_printer kconfig_printer_cb =
525{
526 .print_symbol = kconfig_print_symbol,
527 .print_comment = kconfig_print_comment,
528};
529
530/*
531 * Header printer
532 *
533 * This printer is used when generating the `include/generated/autoconf.h' file.
534 */
535static void
536header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
537{
538
539 switch (sym->type) {
540 case S_BOOLEAN:
541 case S_TRISTATE: {
542 const char *suffix = "";
543
544 switch (*value) {
545 case 'n':
546 if (getenv("KCONFIG_NEGATIVES")) {
547 fprintf(fp, "#define %s%s%s 0\n",
548 CONFIG_, sym->name, suffix);
549 }
550 break;
551 case 'm':
552 suffix = "_MODULE";
553 /* fall through */
554 default:
555 fprintf(fp, "#define %s%s%s 1\n",
556 CONFIG_, sym->name, suffix);
557 }
558 break;
559 }
560 case S_HEX: {
561 const char *prefix = "";
562
563 if (!value || (value[0] == '\0')) {
564 value = "0";
565 } else
566 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
567 prefix = "0x";
568 fprintf(fp, "#define %s%s %s%s\n",
569 CONFIG_, sym->name, prefix, value);
570 break;
571 }
572 case S_INT:
573 if (!value || (value[0] == '\0')) {
574 value = "0";
575 }
576 /* fall through */
577 case S_STRING:
578 fprintf(fp, "#define %s%s %s\n",
579 CONFIG_, sym->name, value);
580 break;
581 default:
582 break;
583 }
584
585}
586
587static void
588header_print_comment(FILE *fp, const char *value, void *arg)
589{
590 const char *p = value;
591 size_t l;
592
593 fprintf(fp, "/*\n");
594 for (;;) {
595 l = strcspn(p, "\n");
596 fprintf(fp, " *");
597 if (l) {
598 fprintf(fp, " ");
599 xfwrite(p, l, 1, fp);
600 p += l;
601 }
602 fprintf(fp, "\n");
603 if (*p++ == '\0')
604 break;
605 }
606 fprintf(fp, " */\n");
607}
608
609static struct conf_printer header_printer_cb =
610{
611 .print_symbol = header_print_symbol,
612 .print_comment = header_print_comment,
613};
614
615/*
616 * Tristate printer
617 *
618 * This printer is used when generating the `include/config/tristate.conf' file.
619 */
620static void
621tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
622{
623
624 if (sym->type == S_TRISTATE && (*value != 'n' || arg == NULL))
625 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
626}
627
628static struct conf_printer tristate_printer_cb =
629{
630 .print_symbol = tristate_print_symbol,
631 .print_comment = kconfig_print_comment,
632};
633
634static void conf_write_symbol(FILE *fp, struct symbol *sym,
635 struct conf_printer *printer, void *printer_arg)
636{
637 const char *str;
638
639 switch (sym->type) {
640 case S_OTHER:
641 case S_UNKNOWN:
642 break;
643 case S_STRING:
644 str = sym_get_string_value(sym);
645 str = sym_escape_string_value(str);
646 printer->print_symbol(fp, sym, str, printer_arg);
647 free((void *)str);
648 break;
649 default:
650 str = sym_get_string_value(sym);
651 printer->print_symbol(fp, sym, str, printer_arg);
652 }
653}
654
655static void
656conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
657{
658 char buf[256];
659
660 snprintf(buf, sizeof(buf),
661 "\n"
662 "Automatically generated file; DO NOT EDIT.\n"
663 "%s\n",
664 rootmenu.prompt->text);
665
666 printer->print_comment(fp, buf, printer_arg);
667}
668
669/*
670 * Write out a minimal config.
671 * All values that has default values are skipped as this is redundant.
672 */
673int conf_write_defconfig(const char *filename)
674{
675 struct symbol *sym;
676 struct menu *menu;
677 FILE *out;
678
679 out = fopen(filename, "w");
680 if (!out)
681 return 1;
682
683 sym_clear_all_valid();
684
685 /* Traverse all menus to find all relevant symbols */
686 menu = rootmenu.list;
687
688 while (menu != NULL)
689 {
690 sym = menu->sym;
691 if (sym == NULL) {
692 if (!menu_is_visible(menu))
693 goto next_menu;
694 } else if (!sym_is_choice(sym)) {
695 sym_calc_value(sym);
696 if (!(sym->flags & SYMBOL_WRITE))
697 goto next_menu;
698 sym->flags &= ~SYMBOL_WRITE;
699 /* If we cannot change the symbol - skip */
700 if (!sym_is_changable(sym))
701 goto next_menu;
702 /* If symbol equals to default value - skip */
703 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
704 goto next_menu;
705
706 /*
707 * If symbol is a choice value and equals to the
708 * default for a choice - skip.
709 * But only if value is bool and equal to "y" and
710 * choice is not "optional".
711 * (If choice is "optional" then all values can be "n")
712 */
713 if (sym_is_choice_value(sym)) {
714 struct symbol *cs;
715 struct symbol *ds;
716
717 cs = prop_get_symbol(sym_get_choice_prop(sym));
718 ds = sym_choice_default(cs);
719 if (!sym_is_optional(cs) && sym == ds) {
720 if ((sym->type == S_BOOLEAN) &&
721 sym_get_tristate_value(sym) == yes)
722 goto next_menu;
723 }
724 }
725 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
726 }
727next_menu:
728 if (menu->list != NULL) {
729 menu = menu->list;
730 }
731 else if (menu->next != NULL) {
732 menu = menu->next;
733 } else {
734 while ((menu = menu->parent)) {
735 if (menu->next != NULL) {
736 menu = menu->next;
737 break;
738 }
739 }
740 }
741 }
742 fclose(out);
743 return 0;
744}
745
Patrick Georgi0588d192009-08-12 15:00:51 +0000746int conf_write(const char *name)
747{
748 FILE *out;
749 struct symbol *sym;
750 struct menu *menu;
751 const char *basename;
Patrick Georgi0588d192009-08-12 15:00:51 +0000752 const char *str;
Patrick Georgid5208402014-04-11 20:24:06 +0200753 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000754 char *env;
755
756 dirname[0] = 0;
757 if (name && name[0]) {
758 struct stat st;
759 char *slash;
760
761 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
762 strcpy(dirname, name);
763 strcat(dirname, "/");
764 basename = conf_get_configname();
765 } else if ((slash = strrchr(name, '/'))) {
766 int size = slash - name + 1;
767 memcpy(dirname, name, size);
768 dirname[size] = 0;
769 if (slash[1])
770 basename = slash + 1;
771 else
772 basename = conf_get_configname();
773 } else
774 basename = name;
775 } else
776 basename = conf_get_configname();
777
778 sprintf(newname, "%s%s", dirname, basename);
779 env = getenv("KCONFIG_OVERWRITECONFIG");
780 if (!env || !*env) {
781 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
782 out = fopen(tmpname, "w");
783 } else {
784 *tmpname = 0;
785 out = fopen(newname, "w");
786 }
787 if (!out)
788 return 1;
789
Patrick Georgid5208402014-04-11 20:24:06 +0200790 conf_write_heading(out, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000791
792 if (!conf_get_changed())
793 sym_clear_all_valid();
794
795 menu = rootmenu.list;
796 while (menu) {
797 sym = menu->sym;
798 if (!sym) {
799 if (!menu_is_visible(menu))
800 goto next;
801 str = menu_get_prompt(menu);
802 fprintf(out, "\n"
803 "#\n"
804 "# %s\n"
805 "#\n", str);
806 } else if (!(sym->flags & SYMBOL_CHOICE)) {
807 sym_calc_value(sym);
808 if (!(sym->flags & SYMBOL_WRITE))
809 goto next;
810 sym->flags &= ~SYMBOL_WRITE;
Patrick Georgid5208402014-04-11 20:24:06 +0200811
812 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000813 }
814
Patrick Georgid5208402014-04-11 20:24:06 +0200815next:
Patrick Georgi0588d192009-08-12 15:00:51 +0000816 if (menu->list) {
817 menu = menu->list;
818 continue;
819 }
820 if (menu->next)
821 menu = menu->next;
822 else while ((menu = menu->parent)) {
823 if (menu->next) {
824 menu = menu->next;
825 break;
826 }
827 }
828 }
829 fclose(out);
830
831 if (*tmpname) {
832 strcat(dirname, basename);
833 strcat(dirname, ".old");
834 rename(newname, dirname);
835 if (rename(tmpname, newname))
836 return 1;
837 }
838
Patrick Georgid5208402014-04-11 20:24:06 +0200839 conf_message(_("configuration written to %s"), newname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000840
841 sym_set_change_count(0);
842
843 return 0;
844}
845
Patrick Georgid5208402014-04-11 20:24:06 +0200846static int conf_split_config(void)
Patrick Georgi0588d192009-08-12 15:00:51 +0000847{
Patrick Georgid5208402014-04-11 20:24:06 +0200848 const char *name;
849 char path[PATH_MAX+1];
850 char pwd[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000851 char *s, *d, c;
852 struct symbol *sym;
853 struct stat sb;
854 int res, i, fd;
855
Patrick Georgid5208402014-04-11 20:24:06 +0200856 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +0000857 conf_read_simple(name, S_DEF_AUTO);
858
Patrick Georgid5208402014-04-11 20:24:06 +0200859 getcwd(pwd, sizeof(pwd));
860 name = getenv("KCONFIG_SPLITCONFIG");
861 if (!name)
862 name = "include/config";
863 if (chdir(name))
Patrick Georgi0588d192009-08-12 15:00:51 +0000864 return 1;
865
866 res = 0;
867 for_all_symbols(i, sym) {
868 sym_calc_value(sym);
869 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
870 continue;
871 if (sym->flags & SYMBOL_WRITE) {
872 if (sym->flags & SYMBOL_DEF_AUTO) {
873 /*
874 * symbol has old and new value,
875 * so compare them...
876 */
877 switch (sym->type) {
878 case S_BOOLEAN:
879 case S_TRISTATE:
880 if (sym_get_tristate_value(sym) ==
881 sym->def[S_DEF_AUTO].tri)
882 continue;
883 break;
884 case S_STRING:
885 case S_HEX:
886 case S_INT:
887 if (!strcmp(sym_get_string_value(sym),
888 sym->def[S_DEF_AUTO].val))
889 continue;
890 break;
891 default:
892 break;
893 }
894 } else {
895 /*
896 * If there is no old value, only 'no' (unset)
897 * is allowed as new value.
898 */
899 switch (sym->type) {
900 case S_BOOLEAN:
901 case S_TRISTATE:
902 if (sym_get_tristate_value(sym) == no)
903 continue;
904 break;
905 default:
906 break;
907 }
908 }
909 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
910 /* There is neither an old nor a new value. */
911 continue;
912 /* else
913 * There is an old value, but no new value ('no' (unset)
914 * isn't saved in auto.conf, so the old value is always
915 * different from 'no').
916 */
917
918 /* Replace all '_' and append ".h" */
919 s = sym->name;
920 d = path;
921 while ((c = *s++)) {
922 c = tolower(c);
923 *d++ = (c == '_') ? '/' : c;
924 }
925 strcpy(d, ".h");
926
927 /* Assume directory path already exists. */
928 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
929 if (fd == -1) {
930 if (errno != ENOENT) {
931 res = 1;
932 break;
933 }
934 /*
935 * Create directory components,
936 * unless they exist already.
937 */
938 d = path;
939 while ((d = strchr(d, '/'))) {
940 *d = 0;
941 if (stat(path, &sb) && mkdir(path, 0755)) {
942 res = 1;
943 goto out;
944 }
945 *d++ = '/';
946 }
947 /* Try it again. */
948 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
949 if (fd == -1) {
950 res = 1;
951 break;
952 }
953 }
954 close(fd);
955 }
956out:
Patrick Georgid5208402014-04-11 20:24:06 +0200957 if (chdir(pwd))
Patrick Georgi0588d192009-08-12 15:00:51 +0000958 return 1;
959
960 return res;
961}
962
963int conf_write_autoconf(void)
964{
965 struct symbol *sym;
Patrick Georgid5208402014-04-11 20:24:06 +0200966 const char *name;
967 FILE *out, *tristate, *out_h;
968 int i;
969 int print_negatives;
970
971 print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +0000972
973 sym_clear_all_valid();
974
Patrick Georgid5208402014-04-11 20:24:06 +0200975 char *dep_file = getenv("KCONFIG_DEPENDENCIES");
976 if (!dep_file)
977 dep_file = "include/config/auto.conf.cmd";
978 file_write_dep(dep_file);
Vadim Bendeburyc302d202011-10-24 14:06:23 -0700979
Patrick Georgi0588d192009-08-12 15:00:51 +0000980 if (conf_split_config())
981 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000982
Patrick Georgi129462d2014-11-17 14:56:49 +0100983 char *tmpconfig_name = malloc(PATH_MAX);
984 if (getenv("COREBOOT_BUILD_DIR")) {
985 sprintf(tmpconfig_name, "%s/.tmpconfig.XXXXXX",
986 getenv("COREBOOT_BUILD_DIR"));
987 } else {
988 tmpconfig_name = strdup(".tmpconfig.XXXXXX");
989 }
Patrick Georgid5208402014-04-11 20:24:06 +0200990 if ((i = mkstemp(tmpconfig_name)) == -1)
991 return 1;
992 out = fdopen(i, "w");
Patrick Georgi0588d192009-08-12 15:00:51 +0000993 if (!out)
994 return 1;
995
Patrick Georgi129462d2014-11-17 14:56:49 +0100996 char *tmpconfig_triname = malloc(PATH_MAX);
997 if (getenv("COREBOOT_BUILD_DIR")) {
998 sprintf(tmpconfig_triname, "%s/.tmpconfig_tristate.XXXXXX",
999 getenv("COREBOOT_BUILD_DIR"));
1000 } else {
1001 tmpconfig_triname = strdup(".tmpconfig_tristate.XXXXXX");
1002 }
Patrick Georgid5208402014-04-11 20:24:06 +02001003 if ((i = mkstemp(tmpconfig_triname)) == -1)
1004 return 1;
1005 tristate = fdopen(i, "w");
1006 if (!tristate) {
Patrick Georgi0588d192009-08-12 15:00:51 +00001007 fclose(out);
1008 return 1;
1009 }
1010
Patrick Georgi129462d2014-11-17 14:56:49 +01001011 char *tmpconfig_h = malloc(PATH_MAX);
1012 if (getenv("COREBOOT_BUILD_DIR")) {
1013 sprintf(tmpconfig_h, "%s/.tmpconfig_tristate.XXXXXX",
1014 getenv("COREBOOT_BUILD_DIR"));
1015 } else {
1016 tmpconfig_h = strdup(".tmpconfig_tristate.XXXXXX");
1017 }
Patrick Georgid5208402014-04-11 20:24:06 +02001018 if ((i = mkstemp(tmpconfig_h)) == -1)
1019 return 1;
1020 out_h = fdopen(i, "w");
1021 if (!out_h) {
1022 fclose(out);
1023 fclose(tristate);
1024 return 1;
1025 }
1026
1027 conf_write_heading(out, &kconfig_printer_cb, NULL);
1028
1029 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1030
1031 conf_write_heading(out_h, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001032
1033 for_all_symbols(i, sym) {
1034 sym_calc_value(sym);
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001035 if (!sym->name)
Patrick Georgi0588d192009-08-12 15:00:51 +00001036 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001037
1038 if (!(sym->flags & SYMBOL_WRITE) && !print_negatives)
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001039 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001040
1041 /* these are safe to write out, so do it all the time */
1042 if (!(sym->flags & SYMBOL_WRITE) &&
1043 !(sym->type == S_BOOLEAN ||
1044 sym->type == S_HEX ||
1045 sym->type == S_INT))
1046 continue;
1047
1048 /* write symbol to auto.conf, tristate and header files */
1049 conf_write_symbol(out, sym, &kconfig_printer_cb, print_negatives?NULL:(void *)1);
1050
1051 conf_write_symbol(tristate, sym, &tristate_printer_cb, print_negatives?NULL:(void *)1);
1052
1053 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001054 }
1055 fclose(out);
Patrick Georgid5208402014-04-11 20:24:06 +02001056 fclose(tristate);
Patrick Georgi0588d192009-08-12 15:00:51 +00001057 fclose(out_h);
1058
Patrick Georgid5208402014-04-11 20:24:06 +02001059 name = getenv("KCONFIG_AUTOHEADER");
Patrick Georgi0588d192009-08-12 15:00:51 +00001060 if (!name)
Patrick Georgid5208402014-04-11 20:24:06 +02001061 name = "include/generated/autoconf.h";
1062 if (rename(tmpconfig_h, name))
1063 return 1;
1064 name = getenv("KCONFIG_TRISTATE");
1065 if (!name)
1066 name = "include/config/tristate.conf";
1067 if (rename(tmpconfig_triname, name))
1068 return 1;
1069 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +00001070 /*
1071 * This must be the last step, kbuild has a dependency on auto.conf
1072 * and this marks the successful completion of the previous steps.
1073 */
Patrick Georgid5208402014-04-11 20:24:06 +02001074 if (rename(tmpconfig_name, name))
Patrick Georgi0588d192009-08-12 15:00:51 +00001075 return 1;
1076
Patrick Georgi0588d192009-08-12 15:00:51 +00001077 return 0;
1078}
1079
1080static int sym_change_count;
1081static void (*conf_changed_callback)(void);
1082
1083void sym_set_change_count(int count)
1084{
1085 int _sym_change_count = sym_change_count;
1086 sym_change_count = count;
1087 if (conf_changed_callback &&
1088 (bool)_sym_change_count != (bool)count)
1089 conf_changed_callback();
1090}
1091
1092void sym_add_change_count(int count)
1093{
1094 sym_set_change_count(count + sym_change_count);
1095}
1096
1097bool conf_get_changed(void)
1098{
1099 return sym_change_count;
1100}
1101
1102void conf_set_changed_callback(void (*fn)(void))
1103{
1104 conf_changed_callback = fn;
1105}
Patrick Georgid5208402014-04-11 20:24:06 +02001106
1107static bool randomize_choice_values(struct symbol *csym)
1108{
1109 struct property *prop;
1110 struct symbol *sym;
1111 struct expr *e;
1112 int cnt, def;
1113
1114 /*
1115 * If choice is mod then we may have more items selected
1116 * and if no then no-one.
1117 * In both cases stop.
1118 */
1119 if (csym->curr.tri != yes)
1120 return false;
1121
1122 prop = sym_get_choice_prop(csym);
1123
1124 /* count entries in choice block */
1125 cnt = 0;
1126 expr_list_for_each_sym(prop->expr, e, sym)
1127 cnt++;
1128
1129 /*
1130 * find a random value and set it to yes,
1131 * set the rest to no so we have only one set
1132 */
1133 def = (rand() % cnt);
1134
1135 cnt = 0;
1136 expr_list_for_each_sym(prop->expr, e, sym) {
1137 if (def == cnt++) {
1138 sym->def[S_DEF_USER].tri = yes;
1139 csym->def[S_DEF_USER].val = sym;
1140 }
1141 else {
1142 sym->def[S_DEF_USER].tri = no;
1143 }
1144 sym->flags |= SYMBOL_DEF_USER;
1145 /* clear VALID to get value calculated */
1146 sym->flags &= ~SYMBOL_VALID;
1147 }
1148 csym->flags |= SYMBOL_DEF_USER;
1149 /* clear VALID to get value calculated */
1150 csym->flags &= ~(SYMBOL_VALID);
1151
1152 return true;
1153}
1154
1155void set_all_choice_values(struct symbol *csym)
1156{
1157 struct property *prop;
1158 struct symbol *sym;
1159 struct expr *e;
1160
1161 prop = sym_get_choice_prop(csym);
1162
1163 /*
1164 * Set all non-assinged choice values to no
1165 */
1166 expr_list_for_each_sym(prop->expr, e, sym) {
1167 if (!sym_has_value(sym))
1168 sym->def[S_DEF_USER].tri = no;
1169 }
1170 csym->flags |= SYMBOL_DEF_USER;
1171 /* clear VALID to get value calculated */
1172 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1173}
1174
1175bool conf_set_all_new_symbols(enum conf_def_mode mode)
1176{
1177 struct symbol *sym, *csym;
1178 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1179 * pty: probability of tristate = y
1180 * ptm: probability of tristate = m
1181 */
1182
1183 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1184 * below, otherwise gcc whines about
1185 * -Wmaybe-uninitialized */
1186 if (mode == def_random) {
1187 int n, p[3];
1188 char *env = getenv("KCONFIG_PROBABILITY");
1189 n = 0;
1190 while( env && *env ) {
1191 char *endp;
1192 int tmp = strtol( env, &endp, 10 );
1193 if( tmp >= 0 && tmp <= 100 ) {
1194 p[n++] = tmp;
1195 } else {
1196 errno = ERANGE;
1197 perror( "KCONFIG_PROBABILITY" );
1198 exit( 1 );
1199 }
1200 env = (*endp == ':') ? endp+1 : endp;
1201 if( n >=3 ) {
1202 break;
1203 }
1204 }
1205 switch( n ) {
1206 case 1:
1207 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1208 break;
1209 case 2:
1210 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1211 break;
1212 case 3:
1213 pby = p[0]; pty = p[1]; ptm = p[2];
1214 break;
1215 }
1216
1217 if( pty+ptm > 100 ) {
1218 errno = ERANGE;
1219 perror( "KCONFIG_PROBABILITY" );
1220 exit( 1 );
1221 }
1222 }
1223 bool has_changed = false;
1224
1225 for_all_symbols(i, sym) {
1226 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1227 continue;
1228 switch (sym_get_type(sym)) {
1229 case S_BOOLEAN:
1230 case S_TRISTATE:
1231 has_changed = true;
1232 switch (mode) {
1233 case def_yes:
1234 sym->def[S_DEF_USER].tri = yes;
1235 break;
1236 case def_mod:
1237 sym->def[S_DEF_USER].tri = mod;
1238 break;
1239 case def_no:
1240 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1241 sym->def[S_DEF_USER].tri = yes;
1242 else
1243 sym->def[S_DEF_USER].tri = no;
1244 break;
1245 case def_random:
1246 sym->def[S_DEF_USER].tri = no;
1247 cnt = rand() % 100;
1248 if (sym->type == S_TRISTATE) {
1249 if (cnt < pty)
1250 sym->def[S_DEF_USER].tri = yes;
1251 else if (cnt < (pty+ptm))
1252 sym->def[S_DEF_USER].tri = mod;
1253 } else if (cnt < pby)
1254 sym->def[S_DEF_USER].tri = yes;
1255 break;
1256 default:
1257 continue;
1258 }
1259 if (!(sym_is_choice(sym) && mode == def_random))
1260 sym->flags |= SYMBOL_DEF_USER;
1261 break;
1262 default:
1263 break;
1264 }
1265
1266 }
1267
1268 sym_clear_all_valid();
1269
1270 /*
1271 * We have different type of choice blocks.
1272 * If curr.tri equals to mod then we can select several
1273 * choice symbols in one block.
1274 * In this case we do nothing.
1275 * If curr.tri equals yes then only one symbol can be
1276 * selected in a choice block and we set it to yes,
1277 * and the rest to no.
1278 */
1279 if (mode != def_random) {
1280 for_all_symbols(i, csym) {
1281 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1282 sym_is_choice_value(csym))
1283 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1284 }
1285 }
1286
1287 for_all_symbols(i, csym) {
1288 if (sym_has_value(csym) || !sym_is_choice(csym))
1289 continue;
1290
1291 sym_calc_value(csym);
1292 if (mode == def_random)
1293 has_changed = randomize_choice_values(csym);
1294 else {
1295 set_all_choice_values(csym);
1296 has_changed = true;
1297 }
1298 }
1299
1300 return has_changed;
1301}