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 static char *conf_expand_value(const char *in)
48 static char res_value[SYMBOL_MAXLENGTH];
49 char *dst, name[SYMBOL_MAXLENGTH];
53 while ((src = strchr(in, '$'))) {
54 strncat(res_value, in, src - in);
57 while (isalnum(*src) || *src == '_')
60 sym = sym_lookup(name, 0);
62 strcat(res_value, sym_get_string_value(sym));
65 strcat(res_value, in);
70 char *conf_get_default_confname(void)
73 static char fullname[PATH_MAX+1];
76 name = conf_expand_value(conf_defname);
77 env = getenv(SRCTREE);
79 sprintf(fullname, "%s/%s", env, name);
80 if (!stat(fullname, &buf))
86 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
93 sym->def[def].tri = mod;
94 sym->flags |= def_flags;
99 sym->def[def].tri = yes;
100 sym->flags |= def_flags;
104 sym->def[def].tri = no;
105 sym->flags |= def_flags;
108 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
112 for (p2 = p; *p2 && !isspace(*p2); p2++)
114 sym->type = S_STRING;
120 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
125 memmove(p2, p2 + 1, strlen(p2));
128 conf_warning("invalid string found");
134 if (sym_string_valid(sym, p)) {
135 sym->def[def].val = strdup(p);
136 sym->flags |= def_flags;
138 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
148 int conf_read_simple(const char *name, int def)
157 in = zconf_fopen(name);
159 struct property *prop;
161 name = conf_get_configname();
162 in = zconf_fopen(name);
165 sym_add_change_count(1);
166 if (!sym_defconfig_list)
169 for_all_defaults(sym_defconfig_list, prop) {
170 if (expr_calc_value(prop->visible.expr) == no ||
171 prop->expr->type != E_SYMBOL)
173 name = conf_expand_value(prop->expr->left.sym->name);
174 in = zconf_fopen(name);
177 "# using defaults found in %s\n"
187 conf_filename = name;
192 def_flags = SYMBOL_DEF << def;
193 for_all_symbols(i, sym) {
194 sym->flags |= SYMBOL_CHANGED;
195 sym->flags &= ~(def_flags|SYMBOL_VALID);
196 if (sym_is_choice(sym))
197 sym->flags |= def_flags;
202 if (sym->def[def].val)
203 free(sym->def[def].val);
205 sym->def[def].val = NULL;
206 sym->def[def].tri = no;
210 while (fgets(line, sizeof(line), in)) {
215 if (memcmp(line + 2, "CONFIG_", 7))
217 p = strchr(line + 9, ' ');
221 if (strncmp(p, "is not set", 10))
223 if (def == S_DEF_USER) {
224 sym = sym_find(line + 9);
228 sym = sym_lookup(line + 9, 0);
229 if (sym->type == S_UNKNOWN)
230 sym->type = S_BOOLEAN;
232 if (sym->flags & def_flags) {
233 conf_warning("override: reassigning to symbol %s", sym->name);
238 sym->def[def].tri = no;
239 sym->flags |= def_flags;
246 if (memcmp(line, "CONFIG_", 7)) {
247 conf_warning("unexpected data");
250 p = strchr(line + 7, '=');
254 p2 = strchr(p, '\n');
260 if (def == S_DEF_USER) {
261 sym = sym_find(line + 7);
265 sym = sym_lookup(line + 7, 0);
266 if (sym->type == S_UNKNOWN)
269 if (sym->flags & def_flags) {
270 conf_warning("override: reassigning to symbol %s", sym->name);
272 if (conf_set_sym_val(sym, def, def_flags, p))
279 conf_warning("unexpected data");
282 if (sym && sym_is_choice_value(sym)) {
283 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
284 switch (sym->def[def].tri) {
288 if (cs->def[def].tri == yes) {
289 conf_warning("%s creates inconsistent choice state", sym->name);
290 cs->flags &= ~def_flags;
294 if (cs->def[def].tri != no)
295 conf_warning("override: %s changes choice state", sym->name);
296 cs->def[def].val = sym;
299 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
305 sym_calc_value(modules_sym);
309 int conf_read(const char *name)
311 struct symbol *sym, *choice_sym;
312 struct property *prop;
316 sym_set_change_count(0);
318 if (conf_read_simple(name, S_DEF_USER))
321 for_all_symbols(i, sym) {
323 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
325 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
326 /* check that calculated value agrees with saved value */
330 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
332 if (!sym_is_choice(sym))
335 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
339 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
340 /* no previous value and not saved */
343 /* maybe print value in verbose mode... */
345 if (!sym_is_choice(sym))
347 /* The choice symbol only has a set value (and thus is not new)
348 * if all its visible childs have values.
350 prop = sym_get_choice_prop(sym);
352 expr_list_for_each_sym(prop->expr, e, choice_sym)
353 if (choice_sym->visible != no)
354 flags &= choice_sym->flags;
355 sym->flags &= flags | ~SYMBOL_DEF_USER;
358 for_all_symbols(i, sym) {
359 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
360 /* Reset values of generates values, so they'll appear
361 * as new, if they should become visible, but that
362 * doesn't quite work if the Kconfig and the saved
363 * configuration disagree.
365 if (sym->visible == no && !conf_unsaved)
366 sym->flags &= ~SYMBOL_DEF_USER;
371 /* Reset a string value if it's out of range */
372 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
374 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
383 sym_add_change_count(conf_warnings || conf_unsaved);
388 int conf_write(const char *name)
393 const char *basename;
394 char dirname[128], tmpname[128], newname[128];
398 int use_timestamp = 1;
402 if (name && name[0]) {
406 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
407 strcpy(dirname, name);
408 strcat(dirname, "/");
409 basename = conf_get_configname();
410 } else if ((slash = strrchr(name, '/'))) {
411 int size = slash - name + 1;
412 memcpy(dirname, name, size);
415 basename = slash + 1;
417 basename = conf_get_configname();
421 basename = conf_get_configname();
423 sprintf(newname, "%s%s", dirname, basename);
424 env = getenv("KCONFIG_OVERWRITECONFIG");
426 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
427 out = fopen(tmpname, "w");
430 out = fopen(newname, "w");
435 sym = sym_lookup("KERNELVERSION", 0);
438 env = getenv("KCONFIG_NOTIMESTAMP");
443 "# Automatically generated make config: don't edit\n"
444 "# Linux kernel version: %s\n"
447 sym_get_string_value(sym),
448 use_timestamp ? "# " : "",
449 use_timestamp ? ctime(&now) : "");
451 if (!conf_get_changed())
452 sym_clear_all_valid();
454 menu = rootmenu.list;
458 if (!menu_is_visible(menu))
460 str = menu_get_prompt(menu);
465 } else if (!(sym->flags & SYMBOL_CHOICE)) {
467 if (!(sym->flags & SYMBOL_WRITE))
469 sym->flags &= ~SYMBOL_WRITE;
471 if (type == S_TRISTATE) {
472 sym_calc_value(modules_sym);
473 if (modules_sym->curr.tri == no)
479 switch (sym_get_tristate_value(sym)) {
481 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
484 fprintf(out, "CONFIG_%s=m\n", sym->name);
487 fprintf(out, "CONFIG_%s=y\n", sym->name);
492 str = sym_get_string_value(sym);
493 fprintf(out, "CONFIG_%s=\"", sym->name);
495 l = strcspn(str, "\"\\");
497 fwrite(str, l, 1, out);
502 fprintf(out, "\\%c", *str++);
507 str = sym_get_string_value(sym);
508 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
509 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
513 str = sym_get_string_value(sym);
514 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
526 else while ((menu = menu->parent)) {
536 strcat(dirname, basename);
537 strcat(dirname, ".old");
538 rename(newname, dirname);
539 if (rename(tmpname, newname))
544 "# configuration written to %s\n"
547 sym_set_change_count(0);
552 int conf_split_config(void)
554 char *name, path[128];
560 name = getenv("KCONFIG_AUTOCONFIG");
562 name = "include/config/auto.conf";
563 conf_read_simple(name, S_DEF_AUTO);
565 if (chdir("include/config"))
569 for_all_symbols(i, sym) {
571 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
573 if (sym->flags & SYMBOL_WRITE) {
574 if (sym->flags & SYMBOL_DEF_AUTO) {
576 * symbol has old and new value,
582 if (sym_get_tristate_value(sym) ==
583 sym->def[S_DEF_AUTO].tri)
589 if (!strcmp(sym_get_string_value(sym),
590 sym->def[S_DEF_AUTO].val))
598 * If there is no old value, only 'no' (unset)
599 * is allowed as new value.
604 if (sym_get_tristate_value(sym) == no)
611 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
612 /* There is neither an old nor a new value. */
615 * There is an old value, but no new value ('no' (unset)
616 * isn't saved in auto.conf, so the old value is always
617 * different from 'no').
620 /* Replace all '_' and append ".h" */
625 *d++ = (c == '_') ? '/' : c;
629 /* Assume directory path already exists. */
630 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
632 if (errno != ENOENT) {
637 * Create directory components,
638 * unless they exist already.
641 while ((d = strchr(d, '/'))) {
643 if (stat(path, &sb) && mkdir(path, 0755)) {
650 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
665 int conf_write_autoconf(void)
674 sym_clear_all_valid();
676 file_write_dep("include/config/auto.conf.cmd");
678 if (conf_split_config())
681 out = fopen(".tmpconfig", "w");
685 out_h = fopen(".tmpconfig.h", "w");
691 sym = sym_lookup("KERNELVERSION", 0);
695 "# Automatically generated make config: don't edit\n"
696 "# Linux kernel version: %s\n"
699 sym_get_string_value(sym), ctime(&now));
700 fprintf(out_h, "/*\n"
701 " * Automatically generated C config: don't edit\n"
702 " * Linux kernel version: %s\n"
705 "#define AUTOCONF_INCLUDED\n",
706 sym_get_string_value(sym), ctime(&now));
708 for_all_symbols(i, sym) {
710 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
715 switch (sym_get_tristate_value(sym)) {
719 fprintf(out, "CONFIG_%s=m\n", sym->name);
720 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
723 fprintf(out, "CONFIG_%s=y\n", sym->name);
724 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
729 str = sym_get_string_value(sym);
730 fprintf(out, "CONFIG_%s=\"", sym->name);
731 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
733 l = strcspn(str, "\"\\");
735 fwrite(str, l, 1, out);
736 fwrite(str, l, 1, out_h);
741 fprintf(out, "\\%c", *str);
742 fprintf(out_h, "\\%c", *str);
746 fputs("\"\n", out_h);
749 str = sym_get_string_value(sym);
750 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
751 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
752 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
756 str = sym_get_string_value(sym);
757 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
758 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
767 name = getenv("KCONFIG_AUTOHEADER");
769 name = "include/linux/autoconf.h";
770 if (rename(".tmpconfig.h", name))
772 name = getenv("KCONFIG_AUTOCONFIG");
774 name = "include/config/auto.conf";
776 * This must be the last step, kbuild has a dependency on auto.conf
777 * and this marks the successful completion of the previous steps.
779 if (rename(".tmpconfig", name))
785 static int sym_change_count;
786 static void (*conf_changed_callback)(void);
788 void sym_set_change_count(int count)
790 int _sym_change_count = sym_change_count;
791 sym_change_count = count;
792 if (conf_changed_callback &&
793 (bool)_sym_change_count != (bool)count)
794 conf_changed_callback();
797 void sym_add_change_count(int count)
799 sym_set_change_count(count + sym_change_count);
802 bool conf_get_changed(void)
804 return sym_change_count;
807 void conf_set_changed_callback(void (*fn)(void))
809 conf_changed_callback = fn;
813 void conf_set_all_new_symbols(enum conf_def_mode mode)
815 struct symbol *sym, *csym;
816 struct property *prop;
820 for_all_symbols(i, sym) {
821 if (sym_has_value(sym))
823 switch (sym_get_type(sym)) {
828 sym->def[S_DEF_USER].tri = yes;
831 sym->def[S_DEF_USER].tri = mod;
834 sym->def[S_DEF_USER].tri = no;
837 sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
842 if (!sym_is_choice(sym) || mode != def_random)
843 sym->flags |= SYMBOL_DEF_USER;
852 sym_calc_value(modules_sym);
854 if (mode != def_random)
857 for_all_symbols(i, csym) {
858 if (sym_has_value(csym) || !sym_is_choice(csym))
861 sym_calc_value(csym);
862 prop = sym_get_choice_prop(csym);
866 expr_list_for_each_sym(prop->expr, e, sym) {
867 if (sym->visible == no)
870 csym->def[S_DEF_USER].val = sym;
874 if (def >= 0 || cnt < 2)
876 def = (rand() % cnt) + 1;
878 csym->flags |= SYMBOL_DEF_USER;