1 // SPDX-License-Identifier: GPL-2.0
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 &= ~(def_flags|SYMBOL_VALID);
390 free(sym->def[def].val);
393 sym->def[def].val = NULL;
394 sym->def[def].tri = no;
398 while (getline_stripped(&line, &line_asize, in) != -1) {
403 if (!line[0]) /* blank line */
406 if (line[0] == '#') {
410 if (memcmp(p, CONFIG_, strlen(CONFIG_)))
412 sym_name = p + strlen(CONFIG_);
413 p = strchr(sym_name, ' ');
417 if (strcmp(p, "is not set"))
422 if (memcmp(line, CONFIG_, strlen(CONFIG_))) {
423 conf_warning("unexpected data: %s", line);
427 sym_name = line + strlen(CONFIG_);
428 p = strchr(sym_name, '=');
430 conf_warning("unexpected data: %s", line);
437 sym = sym_find(sym_name);
439 if (def == S_DEF_AUTO) {
441 * Reading from include/config/auto.conf.
442 * If CONFIG_FOO previously existed in auto.conf
443 * but it is missing now, include/config/FOO
446 conf_touch_dep(sym_name);
449 conf_warning("unknown symbol: %s", sym_name);
451 conf_set_changed(true);
456 if (sym->flags & def_flags)
457 conf_warning("override: reassigning to symbol %s", sym->name);
459 if (conf_set_sym_val(sym, def, def_flags, val))
463 * If this is a choice member, give it the highest priority.
464 * If conflicting CONFIG options are given from an input file,
467 choice = sym_get_choice_menu(sym);
469 list_move(&sym->choice_link, &choice->choice_members);
477 int conf_read(const char *name)
481 conf_set_changed(false);
483 if (conf_read_simple(name, S_DEF_USER)) {
484 sym_calc_value(modules_sym);
488 sym_calc_value(modules_sym);
490 for_all_symbols(sym) {
492 if (sym_is_choice(sym))
494 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
495 /* check that calculated value agrees with saved value */
499 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
503 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
507 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
508 /* no previous value and not saved */
510 conf_set_changed(true);
511 /* maybe print value in verbose mode... */
515 conf_set_changed(true);
520 struct comment_style {
521 const char *decoration;
526 static const struct comment_style comment_style_pound = {
532 static const struct comment_style comment_style_c = {
538 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
543 fprintf(fp, "%s\n", cs->prefix);
545 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
548 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
550 fprintf(fp, "%s\n", cs->postfix);
553 /* The returned pointer must be freed on the caller side */
554 static char *escape_string_value(const char *in)
560 len = strlen(in) + strlen("\"\"") + 1;
564 p += strcspn(p, "\"\\");
580 len = strcspn(p, "\"\\");
581 strncat(out, p, len);
588 strncat(out, p++, 1);
596 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
598 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
602 char *escaped = NULL;
604 if (sym->type == S_UNKNOWN)
607 val = sym_get_string_value(sym);
609 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
610 output_n != OUTPUT_N && *val == 'n') {
611 if (output_n == OUTPUT_N_AS_UNSET)
612 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
616 if (sym->type == S_STRING && escape_string) {
617 escaped = escape_string_value(val);
621 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
626 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
628 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
631 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
633 __print_symbol(fp, sym, OUTPUT_N_NONE, false);
636 void print_symbol_for_listconfig(struct symbol *sym)
638 __print_symbol(stdout, sym, OUTPUT_N, true);
641 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
644 const char *sym_suffix = "";
645 const char *val_prefix = "";
646 char *escaped = NULL;
648 if (sym->type == S_UNKNOWN)
651 val = sym_get_string_value(sym);
660 sym_suffix = "_MODULE";
667 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
671 escaped = escape_string_value(val);
677 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
683 static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym)
686 const char *val_prefix = "";
687 char *val_prefixed = NULL;
688 size_t val_prefixed_len;
689 char *escaped = NULL;
691 if (sym->type == S_UNKNOWN)
694 val = sym_get_string_value(sym);
700 * We do not care about disabled ones, i.e. no need for
701 * what otherwise are "comments" in other printers.
707 * To have similar functionality to the C macro `IS_ENABLED()`
708 * we provide an empty `--cfg CONFIG_X` here in both `y`
711 * Then, the common `fprintf()` below will also give us
712 * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
713 * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
715 fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);
718 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
725 if (strlen(val_prefix) > 0) {
726 val_prefixed_len = strlen(val) + strlen(val_prefix) + 1;
727 val_prefixed = xmalloc(val_prefixed_len);
728 snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val);
732 /* All values get escaped: the `--cfg` option only takes strings */
733 escaped = escape_string_value(val);
736 fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val);
743 * Write out a minimal config.
744 * All values that has default values are skipped as this is redundant.
746 int conf_write_defconfig(const char *filename)
752 out = fopen(filename, "w");
756 sym_clear_all_valid();
758 menu_for_each_entry(menu) {
763 if (!sym || sym_is_choice(sym))
767 if (!(sym->flags & SYMBOL_WRITE))
769 sym->flags &= ~SYMBOL_WRITE;
770 /* Skip unchangeable symbols */
771 if (!sym_is_changeable(sym))
773 /* Skip symbols that are equal to the default */
774 if (!strcmp(sym_get_string_value(sym), sym_get_string_default(sym)))
777 /* Skip choice values that are equal to the default */
778 choice = sym_get_choice_menu(sym);
782 ds = sym_choice_default(choice);
783 if (sym == ds && sym_get_tristate_value(sym) == yes)
786 print_symbol_for_dotconfig(out, sym);
792 int conf_write(const char *name)
798 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
800 bool need_newline = false;
803 name = conf_get_configname();
806 fprintf(stderr, "config name is empty\n");
811 fprintf(stderr, "%s: Is a directory\n", name);
815 if (make_parent_dir(name))
818 env = getenv("KCONFIG_OVERWRITECONFIG");
821 out = fopen(name, "w");
823 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
824 name, (int)getpid());
825 out = fopen(tmpname, "w");
830 conf_write_heading(out, &comment_style_pound);
832 if (!conf_get_changed())
833 sym_clear_all_valid();
835 menu = rootmenu.list;
839 if (!menu_is_visible(menu))
841 str = menu_get_prompt(menu);
846 need_newline = false;
847 } else if (!sym_is_choice(sym) &&
848 !(sym->flags & SYMBOL_WRITTEN)) {
850 if (!(sym->flags & SYMBOL_WRITE))
854 need_newline = false;
856 sym->flags |= SYMBOL_WRITTEN;
857 print_symbol_for_dotconfig(out, sym);
867 if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
868 menu->prompt->type == P_MENU) {
869 fprintf(out, "# end of %s\n", menu_get_prompt(menu));
884 sym->flags &= ~SYMBOL_WRITTEN;
887 if (is_same(name, tmpname)) {
888 conf_message("No change to %s", name);
890 conf_set_changed(false);
894 snprintf(oldname, sizeof(oldname), "%s.old", name);
895 rename(name, oldname);
896 if (rename(tmpname, name))
900 conf_message("configuration written to %s", name);
902 conf_set_changed(false);
907 /* write a dependency file as used by kbuild to track dependencies */
908 static int conf_write_autoconf_cmd(const char *autoconf_name)
910 char name[PATH_MAX], tmp[PATH_MAX];
914 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
915 if (ret >= sizeof(name)) /* check truncation */
918 if (make_parent_dir(name))
921 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
922 if (ret >= sizeof(tmp)) /* check truncation */
925 out = fopen(tmp, "w");
931 fprintf(out, "autoconfig := %s\n", autoconf_name);
933 fputs(str_get(&autoconf_cmd), out);
936 ret = ferror(out); /* error check for all fprintf() calls */
941 if (rename(tmp, name)) {
949 static int conf_touch_deps(void)
951 const char *name, *tmp;
955 name = conf_get_autoconfig_name();
956 tmp = strrchr(name, '/');
957 depfile_prefix_len = tmp ? tmp - name + 1 : 0;
958 if (depfile_prefix_len + 1 > sizeof(depfile_path))
961 strncpy(depfile_path, name, depfile_prefix_len);
962 depfile_path[depfile_prefix_len] = 0;
964 conf_read_simple(name, S_DEF_AUTO);
965 sym_calc_value(modules_sym);
967 for_all_symbols(sym) {
969 if (sym_is_choice(sym))
971 if (sym->flags & SYMBOL_WRITE) {
972 if (sym->flags & SYMBOL_DEF_AUTO) {
974 * symbol has old and new value,
980 if (sym_get_tristate_value(sym) ==
981 sym->def[S_DEF_AUTO].tri)
987 if (!strcmp(sym_get_string_value(sym),
988 sym->def[S_DEF_AUTO].val))
996 * If there is no old value, only 'no' (unset)
997 * is allowed as new value.
1002 if (sym_get_tristate_value(sym) == no)
1009 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1010 /* There is neither an old nor a new value. */
1013 * There is an old value, but no new value ('no' (unset)
1014 * isn't saved in auto.conf, so the old value is always
1015 * different from 'no').
1018 res = conf_touch_dep(sym->name);
1026 static int __conf_write_autoconf(const char *filename,
1027 void (*print_symbol)(FILE *, struct symbol *),
1028 const struct comment_style *comment_style)
1035 if (make_parent_dir(filename))
1038 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1039 if (ret >= sizeof(tmp)) /* check truncation */
1042 file = fopen(tmp, "w");
1048 conf_write_heading(file, comment_style);
1050 for_all_symbols(sym)
1051 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1052 print_symbol(file, sym);
1055 /* check possible errors in conf_write_heading() and print_symbol() */
1061 if (rename(tmp, filename)) {
1069 int conf_write_autoconf(int overwrite)
1072 const char *autoconf_name = conf_get_autoconfig_name();
1075 if (!overwrite && is_present(autoconf_name))
1078 ret = conf_write_autoconf_cmd(autoconf_name);
1082 if (conf_touch_deps())
1085 for_all_symbols(sym)
1086 sym_calc_value(sym);
1088 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1094 ret = __conf_write_autoconf(conf_get_rustccfg_name(),
1095 print_symbol_for_rustccfg,
1101 * Create include/config/auto.conf. This must be the last step because
1102 * Kbuild has a dependency on auto.conf and this marks the successful
1103 * completion of the previous steps.
1105 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1106 print_symbol_for_autoconf,
1107 &comment_style_pound);
1114 static bool conf_changed;
1115 static void (*conf_changed_callback)(bool);
1117 void conf_set_changed(bool val)
1119 if (conf_changed_callback && conf_changed != val)
1120 conf_changed_callback(val);
1125 bool conf_get_changed(void)
1127 return conf_changed;
1130 void conf_set_changed_callback(void (*fn)(bool))
1132 conf_changed_callback = fn;