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)) {
226 if (memcmp(line + 2, "CONFIG_", 7))
228 p = strchr(line + 9, ' ');
232 if (strncmp(p, "is not set", 10))
234 if (def == S_DEF_USER) {
235 sym = sym_find(line + 9);
237 sym_add_change_count(1);
241 sym = sym_lookup(line + 9, 0);
242 if (sym->type == S_UNKNOWN)
243 sym->type = S_BOOLEAN;
245 if (sym->flags & def_flags) {
246 conf_warning("override: reassigning to symbol %s", sym->name);
251 sym->def[def].tri = no;
252 sym->flags |= def_flags;
259 if (memcmp(line, "CONFIG_", 7)) {
260 conf_warning("unexpected data");
263 p = strchr(line + 7, '=');
267 p2 = strchr(p, '\n');
273 if (def == S_DEF_USER) {
274 sym = sym_find(line + 7);
276 sym_add_change_count(1);
280 sym = sym_lookup(line + 7, 0);
281 if (sym->type == S_UNKNOWN)
284 if (sym->flags & def_flags) {
285 conf_warning("override: reassigning to symbol %s", sym->name);
287 if (conf_set_sym_val(sym, def, def_flags, p))
294 conf_warning("unexpected data");
297 if (sym && sym_is_choice_value(sym)) {
298 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
299 switch (sym->def[def].tri) {
303 if (cs->def[def].tri == yes) {
304 conf_warning("%s creates inconsistent choice state", sym->name);
305 cs->flags &= ~def_flags;
309 if (cs->def[def].tri != no)
310 conf_warning("override: %s changes choice state", sym->name);
311 cs->def[def].val = sym;
314 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
320 sym_calc_value(modules_sym);
324 int conf_read(const char *name)
326 struct symbol *sym, *choice_sym;
327 struct property *prop;
331 sym_set_change_count(0);
333 if (conf_read_simple(name, S_DEF_USER))
336 for_all_symbols(i, sym) {
338 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
340 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
341 /* check that calculated value agrees with saved value */
345 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
347 if (!sym_is_choice(sym))
350 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
354 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
355 /* no previous value and not saved */
358 /* maybe print value in verbose mode... */
360 if (!sym_is_choice(sym))
362 /* The choice symbol only has a set value (and thus is not new)
363 * if all its visible childs have values.
365 prop = sym_get_choice_prop(sym);
367 expr_list_for_each_sym(prop->expr, e, choice_sym)
368 if (choice_sym->visible != no)
369 flags &= choice_sym->flags;
370 sym->flags &= flags | ~SYMBOL_DEF_USER;
373 for_all_symbols(i, sym) {
374 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
375 /* Reset values of generates values, so they'll appear
376 * as new, if they should become visible, but that
377 * doesn't quite work if the Kconfig and the saved
378 * configuration disagree.
380 if (sym->visible == no && !conf_unsaved)
381 sym->flags &= ~SYMBOL_DEF_USER;
386 /* Reset a string value if it's out of range */
387 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
389 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
398 sym_add_change_count(conf_warnings || conf_unsaved);
403 /* Write a S_STRING */
404 static void conf_write_string(bool headerfile, const char *name,
405 const char *str, FILE *out)
409 fprintf(out, "#define CONFIG_%s \"", name);
411 fprintf(out, "CONFIG_%s=\"", name);
414 l = strcspn(str, "\"\\");
416 xfwrite(str, l, 1, out);
421 fprintf(out, "\\%c", *str++);
426 static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
427 FILE *out, bool write_no)
434 switch (sym_get_tristate_value(sym)) {
437 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
440 fprintf(out, "CONFIG_%s=m\n", sym->name);
443 fprintf(out, "CONFIG_%s=y\n", sym->name);
448 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
452 str = sym_get_string_value(sym);
453 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
462 * Write out a minimal config.
463 * All values that has default values are skipped as this is redundant.
465 int conf_write_defconfig(const char *filename)
471 out = fopen(filename, "w");
475 sym_clear_all_valid();
477 /* Traverse all menus to find all relevant symbols */
478 menu = rootmenu.list;
484 if (!menu_is_visible(menu))
486 } else if (!sym_is_choice(sym)) {
488 if (!(sym->flags & SYMBOL_WRITE))
490 sym->flags &= ~SYMBOL_WRITE;
491 /* If we cannot change the symbol - skip */
492 if (!sym_is_changable(sym))
494 /* If symbol equals to default value - skip */
495 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
499 * If symbol is a choice value and equals to the
500 * default for a choice - skip.
501 * But only if value is bool and equal to "y" and
502 * choice is not "optional".
503 * (If choice is "optional" then all values can be "n")
505 if (sym_is_choice_value(sym)) {
509 cs = prop_get_symbol(sym_get_choice_prop(sym));
510 ds = sym_choice_default(cs);
511 if (!sym_is_optional(cs) && sym == ds) {
512 if ((sym->type == S_BOOLEAN) &&
513 sym_get_tristate_value(sym) == yes)
517 conf_write_symbol(sym, sym->type, out, true);
520 if (menu->list != NULL) {
523 else if (menu->next != NULL) {
526 while ((menu = menu->parent)) {
527 if (menu->next != NULL) {
538 int conf_write(const char *name)
543 const char *basename;
545 char dirname[128], tmpname[128], newname[128];
546 enum symbol_type type;
548 int use_timestamp = 1;
552 if (name && name[0]) {
556 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
557 strcpy(dirname, name);
558 strcat(dirname, "/");
559 basename = conf_get_configname();
560 } else if ((slash = strrchr(name, '/'))) {
561 int size = slash - name + 1;
562 memcpy(dirname, name, size);
565 basename = slash + 1;
567 basename = conf_get_configname();
571 basename = conf_get_configname();
573 sprintf(newname, "%s%s", dirname, basename);
574 env = getenv("KCONFIG_OVERWRITECONFIG");
576 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
577 out = fopen(tmpname, "w");
580 out = fopen(newname, "w");
585 sym = sym_lookup("KERNELVERSION", 0);
588 env = getenv("KCONFIG_NOTIMESTAMP");
593 "# Automatically generated make config: don't edit\n"
594 "# Linux kernel version: %s\n"
597 sym_get_string_value(sym),
598 use_timestamp ? "# " : "",
599 use_timestamp ? ctime(&now) : "");
601 if (!conf_get_changed())
602 sym_clear_all_valid();
604 menu = rootmenu.list;
608 if (!menu_is_visible(menu))
610 str = menu_get_prompt(menu);
615 } else if (!(sym->flags & SYMBOL_CHOICE)) {
617 if (!(sym->flags & SYMBOL_WRITE))
619 sym->flags &= ~SYMBOL_WRITE;
621 if (type == S_TRISTATE) {
622 sym_calc_value(modules_sym);
623 if (modules_sym->curr.tri == no)
626 /* Write config symbol to file */
627 conf_write_symbol(sym, type, out, true);
637 else while ((menu = menu->parent)) {
647 strcat(dirname, basename);
648 strcat(dirname, ".old");
649 rename(newname, dirname);
650 if (rename(tmpname, newname))
655 "# configuration written to %s\n"
658 sym_set_change_count(0);
663 static int conf_split_config(void)
672 name = conf_get_autoconfig_name();
673 conf_read_simple(name, S_DEF_AUTO);
675 if (chdir("include/config"))
679 for_all_symbols(i, sym) {
681 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
683 if (sym->flags & SYMBOL_WRITE) {
684 if (sym->flags & SYMBOL_DEF_AUTO) {
686 * symbol has old and new value,
692 if (sym_get_tristate_value(sym) ==
693 sym->def[S_DEF_AUTO].tri)
699 if (!strcmp(sym_get_string_value(sym),
700 sym->def[S_DEF_AUTO].val))
708 * If there is no old value, only 'no' (unset)
709 * is allowed as new value.
714 if (sym_get_tristate_value(sym) == no)
721 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
722 /* There is neither an old nor a new value. */
725 * There is an old value, but no new value ('no' (unset)
726 * isn't saved in auto.conf, so the old value is always
727 * different from 'no').
730 /* Replace all '_' and append ".h" */
735 *d++ = (c == '_') ? '/' : c;
739 /* Assume directory path already exists. */
740 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
742 if (errno != ENOENT) {
747 * Create directory components,
748 * unless they exist already.
751 while ((d = strchr(d, '/'))) {
753 if (stat(path, &sb) && mkdir(path, 0755)) {
760 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
775 int conf_write_autoconf(void)
780 FILE *out, *tristate, *out_h;
784 sym_clear_all_valid();
786 file_write_dep("include/config/auto.conf.cmd");
788 if (conf_split_config())
791 out = fopen(".tmpconfig", "w");
795 tristate = fopen(".tmpconfig_tristate", "w");
801 out_h = fopen(".tmpconfig.h", "w");
808 sym = sym_lookup("KERNELVERSION", 0);
812 "# Automatically generated make config: don't edit\n"
813 "# Linux kernel version: %s\n"
816 sym_get_string_value(sym), ctime(&now));
817 fprintf(tristate, "#\n"
818 "# Automatically generated - do not edit\n"
820 fprintf(out_h, "/*\n"
821 " * Automatically generated C config: don't edit\n"
822 " * Linux kernel version: %s\n"
825 "#define AUTOCONF_INCLUDED\n",
826 sym_get_string_value(sym), ctime(&now));
828 for_all_symbols(i, sym) {
830 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
833 /* write symbol to config file */
834 conf_write_symbol(sym, sym->type, out, false);
836 /* update autoconf and tristate files */
840 switch (sym_get_tristate_value(sym)) {
844 fprintf(tristate, "CONFIG_%s=M\n", sym->name);
845 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
848 if (sym->type == S_TRISTATE)
849 fprintf(tristate, "CONFIG_%s=Y\n",
851 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
856 conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
859 str = sym_get_string_value(sym);
860 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
861 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
865 str = sym_get_string_value(sym);
866 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
876 name = getenv("KCONFIG_AUTOHEADER");
878 name = "include/generated/autoconf.h";
879 if (rename(".tmpconfig.h", name))
881 name = getenv("KCONFIG_TRISTATE");
883 name = "include/config/tristate.conf";
884 if (rename(".tmpconfig_tristate", name))
886 name = conf_get_autoconfig_name();
888 * This must be the last step, kbuild has a dependency on auto.conf
889 * and this marks the successful completion of the previous steps.
891 if (rename(".tmpconfig", name))
897 static int sym_change_count;
898 static void (*conf_changed_callback)(void);
900 void sym_set_change_count(int count)
902 int _sym_change_count = sym_change_count;
903 sym_change_count = count;
904 if (conf_changed_callback &&
905 (bool)_sym_change_count != (bool)count)
906 conf_changed_callback();
909 void sym_add_change_count(int count)
911 sym_set_change_count(count + sym_change_count);
914 bool conf_get_changed(void)
916 return sym_change_count;
919 void conf_set_changed_callback(void (*fn)(void))
921 conf_changed_callback = fn;
924 static void randomize_choice_values(struct symbol *csym)
926 struct property *prop;
932 * If choice is mod then we may have more items slected
933 * and if no then no-one.
934 * In both cases stop.
936 if (csym->curr.tri != yes)
939 prop = sym_get_choice_prop(csym);
941 /* count entries in choice block */
943 expr_list_for_each_sym(prop->expr, e, sym)
947 * find a random value and set it to yes,
948 * set the rest to no so we have only one set
950 def = (rand() % cnt);
953 expr_list_for_each_sym(prop->expr, e, sym) {
955 sym->def[S_DEF_USER].tri = yes;
956 csym->def[S_DEF_USER].val = sym;
959 sym->def[S_DEF_USER].tri = no;
962 csym->flags |= SYMBOL_DEF_USER;
963 /* clear VALID to get value calculated */
964 csym->flags &= ~(SYMBOL_VALID);
967 static void set_all_choice_values(struct symbol *csym)
969 struct property *prop;
973 prop = sym_get_choice_prop(csym);
976 * Set all non-assinged choice values to no
978 expr_list_for_each_sym(prop->expr, e, sym) {
979 if (!sym_has_value(sym))
980 sym->def[S_DEF_USER].tri = no;
982 csym->flags |= SYMBOL_DEF_USER;
983 /* clear VALID to get value calculated */
984 csym->flags &= ~(SYMBOL_VALID);
987 void conf_set_all_new_symbols(enum conf_def_mode mode)
989 struct symbol *sym, *csym;
992 for_all_symbols(i, sym) {
993 if (sym_has_value(sym))
995 switch (sym_get_type(sym)) {
1000 sym->def[S_DEF_USER].tri = yes;
1003 sym->def[S_DEF_USER].tri = mod;
1006 sym->def[S_DEF_USER].tri = no;
1009 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1010 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1015 if (!(sym_is_choice(sym) && mode == def_random))
1016 sym->flags |= SYMBOL_DEF_USER;
1024 sym_clear_all_valid();
1027 * We have different type of choice blocks.
1028 * If curr.tri equal to mod then we can select several
1029 * choice symbols in one block.
1030 * In this case we do nothing.
1031 * If curr.tri equal yes then only one symbol can be
1032 * selected in a choice block and we set it to yes,
1033 * and the rest to no.
1035 for_all_symbols(i, csym) {
1036 if (sym_has_value(csym) || !sym_is_choice(csym))
1039 sym_calc_value(csym);
1040 if (mode == def_random)
1041 randomize_choice_values(csym);
1043 set_all_choice_values(csym);