blob: e58246369f3654b2a954cfb189e1eb73804bc43c [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
zbao2f3fd262015-09-26 06:20:53 -040030#ifdef __MINGW32__
31#define mkdir(_n,_p) mkdir((_n))
32#endif
33
Patrick Georgi0588d192009-08-12 15:00:51 +000034static void conf_warning(const char *fmt, ...)
35{
36 va_list ap;
37 va_start(ap, fmt);
38 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
39 vfprintf(stderr, fmt, ap);
40 fprintf(stderr, "\n");
41 va_end(ap);
42 conf_warnings++;
43}
44
Patrick Georgid5208402014-04-11 20:24:06 +020045static void conf_default_message_callback(const char *fmt, va_list ap)
46{
47 printf("#\n# ");
48 vprintf(fmt, ap);
49 printf("\n#\n");
50}
51
52static void (*conf_message_callback) (const char *fmt, va_list ap) =
53 conf_default_message_callback;
54void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
55{
56 conf_message_callback = fn;
57}
58
59static void conf_message(const char *fmt, ...)
60{
61 va_list ap;
62
63 va_start(ap, fmt);
64 if (conf_message_callback)
65 conf_message_callback(fmt, ap);
Stefan Reinauercce66622015-04-06 01:14:21 +020066 va_end(ap);
Patrick Georgid5208402014-04-11 20:24:06 +020067}
68
Patrick Georgi0588d192009-08-12 15:00:51 +000069const char *conf_get_configname(void)
70{
71 char *name = getenv("KCONFIG_CONFIG");
72
73 return name ? name : ".config";
74}
75
Patrick Georgid5208402014-04-11 20:24:06 +020076const char *conf_get_autoconfig_name(void)
77{
78 char *name = getenv("KCONFIG_AUTOCONFIG");
79
80 return name ? name : "include/config/auto.conf";
81}
82
Patrick Georgi0588d192009-08-12 15:00:51 +000083static char *conf_expand_value(const char *in)
84{
85 struct symbol *sym;
86 const char *src;
87 static char res_value[SYMBOL_MAXLENGTH];
88 char *dst, name[SYMBOL_MAXLENGTH];
89
90 res_value[0] = 0;
91 dst = name;
92 while ((src = strchr(in, '$'))) {
93 strncat(res_value, in, src - in);
94 src++;
95 dst = name;
96 while (isalnum(*src) || *src == '_')
97 *dst++ = *src++;
98 *dst = 0;
99 sym = sym_lookup(name, 0);
100 sym_calc_value(sym);
101 strcat(res_value, sym_get_string_value(sym));
102 in = src;
103 }
104 strcat(res_value, in);
105
106 return res_value;
107}
108
109char *conf_get_default_confname(void)
110{
111 struct stat buf;
112 static char fullname[PATH_MAX+1];
113 char *env, *name;
114
115 name = conf_expand_value(conf_defname);
116 env = getenv(SRCTREE);
117 if (env) {
118 sprintf(fullname, "%s/%s", env, name);
119 if (!stat(fullname, &buf))
120 return fullname;
121 }
122 return name;
123}
124
125static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
126{
127 char *p2;
128
129 switch (sym->type) {
130 case S_TRISTATE:
131 if (p[0] == 'm') {
132 sym->def[def].tri = mod;
133 sym->flags |= def_flags;
134 break;
135 }
Patrick Georgid5208402014-04-11 20:24:06 +0200136 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000137 case S_BOOLEAN:
138 if (p[0] == 'y') {
139 sym->def[def].tri = yes;
140 sym->flags |= def_flags;
141 break;
142 }
143 if (p[0] == 'n') {
144 sym->def[def].tri = no;
145 sym->flags |= def_flags;
146 break;
147 }
Patrick Georgid5208402014-04-11 20:24:06 +0200148 if (def != S_DEF_AUTO)
149 conf_warning("symbol value '%s' invalid for %s",
150 p, sym->name);
151 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000152 case S_OTHER:
153 if (*p != '"') {
154 for (p2 = p; *p2 && !isspace(*p2); p2++)
155 ;
156 sym->type = S_STRING;
157 goto done;
158 }
Patrick Georgid5208402014-04-11 20:24:06 +0200159 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000160 case S_STRING:
161 if (*p++ != '"')
162 break;
163 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
164 if (*p2 == '"') {
165 *p2 = 0;
166 break;
167 }
168 memmove(p2, p2 + 1, strlen(p2));
169 }
170 if (!p2) {
Patrick Georgid5208402014-04-11 20:24:06 +0200171 if (def != S_DEF_AUTO)
172 conf_warning("invalid string found");
Patrick Georgi0588d192009-08-12 15:00:51 +0000173 return 1;
174 }
Patrick Georgid5208402014-04-11 20:24:06 +0200175 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000176 case S_INT:
177 case S_HEX:
178 done:
179 if (sym_string_valid(sym, p)) {
180 sym->def[def].val = strdup(p);
181 sym->flags |= def_flags;
182 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200183 if (def != S_DEF_AUTO)
184 conf_warning("symbol value '%s' invalid for %s",
185 p, sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000186 return 1;
187 }
188 break;
189 default:
190 ;
191 }
192 return 0;
193}
194
Patrick Georgid5208402014-04-11 20:24:06 +0200195#define LINE_GROWTH 16
196static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
197{
198 char *nline;
199 size_t new_size = slen + 1;
200 if (new_size > *n) {
201 new_size += LINE_GROWTH - 1;
202 new_size *= 2;
203 nline = realloc(*lineptr, new_size);
204 if (!nline)
205 return -1;
206
207 *lineptr = nline;
208 *n = new_size;
209 }
210
211 (*lineptr)[slen] = c;
212
213 return 0;
214}
215
216static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
217{
218 char *line = *lineptr;
219 size_t slen = 0;
220
221 for (;;) {
222 int c = getc(stream);
223
224 switch (c) {
225 case '\n':
226 if (add_byte(c, &line, slen, n) < 0)
227 goto e_out;
228 slen++;
229 /* fall through */
230 case EOF:
231 if (add_byte('\0', &line, slen, n) < 0)
232 goto e_out;
233 *lineptr = line;
234 if (slen == 0)
235 return -1;
236 return slen;
237 default:
238 if (add_byte(c, &line, slen, n) < 0)
239 goto e_out;
240 slen++;
241 }
242 }
243
244e_out:
245 line[slen-1] = '\0';
246 *lineptr = line;
247 return -1;
248}
249
Patrick Georgi0588d192009-08-12 15:00:51 +0000250int conf_read_simple(const char *name, int def)
251{
252 FILE *in = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200253 char *line = NULL;
254 size_t line_asize = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000255 char *p, *p2;
256 struct symbol *sym;
257 int i, def_flags;
258
259 if (name) {
260 in = zconf_fopen(name);
261 } else {
262 struct property *prop;
263
264 name = conf_get_configname();
265 in = zconf_fopen(name);
266 if (in)
267 goto load;
268 sym_add_change_count(1);
Patrick Georgid5208402014-04-11 20:24:06 +0200269 if (!sym_defconfig_list) {
270 if (modules_sym)
271 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000272 return 1;
Patrick Georgid5208402014-04-11 20:24:06 +0200273 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000274
275 for_all_defaults(sym_defconfig_list, prop) {
276 if (expr_calc_value(prop->visible.expr) == no ||
277 prop->expr->type != E_SYMBOL)
278 continue;
279 name = conf_expand_value(prop->expr->left.sym->name);
280 in = zconf_fopen(name);
281 if (in) {
Patrick Georgid5208402014-04-11 20:24:06 +0200282 conf_message(_("using defaults found in %s"),
283 name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000284 goto load;
285 }
286 }
287 }
288 if (!in)
289 return 1;
290
291load:
292 conf_filename = name;
293 conf_lineno = 0;
294 conf_warnings = 0;
295 conf_unsaved = 0;
296
297 def_flags = SYMBOL_DEF << def;
298 for_all_symbols(i, sym) {
299 sym->flags |= SYMBOL_CHANGED;
300 sym->flags &= ~(def_flags|SYMBOL_VALID);
301 if (sym_is_choice(sym))
302 sym->flags |= def_flags;
303 switch (sym->type) {
304 case S_INT:
305 case S_HEX:
306 case S_STRING:
307 if (sym->def[def].val)
308 free(sym->def[def].val);
Patrick Georgid5208402014-04-11 20:24:06 +0200309 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000310 default:
311 sym->def[def].val = NULL;
312 sym->def[def].tri = no;
313 }
314 }
315
Patrick Georgid5208402014-04-11 20:24:06 +0200316 while (compat_getline(&line, &line_asize, in) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000317 conf_lineno++;
318 sym = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200319 if (line[0] == '#') {
320 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Patrick Georgi0588d192009-08-12 15:00:51 +0000321 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200322 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Patrick Georgi0588d192009-08-12 15:00:51 +0000323 if (!p)
324 continue;
325 *p++ = 0;
326 if (strncmp(p, "is not set", 10))
327 continue;
328 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200329 sym = sym_find(line + 2 + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000330 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200331 sym_add_change_count(1);
332 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000333 }
334 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200335 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000336 if (sym->type == S_UNKNOWN)
337 sym->type = S_BOOLEAN;
338 }
339 if (sym->flags & def_flags) {
340 conf_warning("override: reassigning to symbol %s", sym->name);
341 }
342 switch (sym->type) {
343 case S_BOOLEAN:
344 case S_TRISTATE:
345 sym->def[def].tri = no;
346 sym->flags |= def_flags;
347 break;
348 default:
349 ;
350 }
Patrick Georgid5208402014-04-11 20:24:06 +0200351 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
352 p = strchr(line + strlen(CONFIG_), '=');
Patrick Georgi0588d192009-08-12 15:00:51 +0000353 if (!p)
354 continue;
355 *p++ = 0;
356 p2 = strchr(p, '\n');
357 if (p2) {
358 *p2-- = 0;
359 if (*p2 == '\r')
360 *p2 = 0;
361 }
362 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200363 sym = sym_find(line + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000364 if (!sym) {
Stefan Reinauerf5342032015-07-17 17:26:48 -0700365 conf_warning("trying to assign non-existent symbol %s", line + strlen(CONFIG_));
Patrick Georgid5208402014-04-11 20:24:06 +0200366 sym_add_change_count(1);
367 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000368 }
369 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200370 sym = sym_lookup(line + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000371 if (sym->type == S_UNKNOWN)
372 sym->type = S_OTHER;
373 }
374 if (sym->flags & def_flags) {
375 conf_warning("override: reassigning to symbol %s", sym->name);
376 }
377 if (conf_set_sym_val(sym, def, def_flags, p))
378 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200379 } else {
380 if (line[0] != '\r' && line[0] != '\n')
381 conf_warning("unexpected data");
Patrick Georgi0588d192009-08-12 15:00:51 +0000382 continue;
383 }
Patrick Georgid5208402014-04-11 20:24:06 +0200384setsym:
Patrick Georgi0588d192009-08-12 15:00:51 +0000385 if (sym && sym_is_choice_value(sym)) {
386 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
387 switch (sym->def[def].tri) {
388 case no:
389 break;
390 case mod:
391 if (cs->def[def].tri == yes) {
392 conf_warning("%s creates inconsistent choice state", sym->name);
393 cs->flags &= ~def_flags;
394 }
395 break;
396 case yes:
397 if (cs->def[def].tri != no)
398 conf_warning("override: %s changes choice state", sym->name);
399 cs->def[def].val = sym;
400 break;
401 }
402 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
403 }
404 }
Patrick Georgid5208402014-04-11 20:24:06 +0200405 free(line);
Patrick Georgi0588d192009-08-12 15:00:51 +0000406 fclose(in);
407
408 if (modules_sym)
409 sym_calc_value(modules_sym);
Stefan Reinauerf5342032015-07-17 17:26:48 -0700410
Stefan Reinauer57a31312015-08-20 11:19:34 -0700411 kconfig_warnings += conf_warnings;
Stefan Reinauerf5342032015-07-17 17:26:48 -0700412
Patrick Georgi0588d192009-08-12 15:00:51 +0000413 return 0;
414}
415
416int conf_read(const char *name)
417{
Patrick Georgid5208402014-04-11 20:24:06 +0200418 struct symbol *sym;
419 int i;
Patrick Georgi0588d192009-08-12 15:00:51 +0000420
421 sym_set_change_count(0);
422
423 if (conf_read_simple(name, S_DEF_USER))
424 return 1;
425
426 for_all_symbols(i, sym) {
427 sym_calc_value(sym);
428 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
Patrick Georgid5208402014-04-11 20:24:06 +0200429 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000430 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
431 /* check that calculated value agrees with saved value */
432 switch (sym->type) {
433 case S_BOOLEAN:
434 case S_TRISTATE:
435 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
436 break;
437 if (!sym_is_choice(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200438 continue;
439 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000440 default:
441 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Patrick Georgid5208402014-04-11 20:24:06 +0200442 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000443 break;
444 }
445 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
446 /* no previous value and not saved */
Patrick Georgid5208402014-04-11 20:24:06 +0200447 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000448 conf_unsaved++;
449 /* maybe print value in verbose mode... */
Patrick Georgi0588d192009-08-12 15:00:51 +0000450 }
451
452 for_all_symbols(i, sym) {
453 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
454 /* Reset values of generates values, so they'll appear
455 * as new, if they should become visible, but that
456 * doesn't quite work if the Kconfig and the saved
457 * configuration disagree.
458 */
459 if (sym->visible == no && !conf_unsaved)
460 sym->flags &= ~SYMBOL_DEF_USER;
461 switch (sym->type) {
462 case S_STRING:
463 case S_INT:
464 case S_HEX:
465 /* Reset a string value if it's out of range */
466 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
467 break;
468 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
469 conf_unsaved++;
470 break;
471 default:
472 break;
473 }
474 }
475 }
476
477 sym_add_change_count(conf_warnings || conf_unsaved);
478
479 return 0;
480}
481
Patrick Georgid5208402014-04-11 20:24:06 +0200482/*
483 * Kconfig configuration printer
484 *
485 * This printer is used when generating the resulting configuration after
486 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
487 * passing a non-NULL argument to the printer.
488 *
489 */
490static void
491kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
492{
493
494 switch (sym->type) {
495 case S_BOOLEAN:
496 case S_TRISTATE:
497 if (*value == 'n') {
498 bool skip_unset = (arg != NULL);
499
500 if (!skip_unset)
501 fprintf(fp, "# %s%s is not set\n",
502 CONFIG_, sym->name);
503 return;
504 }
505 break;
506 default:
507 break;
508 }
509
510 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
511}
512
513static void
514kconfig_print_comment(FILE *fp, const char *value, void *arg)
515{
516 const char *p = value;
517 size_t l;
518
519 for (;;) {
520 l = strcspn(p, "\n");
521 fprintf(fp, "#");
522 if (l) {
523 fprintf(fp, " ");
524 xfwrite(p, l, 1, fp);
525 p += l;
526 }
527 fprintf(fp, "\n");
528 if (*p++ == '\0')
529 break;
530 }
531}
532
533static struct conf_printer kconfig_printer_cb =
534{
535 .print_symbol = kconfig_print_symbol,
536 .print_comment = kconfig_print_comment,
537};
538
539/*
540 * Header printer
541 *
542 * This printer is used when generating the `include/generated/autoconf.h' file.
543 */
544static void
545header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
546{
547
548 switch (sym->type) {
549 case S_BOOLEAN:
550 case S_TRISTATE: {
551 const char *suffix = "";
552
553 switch (*value) {
554 case 'n':
555 if (getenv("KCONFIG_NEGATIVES")) {
556 fprintf(fp, "#define %s%s%s 0\n",
557 CONFIG_, sym->name, suffix);
558 }
559 break;
560 case 'm':
561 suffix = "_MODULE";
562 /* fall through */
563 default:
564 fprintf(fp, "#define %s%s%s 1\n",
565 CONFIG_, sym->name, suffix);
566 }
567 break;
568 }
569 case S_HEX: {
570 const char *prefix = "";
571
572 if (!value || (value[0] == '\0')) {
573 value = "0";
574 } else
575 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
576 prefix = "0x";
577 fprintf(fp, "#define %s%s %s%s\n",
578 CONFIG_, sym->name, prefix, value);
579 break;
580 }
581 case S_INT:
582 if (!value || (value[0] == '\0')) {
583 value = "0";
584 }
585 /* fall through */
586 case S_STRING:
587 fprintf(fp, "#define %s%s %s\n",
588 CONFIG_, sym->name, value);
589 break;
590 default:
591 break;
592 }
593
594}
595
596static void
597header_print_comment(FILE *fp, const char *value, void *arg)
598{
599 const char *p = value;
600 size_t l;
601
602 fprintf(fp, "/*\n");
603 for (;;) {
604 l = strcspn(p, "\n");
605 fprintf(fp, " *");
606 if (l) {
607 fprintf(fp, " ");
608 xfwrite(p, l, 1, fp);
609 p += l;
610 }
611 fprintf(fp, "\n");
612 if (*p++ == '\0')
613 break;
614 }
615 fprintf(fp, " */\n");
616}
617
618static struct conf_printer header_printer_cb =
619{
620 .print_symbol = header_print_symbol,
621 .print_comment = header_print_comment,
622};
623
624/*
625 * Tristate printer
626 *
627 * This printer is used when generating the `include/config/tristate.conf' file.
628 */
629static void
630tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
631{
632
633 if (sym->type == S_TRISTATE && (*value != 'n' || arg == NULL))
634 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
635}
636
637static struct conf_printer tristate_printer_cb =
638{
639 .print_symbol = tristate_print_symbol,
640 .print_comment = kconfig_print_comment,
641};
642
643static void conf_write_symbol(FILE *fp, struct symbol *sym,
644 struct conf_printer *printer, void *printer_arg)
645{
646 const char *str;
647
648 switch (sym->type) {
649 case S_OTHER:
650 case S_UNKNOWN:
651 break;
652 case S_STRING:
653 str = sym_get_string_value(sym);
654 str = sym_escape_string_value(str);
655 printer->print_symbol(fp, sym, str, printer_arg);
656 free((void *)str);
657 break;
658 default:
659 str = sym_get_string_value(sym);
660 printer->print_symbol(fp, sym, str, printer_arg);
661 }
662}
663
664static void
665conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
666{
667 char buf[256];
668
669 snprintf(buf, sizeof(buf),
670 "\n"
671 "Automatically generated file; DO NOT EDIT.\n"
672 "%s\n",
673 rootmenu.prompt->text);
674
675 printer->print_comment(fp, buf, printer_arg);
676}
677
678/*
679 * Write out a minimal config.
680 * All values that has default values are skipped as this is redundant.
681 */
682int conf_write_defconfig(const char *filename)
683{
684 struct symbol *sym;
685 struct menu *menu;
686 FILE *out;
687
688 out = fopen(filename, "w");
689 if (!out)
690 return 1;
691
692 sym_clear_all_valid();
693
694 /* Traverse all menus to find all relevant symbols */
695 menu = rootmenu.list;
696
697 while (menu != NULL)
698 {
699 sym = menu->sym;
700 if (sym == NULL) {
701 if (!menu_is_visible(menu))
702 goto next_menu;
703 } else if (!sym_is_choice(sym)) {
704 sym_calc_value(sym);
705 if (!(sym->flags & SYMBOL_WRITE))
706 goto next_menu;
707 sym->flags &= ~SYMBOL_WRITE;
708 /* If we cannot change the symbol - skip */
709 if (!sym_is_changable(sym))
710 goto next_menu;
711 /* If symbol equals to default value - skip */
712 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
713 goto next_menu;
714
715 /*
716 * If symbol is a choice value and equals to the
717 * default for a choice - skip.
718 * But only if value is bool and equal to "y" and
719 * choice is not "optional".
720 * (If choice is "optional" then all values can be "n")
721 */
722 if (sym_is_choice_value(sym)) {
723 struct symbol *cs;
724 struct symbol *ds;
725
726 cs = prop_get_symbol(sym_get_choice_prop(sym));
727 ds = sym_choice_default(cs);
728 if (!sym_is_optional(cs) && sym == ds) {
729 if ((sym->type == S_BOOLEAN) &&
730 sym_get_tristate_value(sym) == yes)
731 goto next_menu;
732 }
733 }
734 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
735 }
736next_menu:
737 if (menu->list != NULL) {
738 menu = menu->list;
739 }
740 else if (menu->next != NULL) {
741 menu = menu->next;
742 } else {
743 while ((menu = menu->parent)) {
744 if (menu->next != NULL) {
745 menu = menu->next;
746 break;
747 }
748 }
749 }
750 }
751 fclose(out);
752 return 0;
753}
754
Patrick Georgi0588d192009-08-12 15:00:51 +0000755int conf_write(const char *name)
756{
757 FILE *out;
758 struct symbol *sym;
759 struct menu *menu;
760 const char *basename;
Patrick Georgi0588d192009-08-12 15:00:51 +0000761 const char *str;
Patrick Georgid5208402014-04-11 20:24:06 +0200762 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000763 char *env;
764
765 dirname[0] = 0;
766 if (name && name[0]) {
767 struct stat st;
768 char *slash;
769
770 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
771 strcpy(dirname, name);
772 strcat(dirname, "/");
773 basename = conf_get_configname();
774 } else if ((slash = strrchr(name, '/'))) {
775 int size = slash - name + 1;
776 memcpy(dirname, name, size);
777 dirname[size] = 0;
778 if (slash[1])
779 basename = slash + 1;
780 else
781 basename = conf_get_configname();
782 } else
783 basename = name;
784 } else
785 basename = conf_get_configname();
786
787 sprintf(newname, "%s%s", dirname, basename);
788 env = getenv("KCONFIG_OVERWRITECONFIG");
789 if (!env || !*env) {
790 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
791 out = fopen(tmpname, "w");
792 } else {
793 *tmpname = 0;
794 out = fopen(newname, "w");
795 }
796 if (!out)
797 return 1;
798
Patrick Georgid5208402014-04-11 20:24:06 +0200799 conf_write_heading(out, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000800
801 if (!conf_get_changed())
802 sym_clear_all_valid();
803
804 menu = rootmenu.list;
805 while (menu) {
806 sym = menu->sym;
807 if (!sym) {
808 if (!menu_is_visible(menu))
809 goto next;
810 str = menu_get_prompt(menu);
811 fprintf(out, "\n"
812 "#\n"
813 "# %s\n"
814 "#\n", str);
815 } else if (!(sym->flags & SYMBOL_CHOICE)) {
816 sym_calc_value(sym);
817 if (!(sym->flags & SYMBOL_WRITE))
818 goto next;
819 sym->flags &= ~SYMBOL_WRITE;
Patrick Georgid5208402014-04-11 20:24:06 +0200820
821 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000822 }
823
Patrick Georgid5208402014-04-11 20:24:06 +0200824next:
Patrick Georgi0588d192009-08-12 15:00:51 +0000825 if (menu->list) {
826 menu = menu->list;
827 continue;
828 }
829 if (menu->next)
830 menu = menu->next;
831 else while ((menu = menu->parent)) {
832 if (menu->next) {
833 menu = menu->next;
834 break;
835 }
836 }
837 }
838 fclose(out);
839
840 if (*tmpname) {
841 strcat(dirname, basename);
842 strcat(dirname, ".old");
843 rename(newname, dirname);
844 if (rename(tmpname, newname))
845 return 1;
846 }
847
Patrick Georgid5208402014-04-11 20:24:06 +0200848 conf_message(_("configuration written to %s"), newname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000849
850 sym_set_change_count(0);
851
852 return 0;
853}
854
Patrick Georgid5208402014-04-11 20:24:06 +0200855static int conf_split_config(void)
Patrick Georgi0588d192009-08-12 15:00:51 +0000856{
Patrick Georgid5208402014-04-11 20:24:06 +0200857 const char *name;
858 char path[PATH_MAX+1];
859 char pwd[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000860 char *s, *d, c;
861 struct symbol *sym;
862 struct stat sb;
863 int res, i, fd;
864
Patrick Georgid5208402014-04-11 20:24:06 +0200865 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +0000866 conf_read_simple(name, S_DEF_AUTO);
867
Patrick Georgid5208402014-04-11 20:24:06 +0200868 getcwd(pwd, sizeof(pwd));
869 name = getenv("KCONFIG_SPLITCONFIG");
870 if (!name)
871 name = "include/config";
872 if (chdir(name))
Patrick Georgi0588d192009-08-12 15:00:51 +0000873 return 1;
874
875 res = 0;
876 for_all_symbols(i, sym) {
877 sym_calc_value(sym);
878 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
879 continue;
880 if (sym->flags & SYMBOL_WRITE) {
881 if (sym->flags & SYMBOL_DEF_AUTO) {
882 /*
883 * symbol has old and new value,
884 * so compare them...
885 */
886 switch (sym->type) {
887 case S_BOOLEAN:
888 case S_TRISTATE:
889 if (sym_get_tristate_value(sym) ==
890 sym->def[S_DEF_AUTO].tri)
891 continue;
892 break;
893 case S_STRING:
894 case S_HEX:
895 case S_INT:
896 if (!strcmp(sym_get_string_value(sym),
897 sym->def[S_DEF_AUTO].val))
898 continue;
899 break;
900 default:
901 break;
902 }
903 } else {
904 /*
905 * If there is no old value, only 'no' (unset)
906 * is allowed as new value.
907 */
908 switch (sym->type) {
909 case S_BOOLEAN:
910 case S_TRISTATE:
911 if (sym_get_tristate_value(sym) == no)
912 continue;
913 break;
914 default:
915 break;
916 }
917 }
918 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
919 /* There is neither an old nor a new value. */
920 continue;
921 /* else
922 * There is an old value, but no new value ('no' (unset)
923 * isn't saved in auto.conf, so the old value is always
924 * different from 'no').
925 */
926
927 /* Replace all '_' and append ".h" */
928 s = sym->name;
929 d = path;
930 while ((c = *s++)) {
931 c = tolower(c);
932 *d++ = (c == '_') ? '/' : c;
933 }
934 strcpy(d, ".h");
935
936 /* Assume directory path already exists. */
937 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
938 if (fd == -1) {
939 if (errno != ENOENT) {
940 res = 1;
941 break;
942 }
943 /*
944 * Create directory components,
945 * unless they exist already.
946 */
947 d = path;
948 while ((d = strchr(d, '/'))) {
949 *d = 0;
950 if (stat(path, &sb) && mkdir(path, 0755)) {
951 res = 1;
952 goto out;
953 }
954 *d++ = '/';
955 }
956 /* Try it again. */
957 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
958 if (fd == -1) {
959 res = 1;
960 break;
961 }
962 }
963 close(fd);
964 }
965out:
Patrick Georgid5208402014-04-11 20:24:06 +0200966 if (chdir(pwd))
Patrick Georgi0588d192009-08-12 15:00:51 +0000967 return 1;
968
969 return res;
970}
971
972int conf_write_autoconf(void)
973{
974 struct symbol *sym;
Patrick Georgid5208402014-04-11 20:24:06 +0200975 const char *name;
976 FILE *out, *tristate, *out_h;
977 int i;
978 int print_negatives;
979
980 print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +0000981
982 sym_clear_all_valid();
983
Patrick Georgid5208402014-04-11 20:24:06 +0200984 char *dep_file = getenv("KCONFIG_DEPENDENCIES");
985 if (!dep_file)
986 dep_file = "include/config/auto.conf.cmd";
987 file_write_dep(dep_file);
Vadim Bendeburyc302d202011-10-24 14:06:23 -0700988
Patrick Georgi0588d192009-08-12 15:00:51 +0000989 if (conf_split_config())
990 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000991
Patrick Georgi129462d2014-11-17 14:56:49 +0100992 char *tmpconfig_name = malloc(PATH_MAX);
993 if (getenv("COREBOOT_BUILD_DIR")) {
994 sprintf(tmpconfig_name, "%s/.tmpconfig.XXXXXX",
995 getenv("COREBOOT_BUILD_DIR"));
996 } else {
997 tmpconfig_name = strdup(".tmpconfig.XXXXXX");
998 }
Patrick Georgid5208402014-04-11 20:24:06 +0200999 if ((i = mkstemp(tmpconfig_name)) == -1)
1000 return 1;
1001 out = fdopen(i, "w");
Patrick Georgi0588d192009-08-12 15:00:51 +00001002 if (!out)
1003 return 1;
1004
Patrick Georgi129462d2014-11-17 14:56:49 +01001005 char *tmpconfig_triname = malloc(PATH_MAX);
1006 if (getenv("COREBOOT_BUILD_DIR")) {
1007 sprintf(tmpconfig_triname, "%s/.tmpconfig_tristate.XXXXXX",
1008 getenv("COREBOOT_BUILD_DIR"));
1009 } else {
1010 tmpconfig_triname = strdup(".tmpconfig_tristate.XXXXXX");
1011 }
Patrick Georgid5208402014-04-11 20:24:06 +02001012 if ((i = mkstemp(tmpconfig_triname)) == -1)
1013 return 1;
1014 tristate = fdopen(i, "w");
1015 if (!tristate) {
Patrick Georgi0588d192009-08-12 15:00:51 +00001016 fclose(out);
1017 return 1;
1018 }
1019
Patrick Georgi129462d2014-11-17 14:56:49 +01001020 char *tmpconfig_h = malloc(PATH_MAX);
1021 if (getenv("COREBOOT_BUILD_DIR")) {
1022 sprintf(tmpconfig_h, "%s/.tmpconfig_tristate.XXXXXX",
1023 getenv("COREBOOT_BUILD_DIR"));
1024 } else {
1025 tmpconfig_h = strdup(".tmpconfig_tristate.XXXXXX");
1026 }
Patrick Georgid5208402014-04-11 20:24:06 +02001027 if ((i = mkstemp(tmpconfig_h)) == -1)
1028 return 1;
1029 out_h = fdopen(i, "w");
1030 if (!out_h) {
1031 fclose(out);
1032 fclose(tristate);
1033 return 1;
1034 }
1035
1036 conf_write_heading(out, &kconfig_printer_cb, NULL);
1037
1038 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1039
1040 conf_write_heading(out_h, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001041
1042 for_all_symbols(i, sym) {
1043 sym_calc_value(sym);
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001044 if (!sym->name)
Patrick Georgi0588d192009-08-12 15:00:51 +00001045 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001046
1047 if (!(sym->flags & SYMBOL_WRITE) && !print_negatives)
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001048 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001049
1050 /* these are safe to write out, so do it all the time */
1051 if (!(sym->flags & SYMBOL_WRITE) &&
1052 !(sym->type == S_BOOLEAN ||
1053 sym->type == S_HEX ||
1054 sym->type == S_INT))
1055 continue;
1056
1057 /* write symbol to auto.conf, tristate and header files */
1058 conf_write_symbol(out, sym, &kconfig_printer_cb, print_negatives?NULL:(void *)1);
1059
1060 conf_write_symbol(tristate, sym, &tristate_printer_cb, print_negatives?NULL:(void *)1);
1061
1062 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001063 }
1064 fclose(out);
Patrick Georgid5208402014-04-11 20:24:06 +02001065 fclose(tristate);
Patrick Georgi0588d192009-08-12 15:00:51 +00001066 fclose(out_h);
1067
Patrick Georgid5208402014-04-11 20:24:06 +02001068 name = getenv("KCONFIG_AUTOHEADER");
Patrick Georgi0588d192009-08-12 15:00:51 +00001069 if (!name)
Patrick Georgid5208402014-04-11 20:24:06 +02001070 name = "include/generated/autoconf.h";
1071 if (rename(tmpconfig_h, name))
1072 return 1;
1073 name = getenv("KCONFIG_TRISTATE");
1074 if (!name)
1075 name = "include/config/tristate.conf";
1076 if (rename(tmpconfig_triname, name))
1077 return 1;
1078 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +00001079 /*
1080 * This must be the last step, kbuild has a dependency on auto.conf
1081 * and this marks the successful completion of the previous steps.
1082 */
Patrick Georgid5208402014-04-11 20:24:06 +02001083 if (rename(tmpconfig_name, name))
Patrick Georgi0588d192009-08-12 15:00:51 +00001084 return 1;
1085
Patrick Georgi0588d192009-08-12 15:00:51 +00001086 return 0;
1087}
1088
1089static int sym_change_count;
1090static void (*conf_changed_callback)(void);
1091
1092void sym_set_change_count(int count)
1093{
1094 int _sym_change_count = sym_change_count;
1095 sym_change_count = count;
1096 if (conf_changed_callback &&
1097 (bool)_sym_change_count != (bool)count)
1098 conf_changed_callback();
1099}
1100
1101void sym_add_change_count(int count)
1102{
1103 sym_set_change_count(count + sym_change_count);
1104}
1105
1106bool conf_get_changed(void)
1107{
1108 return sym_change_count;
1109}
1110
1111void conf_set_changed_callback(void (*fn)(void))
1112{
1113 conf_changed_callback = fn;
1114}
Patrick Georgid5208402014-04-11 20:24:06 +02001115
1116static bool randomize_choice_values(struct symbol *csym)
1117{
1118 struct property *prop;
1119 struct symbol *sym;
1120 struct expr *e;
1121 int cnt, def;
1122
1123 /*
1124 * If choice is mod then we may have more items selected
1125 * and if no then no-one.
1126 * In both cases stop.
1127 */
1128 if (csym->curr.tri != yes)
1129 return false;
1130
1131 prop = sym_get_choice_prop(csym);
1132
1133 /* count entries in choice block */
1134 cnt = 0;
1135 expr_list_for_each_sym(prop->expr, e, sym)
1136 cnt++;
1137
1138 /*
1139 * find a random value and set it to yes,
1140 * set the rest to no so we have only one set
1141 */
1142 def = (rand() % cnt);
1143
1144 cnt = 0;
1145 expr_list_for_each_sym(prop->expr, e, sym) {
1146 if (def == cnt++) {
1147 sym->def[S_DEF_USER].tri = yes;
1148 csym->def[S_DEF_USER].val = sym;
1149 }
1150 else {
1151 sym->def[S_DEF_USER].tri = no;
1152 }
1153 sym->flags |= SYMBOL_DEF_USER;
1154 /* clear VALID to get value calculated */
1155 sym->flags &= ~SYMBOL_VALID;
1156 }
1157 csym->flags |= SYMBOL_DEF_USER;
1158 /* clear VALID to get value calculated */
1159 csym->flags &= ~(SYMBOL_VALID);
1160
1161 return true;
1162}
1163
1164void set_all_choice_values(struct symbol *csym)
1165{
1166 struct property *prop;
1167 struct symbol *sym;
1168 struct expr *e;
1169
1170 prop = sym_get_choice_prop(csym);
1171
1172 /*
1173 * Set all non-assinged choice values to no
1174 */
1175 expr_list_for_each_sym(prop->expr, e, sym) {
1176 if (!sym_has_value(sym))
1177 sym->def[S_DEF_USER].tri = no;
1178 }
1179 csym->flags |= SYMBOL_DEF_USER;
1180 /* clear VALID to get value calculated */
1181 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1182}
1183
1184bool conf_set_all_new_symbols(enum conf_def_mode mode)
1185{
1186 struct symbol *sym, *csym;
1187 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1188 * pty: probability of tristate = y
1189 * ptm: probability of tristate = m
1190 */
1191
1192 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1193 * below, otherwise gcc whines about
1194 * -Wmaybe-uninitialized */
1195 if (mode == def_random) {
1196 int n, p[3];
1197 char *env = getenv("KCONFIG_PROBABILITY");
1198 n = 0;
1199 while( env && *env ) {
1200 char *endp;
1201 int tmp = strtol( env, &endp, 10 );
1202 if( tmp >= 0 && tmp <= 100 ) {
1203 p[n++] = tmp;
1204 } else {
1205 errno = ERANGE;
1206 perror( "KCONFIG_PROBABILITY" );
1207 exit( 1 );
1208 }
1209 env = (*endp == ':') ? endp+1 : endp;
1210 if( n >=3 ) {
1211 break;
1212 }
1213 }
1214 switch( n ) {
1215 case 1:
1216 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1217 break;
1218 case 2:
1219 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1220 break;
1221 case 3:
1222 pby = p[0]; pty = p[1]; ptm = p[2];
1223 break;
1224 }
1225
1226 if( pty+ptm > 100 ) {
1227 errno = ERANGE;
1228 perror( "KCONFIG_PROBABILITY" );
1229 exit( 1 );
1230 }
1231 }
1232 bool has_changed = false;
1233
1234 for_all_symbols(i, sym) {
1235 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1236 continue;
1237 switch (sym_get_type(sym)) {
1238 case S_BOOLEAN:
1239 case S_TRISTATE:
1240 has_changed = true;
1241 switch (mode) {
1242 case def_yes:
1243 sym->def[S_DEF_USER].tri = yes;
1244 break;
1245 case def_mod:
1246 sym->def[S_DEF_USER].tri = mod;
1247 break;
1248 case def_no:
1249 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1250 sym->def[S_DEF_USER].tri = yes;
1251 else
1252 sym->def[S_DEF_USER].tri = no;
1253 break;
1254 case def_random:
1255 sym->def[S_DEF_USER].tri = no;
1256 cnt = rand() % 100;
1257 if (sym->type == S_TRISTATE) {
1258 if (cnt < pty)
1259 sym->def[S_DEF_USER].tri = yes;
1260 else if (cnt < (pty+ptm))
1261 sym->def[S_DEF_USER].tri = mod;
1262 } else if (cnt < pby)
1263 sym->def[S_DEF_USER].tri = yes;
1264 break;
1265 default:
1266 continue;
1267 }
1268 if (!(sym_is_choice(sym) && mode == def_random))
1269 sym->flags |= SYMBOL_DEF_USER;
1270 break;
1271 default:
1272 break;
1273 }
1274
1275 }
1276
1277 sym_clear_all_valid();
1278
1279 /*
1280 * We have different type of choice blocks.
1281 * If curr.tri equals to mod then we can select several
1282 * choice symbols in one block.
1283 * In this case we do nothing.
1284 * If curr.tri equals yes then only one symbol can be
1285 * selected in a choice block and we set it to yes,
1286 * and the rest to no.
1287 */
1288 if (mode != def_random) {
1289 for_all_symbols(i, csym) {
1290 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1291 sym_is_choice_value(csym))
1292 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1293 }
1294 }
1295
1296 for_all_symbols(i, csym) {
1297 if (sym_has_value(csym) || !sym_is_choice(csym))
1298 continue;
1299
1300 sym_calc_value(csym);
1301 if (mode == def_random)
1302 has_changed = randomize_choice_values(csym);
1303 else {
1304 set_all_choice_values(csym);
1305 has_changed = true;
1306 }
1307 }
1308
1309 return has_changed;
1310}