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