1 // SPDX-License-Identifier: GPL-2.0
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 int conf_read_simple(const char *name, int def)
350 size_t line_asize = 0;
356 in = zconf_fopen(name);
360 name = conf_get_configname();
361 in = zconf_fopen(name);
364 conf_set_changed(true);
366 env = getenv("KCONFIG_DEFCONFIG_LIST");
373 while (isspace(*env))
380 while (*p && !isspace(*p))
383 is_last = (*p == '\0');
387 in = zconf_fopen(env);
389 conf_message("using defaults found in %s",
404 conf_filename = name;
408 def_flags = SYMBOL_DEF << def;
409 for_all_symbols(i, sym) {
410 sym->flags |= SYMBOL_CHANGED;
411 sym->flags &= ~(def_flags|SYMBOL_VALID);
412 if (sym_is_choice(sym))
413 sym->flags |= def_flags;
418 if (sym->def[def].val)
419 free(sym->def[def].val);
422 sym->def[def].val = NULL;
423 sym->def[def].tri = no;
427 while (compat_getline(&line, &line_asize, in) != -1) {
430 if (line[0] == '#') {
431 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
433 p = strchr(line + 2 + strlen(CONFIG_), ' ');
437 if (strncmp(p, "is not set", 10))
439 if (def == S_DEF_USER) {
440 sym = sym_find(line + 2 + strlen(CONFIG_));
442 conf_set_changed(true);
446 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
447 if (sym->type == S_UNKNOWN)
448 sym->type = S_BOOLEAN;
450 if (sym->flags & def_flags) {
451 conf_warning("override: reassigning to symbol %s", sym->name);
456 sym->def[def].tri = no;
457 sym->flags |= def_flags;
462 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
463 p = strchr(line + strlen(CONFIG_), '=');
467 p2 = strchr(p, '\n');
474 sym = sym_find(line + strlen(CONFIG_));
476 if (def == S_DEF_AUTO)
478 * Reading from include/config/auto.conf
479 * If CONFIG_FOO previously existed in
480 * auto.conf but it is missing now,
481 * include/config/FOO must be touched.
483 conf_touch_dep(line + strlen(CONFIG_));
485 conf_set_changed(true);
489 if (sym->flags & def_flags) {
490 conf_warning("override: reassigning to symbol %s", sym->name);
492 if (conf_set_sym_val(sym, def, def_flags, p))
495 if (line[0] != '\r' && line[0] != '\n')
496 conf_warning("unexpected data: %.*s",
497 (int)strcspn(line, "\r\n"), line);
502 if (sym && sym_is_choice_value(sym)) {
503 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
504 switch (sym->def[def].tri) {
508 if (cs->def[def].tri == yes) {
509 conf_warning("%s creates inconsistent choice state", sym->name);
510 cs->flags &= ~def_flags;
514 if (cs->def[def].tri != no)
515 conf_warning("override: %s changes choice state", sym->name);
516 cs->def[def].val = sym;
519 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
527 int conf_read(const char *name)
530 int conf_unsaved = 0;
533 conf_set_changed(false);
535 if (conf_read_simple(name, S_DEF_USER)) {
536 sym_calc_value(modules_sym);
540 sym_calc_value(modules_sym);
542 for_all_symbols(i, sym) {
544 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
546 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
547 /* check that calculated value agrees with saved value */
551 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
555 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
559 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
560 /* no previous value and not saved */
563 /* maybe print value in verbose mode... */
566 for_all_symbols(i, sym) {
567 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
568 /* Reset values of generates values, so they'll appear
569 * as new, if they should become visible, but that
570 * doesn't quite work if the Kconfig and the saved
571 * configuration disagree.
573 if (sym->visible == no && !conf_unsaved)
574 sym->flags &= ~SYMBOL_DEF_USER;
579 /* Reset a string value if it's out of range */
580 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
582 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
591 if (conf_warnings || conf_unsaved)
592 conf_set_changed(true);
598 * Kconfig configuration printer
600 * This printer is used when generating the resulting configuration after
601 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
602 * passing a non-NULL argument to the printer.
606 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
613 bool skip_unset = (arg != NULL);
616 fprintf(fp, "# %s%s is not set\n",
625 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
629 kconfig_print_comment(FILE *fp, const char *value, void *arg)
631 const char *p = value;
635 l = strcspn(p, "\n");
639 xfwrite(p, l, 1, fp);
648 static struct conf_printer kconfig_printer_cb =
650 .print_symbol = kconfig_print_symbol,
651 .print_comment = kconfig_print_comment,
657 * This printer is used when generating the `include/generated/autoconf.h' file.
660 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
666 const char *suffix = "";
675 fprintf(fp, "#define %s%s%s 1\n",
676 CONFIG_, sym->name, suffix);
681 const char *prefix = "";
683 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
685 fprintf(fp, "#define %s%s %s%s\n",
686 CONFIG_, sym->name, prefix, value);
691 fprintf(fp, "#define %s%s %s\n",
692 CONFIG_, sym->name, value);
701 header_print_comment(FILE *fp, const char *value, void *arg)
703 const char *p = value;
708 l = strcspn(p, "\n");
712 xfwrite(p, l, 1, fp);
719 fprintf(fp, " */\n");
722 static struct conf_printer header_printer_cb =
724 .print_symbol = header_print_symbol,
725 .print_comment = header_print_comment,
728 static void conf_write_symbol(FILE *fp, struct symbol *sym,
729 struct conf_printer *printer, void *printer_arg)
737 str = sym_get_string_value(sym);
738 str = sym_escape_string_value(str);
739 printer->print_symbol(fp, sym, str, printer_arg);
743 str = sym_get_string_value(sym);
744 printer->print_symbol(fp, sym, str, printer_arg);
749 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
753 snprintf(buf, sizeof(buf),
755 "Automatically generated file; DO NOT EDIT.\n"
757 rootmenu.prompt->text);
759 printer->print_comment(fp, buf, printer_arg);
763 * Write out a minimal config.
764 * All values that has default values are skipped as this is redundant.
766 int conf_write_defconfig(const char *filename)
772 out = fopen(filename, "w");
776 sym_clear_all_valid();
778 /* Traverse all menus to find all relevant symbols */
779 menu = rootmenu.list;
785 if (!menu_is_visible(menu))
787 } else if (!sym_is_choice(sym)) {
789 if (!(sym->flags & SYMBOL_WRITE))
791 sym->flags &= ~SYMBOL_WRITE;
792 /* If we cannot change the symbol - skip */
793 if (!sym_is_changeable(sym))
795 /* If symbol equals to default value - skip */
796 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
800 * If symbol is a choice value and equals to the
801 * default for a choice - skip.
802 * But only if value is bool and equal to "y" and
803 * choice is not "optional".
804 * (If choice is "optional" then all values can be "n")
806 if (sym_is_choice_value(sym)) {
810 cs = prop_get_symbol(sym_get_choice_prop(sym));
811 ds = sym_choice_default(cs);
812 if (!sym_is_optional(cs) && sym == ds) {
813 if ((sym->type == S_BOOLEAN) &&
814 sym_get_tristate_value(sym) == yes)
818 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
821 if (menu->list != NULL) {
824 else if (menu->next != NULL) {
827 while ((menu = menu->parent)) {
828 if (menu->next != NULL) {
839 int conf_write(const char *name)
845 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
848 bool need_newline = false;
851 name = conf_get_configname();
854 fprintf(stderr, "config name is empty\n");
859 fprintf(stderr, "%s: Is a directory\n", name);
863 if (make_parent_dir(name))
866 env = getenv("KCONFIG_OVERWRITECONFIG");
869 out = fopen(name, "w");
871 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
872 name, (int)getpid());
873 out = fopen(tmpname, "w");
878 conf_write_heading(out, &kconfig_printer_cb, NULL);
880 if (!conf_get_changed())
881 sym_clear_all_valid();
883 menu = rootmenu.list;
887 if (!menu_is_visible(menu))
889 str = menu_get_prompt(menu);
894 need_newline = false;
895 } else if (!(sym->flags & SYMBOL_CHOICE) &&
896 !(sym->flags & SYMBOL_WRITTEN)) {
898 if (!(sym->flags & SYMBOL_WRITE))
902 need_newline = false;
904 sym->flags |= SYMBOL_WRITTEN;
905 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
915 else while ((menu = menu->parent)) {
916 if (!menu->sym && menu_is_visible(menu) &&
918 str = menu_get_prompt(menu);
919 fprintf(out, "# end of %s\n", str);
930 for_all_symbols(i, sym)
931 sym->flags &= ~SYMBOL_WRITTEN;
934 if (is_same(name, tmpname)) {
935 conf_message("No change to %s", name);
937 conf_set_changed(false);
941 snprintf(oldname, sizeof(oldname), "%s.old", name);
942 rename(name, oldname);
943 if (rename(tmpname, name))
947 conf_message("configuration written to %s", name);
949 conf_set_changed(false);
954 /* write a dependency file as used by kbuild to track dependencies */
955 static int conf_write_dep(const char *name)
960 out = fopen("..config.tmp", "w");
963 fprintf(out, "deps_config := \\\n");
964 for (file = file_list; file; file = file->next) {
966 fprintf(out, "\t%s \\\n", file->name);
968 fprintf(out, "\t%s\n", file->name);
970 fprintf(out, "\n%s: \\\n"
971 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
973 env_write_dep(out, conf_get_autoconfig_name());
975 fprintf(out, "\n$(deps_config): ;\n");
978 if (make_parent_dir(name))
980 rename("..config.tmp", name);
984 static int conf_touch_deps(void)
990 strcpy(depfile_path, "include/config/");
991 depfile_prefix_len = strlen(depfile_path);
993 name = conf_get_autoconfig_name();
994 conf_read_simple(name, S_DEF_AUTO);
995 sym_calc_value(modules_sym);
997 for_all_symbols(i, sym) {
999 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1001 if (sym->flags & SYMBOL_WRITE) {
1002 if (sym->flags & SYMBOL_DEF_AUTO) {
1004 * symbol has old and new value,
1005 * so compare them...
1007 switch (sym->type) {
1010 if (sym_get_tristate_value(sym) ==
1011 sym->def[S_DEF_AUTO].tri)
1017 if (!strcmp(sym_get_string_value(sym),
1018 sym->def[S_DEF_AUTO].val))
1026 * If there is no old value, only 'no' (unset)
1027 * is allowed as new value.
1029 switch (sym->type) {
1032 if (sym_get_tristate_value(sym) == no)
1039 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1040 /* There is neither an old nor a new value. */
1043 * There is an old value, but no new value ('no' (unset)
1044 * isn't saved in auto.conf, so the old value is always
1045 * different from 'no').
1048 res = conf_touch_dep(sym->name);
1056 int conf_write_autoconf(int overwrite)
1060 const char *autoconf_name = conf_get_autoconfig_name();
1064 if (!overwrite && is_present(autoconf_name))
1067 conf_write_dep("include/config/auto.conf.cmd");
1069 if (conf_touch_deps())
1072 out = fopen(".tmpconfig", "w");
1076 out_h = fopen(".tmpconfig.h", "w");
1082 conf_write_heading(out, &kconfig_printer_cb, NULL);
1083 conf_write_heading(out_h, &header_printer_cb, NULL);
1085 for_all_symbols(i, sym) {
1086 sym_calc_value(sym);
1087 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1090 /* write symbols to auto.conf and autoconf.h */
1091 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
1092 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1097 name = getenv("KCONFIG_AUTOHEADER");
1099 name = "include/generated/autoconf.h";
1100 if (make_parent_dir(name))
1102 if (rename(".tmpconfig.h", name))
1105 if (make_parent_dir(autoconf_name))
1108 * This must be the last step, kbuild has a dependency on auto.conf
1109 * and this marks the successful completion of the previous steps.
1111 if (rename(".tmpconfig", autoconf_name))
1117 static bool conf_changed;
1118 static void (*conf_changed_callback)(void);
1120 void conf_set_changed(bool val)
1122 if (conf_changed_callback && conf_changed != val)
1123 conf_changed_callback();
1128 bool conf_get_changed(void)
1130 return conf_changed;
1133 void conf_set_changed_callback(void (*fn)(void))
1135 conf_changed_callback = fn;
1138 void set_all_choice_values(struct symbol *csym)
1140 struct property *prop;
1144 prop = sym_get_choice_prop(csym);
1147 * Set all non-assinged choice values to no
1149 expr_list_for_each_sym(prop->expr, e, sym) {
1150 if (!sym_has_value(sym))
1151 sym->def[S_DEF_USER].tri = no;
1153 csym->flags |= SYMBOL_DEF_USER;
1154 /* clear VALID to get value calculated */
1155 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);