Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> |
| 3 | * Released under the terms of the GNU GPL v2.0. |
| 4 | */ |
| 5 | |
| 6 | #include <sys/stat.h> |
| 7 | #include <ctype.h> |
Arnaud Lacombe | 94bedec | 2010-08-17 01:40:20 -0400 | [diff] [blame] | 8 | #include <errno.h> |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 9 | #include <fcntl.h> |
Arnaud Lacombe | 10a4b27 | 2011-06-01 16:00:46 -0400 | [diff] [blame] | 10 | #include <stdarg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "lkc.h" |
| 18 | |
Masahiro Yamada | 0608182 | 2018-07-20 16:46:27 +0900 | [diff] [blame] | 19 | /* return true if 'path' exists, false otherwise */ |
| 20 | static bool is_present(const char *path) |
| 21 | { |
| 22 | struct stat st; |
| 23 | |
| 24 | return !stat(path, &st); |
| 25 | } |
| 26 | |
| 27 | /* return true if 'path' exists and it is a directory, false otherwise */ |
| 28 | static bool is_dir(const char *path) |
| 29 | { |
| 30 | struct stat st; |
| 31 | |
| 32 | if (stat(path, &st)) |
| 33 | return 0; |
| 34 | |
| 35 | return S_ISDIR(st.st_mode); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Create the parent directory of the given path. |
| 40 | * |
| 41 | * For example, if 'include/config/auto.conf' is given, create 'include/config'. |
| 42 | */ |
| 43 | static int make_parent_dir(const char *path) |
| 44 | { |
| 45 | char tmp[PATH_MAX + 1]; |
| 46 | char *p; |
| 47 | |
| 48 | strncpy(tmp, path, sizeof(tmp)); |
| 49 | tmp[sizeof(tmp) - 1] = 0; |
| 50 | |
| 51 | /* Remove the base name. Just return if nothing is left */ |
| 52 | p = strrchr(tmp, '/'); |
| 53 | if (!p) |
| 54 | return 0; |
| 55 | *(p + 1) = 0; |
| 56 | |
| 57 | /* Just in case it is an absolute path */ |
| 58 | p = tmp; |
| 59 | while (*p == '/') |
| 60 | p++; |
| 61 | |
| 62 | while ((p = strchr(p, '/'))) { |
| 63 | *p = 0; |
| 64 | |
| 65 | /* skip if the directory exists */ |
| 66 | if (!is_dir(tmp) && mkdir(tmp, 0755)) |
| 67 | return -1; |
| 68 | |
| 69 | *p = '/'; |
| 70 | while (*p == '/') |
| 71 | p++; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Masahiro Yamada | 1508fec | 2018-11-30 18:15:50 +0900 | [diff] [blame^] | 77 | static char depfile_path[PATH_MAX]; |
| 78 | static size_t depfile_prefix_len; |
| 79 | |
| 80 | /* touch depfile for symbol 'name' */ |
| 81 | static int conf_touch_dep(const char *name) |
| 82 | { |
| 83 | int fd, ret; |
| 84 | const char *s; |
| 85 | char *d, c; |
| 86 | |
| 87 | /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */ |
| 88 | if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path)) |
| 89 | return -1; |
| 90 | |
| 91 | d = depfile_path + depfile_prefix_len; |
| 92 | s = name; |
| 93 | |
| 94 | while ((c = *s++)) |
| 95 | *d++ = (c == '_') ? '/' : tolower(c); |
| 96 | strcpy(d, ".h"); |
| 97 | |
| 98 | /* Assume directory path already exists. */ |
| 99 | fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 100 | if (fd == -1) { |
| 101 | if (errno != ENOENT) |
| 102 | return -1; |
| 103 | |
| 104 | ret = make_parent_dir(depfile_path); |
| 105 | if (ret) |
| 106 | return ret; |
| 107 | |
| 108 | /* Try it again. */ |
| 109 | fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 110 | if (fd == -1) |
| 111 | return -1; |
| 112 | } |
| 113 | close(fd); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Michal Marek | ad8d40c | 2015-02-24 16:37:13 +0100 | [diff] [blame] | 118 | struct conf_printer { |
| 119 | void (*print_symbol)(FILE *, struct symbol *, const char *, void *); |
| 120 | void (*print_comment)(FILE *, const char *, void *); |
| 121 | }; |
| 122 | |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 123 | static void conf_warning(const char *fmt, ...) |
| 124 | __attribute__ ((format (printf, 1, 2))); |
| 125 | |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 126 | static void conf_message(const char *fmt, ...) |
| 127 | __attribute__ ((format (printf, 1, 2))); |
| 128 | |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 129 | static const char *conf_filename; |
Masahiro Yamada | 84dd95d | 2018-01-11 22:39:41 +0900 | [diff] [blame] | 130 | static int conf_lineno, conf_warnings; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 131 | |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 132 | const char conf_defname[] = "arch/$(ARCH)/defconfig"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 134 | static void conf_warning(const char *fmt, ...) |
| 135 | { |
| 136 | va_list ap; |
| 137 | va_start(ap, fmt); |
| 138 | fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); |
| 139 | vfprintf(stderr, fmt, ap); |
| 140 | fprintf(stderr, "\n"); |
| 141 | va_end(ap); |
| 142 | conf_warnings++; |
| 143 | } |
| 144 | |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 145 | static void conf_default_message_callback(const char *s) |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 146 | { |
| 147 | printf("#\n# "); |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 148 | printf("%s", s); |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 149 | printf("\n#\n"); |
| 150 | } |
| 151 | |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 152 | static void (*conf_message_callback)(const char *s) = |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 153 | conf_default_message_callback; |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 154 | void conf_set_message_callback(void (*fn)(const char *s)) |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 155 | { |
| 156 | conf_message_callback = fn; |
| 157 | } |
| 158 | |
| 159 | static void conf_message(const char *fmt, ...) |
| 160 | { |
| 161 | va_list ap; |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 162 | char buf[4096]; |
| 163 | |
| 164 | if (!conf_message_callback) |
| 165 | return; |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 166 | |
| 167 | va_start(ap, fmt); |
Masahiro Yamada | 5accd7f | 2018-07-05 11:46:12 +0900 | [diff] [blame] | 168 | |
| 169 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 170 | conf_message_callback(buf); |
Colin Ian King | b6a2ab2 | 2015-01-12 13:18:26 +0000 | [diff] [blame] | 171 | va_end(ap); |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 174 | const char *conf_get_configname(void) |
| 175 | { |
| 176 | char *name = getenv("KCONFIG_CONFIG"); |
| 177 | |
| 178 | return name ? name : ".config"; |
| 179 | } |
| 180 | |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 181 | const char *conf_get_autoconfig_name(void) |
| 182 | { |
| 183 | char *name = getenv("KCONFIG_AUTOCONFIG"); |
| 184 | |
| 185 | return name ? name : "include/config/auto.conf"; |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | char *conf_get_default_confname(void) |
| 189 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static char fullname[PATH_MAX+1]; |
| 191 | char *env, *name; |
| 192 | |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 193 | name = expand_string(conf_defname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | env = getenv(SRCTREE); |
| 195 | if (env) { |
| 196 | sprintf(fullname, "%s/%s", env, name); |
Masahiro Yamada | 0608182 | 2018-07-20 16:46:27 +0900 | [diff] [blame] | 197 | if (is_present(fullname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | return fullname; |
| 199 | } |
| 200 | return name; |
| 201 | } |
| 202 | |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 203 | static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) |
| 204 | { |
| 205 | char *p2; |
| 206 | |
| 207 | switch (sym->type) { |
| 208 | case S_TRISTATE: |
| 209 | if (p[0] == 'm') { |
| 210 | sym->def[def].tri = mod; |
| 211 | sym->flags |= def_flags; |
| 212 | break; |
| 213 | } |
Arnaud Lacombe | d8fc320 | 2011-05-31 12:30:26 -0400 | [diff] [blame] | 214 | /* fall through */ |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 215 | case S_BOOLEAN: |
| 216 | if (p[0] == 'y') { |
| 217 | sym->def[def].tri = yes; |
| 218 | sym->flags |= def_flags; |
| 219 | break; |
| 220 | } |
| 221 | if (p[0] == 'n') { |
| 222 | sym->def[def].tri = no; |
| 223 | sym->flags |= def_flags; |
| 224 | break; |
| 225 | } |
Yann E. MORIN | 04b19b77 | 2013-08-06 18:45:07 +0200 | [diff] [blame] | 226 | if (def != S_DEF_AUTO) |
| 227 | conf_warning("symbol value '%s' invalid for %s", |
| 228 | p, sym->name); |
Arnaud Lacombe | 75f1468 | 2011-05-31 12:31:57 -0400 | [diff] [blame] | 229 | return 1; |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 230 | case S_OTHER: |
| 231 | if (*p != '"') { |
| 232 | for (p2 = p; *p2 && !isspace(*p2); p2++) |
| 233 | ; |
| 234 | sym->type = S_STRING; |
| 235 | goto done; |
| 236 | } |
Arnaud Lacombe | d8fc320 | 2011-05-31 12:30:26 -0400 | [diff] [blame] | 237 | /* fall through */ |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 238 | case S_STRING: |
| 239 | if (*p++ != '"') |
| 240 | break; |
| 241 | for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { |
| 242 | if (*p2 == '"') { |
| 243 | *p2 = 0; |
| 244 | break; |
| 245 | } |
| 246 | memmove(p2, p2 + 1, strlen(p2)); |
| 247 | } |
| 248 | if (!p2) { |
Yann E. MORIN | 04b19b77 | 2013-08-06 18:45:07 +0200 | [diff] [blame] | 249 | if (def != S_DEF_AUTO) |
| 250 | conf_warning("invalid string found"); |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 251 | return 1; |
| 252 | } |
Arnaud Lacombe | d8fc320 | 2011-05-31 12:30:26 -0400 | [diff] [blame] | 253 | /* fall through */ |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 254 | case S_INT: |
| 255 | case S_HEX: |
| 256 | done: |
| 257 | if (sym_string_valid(sym, p)) { |
Masahiro Yamada | cd81fc8 | 2018-02-17 03:38:31 +0900 | [diff] [blame] | 258 | sym->def[def].val = xstrdup(p); |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 259 | sym->flags |= def_flags; |
| 260 | } else { |
Yann E. MORIN | 04b19b77 | 2013-08-06 18:45:07 +0200 | [diff] [blame] | 261 | if (def != S_DEF_AUTO) |
| 262 | conf_warning("symbol value '%s' invalid for %s", |
| 263 | p, sym->name); |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 264 | return 1; |
| 265 | } |
| 266 | break; |
| 267 | default: |
| 268 | ; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
Cody Schafer | 1a7a8c6 | 2012-07-13 11:27:12 -0700 | [diff] [blame] | 273 | #define LINE_GROWTH 16 |
| 274 | static int add_byte(int c, char **lineptr, size_t slen, size_t *n) |
| 275 | { |
| 276 | char *nline; |
| 277 | size_t new_size = slen + 1; |
| 278 | if (new_size > *n) { |
| 279 | new_size += LINE_GROWTH - 1; |
| 280 | new_size *= 2; |
Masahiro Yamada | d717f24 | 2018-02-09 01:19:07 +0900 | [diff] [blame] | 281 | nline = xrealloc(*lineptr, new_size); |
Cody Schafer | 1a7a8c6 | 2012-07-13 11:27:12 -0700 | [diff] [blame] | 282 | if (!nline) |
| 283 | return -1; |
| 284 | |
| 285 | *lineptr = nline; |
| 286 | *n = new_size; |
| 287 | } |
| 288 | |
| 289 | (*lineptr)[slen] = c; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream) |
| 295 | { |
| 296 | char *line = *lineptr; |
| 297 | size_t slen = 0; |
| 298 | |
| 299 | for (;;) { |
| 300 | int c = getc(stream); |
| 301 | |
| 302 | switch (c) { |
| 303 | case '\n': |
| 304 | if (add_byte(c, &line, slen, n) < 0) |
| 305 | goto e_out; |
| 306 | slen++; |
| 307 | /* fall through */ |
| 308 | case EOF: |
| 309 | if (add_byte('\0', &line, slen, n) < 0) |
| 310 | goto e_out; |
| 311 | *lineptr = line; |
| 312 | if (slen == 0) |
| 313 | return -1; |
| 314 | return slen; |
| 315 | default: |
| 316 | if (add_byte(c, &line, slen, n) < 0) |
| 317 | goto e_out; |
| 318 | slen++; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | e_out: |
| 323 | line[slen-1] = '\0'; |
| 324 | *lineptr = line; |
| 325 | return -1; |
| 326 | } |
| 327 | |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 328 | int conf_read_simple(const char *name, int def) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
| 330 | FILE *in = NULL; |
Cody Schafer | 1a7a8c6 | 2012-07-13 11:27:12 -0700 | [diff] [blame] | 331 | char *line = NULL; |
| 332 | size_t line_asize = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | char *p, *p2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | struct symbol *sym; |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 335 | int i, def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | if (name) { |
| 338 | in = zconf_fopen(name); |
| 339 | } else { |
Roman Zippel | face437 | 2006-06-08 22:12:45 -0700 | [diff] [blame] | 340 | struct property *prop; |
| 341 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 342 | name = conf_get_configname(); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 343 | in = zconf_fopen(name); |
| 344 | if (in) |
| 345 | goto load; |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 346 | sym_add_change_count(1); |
Al Viro | 6b87b70 | 2016-01-14 18:13:49 +0000 | [diff] [blame] | 347 | if (!sym_defconfig_list) |
Roman Zippel | face437 | 2006-06-08 22:12:45 -0700 | [diff] [blame] | 348 | return 1; |
| 349 | |
| 350 | for_all_defaults(sym_defconfig_list, prop) { |
| 351 | if (expr_calc_value(prop->visible.expr) == no || |
| 352 | prop->expr->type != E_SYMBOL) |
| 353 | continue; |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 354 | sym_calc_value(prop->expr->left.sym); |
| 355 | name = sym_get_string_value(prop->expr->left.sym); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | in = zconf_fopen(name); |
| 357 | if (in) { |
Sam Ravnborg | 694c49a | 2018-05-22 21:36:12 +0200 | [diff] [blame] | 358 | conf_message("using defaults found in %s", |
Michal Marek | 42368c3 | 2010-08-17 10:21:19 +0200 | [diff] [blame] | 359 | name); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 360 | goto load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (!in) |
| 365 | return 1; |
| 366 | |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 367 | load: |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 368 | conf_filename = name; |
| 369 | conf_lineno = 0; |
| 370 | conf_warnings = 0; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 371 | |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 372 | def_flags = SYMBOL_DEF << def; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | for_all_symbols(i, sym) { |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 374 | sym->flags |= SYMBOL_CHANGED; |
| 375 | sym->flags &= ~(def_flags|SYMBOL_VALID); |
Yann E. MORIN | 490f161 | 2013-06-25 23:37:44 +0200 | [diff] [blame] | 376 | if (sym_is_choice(sym)) |
| 377 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | switch (sym->type) { |
| 379 | case S_INT: |
| 380 | case S_HEX: |
| 381 | case S_STRING: |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 382 | if (sym->def[def].val) |
| 383 | free(sym->def[def].val); |
Arnaud Lacombe | d8fc320 | 2011-05-31 12:30:26 -0400 | [diff] [blame] | 384 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | default: |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 386 | sym->def[def].val = NULL; |
| 387 | sym->def[def].tri = no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Cody Schafer | 1a7a8c6 | 2012-07-13 11:27:12 -0700 | [diff] [blame] | 391 | while (compat_getline(&line, &line_asize, in) != -1) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 392 | conf_lineno++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | sym = NULL; |
Arnaud Lacombe | 8baefd3 | 2010-08-24 00:14:47 -0400 | [diff] [blame] | 394 | if (line[0] == '#') { |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 395 | if (memcmp(line + 2, CONFIG_, strlen(CONFIG_))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | continue; |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 397 | p = strchr(line + 2 + strlen(CONFIG_), ' '); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | if (!p) |
| 399 | continue; |
| 400 | *p++ = 0; |
| 401 | if (strncmp(p, "is not set", 10)) |
| 402 | continue; |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 403 | if (def == S_DEF_USER) { |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 404 | sym = sym_find(line + 2 + strlen(CONFIG_)); |
zippel@linux-m68k.org | 661b068 | 2008-09-29 05:27:11 +0200 | [diff] [blame] | 405 | if (!sym) { |
| 406 | sym_add_change_count(1); |
Masahiro Yamada | 75889e9 | 2018-11-30 18:15:48 +0900 | [diff] [blame] | 407 | continue; |
zippel@linux-m68k.org | 661b068 | 2008-09-29 05:27:11 +0200 | [diff] [blame] | 408 | } |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 409 | } else { |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 410 | sym = sym_lookup(line + 2 + strlen(CONFIG_), 0); |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 411 | if (sym->type == S_UNKNOWN) |
| 412 | sym->type = S_BOOLEAN; |
| 413 | } |
| 414 | if (sym->flags & def_flags) { |
Jan Engelhardt | d84876f | 2008-01-03 23:33:44 +0100 | [diff] [blame] | 415 | conf_warning("override: reassigning to symbol %s", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | switch (sym->type) { |
| 418 | case S_BOOLEAN: |
| 419 | case S_TRISTATE: |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 420 | sym->def[def].tri = no; |
| 421 | sym->flags |= def_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | break; |
| 423 | default: |
| 424 | ; |
| 425 | } |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 426 | } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) { |
| 427 | p = strchr(line + strlen(CONFIG_), '='); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (!p) |
| 429 | continue; |
| 430 | *p++ = 0; |
| 431 | p2 = strchr(p, '\n'); |
Matthew Wilcox | d3660a8 | 2006-07-13 12:54:07 -0600 | [diff] [blame] | 432 | if (p2) { |
| 433 | *p2-- = 0; |
| 434 | if (*p2 == '\r') |
| 435 | *p2 = 0; |
| 436 | } |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 437 | if (def == S_DEF_USER) { |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 438 | sym = sym_find(line + strlen(CONFIG_)); |
zippel@linux-m68k.org | 661b068 | 2008-09-29 05:27:11 +0200 | [diff] [blame] | 439 | if (!sym) { |
| 440 | sym_add_change_count(1); |
Masahiro Yamada | 75889e9 | 2018-11-30 18:15:48 +0900 | [diff] [blame] | 441 | continue; |
zippel@linux-m68k.org | 661b068 | 2008-09-29 05:27:11 +0200 | [diff] [blame] | 442 | } |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 443 | } else { |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 444 | sym = sym_lookup(line + strlen(CONFIG_), 0); |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 445 | if (sym->type == S_UNKNOWN) |
| 446 | sym->type = S_OTHER; |
| 447 | } |
| 448 | if (sym->flags & def_flags) { |
Jan Engelhardt | d84876f | 2008-01-03 23:33:44 +0100 | [diff] [blame] | 449 | conf_warning("override: reassigning to symbol %s", sym->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
Sam Ravnborg | 9c900a9 | 2007-11-10 20:01:56 +0100 | [diff] [blame] | 451 | if (conf_set_sym_val(sym, def, def_flags, p)) |
| 452 | continue; |
Arnaud Lacombe | 8baefd3 | 2010-08-24 00:14:47 -0400 | [diff] [blame] | 453 | } else { |
| 454 | if (line[0] != '\r' && line[0] != '\n') |
Paul Bolle | a466391 | 2016-03-16 21:27:27 +0100 | [diff] [blame] | 455 | conf_warning("unexpected data: %.*s", |
| 456 | (int)strcspn(line, "\r\n"), line); |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | continue; |
| 459 | } |
Masahiro Yamada | 75889e9 | 2018-11-30 18:15:48 +0900 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (sym && sym_is_choice_value(sym)) { |
| 462 | struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 463 | switch (sym->def[def].tri) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | case no: |
| 465 | break; |
| 466 | case mod: |
Roman Zippel | 669bfad9 | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 467 | if (cs->def[def].tri == yes) { |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 468 | conf_warning("%s creates inconsistent choice state", sym->name); |
Yann E. MORIN | 490f161 | 2013-06-25 23:37:44 +0200 | [diff] [blame] | 469 | cs->flags &= ~def_flags; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 470 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | break; |
| 472 | case yes: |
Jan Engelhardt | d84876f | 2008-01-03 23:33:44 +0100 | [diff] [blame] | 473 | if (cs->def[def].tri != no) |
| 474 | conf_warning("override: %s changes choice state", sym->name); |
| 475 | cs->def[def].val = sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | break; |
| 477 | } |
Sam Ravnborg | d6ee357 | 2008-01-07 21:09:55 +0100 | [diff] [blame] | 478 | cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
Cody Schafer | 1a7a8c6 | 2012-07-13 11:27:12 -0700 | [diff] [blame] | 481 | free(line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | fclose(in); |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | int conf_read(const char *name) |
| 487 | { |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 488 | struct symbol *sym; |
Masahiro Yamada | 84dd95d | 2018-01-11 22:39:41 +0900 | [diff] [blame] | 489 | int conf_unsaved = 0; |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 490 | int i; |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 491 | |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 492 | sym_set_change_count(0); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 493 | |
Al Viro | 6b87b70 | 2016-01-14 18:13:49 +0000 | [diff] [blame] | 494 | if (conf_read_simple(name, S_DEF_USER)) { |
| 495 | sym_calc_value(modules_sym); |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 496 | return 1; |
Al Viro | 6b87b70 | 2016-01-14 18:13:49 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | sym_calc_value(modules_sym); |
Roman Zippel | 9038916 | 2005-11-08 21:34:49 -0800 | [diff] [blame] | 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | for_all_symbols(i, sym) { |
| 502 | sym_calc_value(sym); |
Dirk Gouders | 693359f | 2018-07-03 14:43:31 +0200 | [diff] [blame] | 503 | if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE)) |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 504 | continue; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 505 | if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { |
| 506 | /* check that calculated value agrees with saved value */ |
| 507 | switch (sym->type) { |
| 508 | case S_BOOLEAN: |
| 509 | case S_TRISTATE: |
Roman Zippel | 0c1822e | 2006-06-08 22:12:41 -0700 | [diff] [blame] | 510 | if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym)) |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 511 | break; |
| 512 | if (!sym_is_choice(sym)) |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 513 | continue; |
Arnaud Lacombe | d8fc320 | 2011-05-31 12:30:26 -0400 | [diff] [blame] | 514 | /* fall through */ |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 515 | default: |
Roman Zippel | 0c1822e | 2006-06-08 22:12:41 -0700 | [diff] [blame] | 516 | if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 517 | continue; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) |
| 521 | /* no previous value and not saved */ |
Arnaud Lacombe | 5d09598 | 2012-01-23 17:29:05 -0500 | [diff] [blame] | 522 | continue; |
Roman Zippel | c1a0f5e | 2005-11-08 21:34:54 -0800 | [diff] [blame] | 523 | conf_unsaved++; |
| 524 | /* maybe print value in verbose mode... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Roman Zippel | d8982ba | 2007-07-09 11:43:58 -0700 | [diff] [blame] | 527 | for_all_symbols(i, 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. |
| 533 | */ |
| 534 | if (sym->visible == no && !conf_unsaved) |
| 535 | sym->flags &= ~SYMBOL_DEF_USER; |
| 536 | switch (sym->type) { |
| 537 | case S_STRING: |
| 538 | case S_INT: |
| 539 | case S_HEX: |
| 540 | /* Reset a string value if it's out of range */ |
| 541 | if (sym_string_within_range(sym, sym->def[S_DEF_USER].val)) |
| 542 | break; |
| 543 | sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER); |
| 544 | conf_unsaved++; |
| 545 | break; |
| 546 | default: |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 552 | sym_add_change_count(conf_warnings || conf_unsaved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 557 | /* |
| 558 | * Kconfig configuration printer |
| 559 | * |
| 560 | * This printer is used when generating the resulting configuration after |
| 561 | * kconfig invocation and `defconfig' files. Unset symbol might be omitted by |
| 562 | * passing a non-NULL argument to the printer. |
| 563 | * |
| 564 | */ |
| 565 | static void |
| 566 | kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 567 | { |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 568 | |
Arnaud Lacombe | 0dce631 | 2010-12-05 01:33:16 -0500 | [diff] [blame] | 569 | switch (sym->type) { |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 570 | case S_BOOLEAN: |
| 571 | case S_TRISTATE: |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 572 | if (*value == 'n') { |
| 573 | bool skip_unset = (arg != NULL); |
| 574 | |
| 575 | if (!skip_unset) |
| 576 | fprintf(fp, "# %s%s is not set\n", |
Arnaud Lacombe | ffb5957 | 2010-08-14 23:57:43 -0400 | [diff] [blame] | 577 | CONFIG_, sym->name); |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 578 | return; |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 579 | } |
| 580 | break; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 581 | default: |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 582 | break; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value); |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | kconfig_print_comment(FILE *fp, const char *value, void *arg) |
| 590 | { |
| 591 | const char *p = value; |
| 592 | size_t l; |
| 593 | |
| 594 | for (;;) { |
| 595 | l = strcspn(p, "\n"); |
| 596 | fprintf(fp, "#"); |
| 597 | if (l) { |
| 598 | fprintf(fp, " "); |
Peter Foley | 70cc01e | 2011-10-22 10:48:49 -0400 | [diff] [blame] | 599 | xfwrite(p, l, 1, fp); |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 600 | p += l; |
| 601 | } |
| 602 | fprintf(fp, "\n"); |
| 603 | if (*p++ == '\0') |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static struct conf_printer kconfig_printer_cb = |
| 609 | { |
| 610 | .print_symbol = kconfig_print_symbol, |
| 611 | .print_comment = kconfig_print_comment, |
| 612 | }; |
| 613 | |
| 614 | /* |
| 615 | * Header printer |
| 616 | * |
| 617 | * This printer is used when generating the `include/generated/autoconf.h' file. |
| 618 | */ |
| 619 | static void |
| 620 | header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) |
| 621 | { |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 622 | |
| 623 | switch (sym->type) { |
| 624 | case S_BOOLEAN: |
Arnaud Lacombe | eb4cf5a | 2011-07-14 15:31:07 -0400 | [diff] [blame] | 625 | case S_TRISTATE: { |
| 626 | const char *suffix = ""; |
| 627 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 628 | switch (*value) { |
| 629 | case 'n': |
Michal Marek | 2a11c8e | 2011-07-20 17:38:57 +0200 | [diff] [blame] | 630 | break; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 631 | case 'm': |
| 632 | suffix = "_MODULE"; |
Arnaud Lacombe | eb4cf5a | 2011-07-14 15:31:07 -0400 | [diff] [blame] | 633 | /* fall through */ |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 634 | default: |
Michal Marek | 2a11c8e | 2011-07-20 17:38:57 +0200 | [diff] [blame] | 635 | fprintf(fp, "#define %s%s%s 1\n", |
| 636 | CONFIG_, sym->name, suffix); |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 637 | } |
Arnaud Lacombe | eb4cf5a | 2011-07-14 15:31:07 -0400 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | case S_HEX: { |
| 641 | const char *prefix = ""; |
| 642 | |
| 643 | if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X')) |
| 644 | prefix = "0x"; |
| 645 | fprintf(fp, "#define %s%s %s%s\n", |
| 646 | CONFIG_, sym->name, prefix, value); |
| 647 | break; |
| 648 | } |
| 649 | case S_STRING: |
| 650 | case S_INT: |
| 651 | fprintf(fp, "#define %s%s %s\n", |
| 652 | CONFIG_, sym->name, value); |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 653 | break; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 654 | default: |
| 655 | break; |
| 656 | } |
| 657 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static void |
| 661 | header_print_comment(FILE *fp, const char *value, void *arg) |
| 662 | { |
| 663 | const char *p = value; |
| 664 | size_t l; |
| 665 | |
| 666 | fprintf(fp, "/*\n"); |
| 667 | for (;;) { |
| 668 | l = strcspn(p, "\n"); |
| 669 | fprintf(fp, " *"); |
| 670 | if (l) { |
| 671 | fprintf(fp, " "); |
Peter Foley | 70cc01e | 2011-10-22 10:48:49 -0400 | [diff] [blame] | 672 | xfwrite(p, l, 1, fp); |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 673 | p += l; |
| 674 | } |
| 675 | fprintf(fp, "\n"); |
| 676 | if (*p++ == '\0') |
| 677 | break; |
| 678 | } |
| 679 | fprintf(fp, " */\n"); |
| 680 | } |
| 681 | |
| 682 | static struct conf_printer header_printer_cb = |
| 683 | { |
| 684 | .print_symbol = header_print_symbol, |
| 685 | .print_comment = header_print_comment, |
| 686 | }; |
| 687 | |
| 688 | /* |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 689 | * Tristate printer |
| 690 | * |
| 691 | * This printer is used when generating the `include/config/tristate.conf' file. |
| 692 | */ |
| 693 | static void |
| 694 | tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) |
| 695 | { |
| 696 | |
| 697 | if (sym->type == S_TRISTATE && *value != 'n') |
| 698 | fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value)); |
| 699 | } |
| 700 | |
| 701 | static struct conf_printer tristate_printer_cb = |
| 702 | { |
| 703 | .print_symbol = tristate_print_symbol, |
| 704 | .print_comment = kconfig_print_comment, |
| 705 | }; |
| 706 | |
| 707 | static void conf_write_symbol(FILE *fp, struct symbol *sym, |
| 708 | struct conf_printer *printer, void *printer_arg) |
| 709 | { |
| 710 | const char *str; |
| 711 | |
| 712 | switch (sym->type) { |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 713 | case S_OTHER: |
| 714 | case S_UNKNOWN: |
| 715 | break; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 716 | case S_STRING: |
| 717 | str = sym_get_string_value(sym); |
| 718 | str = sym_escape_string_value(str); |
| 719 | printer->print_symbol(fp, sym, str, printer_arg); |
| 720 | free((void *)str); |
| 721 | break; |
| 722 | default: |
| 723 | str = sym_get_string_value(sym); |
| 724 | printer->print_symbol(fp, sym, str, printer_arg); |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 728 | static void |
| 729 | conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg) |
| 730 | { |
| 731 | char buf[256]; |
| 732 | |
| 733 | snprintf(buf, sizeof(buf), |
| 734 | "\n" |
| 735 | "Automatically generated file; DO NOT EDIT.\n" |
| 736 | "%s\n", |
| 737 | rootmenu.prompt->text); |
| 738 | |
| 739 | printer->print_comment(fp, buf, printer_arg); |
| 740 | } |
| 741 | |
Sam Ravnborg | 7cf3d73 | 2010-07-31 23:35:34 +0200 | [diff] [blame] | 742 | /* |
| 743 | * Write out a minimal config. |
| 744 | * All values that has default values are skipped as this is redundant. |
| 745 | */ |
| 746 | int conf_write_defconfig(const char *filename) |
| 747 | { |
| 748 | struct symbol *sym; |
| 749 | struct menu *menu; |
| 750 | FILE *out; |
| 751 | |
| 752 | out = fopen(filename, "w"); |
| 753 | if (!out) |
| 754 | return 1; |
| 755 | |
| 756 | sym_clear_all_valid(); |
| 757 | |
| 758 | /* Traverse all menus to find all relevant symbols */ |
| 759 | menu = rootmenu.list; |
| 760 | |
| 761 | while (menu != NULL) |
| 762 | { |
| 763 | sym = menu->sym; |
| 764 | if (sym == NULL) { |
| 765 | if (!menu_is_visible(menu)) |
| 766 | goto next_menu; |
| 767 | } else if (!sym_is_choice(sym)) { |
| 768 | sym_calc_value(sym); |
| 769 | if (!(sym->flags & SYMBOL_WRITE)) |
| 770 | goto next_menu; |
| 771 | sym->flags &= ~SYMBOL_WRITE; |
| 772 | /* If we cannot change the symbol - skip */ |
| 773 | if (!sym_is_changable(sym)) |
| 774 | goto next_menu; |
| 775 | /* If symbol equals to default value - skip */ |
| 776 | if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0) |
| 777 | goto next_menu; |
| 778 | |
| 779 | /* |
| 780 | * If symbol is a choice value and equals to the |
| 781 | * default for a choice - skip. |
Sam Ravnborg | 84062dd | 2010-08-14 23:22:16 +0200 | [diff] [blame] | 782 | * But only if value is bool and equal to "y" and |
| 783 | * choice is not "optional". |
| 784 | * (If choice is "optional" then all values can be "n") |
Sam Ravnborg | 7cf3d73 | 2010-07-31 23:35:34 +0200 | [diff] [blame] | 785 | */ |
| 786 | if (sym_is_choice_value(sym)) { |
| 787 | struct symbol *cs; |
| 788 | struct symbol *ds; |
| 789 | |
| 790 | cs = prop_get_symbol(sym_get_choice_prop(sym)); |
| 791 | ds = sym_choice_default(cs); |
Sam Ravnborg | 84062dd | 2010-08-14 23:22:16 +0200 | [diff] [blame] | 792 | if (!sym_is_optional(cs) && sym == ds) { |
Sam Ravnborg | 801690c | 2010-08-12 09:11:51 +0200 | [diff] [blame] | 793 | if ((sym->type == S_BOOLEAN) && |
| 794 | sym_get_tristate_value(sym) == yes) |
Sam Ravnborg | 7cf3d73 | 2010-07-31 23:35:34 +0200 | [diff] [blame] | 795 | goto next_menu; |
| 796 | } |
| 797 | } |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 798 | conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); |
Sam Ravnborg | 7cf3d73 | 2010-07-31 23:35:34 +0200 | [diff] [blame] | 799 | } |
| 800 | next_menu: |
| 801 | if (menu->list != NULL) { |
| 802 | menu = menu->list; |
| 803 | } |
| 804 | else if (menu->next != NULL) { |
| 805 | menu = menu->next; |
| 806 | } else { |
| 807 | while ((menu = menu->parent)) { |
| 808 | if (menu->next != NULL) { |
| 809 | menu = menu->next; |
| 810 | break; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | fclose(out); |
| 816 | return 0; |
| 817 | } |
| 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | int conf_write(const char *name) |
| 820 | { |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 821 | FILE *out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | struct symbol *sym; |
| 823 | struct menu *menu; |
| 824 | const char *basename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | const char *str; |
Nathan Chancellor | 2ae89c7 | 2018-06-02 09:02:09 -0700 | [diff] [blame] | 826 | char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | char *env; |
| 828 | |
| 829 | dirname[0] = 0; |
| 830 | if (name && name[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | char *slash; |
| 832 | |
Masahiro Yamada | 0608182 | 2018-07-20 16:46:27 +0900 | [diff] [blame] | 833 | if (is_dir(name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | strcpy(dirname, name); |
| 835 | strcat(dirname, "/"); |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 836 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } else if ((slash = strrchr(name, '/'))) { |
| 838 | int size = slash - name + 1; |
| 839 | memcpy(dirname, name, size); |
| 840 | dirname[size] = 0; |
| 841 | if (slash[1]) |
| 842 | basename = slash + 1; |
| 843 | else |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 844 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } else |
| 846 | basename = name; |
| 847 | } else |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 848 | basename = conf_get_configname(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 850 | sprintf(newname, "%s%s", dirname, basename); |
| 851 | env = getenv("KCONFIG_OVERWRITECONFIG"); |
| 852 | if (!env || !*env) { |
| 853 | sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); |
| 854 | out = fopen(tmpname, "w"); |
| 855 | } else { |
| 856 | *tmpname = 0; |
| 857 | out = fopen(newname, "w"); |
| 858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (!out) |
| 860 | return 1; |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 861 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 862 | conf_write_heading(out, &kconfig_printer_cb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Karsten Wiese | b321429 | 2006-12-13 00:34:06 -0800 | [diff] [blame] | 864 | if (!conf_get_changed()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | sym_clear_all_valid(); |
| 866 | |
| 867 | menu = rootmenu.list; |
| 868 | while (menu) { |
| 869 | sym = menu->sym; |
| 870 | if (!sym) { |
| 871 | if (!menu_is_visible(menu)) |
| 872 | goto next; |
| 873 | str = menu_get_prompt(menu); |
| 874 | fprintf(out, "\n" |
| 875 | "#\n" |
| 876 | "# %s\n" |
| 877 | "#\n", str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } else if (!(sym->flags & SYMBOL_CHOICE)) { |
| 879 | sym_calc_value(sym); |
| 880 | if (!(sym->flags & SYMBOL_WRITE)) |
| 881 | goto next; |
| 882 | sym->flags &= ~SYMBOL_WRITE; |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 883 | |
| 884 | conf_write_symbol(out, sym, &kconfig_printer_cb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 887 | next: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (menu->list) { |
| 889 | menu = menu->list; |
| 890 | continue; |
| 891 | } |
| 892 | if (menu->next) |
| 893 | menu = menu->next; |
| 894 | else while ((menu = menu->parent)) { |
| 895 | if (menu->next) { |
| 896 | menu = menu->next; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | fclose(out); |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 902 | |
| 903 | if (*tmpname) { |
Sam Ravnborg | 9a3d0fe | 2006-10-01 11:48:53 +0200 | [diff] [blame] | 904 | strcat(dirname, basename); |
Roman Zippel | 14cdd3c | 2006-06-08 22:12:51 -0700 | [diff] [blame] | 905 | strcat(dirname, ".old"); |
| 906 | rename(newname, dirname); |
| 907 | if (rename(tmpname, newname)) |
| 908 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Sam Ravnborg | 694c49a | 2018-05-22 21:36:12 +0200 | [diff] [blame] | 911 | conf_message("configuration written to %s", newname); |
Roman Zippel | ddc97ca | 2006-06-08 22:12:38 -0700 | [diff] [blame] | 912 | |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 913 | sym_set_change_count(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | return 0; |
| 916 | } |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 917 | |
Masahiro Yamada | a2ff404 | 2018-07-20 16:46:26 +0900 | [diff] [blame] | 918 | /* write a dependency file as used by kbuild to track dependencies */ |
| 919 | static int conf_write_dep(const char *name) |
| 920 | { |
| 921 | struct file *file; |
| 922 | FILE *out; |
| 923 | |
| 924 | if (!name) |
| 925 | name = ".kconfig.d"; |
| 926 | out = fopen("..config.tmp", "w"); |
| 927 | if (!out) |
| 928 | return 1; |
| 929 | fprintf(out, "deps_config := \\\n"); |
| 930 | for (file = file_list; file; file = file->next) { |
| 931 | if (file->next) |
| 932 | fprintf(out, "\t%s \\\n", file->name); |
| 933 | else |
| 934 | fprintf(out, "\t%s\n", file->name); |
| 935 | } |
| 936 | fprintf(out, "\n%s: \\\n" |
| 937 | "\t$(deps_config)\n\n", conf_get_autoconfig_name()); |
| 938 | |
| 939 | env_write_dep(out, conf_get_autoconfig_name()); |
| 940 | |
| 941 | fprintf(out, "\n$(deps_config): ;\n"); |
| 942 | fclose(out); |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 943 | |
| 944 | if (make_parent_dir(name)) |
| 945 | return 1; |
Masahiro Yamada | a2ff404 | 2018-07-20 16:46:26 +0900 | [diff] [blame] | 946 | rename("..config.tmp", name); |
| 947 | return 0; |
| 948 | } |
| 949 | |
Masahiro Yamada | 0849d21 | 2018-11-30 18:15:49 +0900 | [diff] [blame] | 950 | static int conf_touch_deps(void) |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 951 | { |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 952 | const char *name; |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 953 | struct symbol *sym; |
Masahiro Yamada | 1508fec | 2018-11-30 18:15:50 +0900 | [diff] [blame^] | 954 | int res, i; |
| 955 | |
| 956 | strcpy(depfile_path, "include/config/"); |
| 957 | depfile_prefix_len = strlen(depfile_path); |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 958 | |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 959 | name = conf_get_autoconfig_name(); |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 960 | conf_read_simple(name, S_DEF_AUTO); |
Al Viro | 6b87b70 | 2016-01-14 18:13:49 +0000 | [diff] [blame] | 961 | sym_calc_value(modules_sym); |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 962 | |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 963 | for_all_symbols(i, sym) { |
| 964 | sym_calc_value(sym); |
Dirk Gouders | 693359f | 2018-07-03 14:43:31 +0200 | [diff] [blame] | 965 | if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name) |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 966 | continue; |
| 967 | if (sym->flags & SYMBOL_WRITE) { |
| 968 | if (sym->flags & SYMBOL_DEF_AUTO) { |
| 969 | /* |
| 970 | * symbol has old and new value, |
| 971 | * so compare them... |
| 972 | */ |
| 973 | switch (sym->type) { |
| 974 | case S_BOOLEAN: |
| 975 | case S_TRISTATE: |
| 976 | if (sym_get_tristate_value(sym) == |
| 977 | sym->def[S_DEF_AUTO].tri) |
| 978 | continue; |
| 979 | break; |
| 980 | case S_STRING: |
| 981 | case S_HEX: |
| 982 | case S_INT: |
| 983 | if (!strcmp(sym_get_string_value(sym), |
| 984 | sym->def[S_DEF_AUTO].val)) |
| 985 | continue; |
| 986 | break; |
| 987 | default: |
| 988 | break; |
| 989 | } |
| 990 | } else { |
| 991 | /* |
| 992 | * If there is no old value, only 'no' (unset) |
| 993 | * is allowed as new value. |
| 994 | */ |
| 995 | switch (sym->type) { |
| 996 | case S_BOOLEAN: |
| 997 | case S_TRISTATE: |
| 998 | if (sym_get_tristate_value(sym) == no) |
| 999 | continue; |
| 1000 | break; |
| 1001 | default: |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } else if (!(sym->flags & SYMBOL_DEF_AUTO)) |
| 1006 | /* There is neither an old nor a new value. */ |
| 1007 | continue; |
| 1008 | /* else |
| 1009 | * There is an old value, but no new value ('no' (unset) |
| 1010 | * isn't saved in auto.conf, so the old value is always |
| 1011 | * different from 'no'). |
| 1012 | */ |
| 1013 | |
Masahiro Yamada | 1508fec | 2018-11-30 18:15:50 +0900 | [diff] [blame^] | 1014 | res = conf_touch_dep(sym->name); |
| 1015 | if (res) |
| 1016 | return res; |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 1017 | } |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 1018 | |
Masahiro Yamada | 1508fec | 2018-11-30 18:15:50 +0900 | [diff] [blame^] | 1019 | return 0; |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Masahiro Yamada | 00c864f | 2018-07-20 16:46:31 +0900 | [diff] [blame] | 1022 | int conf_write_autoconf(int overwrite) |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1023 | { |
| 1024 | struct symbol *sym; |
Markus Heidelberg | 12122f6 | 2009-05-18 01:36:54 +0200 | [diff] [blame] | 1025 | const char *name; |
Masahiro Yamada | 00c864f | 2018-07-20 16:46:31 +0900 | [diff] [blame] | 1026 | const char *autoconf_name = conf_get_autoconfig_name(); |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1027 | FILE *out, *tristate, *out_h; |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 1028 | int i; |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1029 | |
Masahiro Yamada | 00c864f | 2018-07-20 16:46:31 +0900 | [diff] [blame] | 1030 | if (!overwrite && is_present(autoconf_name)) |
| 1031 | return 0; |
| 1032 | |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 1033 | sym_clear_all_valid(); |
| 1034 | |
Masahiro Yamada | a2ff404 | 2018-07-20 16:46:26 +0900 | [diff] [blame] | 1035 | conf_write_dep("include/config/auto.conf.cmd"); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1036 | |
Masahiro Yamada | 0849d21 | 2018-11-30 18:15:49 +0900 | [diff] [blame] | 1037 | if (conf_touch_deps()) |
Roman Zippel | 2e3646e | 2006-06-08 22:12:42 -0700 | [diff] [blame] | 1038 | return 1; |
| 1039 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1040 | out = fopen(".tmpconfig", "w"); |
| 1041 | if (!out) |
| 1042 | return 1; |
| 1043 | |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1044 | tristate = fopen(".tmpconfig_tristate", "w"); |
| 1045 | if (!tristate) { |
| 1046 | fclose(out); |
| 1047 | return 1; |
| 1048 | } |
| 1049 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1050 | out_h = fopen(".tmpconfig.h", "w"); |
| 1051 | if (!out_h) { |
| 1052 | fclose(out); |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1053 | fclose(tristate); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1054 | return 1; |
| 1055 | } |
| 1056 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 1057 | conf_write_heading(out, &kconfig_printer_cb, NULL); |
| 1058 | |
| 1059 | conf_write_heading(tristate, &tristate_printer_cb, NULL); |
| 1060 | |
| 1061 | conf_write_heading(out_h, &header_printer_cb, NULL); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1062 | |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1063 | for_all_symbols(i, sym) { |
Arnaud Lacombe | 953742c | 2011-08-16 01:20:20 -0400 | [diff] [blame] | 1064 | sym_calc_value(sym); |
Paul Gortmaker | a959613 | 2012-04-12 19:46:33 -0400 | [diff] [blame] | 1065 | if (!(sym->flags & SYMBOL_WRITE) || !sym->name) |
Arnaud Lacombe | 953742c | 2011-08-16 01:20:20 -0400 | [diff] [blame] | 1066 | continue; |
| 1067 | |
Paul Gortmaker | a959613 | 2012-04-12 19:46:33 -0400 | [diff] [blame] | 1068 | /* write symbol to auto.conf, tristate and header files */ |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 1069 | conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); |
Sam Ravnborg | 49192f2 | 2010-07-31 23:35:33 +0200 | [diff] [blame] | 1070 | |
Arnaud Lacombe | e54e692 | 2011-05-15 23:42:09 -0400 | [diff] [blame] | 1071 | conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1); |
| 1072 | |
| 1073 | conf_write_symbol(out_h, sym, &header_printer_cb, NULL); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1074 | } |
| 1075 | fclose(out); |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1076 | fclose(tristate); |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1077 | fclose(out_h); |
| 1078 | |
| 1079 | name = getenv("KCONFIG_AUTOHEADER"); |
| 1080 | if (!name) |
Sam Ravnborg | 264a268 | 2009-10-18 00:49:24 +0200 | [diff] [blame] | 1081 | name = "include/generated/autoconf.h"; |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 1082 | if (make_parent_dir(name)) |
| 1083 | return 1; |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1084 | if (rename(".tmpconfig.h", name)) |
| 1085 | return 1; |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 1086 | |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1087 | name = getenv("KCONFIG_TRISTATE"); |
| 1088 | if (!name) |
| 1089 | name = "include/config/tristate.conf"; |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 1090 | if (make_parent_dir(name)) |
| 1091 | return 1; |
Michal Marek | bc081dd | 2009-12-07 16:38:33 +0100 | [diff] [blame] | 1092 | if (rename(".tmpconfig_tristate", name)) |
| 1093 | return 1; |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 1094 | |
Masahiro Yamada | 00c864f | 2018-07-20 16:46:31 +0900 | [diff] [blame] | 1095 | if (make_parent_dir(autoconf_name)) |
Masahiro Yamada | 79123b1 | 2018-07-20 16:46:29 +0900 | [diff] [blame] | 1096 | return 1; |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1097 | /* |
| 1098 | * This must be the last step, kbuild has a dependency on auto.conf |
| 1099 | * and this marks the successful completion of the previous steps. |
| 1100 | */ |
Masahiro Yamada | 00c864f | 2018-07-20 16:46:31 +0900 | [diff] [blame] | 1101 | if (rename(".tmpconfig", autoconf_name)) |
Roman Zippel | c955cca | 2006-06-08 22:12:39 -0700 | [diff] [blame] | 1102 | return 1; |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
Karsten Wiese | b321429 | 2006-12-13 00:34:06 -0800 | [diff] [blame] | 1106 | |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 1107 | static int sym_change_count; |
Karsten Wiese | 3b354c5 | 2006-12-13 00:34:08 -0800 | [diff] [blame] | 1108 | static void (*conf_changed_callback)(void); |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 1109 | |
| 1110 | void sym_set_change_count(int count) |
| 1111 | { |
Karsten Wiese | 3b354c5 | 2006-12-13 00:34:08 -0800 | [diff] [blame] | 1112 | int _sym_change_count = sym_change_count; |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 1113 | sym_change_count = count; |
Karsten Wiese | 3b354c5 | 2006-12-13 00:34:08 -0800 | [diff] [blame] | 1114 | if (conf_changed_callback && |
| 1115 | (bool)_sym_change_count != (bool)count) |
| 1116 | conf_changed_callback(); |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | void sym_add_change_count(int count) |
| 1120 | { |
Karsten Wiese | 3b354c5 | 2006-12-13 00:34:08 -0800 | [diff] [blame] | 1121 | sym_set_change_count(count + sym_change_count); |
Karsten Wiese | bfc1000 | 2006-12-13 00:34:07 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Karsten Wiese | b321429 | 2006-12-13 00:34:06 -0800 | [diff] [blame] | 1124 | bool conf_get_changed(void) |
| 1125 | { |
| 1126 | return sym_change_count; |
| 1127 | } |
Karsten Wiese | 3b354c5 | 2006-12-13 00:34:08 -0800 | [diff] [blame] | 1128 | |
| 1129 | void conf_set_changed_callback(void (*fn)(void)) |
| 1130 | { |
| 1131 | conf_changed_callback = fn; |
| 1132 | } |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1133 | |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1134 | static bool randomize_choice_values(struct symbol *csym) |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1135 | { |
| 1136 | struct property *prop; |
| 1137 | struct symbol *sym; |
| 1138 | struct expr *e; |
| 1139 | int cnt, def; |
| 1140 | |
| 1141 | /* |
Arnaud Lacombe | 579fb8e | 2010-12-05 01:41:16 -0500 | [diff] [blame] | 1142 | * If choice is mod then we may have more items selected |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1143 | * and if no then no-one. |
| 1144 | * In both cases stop. |
| 1145 | */ |
| 1146 | if (csym->curr.tri != yes) |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1147 | return false; |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1148 | |
| 1149 | prop = sym_get_choice_prop(csym); |
| 1150 | |
| 1151 | /* count entries in choice block */ |
| 1152 | cnt = 0; |
| 1153 | expr_list_for_each_sym(prop->expr, e, sym) |
| 1154 | cnt++; |
| 1155 | |
| 1156 | /* |
| 1157 | * find a random value and set it to yes, |
| 1158 | * set the rest to no so we have only one set |
| 1159 | */ |
| 1160 | def = (rand() % cnt); |
| 1161 | |
| 1162 | cnt = 0; |
| 1163 | expr_list_for_each_sym(prop->expr, e, sym) { |
| 1164 | if (def == cnt++) { |
| 1165 | sym->def[S_DEF_USER].tri = yes; |
| 1166 | csym->def[S_DEF_USER].val = sym; |
| 1167 | } |
| 1168 | else { |
| 1169 | sym->def[S_DEF_USER].tri = no; |
| 1170 | } |
Yann E. MORIN | e6abf12 | 2013-04-28 17:33:15 +0200 | [diff] [blame] | 1171 | sym->flags |= SYMBOL_DEF_USER; |
| 1172 | /* clear VALID to get value calculated */ |
| 1173 | sym->flags &= ~SYMBOL_VALID; |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1174 | } |
| 1175 | csym->flags |= SYMBOL_DEF_USER; |
| 1176 | /* clear VALID to get value calculated */ |
| 1177 | csym->flags &= ~(SYMBOL_VALID); |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1178 | |
| 1179 | return true; |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Arve Hjønnevåg | fbe98bb9 | 2013-06-06 20:37:00 -0700 | [diff] [blame] | 1182 | void set_all_choice_values(struct symbol *csym) |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1183 | { |
| 1184 | struct property *prop; |
| 1185 | struct symbol *sym; |
| 1186 | struct expr *e; |
| 1187 | |
| 1188 | prop = sym_get_choice_prop(csym); |
| 1189 | |
| 1190 | /* |
| 1191 | * Set all non-assinged choice values to no |
| 1192 | */ |
| 1193 | expr_list_for_each_sym(prop->expr, e, sym) { |
| 1194 | if (!sym_has_value(sym)) |
| 1195 | sym->def[S_DEF_USER].tri = no; |
| 1196 | } |
| 1197 | csym->flags |= SYMBOL_DEF_USER; |
| 1198 | /* clear VALID to get value calculated */ |
Arve Hjønnevåg | fbe98bb9 | 2013-06-06 20:37:00 -0700 | [diff] [blame] | 1199 | csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES); |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1200 | } |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1201 | |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1202 | bool conf_set_all_new_symbols(enum conf_def_mode mode) |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1203 | { |
| 1204 | struct symbol *sym, *csym; |
Masahiro Yamada | b92d804 | 2017-12-16 00:38:02 +0900 | [diff] [blame] | 1205 | int i, cnt, pby, pty, ptm; /* pby: probability of bool = y |
Yann E. MORIN | e43956e | 2013-04-13 17:18:36 +0200 | [diff] [blame] | 1206 | * pty: probability of tristate = y |
| 1207 | * ptm: probability of tristate = m |
| 1208 | */ |
| 1209 | |
| 1210 | pby = 50; pty = ptm = 33; /* can't go as the default in switch-case |
| 1211 | * below, otherwise gcc whines about |
| 1212 | * -Wmaybe-uninitialized */ |
| 1213 | if (mode == def_random) { |
| 1214 | int n, p[3]; |
| 1215 | char *env = getenv("KCONFIG_PROBABILITY"); |
| 1216 | n = 0; |
| 1217 | while( env && *env ) { |
| 1218 | char *endp; |
| 1219 | int tmp = strtol( env, &endp, 10 ); |
| 1220 | if( tmp >= 0 && tmp <= 100 ) { |
| 1221 | p[n++] = tmp; |
| 1222 | } else { |
| 1223 | errno = ERANGE; |
| 1224 | perror( "KCONFIG_PROBABILITY" ); |
| 1225 | exit( 1 ); |
| 1226 | } |
| 1227 | env = (*endp == ':') ? endp+1 : endp; |
| 1228 | if( n >=3 ) { |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | switch( n ) { |
| 1233 | case 1: |
| 1234 | pby = p[0]; ptm = pby/2; pty = pby-ptm; |
| 1235 | break; |
| 1236 | case 2: |
| 1237 | pty = p[0]; ptm = p[1]; pby = pty + ptm; |
| 1238 | break; |
| 1239 | case 3: |
| 1240 | pby = p[0]; pty = p[1]; ptm = p[2]; |
| 1241 | break; |
| 1242 | } |
| 1243 | |
| 1244 | if( pty+ptm > 100 ) { |
| 1245 | errno = ERANGE; |
| 1246 | perror( "KCONFIG_PROBABILITY" ); |
| 1247 | exit( 1 ); |
| 1248 | } |
| 1249 | } |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1250 | bool has_changed = false; |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1251 | |
| 1252 | for_all_symbols(i, sym) { |
Yann E. MORIN | cfa98f2 | 2013-04-24 22:00:04 +0200 | [diff] [blame] | 1253 | if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID)) |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1254 | continue; |
| 1255 | switch (sym_get_type(sym)) { |
| 1256 | case S_BOOLEAN: |
| 1257 | case S_TRISTATE: |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1258 | has_changed = true; |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1259 | switch (mode) { |
| 1260 | case def_yes: |
| 1261 | sym->def[S_DEF_USER].tri = yes; |
| 1262 | break; |
| 1263 | case def_mod: |
| 1264 | sym->def[S_DEF_USER].tri = mod; |
| 1265 | break; |
| 1266 | case def_no: |
Josh Triplett | 5d2acfc | 2014-04-07 15:39:09 -0700 | [diff] [blame] | 1267 | if (sym->flags & SYMBOL_ALLNOCONFIG_Y) |
| 1268 | sym->def[S_DEF_USER].tri = yes; |
| 1269 | else |
| 1270 | sym->def[S_DEF_USER].tri = no; |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1271 | break; |
| 1272 | case def_random: |
Yann E. MORIN | e43956e | 2013-04-13 17:18:36 +0200 | [diff] [blame] | 1273 | sym->def[S_DEF_USER].tri = no; |
| 1274 | cnt = rand() % 100; |
| 1275 | if (sym->type == S_TRISTATE) { |
| 1276 | if (cnt < pty) |
| 1277 | sym->def[S_DEF_USER].tri = yes; |
| 1278 | else if (cnt < (pty+ptm)) |
| 1279 | sym->def[S_DEF_USER].tri = mod; |
| 1280 | } else if (cnt < pby) |
| 1281 | sym->def[S_DEF_USER].tri = yes; |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1282 | break; |
| 1283 | default: |
| 1284 | continue; |
| 1285 | } |
Sam Ravnborg | 184832c | 2009-03-15 11:05:12 +0100 | [diff] [blame] | 1286 | if (!(sym_is_choice(sym) && mode == def_random)) |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1287 | sym->flags |= SYMBOL_DEF_USER; |
| 1288 | break; |
| 1289 | default: |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | } |
| 1294 | |
Al Viro | ce97e13 | 2008-10-26 05:12:34 +0000 | [diff] [blame] | 1295 | sym_clear_all_valid(); |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1296 | |
Sam Ravnborg | 184832c | 2009-03-15 11:05:12 +0100 | [diff] [blame] | 1297 | /* |
| 1298 | * We have different type of choice blocks. |
Arnaud Lacombe | 579fb8e | 2010-12-05 01:41:16 -0500 | [diff] [blame] | 1299 | * If curr.tri equals to mod then we can select several |
Sam Ravnborg | 184832c | 2009-03-15 11:05:12 +0100 | [diff] [blame] | 1300 | * choice symbols in one block. |
| 1301 | * In this case we do nothing. |
Arnaud Lacombe | 579fb8e | 2010-12-05 01:41:16 -0500 | [diff] [blame] | 1302 | * If curr.tri equals yes then only one symbol can be |
Sam Ravnborg | 184832c | 2009-03-15 11:05:12 +0100 | [diff] [blame] | 1303 | * selected in a choice block and we set it to yes, |
| 1304 | * and the rest to no. |
| 1305 | */ |
Arve Hjønnevåg | fbe98bb9 | 2013-06-06 20:37:00 -0700 | [diff] [blame] | 1306 | if (mode != def_random) { |
| 1307 | for_all_symbols(i, csym) { |
| 1308 | if ((sym_is_choice(csym) && !sym_has_value(csym)) || |
| 1309 | sym_is_choice_value(csym)) |
| 1310 | csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES; |
| 1311 | } |
| 1312 | } |
| 1313 | |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1314 | for_all_symbols(i, csym) { |
| 1315 | if (sym_has_value(csym) || !sym_is_choice(csym)) |
| 1316 | continue; |
| 1317 | |
| 1318 | sym_calc_value(csym); |
Sam Ravnborg | a64b44e | 2010-08-12 09:11:52 +0200 | [diff] [blame] | 1319 | if (mode == def_random) |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1320 | has_changed = randomize_choice_values(csym); |
| 1321 | else { |
| 1322 | set_all_choice_values(csym); |
| 1323 | has_changed = true; |
| 1324 | } |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1325 | } |
Yann E. MORIN | 3b9a19e | 2013-04-28 22:36:38 +0200 | [diff] [blame] | 1326 | |
| 1327 | return has_changed; |
Roman Zippel | dc7862e | 2008-05-06 04:55:55 +0200 | [diff] [blame] | 1328 | } |