Martin Roth | be08c1d | 2024-02-16 09:12:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 6 | #include <sys/mman.h> |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 7 | #include <sys/stat.h> |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 8 | #include <sys/types.h> |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 11 | #include <fcntl.h> |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 12 | #include <limits.h> |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 13 | #include <stdarg.h> |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 14 | #include <stdbool.h> |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 | #include <unistd.h> |
| 20 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 21 | #include "lkc.h" |
| 22 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [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)) |
| 37 | return false; |
| 38 | |
| 39 | return S_ISDIR(st.st_mode); |
| 40 | } |
| 41 | |
| 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 | |
| 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 | |
| 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 | { |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 133 | int fd; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 134 | |
| 135 | /* check overflow: prefix + name + '\0' must fit in buffer. */ |
| 136 | if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path)) |
| 137 | return -1; |
| 138 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 139 | strcpy(depfile_path + depfile_prefix_len, name); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 140 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 141 | fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 142 | if (fd == -1) |
| 143 | return -1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 144 | close(fd); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 149 | static void conf_warning(const char *fmt, ...) |
| 150 | __attribute__ ((format (printf, 1, 2))); |
| 151 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 152 | static void conf_message(const char *fmt, ...) |
| 153 | __attribute__ ((format (printf, 1, 2))); |
| 154 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 155 | static const char *conf_filename; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 156 | static int conf_lineno, conf_warnings; |
zbao | 2f3fd26 | 2015-09-26 06:20:53 -0400 | [diff] [blame] | 157 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 158 | bool conf_errors(void) |
| 159 | { |
| 160 | if (conf_warnings) |
| 161 | return getenv("KCONFIG_WERROR"); |
| 162 | return false; |
| 163 | } |
| 164 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 165 | #ifdef __MINGW32__ |
| 166 | #define mkdir(_n,_p) mkdir((_n)) |
| 167 | #endif |
| 168 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 169 | static void conf_warning(const char *fmt, ...) |
| 170 | { |
| 171 | va_list ap; |
| 172 | va_start(ap, fmt); |
| 173 | fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno); |
| 174 | vfprintf(stderr, fmt, ap); |
| 175 | fprintf(stderr, "\n"); |
| 176 | va_end(ap); |
| 177 | conf_warnings++; |
| 178 | } |
| 179 | |
Martin Roth | 00cbc7f | 2016-09-21 14:27:26 -0600 | [diff] [blame] | 180 | static void conf_notice(const char *fmt, ...) |
| 181 | { |
| 182 | va_list ap; |
| 183 | va_start(ap, fmt); |
| 184 | fprintf(stderr, "%s:%d:notice: ", conf_filename, conf_lineno); |
| 185 | vfprintf(stderr, fmt, ap); |
| 186 | fprintf(stderr, "\n"); |
| 187 | va_end(ap); |
| 188 | } |
| 189 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 190 | static void conf_default_message_callback(const char *s) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 191 | { |
| 192 | printf("#\n# "); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 193 | printf("%s", s); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 194 | printf("\n#\n"); |
| 195 | } |
| 196 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 197 | static void (*conf_message_callback)(const char *s) = |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 198 | conf_default_message_callback; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 199 | void conf_set_message_callback(void (*fn)(const char *s)) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 200 | { |
| 201 | conf_message_callback = fn; |
| 202 | } |
| 203 | |
| 204 | static void conf_message(const char *fmt, ...) |
| 205 | { |
| 206 | va_list ap; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 207 | char buf[4096]; |
| 208 | |
| 209 | if (!conf_message_callback) |
| 210 | return; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 211 | |
| 212 | va_start(ap, fmt); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 213 | |
| 214 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 215 | conf_message_callback(buf); |
Stefan Reinauer | cce6662 | 2015-04-06 01:14:21 +0200 | [diff] [blame] | 216 | va_end(ap); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 219 | const char *conf_get_configname(void) |
| 220 | { |
| 221 | char *name = getenv("KCONFIG_CONFIG"); |
| 222 | |
| 223 | return name ? name : ".config"; |
| 224 | } |
| 225 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 226 | static const char *conf_get_autoconfig_name(void) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 227 | { |
| 228 | char *name = getenv("KCONFIG_AUTOCONFIG"); |
| 229 | |
| 230 | return name ? name : "include/config/auto.conf"; |
| 231 | } |
| 232 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 233 | static const char *conf_get_autoheader_name(void) |
| 234 | { |
| 235 | char *name = getenv("KCONFIG_AUTOHEADER"); |
| 236 | |
| 237 | return name ? name : "include/generated/autoconf.h"; |
| 238 | } |
| 239 | |
Patrick Georgi | 7f93aa4 | 2023-11-20 17:58:40 +0100 | [diff] [blame] | 240 | static const char *conf_get_rustccfg_name(void) |
| 241 | { |
| 242 | char *name = getenv("KCONFIG_RUSTCCFG"); |
| 243 | |
| 244 | return name ? name : "include/generated/rustc_cfg"; |
| 245 | } |
| 246 | |
Martin Roth | 35e09ec | 2022-11-13 15:20:57 -0700 | [diff] [blame] | 247 | static const char *conf_get_autobase_name(void) |
| 248 | { |
| 249 | char *name = getenv("KCONFIG_SPLITCONFIG"); |
| 250 | |
| 251 | return name ? name : "include/config/"; |
| 252 | } |
| 253 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 254 | static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) |
| 255 | { |
| 256 | char *p2; |
| 257 | |
| 258 | switch (sym->type) { |
| 259 | case S_TRISTATE: |
| 260 | if (p[0] == 'm') { |
| 261 | sym->def[def].tri = mod; |
| 262 | sym->flags |= def_flags; |
| 263 | break; |
| 264 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 265 | /* fall through */ |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 266 | case S_BOOLEAN: |
| 267 | if (p[0] == 'y') { |
| 268 | sym->def[def].tri = yes; |
| 269 | sym->flags |= def_flags; |
| 270 | break; |
| 271 | } |
| 272 | if (p[0] == 'n') { |
| 273 | sym->def[def].tri = no; |
| 274 | sym->flags |= def_flags; |
| 275 | break; |
| 276 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 277 | if (def != S_DEF_AUTO) |
| 278 | conf_warning("symbol value '%s' invalid for %s", |
| 279 | p, sym->name); |
| 280 | return 1; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 281 | case S_STRING: |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 282 | /* No escaping for S_DEF_AUTO (include/config/auto.conf) */ |
| 283 | if (def != S_DEF_AUTO) { |
| 284 | if (*p++ != '"') |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 285 | break; |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 286 | for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) { |
| 287 | if (*p2 == '"') { |
| 288 | *p2 = 0; |
| 289 | break; |
| 290 | } |
| 291 | memmove(p2, p2 + 1, strlen(p2)); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 292 | } |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 293 | if (!p2) { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 294 | conf_warning("invalid string found"); |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 295 | return 1; |
| 296 | } |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 297 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 298 | /* fall through */ |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 299 | case S_INT: |
| 300 | case S_HEX: |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 301 | if (sym_string_valid(sym, p)) { |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 302 | sym->def[def].val = xstrdup(p); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 303 | sym->flags |= def_flags; |
| 304 | } else { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 305 | if (def != S_DEF_AUTO) |
| 306 | conf_warning("symbol value '%s' invalid for %s", |
| 307 | p, sym->name); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 308 | return 1; |
| 309 | } |
| 310 | break; |
| 311 | default: |
| 312 | ; |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 317 | #define LINE_GROWTH 16 |
| 318 | static int add_byte(int c, char **lineptr, size_t slen, size_t *n) |
| 319 | { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 320 | size_t new_size = slen + 1; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 321 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 322 | if (new_size > *n) { |
| 323 | new_size += LINE_GROWTH - 1; |
| 324 | new_size *= 2; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 325 | *lineptr = xrealloc(*lineptr, new_size); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 326 | *n = new_size; |
| 327 | } |
| 328 | |
| 329 | (*lineptr)[slen] = c; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream) |
| 335 | { |
| 336 | char *line = *lineptr; |
| 337 | size_t slen = 0; |
| 338 | |
| 339 | for (;;) { |
| 340 | int c = getc(stream); |
| 341 | |
| 342 | switch (c) { |
| 343 | case '\n': |
| 344 | if (add_byte(c, &line, slen, n) < 0) |
| 345 | goto e_out; |
| 346 | slen++; |
| 347 | /* fall through */ |
| 348 | case EOF: |
| 349 | if (add_byte('\0', &line, slen, n) < 0) |
| 350 | goto e_out; |
| 351 | *lineptr = line; |
| 352 | if (slen == 0) |
| 353 | return -1; |
| 354 | return slen; |
| 355 | default: |
| 356 | if (add_byte(c, &line, slen, n) < 0) |
| 357 | goto e_out; |
| 358 | slen++; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | e_out: |
| 363 | line[slen-1] = '\0'; |
| 364 | *lineptr = line; |
| 365 | return -1; |
| 366 | } |
| 367 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 368 | /* like getline(), but the newline character is stripped away */ |
| 369 | static ssize_t getline_stripped(char **lineptr, size_t *n, FILE *stream) |
| 370 | { |
| 371 | ssize_t len; |
| 372 | |
| 373 | len = compat_getline(lineptr, n, stream); |
| 374 | |
| 375 | if (len > 0 && (*lineptr)[len - 1] == '\n') { |
| 376 | len--; |
| 377 | (*lineptr)[len] = '\0'; |
| 378 | |
| 379 | if (len > 0 && (*lineptr)[len - 1] == '\r') { |
| 380 | len--; |
| 381 | (*lineptr)[len] = '\0'; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return len; |
| 386 | } |
| 387 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 388 | int conf_read_simple(const char *name, int def) |
| 389 | { |
| 390 | FILE *in = NULL; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 391 | char *line = NULL; |
| 392 | size_t line_asize = 0; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 393 | char *p, *val; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 394 | struct symbol *sym; |
| 395 | int i, def_flags; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 396 | const char *warn_unknown, *sym_name; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 397 | |
Patrick Georgi | 0eab62b | 2023-11-20 19:49:29 +0100 | [diff] [blame] | 398 | warn_unknown = getenv("KCONFIG_WARN_UNKNOWN_SYMBOLS"); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 399 | if (name) { |
| 400 | in = zconf_fopen(name); |
| 401 | } else { |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 402 | char *env; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 403 | |
| 404 | name = conf_get_configname(); |
| 405 | in = zconf_fopen(name); |
| 406 | if (in) |
| 407 | goto load; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 408 | conf_set_changed(true); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 409 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 410 | env = getenv("KCONFIG_DEFCONFIG_LIST"); |
| 411 | if (!env) |
| 412 | return 1; |
| 413 | |
| 414 | while (1) { |
| 415 | bool is_last; |
| 416 | |
| 417 | while (isspace(*env)) |
| 418 | env++; |
| 419 | |
| 420 | if (!*env) |
| 421 | break; |
| 422 | |
| 423 | p = env; |
| 424 | while (*p && !isspace(*p)) |
| 425 | p++; |
| 426 | |
| 427 | is_last = (*p == '\0'); |
| 428 | |
| 429 | *p = '\0'; |
| 430 | |
| 431 | in = zconf_fopen(env); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 432 | if (in) { |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 433 | conf_message("using defaults found in %s", |
| 434 | env); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 435 | goto load; |
| 436 | } |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 437 | |
| 438 | if (is_last) |
| 439 | break; |
| 440 | |
| 441 | env = p + 1; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | if (!in) |
| 445 | return 1; |
| 446 | |
| 447 | load: |
| 448 | conf_filename = name; |
| 449 | conf_lineno = 0; |
| 450 | conf_warnings = 0; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 451 | |
| 452 | def_flags = SYMBOL_DEF << def; |
| 453 | for_all_symbols(i, sym) { |
| 454 | sym->flags |= SYMBOL_CHANGED; |
| 455 | sym->flags &= ~(def_flags|SYMBOL_VALID); |
| 456 | if (sym_is_choice(sym)) |
| 457 | sym->flags |= def_flags; |
| 458 | switch (sym->type) { |
| 459 | case S_INT: |
| 460 | case S_HEX: |
| 461 | case S_STRING: |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 462 | free(sym->def[def].val); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 463 | /* fall through */ |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 464 | default: |
| 465 | sym->def[def].val = NULL; |
| 466 | sym->def[def].tri = no; |
| 467 | } |
| 468 | } |
| 469 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 470 | while (getline_stripped(&line, &line_asize, in) != -1) { |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 471 | conf_lineno++; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 472 | |
| 473 | if (!line[0]) /* blank line */ |
| 474 | continue; |
| 475 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 476 | if (line[0] == '#') { |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 477 | if (line[1] != ' ') |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 478 | continue; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 479 | p = line + 2; |
| 480 | if (memcmp(p, CONFIG_, strlen(CONFIG_))) |
| 481 | continue; |
| 482 | sym_name = p + strlen(CONFIG_); |
| 483 | p = strchr(sym_name, ' '); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 484 | if (!p) |
| 485 | continue; |
| 486 | *p++ = 0; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 487 | if (strcmp(p, "is not set")) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 488 | continue; |
Patrick Georgi | 0eab62b | 2023-11-20 19:49:29 +0100 | [diff] [blame] | 489 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 490 | val = "n"; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 491 | } else { |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 492 | if (memcmp(line, CONFIG_, strlen(CONFIG_))) { |
| 493 | conf_warning("unexpected data: %s", line); |
| 494 | continue; |
| 495 | } |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 496 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 497 | sym_name = line + strlen(CONFIG_); |
| 498 | p = strchr(sym_name, '='); |
| 499 | if (!p) { |
| 500 | conf_warning("unexpected data: %s", line); |
| 501 | continue; |
| 502 | } |
| 503 | *p = 0; |
| 504 | val = p + 1; |
| 505 | } |
| 506 | |
| 507 | sym = sym_find(sym_name); |
| 508 | if (!sym) { |
| 509 | if (def == S_DEF_AUTO) { |
| 510 | /* |
| 511 | * Reading from include/config/auto.conf. |
| 512 | * If CONFIG_FOO previously existed in auto.conf |
| 513 | * but it is missing now, include/config/FOO |
| 514 | * must be touched. |
| 515 | */ |
| 516 | conf_touch_dep(sym_name); |
| 517 | } else { |
| 518 | if (warn_unknown) |
| 519 | conf_warning("unknown symbol: %s", sym_name); |
| 520 | |
| 521 | conf_set_changed(true); |
| 522 | } |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 523 | continue; |
| 524 | } |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 525 | |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 526 | if (sym->flags & def_flags) |
| 527 | conf_notice("override: reassigning to symbol %s", sym->name); |
| 528 | |
| 529 | if (conf_set_sym_val(sym, def, def_flags, val)) |
| 530 | continue; |
| 531 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 532 | if (sym && sym_is_choice_value(sym)) { |
| 533 | struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); |
| 534 | switch (sym->def[def].tri) { |
| 535 | case no: |
| 536 | break; |
| 537 | case mod: |
| 538 | if (cs->def[def].tri == yes) { |
| 539 | conf_warning("%s creates inconsistent choice state", sym->name); |
| 540 | cs->flags &= ~def_flags; |
| 541 | } |
| 542 | break; |
| 543 | case yes: |
| 544 | if (cs->def[def].tri != no) |
Martin Roth | 00cbc7f | 2016-09-21 14:27:26 -0600 | [diff] [blame] | 545 | conf_notice("override: %s changes choice state", sym->name); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 546 | cs->def[def].val = sym; |
| 547 | break; |
| 548 | } |
| 549 | cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri); |
| 550 | } |
| 551 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 552 | free(line); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 553 | fclose(in); |
| 554 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | int conf_read(const char *name) |
| 559 | { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 560 | struct symbol *sym; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 561 | int conf_unsaved = 0; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 562 | int i; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 563 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 564 | conf_set_changed(false); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 565 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 566 | if (conf_read_simple(name, S_DEF_USER)) { |
| 567 | sym_calc_value(modules_sym); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 568 | return 1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | sym_calc_value(modules_sym); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 572 | |
| 573 | for_all_symbols(i, sym) { |
| 574 | sym_calc_value(sym); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 575 | if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE)) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 576 | continue; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 577 | if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) { |
| 578 | /* check that calculated value agrees with saved value */ |
| 579 | switch (sym->type) { |
| 580 | case S_BOOLEAN: |
| 581 | case S_TRISTATE: |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 582 | if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym)) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 583 | continue; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 584 | break; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 585 | default: |
| 586 | if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val)) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 587 | continue; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE)) |
| 591 | /* no previous value and not saved */ |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 592 | continue; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 593 | conf_unsaved++; |
| 594 | /* maybe print value in verbose mode... */ |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | for_all_symbols(i, sym) { |
| 598 | if (sym_has_value(sym) && !sym_is_choice_value(sym)) { |
| 599 | /* Reset values of generates values, so they'll appear |
| 600 | * as new, if they should become visible, but that |
| 601 | * doesn't quite work if the Kconfig and the saved |
| 602 | * configuration disagree. |
| 603 | */ |
| 604 | if (sym->visible == no && !conf_unsaved) |
| 605 | sym->flags &= ~SYMBOL_DEF_USER; |
| 606 | switch (sym->type) { |
| 607 | case S_STRING: |
| 608 | case S_INT: |
| 609 | case S_HEX: |
| 610 | /* Reset a string value if it's out of range */ |
| 611 | if (sym_string_within_range(sym, sym->def[S_DEF_USER].val)) |
| 612 | break; |
Patrick Georgi | 7761237 | 2024-03-13 13:01:21 +0100 | [diff] [blame] | 613 | sym->flags &= ~SYMBOL_VALID; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 614 | conf_unsaved++; |
| 615 | break; |
| 616 | default: |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 622 | if (conf_warnings || conf_unsaved) |
| 623 | conf_set_changed(true); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 628 | struct comment_style { |
| 629 | const char *decoration; |
| 630 | const char *prefix; |
| 631 | const char *postfix; |
| 632 | }; |
| 633 | |
| 634 | static const struct comment_style comment_style_pound = { |
| 635 | .decoration = "#", |
| 636 | .prefix = "#", |
| 637 | .postfix = "#", |
| 638 | }; |
| 639 | |
| 640 | static const struct comment_style comment_style_c = { |
| 641 | .decoration = " *", |
| 642 | .prefix = "/*", |
| 643 | .postfix = " */", |
| 644 | }; |
| 645 | |
| 646 | static void conf_write_heading(FILE *fp, const struct comment_style *cs) |
| 647 | { |
Patrick Georgi | 7f93aa4 | 2023-11-20 17:58:40 +0100 | [diff] [blame] | 648 | if (!cs) |
| 649 | return; |
| 650 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 651 | fprintf(fp, "%s\n", cs->prefix); |
| 652 | |
| 653 | fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n", |
| 654 | cs->decoration); |
| 655 | |
| 656 | fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text); |
| 657 | |
| 658 | fprintf(fp, "%s\n", cs->postfix); |
| 659 | } |
| 660 | |
| 661 | /* The returned pointer must be freed on the caller side */ |
| 662 | static char *escape_string_value(const char *in) |
| 663 | { |
| 664 | const char *p; |
| 665 | char *out; |
| 666 | size_t len; |
| 667 | |
| 668 | len = strlen(in) + strlen("\"\"") + 1; |
| 669 | |
| 670 | p = in; |
| 671 | while (1) { |
| 672 | p += strcspn(p, "\"\\"); |
| 673 | |
| 674 | if (p[0] == '\0') |
| 675 | break; |
| 676 | |
| 677 | len++; |
| 678 | p++; |
| 679 | } |
| 680 | |
| 681 | out = xmalloc(len); |
| 682 | out[0] = '\0'; |
| 683 | |
| 684 | strcat(out, "\""); |
| 685 | |
| 686 | p = in; |
| 687 | while (1) { |
| 688 | len = strcspn(p, "\"\\"); |
| 689 | strncat(out, p, len); |
| 690 | p += len; |
| 691 | |
| 692 | if (p[0] == '\0') |
| 693 | break; |
| 694 | |
| 695 | strcat(out, "\\"); |
| 696 | strncat(out, p++, 1); |
| 697 | } |
| 698 | |
| 699 | strcat(out, "\""); |
| 700 | |
| 701 | return out; |
| 702 | } |
| 703 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 704 | enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE }; |
| 705 | |
| 706 | static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, |
| 707 | bool escape_string) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 708 | { |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 709 | const char *val; |
| 710 | char *escaped = NULL; |
| 711 | |
| 712 | if (sym->type == S_UNKNOWN) |
| 713 | return; |
| 714 | |
| 715 | val = sym_get_string_value(sym); |
| 716 | |
| 717 | if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) && |
| 718 | output_n != OUTPUT_N && *val == 'n') { |
| 719 | if (output_n == OUTPUT_N_AS_UNSET) |
| 720 | fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name); |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | if (sym->type == S_STRING && escape_string) { |
| 725 | escaped = escape_string_value(val); |
| 726 | val = escaped; |
| 727 | } |
| 728 | |
| 729 | fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); |
| 730 | |
| 731 | free(escaped); |
| 732 | } |
| 733 | |
| 734 | static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym) |
| 735 | { |
| 736 | __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true); |
| 737 | } |
| 738 | |
| 739 | static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym) |
| 740 | { |
| 741 | int print_negatives = getenv("KCONFIG_NEGATIVES") != NULL; |
| 742 | enum output_n out = OUTPUT_N_NONE; |
| 743 | if (print_negatives) { |
| 744 | out = OUTPUT_N; |
| 745 | } |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 746 | __print_symbol(fp, sym, out, false); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void print_symbol_for_listconfig(struct symbol *sym) |
| 750 | { |
| 751 | __print_symbol(stdout, sym, OUTPUT_N, true); |
| 752 | } |
| 753 | |
| 754 | static void print_symbol_for_c(FILE *fp, struct symbol *sym) |
| 755 | { |
| 756 | const char *val; |
| 757 | const char *sym_suffix = ""; |
| 758 | const char *val_prefix = ""; |
| 759 | char *escaped = NULL; |
| 760 | |
| 761 | if (sym->type == S_UNKNOWN) |
| 762 | return; |
| 763 | |
| 764 | val = sym_get_string_value(sym); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 765 | |
| 766 | switch (sym->type) { |
| 767 | case S_BOOLEAN: |
| 768 | case S_TRISTATE: |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 769 | switch (*val) { |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 770 | case 'n': |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 771 | if (getenv("KCONFIG_NEGATIVES") != NULL) { |
| 772 | val = "0"; |
| 773 | break; |
| 774 | } |
| 775 | return; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 776 | case 'm': |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 777 | sym_suffix = "_MODULE"; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 778 | /* fall through */ |
| 779 | default: |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 780 | val = "1"; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 781 | } |
| 782 | break; |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 783 | case S_HEX: |
| 784 | if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X')) |
| 785 | val_prefix = "0x"; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 786 | /* fall through */ |
| 787 | case S_INT: |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 788 | if (val[0] == '\0') { |
| 789 | val = "0"; |
| 790 | val_prefix = ""; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 791 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 792 | break; |
| 793 | case S_STRING: |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 794 | escaped = escape_string_value(val); |
| 795 | val = escaped; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 796 | default: |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 797 | break; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 798 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 799 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 800 | fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix, |
| 801 | val_prefix, val); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 802 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 803 | free(escaped); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Patrick Georgi | 7f93aa4 | 2023-11-20 17:58:40 +0100 | [diff] [blame] | 806 | static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym) |
| 807 | { |
| 808 | const char *val; |
| 809 | const char *val_prefix = ""; |
| 810 | char *val_prefixed = NULL; |
| 811 | size_t val_prefixed_len; |
| 812 | char *escaped = NULL; |
| 813 | |
| 814 | if (sym->type == S_UNKNOWN) |
| 815 | return; |
| 816 | |
| 817 | val = sym_get_string_value(sym); |
| 818 | |
| 819 | switch (sym->type) { |
| 820 | case S_BOOLEAN: |
| 821 | case S_TRISTATE: |
| 822 | /* |
| 823 | * We do not care about disabled ones, i.e. no need for |
| 824 | * what otherwise are "comments" in other printers. |
| 825 | */ |
| 826 | if (*val == 'n') |
| 827 | return; |
| 828 | |
| 829 | /* |
| 830 | * To have similar functionality to the C macro `IS_ENABLED()` |
| 831 | * we provide an empty `--cfg CONFIG_X` here in both `y` |
| 832 | * and `m` cases. |
| 833 | * |
| 834 | * Then, the common `fprintf()` below will also give us |
| 835 | * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can |
| 836 | * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`. |
| 837 | */ |
| 838 | fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name); |
| 839 | break; |
| 840 | case S_HEX: |
| 841 | if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X')) |
| 842 | val_prefix = "0x"; |
| 843 | break; |
| 844 | default: |
| 845 | break; |
| 846 | } |
| 847 | |
| 848 | if (strlen(val_prefix) > 0) { |
| 849 | val_prefixed_len = strlen(val) + strlen(val_prefix) + 1; |
| 850 | val_prefixed = xmalloc(val_prefixed_len); |
| 851 | snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val); |
| 852 | val = val_prefixed; |
| 853 | } |
| 854 | |
| 855 | /* All values get escaped: the `--cfg` option only takes strings */ |
| 856 | escaped = escape_string_value(val); |
| 857 | val = escaped; |
| 858 | |
| 859 | fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val); |
| 860 | |
| 861 | free(escaped); |
| 862 | free(val_prefixed); |
| 863 | } |
| 864 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 865 | /* |
| 866 | * Write out a minimal config. |
| 867 | * All values that has default values are skipped as this is redundant. |
| 868 | */ |
| 869 | int conf_write_defconfig(const char *filename) |
| 870 | { |
| 871 | struct symbol *sym; |
| 872 | struct menu *menu; |
| 873 | FILE *out; |
| 874 | |
| 875 | out = fopen(filename, "w"); |
| 876 | if (!out) |
| 877 | return 1; |
| 878 | |
| 879 | sym_clear_all_valid(); |
| 880 | |
| 881 | /* Traverse all menus to find all relevant symbols */ |
| 882 | menu = rootmenu.list; |
| 883 | |
| 884 | while (menu != NULL) |
| 885 | { |
| 886 | sym = menu->sym; |
| 887 | if (sym == NULL) { |
| 888 | if (!menu_is_visible(menu)) |
| 889 | goto next_menu; |
| 890 | } else if (!sym_is_choice(sym)) { |
| 891 | sym_calc_value(sym); |
| 892 | if (!(sym->flags & SYMBOL_WRITE)) |
| 893 | goto next_menu; |
| 894 | sym->flags &= ~SYMBOL_WRITE; |
| 895 | /* If we cannot change the symbol - skip */ |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 896 | if (!sym_is_changeable(sym)) |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 897 | goto next_menu; |
| 898 | /* If symbol equals to default value - skip */ |
| 899 | if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0) |
| 900 | goto next_menu; |
| 901 | |
| 902 | /* |
| 903 | * If symbol is a choice value and equals to the |
| 904 | * default for a choice - skip. |
| 905 | * But only if value is bool and equal to "y" and |
| 906 | * choice is not "optional". |
| 907 | * (If choice is "optional" then all values can be "n") |
| 908 | */ |
| 909 | if (sym_is_choice_value(sym)) { |
| 910 | struct symbol *cs; |
| 911 | struct symbol *ds; |
| 912 | |
| 913 | cs = prop_get_symbol(sym_get_choice_prop(sym)); |
| 914 | ds = sym_choice_default(cs); |
| 915 | if (!sym_is_optional(cs) && sym == ds) { |
| 916 | if ((sym->type == S_BOOLEAN) && |
| 917 | sym_get_tristate_value(sym) == yes) |
| 918 | goto next_menu; |
| 919 | } |
| 920 | } |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 921 | print_symbol_for_dotconfig(out, sym); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 922 | } |
| 923 | next_menu: |
| 924 | if (menu->list != NULL) { |
| 925 | menu = menu->list; |
| 926 | } |
| 927 | else if (menu->next != NULL) { |
| 928 | menu = menu->next; |
| 929 | } else { |
| 930 | while ((menu = menu->parent)) { |
| 931 | if (menu->next != NULL) { |
| 932 | menu = menu->next; |
| 933 | break; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | fclose(out); |
| 939 | return 0; |
| 940 | } |
| 941 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 942 | int conf_write(const char *name) |
| 943 | { |
| 944 | FILE *out; |
| 945 | struct symbol *sym; |
| 946 | struct menu *menu; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 947 | const char *str; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 948 | char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1]; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 949 | char *env; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 950 | int i; |
| 951 | bool need_newline = false; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 952 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 953 | if (!name) |
Raul E Rangel | d2f90a0 | 2019-07-25 16:00:50 -0600 | [diff] [blame] | 954 | name = conf_get_configname(); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 955 | |
| 956 | if (!*name) { |
| 957 | fprintf(stderr, "config name is empty\n"); |
| 958 | return -1; |
Raul E Rangel | d2f90a0 | 2019-07-25 16:00:50 -0600 | [diff] [blame] | 959 | } |
| 960 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 961 | if (is_dir(name)) { |
| 962 | fprintf(stderr, "%s: Is a directory\n", name); |
| 963 | return -1; |
Raul E Rangel | d2f90a0 | 2019-07-25 16:00:50 -0600 | [diff] [blame] | 964 | } |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 965 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 966 | if (make_parent_dir(name)) |
| 967 | return -1; |
| 968 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 969 | env = getenv("KCONFIG_OVERWRITECONFIG"); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 970 | if (env && *env) { |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 971 | *tmpname = 0; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 972 | out = fopen(name, "w"); |
| 973 | } else { |
| 974 | snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp", |
| 975 | name, (int)getpid()); |
| 976 | out = fopen(tmpname, "w"); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 977 | } |
| 978 | if (!out) |
| 979 | return 1; |
| 980 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 981 | conf_write_heading(out, &comment_style_pound); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 982 | |
| 983 | if (!conf_get_changed()) |
| 984 | sym_clear_all_valid(); |
| 985 | |
| 986 | menu = rootmenu.list; |
| 987 | while (menu) { |
| 988 | sym = menu->sym; |
| 989 | if (!sym) { |
| 990 | if (!menu_is_visible(menu)) |
| 991 | goto next; |
| 992 | str = menu_get_prompt(menu); |
| 993 | fprintf(out, "\n" |
| 994 | "#\n" |
| 995 | "# %s\n" |
| 996 | "#\n", str); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 997 | need_newline = false; |
| 998 | } else if (!(sym->flags & SYMBOL_CHOICE) && |
| 999 | !(sym->flags & SYMBOL_WRITTEN)) { |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1000 | sym_calc_value(sym); |
| 1001 | if (!(sym->flags & SYMBOL_WRITE)) |
| 1002 | goto next; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1003 | if (need_newline) { |
| 1004 | fprintf(out, "\n"); |
| 1005 | need_newline = false; |
| 1006 | } |
| 1007 | sym->flags |= SYMBOL_WRITTEN; |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1008 | print_symbol_for_dotconfig(out, sym); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1011 | next: |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1012 | if (menu->list) { |
| 1013 | menu = menu->list; |
| 1014 | continue; |
| 1015 | } |
Patrick Georgi | 5526be2 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1016 | |
| 1017 | end_check: |
| 1018 | if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu && |
| 1019 | menu->prompt->type == P_MENU) { |
| 1020 | fprintf(out, "# end of %s\n", menu_get_prompt(menu)); |
| 1021 | need_newline = true; |
| 1022 | } |
| 1023 | |
| 1024 | if (menu->next) { |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1025 | menu = menu->next; |
Patrick Georgi | 5526be2 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1026 | } else { |
| 1027 | menu = menu->parent; |
| 1028 | if (menu) |
| 1029 | goto end_check; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | fclose(out); |
| 1033 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1034 | for_all_symbols(i, sym) |
| 1035 | sym->flags &= ~SYMBOL_WRITTEN; |
| 1036 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1037 | if (*tmpname) { |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1038 | if (is_same(name, tmpname)) { |
| 1039 | conf_message("No change to %s", name); |
| 1040 | unlink(tmpname); |
| 1041 | conf_set_changed(false); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | snprintf(oldname, sizeof(oldname), "%s.old", name); |
| 1046 | rename(name, oldname); |
| 1047 | if (rename(tmpname, name)) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1048 | return 1; |
| 1049 | } |
| 1050 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1051 | conf_message("configuration written to %s", name); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1052 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1053 | conf_set_changed(false); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1054 | |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1058 | /* write a dependency file as used by kbuild to track dependencies */ |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1059 | static int conf_write_autoconf_cmd(const char *autoconf_name) |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1060 | { |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1061 | char name[PATH_MAX], tmp[PATH_MAX]; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1062 | struct file *file; |
| 1063 | FILE *out; |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1064 | int ret; |
| 1065 | |
| 1066 | ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name); |
| 1067 | if (ret >= sizeof(name)) /* check truncation */ |
| 1068 | return -1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1069 | |
| 1070 | if (make_parent_dir(name)) |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1071 | return -1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1072 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1073 | ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name); |
| 1074 | if (ret >= sizeof(tmp)) /* check truncation */ |
| 1075 | return -1; |
| 1076 | |
| 1077 | out = fopen(tmp, "w"); |
| 1078 | if (!out) { |
| 1079 | perror("fopen"); |
| 1080 | return -1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1081 | } |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1082 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1083 | fprintf(out, "deps_config := \\\n"); |
| 1084 | for (file = file_list; file; file = file->next) |
| 1085 | fprintf(out, "\t%s \\\n", file->name); |
| 1086 | |
| 1087 | fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name); |
| 1088 | |
| 1089 | env_write_dep(out, autoconf_name); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1090 | |
| 1091 | fprintf(out, "\n$(deps_config): ;\n"); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1092 | |
Patrick Georgi | 5526be2 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1093 | fflush(out); |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1094 | ret = ferror(out); /* error check for all fprintf() calls */ |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1095 | fclose(out); |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1096 | if (ret) |
| 1097 | return -1; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1098 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1099 | if (rename(tmp, name)) { |
| 1100 | perror("rename"); |
| 1101 | return -1; |
| 1102 | } |
| 1103 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | static int conf_touch_deps(void) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1108 | { |
Martin Roth | 35e09ec | 2022-11-13 15:20:57 -0700 | [diff] [blame] | 1109 | const char *name; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1110 | struct symbol *sym; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1111 | int res, i; |
| 1112 | |
Nicholas Chin | 8ef2f7c | 2022-11-23 10:18:32 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * Upstream Kconfig sets depfile_path based on the directory |
| 1115 | * prefix of the autoconfig path, but coreboot overrides this |
| 1116 | * using the KCONFIG_SPLITCONFIG environment variable |
| 1117 | */ |
Martin Roth | 35e09ec | 2022-11-13 15:20:57 -0700 | [diff] [blame] | 1118 | strcpy(depfile_path, conf_get_autobase_name()); |
| 1119 | depfile_prefix_len = strlen(depfile_path); |
| 1120 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1121 | name = conf_get_autoconfig_name(); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1122 | conf_read_simple(name, S_DEF_AUTO); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1123 | sym_calc_value(modules_sym); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1124 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1125 | for_all_symbols(i, sym) { |
| 1126 | sym_calc_value(sym); |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1127 | if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1128 | continue; |
| 1129 | if (sym->flags & SYMBOL_WRITE) { |
| 1130 | if (sym->flags & SYMBOL_DEF_AUTO) { |
| 1131 | /* |
| 1132 | * symbol has old and new value, |
| 1133 | * so compare them... |
| 1134 | */ |
| 1135 | switch (sym->type) { |
| 1136 | case S_BOOLEAN: |
| 1137 | case S_TRISTATE: |
| 1138 | if (sym_get_tristate_value(sym) == |
| 1139 | sym->def[S_DEF_AUTO].tri) |
| 1140 | continue; |
| 1141 | break; |
| 1142 | case S_STRING: |
| 1143 | case S_HEX: |
| 1144 | case S_INT: |
| 1145 | if (!strcmp(sym_get_string_value(sym), |
| 1146 | sym->def[S_DEF_AUTO].val)) |
| 1147 | continue; |
| 1148 | break; |
| 1149 | default: |
| 1150 | break; |
| 1151 | } |
| 1152 | } else { |
| 1153 | /* |
| 1154 | * If there is no old value, only 'no' (unset) |
| 1155 | * is allowed as new value. |
| 1156 | */ |
| 1157 | switch (sym->type) { |
| 1158 | case S_BOOLEAN: |
| 1159 | case S_TRISTATE: |
| 1160 | if (sym_get_tristate_value(sym) == no) |
| 1161 | continue; |
| 1162 | break; |
| 1163 | default: |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | } else if (!(sym->flags & SYMBOL_DEF_AUTO)) |
| 1168 | /* There is neither an old nor a new value. */ |
| 1169 | continue; |
| 1170 | /* else |
| 1171 | * There is an old value, but no new value ('no' (unset) |
| 1172 | * isn't saved in auto.conf, so the old value is always |
| 1173 | * different from 'no'). |
| 1174 | */ |
| 1175 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1176 | res = conf_touch_dep(sym->name); |
| 1177 | if (res) |
| 1178 | return res; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1179 | } |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1180 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1181 | return 0; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1184 | static int __conf_write_autoconf(const char *filename, |
| 1185 | void (*print_symbol)(FILE *, struct symbol *), |
| 1186 | const struct comment_style *comment_style) |
| 1187 | { |
| 1188 | char tmp[PATH_MAX]; |
| 1189 | FILE *file; |
| 1190 | struct symbol *sym; |
| 1191 | int ret, i; |
| 1192 | |
| 1193 | if (make_parent_dir(filename)) |
| 1194 | return -1; |
| 1195 | |
| 1196 | ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename); |
| 1197 | if (ret >= sizeof(tmp)) /* check truncation */ |
| 1198 | return -1; |
| 1199 | |
| 1200 | file = fopen(tmp, "w"); |
| 1201 | if (!file) { |
| 1202 | perror("fopen"); |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
| 1206 | conf_write_heading(file, comment_style); |
| 1207 | |
| 1208 | int print_negatives = getenv("KCONFIG_NEGATIVES") != NULL; |
| 1209 | for_all_symbols(i, sym) |
| 1210 | if (((sym->flags & SYMBOL_WRITE) || (print_negatives && sym->type != S_STRING)) && sym->name) |
| 1211 | print_symbol(file, sym); |
| 1212 | |
Patrick Georgi | 5526be2 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1213 | fflush(file); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1214 | /* check possible errors in conf_write_heading() and print_symbol() */ |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1215 | ret = ferror(file); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1216 | fclose(file); |
Patrick Georgi | 7eb03cb | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1217 | if (ret) |
| 1218 | return -1; |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1219 | |
| 1220 | if (rename(tmp, filename)) { |
| 1221 | perror("rename"); |
| 1222 | return -1; |
| 1223 | } |
| 1224 | |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1228 | int conf_write_autoconf(int overwrite) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1229 | { |
| 1230 | struct symbol *sym; |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1231 | const char *autoconf_name = conf_get_autoconfig_name(); |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1232 | int ret, i; |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1233 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1234 | if (!overwrite && is_present(autoconf_name)) |
| 1235 | return 0; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1236 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1237 | ret = conf_write_autoconf_cmd(autoconf_name); |
| 1238 | if (ret) |
| 1239 | return -1; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1240 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1241 | if (conf_touch_deps()) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1242 | return 1; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1243 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1244 | for_all_symbols(i, sym) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1245 | sym_calc_value(sym); |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1246 | |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1247 | ret = __conf_write_autoconf(conf_get_autoheader_name(), |
| 1248 | print_symbol_for_c, |
| 1249 | &comment_style_c); |
| 1250 | if (ret) |
| 1251 | return ret; |
Raul E Rangel | 7b2dedd | 2019-07-25 15:49:52 -0600 | [diff] [blame] | 1252 | |
Patrick Georgi | 7f93aa4 | 2023-11-20 17:58:40 +0100 | [diff] [blame] | 1253 | ret = __conf_write_autoconf(conf_get_rustccfg_name(), |
| 1254 | print_symbol_for_rustccfg, |
| 1255 | NULL); |
| 1256 | if (ret) |
| 1257 | return ret; |
| 1258 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1259 | /* |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1260 | * Create include/config/auto.conf. This must be the last step because |
| 1261 | * Kbuild has a dependency on auto.conf and this marks the successful |
| 1262 | * completion of the previous steps. |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1263 | */ |
Patrick Georgi | 4c9b9e9 | 2022-10-28 01:00:26 +0200 | [diff] [blame] | 1264 | ret = __conf_write_autoconf(conf_get_autoconfig_name(), |
| 1265 | print_symbol_for_autoconf, |
| 1266 | &comment_style_pound); |
| 1267 | if (ret) |
| 1268 | return ret; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1269 | |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1273 | static bool conf_changed; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1274 | static void (*conf_changed_callback)(void); |
| 1275 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1276 | void conf_set_changed(bool val) |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1277 | { |
Patrick Georgi | 54cec70 | 2023-11-20 18:07:18 +0100 | [diff] [blame] | 1278 | bool changed = conf_changed != val; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1279 | |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1280 | conf_changed = val; |
Patrick Georgi | 54cec70 | 2023-11-20 18:07:18 +0100 | [diff] [blame] | 1281 | |
| 1282 | if (conf_changed_callback && changed) |
| 1283 | conf_changed_callback(); |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | bool conf_get_changed(void) |
| 1287 | { |
Patrick Georgi | 53ea1d4 | 2019-11-22 16:55:58 +0100 | [diff] [blame] | 1288 | return conf_changed; |
Patrick Georgi | 0588d19 | 2009-08-12 15:00:51 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | void conf_set_changed_callback(void (*fn)(void)) |
| 1292 | { |
| 1293 | conf_changed_callback = fn; |
| 1294 | } |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1295 | |
Patrick Georgi | d520840 | 2014-04-11 20:24:06 +0200 | [diff] [blame] | 1296 | void set_all_choice_values(struct symbol *csym) |
| 1297 | { |
| 1298 | struct property *prop; |
| 1299 | struct symbol *sym; |
| 1300 | struct expr *e; |
| 1301 | |
| 1302 | prop = sym_get_choice_prop(csym); |
| 1303 | |
| 1304 | /* |
| 1305 | * Set all non-assinged choice values to no |
| 1306 | */ |
| 1307 | expr_list_for_each_sym(prop->expr, e, sym) { |
| 1308 | if (!sym_has_value(sym)) |
| 1309 | sym->def[S_DEF_USER].tri = no; |
| 1310 | } |
| 1311 | csym->flags |= SYMBOL_DEF_USER; |
| 1312 | /* clear VALID to get value calculated */ |
| 1313 | csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES); |
| 1314 | } |