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