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