2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
16 #define LKC_DIRECT_LINK
19 static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
22 static const char *conf_filename;
23 static int conf_lineno, conf_warnings, conf_unsaved;
25 const char conf_defname[] = "arch/$ARCH/defconfig";
27 static void conf_warning(const char *fmt, ...)
31 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
32 vfprintf(stderr, fmt, ap);
33 fprintf(stderr, "\n");
38 const char *conf_get_configname(void)
40 char *name = getenv("KCONFIG_CONFIG");
42 return name ? name : ".config";
45 const char *conf_get_autoconfig_name(void)
47 char *name = getenv("KCONFIG_AUTOCONFIG");
49 return name ? name : "include/config/auto.conf";
52 static char *conf_expand_value(const char *in)
56 static char res_value[SYMBOL_MAXLENGTH];
57 char *dst, name[SYMBOL_MAXLENGTH];
61 while ((src = strchr(in, '$'))) {
62 strncat(res_value, in, src - in);
65 while (isalnum(*src) || *src == '_')
68 sym = sym_lookup(name, 0);
70 strcat(res_value, sym_get_string_value(sym));
73 strcat(res_value, in);
78 char *conf_get_default_confname(void)
81 static char fullname[PATH_MAX+1];
84 name = conf_expand_value(conf_defname);
85 env = getenv(SRCTREE);
87 sprintf(fullname, "%s/%s", env, name);
88 if (!stat(fullname, &buf))
94 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
101 sym->def[def].tri = mod;
102 sym->flags |= def_flags;
107 sym->def[def].tri = yes;
108 sym->flags |= def_flags;
112 sym->def[def].tri = no;
113 sym->flags |= def_flags;
116 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
120 for (p2 = p; *p2 && !isspace(*p2); p2++)
122 sym->type = S_STRING;
128 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
133 memmove(p2, p2 + 1, strlen(p2));
136 conf_warning("invalid string found");
142 if (sym_string_valid(sym, p)) {
143 sym->def[def].val = strdup(p);
144 sym->flags |= def_flags;
146 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
156 int conf_read_simple(const char *name, int def)
165 in = zconf_fopen(name);
167 struct property *prop;
169 name = conf_get_configname();
170 in = zconf_fopen(name);
173 sym_add_change_count(1);
174 if (!sym_defconfig_list) {
176 sym_calc_value(modules_sym);
180 for_all_defaults(sym_defconfig_list, prop) {
181 if (expr_calc_value(prop->visible.expr) == no ||
182 prop->expr->type != E_SYMBOL)
184 name = conf_expand_value(prop->expr->left.sym->name);
185 in = zconf_fopen(name);
188 "# using defaults found in %s\n"
198 conf_filename = name;
203 def_flags = SYMBOL_DEF << def;
204 for_all_symbols(i, sym) {
205 sym->flags |= SYMBOL_CHANGED;
206 sym->flags &= ~(def_flags|SYMBOL_VALID);
207 if (sym_is_choice(sym))
208 sym->flags |= def_flags;
213 if (sym->def[def].val)
214 free(sym->def[def].val);
216 sym->def[def].val = NULL;
217 sym->def[def].tri = no;
221 while (fgets(line, sizeof(line), in)) {
224 if (line[0] == '#') {
225 if (memcmp(line + 2, "CONFIG_", 7))
227 p = strchr(line + 9, ' ');
231 if (strncmp(p, "is not set", 10))
233 if (def == S_DEF_USER) {
234 sym = sym_find(line + 9);
236 sym_add_change_count(1);
240 sym = sym_lookup(line + 9, 0);
241 if (sym->type == S_UNKNOWN)
242 sym->type = S_BOOLEAN;
244 if (sym->flags & def_flags) {
245 conf_warning("override: reassigning to symbol %s", sym->name);
250 sym->def[def].tri = no;
251 sym->flags |= def_flags;
256 } else if (memcmp(line, "CONFIG_", 7) == 0) {
257 p = strchr(line + 7, '=');
261 p2 = strchr(p, '\n');
267 if (def == S_DEF_USER) {
268 sym = sym_find(line + 7);
270 sym_add_change_count(1);
274 sym = sym_lookup(line + 7, 0);
275 if (sym->type == S_UNKNOWN)
278 if (sym->flags & def_flags) {
279 conf_warning("override: reassigning to symbol %s", sym->name);
281 if (conf_set_sym_val(sym, def, def_flags, p))
284 if (line[0] != '\r' && line[0] != '\n')
285 conf_warning("unexpected data");
288 if (sym && sym_is_choice_value(sym)) {
289 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
290 switch (sym->def[def].tri) {
294 if (cs->def[def].tri == yes) {
295 conf_warning("%s creates inconsistent choice state", sym->name);
296 cs->flags &= ~def_flags;
300 if (cs->def[def].tri != no)
301 conf_warning("override: %s changes choice state", sym->name);
302 cs->def[def].val = sym;
305 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
311 sym_calc_value(modules_sym);
315 int conf_read(const char *name)
317 struct symbol *sym, *choice_sym;
318 struct property *prop;
322 sym_set_change_count(0);
324 if (conf_read_simple(name, S_DEF_USER))
327 for_all_symbols(i, sym) {
329 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
331 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
332 /* check that calculated value agrees with saved value */
336 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
338 if (!sym_is_choice(sym))
341 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
345 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
346 /* no previous value and not saved */
349 /* maybe print value in verbose mode... */
351 if (!sym_is_choice(sym))
353 /* The choice symbol only has a set value (and thus is not new)
354 * if all its visible childs have values.
356 prop = sym_get_choice_prop(sym);
358 expr_list_for_each_sym(prop->expr, e, choice_sym)
359 if (choice_sym->visible != no)
360 flags &= choice_sym->flags;
361 sym->flags &= flags | ~SYMBOL_DEF_USER;
364 for_all_symbols(i, sym) {
365 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
366 /* Reset values of generates values, so they'll appear
367 * as new, if they should become visible, but that
368 * doesn't quite work if the Kconfig and the saved
369 * configuration disagree.
371 if (sym->visible == no && !conf_unsaved)
372 sym->flags &= ~SYMBOL_DEF_USER;
377 /* Reset a string value if it's out of range */
378 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
380 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
389 sym_add_change_count(conf_warnings || conf_unsaved);
394 /* Write a S_STRING */
395 static void conf_write_string(bool headerfile, const char *name,
396 const char *str, FILE *out)
400 fprintf(out, "#define CONFIG_%s \"", name);
402 fprintf(out, "CONFIG_%s=\"", name);
405 l = strcspn(str, "\"\\");
407 xfwrite(str, l, 1, out);
412 fprintf(out, "\\%c", *str++);
417 static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
418 FILE *out, bool write_no)
425 switch (sym_get_tristate_value(sym)) {
428 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
431 fprintf(out, "CONFIG_%s=m\n", sym->name);
434 fprintf(out, "CONFIG_%s=y\n", sym->name);
439 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
443 str = sym_get_string_value(sym);
444 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
453 * Write out a minimal config.
454 * All values that has default values are skipped as this is redundant.
456 int conf_write_defconfig(const char *filename)
462 out = fopen(filename, "w");
466 sym_clear_all_valid();
468 /* Traverse all menus to find all relevant symbols */
469 menu = rootmenu.list;
475 if (!menu_is_visible(menu))
477 } else if (!sym_is_choice(sym)) {
479 if (!(sym->flags & SYMBOL_WRITE))
481 sym->flags &= ~SYMBOL_WRITE;
482 /* If we cannot change the symbol - skip */
483 if (!sym_is_changable(sym))
485 /* If symbol equals to default value - skip */
486 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
490 * If symbol is a choice value and equals to the
491 * default for a choice - skip.
492 * But only if value is bool and equal to "y" and
493 * choice is not "optional".
494 * (If choice is "optional" then all values can be "n")
496 if (sym_is_choice_value(sym)) {
500 cs = prop_get_symbol(sym_get_choice_prop(sym));
501 ds = sym_choice_default(cs);
502 if (!sym_is_optional(cs) && sym == ds) {
503 if ((sym->type == S_BOOLEAN) &&
504 sym_get_tristate_value(sym) == yes)
508 conf_write_symbol(sym, sym->type, out, true);
511 if (menu->list != NULL) {
514 else if (menu->next != NULL) {
517 while ((menu = menu->parent)) {
518 if (menu->next != NULL) {
529 int conf_write(const char *name)
534 const char *basename;
536 char dirname[128], tmpname[128], newname[128];
537 enum symbol_type type;
539 int use_timestamp = 1;
543 if (name && name[0]) {
547 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
548 strcpy(dirname, name);
549 strcat(dirname, "/");
550 basename = conf_get_configname();
551 } else if ((slash = strrchr(name, '/'))) {
552 int size = slash - name + 1;
553 memcpy(dirname, name, size);
556 basename = slash + 1;
558 basename = conf_get_configname();
562 basename = conf_get_configname();
564 sprintf(newname, "%s%s", dirname, basename);
565 env = getenv("KCONFIG_OVERWRITECONFIG");
567 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
568 out = fopen(tmpname, "w");
571 out = fopen(newname, "w");
576 sym = sym_lookup("KERNELVERSION", 0);
579 env = getenv("KCONFIG_NOTIMESTAMP");
584 "# Automatically generated make config: don't edit\n"
585 "# Linux kernel version: %s\n"
588 sym_get_string_value(sym),
589 use_timestamp ? "# " : "",
590 use_timestamp ? ctime(&now) : "");
592 if (!conf_get_changed())
593 sym_clear_all_valid();
595 menu = rootmenu.list;
599 if (!menu_is_visible(menu))
601 str = menu_get_prompt(menu);
606 } else if (!(sym->flags & SYMBOL_CHOICE)) {
608 if (!(sym->flags & SYMBOL_WRITE))
610 sym->flags &= ~SYMBOL_WRITE;
612 if (type == S_TRISTATE) {
613 sym_calc_value(modules_sym);
614 if (modules_sym->curr.tri == no)
617 /* Write config symbol to file */
618 conf_write_symbol(sym, type, out, true);
628 else while ((menu = menu->parent)) {
638 strcat(dirname, basename);
639 strcat(dirname, ".old");
640 rename(newname, dirname);
641 if (rename(tmpname, newname))
646 "# configuration written to %s\n"
649 sym_set_change_count(0);
654 static int conf_split_config(void)
663 name = conf_get_autoconfig_name();
664 conf_read_simple(name, S_DEF_AUTO);
666 if (chdir("include/config"))
670 for_all_symbols(i, sym) {
672 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
674 if (sym->flags & SYMBOL_WRITE) {
675 if (sym->flags & SYMBOL_DEF_AUTO) {
677 * symbol has old and new value,
683 if (sym_get_tristate_value(sym) ==
684 sym->def[S_DEF_AUTO].tri)
690 if (!strcmp(sym_get_string_value(sym),
691 sym->def[S_DEF_AUTO].val))
699 * If there is no old value, only 'no' (unset)
700 * is allowed as new value.
705 if (sym_get_tristate_value(sym) == no)
712 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
713 /* There is neither an old nor a new value. */
716 * There is an old value, but no new value ('no' (unset)
717 * isn't saved in auto.conf, so the old value is always
718 * different from 'no').
721 /* Replace all '_' and append ".h" */
726 *d++ = (c == '_') ? '/' : c;
730 /* Assume directory path already exists. */
731 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
733 if (errno != ENOENT) {
738 * Create directory components,
739 * unless they exist already.
742 while ((d = strchr(d, '/'))) {
744 if (stat(path, &sb) && mkdir(path, 0755)) {
751 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
766 int conf_write_autoconf(void)
771 FILE *out, *tristate, *out_h;
775 sym_clear_all_valid();
777 file_write_dep("include/config/auto.conf.cmd");
779 if (conf_split_config())
782 out = fopen(".tmpconfig", "w");
786 tristate = fopen(".tmpconfig_tristate", "w");
792 out_h = fopen(".tmpconfig.h", "w");
799 sym = sym_lookup("KERNELVERSION", 0);
803 "# Automatically generated make config: don't edit\n"
804 "# Linux kernel version: %s\n"
807 sym_get_string_value(sym), ctime(&now));
808 fprintf(tristate, "#\n"
809 "# Automatically generated - do not edit\n"
811 fprintf(out_h, "/*\n"
812 " * Automatically generated C config: don't edit\n"
813 " * Linux kernel version: %s\n"
816 "#define AUTOCONF_INCLUDED\n",
817 sym_get_string_value(sym), ctime(&now));
819 for_all_symbols(i, sym) {
821 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
824 /* write symbol to config file */
825 conf_write_symbol(sym, sym->type, out, false);
827 /* update autoconf and tristate files */
831 switch (sym_get_tristate_value(sym)) {
835 fprintf(tristate, "CONFIG_%s=M\n", sym->name);
836 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
839 if (sym->type == S_TRISTATE)
840 fprintf(tristate, "CONFIG_%s=Y\n",
842 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
847 conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
850 str = sym_get_string_value(sym);
851 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
852 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
856 str = sym_get_string_value(sym);
857 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
867 name = getenv("KCONFIG_AUTOHEADER");
869 name = "include/generated/autoconf.h";
870 if (rename(".tmpconfig.h", name))
872 name = getenv("KCONFIG_TRISTATE");
874 name = "include/config/tristate.conf";
875 if (rename(".tmpconfig_tristate", name))
877 name = conf_get_autoconfig_name();
879 * This must be the last step, kbuild has a dependency on auto.conf
880 * and this marks the successful completion of the previous steps.
882 if (rename(".tmpconfig", name))
888 static int sym_change_count;
889 static void (*conf_changed_callback)(void);
891 void sym_set_change_count(int count)
893 int _sym_change_count = sym_change_count;
894 sym_change_count = count;
895 if (conf_changed_callback &&
896 (bool)_sym_change_count != (bool)count)
897 conf_changed_callback();
900 void sym_add_change_count(int count)
902 sym_set_change_count(count + sym_change_count);
905 bool conf_get_changed(void)
907 return sym_change_count;
910 void conf_set_changed_callback(void (*fn)(void))
912 conf_changed_callback = fn;
915 static void randomize_choice_values(struct symbol *csym)
917 struct property *prop;
923 * If choice is mod then we may have more items slected
924 * and if no then no-one.
925 * In both cases stop.
927 if (csym->curr.tri != yes)
930 prop = sym_get_choice_prop(csym);
932 /* count entries in choice block */
934 expr_list_for_each_sym(prop->expr, e, sym)
938 * find a random value and set it to yes,
939 * set the rest to no so we have only one set
941 def = (rand() % cnt);
944 expr_list_for_each_sym(prop->expr, e, sym) {
946 sym->def[S_DEF_USER].tri = yes;
947 csym->def[S_DEF_USER].val = sym;
950 sym->def[S_DEF_USER].tri = no;
953 csym->flags |= SYMBOL_DEF_USER;
954 /* clear VALID to get value calculated */
955 csym->flags &= ~(SYMBOL_VALID);
958 static void set_all_choice_values(struct symbol *csym)
960 struct property *prop;
964 prop = sym_get_choice_prop(csym);
967 * Set all non-assinged choice values to no
969 expr_list_for_each_sym(prop->expr, e, sym) {
970 if (!sym_has_value(sym))
971 sym->def[S_DEF_USER].tri = no;
973 csym->flags |= SYMBOL_DEF_USER;
974 /* clear VALID to get value calculated */
975 csym->flags &= ~(SYMBOL_VALID);
978 void conf_set_all_new_symbols(enum conf_def_mode mode)
980 struct symbol *sym, *csym;
983 for_all_symbols(i, sym) {
984 if (sym_has_value(sym))
986 switch (sym_get_type(sym)) {
991 sym->def[S_DEF_USER].tri = yes;
994 sym->def[S_DEF_USER].tri = mod;
997 sym->def[S_DEF_USER].tri = no;
1000 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1001 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1006 if (!(sym_is_choice(sym) && mode == def_random))
1007 sym->flags |= SYMBOL_DEF_USER;
1015 sym_clear_all_valid();
1018 * We have different type of choice blocks.
1019 * If curr.tri equal to mod then we can select several
1020 * choice symbols in one block.
1021 * In this case we do nothing.
1022 * If curr.tri equal yes then only one symbol can be
1023 * selected in a choice block and we set it to yes,
1024 * and the rest to no.
1026 for_all_symbols(i, csym) {
1027 if (sym_has_value(csym) || !sym_is_choice(csym))
1030 sym_calc_value(csym);
1031 if (mode == def_random)
1032 randomize_choice_values(csym);
1034 set_all_choice_values(csym);