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
[] = ".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("BUILDROOT_CONFIG");
42 return name
? name
: ".config";
45 static char *conf_expand_value(const char *in
)
49 static char res_value
[SYMBOL_MAXLENGTH
];
50 char *dst
, name
[SYMBOL_MAXLENGTH
];
54 while ((src
= strchr(in
, '$'))) {
55 strncat(res_value
, in
, src
- in
);
58 while (isalnum(*src
) || *src
== '_')
61 sym
= sym_lookup(name
, 0);
63 strcat(res_value
, sym_get_string_value(sym
));
66 strcat(res_value
, in
);
71 char *conf_get_default_confname(void)
74 static char fullname
[PATH_MAX
+1];
77 name
= conf_expand_value(conf_defname
);
78 env
= getenv(SRCTREE
);
80 sprintf(fullname
, "%s/%s", env
, name
);
81 if (!stat(fullname
, &buf
))
87 static int conf_set_sym_val(struct symbol
*sym
, int def
, int def_flags
, char *p
)
94 sym
->def
[def
].tri
= mod
;
95 sym
->flags
|= def_flags
;
100 sym
->def
[def
].tri
= yes
;
101 sym
->flags
|= def_flags
;
105 sym
->def
[def
].tri
= no
;
106 sym
->flags
|= def_flags
;
109 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
113 for (p2
= p
; *p2
&& !isspace(*p2
); p2
++)
115 sym
->type
= S_STRING
;
121 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
126 memmove(p2
, p2
+ 1, strlen(p2
));
129 conf_warning("invalid string found");
135 if (sym_string_valid(sym
, p
)) {
136 sym
->def
[def
].val
= strdup(p
);
137 sym
->flags
|= def_flags
;
139 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
149 int conf_read_simple(const char *name
, int def
)
158 in
= zconf_fopen(name
);
160 struct property
*prop
;
162 name
= conf_get_configname();
163 in
= zconf_fopen(name
);
166 sym_add_change_count(1);
167 if (!sym_defconfig_list
)
170 for_all_defaults(sym_defconfig_list
, prop
) {
171 if (expr_calc_value(prop
->visible
.expr
) == no
||
172 prop
->expr
->type
!= E_SYMBOL
)
174 name
= conf_expand_value(prop
->expr
->left
.sym
->name
);
175 in
= zconf_fopen(name
);
178 "# using defaults found in %s\n"
188 conf_filename
= name
;
193 def_flags
= SYMBOL_DEF
<< def
;
194 for_all_symbols(i
, sym
) {
195 sym
->flags
|= SYMBOL_CHANGED
;
196 sym
->flags
&= ~(def_flags
|SYMBOL_VALID
);
197 if (sym_is_choice(sym
))
198 sym
->flags
|= def_flags
;
203 if (sym
->def
[def
].val
)
204 free(sym
->def
[def
].val
);
206 sym
->def
[def
].val
= NULL
;
207 sym
->def
[def
].tri
= no
;
211 while (fgets(line
, sizeof(line
), in
)) {
218 p
= strchr(line
+ 2, ' ');
222 if (strncmp(p
, "is not set", 10))
224 if (def
== S_DEF_USER
) {
225 sym
= sym_find(line
+ 2);
227 conf_warning("trying to assign nonexistent symbol %s", line
+ 2);
231 sym
= sym_lookup(line
+ 2, 0);
232 if (sym
->type
== S_UNKNOWN
)
233 sym
->type
= S_BOOLEAN
;
235 if (sym
->flags
& def_flags
) {
236 conf_warning("trying to reassign symbol %s", sym
->name
);
242 sym
->def
[def
].tri
= no
;
243 sym
->flags
|= def_flags
;
249 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
250 p
= strchr(line
, '=');
254 p2
= strchr(p
, '\n');
260 if (def
== S_DEF_USER
) {
261 sym
= sym_find(line
);
263 conf_warning("trying to assign nonexistent symbol %s", line
);
267 sym
= sym_lookup(line
, 0);
268 if (sym
->type
== S_UNKNOWN
)
271 if (sym
->flags
& def_flags
) {
272 conf_warning("trying to reassign symbol %s", sym
->name
);
275 if (conf_set_sym_val(sym
, def
, def_flags
, p
))
282 conf_warning("unexpected data");
285 if (sym
&& sym_is_choice_value(sym
)) {
286 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
287 switch (sym
->def
[def
].tri
) {
291 if (cs
->def
[def
].tri
== yes
) {
292 conf_warning("%s creates inconsistent choice state", sym
->name
);
293 cs
->flags
&= ~def_flags
;
297 if (cs
->def
[def
].tri
!= no
) {
298 conf_warning("%s creates inconsistent choice state", sym
->name
);
299 cs
->flags
&= ~def_flags
;
301 cs
->def
[def
].val
= sym
;
304 cs
->def
[def
].tri
= E_OR(cs
->def
[def
].tri
, sym
->def
[def
].tri
);
310 sym_calc_value(modules_sym
);
314 int conf_read(const char *name
)
317 struct property
*prop
;
321 sym_set_change_count(0);
323 if (conf_read_simple(name
, S_DEF_USER
))
326 for_all_symbols(i
, sym
) {
328 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
330 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
331 /* check that calculated value agrees with saved value */
335 if (sym
->def
[S_DEF_USER
].tri
!= sym_get_tristate_value(sym
))
337 if (!sym_is_choice(sym
))
340 if (!strcmp(sym
->curr
.val
, sym
->def
[S_DEF_USER
].val
))
344 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
345 /* no previous value and not saved */
348 /* maybe print value in verbose mode... */
350 if (!sym_is_choice(sym
))
352 /* The choice symbol only has a set value (and thus is not new)
353 * if all its visible childs have values.
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 for_all_symbols(i
, sym
) {
364 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
365 /* Reset values of generates values, so they'll appear
366 * as new, if they should become visible, but that
367 * doesn't quite work if the Kconfig and the saved
368 * configuration disagree.
370 if (sym
->visible
== no
&& !conf_unsaved
)
371 sym
->flags
&= ~SYMBOL_DEF_USER
;
376 /* Reset a string value if it's out of range */
377 if (sym_string_within_range(sym
, sym
->def
[S_DEF_USER
].val
))
379 sym
->flags
&= ~(SYMBOL_VALID
|SYMBOL_DEF_USER
);
388 sym_add_change_count(conf_warnings
|| conf_unsaved
);
393 int conf_write(const char *name
)
398 const char *basename
;
399 char dirname
[128], tmpname
[128], newname
[128];
403 int use_timestamp
= 1;
407 if (name
&& name
[0]) {
411 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
412 strcpy(dirname
, name
);
413 strcat(dirname
, "/");
414 basename
= conf_get_configname();
415 } else if ((slash
= strrchr(name
, '/'))) {
416 int size
= slash
- name
+ 1;
417 memcpy(dirname
, name
, size
);
420 basename
= slash
+ 1;
422 basename
= conf_get_configname();
426 basename
= conf_get_configname();
428 sprintf(newname
, "%s%s", dirname
, basename
);
429 env
= getenv("KCONFIG_OVERWRITECONFIG");
431 sprintf(tmpname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
432 out
= fopen(tmpname
, "w");
435 out
= fopen(newname
, "w");
440 sym
= sym_lookup("BR2_VERSION", 0);
443 env
= getenv("KCONFIG_NOTIMESTAMP");
448 "# Automatically generated make config: don't edit\n"
451 use_timestamp
? "# " : "",
452 use_timestamp
? ctime(&now
) : "");
454 if (!conf_get_changed())
455 sym_clear_all_valid();
457 menu
= rootmenu
.list
;
461 if (!menu_is_visible(menu
))
463 str
= menu_get_prompt(menu
);
468 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
470 if (!(sym
->flags
& SYMBOL_WRITE
))
472 sym
->flags
&= ~SYMBOL_WRITE
;
474 if (type
== S_TRISTATE
) {
475 sym_calc_value(modules_sym
);
476 if (modules_sym
->curr
.tri
== no
)
482 switch (sym_get_tristate_value(sym
)) {
484 fprintf(out
, "# %s is not set\n", sym
->name
);
487 fprintf(out
, "%s=m\n", sym
->name
);
490 fprintf(out
, "%s=y\n", sym
->name
);
495 str
= sym_get_string_value(sym
);
496 fprintf(out
, "%s=\"", sym
->name
);
498 l
= strcspn(str
, "\"\\");
500 fwrite(str
, l
, 1, out
);
505 fprintf(out
, "\\%c", *str
++);
510 str
= sym_get_string_value(sym
);
511 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
512 fprintf(out
, "%s=%s\n", sym
->name
, str
);
516 str
= sym_get_string_value(sym
);
517 fprintf(out
, "%s=%s\n", sym
->name
, str
);
529 else while ((menu
= menu
->parent
)) {
539 strcat(dirname
, basename
);
540 strcat(dirname
, ".old");
541 rename(newname
, dirname
);
542 if (rename(tmpname
, newname
))
547 "# configuration written to %s\n"
550 sym_set_change_count(0);
555 int conf_split_config(void)
557 char *name
, path
[128], *opwd
, *dir
, *_name
;
563 name
= getenv("KCONFIG_AUTOCONFIG");
565 name
= "include/config/auto.conf";
566 conf_read_simple(name
, S_DEF_AUTO
);
569 _name
= strdup(name
);
570 if (opwd
== NULL
|| _name
== NULL
)
572 opwd
= getcwd(opwd
, 256);
573 dir
= dirname(_name
);
584 for_all_symbols(i
, sym
) {
586 if ((sym
->flags
& SYMBOL_AUTO
) || !sym
->name
)
588 if (sym
->flags
& SYMBOL_WRITE
) {
589 if (sym
->flags
& SYMBOL_DEF_AUTO
) {
591 * symbol has old and new value,
597 if (sym_get_tristate_value(sym
) ==
598 sym
->def
[S_DEF_AUTO
].tri
)
604 if (!strcmp(sym_get_string_value(sym
),
605 sym
->def
[S_DEF_AUTO
].val
))
613 * If there is no old value, only 'no' (unset)
614 * is allowed as new value.
619 if (sym_get_tristate_value(sym
) == no
)
626 } else if (!(sym
->flags
& SYMBOL_DEF_AUTO
))
627 /* There is neither an old nor a new value. */
630 * There is an old value, but no new value ('no' (unset)
631 * isn't saved in auto.conf, so the old value is always
632 * different from 'no').
635 /* Replace all '_' and append ".h" */
640 *d
++ = (c
== '_') ? '/' : c
;
644 /* Assume directory path already exists. */
645 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
647 if (errno
!= ENOENT
) {
652 * Create directory components,
653 * unless they exist already.
656 while ((d
= strchr(d
, '/'))) {
658 if (stat(path
, &sb
) && mkdir(path
, 0755)) {
665 fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
682 int conf_write_autoconf(void)
691 sym_clear_all_valid();
693 file_write_dep(".config.cmd");
695 if (conf_split_config())
698 out
= fopen(".tmpconfig", "w");
702 out_h
= fopen(".tmpconfig.h", "w");
708 sym
= sym_lookup("BR2_VERSION", 0);
712 "# Automatically generated make config: don't edit\n"
716 fprintf(out_h
, "/*\n"
717 " * Automatically generated C config: don't edit\n"
722 for_all_symbols(i
, sym
) {
724 if (!(sym
->flags
& SYMBOL_WRITE
) || !sym
->name
)
729 switch (sym_get_tristate_value(sym
)) {
733 fprintf(out
, "%s=m\n", sym
->name
);
734 fprintf(out_h
, "#define %s_MODULE 1\n", sym
->name
);
737 fprintf(out
, "%s=y\n", sym
->name
);
738 fprintf(out_h
, "#define %s 1\n", sym
->name
);
743 str
= sym_get_string_value(sym
);
744 fprintf(out
, "%s=\"", sym
->name
);
745 fprintf(out_h
, "#define %s \"", sym
->name
);
747 l
= strcspn(str
, "\"\\");
749 fwrite(str
, l
, 1, out
);
750 fwrite(str
, l
, 1, out_h
);
755 fprintf(out
, "\\%c", *str
);
756 fprintf(out_h
, "\\%c", *str
);
760 fputs("\"\n", out_h
);
763 str
= sym_get_string_value(sym
);
764 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
765 fprintf(out
, "%s=%s\n", sym
->name
, str
);
766 fprintf(out_h
, "#define %s 0x%s\n", sym
->name
, str
);
770 str
= sym_get_string_value(sym
);
771 fprintf(out
, "%s=%s\n", sym
->name
, str
);
772 fprintf(out_h
, "#define %s %s\n", sym
->name
, str
);
781 name
= getenv("KCONFIG_AUTOHEADER");
783 name
= "include/linux/autoconf.h";
784 if (rename(".tmpconfig.h", name
))
786 name
= getenv("KCONFIG_AUTOCONFIG");
788 name
= "include/config/auto.conf";
790 * This must be the last step, kbuild has a dependency on auto.conf
791 * and this marks the successful completion of the previous steps.
793 if (rename(".tmpconfig", name
))
799 static int sym_change_count
;
800 static void (*conf_changed_callback
)(void);
802 void sym_set_change_count(int count
)
804 int _sym_change_count
= sym_change_count
;
805 sym_change_count
= count
;
806 if (conf_changed_callback
&&
807 (bool)_sym_change_count
!= (bool)count
)
808 conf_changed_callback();
811 void sym_add_change_count(int count
)
813 sym_set_change_count(count
+ sym_change_count
);
816 bool conf_get_changed(void)
818 return sym_change_count
;
821 void conf_set_changed_callback(void (*fn
)(void))
823 conf_changed_callback
= fn
;