blob: 235debf614617789d91766950dce571468377be3 [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);
Stefan Reinauercce66622015-04-06 01:14:21 +020062 va_end(ap);
Patrick Georgid5208402014-04-11 20:24:06 +020063}
64
Patrick Georgi0588d192009-08-12 15:00:51 +000065const char *conf_get_configname(void)
66{
67 char *name = getenv("KCONFIG_CONFIG");
68
69 return name ? name : ".config";
70}
71
Patrick Georgid5208402014-04-11 20:24:06 +020072const char *conf_get_autoconfig_name(void)
73{
74 char *name = getenv("KCONFIG_AUTOCONFIG");
75
76 return name ? name : "include/config/auto.conf";
77}
78
Patrick Georgi0588d192009-08-12 15:00:51 +000079static char *conf_expand_value(const char *in)
80{
81 struct symbol *sym;
82 const char *src;
83 static char res_value[SYMBOL_MAXLENGTH];
84 char *dst, name[SYMBOL_MAXLENGTH];
85
86 res_value[0] = 0;
87 dst = name;
88 while ((src = strchr(in, '$'))) {
89 strncat(res_value, in, src - in);
90 src++;
91 dst = name;
92 while (isalnum(*src) || *src == '_')
93 *dst++ = *src++;
94 *dst = 0;
95 sym = sym_lookup(name, 0);
96 sym_calc_value(sym);
97 strcat(res_value, sym_get_string_value(sym));
98 in = src;
99 }
100 strcat(res_value, in);
101
102 return res_value;
103}
104
105char *conf_get_default_confname(void)
106{
107 struct stat buf;
108 static char fullname[PATH_MAX+1];
109 char *env, *name;
110
111 name = conf_expand_value(conf_defname);
112 env = getenv(SRCTREE);
113 if (env) {
114 sprintf(fullname, "%s/%s", env, name);
115 if (!stat(fullname, &buf))
116 return fullname;
117 }
118 return name;
119}
120
121static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
122{
123 char *p2;
124
125 switch (sym->type) {
126 case S_TRISTATE:
127 if (p[0] == 'm') {
128 sym->def[def].tri = mod;
129 sym->flags |= def_flags;
130 break;
131 }
Patrick Georgid5208402014-04-11 20:24:06 +0200132 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000133 case S_BOOLEAN:
134 if (p[0] == 'y') {
135 sym->def[def].tri = yes;
136 sym->flags |= def_flags;
137 break;
138 }
139 if (p[0] == 'n') {
140 sym->def[def].tri = no;
141 sym->flags |= def_flags;
142 break;
143 }
Patrick Georgid5208402014-04-11 20:24:06 +0200144 if (def != S_DEF_AUTO)
145 conf_warning("symbol value '%s' invalid for %s",
146 p, sym->name);
147 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000148 case S_OTHER:
149 if (*p != '"') {
150 for (p2 = p; *p2 && !isspace(*p2); p2++)
151 ;
152 sym->type = S_STRING;
153 goto done;
154 }
Patrick Georgid5208402014-04-11 20:24:06 +0200155 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000156 case S_STRING:
157 if (*p++ != '"')
158 break;
159 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
160 if (*p2 == '"') {
161 *p2 = 0;
162 break;
163 }
164 memmove(p2, p2 + 1, strlen(p2));
165 }
166 if (!p2) {
Patrick Georgid5208402014-04-11 20:24:06 +0200167 if (def != S_DEF_AUTO)
168 conf_warning("invalid string found");
Patrick Georgi0588d192009-08-12 15:00:51 +0000169 return 1;
170 }
Patrick Georgid5208402014-04-11 20:24:06 +0200171 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000172 case S_INT:
173 case S_HEX:
174 done:
175 if (sym_string_valid(sym, p)) {
176 sym->def[def].val = strdup(p);
177 sym->flags |= def_flags;
178 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200179 if (def != S_DEF_AUTO)
180 conf_warning("symbol value '%s' invalid for %s",
181 p, sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000182 return 1;
183 }
184 break;
185 default:
186 ;
187 }
188 return 0;
189}
190
Patrick Georgid5208402014-04-11 20:24:06 +0200191#define LINE_GROWTH 16
192static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
193{
194 char *nline;
195 size_t new_size = slen + 1;
196 if (new_size > *n) {
197 new_size += LINE_GROWTH - 1;
198 new_size *= 2;
199 nline = realloc(*lineptr, new_size);
200 if (!nline)
201 return -1;
202
203 *lineptr = nline;
204 *n = new_size;
205 }
206
207 (*lineptr)[slen] = c;
208
209 return 0;
210}
211
212static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
213{
214 char *line = *lineptr;
215 size_t slen = 0;
216
217 for (;;) {
218 int c = getc(stream);
219
220 switch (c) {
221 case '\n':
222 if (add_byte(c, &line, slen, n) < 0)
223 goto e_out;
224 slen++;
225 /* fall through */
226 case EOF:
227 if (add_byte('\0', &line, slen, n) < 0)
228 goto e_out;
229 *lineptr = line;
230 if (slen == 0)
231 return -1;
232 return slen;
233 default:
234 if (add_byte(c, &line, slen, n) < 0)
235 goto e_out;
236 slen++;
237 }
238 }
239
240e_out:
241 line[slen-1] = '\0';
242 *lineptr = line;
243 return -1;
244}
245
Patrick Georgi0588d192009-08-12 15:00:51 +0000246int conf_read_simple(const char *name, int def)
247{
248 FILE *in = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200249 char *line = NULL;
250 size_t line_asize = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000251 char *p, *p2;
252 struct symbol *sym;
253 int i, def_flags;
254
255 if (name) {
256 in = zconf_fopen(name);
257 } else {
258 struct property *prop;
259
260 name = conf_get_configname();
261 in = zconf_fopen(name);
262 if (in)
263 goto load;
264 sym_add_change_count(1);
Patrick Georgid5208402014-04-11 20:24:06 +0200265 if (!sym_defconfig_list) {
266 if (modules_sym)
267 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000268 return 1;
Patrick Georgid5208402014-04-11 20:24:06 +0200269 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000270
271 for_all_defaults(sym_defconfig_list, prop) {
272 if (expr_calc_value(prop->visible.expr) == no ||
273 prop->expr->type != E_SYMBOL)
274 continue;
275 name = conf_expand_value(prop->expr->left.sym->name);
276 in = zconf_fopen(name);
277 if (in) {
Patrick Georgid5208402014-04-11 20:24:06 +0200278 conf_message(_("using defaults found in %s"),
279 name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000280 goto load;
281 }
282 }
283 }
284 if (!in)
285 return 1;
286
287load:
288 conf_filename = name;
289 conf_lineno = 0;
290 conf_warnings = 0;
291 conf_unsaved = 0;
292
293 def_flags = SYMBOL_DEF << def;
294 for_all_symbols(i, sym) {
295 sym->flags |= SYMBOL_CHANGED;
296 sym->flags &= ~(def_flags|SYMBOL_VALID);
297 if (sym_is_choice(sym))
298 sym->flags |= def_flags;
299 switch (sym->type) {
300 case S_INT:
301 case S_HEX:
302 case S_STRING:
303 if (sym->def[def].val)
304 free(sym->def[def].val);
Patrick Georgid5208402014-04-11 20:24:06 +0200305 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000306 default:
307 sym->def[def].val = NULL;
308 sym->def[def].tri = no;
309 }
310 }
311
Patrick Georgid5208402014-04-11 20:24:06 +0200312 while (compat_getline(&line, &line_asize, in) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000313 conf_lineno++;
314 sym = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200315 if (line[0] == '#') {
316 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Patrick Georgi0588d192009-08-12 15:00:51 +0000317 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200318 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Patrick Georgi0588d192009-08-12 15:00:51 +0000319 if (!p)
320 continue;
321 *p++ = 0;
322 if (strncmp(p, "is not set", 10))
323 continue;
324 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200325 sym = sym_find(line + 2 + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000326 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200327 sym_add_change_count(1);
328 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000329 }
330 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200331 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000332 if (sym->type == S_UNKNOWN)
333 sym->type = S_BOOLEAN;
334 }
335 if (sym->flags & def_flags) {
336 conf_warning("override: reassigning to symbol %s", sym->name);
337 }
338 switch (sym->type) {
339 case S_BOOLEAN:
340 case S_TRISTATE:
341 sym->def[def].tri = no;
342 sym->flags |= def_flags;
343 break;
344 default:
345 ;
346 }
Patrick Georgid5208402014-04-11 20:24:06 +0200347 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
348 p = strchr(line + strlen(CONFIG_), '=');
Patrick Georgi0588d192009-08-12 15:00:51 +0000349 if (!p)
350 continue;
351 *p++ = 0;
352 p2 = strchr(p, '\n');
353 if (p2) {
354 *p2-- = 0;
355 if (*p2 == '\r')
356 *p2 = 0;
357 }
358 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200359 sym = sym_find(line + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000360 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200361 sym_add_change_count(1);
362 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000363 }
364 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200365 sym = sym_lookup(line + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000366 if (sym->type == S_UNKNOWN)
367 sym->type = S_OTHER;
368 }
369 if (sym->flags & def_flags) {
370 conf_warning("override: reassigning to symbol %s", sym->name);
371 }
372 if (conf_set_sym_val(sym, def, def_flags, p))
373 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200374 } else {
375 if (line[0] != '\r' && line[0] != '\n')
376 conf_warning("unexpected data");
Patrick Georgi0588d192009-08-12 15:00:51 +0000377 continue;
378 }
Patrick Georgid5208402014-04-11 20:24:06 +0200379setsym:
Patrick Georgi0588d192009-08-12 15:00:51 +0000380 if (sym && sym_is_choice_value(sym)) {
381 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
382 switch (sym->def[def].tri) {
383 case no:
384 break;
385 case mod:
386 if (cs->def[def].tri == yes) {
387 conf_warning("%s creates inconsistent choice state", sym->name);
388 cs->flags &= ~def_flags;
389 }
390 break;
391 case yes:
392 if (cs->def[def].tri != no)
393 conf_warning("override: %s changes choice state", sym->name);
394 cs->def[def].val = sym;
395 break;
396 }
397 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
398 }
399 }
Patrick Georgid5208402014-04-11 20:24:06 +0200400 free(line);
Patrick Georgi0588d192009-08-12 15:00:51 +0000401 fclose(in);
402
403 if (modules_sym)
404 sym_calc_value(modules_sym);
405 return 0;
406}
407
408int conf_read(const char *name)
409{
Patrick Georgid5208402014-04-11 20:24:06 +0200410 struct symbol *sym;
411 int i;
Patrick Georgi0588d192009-08-12 15:00:51 +0000412
413 sym_set_change_count(0);
414
415 if (conf_read_simple(name, S_DEF_USER))
416 return 1;
417
418 for_all_symbols(i, sym) {
419 sym_calc_value(sym);
420 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
Patrick Georgid5208402014-04-11 20:24:06 +0200421 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000422 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
423 /* check that calculated value agrees with saved value */
424 switch (sym->type) {
425 case S_BOOLEAN:
426 case S_TRISTATE:
427 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
428 break;
429 if (!sym_is_choice(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200430 continue;
431 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000432 default:
433 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Patrick Georgid5208402014-04-11 20:24:06 +0200434 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000435 break;
436 }
437 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
438 /* no previous value and not saved */
Patrick Georgid5208402014-04-11 20:24:06 +0200439 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000440 conf_unsaved++;
441 /* maybe print value in verbose mode... */
Patrick Georgi0588d192009-08-12 15:00:51 +0000442 }
443
444 for_all_symbols(i, sym) {
445 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
446 /* Reset values of generates values, so they'll appear
447 * as new, if they should become visible, but that
448 * doesn't quite work if the Kconfig and the saved
449 * configuration disagree.
450 */
451 if (sym->visible == no && !conf_unsaved)
452 sym->flags &= ~SYMBOL_DEF_USER;
453 switch (sym->type) {
454 case S_STRING:
455 case S_INT:
456 case S_HEX:
457 /* Reset a string value if it's out of range */
458 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
459 break;
460 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
461 conf_unsaved++;
462 break;
463 default:
464 break;
465 }
466 }
467 }
468
469 sym_add_change_count(conf_warnings || conf_unsaved);
470
471 return 0;
472}
473
Patrick Georgid5208402014-04-11 20:24:06 +0200474/*
475 * Kconfig configuration printer
476 *
477 * This printer is used when generating the resulting configuration after
478 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
479 * passing a non-NULL argument to the printer.
480 *
481 */
482static void
483kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
484{
485
486 switch (sym->type) {
487 case S_BOOLEAN:
488 case S_TRISTATE:
489 if (*value == 'n') {
490 bool skip_unset = (arg != NULL);
491
492 if (!skip_unset)
493 fprintf(fp, "# %s%s is not set\n",
494 CONFIG_, sym->name);
495 return;
496 }
497 break;
498 default:
499 break;
500 }
501
502 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
503}
504
505static void
506kconfig_print_comment(FILE *fp, const char *value, void *arg)
507{
508 const char *p = value;
509 size_t l;
510
511 for (;;) {
512 l = strcspn(p, "\n");
513 fprintf(fp, "#");
514 if (l) {
515 fprintf(fp, " ");
516 xfwrite(p, l, 1, fp);
517 p += l;
518 }
519 fprintf(fp, "\n");
520 if (*p++ == '\0')
521 break;
522 }
523}
524
525static struct conf_printer kconfig_printer_cb =
526{
527 .print_symbol = kconfig_print_symbol,
528 .print_comment = kconfig_print_comment,
529};
530
531/*
532 * Header printer
533 *
534 * This printer is used when generating the `include/generated/autoconf.h' file.
535 */
536static void
537header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
538{
539
540 switch (sym->type) {
541 case S_BOOLEAN:
542 case S_TRISTATE: {
543 const char *suffix = "";
544
545 switch (*value) {
546 case 'n':
547 if (getenv("KCONFIG_NEGATIVES")) {
548 fprintf(fp, "#define %s%s%s 0\n",
549 CONFIG_, sym->name, suffix);
550 }
551 break;
552 case 'm':
553 suffix = "_MODULE";
554 /* fall through */
555 default:
556 fprintf(fp, "#define %s%s%s 1\n",
557 CONFIG_, sym->name, suffix);
558 }
559 break;
560 }
561 case S_HEX: {
562 const char *prefix = "";
563
564 if (!value || (value[0] == '\0')) {
565 value = "0";
566 } else
567 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
568 prefix = "0x";
569 fprintf(fp, "#define %s%s %s%s\n",
570 CONFIG_, sym->name, prefix, value);
571 break;
572 }
573 case S_INT:
574 if (!value || (value[0] == '\0')) {
575 value = "0";
576 }
577 /* fall through */
578 case S_STRING:
579 fprintf(fp, "#define %s%s %s\n",
580 CONFIG_, sym->name, value);
581 break;
582 default:
583 break;
584 }
585
586}
587
588static void
589header_print_comment(FILE *fp, const char *value, void *arg)
590{
591 const char *p = value;
592 size_t l;
593
594 fprintf(fp, "/*\n");
595 for (;;) {
596 l = strcspn(p, "\n");
597 fprintf(fp, " *");
598 if (l) {
599 fprintf(fp, " ");
600 xfwrite(p, l, 1, fp);
601 p += l;
602 }
603 fprintf(fp, "\n");
604 if (*p++ == '\0')
605 break;
606 }
607 fprintf(fp, " */\n");
608}
609
610static struct conf_printer header_printer_cb =
611{
612 .print_symbol = header_print_symbol,
613 .print_comment = header_print_comment,
614};
615
616/*
617 * Tristate printer
618 *
619 * This printer is used when generating the `include/config/tristate.conf' file.
620 */
621static void
622tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
623{
624
625 if (sym->type == S_TRISTATE && (*value != 'n' || arg == NULL))
626 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
627}
628
629static struct conf_printer tristate_printer_cb =
630{
631 .print_symbol = tristate_print_symbol,
632 .print_comment = kconfig_print_comment,
633};
634
635static void conf_write_symbol(FILE *fp, struct symbol *sym,
636 struct conf_printer *printer, void *printer_arg)
637{
638 const char *str;
639
640 switch (sym->type) {
641 case S_OTHER:
642 case S_UNKNOWN:
643 break;
644 case S_STRING:
645 str = sym_get_string_value(sym);
646 str = sym_escape_string_value(str);
647 printer->print_symbol(fp, sym, str, printer_arg);
648 free((void *)str);
649 break;
650 default:
651 str = sym_get_string_value(sym);
652 printer->print_symbol(fp, sym, str, printer_arg);
653 }
654}
655
656static void
657conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
658{
659 char buf[256];
660
661 snprintf(buf, sizeof(buf),
662 "\n"
663 "Automatically generated file; DO NOT EDIT.\n"
664 "%s\n",
665 rootmenu.prompt->text);
666
667 printer->print_comment(fp, buf, printer_arg);
668}
669
670/*
671 * Write out a minimal config.
672 * All values that has default values are skipped as this is redundant.
673 */
674int conf_write_defconfig(const char *filename)
675{
676 struct symbol *sym;
677 struct menu *menu;
678 FILE *out;
679
680 out = fopen(filename, "w");
681 if (!out)
682 return 1;
683
684 sym_clear_all_valid();
685
686 /* Traverse all menus to find all relevant symbols */
687 menu = rootmenu.list;
688
689 while (menu != NULL)
690 {
691 sym = menu->sym;
692 if (sym == NULL) {
693 if (!menu_is_visible(menu))
694 goto next_menu;
695 } else if (!sym_is_choice(sym)) {
696 sym_calc_value(sym);
697 if (!(sym->flags & SYMBOL_WRITE))
698 goto next_menu;
699 sym->flags &= ~SYMBOL_WRITE;
700 /* If we cannot change the symbol - skip */
701 if (!sym_is_changable(sym))
702 goto next_menu;
703 /* If symbol equals to default value - skip */
704 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
705 goto next_menu;
706
707 /*
708 * If symbol is a choice value and equals to the
709 * default for a choice - skip.
710 * But only if value is bool and equal to "y" and
711 * choice is not "optional".
712 * (If choice is "optional" then all values can be "n")
713 */
714 if (sym_is_choice_value(sym)) {
715 struct symbol *cs;
716 struct symbol *ds;
717
718 cs = prop_get_symbol(sym_get_choice_prop(sym));
719 ds = sym_choice_default(cs);
720 if (!sym_is_optional(cs) && sym == ds) {
721 if ((sym->type == S_BOOLEAN) &&
722 sym_get_tristate_value(sym) == yes)
723 goto next_menu;
724 }
725 }
726 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
727 }
728next_menu:
729 if (menu->list != NULL) {
730 menu = menu->list;
731 }
732 else if (menu->next != NULL) {
733 menu = menu->next;
734 } else {
735 while ((menu = menu->parent)) {
736 if (menu->next != NULL) {
737 menu = menu->next;
738 break;
739 }
740 }
741 }
742 }
743 fclose(out);
744 return 0;
745}
746
Patrick Georgi0588d192009-08-12 15:00:51 +0000747int conf_write(const char *name)
748{
749 FILE *out;
750 struct symbol *sym;
751 struct menu *menu;
752 const char *basename;
Patrick Georgi0588d192009-08-12 15:00:51 +0000753 const char *str;
Patrick Georgid5208402014-04-11 20:24:06 +0200754 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000755 char *env;
756
757 dirname[0] = 0;
758 if (name && name[0]) {
759 struct stat st;
760 char *slash;
761
762 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
763 strcpy(dirname, name);
764 strcat(dirname, "/");
765 basename = conf_get_configname();
766 } else if ((slash = strrchr(name, '/'))) {
767 int size = slash - name + 1;
768 memcpy(dirname, name, size);
769 dirname[size] = 0;
770 if (slash[1])
771 basename = slash + 1;
772 else
773 basename = conf_get_configname();
774 } else
775 basename = name;
776 } else
777 basename = conf_get_configname();
778
779 sprintf(newname, "%s%s", dirname, basename);
780 env = getenv("KCONFIG_OVERWRITECONFIG");
781 if (!env || !*env) {
782 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
783 out = fopen(tmpname, "w");
784 } else {
785 *tmpname = 0;
786 out = fopen(newname, "w");
787 }
788 if (!out)
789 return 1;
790
Patrick Georgid5208402014-04-11 20:24:06 +0200791 conf_write_heading(out, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000792
793 if (!conf_get_changed())
794 sym_clear_all_valid();
795
796 menu = rootmenu.list;
797 while (menu) {
798 sym = menu->sym;
799 if (!sym) {
800 if (!menu_is_visible(menu))
801 goto next;
802 str = menu_get_prompt(menu);
803 fprintf(out, "\n"
804 "#\n"
805 "# %s\n"
806 "#\n", str);
807 } else if (!(sym->flags & SYMBOL_CHOICE)) {
808 sym_calc_value(sym);
809 if (!(sym->flags & SYMBOL_WRITE))
810 goto next;
811 sym->flags &= ~SYMBOL_WRITE;
Patrick Georgid5208402014-04-11 20:24:06 +0200812
813 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000814 }
815
Patrick Georgid5208402014-04-11 20:24:06 +0200816next:
Patrick Georgi0588d192009-08-12 15:00:51 +0000817 if (menu->list) {
818 menu = menu->list;
819 continue;
820 }
821 if (menu->next)
822 menu = menu->next;
823 else while ((menu = menu->parent)) {
824 if (menu->next) {
825 menu = menu->next;
826 break;
827 }
828 }
829 }
830 fclose(out);
831
832 if (*tmpname) {
833 strcat(dirname, basename);
834 strcat(dirname, ".old");
835 rename(newname, dirname);
836 if (rename(tmpname, newname))
837 return 1;
838 }
839
Patrick Georgid5208402014-04-11 20:24:06 +0200840 conf_message(_("configuration written to %s"), newname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000841
842 sym_set_change_count(0);
843
844 return 0;
845}
846
Patrick Georgid5208402014-04-11 20:24:06 +0200847static int conf_split_config(void)
Patrick Georgi0588d192009-08-12 15:00:51 +0000848{
Patrick Georgid5208402014-04-11 20:24:06 +0200849 const char *name;
850 char path[PATH_MAX+1];
851 char pwd[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000852 char *s, *d, c;
853 struct symbol *sym;
854 struct stat sb;
855 int res, i, fd;
856
Patrick Georgid5208402014-04-11 20:24:06 +0200857 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +0000858 conf_read_simple(name, S_DEF_AUTO);
859
Patrick Georgid5208402014-04-11 20:24:06 +0200860 getcwd(pwd, sizeof(pwd));
861 name = getenv("KCONFIG_SPLITCONFIG");
862 if (!name)
863 name = "include/config";
864 if (chdir(name))
Patrick Georgi0588d192009-08-12 15:00:51 +0000865 return 1;
866
867 res = 0;
868 for_all_symbols(i, sym) {
869 sym_calc_value(sym);
870 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
871 continue;
872 if (sym->flags & SYMBOL_WRITE) {
873 if (sym->flags & SYMBOL_DEF_AUTO) {
874 /*
875 * symbol has old and new value,
876 * so compare them...
877 */
878 switch (sym->type) {
879 case S_BOOLEAN:
880 case S_TRISTATE:
881 if (sym_get_tristate_value(sym) ==
882 sym->def[S_DEF_AUTO].tri)
883 continue;
884 break;
885 case S_STRING:
886 case S_HEX:
887 case S_INT:
888 if (!strcmp(sym_get_string_value(sym),
889 sym->def[S_DEF_AUTO].val))
890 continue;
891 break;
892 default:
893 break;
894 }
895 } else {
896 /*
897 * If there is no old value, only 'no' (unset)
898 * is allowed as new value.
899 */
900 switch (sym->type) {
901 case S_BOOLEAN:
902 case S_TRISTATE:
903 if (sym_get_tristate_value(sym) == no)
904 continue;
905 break;
906 default:
907 break;
908 }
909 }
910 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
911 /* There is neither an old nor a new value. */
912 continue;
913 /* else
914 * There is an old value, but no new value ('no' (unset)
915 * isn't saved in auto.conf, so the old value is always
916 * different from 'no').
917 */
918
919 /* Replace all '_' and append ".h" */
920 s = sym->name;
921 d = path;
922 while ((c = *s++)) {
923 c = tolower(c);
924 *d++ = (c == '_') ? '/' : c;
925 }
926 strcpy(d, ".h");
927
928 /* Assume directory path already exists. */
929 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
930 if (fd == -1) {
931 if (errno != ENOENT) {
932 res = 1;
933 break;
934 }
935 /*
936 * Create directory components,
937 * unless they exist already.
938 */
939 d = path;
940 while ((d = strchr(d, '/'))) {
941 *d = 0;
942 if (stat(path, &sb) && mkdir(path, 0755)) {
943 res = 1;
944 goto out;
945 }
946 *d++ = '/';
947 }
948 /* Try it again. */
949 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
950 if (fd == -1) {
951 res = 1;
952 break;
953 }
954 }
955 close(fd);
956 }
957out:
Patrick Georgid5208402014-04-11 20:24:06 +0200958 if (chdir(pwd))
Patrick Georgi0588d192009-08-12 15:00:51 +0000959 return 1;
960
961 return res;
962}
963
964int conf_write_autoconf(void)
965{
966 struct symbol *sym;
Patrick Georgid5208402014-04-11 20:24:06 +0200967 const char *name;
968 FILE *out, *tristate, *out_h;
969 int i;
970 int print_negatives;
971
972 print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +0000973
974 sym_clear_all_valid();
975
Patrick Georgid5208402014-04-11 20:24:06 +0200976 char *dep_file = getenv("KCONFIG_DEPENDENCIES");
977 if (!dep_file)
978 dep_file = "include/config/auto.conf.cmd";
979 file_write_dep(dep_file);
Vadim Bendeburyc302d202011-10-24 14:06:23 -0700980
Patrick Georgi0588d192009-08-12 15:00:51 +0000981 if (conf_split_config())
982 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000983
Patrick Georgi129462d2014-11-17 14:56:49 +0100984 char *tmpconfig_name = malloc(PATH_MAX);
985 if (getenv("COREBOOT_BUILD_DIR")) {
986 sprintf(tmpconfig_name, "%s/.tmpconfig.XXXXXX",
987 getenv("COREBOOT_BUILD_DIR"));
988 } else {
989 tmpconfig_name = strdup(".tmpconfig.XXXXXX");
990 }
Patrick Georgid5208402014-04-11 20:24:06 +0200991 if ((i = mkstemp(tmpconfig_name)) == -1)
992 return 1;
993 out = fdopen(i, "w");
Patrick Georgi0588d192009-08-12 15:00:51 +0000994 if (!out)
995 return 1;
996
Patrick Georgi129462d2014-11-17 14:56:49 +0100997 char *tmpconfig_triname = malloc(PATH_MAX);
998 if (getenv("COREBOOT_BUILD_DIR")) {
999 sprintf(tmpconfig_triname, "%s/.tmpconfig_tristate.XXXXXX",
1000 getenv("COREBOOT_BUILD_DIR"));
1001 } else {
1002 tmpconfig_triname = strdup(".tmpconfig_tristate.XXXXXX");
1003 }
Patrick Georgid5208402014-04-11 20:24:06 +02001004 if ((i = mkstemp(tmpconfig_triname)) == -1)
1005 return 1;
1006 tristate = fdopen(i, "w");
1007 if (!tristate) {
Patrick Georgi0588d192009-08-12 15:00:51 +00001008 fclose(out);
1009 return 1;
1010 }
1011
Patrick Georgi129462d2014-11-17 14:56:49 +01001012 char *tmpconfig_h = malloc(PATH_MAX);
1013 if (getenv("COREBOOT_BUILD_DIR")) {
1014 sprintf(tmpconfig_h, "%s/.tmpconfig_tristate.XXXXXX",
1015 getenv("COREBOOT_BUILD_DIR"));
1016 } else {
1017 tmpconfig_h = strdup(".tmpconfig_tristate.XXXXXX");
1018 }
Patrick Georgid5208402014-04-11 20:24:06 +02001019 if ((i = mkstemp(tmpconfig_h)) == -1)
1020 return 1;
1021 out_h = fdopen(i, "w");
1022 if (!out_h) {
1023 fclose(out);
1024 fclose(tristate);
1025 return 1;
1026 }
1027
1028 conf_write_heading(out, &kconfig_printer_cb, NULL);
1029
1030 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1031
1032 conf_write_heading(out_h, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001033
1034 for_all_symbols(i, sym) {
1035 sym_calc_value(sym);
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001036 if (!sym->name)
Patrick Georgi0588d192009-08-12 15:00:51 +00001037 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001038
1039 if (!(sym->flags & SYMBOL_WRITE) && !print_negatives)
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001040 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001041
1042 /* these are safe to write out, so do it all the time */
1043 if (!(sym->flags & SYMBOL_WRITE) &&
1044 !(sym->type == S_BOOLEAN ||
1045 sym->type == S_HEX ||
1046 sym->type == S_INT))
1047 continue;
1048
1049 /* write symbol to auto.conf, tristate and header files */
1050 conf_write_symbol(out, sym, &kconfig_printer_cb, print_negatives?NULL:(void *)1);
1051
1052 conf_write_symbol(tristate, sym, &tristate_printer_cb, print_negatives?NULL:(void *)1);
1053
1054 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001055 }
1056 fclose(out);
Patrick Georgid5208402014-04-11 20:24:06 +02001057 fclose(tristate);
Patrick Georgi0588d192009-08-12 15:00:51 +00001058 fclose(out_h);
1059
Patrick Georgid5208402014-04-11 20:24:06 +02001060 name = getenv("KCONFIG_AUTOHEADER");
Patrick Georgi0588d192009-08-12 15:00:51 +00001061 if (!name)
Patrick Georgid5208402014-04-11 20:24:06 +02001062 name = "include/generated/autoconf.h";
1063 if (rename(tmpconfig_h, name))
1064 return 1;
1065 name = getenv("KCONFIG_TRISTATE");
1066 if (!name)
1067 name = "include/config/tristate.conf";
1068 if (rename(tmpconfig_triname, name))
1069 return 1;
1070 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +00001071 /*
1072 * This must be the last step, kbuild has a dependency on auto.conf
1073 * and this marks the successful completion of the previous steps.
1074 */
Patrick Georgid5208402014-04-11 20:24:06 +02001075 if (rename(tmpconfig_name, name))
Patrick Georgi0588d192009-08-12 15:00:51 +00001076 return 1;
1077
Patrick Georgi0588d192009-08-12 15:00:51 +00001078 return 0;
1079}
1080
1081static int sym_change_count;
1082static void (*conf_changed_callback)(void);
1083
1084void sym_set_change_count(int count)
1085{
1086 int _sym_change_count = sym_change_count;
1087 sym_change_count = count;
1088 if (conf_changed_callback &&
1089 (bool)_sym_change_count != (bool)count)
1090 conf_changed_callback();
1091}
1092
1093void sym_add_change_count(int count)
1094{
1095 sym_set_change_count(count + sym_change_count);
1096}
1097
1098bool conf_get_changed(void)
1099{
1100 return sym_change_count;
1101}
1102
1103void conf_set_changed_callback(void (*fn)(void))
1104{
1105 conf_changed_callback = fn;
1106}
Patrick Georgid5208402014-04-11 20:24:06 +02001107
1108static bool randomize_choice_values(struct symbol *csym)
1109{
1110 struct property *prop;
1111 struct symbol *sym;
1112 struct expr *e;
1113 int cnt, def;
1114
1115 /*
1116 * If choice is mod then we may have more items selected
1117 * and if no then no-one.
1118 * In both cases stop.
1119 */
1120 if (csym->curr.tri != yes)
1121 return false;
1122
1123 prop = sym_get_choice_prop(csym);
1124
1125 /* count entries in choice block */
1126 cnt = 0;
1127 expr_list_for_each_sym(prop->expr, e, sym)
1128 cnt++;
1129
1130 /*
1131 * find a random value and set it to yes,
1132 * set the rest to no so we have only one set
1133 */
1134 def = (rand() % cnt);
1135
1136 cnt = 0;
1137 expr_list_for_each_sym(prop->expr, e, sym) {
1138 if (def == cnt++) {
1139 sym->def[S_DEF_USER].tri = yes;
1140 csym->def[S_DEF_USER].val = sym;
1141 }
1142 else {
1143 sym->def[S_DEF_USER].tri = no;
1144 }
1145 sym->flags |= SYMBOL_DEF_USER;
1146 /* clear VALID to get value calculated */
1147 sym->flags &= ~SYMBOL_VALID;
1148 }
1149 csym->flags |= SYMBOL_DEF_USER;
1150 /* clear VALID to get value calculated */
1151 csym->flags &= ~(SYMBOL_VALID);
1152
1153 return true;
1154}
1155
1156void set_all_choice_values(struct symbol *csym)
1157{
1158 struct property *prop;
1159 struct symbol *sym;
1160 struct expr *e;
1161
1162 prop = sym_get_choice_prop(csym);
1163
1164 /*
1165 * Set all non-assinged choice values to no
1166 */
1167 expr_list_for_each_sym(prop->expr, e, sym) {
1168 if (!sym_has_value(sym))
1169 sym->def[S_DEF_USER].tri = no;
1170 }
1171 csym->flags |= SYMBOL_DEF_USER;
1172 /* clear VALID to get value calculated */
1173 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1174}
1175
1176bool conf_set_all_new_symbols(enum conf_def_mode mode)
1177{
1178 struct symbol *sym, *csym;
1179 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1180 * pty: probability of tristate = y
1181 * ptm: probability of tristate = m
1182 */
1183
1184 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1185 * below, otherwise gcc whines about
1186 * -Wmaybe-uninitialized */
1187 if (mode == def_random) {
1188 int n, p[3];
1189 char *env = getenv("KCONFIG_PROBABILITY");
1190 n = 0;
1191 while( env && *env ) {
1192 char *endp;
1193 int tmp = strtol( env, &endp, 10 );
1194 if( tmp >= 0 && tmp <= 100 ) {
1195 p[n++] = tmp;
1196 } else {
1197 errno = ERANGE;
1198 perror( "KCONFIG_PROBABILITY" );
1199 exit( 1 );
1200 }
1201 env = (*endp == ':') ? endp+1 : endp;
1202 if( n >=3 ) {
1203 break;
1204 }
1205 }
1206 switch( n ) {
1207 case 1:
1208 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1209 break;
1210 case 2:
1211 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1212 break;
1213 case 3:
1214 pby = p[0]; pty = p[1]; ptm = p[2];
1215 break;
1216 }
1217
1218 if( pty+ptm > 100 ) {
1219 errno = ERANGE;
1220 perror( "KCONFIG_PROBABILITY" );
1221 exit( 1 );
1222 }
1223 }
1224 bool has_changed = false;
1225
1226 for_all_symbols(i, sym) {
1227 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1228 continue;
1229 switch (sym_get_type(sym)) {
1230 case S_BOOLEAN:
1231 case S_TRISTATE:
1232 has_changed = true;
1233 switch (mode) {
1234 case def_yes:
1235 sym->def[S_DEF_USER].tri = yes;
1236 break;
1237 case def_mod:
1238 sym->def[S_DEF_USER].tri = mod;
1239 break;
1240 case def_no:
1241 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1242 sym->def[S_DEF_USER].tri = yes;
1243 else
1244 sym->def[S_DEF_USER].tri = no;
1245 break;
1246 case def_random:
1247 sym->def[S_DEF_USER].tri = no;
1248 cnt = rand() % 100;
1249 if (sym->type == S_TRISTATE) {
1250 if (cnt < pty)
1251 sym->def[S_DEF_USER].tri = yes;
1252 else if (cnt < (pty+ptm))
1253 sym->def[S_DEF_USER].tri = mod;
1254 } else if (cnt < pby)
1255 sym->def[S_DEF_USER].tri = yes;
1256 break;
1257 default:
1258 continue;
1259 }
1260 if (!(sym_is_choice(sym) && mode == def_random))
1261 sym->flags |= SYMBOL_DEF_USER;
1262 break;
1263 default:
1264 break;
1265 }
1266
1267 }
1268
1269 sym_clear_all_valid();
1270
1271 /*
1272 * We have different type of choice blocks.
1273 * If curr.tri equals to mod then we can select several
1274 * choice symbols in one block.
1275 * In this case we do nothing.
1276 * If curr.tri equals yes then only one symbol can be
1277 * selected in a choice block and we set it to yes,
1278 * and the rest to no.
1279 */
1280 if (mode != def_random) {
1281 for_all_symbols(i, csym) {
1282 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1283 sym_is_choice_value(csym))
1284 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1285 }
1286 }
1287
1288 for_all_symbols(i, csym) {
1289 if (sym_has_value(csym) || !sym_is_choice(csym))
1290 continue;
1291
1292 sym_calc_value(csym);
1293 if (mode == def_random)
1294 has_changed = randomize_choice_values(csym);
1295 else {
1296 set_all_choice_values(csym);
1297 has_changed = true;
1298 }
1299 }
1300
1301 return has_changed;
1302}