1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
24 struct gstr autoconf_cmd;
26 /* return true if 'path' exists, false otherwise */
27 static bool is_present(const char *path)
31 return !stat(path, &st);
34 /* return true if 'path' exists and it is a directory, false otherwise */
35 static bool is_dir(const char *path)
42 return S_ISDIR(st.st_mode);
45 /* return true if the given two files are the same, false otherwise */
46 static bool is_same(const char *file1, const char *file2)
53 fd1 = open(file1, O_RDONLY);
57 fd2 = open(file2, O_RDONLY);
61 ret = fstat(fd1, &st1);
64 ret = fstat(fd2, &st2);
68 if (st1.st_size != st2.st_size)
71 map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
72 if (map1 == MAP_FAILED)
75 map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
76 if (map2 == MAP_FAILED)
79 if (bcmp(map1, map2, st1.st_size))
92 * Create the parent directory of the given path.
94 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
96 static int make_parent_dir(const char *path)
98 char tmp[PATH_MAX + 1];
101 strncpy(tmp, path, sizeof(tmp));
102 tmp[sizeof(tmp) - 1] = 0;
104 /* Remove the base name. Just return if nothing is left */
105 p = strrchr(tmp, '/');
110 /* Just in case it is an absolute path */
115 while ((p = strchr(p, '/'))) {
118 /* skip if the directory exists */
119 if (!is_dir(tmp) && mkdir(tmp, 0755))
130 static char depfile_path[PATH_MAX];
131 static size_t depfile_prefix_len;
133 /* touch depfile for symbol 'name' */
134 static int conf_touch_dep(const char *name)
138 /* check overflow: prefix + name + '\0' must fit in buffer. */
139 if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
142 strcpy(depfile_path + depfile_prefix_len, name);
144 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
152 static void conf_warning(const char *fmt, ...)
153 __attribute__ ((format (printf, 1, 2)));
155 static void conf_message(const char *fmt, ...)
156 __attribute__ ((format (printf, 1, 2)));
158 static const char *conf_filename;
159 static int conf_lineno, conf_warnings;
161 bool conf_errors(void)
164 return getenv("KCONFIG_WERROR");
168 static void conf_warning(const char *fmt, ...)
172 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
173 vfprintf(stderr, fmt, ap);
174 fprintf(stderr, "\n");
179 static void conf_default_message_callback(const char *s)
186 static void (*conf_message_callback)(const char *s) =
187 conf_default_message_callback;
188 void conf_set_message_callback(void (*fn)(const char *s))
190 conf_message_callback = fn;
193 static void conf_message(const char *fmt, ...)
198 if (!conf_message_callback)
203 vsnprintf(buf, sizeof(buf), fmt, ap);
204 conf_message_callback(buf);
208 const char *conf_get_configname(void)
210 char *name = getenv("KCONFIG_CONFIG");
212 return name ? name : ".config";
215 static const char *conf_get_autoconfig_name(void)
217 char *name = getenv("KCONFIG_AUTOCONFIG");
219 return name ? name : "include/config/auto.conf";
222 static const char *conf_get_autoheader_name(void)
224 char *name = getenv("KCONFIG_AUTOHEADER");
226 return name ? name : "include/generated/autoconf.h";
229 static const char *conf_get_rustccfg_name(void)
231 char *name = getenv("KCONFIG_RUSTCCFG");
233 return name ? name : "include/generated/rustc_cfg";
236 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
243 sym->def[def].tri = mod;
244 sym->flags |= def_flags;
250 sym->def[def].tri = yes;
251 sym->flags |= def_flags;
255 sym->def[def].tri = no;
256 sym->flags |= def_flags;
259 if (def != S_DEF_AUTO)
260 conf_warning("symbol value '%s' invalid for %s",
264 /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
265 if (def != S_DEF_AUTO) {
268 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
273 memmove(p2, p2 + 1, strlen(p2));
276 conf_warning("invalid string found");
283 if (sym_string_valid(sym, p)) {
284 sym->def[def].val = xstrdup(p);
285 sym->flags |= def_flags;
287 if (def != S_DEF_AUTO)
288 conf_warning("symbol value '%s' invalid for %s",
299 /* like getline(), but the newline character is stripped away */
300 static ssize_t getline_stripped(char **lineptr, size_t *n, FILE *stream)
304 len = getline(lineptr, n, stream);
306 if (len > 0 && (*lineptr)[len - 1] == '\n') {
308 (*lineptr)[len] = '\0';
310 if (len > 0 && (*lineptr)[len - 1] == '\r') {
312 (*lineptr)[len] = '\0';
319 int conf_read_simple(const char *name, int def)
323 size_t line_asize = 0;
327 const char *warn_unknown, *sym_name;
329 warn_unknown = getenv("KCONFIG_WARN_UNKNOWN_SYMBOLS");
331 in = zconf_fopen(name);
335 name = conf_get_configname();
336 in = zconf_fopen(name);
339 conf_set_changed(true);
341 env = getenv("KCONFIG_DEFCONFIG_LIST");
348 while (isspace(*env))
355 while (*p && !isspace(*p))
358 is_last = (*p == '\0');
362 in = zconf_fopen(env);
364 conf_message("using defaults found in %s",
379 conf_filename = name;
383 def_flags = SYMBOL_DEF << def;
384 for_all_symbols(sym) {
385 sym->flags |= SYMBOL_CHANGED;
386 sym->flags &= ~(def_flags|SYMBOL_VALID);
387 if (sym_is_choice(sym))
388 sym->flags |= def_flags;
393 free(sym->def[def].val);
396 sym->def[def].val = NULL;
397 sym->def[def].tri = no;
401 while (getline_stripped(&line, &line_asize, in) != -1) {
404 if (!line[0]) /* blank line */
407 if (line[0] == '#') {
411 if (memcmp(p, CONFIG_, strlen(CONFIG_)))
413 sym_name = p + strlen(CONFIG_);
414 p = strchr(sym_name, ' ');
418 if (strcmp(p, "is not set"))
423 if (memcmp(line, CONFIG_, strlen(CONFIG_))) {
424 conf_warning("unexpected data: %s", line);
428 sym_name = line + strlen(CONFIG_);
429 p = strchr(sym_name, '=');
431 conf_warning("unexpected data: %s", line);
438 sym = sym_find(sym_name);
440 if (def == S_DEF_AUTO) {
442 * Reading from include/config/auto.conf.
443 * If CONFIG_FOO previously existed in auto.conf
444 * but it is missing now, include/config/FOO
447 conf_touch_dep(sym_name);
450 conf_warning("unknown symbol: %s", sym_name);
452 conf_set_changed(true);
457 if (sym->flags & def_flags)
458 conf_warning("override: reassigning to symbol %s", sym->name);
460 if (conf_set_sym_val(sym, def, def_flags, val))
463 if (sym && sym_is_choice_value(sym)) {
464 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
465 switch (sym->def[def].tri) {
469 if (cs->def[def].tri == yes) {
470 conf_warning("%s creates inconsistent choice state", sym->name);
471 cs->flags &= ~def_flags;
475 if (cs->def[def].tri != no)
476 conf_warning("override: %s changes choice state", sym->name);
477 cs->def[def].val = sym;
480 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
489 int conf_read(const char *name)
492 int conf_unsaved = 0;
494 conf_set_changed(false);
496 if (conf_read_simple(name, S_DEF_USER)) {
497 sym_calc_value(modules_sym);
501 sym_calc_value(modules_sym);
503 for_all_symbols(sym) {
505 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
507 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
508 /* check that calculated value agrees with saved value */
512 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
516 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
520 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
521 /* no previous value and not saved */
524 /* maybe print value in verbose mode... */
527 for_all_symbols(sym) {
528 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
529 /* Reset values of generates values, so they'll appear
530 * as new, if they should become visible, but that
531 * doesn't quite work if the Kconfig and the saved
532 * configuration disagree.
534 if (sym->visible == no && !conf_unsaved)
535 sym->flags &= ~SYMBOL_DEF_USER;
540 /* Reset a string value if it's out of range */
541 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
543 sym->flags &= ~SYMBOL_VALID;
552 if (conf_warnings || conf_unsaved)
553 conf_set_changed(true);
558 struct comment_style {
559 const char *decoration;
564 static const struct comment_style comment_style_pound = {
570 static const struct comment_style comment_style_c = {
576 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
581 fprintf(fp, "%s\n", cs->prefix);
583 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
586 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
588 fprintf(fp, "%s\n", cs->postfix);
591 /* The returned pointer must be freed on the caller side */
592 static char *escape_string_value(const char *in)
598 len = strlen(in) + strlen("\"\"") + 1;
602 p += strcspn(p, "\"\\");
618 len = strcspn(p, "\"\\");
619 strncat(out, p, len);
626 strncat(out, p++, 1);
634 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
636 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
640 char *escaped = NULL;
642 if (sym->type == S_UNKNOWN)
645 val = sym_get_string_value(sym);
647 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
648 output_n != OUTPUT_N && *val == 'n') {
649 if (output_n == OUTPUT_N_AS_UNSET)
650 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
654 if (sym->type == S_STRING && escape_string) {
655 escaped = escape_string_value(val);
659 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
664 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
666 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
669 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
671 __print_symbol(fp, sym, OUTPUT_N_NONE, false);
674 void print_symbol_for_listconfig(struct symbol *sym)
676 __print_symbol(stdout, sym, OUTPUT_N, true);
679 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
682 const char *sym_suffix = "";
683 const char *val_prefix = "";
684 char *escaped = NULL;
686 if (sym->type == S_UNKNOWN)
689 val = sym_get_string_value(sym);
698 sym_suffix = "_MODULE";
705 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
709 escaped = escape_string_value(val);
715 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
721 static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym)
724 const char *val_prefix = "";
725 char *val_prefixed = NULL;
726 size_t val_prefixed_len;
727 char *escaped = NULL;
729 if (sym->type == S_UNKNOWN)
732 val = sym_get_string_value(sym);
738 * We do not care about disabled ones, i.e. no need for
739 * what otherwise are "comments" in other printers.
745 * To have similar functionality to the C macro `IS_ENABLED()`
746 * we provide an empty `--cfg CONFIG_X` here in both `y`
749 * Then, the common `fprintf()` below will also give us
750 * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
751 * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
753 fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);
756 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
763 if (strlen(val_prefix) > 0) {
764 val_prefixed_len = strlen(val) + strlen(val_prefix) + 1;
765 val_prefixed = xmalloc(val_prefixed_len);
766 snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val);
770 /* All values get escaped: the `--cfg` option only takes strings */
771 escaped = escape_string_value(val);
774 fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val);
781 * Write out a minimal config.
782 * All values that has default values are skipped as this is redundant.
784 int conf_write_defconfig(const char *filename)
790 out = fopen(filename, "w");
794 sym_clear_all_valid();
796 /* Traverse all menus to find all relevant symbols */
797 menu = rootmenu.list;
802 if (sym && !sym_is_choice(sym)) {
804 if (!(sym->flags & SYMBOL_WRITE))
806 sym->flags &= ~SYMBOL_WRITE;
807 /* If we cannot change the symbol - skip */
808 if (!sym_is_changeable(sym))
810 /* If symbol equals to default value - skip */
811 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
815 * If symbol is a choice value and equals to the
816 * default for a choice - skip.
817 * But only if value is bool and equal to "y" and
818 * choice is not "optional".
819 * (If choice is "optional" then all values can be "n")
821 if (sym_is_choice_value(sym)) {
825 cs = prop_get_symbol(sym_get_choice_prop(sym));
826 ds = sym_choice_default(cs);
827 if (!sym_is_optional(cs) && sym == ds) {
828 if ((sym->type == S_BOOLEAN) &&
829 sym_get_tristate_value(sym) == yes)
833 print_symbol_for_dotconfig(out, sym);
836 if (menu->list != NULL) {
839 else if (menu->next != NULL) {
842 while ((menu = menu->parent)) {
843 if (menu->next != NULL) {
854 int conf_write(const char *name)
860 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
862 bool need_newline = false;
865 name = conf_get_configname();
868 fprintf(stderr, "config name is empty\n");
873 fprintf(stderr, "%s: Is a directory\n", name);
877 if (make_parent_dir(name))
880 env = getenv("KCONFIG_OVERWRITECONFIG");
883 out = fopen(name, "w");
885 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
886 name, (int)getpid());
887 out = fopen(tmpname, "w");
892 conf_write_heading(out, &comment_style_pound);
894 if (!conf_get_changed())
895 sym_clear_all_valid();
897 menu = rootmenu.list;
901 if (!menu_is_visible(menu))
903 str = menu_get_prompt(menu);
908 need_newline = false;
909 } else if (!(sym->flags & SYMBOL_CHOICE) &&
910 !(sym->flags & SYMBOL_WRITTEN)) {
912 if (!(sym->flags & SYMBOL_WRITE))
916 need_newline = false;
918 sym->flags |= SYMBOL_WRITTEN;
919 print_symbol_for_dotconfig(out, sym);
929 if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
930 menu->prompt->type == P_MENU) {
931 fprintf(out, "# end of %s\n", menu_get_prompt(menu));
946 sym->flags &= ~SYMBOL_WRITTEN;
949 if (is_same(name, tmpname)) {
950 conf_message("No change to %s", name);
952 conf_set_changed(false);
956 snprintf(oldname, sizeof(oldname), "%s.old", name);
957 rename(name, oldname);
958 if (rename(tmpname, name))
962 conf_message("configuration written to %s", name);
964 conf_set_changed(false);
969 /* write a dependency file as used by kbuild to track dependencies */
970 static int conf_write_autoconf_cmd(const char *autoconf_name)
972 char name[PATH_MAX], tmp[PATH_MAX];
976 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
977 if (ret >= sizeof(name)) /* check truncation */
980 if (make_parent_dir(name))
983 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
984 if (ret >= sizeof(tmp)) /* check truncation */
987 out = fopen(tmp, "w");
993 fprintf(out, "autoconfig := %s\n", autoconf_name);
995 fputs(str_get(&autoconf_cmd), out);
998 ret = ferror(out); /* error check for all fprintf() calls */
1003 if (rename(tmp, name)) {
1011 static int conf_touch_deps(void)
1013 const char *name, *tmp;
1017 name = conf_get_autoconfig_name();
1018 tmp = strrchr(name, '/');
1019 depfile_prefix_len = tmp ? tmp - name + 1 : 0;
1020 if (depfile_prefix_len + 1 > sizeof(depfile_path))
1023 strncpy(depfile_path, name, depfile_prefix_len);
1024 depfile_path[depfile_prefix_len] = 0;
1026 conf_read_simple(name, S_DEF_AUTO);
1027 sym_calc_value(modules_sym);
1029 for_all_symbols(sym) {
1030 sym_calc_value(sym);
1031 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1033 if (sym->flags & SYMBOL_WRITE) {
1034 if (sym->flags & SYMBOL_DEF_AUTO) {
1036 * symbol has old and new value,
1037 * so compare them...
1039 switch (sym->type) {
1042 if (sym_get_tristate_value(sym) ==
1043 sym->def[S_DEF_AUTO].tri)
1049 if (!strcmp(sym_get_string_value(sym),
1050 sym->def[S_DEF_AUTO].val))
1058 * If there is no old value, only 'no' (unset)
1059 * is allowed as new value.
1061 switch (sym->type) {
1064 if (sym_get_tristate_value(sym) == no)
1071 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1072 /* There is neither an old nor a new value. */
1075 * There is an old value, but no new value ('no' (unset)
1076 * isn't saved in auto.conf, so the old value is always
1077 * different from 'no').
1080 res = conf_touch_dep(sym->name);
1088 static int __conf_write_autoconf(const char *filename,
1089 void (*print_symbol)(FILE *, struct symbol *),
1090 const struct comment_style *comment_style)
1097 if (make_parent_dir(filename))
1100 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1101 if (ret >= sizeof(tmp)) /* check truncation */
1104 file = fopen(tmp, "w");
1110 conf_write_heading(file, comment_style);
1112 for_all_symbols(sym)
1113 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1114 print_symbol(file, sym);
1117 /* check possible errors in conf_write_heading() and print_symbol() */
1123 if (rename(tmp, filename)) {
1131 int conf_write_autoconf(int overwrite)
1134 const char *autoconf_name = conf_get_autoconfig_name();
1137 if (!overwrite && is_present(autoconf_name))
1140 ret = conf_write_autoconf_cmd(autoconf_name);
1144 if (conf_touch_deps())
1147 for_all_symbols(sym)
1148 sym_calc_value(sym);
1150 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1156 ret = __conf_write_autoconf(conf_get_rustccfg_name(),
1157 print_symbol_for_rustccfg,
1163 * Create include/config/auto.conf. This must be the last step because
1164 * Kbuild has a dependency on auto.conf and this marks the successful
1165 * completion of the previous steps.
1167 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1168 print_symbol_for_autoconf,
1169 &comment_style_pound);
1176 static bool conf_changed;
1177 static void (*conf_changed_callback)(void);
1179 void conf_set_changed(bool val)
1181 bool changed = conf_changed != val;
1185 if (conf_changed_callback && changed)
1186 conf_changed_callback();
1189 bool conf_get_changed(void)
1191 return conf_changed;
1194 void conf_set_changed_callback(void (*fn)(void))
1196 conf_changed_callback = fn;
1199 void set_all_choice_values(struct symbol *csym)
1201 struct property *prop;
1205 prop = sym_get_choice_prop(csym);
1208 * Set all non-assinged choice values to no
1210 expr_list_for_each_sym(prop->expr, e, sym) {
1211 if (!sym_has_value(sym))
1212 sym->def[S_DEF_USER].tri = no;
1214 csym->flags |= SYMBOL_DEF_USER;
1215 /* clear VALID to get value calculated */
1216 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);