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 int conf_read_simple(const char *name, int def)
95 in = zconf_fopen(name);
97 struct property *prop;
99 name = conf_get_configname();
100 in = zconf_fopen(name);
103 sym_add_change_count(1);
104 if (!sym_defconfig_list)
107 for_all_defaults(sym_defconfig_list, prop) {
108 if (expr_calc_value(prop->visible.expr) == no ||
109 prop->expr->type != E_SYMBOL)
111 name = conf_expand_value(prop->expr->left.sym->name);
112 in = zconf_fopen(name);
115 "# using defaults found in %s\n"
125 conf_filename = name;
130 def_flags = SYMBOL_DEF << def;
131 for_all_symbols(i, sym) {
132 sym->flags |= SYMBOL_CHANGED;
133 sym->flags &= ~(def_flags|SYMBOL_VALID);
134 if (sym_is_choice(sym))
135 sym->flags |= def_flags;
140 if (sym->def[def].val)
141 free(sym->def[def].val);
143 sym->def[def].val = NULL;
144 sym->def[def].tri = no;
148 while (fgets(line, sizeof(line), in)) {
153 if (memcmp(line + 2, "CONFIG_", 7))
155 p = strchr(line + 9, ' ');
159 if (strncmp(p, "is not set", 10))
161 if (def == S_DEF_USER) {
162 sym = sym_find(line + 9);
164 conf_warning("trying to assign nonexistent symbol %s", line + 9);
168 sym = sym_lookup(line + 9, 0);
169 if (sym->type == S_UNKNOWN)
170 sym->type = S_BOOLEAN;
172 if (sym->flags & def_flags) {
173 conf_warning("trying to reassign symbol %s", sym->name);
179 sym->def[def].tri = no;
180 sym->flags |= def_flags;
187 if (memcmp(line, "CONFIG_", 7)) {
188 conf_warning("unexpected data");
191 p = strchr(line + 7, '=');
195 p2 = strchr(p, '\n');
201 if (def == S_DEF_USER) {
202 sym = sym_find(line + 7);
204 conf_warning("trying to assign nonexistent symbol %s", line + 7);
208 sym = sym_lookup(line + 7, 0);
209 if (sym->type == S_UNKNOWN)
212 if (sym->flags & def_flags) {
213 conf_warning("trying to reassign symbol %s", sym->name);
219 sym->def[def].tri = mod;
220 sym->flags |= def_flags;
225 sym->def[def].tri = yes;
226 sym->flags |= def_flags;
230 sym->def[def].tri = no;
231 sym->flags |= def_flags;
234 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
238 for (p2 = p; *p2 && !isspace(*p2); p2++)
240 sym->type = S_STRING;
246 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
251 memmove(p2, p2 + 1, strlen(p2));
254 conf_warning("invalid string found");
260 if (sym_string_valid(sym, p)) {
261 sym->def[def].val = strdup(p);
262 sym->flags |= def_flags;
264 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
276 conf_warning("unexpected data");
279 if (sym && sym_is_choice_value(sym)) {
280 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
281 switch (sym->def[def].tri) {
285 if (cs->def[def].tri == yes) {
286 conf_warning("%s creates inconsistent choice state", sym->name);
287 cs->flags &= ~def_flags;
291 if (cs->def[def].tri != no) {
292 conf_warning("%s creates inconsistent choice state", sym->name);
293 cs->flags &= ~def_flags;
295 cs->def[def].val = sym;
298 cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
304 sym_calc_value(modules_sym);
308 int conf_read(const char *name)
311 struct property *prop;
315 sym_set_change_count(0);
317 if (conf_read_simple(name, S_DEF_USER))
320 for_all_symbols(i, sym) {
322 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
324 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
325 /* check that calculated value agrees with saved value */
329 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
331 if (!sym_is_choice(sym))
334 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
338 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
339 /* no previous value and not saved */
342 /* maybe print value in verbose mode... */
344 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
345 if (sym->visible == no)
346 sym->flags &= ~SYMBOL_DEF_USER;
351 if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
352 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
357 if (!sym_is_choice(sym))
359 prop = sym_get_choice_prop(sym);
361 for (e = prop->expr; e; e = e->left.expr)
362 if (e->right.sym->visible != no)
363 flags &= e->right.sym->flags;
364 sym->flags &= flags | ~SYMBOL_DEF_USER;
367 sym_add_change_count(conf_warnings || conf_unsaved);
372 int conf_write(const char *name)
377 const char *basename;
378 char dirname[128], tmpname[128], newname[128];
382 int use_timestamp = 1;
386 if (name && name[0]) {
390 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
391 strcpy(dirname, name);
392 strcat(dirname, "/");
393 basename = conf_get_configname();
394 } else if ((slash = strrchr(name, '/'))) {
395 int size = slash - name + 1;
396 memcpy(dirname, name, size);
399 basename = slash + 1;
401 basename = conf_get_configname();
405 basename = conf_get_configname();
407 sprintf(newname, "%s%s", dirname, basename);
408 env = getenv("KCONFIG_OVERWRITECONFIG");
410 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
411 out = fopen(tmpname, "w");
414 out = fopen(newname, "w");
419 sym = sym_lookup("KERNELVERSION", 0);
422 env = getenv("KCONFIG_NOTIMESTAMP");
427 "# Automatically generated make config: don't edit\n"
428 "# Linux kernel version: %s\n"
431 sym_get_string_value(sym),
432 use_timestamp ? "# " : "",
433 use_timestamp ? ctime(&now) : "");
435 if (!conf_get_changed())
436 sym_clear_all_valid();
438 menu = rootmenu.list;
442 if (!menu_is_visible(menu))
444 str = menu_get_prompt(menu);
449 } else if (!(sym->flags & SYMBOL_CHOICE)) {
451 if (!(sym->flags & SYMBOL_WRITE))
453 sym->flags &= ~SYMBOL_WRITE;
455 if (type == S_TRISTATE) {
456 sym_calc_value(modules_sym);
457 if (modules_sym->curr.tri == no)
463 switch (sym_get_tristate_value(sym)) {
465 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
468 fprintf(out, "CONFIG_%s=m\n", sym->name);
471 fprintf(out, "CONFIG_%s=y\n", sym->name);
476 str = sym_get_string_value(sym);
477 fprintf(out, "CONFIG_%s=\"", sym->name);
479 l = strcspn(str, "\"\\");
481 fwrite(str, l, 1, out);
486 fprintf(out, "\\%c", *str++);
491 str = sym_get_string_value(sym);
492 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
493 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
497 str = sym_get_string_value(sym);
498 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
510 else while ((menu = menu->parent)) {
520 strcat(dirname, basename);
521 strcat(dirname, ".old");
522 rename(newname, dirname);
523 if (rename(tmpname, newname))
528 "# configuration written to %s\n"
531 sym_set_change_count(0);
536 int conf_split_config(void)
538 char *name, path[128];
544 name = getenv("KCONFIG_AUTOCONFIG");
546 name = "include/config/auto.conf";
547 conf_read_simple(name, S_DEF_AUTO);
549 if (chdir("include/config"))
553 for_all_symbols(i, sym) {
555 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
557 if (sym->flags & SYMBOL_WRITE) {
558 if (sym->flags & SYMBOL_DEF_AUTO) {
560 * symbol has old and new value,
566 if (sym_get_tristate_value(sym) ==
567 sym->def[S_DEF_AUTO].tri)
573 if (!strcmp(sym_get_string_value(sym),
574 sym->def[S_DEF_AUTO].val))
582 * If there is no old value, only 'no' (unset)
583 * is allowed as new value.
588 if (sym_get_tristate_value(sym) == no)
595 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
596 /* There is neither an old nor a new value. */
599 * There is an old value, but no new value ('no' (unset)
600 * isn't saved in auto.conf, so the old value is always
601 * different from 'no').
604 /* Replace all '_' and append ".h" */
609 *d++ = (c == '_') ? '/' : c;
613 /* Assume directory path already exists. */
614 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
616 if (errno != ENOENT) {
621 * Create directory components,
622 * unless they exist already.
625 while ((d = strchr(d, '/'))) {
627 if (stat(path, &sb) && mkdir(path, 0755)) {
634 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
649 int conf_write_autoconf(void)
658 sym_clear_all_valid();
660 file_write_dep("include/config/auto.conf.cmd");
662 if (conf_split_config())
665 out = fopen(".tmpconfig", "w");
669 out_h = fopen(".tmpconfig.h", "w");
675 sym = sym_lookup("KERNELVERSION", 0);
679 "# Automatically generated make config: don't edit\n"
680 "# Linux kernel version: %s\n"
683 sym_get_string_value(sym), ctime(&now));
684 fprintf(out_h, "/*\n"
685 " * Automatically generated C config: don't edit\n"
686 " * Linux kernel version: %s\n"
689 "#define AUTOCONF_INCLUDED\n",
690 sym_get_string_value(sym), ctime(&now));
692 for_all_symbols(i, sym) {
694 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
699 switch (sym_get_tristate_value(sym)) {
703 fprintf(out, "CONFIG_%s=m\n", sym->name);
704 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
707 fprintf(out, "CONFIG_%s=y\n", sym->name);
708 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
713 str = sym_get_string_value(sym);
714 fprintf(out, "CONFIG_%s=\"", sym->name);
715 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
717 l = strcspn(str, "\"\\");
719 fwrite(str, l, 1, out);
720 fwrite(str, l, 1, out_h);
725 fprintf(out, "\\%c", *str);
726 fprintf(out_h, "\\%c", *str);
730 fputs("\"\n", out_h);
733 str = sym_get_string_value(sym);
734 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
735 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
736 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
740 str = sym_get_string_value(sym);
741 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
742 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
751 name = getenv("KCONFIG_AUTOHEADER");
753 name = "include/linux/autoconf.h";
754 if (rename(".tmpconfig.h", name))
756 name = getenv("KCONFIG_AUTOCONFIG");
758 name = "include/config/auto.conf";
760 * This must be the last step, kbuild has a dependency on auto.conf
761 * and this marks the successful completion of the previous steps.
763 if (rename(".tmpconfig", name))
769 static int sym_change_count;
770 static void (*conf_changed_callback)(void);
772 void sym_set_change_count(int count)
774 int _sym_change_count = sym_change_count;
775 sym_change_count = count;
776 if (conf_changed_callback &&
777 (bool)_sym_change_count != (bool)count)
778 conf_changed_callback();
781 void sym_add_change_count(int count)
783 sym_set_change_count(count + sym_change_count);
786 bool conf_get_changed(void)
788 return sym_change_count;
791 void conf_set_changed_callback(void (*fn)(void))
793 conf_changed_callback = fn;