3 * Released under the terms of the GNU GPL v2.0.
20 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
21 void (*print_comment)(FILE *, const char *, void *);
24 static void conf_warning(const char *fmt, ...)
25 __attribute__ ((format (printf, 1, 2)));
27 static void conf_message(const char *fmt, ...)
28 __attribute__ ((format (printf, 1, 2)));
30 static const char *conf_filename;
31 static int conf_lineno, conf_warnings;
33 const char conf_defname[] = "arch/$(ARCH)/defconfig";
35 static void conf_warning(const char *fmt, ...)
39 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
40 vfprintf(stderr, fmt, ap);
41 fprintf(stderr, "\n");
46 static void conf_default_message_callback(const char *fmt, va_list ap)
53 static void (*conf_message_callback) (const char *fmt, va_list ap) =
54 conf_default_message_callback;
55 void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
57 conf_message_callback = fn;
60 static void conf_message(const char *fmt, ...)
65 if (conf_message_callback)
66 conf_message_callback(fmt, ap);
70 const char *conf_get_configname(void)
72 char *name = getenv("KCONFIG_CONFIG");
74 return name ? name : ".config";
77 const char *conf_get_autoconfig_name(void)
79 char *name = getenv("KCONFIG_AUTOCONFIG");
81 return name ? name : "include/config/auto.conf";
84 char *conf_get_default_confname(void)
87 static char fullname[PATH_MAX+1];
90 name = expand_string(conf_defname);
91 env = getenv(SRCTREE);
93 sprintf(fullname, "%s/%s", env, name);
94 if (!stat(fullname, &buf))
100 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
107 sym->def[def].tri = mod;
108 sym->flags |= def_flags;
114 sym->def[def].tri = yes;
115 sym->flags |= def_flags;
119 sym->def[def].tri = no;
120 sym->flags |= def_flags;
123 if (def != S_DEF_AUTO)
124 conf_warning("symbol value '%s' invalid for %s",
129 for (p2 = p; *p2 && !isspace(*p2); p2++)
131 sym->type = S_STRING;
138 /* Last char has to be a '"' */
139 if (p[strlen(p) - 1] != '"') {
140 if (def != S_DEF_AUTO)
141 conf_warning("invalid string found");
144 /* Overwrite '"' with \0 for string termination */
145 p[strlen(p) - 1] = 0;
150 if (sym_string_valid(sym, p)) {
151 sym->def[def].val = xstrdup(p);
152 sym->flags |= def_flags;
154 if (def != S_DEF_AUTO)
155 conf_warning("symbol value '%s' invalid for %s",
166 #define LINE_GROWTH 16
167 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
170 size_t new_size = slen + 1;
172 new_size += LINE_GROWTH - 1;
174 nline = xrealloc(*lineptr, new_size);
182 (*lineptr)[slen] = c;
187 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
189 char *line = *lineptr;
193 int c = getc(stream);
197 if (add_byte(c, &line, slen, n) < 0)
202 if (add_byte('\0', &line, slen, n) < 0)
209 if (add_byte(c, &line, slen, n) < 0)
221 int conf_read_simple(const char *name, int def)
225 size_t line_asize = 0;
231 in = zconf_fopen(name);
233 struct property *prop;
235 name = conf_get_configname();
236 in = zconf_fopen(name);
239 sym_add_change_count(1);
240 if (!sym_defconfig_list)
243 for_all_defaults(sym_defconfig_list, prop) {
244 if (expr_calc_value(prop->visible.expr) == no ||
245 prop->expr->type != E_SYMBOL)
247 sym_calc_value(prop->expr->left.sym);
248 name = sym_get_string_value(prop->expr->left.sym);
249 in = zconf_fopen(name);
251 conf_message("using defaults found in %s",
261 conf_filename = name;
265 def_flags = SYMBOL_DEF << def;
266 for_all_symbols(i, sym) {
267 sym->flags |= SYMBOL_CHANGED;
268 sym->flags &= ~(def_flags|SYMBOL_VALID);
269 if (sym_is_choice(sym))
270 sym->flags |= def_flags;
275 if (sym->def[def].val)
276 free(sym->def[def].val);
279 sym->def[def].val = NULL;
280 sym->def[def].tri = no;
284 while (compat_getline(&line, &line_asize, in) != -1) {
287 if (line[0] == '#') {
288 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
290 p = strchr(line + 2 + strlen(CONFIG_), ' ');
294 if (strncmp(p, "is not set", 10))
296 if (def == S_DEF_USER) {
297 sym = sym_find(line + 2 + strlen(CONFIG_));
299 sym_add_change_count(1);
303 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
304 if (sym->type == S_UNKNOWN)
305 sym->type = S_BOOLEAN;
307 if (sym->flags & def_flags) {
308 conf_warning("override: reassigning to symbol %s", sym->name);
313 sym->def[def].tri = no;
314 sym->flags |= def_flags;
319 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
320 p = strchr(line + strlen(CONFIG_), '=');
324 p2 = strchr(p, '\n');
330 if (def == S_DEF_USER) {
331 sym = sym_find(line + strlen(CONFIG_));
333 sym_add_change_count(1);
337 sym = sym_lookup(line + strlen(CONFIG_), 0);
338 if (sym->type == S_UNKNOWN)
341 if (sym->flags & def_flags) {
342 conf_warning("override: reassigning to symbol %s", sym->name);
344 if (conf_set_sym_val(sym, def, def_flags, p))
347 if (line[0] != '\r' && line[0] != '\n')
348 conf_warning("unexpected data: %.*s",
349 (int)strcspn(line, "\r\n"), line);
354 if (sym && sym_is_choice_value(sym)) {
355 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
356 switch (sym->def[def].tri) {
360 if (cs->def[def].tri == yes) {
361 conf_warning("%s creates inconsistent choice state", sym->name);
362 cs->flags &= ~def_flags;
366 if (cs->def[def].tri != no)
367 conf_warning("override: %s changes choice state", sym->name);
368 cs->def[def].val = sym;
371 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
379 int conf_read(const char *name)
382 int conf_unsaved = 0;
385 sym_set_change_count(0);
387 if (conf_read_simple(name, S_DEF_USER)) {
388 sym_calc_value(modules_sym);
392 sym_calc_value(modules_sym);
394 for_all_symbols(i, sym) {
396 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
398 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
399 /* check that calculated value agrees with saved value */
403 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
405 if (!sym_is_choice(sym))
409 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
413 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
414 /* no previous value and not saved */
417 /* maybe print value in verbose mode... */
420 for_all_symbols(i, sym) {
421 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
422 /* Reset values of generates values, so they'll appear
423 * as new, if they should become visible, but that
424 * doesn't quite work if the Kconfig and the saved
425 * configuration disagree.
427 if (sym->visible == no && !conf_unsaved)
428 sym->flags &= ~SYMBOL_DEF_USER;
433 /* Reset a string value if it's out of range */
434 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
436 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
445 sym_add_change_count(conf_warnings || conf_unsaved);
451 * Kconfig configuration printer
453 * This printer is used when generating the resulting configuration after
454 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
455 * passing a non-NULL argument to the printer.
459 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
466 bool skip_unset = (arg != NULL);
469 fprintf(fp, "# %s%s is not set\n",
478 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
482 kconfig_print_comment(FILE *fp, const char *value, void *arg)
484 const char *p = value;
488 l = strcspn(p, "\n");
492 xfwrite(p, l, 1, fp);
501 static struct conf_printer kconfig_printer_cb =
503 .print_symbol = kconfig_print_symbol,
504 .print_comment = kconfig_print_comment,
510 * This printer is used when generating the `include/generated/autoconf.h' file.
513 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
519 const char *suffix = "";
528 fprintf(fp, "#define %s%s%s 1\n",
529 CONFIG_, sym->name, suffix);
534 const char *prefix = "";
536 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
538 fprintf(fp, "#define %s%s %s%s\n",
539 CONFIG_, sym->name, prefix, value);
544 fprintf(fp, "#define %s%s %s\n",
545 CONFIG_, sym->name, value);
554 header_print_comment(FILE *fp, const char *value, void *arg)
556 const char *p = value;
561 l = strcspn(p, "\n");
565 xfwrite(p, l, 1, fp);
572 fprintf(fp, " */\n");
575 static struct conf_printer header_printer_cb =
577 .print_symbol = header_print_symbol,
578 .print_comment = header_print_comment,
584 * This printer is used when generating the `include/config/tristate.conf' file.
587 tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
590 if (sym->type == S_TRISTATE && *value != 'n')
591 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
594 static struct conf_printer tristate_printer_cb =
596 .print_symbol = tristate_print_symbol,
597 .print_comment = kconfig_print_comment,
600 static void conf_write_symbol(FILE *fp, struct symbol *sym,
601 struct conf_printer *printer, void *printer_arg)
611 str = sym_get_string_value(sym);
612 str2 = xmalloc(strlen(str) + 3);
613 sprintf(str2, "\"%s\"", str);
614 printer->print_symbol(fp, sym, str2, printer_arg);
618 str = sym_get_string_value(sym);
619 printer->print_symbol(fp, sym, str, printer_arg);
624 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
628 snprintf(buf, sizeof(buf),
630 "Automatically generated file; DO NOT EDIT.\n"
632 rootmenu.prompt->text);
634 printer->print_comment(fp, buf, printer_arg);
638 * Write out a minimal config.
639 * All values that has default values are skipped as this is redundant.
641 int conf_write_defconfig(const char *filename)
647 out = fopen(filename, "w");
651 sym_clear_all_valid();
653 /* Traverse all menus to find all relevant symbols */
654 menu = rootmenu.list;
660 if (!menu_is_visible(menu))
662 } else if (!sym_is_choice(sym)) {
664 if (!(sym->flags & SYMBOL_WRITE))
666 sym->flags &= ~SYMBOL_WRITE;
667 /* If we cannot change the symbol - skip */
668 if (!sym_is_changable(sym))
670 /* If symbol equals to default value - skip */
671 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
675 * If symbol is a choice value and equals to the
676 * default for a choice - skip.
677 * But only if value is bool and equal to "y" and
678 * choice is not "optional".
679 * (If choice is "optional" then all values can be "n")
681 if (sym_is_choice_value(sym)) {
685 cs = prop_get_symbol(sym_get_choice_prop(sym));
686 ds = sym_choice_default(cs);
687 if (!sym_is_optional(cs) && sym == ds) {
688 if ((sym->type == S_BOOLEAN) &&
689 sym_get_tristate_value(sym) == yes)
693 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
696 if (menu->list != NULL) {
699 else if (menu->next != NULL) {
702 while ((menu = menu->parent)) {
703 if (menu->next != NULL) {
714 int conf_write(const char *name)
719 const char *basename;
721 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
725 if (name && name[0]) {
729 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
730 strcpy(dirname, name);
731 strcat(dirname, "/");
732 basename = conf_get_configname();
733 } else if ((slash = strrchr(name, '/'))) {
734 int size = slash - name + 1;
735 memcpy(dirname, name, size);
738 basename = slash + 1;
740 basename = conf_get_configname();
744 basename = conf_get_configname();
746 sprintf(newname, "%s%s", dirname, basename);
747 env = getenv("KCONFIG_OVERWRITECONFIG");
749 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
750 out = fopen(tmpname, "w");
753 out = fopen(newname, "w");
758 conf_write_heading(out, &kconfig_printer_cb, NULL);
760 if (!conf_get_changed())
761 sym_clear_all_valid();
763 menu = rootmenu.list;
767 if (!menu_is_visible(menu))
769 str = menu_get_prompt(menu);
774 } else if (!(sym->flags & SYMBOL_CHOICE)) {
776 if (!(sym->flags & SYMBOL_WRITE))
778 sym->flags &= ~SYMBOL_WRITE;
780 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
790 else while ((menu = menu->parent)) {
800 strcat(dirname, basename);
801 strcat(dirname, ".old");
802 rename(newname, dirname);
803 if (rename(tmpname, newname))
807 conf_message("configuration written to %s", newname);
809 sym_set_change_count(0);
814 static int conf_split_config(void)
817 char path[PATH_MAX+1];
823 name = conf_get_autoconfig_name();
824 conf_read_simple(name, S_DEF_AUTO);
825 sym_calc_value(modules_sym);
827 if (chdir("include/config"))
831 for_all_symbols(i, sym) {
833 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
835 if (sym->flags & SYMBOL_WRITE) {
836 if (sym->flags & SYMBOL_DEF_AUTO) {
838 * symbol has old and new value,
844 if (sym_get_tristate_value(sym) ==
845 sym->def[S_DEF_AUTO].tri)
851 if (!strcmp(sym_get_string_value(sym),
852 sym->def[S_DEF_AUTO].val))
860 * If there is no old value, only 'no' (unset)
861 * is allowed as new value.
866 if (sym_get_tristate_value(sym) == no)
873 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
874 /* There is neither an old nor a new value. */
877 * There is an old value, but no new value ('no' (unset)
878 * isn't saved in auto.conf, so the old value is always
879 * different from 'no').
882 /* Replace all '_' and append ".h" */
887 *d++ = (c == '_') ? '/' : c;
891 /* Assume directory path already exists. */
892 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
894 if (errno != ENOENT) {
899 * Create directory components,
900 * unless they exist already.
903 while ((d = strchr(d, '/'))) {
905 if (stat(path, &sb) && mkdir(path, 0755)) {
912 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
927 int conf_write_autoconf(void)
931 FILE *out, *tristate, *out_h;
934 sym_clear_all_valid();
936 file_write_dep("include/config/auto.conf.cmd");
938 if (conf_split_config())
941 out = fopen(".tmpconfig", "w");
945 tristate = fopen(".tmpconfig_tristate", "w");
951 out_h = fopen(".tmpconfig.h", "w");
958 conf_write_heading(out, &kconfig_printer_cb, NULL);
960 conf_write_heading(tristate, &tristate_printer_cb, NULL);
962 conf_write_heading(out_h, &header_printer_cb, NULL);
964 for_all_symbols(i, sym) {
966 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
969 /* write symbol to auto.conf, tristate and header files */
970 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
972 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
974 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
980 name = getenv("KCONFIG_AUTOHEADER");
982 name = "include/generated/autoconf.h";
983 if (rename(".tmpconfig.h", name))
985 name = getenv("KCONFIG_TRISTATE");
987 name = "include/config/tristate.conf";
988 if (rename(".tmpconfig_tristate", name))
990 name = conf_get_autoconfig_name();
992 * This must be the last step, kbuild has a dependency on auto.conf
993 * and this marks the successful completion of the previous steps.
995 if (rename(".tmpconfig", name))
1001 static int sym_change_count;
1002 static void (*conf_changed_callback)(void);
1004 void sym_set_change_count(int count)
1006 int _sym_change_count = sym_change_count;
1007 sym_change_count = count;
1008 if (conf_changed_callback &&
1009 (bool)_sym_change_count != (bool)count)
1010 conf_changed_callback();
1013 void sym_add_change_count(int count)
1015 sym_set_change_count(count + sym_change_count);
1018 bool conf_get_changed(void)
1020 return sym_change_count;
1023 void conf_set_changed_callback(void (*fn)(void))
1025 conf_changed_callback = fn;
1028 static bool randomize_choice_values(struct symbol *csym)
1030 struct property *prop;
1036 * If choice is mod then we may have more items selected
1037 * and if no then no-one.
1038 * In both cases stop.
1040 if (csym->curr.tri != yes)
1043 prop = sym_get_choice_prop(csym);
1045 /* count entries in choice block */
1047 expr_list_for_each_sym(prop->expr, e, sym)
1051 * find a random value and set it to yes,
1052 * set the rest to no so we have only one set
1054 def = (rand() % cnt);
1057 expr_list_for_each_sym(prop->expr, e, sym) {
1059 sym->def[S_DEF_USER].tri = yes;
1060 csym->def[S_DEF_USER].val = sym;
1063 sym->def[S_DEF_USER].tri = no;
1065 sym->flags |= SYMBOL_DEF_USER;
1066 /* clear VALID to get value calculated */
1067 sym->flags &= ~SYMBOL_VALID;
1069 csym->flags |= SYMBOL_DEF_USER;
1070 /* clear VALID to get value calculated */
1071 csym->flags &= ~(SYMBOL_VALID);
1076 void set_all_choice_values(struct symbol *csym)
1078 struct property *prop;
1082 prop = sym_get_choice_prop(csym);
1085 * Set all non-assinged choice values to no
1087 expr_list_for_each_sym(prop->expr, e, sym) {
1088 if (!sym_has_value(sym))
1089 sym->def[S_DEF_USER].tri = no;
1091 csym->flags |= SYMBOL_DEF_USER;
1092 /* clear VALID to get value calculated */
1093 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1096 bool conf_set_all_new_symbols(enum conf_def_mode mode)
1098 struct symbol *sym, *csym;
1099 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
1100 * pty: probability of tristate = y
1101 * ptm: probability of tristate = m
1104 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1105 * below, otherwise gcc whines about
1106 * -Wmaybe-uninitialized */
1107 if (mode == def_random) {
1109 char *env = getenv("KCONFIG_PROBABILITY");
1111 while( env && *env ) {
1113 int tmp = strtol( env, &endp, 10 );
1114 if( tmp >= 0 && tmp <= 100 ) {
1118 perror( "KCONFIG_PROBABILITY" );
1121 env = (*endp == ':') ? endp+1 : endp;
1128 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1131 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1134 pby = p[0]; pty = p[1]; ptm = p[2];
1138 if( pty+ptm > 100 ) {
1140 perror( "KCONFIG_PROBABILITY" );
1144 bool has_changed = false;
1146 for_all_symbols(i, sym) {
1147 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1149 switch (sym_get_type(sym)) {
1155 sym->def[S_DEF_USER].tri = yes;
1158 sym->def[S_DEF_USER].tri = mod;
1161 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1162 sym->def[S_DEF_USER].tri = yes;
1164 sym->def[S_DEF_USER].tri = no;
1167 sym->def[S_DEF_USER].tri = no;
1169 if (sym->type == S_TRISTATE) {
1171 sym->def[S_DEF_USER].tri = yes;
1172 else if (cnt < (pty+ptm))
1173 sym->def[S_DEF_USER].tri = mod;
1174 } else if (cnt < pby)
1175 sym->def[S_DEF_USER].tri = yes;
1180 if (!(sym_is_choice(sym) && mode == def_random))
1181 sym->flags |= SYMBOL_DEF_USER;
1189 sym_clear_all_valid();
1192 * We have different type of choice blocks.
1193 * If curr.tri equals to mod then we can select several
1194 * choice symbols in one block.
1195 * In this case we do nothing.
1196 * If curr.tri equals yes then only one symbol can be
1197 * selected in a choice block and we set it to yes,
1198 * and the rest to no.
1200 if (mode != def_random) {
1201 for_all_symbols(i, csym) {
1202 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1203 sym_is_choice_value(csym))
1204 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1208 for_all_symbols(i, csym) {
1209 if (sym_has_value(csym) || !sym_is_choice(csym))
1212 sym_calc_value(csym);
1213 if (mode == def_random)
1214 has_changed = randomize_choice_values(csym);
1216 set_all_choice_values(csym);