blob: 1300ab674fa0346bfb23ac5f993b092db4598245 [file] [log] [blame]
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001// SPDX-License-Identifier: GPL-2.0
Patrick Georgi0588d192009-08-12 15:00:51 +00002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Patrick Georgi0588d192009-08-12 15:00:51 +00004 */
5
Patrick Georgi53ea1d42019-11-22 16:55:58 +01006#include <sys/mman.h>
Patrick Georgi0588d192009-08-12 15:00:51 +00007#include <sys/stat.h>
Patrick Georgi53ea1d42019-11-22 16:55:58 +01008#include <sys/types.h>
Patrick Georgi0588d192009-08-12 15:00:51 +00009#include <ctype.h>
Patrick Georgid5208402014-04-11 20:24:06 +020010#include <errno.h>
Patrick Georgi0588d192009-08-12 15:00:51 +000011#include <fcntl.h>
Patrick Georgi53ea1d42019-11-22 16:55:58 +010012#include <limits.h>
Patrick Georgid5208402014-04-11 20:24:06 +020013#include <stdarg.h>
Patrick Georgi4c9b9e92022-10-28 01:00:26 +020014#include <stdbool.h>
Patrick Georgi0588d192009-08-12 15:00:51 +000015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19#include <unistd.h>
20
Patrick Georgi0588d192009-08-12 15:00:51 +000021#include "lkc.h"
22
Patrick Georgi53ea1d42019-11-22 16:55:58 +010023/* return true if 'path' exists, false otherwise */
24static 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 */
32static 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 */
43static 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;
80close2:
81 close(fd2);
82close1:
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 */
93static 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
127static char depfile_path[PATH_MAX];
128static size_t depfile_prefix_len;
129
130/* touch depfile for symbol 'name' */
131static int conf_touch_dep(const char *name)
132{
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200133 int fd;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100134
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 Georgi4c9b9e92022-10-28 01:00:26 +0200139 strcpy(depfile_path + depfile_prefix_len, name);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100140
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100141 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200142 if (fd == -1)
143 return -1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100144 close(fd);
145
146 return 0;
147}
148
Patrick Georgi0588d192009-08-12 15:00:51 +0000149static void conf_warning(const char *fmt, ...)
150 __attribute__ ((format (printf, 1, 2)));
151
Patrick Georgid5208402014-04-11 20:24:06 +0200152static void conf_message(const char *fmt, ...)
153 __attribute__ ((format (printf, 1, 2)));
154
Patrick Georgi0588d192009-08-12 15:00:51 +0000155static const char *conf_filename;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100156static int conf_lineno, conf_warnings;
zbao2f3fd262015-09-26 06:20:53 -0400157
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200158#ifdef __MINGW32__
159#define mkdir(_n,_p) mkdir((_n))
160#endif
161
Patrick Georgi0588d192009-08-12 15:00:51 +0000162static void conf_warning(const char *fmt, ...)
163{
164 va_list ap;
165 va_start(ap, fmt);
166 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
167 vfprintf(stderr, fmt, ap);
168 fprintf(stderr, "\n");
169 va_end(ap);
170 conf_warnings++;
171}
172
Martin Roth00cbc7f2016-09-21 14:27:26 -0600173static void conf_notice(const char *fmt, ...)
174{
175 va_list ap;
176 va_start(ap, fmt);
177 fprintf(stderr, "%s:%d:notice: ", conf_filename, conf_lineno);
178 vfprintf(stderr, fmt, ap);
179 fprintf(stderr, "\n");
180 va_end(ap);
181}
182
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100183static void conf_default_message_callback(const char *s)
Patrick Georgid5208402014-04-11 20:24:06 +0200184{
185 printf("#\n# ");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100186 printf("%s", s);
Patrick Georgid5208402014-04-11 20:24:06 +0200187 printf("\n#\n");
188}
189
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100190static void (*conf_message_callback)(const char *s) =
Patrick Georgid5208402014-04-11 20:24:06 +0200191 conf_default_message_callback;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100192void conf_set_message_callback(void (*fn)(const char *s))
Patrick Georgid5208402014-04-11 20:24:06 +0200193{
194 conf_message_callback = fn;
195}
196
197static void conf_message(const char *fmt, ...)
198{
199 va_list ap;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100200 char buf[4096];
201
202 if (!conf_message_callback)
203 return;
Patrick Georgid5208402014-04-11 20:24:06 +0200204
205 va_start(ap, fmt);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100206
207 vsnprintf(buf, sizeof(buf), fmt, ap);
208 conf_message_callback(buf);
Stefan Reinauercce66622015-04-06 01:14:21 +0200209 va_end(ap);
Patrick Georgid5208402014-04-11 20:24:06 +0200210}
211
Patrick Georgi0588d192009-08-12 15:00:51 +0000212const char *conf_get_configname(void)
213{
214 char *name = getenv("KCONFIG_CONFIG");
215
216 return name ? name : ".config";
217}
218
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100219static const char *conf_get_autoconfig_name(void)
Patrick Georgid5208402014-04-11 20:24:06 +0200220{
221 char *name = getenv("KCONFIG_AUTOCONFIG");
222
223 return name ? name : "include/config/auto.conf";
224}
225
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200226static const char *conf_get_autoheader_name(void)
227{
228 char *name = getenv("KCONFIG_AUTOHEADER");
229
230 return name ? name : "include/generated/autoconf.h";
231}
232
Martin Roth35e09ec2022-11-13 15:20:57 -0700233static const char *conf_get_autobase_name(void)
234{
235 char *name = getenv("KCONFIG_SPLITCONFIG");
236
237 return name ? name : "include/config/";
238}
239
Patrick Georgi0588d192009-08-12 15:00:51 +0000240static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
241{
242 char *p2;
243
244 switch (sym->type) {
245 case S_TRISTATE:
246 if (p[0] == 'm') {
247 sym->def[def].tri = mod;
248 sym->flags |= def_flags;
249 break;
250 }
Patrick Georgid5208402014-04-11 20:24:06 +0200251 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000252 case S_BOOLEAN:
253 if (p[0] == 'y') {
254 sym->def[def].tri = yes;
255 sym->flags |= def_flags;
256 break;
257 }
258 if (p[0] == 'n') {
259 sym->def[def].tri = no;
260 sym->flags |= def_flags;
261 break;
262 }
Patrick Georgid5208402014-04-11 20:24:06 +0200263 if (def != S_DEF_AUTO)
264 conf_warning("symbol value '%s' invalid for %s",
265 p, sym->name);
266 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000267 case S_STRING:
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200268 /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
269 if (def != S_DEF_AUTO) {
270 if (*p++ != '"')
Patrick Georgi0588d192009-08-12 15:00:51 +0000271 break;
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200272 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
273 if (*p2 == '"') {
274 *p2 = 0;
275 break;
276 }
277 memmove(p2, p2 + 1, strlen(p2));
Patrick Georgi0588d192009-08-12 15:00:51 +0000278 }
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200279 if (!p2) {
Patrick Georgid5208402014-04-11 20:24:06 +0200280 conf_warning("invalid string found");
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200281 return 1;
282 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000283 }
Patrick Georgid5208402014-04-11 20:24:06 +0200284 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000285 case S_INT:
286 case S_HEX:
Patrick Georgi0588d192009-08-12 15:00:51 +0000287 if (sym_string_valid(sym, p)) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100288 sym->def[def].val = xstrdup(p);
Patrick Georgi0588d192009-08-12 15:00:51 +0000289 sym->flags |= def_flags;
290 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200291 if (def != S_DEF_AUTO)
292 conf_warning("symbol value '%s' invalid for %s",
293 p, sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000294 return 1;
295 }
296 break;
297 default:
298 ;
299 }
300 return 0;
301}
302
Patrick Georgid5208402014-04-11 20:24:06 +0200303#define LINE_GROWTH 16
304static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
305{
306 char *nline;
307 size_t new_size = slen + 1;
308 if (new_size > *n) {
309 new_size += LINE_GROWTH - 1;
310 new_size *= 2;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100311 nline = xrealloc(*lineptr, new_size);
Patrick Georgid5208402014-04-11 20:24:06 +0200312 if (!nline)
313 return -1;
314
315 *lineptr = nline;
316 *n = new_size;
317 }
318
319 (*lineptr)[slen] = c;
320
321 return 0;
322}
323
324static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
325{
326 char *line = *lineptr;
327 size_t slen = 0;
328
329 for (;;) {
330 int c = getc(stream);
331
332 switch (c) {
333 case '\n':
334 if (add_byte(c, &line, slen, n) < 0)
335 goto e_out;
336 slen++;
337 /* fall through */
338 case EOF:
339 if (add_byte('\0', &line, slen, n) < 0)
340 goto e_out;
341 *lineptr = line;
342 if (slen == 0)
343 return -1;
344 return slen;
345 default:
346 if (add_byte(c, &line, slen, n) < 0)
347 goto e_out;
348 slen++;
349 }
350 }
351
352e_out:
353 line[slen-1] = '\0';
354 *lineptr = line;
355 return -1;
356}
357
Patrick Georgi0588d192009-08-12 15:00:51 +0000358int conf_read_simple(const char *name, int def)
359{
360 FILE *in = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200361 char *line = NULL;
362 size_t line_asize = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000363 char *p, *p2;
364 struct symbol *sym;
365 int i, def_flags;
366
367 if (name) {
368 in = zconf_fopen(name);
369 } else {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100370 char *env;
Patrick Georgi0588d192009-08-12 15:00:51 +0000371
372 name = conf_get_configname();
373 in = zconf_fopen(name);
374 if (in)
375 goto load;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100376 conf_set_changed(true);
Patrick Georgi0588d192009-08-12 15:00:51 +0000377
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100378 env = getenv("KCONFIG_DEFCONFIG_LIST");
379 if (!env)
380 return 1;
381
382 while (1) {
383 bool is_last;
384
385 while (isspace(*env))
386 env++;
387
388 if (!*env)
389 break;
390
391 p = env;
392 while (*p && !isspace(*p))
393 p++;
394
395 is_last = (*p == '\0');
396
397 *p = '\0';
398
399 in = zconf_fopen(env);
Patrick Georgi0588d192009-08-12 15:00:51 +0000400 if (in) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100401 conf_message("using defaults found in %s",
402 env);
Patrick Georgi0588d192009-08-12 15:00:51 +0000403 goto load;
404 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100405
406 if (is_last)
407 break;
408
409 env = p + 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000410 }
411 }
412 if (!in)
413 return 1;
414
415load:
416 conf_filename = name;
417 conf_lineno = 0;
418 conf_warnings = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000419
420 def_flags = SYMBOL_DEF << def;
421 for_all_symbols(i, sym) {
422 sym->flags |= SYMBOL_CHANGED;
423 sym->flags &= ~(def_flags|SYMBOL_VALID);
424 if (sym_is_choice(sym))
425 sym->flags |= def_flags;
426 switch (sym->type) {
427 case S_INT:
428 case S_HEX:
429 case S_STRING:
430 if (sym->def[def].val)
431 free(sym->def[def].val);
Patrick Georgid5208402014-04-11 20:24:06 +0200432 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000433 default:
434 sym->def[def].val = NULL;
435 sym->def[def].tri = no;
436 }
437 }
438
Patrick Georgid5208402014-04-11 20:24:06 +0200439 while (compat_getline(&line, &line_asize, in) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000440 conf_lineno++;
441 sym = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200442 if (line[0] == '#') {
443 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Patrick Georgi0588d192009-08-12 15:00:51 +0000444 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200445 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Patrick Georgi0588d192009-08-12 15:00:51 +0000446 if (!p)
447 continue;
448 *p++ = 0;
449 if (strncmp(p, "is not set", 10))
450 continue;
451 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200452 sym = sym_find(line + 2 + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000453 if (!sym) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100454 conf_message(
455 "ignoring nonexistent symbol %s",
456 line + 2 + strlen(CONFIG_));
457 conf_set_changed(true);
458 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000459 }
460 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200461 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000462 if (sym->type == S_UNKNOWN)
463 sym->type = S_BOOLEAN;
464 }
465 if (sym->flags & def_flags) {
Martin Roth00cbc7f2016-09-21 14:27:26 -0600466 conf_notice("override: reassigning to symbol %s", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000467 }
468 switch (sym->type) {
469 case S_BOOLEAN:
470 case S_TRISTATE:
471 sym->def[def].tri = no;
472 sym->flags |= def_flags;
473 break;
474 default:
475 ;
476 }
Patrick Georgid5208402014-04-11 20:24:06 +0200477 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
478 p = strchr(line + strlen(CONFIG_), '=');
Patrick Georgi0588d192009-08-12 15:00:51 +0000479 if (!p)
480 continue;
481 *p++ = 0;
482 p2 = strchr(p, '\n');
483 if (p2) {
484 *p2-- = 0;
485 if (*p2 == '\r')
486 *p2 = 0;
487 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100488
489 sym = sym_find(line + strlen(CONFIG_));
490 if (!sym) {
491 if (def == S_DEF_AUTO)
492 /*
493 * Reading from include/config/auto.conf
494 * If CONFIG_FOO previously existed in
495 * auto.conf but it is missing now,
496 * include/config/FOO must be touched.
497 */
498 conf_touch_dep(line + strlen(CONFIG_));
499 else
500 conf_set_changed(true);
501 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000502 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100503
Patrick Georgi0588d192009-08-12 15:00:51 +0000504 if (sym->flags & def_flags) {
Martin Roth00cbc7f2016-09-21 14:27:26 -0600505 conf_notice("override: reassigning to symbol %s", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000506 }
507 if (conf_set_sym_val(sym, def, def_flags, p))
508 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200509 } else {
510 if (line[0] != '\r' && line[0] != '\n')
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100511 conf_warning("unexpected data: %.*s",
512 (int)strcspn(line, "\r\n"), line);
513
Patrick Georgi0588d192009-08-12 15:00:51 +0000514 continue;
515 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100516
Patrick Georgi0588d192009-08-12 15:00:51 +0000517 if (sym && sym_is_choice_value(sym)) {
518 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
519 switch (sym->def[def].tri) {
520 case no:
521 break;
522 case mod:
523 if (cs->def[def].tri == yes) {
524 conf_warning("%s creates inconsistent choice state", sym->name);
525 cs->flags &= ~def_flags;
526 }
527 break;
528 case yes:
529 if (cs->def[def].tri != no)
Martin Roth00cbc7f2016-09-21 14:27:26 -0600530 conf_notice("override: %s changes choice state", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000531 cs->def[def].val = sym;
532 break;
533 }
534 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
535 }
536 }
Patrick Georgid5208402014-04-11 20:24:06 +0200537 free(line);
Patrick Georgi0588d192009-08-12 15:00:51 +0000538 fclose(in);
539
Stefan Reinauer57a31312015-08-20 11:19:34 -0700540 kconfig_warnings += conf_warnings;
Stefan Reinauerf5342032015-07-17 17:26:48 -0700541
Patrick Georgi0588d192009-08-12 15:00:51 +0000542 return 0;
543}
544
545int conf_read(const char *name)
546{
Patrick Georgid5208402014-04-11 20:24:06 +0200547 struct symbol *sym;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100548 int conf_unsaved = 0;
Patrick Georgid5208402014-04-11 20:24:06 +0200549 int i;
Patrick Georgi0588d192009-08-12 15:00:51 +0000550
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100551 conf_set_changed(false);
Patrick Georgi0588d192009-08-12 15:00:51 +0000552
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100553 if (conf_read_simple(name, S_DEF_USER)) {
554 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000555 return 1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100556 }
557
558 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000559
560 for_all_symbols(i, sym) {
561 sym_calc_value(sym);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100562 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Patrick Georgid5208402014-04-11 20:24:06 +0200563 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000564 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
565 /* check that calculated value agrees with saved value */
566 switch (sym->type) {
567 case S_BOOLEAN:
568 case S_TRISTATE:
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100569 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200570 continue;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100571 break;
Patrick Georgi0588d192009-08-12 15:00:51 +0000572 default:
573 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Patrick Georgid5208402014-04-11 20:24:06 +0200574 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000575 break;
576 }
577 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
578 /* no previous value and not saved */
Patrick Georgid5208402014-04-11 20:24:06 +0200579 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000580 conf_unsaved++;
581 /* maybe print value in verbose mode... */
Patrick Georgi0588d192009-08-12 15:00:51 +0000582 }
583
584 for_all_symbols(i, sym) {
585 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
586 /* Reset values of generates values, so they'll appear
587 * as new, if they should become visible, but that
588 * doesn't quite work if the Kconfig and the saved
589 * configuration disagree.
590 */
591 if (sym->visible == no && !conf_unsaved)
592 sym->flags &= ~SYMBOL_DEF_USER;
593 switch (sym->type) {
594 case S_STRING:
595 case S_INT:
596 case S_HEX:
597 /* Reset a string value if it's out of range */
598 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
599 break;
600 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
601 conf_unsaved++;
602 break;
603 default:
604 break;
605 }
606 }
607 }
608
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100609 if (conf_warnings || conf_unsaved)
610 conf_set_changed(true);
Patrick Georgi0588d192009-08-12 15:00:51 +0000611
612 return 0;
613}
614
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200615struct comment_style {
616 const char *decoration;
617 const char *prefix;
618 const char *postfix;
619};
620
621static const struct comment_style comment_style_pound = {
622 .decoration = "#",
623 .prefix = "#",
624 .postfix = "#",
625};
626
627static const struct comment_style comment_style_c = {
628 .decoration = " *",
629 .prefix = "/*",
630 .postfix = " */",
631};
632
633static void conf_write_heading(FILE *fp, const struct comment_style *cs)
634{
635 fprintf(fp, "%s\n", cs->prefix);
636
637 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
638 cs->decoration);
639
640 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
641
642 fprintf(fp, "%s\n", cs->postfix);
643}
644
645/* The returned pointer must be freed on the caller side */
646static char *escape_string_value(const char *in)
647{
648 const char *p;
649 char *out;
650 size_t len;
651
652 len = strlen(in) + strlen("\"\"") + 1;
653
654 p = in;
655 while (1) {
656 p += strcspn(p, "\"\\");
657
658 if (p[0] == '\0')
659 break;
660
661 len++;
662 p++;
663 }
664
665 out = xmalloc(len);
666 out[0] = '\0';
667
668 strcat(out, "\"");
669
670 p = in;
671 while (1) {
672 len = strcspn(p, "\"\\");
673 strncat(out, p, len);
674 p += len;
675
676 if (p[0] == '\0')
677 break;
678
679 strcat(out, "\\");
680 strncat(out, p++, 1);
681 }
682
683 strcat(out, "\"");
684
685 return out;
686}
687
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200688enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
689
690static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
691 bool escape_string)
Patrick Georgid5208402014-04-11 20:24:06 +0200692{
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200693 const char *val;
694 char *escaped = NULL;
695
696 if (sym->type == S_UNKNOWN)
697 return;
698
699 val = sym_get_string_value(sym);
700
701 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
702 output_n != OUTPUT_N && *val == 'n') {
703 if (output_n == OUTPUT_N_AS_UNSET)
704 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
705 return;
706 }
707
708 if (sym->type == S_STRING && escape_string) {
709 escaped = escape_string_value(val);
710 val = escaped;
711 }
712
713 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
714
715 free(escaped);
716}
717
718static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
719{
720 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
721}
722
723static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
724{
725 int print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
726 enum output_n out = OUTPUT_N_NONE;
727 if (print_negatives) {
728 out = OUTPUT_N;
729 }
Patrick Georgi7eb03cb2022-10-28 01:00:26 +0200730 __print_symbol(fp, sym, out, false);
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200731}
732
733void print_symbol_for_listconfig(struct symbol *sym)
734{
735 __print_symbol(stdout, sym, OUTPUT_N, true);
736}
737
738static void print_symbol_for_c(FILE *fp, struct symbol *sym)
739{
740 const char *val;
741 const char *sym_suffix = "";
742 const char *val_prefix = "";
743 char *escaped = NULL;
744
745 if (sym->type == S_UNKNOWN)
746 return;
747
748 val = sym_get_string_value(sym);
Patrick Georgid5208402014-04-11 20:24:06 +0200749
750 switch (sym->type) {
751 case S_BOOLEAN:
752 case S_TRISTATE:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200753 switch (*val) {
Patrick Georgid5208402014-04-11 20:24:06 +0200754 case 'n':
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200755 if (getenv("KCONFIG_NEGATIVES") != NULL) {
756 val = "0";
757 break;
758 }
759 return;
Patrick Georgid5208402014-04-11 20:24:06 +0200760 case 'm':
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200761 sym_suffix = "_MODULE";
Patrick Georgid5208402014-04-11 20:24:06 +0200762 /* fall through */
763 default:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200764 val = "1";
Patrick Georgid5208402014-04-11 20:24:06 +0200765 }
766 break;
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200767 case S_HEX:
768 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
769 val_prefix = "0x";
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100770 /* fall through */
771 case S_INT:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200772 if (val[0] == '\0') {
773 val = "0";
774 val_prefix = "";
Patrick Georgid5208402014-04-11 20:24:06 +0200775 }
Patrick Georgid5208402014-04-11 20:24:06 +0200776 break;
777 case S_STRING:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200778 escaped = escape_string_value(val);
779 val = escaped;
Patrick Georgid5208402014-04-11 20:24:06 +0200780 default:
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200781 break;
Patrick Georgid5208402014-04-11 20:24:06 +0200782 }
Patrick Georgid5208402014-04-11 20:24:06 +0200783
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200784 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
785 val_prefix, val);
Patrick Georgid5208402014-04-11 20:24:06 +0200786
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200787 free(escaped);
Patrick Georgid5208402014-04-11 20:24:06 +0200788}
789
790/*
791 * Write out a minimal config.
792 * All values that has default values are skipped as this is redundant.
793 */
794int conf_write_defconfig(const char *filename)
795{
796 struct symbol *sym;
797 struct menu *menu;
798 FILE *out;
799
800 out = fopen(filename, "w");
801 if (!out)
802 return 1;
803
804 sym_clear_all_valid();
805
806 /* Traverse all menus to find all relevant symbols */
807 menu = rootmenu.list;
808
809 while (menu != NULL)
810 {
811 sym = menu->sym;
812 if (sym == NULL) {
813 if (!menu_is_visible(menu))
814 goto next_menu;
815 } else if (!sym_is_choice(sym)) {
816 sym_calc_value(sym);
817 if (!(sym->flags & SYMBOL_WRITE))
818 goto next_menu;
819 sym->flags &= ~SYMBOL_WRITE;
820 /* If we cannot change the symbol - skip */
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100821 if (!sym_is_changeable(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200822 goto next_menu;
823 /* If symbol equals to default value - skip */
824 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
825 goto next_menu;
826
827 /*
828 * If symbol is a choice value and equals to the
829 * default for a choice - skip.
830 * But only if value is bool and equal to "y" and
831 * choice is not "optional".
832 * (If choice is "optional" then all values can be "n")
833 */
834 if (sym_is_choice_value(sym)) {
835 struct symbol *cs;
836 struct symbol *ds;
837
838 cs = prop_get_symbol(sym_get_choice_prop(sym));
839 ds = sym_choice_default(cs);
840 if (!sym_is_optional(cs) && sym == ds) {
841 if ((sym->type == S_BOOLEAN) &&
842 sym_get_tristate_value(sym) == yes)
843 goto next_menu;
844 }
845 }
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200846 print_symbol_for_dotconfig(out, sym);
Patrick Georgid5208402014-04-11 20:24:06 +0200847 }
848next_menu:
849 if (menu->list != NULL) {
850 menu = menu->list;
851 }
852 else if (menu->next != NULL) {
853 menu = menu->next;
854 } else {
855 while ((menu = menu->parent)) {
856 if (menu->next != NULL) {
857 menu = menu->next;
858 break;
859 }
860 }
861 }
862 }
863 fclose(out);
864 return 0;
865}
866
Patrick Georgi0588d192009-08-12 15:00:51 +0000867int conf_write(const char *name)
868{
869 FILE *out;
870 struct symbol *sym;
871 struct menu *menu;
Patrick Georgi0588d192009-08-12 15:00:51 +0000872 const char *str;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100873 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000874 char *env;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100875 int i;
876 bool need_newline = false;
Patrick Georgi0588d192009-08-12 15:00:51 +0000877
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100878 if (!name)
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600879 name = conf_get_configname();
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100880
881 if (!*name) {
882 fprintf(stderr, "config name is empty\n");
883 return -1;
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600884 }
885
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100886 if (is_dir(name)) {
887 fprintf(stderr, "%s: Is a directory\n", name);
888 return -1;
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600889 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000890
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100891 if (make_parent_dir(name))
892 return -1;
893
Patrick Georgi0588d192009-08-12 15:00:51 +0000894 env = getenv("KCONFIG_OVERWRITECONFIG");
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100895 if (env && *env) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000896 *tmpname = 0;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100897 out = fopen(name, "w");
898 } else {
899 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
900 name, (int)getpid());
901 out = fopen(tmpname, "w");
Patrick Georgi0588d192009-08-12 15:00:51 +0000902 }
903 if (!out)
904 return 1;
905
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200906 conf_write_heading(out, &comment_style_pound);
Patrick Georgi0588d192009-08-12 15:00:51 +0000907
908 if (!conf_get_changed())
909 sym_clear_all_valid();
910
911 menu = rootmenu.list;
912 while (menu) {
913 sym = menu->sym;
914 if (!sym) {
915 if (!menu_is_visible(menu))
916 goto next;
917 str = menu_get_prompt(menu);
918 fprintf(out, "\n"
919 "#\n"
920 "# %s\n"
921 "#\n", str);
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100922 need_newline = false;
923 } else if (!(sym->flags & SYMBOL_CHOICE) &&
924 !(sym->flags & SYMBOL_WRITTEN)) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000925 sym_calc_value(sym);
926 if (!(sym->flags & SYMBOL_WRITE))
927 goto next;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100928 if (need_newline) {
929 fprintf(out, "\n");
930 need_newline = false;
931 }
932 sym->flags |= SYMBOL_WRITTEN;
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200933 print_symbol_for_dotconfig(out, sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000934 }
935
Patrick Georgid5208402014-04-11 20:24:06 +0200936next:
Patrick Georgi0588d192009-08-12 15:00:51 +0000937 if (menu->list) {
938 menu = menu->list;
939 continue;
940 }
Patrick Georgi5526be22022-10-28 01:00:26 +0200941
942end_check:
943 if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
944 menu->prompt->type == P_MENU) {
945 fprintf(out, "# end of %s\n", menu_get_prompt(menu));
946 need_newline = true;
947 }
948
949 if (menu->next) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000950 menu = menu->next;
Patrick Georgi5526be22022-10-28 01:00:26 +0200951 } else {
952 menu = menu->parent;
953 if (menu)
954 goto end_check;
Patrick Georgi0588d192009-08-12 15:00:51 +0000955 }
956 }
957 fclose(out);
958
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100959 for_all_symbols(i, sym)
960 sym->flags &= ~SYMBOL_WRITTEN;
961
Patrick Georgi0588d192009-08-12 15:00:51 +0000962 if (*tmpname) {
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100963 if (is_same(name, tmpname)) {
964 conf_message("No change to %s", name);
965 unlink(tmpname);
966 conf_set_changed(false);
967 return 0;
968 }
969
970 snprintf(oldname, sizeof(oldname), "%s.old", name);
971 rename(name, oldname);
972 if (rename(tmpname, name))
Patrick Georgi0588d192009-08-12 15:00:51 +0000973 return 1;
974 }
975
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100976 conf_message("configuration written to %s", name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000977
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100978 conf_set_changed(false);
Patrick Georgi0588d192009-08-12 15:00:51 +0000979
980 return 0;
981}
982
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100983/* write a dependency file as used by kbuild to track dependencies */
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200984static int conf_write_autoconf_cmd(const char *autoconf_name)
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100985{
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200986 char name[PATH_MAX], tmp[PATH_MAX];
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100987 struct file *file;
988 FILE *out;
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200989 int ret;
990
991 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
992 if (ret >= sizeof(name)) /* check truncation */
993 return -1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100994
995 if (make_parent_dir(name))
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200996 return -1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +0100997
Patrick Georgi4c9b9e92022-10-28 01:00:26 +0200998 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
999 if (ret >= sizeof(tmp)) /* check truncation */
1000 return -1;
1001
1002 out = fopen(tmp, "w");
1003 if (!out) {
1004 perror("fopen");
1005 return -1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001006 }
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001007
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001008 fprintf(out, "deps_config := \\\n");
1009 for (file = file_list; file; file = file->next)
1010 fprintf(out, "\t%s \\\n", file->name);
1011
1012 fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
1013
1014 env_write_dep(out, autoconf_name);
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001015
1016 fprintf(out, "\n$(deps_config): ;\n");
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001017
Patrick Georgi5526be22022-10-28 01:00:26 +02001018 fflush(out);
Patrick Georgi7eb03cb2022-10-28 01:00:26 +02001019 ret = ferror(out); /* error check for all fprintf() calls */
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001020 fclose(out);
Patrick Georgi7eb03cb2022-10-28 01:00:26 +02001021 if (ret)
1022 return -1;
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001023
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001024 if (rename(tmp, name)) {
1025 perror("rename");
1026 return -1;
1027 }
1028
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001029 return 0;
1030}
1031
1032static int conf_touch_deps(void)
Patrick Georgi0588d192009-08-12 15:00:51 +00001033{
Martin Roth35e09ec2022-11-13 15:20:57 -07001034 const char *name;
Patrick Georgi0588d192009-08-12 15:00:51 +00001035 struct symbol *sym;
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001036 int res, i;
1037
Nicholas Chin8ef2f7c2022-11-23 10:18:32 -07001038 /*
1039 * Upstream Kconfig sets depfile_path based on the directory
1040 * prefix of the autoconfig path, but coreboot overrides this
1041 * using the KCONFIG_SPLITCONFIG environment variable
1042 */
Martin Roth35e09ec2022-11-13 15:20:57 -07001043 strcpy(depfile_path, conf_get_autobase_name());
1044 depfile_prefix_len = strlen(depfile_path);
1045
Patrick Georgid5208402014-04-11 20:24:06 +02001046 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +00001047 conf_read_simple(name, S_DEF_AUTO);
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001048 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +00001049
Patrick Georgi0588d192009-08-12 15:00:51 +00001050 for_all_symbols(i, sym) {
1051 sym_calc_value(sym);
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001052 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Patrick Georgi0588d192009-08-12 15:00:51 +00001053 continue;
1054 if (sym->flags & SYMBOL_WRITE) {
1055 if (sym->flags & SYMBOL_DEF_AUTO) {
1056 /*
1057 * symbol has old and new value,
1058 * so compare them...
1059 */
1060 switch (sym->type) {
1061 case S_BOOLEAN:
1062 case S_TRISTATE:
1063 if (sym_get_tristate_value(sym) ==
1064 sym->def[S_DEF_AUTO].tri)
1065 continue;
1066 break;
1067 case S_STRING:
1068 case S_HEX:
1069 case S_INT:
1070 if (!strcmp(sym_get_string_value(sym),
1071 sym->def[S_DEF_AUTO].val))
1072 continue;
1073 break;
1074 default:
1075 break;
1076 }
1077 } else {
1078 /*
1079 * If there is no old value, only 'no' (unset)
1080 * is allowed as new value.
1081 */
1082 switch (sym->type) {
1083 case S_BOOLEAN:
1084 case S_TRISTATE:
1085 if (sym_get_tristate_value(sym) == no)
1086 continue;
1087 break;
1088 default:
1089 break;
1090 }
1091 }
1092 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1093 /* There is neither an old nor a new value. */
1094 continue;
1095 /* else
1096 * There is an old value, but no new value ('no' (unset)
1097 * isn't saved in auto.conf, so the old value is always
1098 * different from 'no').
1099 */
1100
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001101 res = conf_touch_dep(sym->name);
1102 if (res)
1103 return res;
Patrick Georgi0588d192009-08-12 15:00:51 +00001104 }
Patrick Georgi0588d192009-08-12 15:00:51 +00001105
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001106 return 0;
Patrick Georgi0588d192009-08-12 15:00:51 +00001107}
1108
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001109static int __conf_write_autoconf(const char *filename,
1110 void (*print_symbol)(FILE *, struct symbol *),
1111 const struct comment_style *comment_style)
1112{
1113 char tmp[PATH_MAX];
1114 FILE *file;
1115 struct symbol *sym;
1116 int ret, i;
1117
1118 if (make_parent_dir(filename))
1119 return -1;
1120
1121 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1122 if (ret >= sizeof(tmp)) /* check truncation */
1123 return -1;
1124
1125 file = fopen(tmp, "w");
1126 if (!file) {
1127 perror("fopen");
1128 return -1;
1129 }
1130
1131 conf_write_heading(file, comment_style);
1132
1133 int print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
1134 for_all_symbols(i, sym)
1135 if (((sym->flags & SYMBOL_WRITE) || (print_negatives && sym->type != S_STRING)) && sym->name)
1136 print_symbol(file, sym);
1137
Patrick Georgi5526be22022-10-28 01:00:26 +02001138 fflush(file);
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001139 /* check possible errors in conf_write_heading() and print_symbol() */
Patrick Georgi7eb03cb2022-10-28 01:00:26 +02001140 ret = ferror(file);
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001141 fclose(file);
Patrick Georgi7eb03cb2022-10-28 01:00:26 +02001142 if (ret)
1143 return -1;
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001144
1145 if (rename(tmp, filename)) {
1146 perror("rename");
1147 return -1;
1148 }
1149
1150 return 0;
1151}
1152
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001153int conf_write_autoconf(int overwrite)
Patrick Georgi0588d192009-08-12 15:00:51 +00001154{
1155 struct symbol *sym;
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001156 const char *autoconf_name = conf_get_autoconfig_name();
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001157 int ret, i;
Patrick Georgid5208402014-04-11 20:24:06 +02001158
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001159 if (!overwrite && is_present(autoconf_name))
1160 return 0;
Patrick Georgi0588d192009-08-12 15:00:51 +00001161
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001162 ret = conf_write_autoconf_cmd(autoconf_name);
1163 if (ret)
1164 return -1;
Patrick Georgi0588d192009-08-12 15:00:51 +00001165
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001166 if (conf_touch_deps())
Patrick Georgi0588d192009-08-12 15:00:51 +00001167 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +00001168
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001169 for_all_symbols(i, sym)
Patrick Georgi0588d192009-08-12 15:00:51 +00001170 sym_calc_value(sym);
Patrick Georgid5208402014-04-11 20:24:06 +02001171
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001172 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1173 print_symbol_for_c,
1174 &comment_style_c);
1175 if (ret)
1176 return ret;
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001177
Patrick Georgi0588d192009-08-12 15:00:51 +00001178 /*
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001179 * Create include/config/auto.conf. This must be the last step because
1180 * Kbuild has a dependency on auto.conf and this marks the successful
1181 * completion of the previous steps.
Patrick Georgi0588d192009-08-12 15:00:51 +00001182 */
Patrick Georgi4c9b9e92022-10-28 01:00:26 +02001183 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1184 print_symbol_for_autoconf,
1185 &comment_style_pound);
1186 if (ret)
1187 return ret;
Patrick Georgi0588d192009-08-12 15:00:51 +00001188
Patrick Georgi0588d192009-08-12 15:00:51 +00001189 return 0;
1190}
1191
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001192static bool conf_changed;
Patrick Georgi0588d192009-08-12 15:00:51 +00001193static void (*conf_changed_callback)(void);
1194
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001195void conf_set_changed(bool val)
Patrick Georgi0588d192009-08-12 15:00:51 +00001196{
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001197 if (conf_changed_callback && conf_changed != val)
Patrick Georgi0588d192009-08-12 15:00:51 +00001198 conf_changed_callback();
Patrick Georgi0588d192009-08-12 15:00:51 +00001199
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001200 conf_changed = val;
Patrick Georgi0588d192009-08-12 15:00:51 +00001201}
1202
1203bool conf_get_changed(void)
1204{
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001205 return conf_changed;
Patrick Georgi0588d192009-08-12 15:00:51 +00001206}
1207
1208void conf_set_changed_callback(void (*fn)(void))
1209{
1210 conf_changed_callback = fn;
1211}
Patrick Georgid5208402014-04-11 20:24:06 +02001212
Patrick Georgid5208402014-04-11 20:24:06 +02001213void set_all_choice_values(struct symbol *csym)
1214{
1215 struct property *prop;
1216 struct symbol *sym;
1217 struct expr *e;
1218
1219 prop = sym_get_choice_prop(csym);
1220
1221 /*
1222 * Set all non-assinged choice values to no
1223 */
1224 expr_list_for_each_sym(prop->expr, e, sym) {
1225 if (!sym_has_value(sym))
1226 sym->def[S_DEF_USER].tri = no;
1227 }
1228 csym->flags |= SYMBOL_DEF_USER;
1229 /* clear VALID to get value calculated */
1230 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1231}