1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
22 /* return true if 'path' exists, false otherwise */
23 static bool is_present(const char *path
)
27 return !stat(path
, &st
);
30 /* return true if 'path' exists and it is a directory, false otherwise */
31 static bool is_dir(const char *path
)
38 return S_ISDIR(st
.st_mode
);
41 /* return true if the given two files are the same, false otherwise */
42 static bool is_same(const char *file1
, const char *file2
)
49 fd1
= open(file1
, O_RDONLY
);
53 fd2
= open(file2
, O_RDONLY
);
57 ret
= fstat(fd1
, &st1
);
60 ret
= fstat(fd2
, &st2
);
64 if (st1
.st_size
!= st2
.st_size
)
67 map1
= mmap(NULL
, st1
.st_size
, PROT_READ
, MAP_PRIVATE
, fd1
, 0);
68 if (map1
== MAP_FAILED
)
71 map2
= mmap(NULL
, st2
.st_size
, PROT_READ
, MAP_PRIVATE
, fd2
, 0);
72 if (map2
== MAP_FAILED
)
75 if (bcmp(map1
, map2
, st1
.st_size
))
88 * Create the parent directory of the given path.
90 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
92 static int make_parent_dir(const char *path
)
94 char tmp
[PATH_MAX
+ 1];
97 strncpy(tmp
, path
, sizeof(tmp
));
98 tmp
[sizeof(tmp
) - 1] = 0;
100 /* Remove the base name. Just return if nothing is left */
101 p
= strrchr(tmp
, '/');
106 /* Just in case it is an absolute path */
111 while ((p
= strchr(p
, '/'))) {
114 /* skip if the directory exists */
115 if (!is_dir(tmp
) && mkdir(tmp
, 0755))
126 static char depfile_path
[PATH_MAX
];
127 static size_t depfile_prefix_len
;
129 /* touch depfile for symbol 'name' */
130 static int conf_touch_dep(const char *name
)
135 /* check overflow: prefix + name + '\0' must fit in buffer. */
136 if (depfile_prefix_len
+ strlen(name
) + 1 > sizeof(depfile_path
))
139 d
= depfile_path
+ depfile_prefix_len
;
142 /* Assume directory path already exists. */
143 fd
= open(depfile_path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
148 ret
= make_parent_dir(depfile_path
);
153 fd
= open(depfile_path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
162 struct conf_printer
{
163 void (*print_symbol
)(FILE *, struct symbol
*, const char *, void *);
164 void (*print_comment
)(FILE *, const char *, void *);
167 static void conf_warning(const char *fmt
, ...)
168 __attribute__ ((format (printf
, 1, 2)));
170 static void conf_message(const char *fmt
, ...)
171 __attribute__ ((format (printf
, 1, 2)));
173 static const char *conf_filename
;
174 static int conf_lineno
, conf_warnings
;
176 static void conf_warning(const char *fmt
, ...)
180 fprintf(stderr
, "%s:%d:warning: ", conf_filename
, conf_lineno
);
181 vfprintf(stderr
, fmt
, ap
);
182 fprintf(stderr
, "\n");
187 static void conf_default_message_callback(const char *s
)
194 static void (*conf_message_callback
)(const char *s
) =
195 conf_default_message_callback
;
196 void conf_set_message_callback(void (*fn
)(const char *s
))
198 conf_message_callback
= fn
;
201 static void conf_message(const char *fmt
, ...)
206 if (!conf_message_callback
)
211 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
212 conf_message_callback(buf
);
216 const char *conf_get_configname(void)
218 char *name
= getenv("KCONFIG_CONFIG");
220 return name
? name
: ".config";
223 static const char *conf_get_autoconfig_name(void)
225 char *name
= getenv("KCONFIG_AUTOCONFIG");
227 return name
? name
: "include/config/auto.conf";
230 static int conf_set_sym_val(struct symbol
*sym
, int def
, int def_flags
, char *p
)
237 sym
->def
[def
].tri
= mod
;
238 sym
->flags
|= def_flags
;
244 sym
->def
[def
].tri
= yes
;
245 sym
->flags
|= def_flags
;
249 sym
->def
[def
].tri
= no
;
250 sym
->flags
|= def_flags
;
253 if (def
!= S_DEF_AUTO
)
254 conf_warning("symbol value '%s' invalid for %s",
260 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
265 memmove(p2
, p2
+ 1, strlen(p2
));
268 if (def
!= S_DEF_AUTO
)
269 conf_warning("invalid string found");
275 if (sym_string_valid(sym
, p
)) {
276 sym
->def
[def
].val
= xstrdup(p
);
277 sym
->flags
|= def_flags
;
279 if (def
!= S_DEF_AUTO
)
280 conf_warning("symbol value '%s' invalid for %s",
291 #define LINE_GROWTH 16
292 static int add_byte(int c
, char **lineptr
, size_t slen
, size_t *n
)
295 size_t new_size
= slen
+ 1;
297 new_size
+= LINE_GROWTH
- 1;
299 nline
= xrealloc(*lineptr
, new_size
);
307 (*lineptr
)[slen
] = c
;
312 static ssize_t
compat_getline(char **lineptr
, size_t *n
, FILE *stream
)
314 char *line
= *lineptr
;
318 int c
= getc(stream
);
322 if (add_byte(c
, &line
, slen
, n
) < 0)
327 if (add_byte('\0', &line
, slen
, n
) < 0)
334 if (add_byte(c
, &line
, slen
, n
) < 0)
346 void conf_reset(int def
)
351 def_flags
= SYMBOL_DEF
<< def
;
352 for_all_symbols(i
, sym
) {
353 sym
->flags
|= SYMBOL_CHANGED
;
354 sym
->flags
&= ~(def_flags
|SYMBOL_VALID
);
355 if (sym_is_choice(sym
))
356 sym
->flags
|= def_flags
;
361 if (sym
->def
[def
].val
)
362 free(sym
->def
[def
].val
);
365 sym
->def
[def
].val
= NULL
;
366 sym
->def
[def
].tri
= no
;
371 int conf_read_simple(const char *name
, int def
)
375 size_t line_asize
= 0;
381 in
= zconf_fopen(name
);
385 name
= conf_get_configname();
386 in
= zconf_fopen(name
);
389 conf_set_changed(true);
391 env
= getenv("KCONFIG_DEFCONFIG_LIST");
398 while (isspace(*env
))
405 while (*p
&& !isspace(*p
))
408 is_last
= (*p
== '\0');
412 in
= zconf_fopen(env
);
414 conf_message("using defaults found in %s",
429 conf_filename
= name
;
433 def_flags
= SYMBOL_DEF
<< def
;
436 while (compat_getline(&line
, &line_asize
, in
) != -1) {
439 if (line
[0] == '#') {
440 if (memcmp(line
+ 2, CONFIG_
, strlen(CONFIG_
)))
442 p
= strchr(line
+ 2 + strlen(CONFIG_
), ' ');
446 if (strncmp(p
, "is not set", 10))
448 if (def
== S_DEF_USER
) {
449 sym
= sym_find(line
+ 2 + strlen(CONFIG_
));
451 conf_set_changed(true);
455 sym
= sym_lookup(line
+ 2 + strlen(CONFIG_
), 0);
456 if (sym
->type
== S_UNKNOWN
)
457 sym
->type
= S_BOOLEAN
;
462 sym
->def
[def
].tri
= no
;
463 sym
->flags
|= def_flags
;
468 } else if (memcmp(line
, CONFIG_
, strlen(CONFIG_
)) == 0) {
469 p
= strchr(line
+ strlen(CONFIG_
), '=');
473 p2
= strchr(p
, '\n');
480 sym
= sym_find(line
+ strlen(CONFIG_
));
482 if (def
== S_DEF_AUTO
)
484 * Reading from include/config/auto.conf
485 * If CONFIG_FOO previously existed in
486 * auto.conf but it is missing now,
487 * include/config/FOO must be touched.
489 conf_touch_dep(line
+ strlen(CONFIG_
));
491 conf_set_changed(true);
495 if (conf_set_sym_val(sym
, def
, def_flags
, p
))
498 if (line
[0] != '\r' && line
[0] != '\n')
499 conf_warning("unexpected data: %.*s",
500 (int)strcspn(line
, "\r\n"), line
);
505 if (sym
&& sym_is_choice_value(sym
)) {
506 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
507 switch (sym
->def
[def
].tri
) {
511 if (cs
->def
[def
].tri
== yes
) {
512 conf_warning("%s creates inconsistent choice state", sym
->name
);
513 cs
->flags
&= ~def_flags
;
517 if (cs
->def
[def
].tri
!= no
)
518 conf_warning("override: %s changes choice state", sym
->name
);
519 cs
->def
[def
].val
= sym
;
522 cs
->def
[def
].tri
= EXPR_OR(cs
->def
[def
].tri
, sym
->def
[def
].tri
);
530 int conf_read(const char *name
)
533 int conf_unsaved
= 0;
536 conf_set_changed(false);
538 if (conf_read_simple(name
, S_DEF_USER
)) {
539 sym_calc_value(modules_sym
);
543 sym_calc_value(modules_sym
);
545 for_all_symbols(i
, sym
) {
547 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_NO_WRITE
))
549 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
550 /* check that calculated value agrees with saved value */
554 if (sym
->def
[S_DEF_USER
].tri
== sym_get_tristate_value(sym
))
558 if (!strcmp(sym
->curr
.val
, sym
->def
[S_DEF_USER
].val
))
562 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
563 /* no previous value and not saved */
566 /* maybe print value in verbose mode... */
569 for_all_symbols(i
, sym
) {
570 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
571 /* Reset values of generates values, so they'll appear
572 * as new, if they should become visible, but that
573 * doesn't quite work if the Kconfig and the saved
574 * configuration disagree.
576 if (sym
->visible
== no
&& !conf_unsaved
)
577 sym
->flags
&= ~SYMBOL_DEF_USER
;
582 /* Reset a string value if it's out of range */
583 if (sym_string_within_range(sym
, sym
->def
[S_DEF_USER
].val
))
585 sym
->flags
&= ~(SYMBOL_VALID
|SYMBOL_DEF_USER
);
594 if (conf_warnings
|| conf_unsaved
)
595 conf_set_changed(true);
601 * Kconfig configuration printer
603 * This printer is used when generating the resulting configuration after
604 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
605 * passing a non-NULL argument to the printer.
609 kconfig_print_symbol(FILE *fp
, struct symbol
*sym
, const char *value
, void *arg
)
616 bool skip_unset
= (arg
!= NULL
);
619 fprintf(fp
, "# %s%s is not set\n",
628 fprintf(fp
, "%s%s=%s\n", CONFIG_
, sym
->name
, value
);
632 kconfig_print_comment(FILE *fp
, const char *value
, void *arg
)
634 const char *p
= value
;
638 l
= strcspn(p
, "\n");
642 xfwrite(p
, l
, 1, fp
);
651 static struct conf_printer kconfig_printer_cb
=
653 .print_symbol
= kconfig_print_symbol
,
654 .print_comment
= kconfig_print_comment
,
660 * This printer is used when generating the `include/generated/autoconf.h' file.
663 header_print_symbol(FILE *fp
, struct symbol
*sym
, const char *value
, void *arg
)
669 const char *suffix
= "";
678 fprintf(fp
, "#define %s%s%s 1\n",
679 CONFIG_
, sym
->name
, suffix
);
684 const char *prefix
= "";
686 if (value
[0] != '0' || (value
[1] != 'x' && value
[1] != 'X'))
688 fprintf(fp
, "#define %s%s %s%s\n",
689 CONFIG_
, sym
->name
, prefix
, value
);
694 fprintf(fp
, "#define %s%s %s\n",
695 CONFIG_
, sym
->name
, value
);
704 header_print_comment(FILE *fp
, const char *value
, void *arg
)
706 const char *p
= value
;
711 l
= strcspn(p
, "\n");
715 xfwrite(p
, l
, 1, fp
);
722 fprintf(fp
, " */\n");
725 static struct conf_printer header_printer_cb
=
727 .print_symbol
= header_print_symbol
,
728 .print_comment
= header_print_comment
,
731 static void conf_write_symbol(FILE *fp
, struct symbol
*sym
,
732 struct conf_printer
*printer
, void *printer_arg
)
740 str
= sym_get_string_value(sym
);
741 str
= sym_escape_string_value(str
);
742 printer
->print_symbol(fp
, sym
, str
, printer_arg
);
746 str
= sym_get_string_value(sym
);
747 printer
->print_symbol(fp
, sym
, str
, printer_arg
);
752 conf_write_heading(FILE *fp
, struct conf_printer
*printer
, void *printer_arg
)
756 snprintf(buf
, sizeof(buf
),
758 "Automatically generated file; DO NOT EDIT.\n"
760 rootmenu
.prompt
->text
);
762 printer
->print_comment(fp
, buf
, printer_arg
);
766 * Write out a minimal config.
767 * All values that has default values are skipped as this is redundant.
769 int conf_write_defconfig(const char *filename
)
775 out
= fopen(filename
, "w");
779 sym_clear_all_valid();
781 /* Traverse all menus to find all relevant symbols */
782 menu
= rootmenu
.list
;
788 if (!menu_is_visible(menu
))
790 } else if (!sym_is_choice(sym
)) {
792 if (!(sym
->flags
& SYMBOL_WRITE
))
794 sym
->flags
&= ~SYMBOL_WRITE
;
795 /* If we cannot change the symbol - skip */
796 if (!sym_is_changeable(sym
))
798 /* If symbol equals to default value - skip */
799 if (strcmp(sym_get_string_value(sym
), sym_get_string_default(sym
)) == 0)
803 * If symbol is a choice value and equals to the
804 * default for a choice - skip.
805 * But only if value is bool and equal to "y" and
806 * choice is not "optional".
807 * (If choice is "optional" then all values can be "n")
809 if (sym_is_choice_value(sym
)) {
813 cs
= prop_get_symbol(sym_get_choice_prop(sym
));
814 ds
= sym_choice_default(cs
);
815 if (!sym_is_optional(cs
) && sym
== ds
) {
816 if ((sym
->type
== S_BOOLEAN
) &&
817 sym_get_tristate_value(sym
) == yes
)
821 conf_write_symbol(out
, sym
, &kconfig_printer_cb
, NULL
);
824 if (menu
->list
!= NULL
) {
827 else if (menu
->next
!= NULL
) {
830 while ((menu
= menu
->parent
)) {
831 if (menu
->next
!= NULL
) {
842 int conf_write(const char *name
)
848 char tmpname
[PATH_MAX
+ 1], oldname
[PATH_MAX
+ 1];
851 bool need_newline
= false;
854 name
= conf_get_configname();
857 fprintf(stderr
, "config name is empty\n");
862 fprintf(stderr
, "%s: Is a directory\n", name
);
866 if (make_parent_dir(name
))
869 env
= getenv("KCONFIG_OVERWRITECONFIG");
872 out
= fopen(name
, "w");
874 snprintf(tmpname
, sizeof(tmpname
), "%s.%d.tmp",
875 name
, (int)getpid());
876 out
= fopen(tmpname
, "w");
881 conf_write_heading(out
, &kconfig_printer_cb
, NULL
);
883 if (!conf_get_changed())
884 sym_clear_all_valid();
886 menu
= rootmenu
.list
;
890 if (!menu_is_visible(menu
))
892 str
= menu_get_prompt(menu
);
897 need_newline
= false;
898 } else if (!(sym
->flags
& SYMBOL_CHOICE
) &&
899 !(sym
->flags
& SYMBOL_WRITTEN
)) {
901 if (!(sym
->flags
& SYMBOL_WRITE
))
905 need_newline
= false;
907 sym
->flags
|= SYMBOL_WRITTEN
;
908 conf_write_symbol(out
, sym
, &kconfig_printer_cb
, NULL
);
918 else while ((menu
= menu
->parent
)) {
919 if (!menu
->sym
&& menu_is_visible(menu
) &&
921 str
= menu_get_prompt(menu
);
922 fprintf(out
, "# end of %s\n", str
);
933 for_all_symbols(i
, sym
)
934 sym
->flags
&= ~SYMBOL_WRITTEN
;
937 if (is_same(name
, tmpname
)) {
938 conf_message("No change to %s", name
);
940 conf_set_changed(false);
944 snprintf(oldname
, sizeof(oldname
), "%s.old", name
);
945 rename(name
, oldname
);
946 if (rename(tmpname
, name
))
950 conf_message("configuration written to %s", name
);
952 conf_set_changed(false);
957 /* write a dependency file as used by kbuild to track dependencies */
958 static int conf_write_dep(const char *name
)
963 out
= fopen("..config.tmp", "w");
966 fprintf(out
, "deps_config := \\\n");
967 for (file
= file_list
; file
; file
= file
->next
) {
969 fprintf(out
, "\t%s \\\n", file
->name
);
971 fprintf(out
, "\t%s\n", file
->name
);
973 fprintf(out
, "\n%s: \\\n"
974 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
976 env_write_dep(out
, conf_get_autoconfig_name());
978 fprintf(out
, "\n$(deps_config): ;\n");
981 if (make_parent_dir(name
))
983 rename("..config.tmp", name
);
987 static int conf_touch_deps(void)
993 strcpy(depfile_path
, "include/config/");
994 depfile_prefix_len
= strlen(depfile_path
);
996 name
= conf_get_autoconfig_name();
997 conf_read_simple(name
, S_DEF_AUTO
);
998 sym_calc_value(modules_sym
);
1000 for_all_symbols(i
, sym
) {
1001 sym_calc_value(sym
);
1002 if ((sym
->flags
& SYMBOL_NO_WRITE
) || !sym
->name
)
1004 if (sym
->flags
& SYMBOL_WRITE
) {
1005 if (sym
->flags
& SYMBOL_DEF_AUTO
) {
1007 * symbol has old and new value,
1008 * so compare them...
1010 switch (sym
->type
) {
1013 if (sym_get_tristate_value(sym
) ==
1014 sym
->def
[S_DEF_AUTO
].tri
)
1020 if (!strcmp(sym_get_string_value(sym
),
1021 sym
->def
[S_DEF_AUTO
].val
))
1029 * If there is no old value, only 'no' (unset)
1030 * is allowed as new value.
1032 switch (sym
->type
) {
1035 if (sym_get_tristate_value(sym
) == no
)
1042 } else if (!(sym
->flags
& SYMBOL_DEF_AUTO
))
1043 /* There is neither an old nor a new value. */
1046 * There is an old value, but no new value ('no' (unset)
1047 * isn't saved in auto.conf, so the old value is always
1048 * different from 'no').
1051 res
= conf_touch_dep(sym
->name
);
1059 int conf_write_autoconf(int overwrite
)
1063 const char *autoconf_name
= conf_get_autoconfig_name();
1067 #ifndef OPENWRT_DOES_NOT_WANT_THIS
1070 if (!overwrite
&& is_present(autoconf_name
))
1073 conf_write_dep("include/config/auto.conf.cmd");
1075 if (conf_touch_deps())
1078 out
= fopen(".tmpconfig", "w");
1082 out_h
= fopen(".tmpconfig.h", "w");
1088 conf_write_heading(out
, &kconfig_printer_cb
, NULL
);
1089 conf_write_heading(out_h
, &header_printer_cb
, NULL
);
1091 for_all_symbols(i
, sym
) {
1092 sym_calc_value(sym
);
1093 if (!(sym
->flags
& SYMBOL_WRITE
) || !sym
->name
)
1096 /* write symbols to auto.conf and autoconf.h */
1097 conf_write_symbol(out
, sym
, &kconfig_printer_cb
, (void *)1);
1098 conf_write_symbol(out_h
, sym
, &header_printer_cb
, NULL
);
1103 name
= getenv("KCONFIG_AUTOHEADER");
1105 name
= "include/generated/autoconf.h";
1106 if (make_parent_dir(name
))
1108 if (rename(".tmpconfig.h", name
))
1111 if (make_parent_dir(autoconf_name
))
1114 * This must be the last step, kbuild has a dependency on auto.conf
1115 * and this marks the successful completion of the previous steps.
1117 if (rename(".tmpconfig", autoconf_name
))
1123 static bool conf_changed
;
1124 static void (*conf_changed_callback
)(void);
1126 void conf_set_changed(bool val
)
1128 if (conf_changed_callback
&& conf_changed
!= val
)
1129 conf_changed_callback();
1134 bool conf_get_changed(void)
1136 return conf_changed
;
1139 void conf_set_changed_callback(void (*fn
)(void))
1141 conf_changed_callback
= fn
;
1144 void set_all_choice_values(struct symbol
*csym
)
1146 struct property
*prop
;
1150 prop
= sym_get_choice_prop(csym
);
1153 * Set all non-assinged choice values to no
1155 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
1156 if (!sym_has_value(sym
))
1157 sym
->def
[S_DEF_USER
].tri
= no
;
1159 csym
->flags
|= SYMBOL_DEF_USER
;
1160 /* clear VALID to get value calculated */
1161 csym
->flags
&= ~(SYMBOL_VALID
| SYMBOL_NEED_SET_CHOICE_VALUES
);