2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
15 #define LKC_DIRECT_LINK
18 static void conf_warning(const char *fmt, ...)
19 __attribute__ ((format (printf, 1, 2)));
21 static const char *conf_filename;
22 static int conf_lineno, conf_warnings, conf_unsaved;
24 const char conf_defname[] = "arch/$ARCH/defconfig";
26 static void conf_warning(const char *fmt, ...)
30 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
31 vfprintf(stderr, fmt, ap);
32 fprintf(stderr, "\n");
37 const char *conf_get_configname(void)
39 char *name = getenv("KCONFIG_CONFIG");
41 return name ? name : ".config";
44 const char *conf_get_autoconfig_name(void)
46 char *name = getenv("KCONFIG_AUTOCONFIG");
48 return name ? name : "include/config/auto.conf";
51 static char *conf_expand_value(const char *in)
55 static char res_value[SYMBOL_MAXLENGTH];
56 char *dst, name[SYMBOL_MAXLENGTH];
60 while ((src = strchr(in, '$'))) {
61 strncat(res_value, in, src - in);
64 while (isalnum(*src) || *src == '_')
67 sym = sym_lookup(name, 0);
69 strcat(res_value, sym_get_string_value(sym));
72 strcat(res_value, in);
77 char *conf_get_default_confname(void)
80 static char fullname[PATH_MAX+1];
83 name = conf_expand_value(conf_defname);
84 env = getenv(SRCTREE);
86 sprintf(fullname, "%s/%s", env, name);
87 if (!stat(fullname, &buf))
93 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
100 sym->def[def].tri = mod;
101 sym->flags |= def_flags;
106 sym->def[def].tri = yes;
107 sym->flags |= def_flags;
111 sym->def[def].tri = no;
112 sym->flags |= def_flags;
115 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
119 for (p2 = p; *p2 && !isspace(*p2); p2++)
121 sym->type = S_STRING;
127 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
132 memmove(p2, p2 + 1, strlen(p2));
135 conf_warning("invalid string found");
141 if (sym_string_valid(sym, p)) {
142 sym->def[def].val = strdup(p);
143 sym->flags |= def_flags;
145 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
155 int conf_read_simple(const char *name, int def)
164 in = zconf_fopen(name);
166 struct property *prop;
168 name = conf_get_configname();
169 in = zconf_fopen(name);
172 sym_add_change_count(1);
173 if (!sym_defconfig_list) {
175 sym_calc_value(modules_sym);
179 for_all_defaults(sym_defconfig_list, prop) {
180 if (expr_calc_value(prop->visible.expr) == no ||
181 prop->expr->type != E_SYMBOL)
183 name = conf_expand_value(prop->expr->left.sym->name);
184 in = zconf_fopen(name);
187 "# using defaults found in %s\n"
197 conf_filename = name;
202 def_flags = SYMBOL_DEF << def;
203 for_all_symbols(i, sym) {
204 sym->flags |= SYMBOL_CHANGED;
205 sym->flags &= ~(def_flags|SYMBOL_VALID);
206 if (sym_is_choice(sym))
207 sym->flags |= def_flags;
212 if (sym->def[def].val)
213 free(sym->def[def].val);
215 sym->def[def].val = NULL;
216 sym->def[def].tri = no;
220 while (fgets(line, sizeof(line), in)) {
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;
258 if (memcmp(line, "CONFIG_", 7)) {
259 conf_warning("unexpected data");
262 p = strchr(line + 7, '=');
266 p2 = strchr(p, '\n');
272 if (def == S_DEF_USER) {
273 sym = sym_find(line + 7);
275 sym_add_change_count(1);
279 sym = sym_lookup(line + 7, 0);
280 if (sym->type == S_UNKNOWN)
283 if (sym->flags & def_flags) {
284 conf_warning("override: reassigning to symbol %s", sym->name);
286 if (conf_set_sym_val(sym, def, def_flags, p))
293 conf_warning("unexpected data");
296 if (sym && sym_is_choice_value(sym)) {
297 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
298 switch (sym->def[def].tri) {
302 if (cs->def[def].tri == yes) {
303 conf_warning("%s creates inconsistent choice state", sym->name);
304 cs->flags &= ~def_flags;
308 if (cs->def[def].tri != no)
309 conf_warning("override: %s changes choice state", sym->name);
310 cs->def[def].val = sym;
313 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
319 sym_calc_value(modules_sym);
323 int conf_read(const char *name)
325 struct symbol *sym, *choice_sym;
326 struct property *prop;
330 sym_set_change_count(0);
332 if (conf_read_simple(name, S_DEF_USER))
335 for_all_symbols(i, sym) {
337 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
339 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
340 /* check that calculated value agrees with saved value */
344 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
346 if (!sym_is_choice(sym))
349 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
353 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
354 /* no previous value and not saved */
357 /* maybe print value in verbose mode... */
359 if (!sym_is_choice(sym))
361 /* The choice symbol only has a set value (and thus is not new)
362 * if all its visible childs have values.
364 prop = sym_get_choice_prop(sym);
366 expr_list_for_each_sym(prop->expr, e, choice_sym)
367 if (choice_sym->visible != no)
368 flags &= choice_sym->flags;
369 sym->flags &= flags | ~SYMBOL_DEF_USER;
372 for_all_symbols(i, sym) {
373 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
374 /* Reset values of generates values, so they'll appear
375 * as new, if they should become visible, but that
376 * doesn't quite work if the Kconfig and the saved
377 * configuration disagree.
379 if (sym->visible == no && !conf_unsaved)
380 sym->flags &= ~SYMBOL_DEF_USER;
385 /* Reset a string value if it's out of range */
386 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
388 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
397 sym_add_change_count(conf_warnings || conf_unsaved);
402 /* Write a S_STRING */
403 static void conf_write_string(bool headerfile, const char *name,
404 const char *str, FILE *out)
408 fprintf(out, "#define CONFIG_%s \"", name);
410 fprintf(out, "CONFIG_%s=\"", name);
413 l = strcspn(str, "\"\\");
415 fwrite(str, l, 1, out);
420 fprintf(out, "\\%c", *str++);
425 static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
426 FILE *out, bool write_no)
433 switch (sym_get_tristate_value(sym)) {
436 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
439 fprintf(out, "CONFIG_%s=m\n", sym->name);
442 fprintf(out, "CONFIG_%s=y\n", sym->name);
447 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
451 str = sym_get_string_value(sym);
452 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
461 * Write out a minimal config.
462 * All values that has default values are skipped as this is redundant.
464 int conf_write_defconfig(const char *filename)
470 out = fopen(filename, "w");
474 sym_clear_all_valid();
476 /* Traverse all menus to find all relevant symbols */
477 menu = rootmenu.list;
483 if (!menu_is_visible(menu))
485 } else if (!sym_is_choice(sym)) {
487 if (!(sym->flags & SYMBOL_WRITE))
489 sym->flags &= ~SYMBOL_WRITE;
490 /* If we cannot change the symbol - skip */
491 if (!sym_is_changable(sym))
493 /* If symbol equals to default value - skip */
494 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
498 * If symbol is a choice value and equals to the
499 * default for a choice - skip.
500 * But only if value equal to "y".
502 if (sym_is_choice_value(sym)) {
506 cs = prop_get_symbol(sym_get_choice_prop(sym));
507 ds = sym_choice_default(cs);
509 if ((sym->type == S_BOOLEAN ||
510 sym->type == S_TRISTATE) &&
511 sym_get_tristate_value(sym) == yes)
515 conf_write_symbol(sym, sym->type, out, true);
518 if (menu->list != NULL) {
521 else if (menu->next != NULL) {
524 while ((menu = menu->parent)) {
525 if (menu->next != NULL) {
536 int conf_write(const char *name)
541 const char *basename;
543 char dirname[128], tmpname[128], newname[128];
544 enum symbol_type type;
546 int use_timestamp = 1;
550 if (name && name[0]) {
554 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
555 strcpy(dirname, name);
556 strcat(dirname, "/");
557 basename = conf_get_configname();
558 } else if ((slash = strrchr(name, '/'))) {
559 int size = slash - name + 1;
560 memcpy(dirname, name, size);
563 basename = slash + 1;
565 basename = conf_get_configname();
569 basename = conf_get_configname();
571 sprintf(newname, "%s%s", dirname, basename);
572 env = getenv("KCONFIG_OVERWRITECONFIG");
574 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
575 out = fopen(tmpname, "w");
578 out = fopen(newname, "w");
583 sym = sym_lookup("KERNELVERSION", 0);
586 env = getenv("KCONFIG_NOTIMESTAMP");
591 "# Automatically generated make config: don't edit\n"
592 "# Linux kernel version: %s\n"
595 sym_get_string_value(sym),
596 use_timestamp ? "# " : "",
597 use_timestamp ? ctime(&now) : "");
599 if (!conf_get_changed())
600 sym_clear_all_valid();
602 menu = rootmenu.list;
606 if (!menu_is_visible(menu))
608 str = menu_get_prompt(menu);
613 } else if (!(sym->flags & SYMBOL_CHOICE)) {
615 if (!(sym->flags & SYMBOL_WRITE))
617 sym->flags &= ~SYMBOL_WRITE;
619 if (type == S_TRISTATE) {
620 sym_calc_value(modules_sym);
621 if (modules_sym->curr.tri == no)
624 /* Write config symbol to file */
625 conf_write_symbol(sym, type, out, true);
635 else while ((menu = menu->parent)) {
645 strcat(dirname, basename);
646 strcat(dirname, ".old");
647 rename(newname, dirname);
648 if (rename(tmpname, newname))
653 "# configuration written to %s\n"
656 sym_set_change_count(0);
661 static int conf_split_config(void)
670 name = conf_get_autoconfig_name();
671 conf_read_simple(name, S_DEF_AUTO);
673 if (chdir("include/config"))
677 for_all_symbols(i, sym) {
679 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
681 if (sym->flags & SYMBOL_WRITE) {
682 if (sym->flags & SYMBOL_DEF_AUTO) {
684 * symbol has old and new value,
690 if (sym_get_tristate_value(sym) ==
691 sym->def[S_DEF_AUTO].tri)
697 if (!strcmp(sym_get_string_value(sym),
698 sym->def[S_DEF_AUTO].val))
706 * If there is no old value, only 'no' (unset)
707 * is allowed as new value.
712 if (sym_get_tristate_value(sym) == no)
719 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
720 /* There is neither an old nor a new value. */
723 * There is an old value, but no new value ('no' (unset)
724 * isn't saved in auto.conf, so the old value is always
725 * different from 'no').
728 /* Replace all '_' and append ".h" */
733 *d++ = (c == '_') ? '/' : c;
737 /* Assume directory path already exists. */
738 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
740 if (errno != ENOENT) {
745 * Create directory components,
746 * unless they exist already.
749 while ((d = strchr(d, '/'))) {
751 if (stat(path, &sb) && mkdir(path, 0755)) {
758 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
773 int conf_write_autoconf(void)
778 FILE *out, *tristate, *out_h;
782 sym_clear_all_valid();
784 file_write_dep("include/config/auto.conf.cmd");
786 if (conf_split_config())
789 out = fopen(".tmpconfig", "w");
793 tristate = fopen(".tmpconfig_tristate", "w");
799 out_h = fopen(".tmpconfig.h", "w");
806 sym = sym_lookup("KERNELVERSION", 0);
810 "# Automatically generated make config: don't edit\n"
811 "# Linux kernel version: %s\n"
814 sym_get_string_value(sym), ctime(&now));
815 fprintf(tristate, "#\n"
816 "# Automatically generated - do not edit\n"
818 fprintf(out_h, "/*\n"
819 " * Automatically generated C config: don't edit\n"
820 " * Linux kernel version: %s\n"
823 "#define AUTOCONF_INCLUDED\n",
824 sym_get_string_value(sym), ctime(&now));
826 for_all_symbols(i, sym) {
828 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
831 /* write symbol to config file */
832 conf_write_symbol(sym, sym->type, out, false);
834 /* update autoconf and tristate files */
838 switch (sym_get_tristate_value(sym)) {
842 fprintf(tristate, "CONFIG_%s=M\n", sym->name);
843 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
846 if (sym->type == S_TRISTATE)
847 fprintf(tristate, "CONFIG_%s=Y\n",
849 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
854 conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
857 str = sym_get_string_value(sym);
858 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
859 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
863 str = sym_get_string_value(sym);
864 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
874 name = getenv("KCONFIG_AUTOHEADER");
876 name = "include/generated/autoconf.h";
877 if (rename(".tmpconfig.h", name))
879 name = getenv("KCONFIG_TRISTATE");
881 name = "include/config/tristate.conf";
882 if (rename(".tmpconfig_tristate", name))
884 name = conf_get_autoconfig_name();
886 * This must be the last step, kbuild has a dependency on auto.conf
887 * and this marks the successful completion of the previous steps.
889 if (rename(".tmpconfig", name))
895 static int sym_change_count;
896 static void (*conf_changed_callback)(void);
898 void sym_set_change_count(int count)
900 int _sym_change_count = sym_change_count;
901 sym_change_count = count;
902 if (conf_changed_callback &&
903 (bool)_sym_change_count != (bool)count)
904 conf_changed_callback();
907 void sym_add_change_count(int count)
909 sym_set_change_count(count + sym_change_count);
912 bool conf_get_changed(void)
914 return sym_change_count;
917 void conf_set_changed_callback(void (*fn)(void))
919 conf_changed_callback = fn;
923 void conf_set_all_new_symbols(enum conf_def_mode mode)
925 struct symbol *sym, *csym;
926 struct property *prop;
930 for_all_symbols(i, sym) {
931 if (sym_has_value(sym))
933 switch (sym_get_type(sym)) {
938 sym->def[S_DEF_USER].tri = yes;
941 sym->def[S_DEF_USER].tri = mod;
944 sym->def[S_DEF_USER].tri = no;
947 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
948 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
953 if (!(sym_is_choice(sym) && mode == def_random))
954 sym->flags |= SYMBOL_DEF_USER;
962 sym_clear_all_valid();
964 if (mode != def_random)
967 * We have different type of choice blocks.
968 * If curr.tri equal to mod then we can select several
969 * choice symbols in one block.
970 * In this case we do nothing.
971 * If curr.tri equal yes then only one symbol can be
972 * selected in a choice block and we set it to yes,
973 * and the rest to no.
975 for_all_symbols(i, csym) {
976 if (sym_has_value(csym) || !sym_is_choice(csym))
979 sym_calc_value(csym);
981 if (csym->curr.tri != yes)
984 prop = sym_get_choice_prop(csym);
986 /* count entries in choice block */
988 expr_list_for_each_sym(prop->expr, e, sym)
992 * find a random value and set it to yes,
993 * set the rest to no so we have only one set
995 def = (rand() % cnt);
998 expr_list_for_each_sym(prop->expr, e, sym) {
1000 sym->def[S_DEF_USER].tri = yes;
1001 csym->def[S_DEF_USER].val = sym;
1004 sym->def[S_DEF_USER].tri = no;
1007 csym->flags |= SYMBOL_DEF_USER;
1008 /* clear VALID to get value calculated */
1009 csym->flags &= ~(SYMBOL_VALID);