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);
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');
198 if (def == S_DEF_USER) {
199 sym = sym_find(line + 7);
201 conf_warning("trying to assign nonexistent symbol %s", line + 7);
205 sym = sym_lookup(line + 7, 0);
206 if (sym->type == S_UNKNOWN)
209 if (sym->flags & def_flags) {
210 conf_warning("trying to reassign symbol %s", sym->name);
216 sym->def[def].tri = mod;
217 sym->flags |= def_flags;
222 sym->def[def].tri = yes;
223 sym->flags |= def_flags;
227 sym->def[def].tri = no;
228 sym->flags |= def_flags;
231 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
235 for (p2 = p; *p2 && !isspace(*p2); p2++)
237 sym->type = S_STRING;
243 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
248 memmove(p2, p2 + 1, strlen(p2));
251 conf_warning("invalid string found");
257 if (sym_string_valid(sym, p)) {
258 sym->def[def].val = strdup(p);
259 sym->flags |= def_flags;
261 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
272 conf_warning("unexpected data");
275 if (sym && sym_is_choice_value(sym)) {
276 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
277 switch (sym->def[def].tri) {
281 if (cs->def[def].tri == yes) {
282 conf_warning("%s creates inconsistent choice state", sym->name);
283 cs->flags &= ~def_flags;
287 if (cs->def[def].tri != no) {
288 conf_warning("%s creates inconsistent choice state", sym->name);
289 cs->flags &= ~def_flags;
291 cs->def[def].val = sym;
294 cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
300 sym_calc_value(modules_sym);
304 int conf_read(const char *name)
307 struct property *prop;
311 sym_change_count = 0;
313 if (conf_read_simple(name, S_DEF_USER))
316 for_all_symbols(i, sym) {
318 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
320 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
321 /* check that calculated value agrees with saved value */
325 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
327 if (!sym_is_choice(sym))
330 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
334 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
335 /* no previous value and not saved */
338 /* maybe print value in verbose mode... */
340 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
341 if (sym->visible == no)
342 sym->flags &= ~SYMBOL_DEF_USER;
347 if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
348 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
353 if (!sym_is_choice(sym))
355 prop = sym_get_choice_prop(sym);
357 for (e = prop->expr; e; e = e->left.expr)
358 if (e->right.sym->visible != no)
359 flags &= e->right.sym->flags;
360 sym->flags |= flags & SYMBOL_DEF_USER;
363 sym_change_count += conf_warnings || conf_unsaved;
368 int conf_write(const char *name)
373 const char *basename;
374 char dirname[128], tmpname[128], newname[128];
378 int use_timestamp = 1;
382 if (name && name[0]) {
386 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
387 strcpy(dirname, name);
388 strcat(dirname, "/");
389 basename = conf_get_configname();
390 } else if ((slash = strrchr(name, '/'))) {
391 int size = slash - name + 1;
392 memcpy(dirname, name, size);
395 basename = slash + 1;
397 basename = conf_get_configname();
401 basename = conf_get_configname();
403 sprintf(newname, "%s%s", dirname, basename);
404 env = getenv("KCONFIG_OVERWRITECONFIG");
406 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
407 out = fopen(tmpname, "w");
410 out = fopen(newname, "w");
415 sym = sym_lookup("KERNELVERSION", 0);
418 env = getenv("KCONFIG_NOTIMESTAMP");
423 "# Automatically generated make config: don't edit\n"
424 "# Linux kernel version: %s\n"
427 sym_get_string_value(sym),
428 use_timestamp ? "# " : "",
429 use_timestamp ? ctime(&now) : "");
431 if (!sym_change_count)
432 sym_clear_all_valid();
434 menu = rootmenu.list;
438 if (!menu_is_visible(menu))
440 str = menu_get_prompt(menu);
445 } else if (!(sym->flags & SYMBOL_CHOICE)) {
447 if (!(sym->flags & SYMBOL_WRITE))
449 sym->flags &= ~SYMBOL_WRITE;
451 if (type == S_TRISTATE) {
452 sym_calc_value(modules_sym);
453 if (modules_sym->curr.tri == no)
459 switch (sym_get_tristate_value(sym)) {
461 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
464 fprintf(out, "CONFIG_%s=m\n", sym->name);
467 fprintf(out, "CONFIG_%s=y\n", sym->name);
472 str = sym_get_string_value(sym);
473 fprintf(out, "CONFIG_%s=\"", sym->name);
475 l = strcspn(str, "\"\\");
477 fwrite(str, l, 1, out);
482 fprintf(out, "\\%c", *str++);
487 str = sym_get_string_value(sym);
488 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
489 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
493 str = sym_get_string_value(sym);
494 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
506 else while ((menu = menu->parent)) {
516 strcat(dirname, name ? name : conf_get_configname());
517 strcat(dirname, ".old");
518 rename(newname, dirname);
519 if (rename(tmpname, newname))
524 "# configuration written to %s\n"
527 sym_change_count = 0;
532 int conf_split_config(void)
534 char *name, path[128];
540 name = getenv("KCONFIG_AUTOCONFIG");
542 name = "include/config/auto.conf";
543 conf_read_simple(name, S_DEF_AUTO);
545 if (chdir("include/config"))
549 for_all_symbols(i, sym) {
551 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
553 if (sym->flags & SYMBOL_WRITE) {
554 if (sym->flags & SYMBOL_DEF_AUTO) {
556 * symbol has old and new value,
562 if (sym_get_tristate_value(sym) ==
563 sym->def[S_DEF_AUTO].tri)
569 if (!strcmp(sym_get_string_value(sym),
570 sym->def[S_DEF_AUTO].val))
578 * If there is no old value, only 'no' (unset)
579 * is allowed as new value.
584 if (sym_get_tristate_value(sym) == no)
591 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
592 /* There is neither an old nor a new value. */
595 * There is an old value, but no new value ('no' (unset)
596 * isn't saved in auto.conf, so the old value is always
597 * different from 'no').
600 /* Replace all '_' and append ".h" */
605 *d++ = (c == '_') ? '/' : c;
609 /* Assume directory path already exists. */
610 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
612 if (errno != ENOENT) {
617 * Create directory components,
618 * unless they exist already.
621 while ((d = strchr(d, '/'))) {
623 if (stat(path, &sb) && mkdir(path, 0755)) {
630 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
645 int conf_write_autoconf(void)
654 sym_clear_all_valid();
656 file_write_dep("include/config/auto.conf.cmd");
658 if (conf_split_config())
661 out = fopen(".tmpconfig", "w");
665 out_h = fopen(".tmpconfig.h", "w");
671 sym = sym_lookup("KERNELVERSION", 0);
675 "# Automatically generated make config: don't edit\n"
676 "# Linux kernel version: %s\n"
679 sym_get_string_value(sym), ctime(&now));
680 fprintf(out_h, "/*\n"
681 " * Automatically generated C config: don't edit\n"
682 " * Linux kernel version: %s\n"
685 "#define AUTOCONF_INCLUDED\n",
686 sym_get_string_value(sym), ctime(&now));
688 for_all_symbols(i, sym) {
690 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
695 switch (sym_get_tristate_value(sym)) {
699 fprintf(out, "CONFIG_%s=m\n", sym->name);
700 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
703 fprintf(out, "CONFIG_%s=y\n", sym->name);
704 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
709 str = sym_get_string_value(sym);
710 fprintf(out, "CONFIG_%s=\"", sym->name);
711 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
713 l = strcspn(str, "\"\\");
715 fwrite(str, l, 1, out);
716 fwrite(str, l, 1, out_h);
721 fprintf(out, "\\%c", *str);
722 fprintf(out_h, "\\%c", *str);
726 fputs("\"\n", out_h);
729 str = sym_get_string_value(sym);
730 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
731 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
732 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
736 str = sym_get_string_value(sym);
737 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
738 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
747 name = getenv("KCONFIG_AUTOHEADER");
749 name = "include/linux/autoconf.h";
750 if (rename(".tmpconfig.h", name))
752 name = getenv("KCONFIG_AUTOCONFIG");
754 name = "include/config/auto.conf";
756 * This must be the last step, kbuild has a dependency on auto.conf
757 * and this marks the successful completion of the previous steps.
759 if (rename(".tmpconfig", name))