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
)
176 for_all_defaults(sym_defconfig_list
, prop
) {
177 if (expr_calc_value(prop
->visible
.expr
) == no
||
178 prop
->expr
->type
!= E_SYMBOL
)
180 name
= conf_expand_value(prop
->expr
->left
.sym
->name
);
181 in
= zconf_fopen(name
);
184 "# using defaults found in %s\n"
194 conf_filename
= name
;
199 def_flags
= SYMBOL_DEF
<< def
;
200 for_all_symbols(i
, sym
) {
201 sym
->flags
|= SYMBOL_CHANGED
;
202 sym
->flags
&= ~(def_flags
|SYMBOL_VALID
);
203 if (sym_is_choice(sym
))
204 sym
->flags
|= def_flags
;
209 if (sym
->def
[def
].val
)
210 free(sym
->def
[def
].val
);
212 sym
->def
[def
].val
= NULL
;
213 sym
->def
[def
].tri
= no
;
217 while (fgets(line
, sizeof(line
), in
)) {
222 if (memcmp(line
+ 2, "CONFIG_", 7))
224 p
= strchr(line
+ 9, ' ');
228 if (strncmp(p
, "is not set", 10))
230 if (def
== S_DEF_USER
) {
231 sym
= sym_find(line
+ 9);
233 sym_add_change_count(1);
237 sym
= sym_lookup(line
+ 9, 0);
238 if (sym
->type
== S_UNKNOWN
)
239 sym
->type
= S_BOOLEAN
;
241 if (sym
->flags
& def_flags
) {
242 conf_warning("override: reassigning to symbol %s", sym
->name
);
247 sym
->def
[def
].tri
= no
;
248 sym
->flags
|= def_flags
;
255 if (memcmp(line
, "CONFIG_", 7)) {
256 conf_warning("unexpected data");
259 p
= strchr(line
+ 7, '=');
263 p2
= strchr(p
, '\n');
269 if (def
== S_DEF_USER
) {
270 sym
= sym_find(line
+ 7);
272 sym_add_change_count(1);
276 sym
= sym_lookup(line
+ 7, 0);
277 if (sym
->type
== S_UNKNOWN
)
280 if (sym
->flags
& def_flags
) {
281 conf_warning("override: reassigning to symbol %s", sym
->name
);
283 if (conf_set_sym_val(sym
, def
, def_flags
, p
))
290 conf_warning("unexpected data");
293 if (sym
&& sym_is_choice_value(sym
)) {
294 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
295 switch (sym
->def
[def
].tri
) {
299 if (cs
->def
[def
].tri
== yes
) {
300 conf_warning("%s creates inconsistent choice state", sym
->name
);
301 cs
->flags
&= ~def_flags
;
305 if (cs
->def
[def
].tri
!= no
)
306 conf_warning("override: %s changes choice state", sym
->name
);
307 cs
->def
[def
].val
= sym
;
310 cs
->def
[def
].tri
= EXPR_OR(cs
->def
[def
].tri
, sym
->def
[def
].tri
);
316 sym_calc_value(modules_sym
);
320 int conf_read(const char *name
)
322 struct symbol
*sym
, *choice_sym
;
323 struct property
*prop
;
327 sym_set_change_count(0);
329 if (conf_read_simple(name
, S_DEF_USER
))
332 for_all_symbols(i
, sym
) {
334 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
336 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
337 /* check that calculated value agrees with saved value */
341 if (sym
->def
[S_DEF_USER
].tri
!= sym_get_tristate_value(sym
))
343 if (!sym_is_choice(sym
))
346 if (!strcmp(sym
->curr
.val
, sym
->def
[S_DEF_USER
].val
))
350 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
351 /* no previous value and not saved */
354 /* maybe print value in verbose mode... */
356 if (!sym_is_choice(sym
))
358 /* The choice symbol only has a set value (and thus is not new)
359 * if all its visible childs have values.
361 prop
= sym_get_choice_prop(sym
);
363 expr_list_for_each_sym(prop
->expr
, e
, choice_sym
)
364 if (choice_sym
->visible
!= no
)
365 flags
&= choice_sym
->flags
;
366 sym
->flags
&= flags
| ~SYMBOL_DEF_USER
;
369 for_all_symbols(i
, sym
) {
370 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
371 /* Reset values of generates values, so they'll appear
372 * as new, if they should become visible, but that
373 * doesn't quite work if the Kconfig and the saved
374 * configuration disagree.
376 if (sym
->visible
== no
&& !conf_unsaved
)
377 sym
->flags
&= ~SYMBOL_DEF_USER
;
382 /* Reset a string value if it's out of range */
383 if (sym_string_within_range(sym
, sym
->def
[S_DEF_USER
].val
))
385 sym
->flags
&= ~(SYMBOL_VALID
|SYMBOL_DEF_USER
);
394 sym_add_change_count(conf_warnings
|| conf_unsaved
);
399 int conf_write(const char *name
)
404 const char *basename
;
405 char dirname
[128], tmpname
[128], newname
[128];
409 int use_timestamp
= 1;
413 if (name
&& name
[0]) {
417 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
418 strcpy(dirname
, name
);
419 strcat(dirname
, "/");
420 basename
= conf_get_configname();
421 } else if ((slash
= strrchr(name
, '/'))) {
422 int size
= slash
- name
+ 1;
423 memcpy(dirname
, name
, size
);
426 basename
= slash
+ 1;
428 basename
= conf_get_configname();
432 basename
= conf_get_configname();
434 sprintf(newname
, "%s%s", dirname
, basename
);
435 env
= getenv("KCONFIG_OVERWRITECONFIG");
437 sprintf(tmpname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
438 out
= fopen(tmpname
, "w");
441 out
= fopen(newname
, "w");
446 sym
= sym_lookup("KERNELVERSION", 0);
449 env
= getenv("KCONFIG_NOTIMESTAMP");
454 "# Automatically generated make config: don't edit\n"
455 "# Linux kernel version: %s\n"
458 sym_get_string_value(sym
),
459 use_timestamp
? "# " : "",
460 use_timestamp
? ctime(&now
) : "");
462 if (!conf_get_changed())
463 sym_clear_all_valid();
465 menu
= rootmenu
.list
;
469 if (!menu_is_visible(menu
))
471 str
= menu_get_prompt(menu
);
476 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
478 if (!(sym
->flags
& SYMBOL_WRITE
))
480 sym
->flags
&= ~SYMBOL_WRITE
;
482 if (type
== S_TRISTATE
) {
483 sym_calc_value(modules_sym
);
484 if (modules_sym
->curr
.tri
== no
)
490 switch (sym_get_tristate_value(sym
)) {
492 fprintf(out
, "# CONFIG_%s is not set\n", sym
->name
);
495 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
498 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
503 str
= sym_get_string_value(sym
);
504 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
506 l
= strcspn(str
, "\"\\");
508 fwrite(str
, l
, 1, out
);
513 fprintf(out
, "\\%c", *str
++);
518 str
= sym_get_string_value(sym
);
519 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
520 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
524 str
= sym_get_string_value(sym
);
525 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
537 else while ((menu
= menu
->parent
)) {
547 strcat(dirname
, basename
);
548 strcat(dirname
, ".old");
549 rename(newname
, dirname
);
550 if (rename(tmpname
, newname
))
555 "# configuration written to %s\n"
558 sym_set_change_count(0);
563 static int conf_split_config(void)
572 name
= conf_get_autoconfig_name();
573 conf_read_simple(name
, S_DEF_AUTO
);
575 if (chdir("include/config"))
579 for_all_symbols(i
, sym
) {
581 if ((sym
->flags
& SYMBOL_AUTO
) || !sym
->name
)
583 if (sym
->flags
& SYMBOL_WRITE
) {
584 if (sym
->flags
& SYMBOL_DEF_AUTO
) {
586 * symbol has old and new value,
592 if (sym_get_tristate_value(sym
) ==
593 sym
->def
[S_DEF_AUTO
].tri
)
599 if (!strcmp(sym_get_string_value(sym
),
600 sym
->def
[S_DEF_AUTO
].val
))
608 * If there is no old value, only 'no' (unset)
609 * is allowed as new value.
614 if (sym_get_tristate_value(sym
) == no
)
621 } else if (!(sym
->flags
& SYMBOL_DEF_AUTO
))
622 /* There is neither an old nor a new value. */
625 * There is an old value, but no new value ('no' (unset)
626 * isn't saved in auto.conf, so the old value is always
627 * different from 'no').
630 /* Replace all '_' and append ".h" */
635 *d
++ = (c
== '_') ? '/' : c
;
639 /* Assume directory path already exists. */
640 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
642 if (errno
!= ENOENT
) {
647 * Create directory components,
648 * unless they exist already.
651 while ((d
= strchr(d
, '/'))) {
653 if (stat(path
, &sb
) && mkdir(path
, 0755)) {
660 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
675 int fprintf2(FILE *f1
, FILE *f2
, const char *fmt
, ...)
681 vfprintf(f1
, fmt
, ap
);
684 res
= vfprintf(f2
, fmt
, ap
);
690 int conf_write_autoconf(void)
695 FILE *out
, *out2
, *out_h
;
699 sym_clear_all_valid();
701 file_write_dep("include/config/auto.conf.cmd");
703 if (conf_split_config())
706 out
= fopen(".tmpconfig", "w");
710 out2
= fopen(".tmpconfig2", "w");
716 out_h
= fopen(".tmpconfig.h", "w");
723 sym
= sym_lookup("KERNELVERSION", 0);
726 fprintf2(out
, out2
, "#\n"
727 "# Automatically generated make config: don't edit\n"
728 "# Linux kernel version: %s\n"
731 sym_get_string_value(sym
), ctime(&now
));
732 fprintf(out_h
, "/*\n"
733 " * Automatically generated C config: don't edit\n"
734 " * Linux kernel version: %s\n"
737 "#define AUTOCONF_INCLUDED\n",
738 sym_get_string_value(sym
), ctime(&now
));
740 for_all_symbols(i
, sym
) {
742 if (!(sym
->flags
& SYMBOL_WRITE
) || !sym
->name
)
747 switch (sym_get_tristate_value(sym
)) {
751 fprintf2(out
, out2
, "CONFIG_%s=m\n",
753 fprintf(out_h
, "#define CONFIG_%s_MODULE 1\n", sym
->name
);
756 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
757 fprintf(out2
, "CONFIG_%s=%c\n", sym
->name
,
758 sym
->type
== S_BOOLEAN
? 'y' : 'Y');
759 fprintf(out_h
, "#define CONFIG_%s 1\n", sym
->name
);
764 str
= sym_get_string_value(sym
);
765 fprintf2(out
, out2
, "CONFIG_%s=\"", sym
->name
);
766 fprintf(out_h
, "#define CONFIG_%s \"", sym
->name
);
768 l
= strcspn(str
, "\"\\");
770 fwrite(str
, l
, 1, out
);
771 fwrite(str
, l
, 1, out2
);
772 fwrite(str
, l
, 1, out_h
);
777 fprintf2(out
, out2
, "\\%c", *str
);
778 fprintf(out_h
, "\\%c", *str
);
783 fputs("\"\n", out_h
);
786 str
= sym_get_string_value(sym
);
787 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
788 fprintf2(out
, out2
, "CONFIG_%s=%s\n",
790 fprintf(out_h
, "#define CONFIG_%s 0x%s\n", sym
->name
, str
);
794 str
= sym_get_string_value(sym
);
795 fprintf2(out
, out2
, "CONFIG_%s=%s\n", sym
->name
, str
);
796 fprintf(out_h
, "#define CONFIG_%s %s\n", sym
->name
, str
);
806 name
= getenv("KCONFIG_AUTOHEADER");
808 name
= "include/generated/autoconf.h";
809 if (rename(".tmpconfig.h", name
))
811 name
= getenv("KCONFIG_AUTOCONFIG2");
813 name
= "include/config/auto2.conf";
814 if (rename(".tmpconfig2", name
))
816 name
= conf_get_autoconfig_name();
818 * This must be the last step, kbuild has a dependency on auto.conf
819 * and this marks the successful completion of the previous steps.
821 if (rename(".tmpconfig", name
))
827 static int sym_change_count
;
828 static void (*conf_changed_callback
)(void);
830 void sym_set_change_count(int count
)
832 int _sym_change_count
= sym_change_count
;
833 sym_change_count
= count
;
834 if (conf_changed_callback
&&
835 (bool)_sym_change_count
!= (bool)count
)
836 conf_changed_callback();
839 void sym_add_change_count(int count
)
841 sym_set_change_count(count
+ sym_change_count
);
844 bool conf_get_changed(void)
846 return sym_change_count
;
849 void conf_set_changed_callback(void (*fn
)(void))
851 conf_changed_callback
= fn
;
855 void conf_set_all_new_symbols(enum conf_def_mode mode
)
857 struct symbol
*sym
, *csym
;
858 struct property
*prop
;
862 for_all_symbols(i
, sym
) {
863 if (sym_has_value(sym
))
865 switch (sym_get_type(sym
)) {
870 sym
->def
[S_DEF_USER
].tri
= yes
;
873 sym
->def
[S_DEF_USER
].tri
= mod
;
876 sym
->def
[S_DEF_USER
].tri
= no
;
879 sym
->def
[S_DEF_USER
].tri
= (tristate
)(rand() % 3);
884 if (!(sym_is_choice(sym
) && mode
== def_random
))
885 sym
->flags
|= SYMBOL_DEF_USER
;
893 sym_clear_all_valid();
895 if (mode
!= def_random
)
898 * We have different type of choice blocks.
899 * If curr.tri equal to mod then we can select several
900 * choice symbols in one block.
901 * In this case we do nothing.
902 * If curr.tri equal yes then only one symbol can be
903 * selected in a choice block and we set it to yes,
904 * and the rest to no.
906 for_all_symbols(i
, csym
) {
907 if (sym_has_value(csym
) || !sym_is_choice(csym
))
910 sym_calc_value(csym
);
912 if (csym
->curr
.tri
!= yes
)
915 prop
= sym_get_choice_prop(csym
);
917 /* count entries in choice block */
919 expr_list_for_each_sym(prop
->expr
, e
, sym
)
923 * find a random value and set it to yes,
924 * set the rest to no so we have only one set
926 def
= (rand() % cnt
);
929 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
931 sym
->def
[S_DEF_USER
].tri
= yes
;
932 csym
->def
[S_DEF_USER
].val
= sym
;
935 sym
->def
[S_DEF_USER
].tri
= no
;
938 csym
->flags
|= SYMBOL_DEF_USER
;
939 /* clear VALID to get value calculated */
940 csym
->flags
&= ~(SYMBOL_VALID
);