1 // SPDX-License-Identifier: GPL-2.0
25 struct gstr autoconf_cmd;
27 /* return true if 'path' exists, false otherwise */
28 static bool is_present(const char *path)
32 return !stat(path, &st);
35 /* return true if 'path' exists and it is a directory, false otherwise */
36 static bool is_dir(const char *path)
43 return S_ISDIR(st.st_mode);
46 /* return true if the given two files are the same, false otherwise */
47 static bool is_same(const char *file1, const char *file2)
54 fd1 = open(file1, O_RDONLY);
58 fd2 = open(file2, O_RDONLY);
62 ret = fstat(fd1, &st1);
65 ret = fstat(fd2, &st2);
69 if (st1.st_size != st2.st_size)
72 map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
73 if (map1 == MAP_FAILED)
76 map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
77 if (map2 == MAP_FAILED)
80 if (bcmp(map1, map2, st1.st_size))
93 * Create the parent directory of the given path.
95 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
97 static int make_parent_dir(const char *path)
99 char tmp[PATH_MAX + 1];
102 strncpy(tmp, path, sizeof(tmp));
103 tmp[sizeof(tmp) - 1] = 0;
105 /* Remove the base name. Just return if nothing is left */
106 p = strrchr(tmp, '/');
111 /* Just in case it is an absolute path */
116 while ((p = strchr(p, '/'))) {
119 /* skip if the directory exists */
120 if (!is_dir(tmp) && mkdir(tmp, 0755))
131 static char depfile_path[PATH_MAX];
132 static size_t depfile_prefix_len;
134 /* touch depfile for symbol 'name' */
135 static int conf_touch_dep(const char *name)
139 /* check overflow: prefix + name + '\0' must fit in buffer. */
140 if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
143 strcpy(depfile_path + depfile_prefix_len, name);
145 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
153 static void conf_warning(const char *fmt, ...)
154 __attribute__ ((format (printf, 1, 2)));
156 static void conf_message(const char *fmt, ...)
157 __attribute__ ((format (printf, 1, 2)));
159 static const char *conf_filename;
160 static int conf_lineno, conf_warnings;
162 bool conf_errors(void)
165 return getenv("KCONFIG_WERROR");
169 static void conf_warning(const char *fmt, ...)
173 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
174 vfprintf(stderr, fmt, ap);
175 fprintf(stderr, "\n");
180 static void conf_default_message_callback(const char *s)
187 static void (*conf_message_callback)(const char *s) =
188 conf_default_message_callback;
189 void conf_set_message_callback(void (*fn)(const char *s))
191 conf_message_callback = fn;
194 static void conf_message(const char *fmt, ...)
199 if (!conf_message_callback)
204 vsnprintf(buf, sizeof(buf), fmt, ap);
205 conf_message_callback(buf);
209 const char *conf_get_configname(void)
211 char *name = getenv("KCONFIG_CONFIG");
213 return name ? name : ".config";
216 static const char *conf_get_autoconfig_name(void)
218 char *name = getenv("KCONFIG_AUTOCONFIG");
220 return name ? name : "include/config/auto.conf";
223 static const char *conf_get_autoheader_name(void)
225 char *name = getenv("KCONFIG_AUTOHEADER");
227 return name ? name : "include/generated/autoconf.h";
230 static const char *conf_get_rustccfg_name(void)
232 char *name = getenv("KCONFIG_RUSTCCFG");
234 return name ? name : "include/generated/rustc_cfg";
237 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
244 sym->def[def].tri = mod;
245 sym->flags |= def_flags;
251 sym->def[def].tri = yes;
252 sym->flags |= def_flags;
256 sym->def[def].tri = no;
257 sym->flags |= def_flags;
260 if (def != S_DEF_AUTO)
261 conf_warning("symbol value '%s' invalid for %s",
265 /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
266 if (def != S_DEF_AUTO) {
269 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
274 memmove(p2, p2 + 1, strlen(p2));
277 conf_warning("invalid string found");
284 if (sym_string_valid(sym, p)) {
285 sym->def[def].val = xstrdup(p);
286 sym->flags |= def_flags;
288 if (def != S_DEF_AUTO)
289 conf_warning("symbol value '%s' invalid for %s",
300 /* like getline(), but the newline character is stripped away */
301 static ssize_t getline_stripped(char **lineptr, size_t *n, FILE *stream)
305 len = getline(lineptr, n, stream);
307 if (len > 0 && (*lineptr)[len - 1] == '\n') {
309 (*lineptr)[len] = '\0';
311 if (len > 0 && (*lineptr)[len - 1] == '\r') {
313 (*lineptr)[len] = '\0';
320 int conf_read_simple(const char *name, int def)
324 size_t line_asize = 0;
328 const char *warn_unknown, *sym_name;
330 warn_unknown = getenv("KCONFIG_WARN_UNKNOWN_SYMBOLS");
332 in = zconf_fopen(name);
336 name = conf_get_configname();
337 in = zconf_fopen(name);
340 conf_set_changed(true);
342 env = getenv("KCONFIG_DEFCONFIG_LIST");
349 while (isspace(*env))
356 while (*p && !isspace(*p))
359 is_last = (*p == '\0');
363 in = zconf_fopen(env);
365 conf_message("using defaults found in %s",
380 conf_filename = name;
384 def_flags = SYMBOL_DEF << def;
385 for_all_symbols(sym) {
386 sym->flags &= ~(def_flags|SYMBOL_VALID);
391 free(sym->def[def].val);
394 sym->def[def].val = NULL;
395 sym->def[def].tri = no;
399 expr_invalidate_all();
401 while (getline_stripped(&line, &line_asize, in) != -1) {
406 if (!line[0]) /* blank line */
409 if (line[0] == '#') {
413 if (memcmp(p, CONFIG_, strlen(CONFIG_)))
415 sym_name = p + strlen(CONFIG_);
416 p = strchr(sym_name, ' ');
420 if (strcmp(p, "is not set"))
425 if (memcmp(line, CONFIG_, strlen(CONFIG_))) {
426 conf_warning("unexpected data: %s", line);
430 sym_name = line + strlen(CONFIG_);
431 p = strchr(sym_name, '=');
433 conf_warning("unexpected data: %s", line);
440 sym = sym_find(sym_name);
442 if (def == S_DEF_AUTO) {
444 * Reading from include/config/auto.conf.
445 * If CONFIG_FOO previously existed in auto.conf
446 * but it is missing now, include/config/FOO
449 conf_touch_dep(sym_name);
452 conf_warning("unknown symbol: %s", sym_name);
454 conf_set_changed(true);
459 if (sym->flags & def_flags)
460 conf_warning("override: reassigning to symbol %s", sym->name);
462 if (conf_set_sym_val(sym, def, def_flags, val))
466 * If this is a choice member, give it the highest priority.
467 * If conflicting CONFIG options are given from an input file,
470 choice = sym_get_choice_menu(sym);
472 list_move(&sym->choice_link, &choice->choice_members);
480 int conf_read(const char *name)
484 conf_set_changed(false);
486 if (conf_read_simple(name, S_DEF_USER)) {
487 sym_calc_value(modules_sym);
491 sym_calc_value(modules_sym);
493 for_all_symbols(sym) {
495 if (sym_is_choice(sym))
497 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
498 /* check that calculated value agrees with saved value */
502 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
506 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
510 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
511 /* no previous value and not saved */
513 conf_set_changed(true);
514 /* maybe print value in verbose mode... */
518 conf_set_changed(true);
523 struct comment_style {
524 const char *decoration;
529 static const struct comment_style comment_style_pound = {
535 static const struct comment_style comment_style_c = {
541 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
546 fprintf(fp, "%s\n", cs->prefix);
548 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
551 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
553 fprintf(fp, "%s\n", cs->postfix);
556 /* The returned pointer must be freed on the caller side */
557 static char *escape_string_value(const char *in)
563 len = strlen(in) + strlen("\"\"") + 1;
567 p += strcspn(p, "\"\\");
583 len = strcspn(p, "\"\\");
584 strncat(out, p, len);
591 strncat(out, p++, 1);
599 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
601 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
605 char *escaped = NULL;
607 if (sym->type == S_UNKNOWN)
610 val = sym_get_string_value(sym);
612 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
613 output_n != OUTPUT_N && *val == 'n') {
614 if (output_n == OUTPUT_N_AS_UNSET)
615 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
619 if (sym->type == S_STRING && escape_string) {
620 escaped = escape_string_value(val);
624 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
629 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
631 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
634 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
636 __print_symbol(fp, sym, OUTPUT_N_NONE, false);
639 void print_symbol_for_listconfig(struct symbol *sym)
641 __print_symbol(stdout, sym, OUTPUT_N, true);
644 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
647 const char *sym_suffix = "";
648 const char *val_prefix = "";
649 char *escaped = NULL;
651 if (sym->type == S_UNKNOWN)
654 val = sym_get_string_value(sym);
663 sym_suffix = "_MODULE";
670 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
674 escaped = escape_string_value(val);
680 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
686 static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym)
689 const char *val_prefix = "";
690 char *val_prefixed = NULL;
691 size_t val_prefixed_len;
692 char *escaped = NULL;
694 if (sym->type == S_UNKNOWN)
697 val = sym_get_string_value(sym);
703 * We do not care about disabled ones, i.e. no need for
704 * what otherwise are "comments" in other printers.
710 * To have similar functionality to the C macro `IS_ENABLED()`
711 * we provide an empty `--cfg CONFIG_X` here in both `y`
714 * Then, the common `fprintf()` below will also give us
715 * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
716 * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
718 fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);
721 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
728 if (strlen(val_prefix) > 0) {
729 val_prefixed_len = strlen(val) + strlen(val_prefix) + 1;
730 val_prefixed = xmalloc(val_prefixed_len);
731 snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val);
735 /* All values get escaped: the `--cfg` option only takes strings */
736 escaped = escape_string_value(val);
739 fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val);
746 * Write out a minimal config.
747 * All values that has default values are skipped as this is redundant.
749 int conf_write_defconfig(const char *filename)
755 out = fopen(filename, "w");
759 sym_clear_all_valid();
761 menu_for_each_entry(menu) {
766 if (!sym || sym_is_choice(sym))
770 if (!(sym->flags & SYMBOL_WRITE))
772 sym->flags &= ~SYMBOL_WRITE;
773 /* Skip unchangeable symbols */
774 if (!sym_is_changeable(sym))
776 /* Skip symbols that are equal to the default */
777 if (!strcmp(sym_get_string_value(sym), sym_get_string_default(sym)))
780 /* Skip choice values that are equal to the default */
781 choice = sym_get_choice_menu(sym);
785 ds = sym_choice_default(choice);
786 if (sym == ds && sym_get_tristate_value(sym) == yes)
789 print_symbol_for_dotconfig(out, sym);
795 int conf_write(const char *name)
801 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
803 bool need_newline = false;
806 name = conf_get_configname();
809 fprintf(stderr, "config name is empty\n");
814 fprintf(stderr, "%s: Is a directory\n", name);
818 if (make_parent_dir(name))
821 env = getenv("KCONFIG_OVERWRITECONFIG");
824 out = fopen(name, "w");
826 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
827 name, (int)getpid());
828 out = fopen(tmpname, "w");
833 conf_write_heading(out, &comment_style_pound);
835 if (!conf_get_changed())
836 sym_clear_all_valid();
838 menu = rootmenu.list;
842 if (!menu_is_visible(menu))
844 str = menu_get_prompt(menu);
849 need_newline = false;
850 } else if (!sym_is_choice(sym) &&
851 !(sym->flags & SYMBOL_WRITTEN)) {
853 if (!(sym->flags & SYMBOL_WRITE))
857 need_newline = false;
859 sym->flags |= SYMBOL_WRITTEN;
860 print_symbol_for_dotconfig(out, sym);
870 if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
871 menu->prompt->type == P_MENU) {
872 fprintf(out, "# end of %s\n", menu_get_prompt(menu));
887 sym->flags &= ~SYMBOL_WRITTEN;
890 if (is_same(name, tmpname)) {
891 conf_message("No change to %s", name);
893 conf_set_changed(false);
897 snprintf(oldname, sizeof(oldname), "%s.old", name);
898 rename(name, oldname);
899 if (rename(tmpname, name))
903 conf_message("configuration written to %s", name);
905 conf_set_changed(false);
910 /* write a dependency file as used by kbuild to track dependencies */
911 static int conf_write_autoconf_cmd(const char *autoconf_name)
913 char name[PATH_MAX], tmp[PATH_MAX];
917 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
918 if (ret >= sizeof(name)) /* check truncation */
921 if (make_parent_dir(name))
924 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
925 if (ret >= sizeof(tmp)) /* check truncation */
928 out = fopen(tmp, "w");
934 fprintf(out, "autoconfig := %s\n", autoconf_name);
936 fputs(str_get(&autoconf_cmd), out);
939 ret = ferror(out); /* error check for all fprintf() calls */
944 if (rename(tmp, name)) {
952 static int conf_touch_deps(void)
954 const char *name, *tmp;
958 name = conf_get_autoconfig_name();
959 tmp = strrchr(name, '/');
960 depfile_prefix_len = tmp ? tmp - name + 1 : 0;
961 if (depfile_prefix_len + 1 > sizeof(depfile_path))
964 strncpy(depfile_path, name, depfile_prefix_len);
965 depfile_path[depfile_prefix_len] = 0;
967 conf_read_simple(name, S_DEF_AUTO);
968 sym_calc_value(modules_sym);
970 for_all_symbols(sym) {
972 if (sym_is_choice(sym))
974 if (sym->flags & SYMBOL_WRITE) {
975 if (sym->flags & SYMBOL_DEF_AUTO) {
977 * symbol has old and new value,
983 if (sym_get_tristate_value(sym) ==
984 sym->def[S_DEF_AUTO].tri)
990 if (!strcmp(sym_get_string_value(sym),
991 sym->def[S_DEF_AUTO].val))
999 * If there is no old value, only 'no' (unset)
1000 * is allowed as new value.
1002 switch (sym->type) {
1005 if (sym_get_tristate_value(sym) == no)
1012 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1013 /* There is neither an old nor a new value. */
1016 * There is an old value, but no new value ('no' (unset)
1017 * isn't saved in auto.conf, so the old value is always
1018 * different from 'no').
1021 res = conf_touch_dep(sym->name);
1029 static int __conf_write_autoconf(const char *filename,
1030 void (*print_symbol)(FILE *, struct symbol *),
1031 const struct comment_style *comment_style)
1038 if (make_parent_dir(filename))
1041 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1042 if (ret >= sizeof(tmp)) /* check truncation */
1045 file = fopen(tmp, "w");
1051 conf_write_heading(file, comment_style);
1053 for_all_symbols(sym)
1054 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1055 print_symbol(file, sym);
1058 /* check possible errors in conf_write_heading() and print_symbol() */
1064 if (rename(tmp, filename)) {
1072 int conf_write_autoconf(int overwrite)
1075 const char *autoconf_name = conf_get_autoconfig_name();
1078 if (!overwrite && is_present(autoconf_name))
1081 ret = conf_write_autoconf_cmd(autoconf_name);
1085 if (conf_touch_deps())
1088 for_all_symbols(sym)
1089 sym_calc_value(sym);
1091 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1097 ret = __conf_write_autoconf(conf_get_rustccfg_name(),
1098 print_symbol_for_rustccfg,
1104 * Create include/config/auto.conf. This must be the last step because
1105 * Kbuild has a dependency on auto.conf and this marks the successful
1106 * completion of the previous steps.
1108 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1109 print_symbol_for_autoconf,
1110 &comment_style_pound);
1117 static bool conf_changed;
1118 static void (*conf_changed_callback)(bool);
1120 void conf_set_changed(bool val)
1122 if (conf_changed_callback && conf_changed != val)
1123 conf_changed_callback(val);
1128 bool conf_get_changed(void)
1130 return conf_changed;
1133 void conf_set_changed_callback(void (*fn)(bool))
1135 conf_changed_callback = fn;