blob: 59717be312109e8865c0b4ce9a2730c4367d5934 [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Masahiro Yamada67424f62019-05-10 15:12:05 +09006#include <sys/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <sys/stat.h>
Boris Kolpackov78cb0902020-11-23 11:38:18 +02008#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <ctype.h>
Arnaud Lacombe94bedec2010-08-17 01:40:20 -040010#include <errno.h>
Roman Zippel2e3646e2006-06-08 22:12:42 -070011#include <fcntl.h>
Masahiro Yamada558e78e2018-12-21 17:33:04 +090012#include <limits.h>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040013#include <stdarg.h>
Masahiro Yamada6ce45a92021-10-01 14:32:46 +090014#include <stdbool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <time.h>
19#include <unistd.h>
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "lkc.h"
22
Masahiro Yamada06081822018-07-20 16:46:27 +090023/* 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))
Yang Lia69b1912021-03-15 14:55:44 +080037 return false;
Masahiro Yamada06081822018-07-20 16:46:27 +090038
39 return S_ISDIR(st.st_mode);
40}
41
Masahiro Yamada67424f62019-05-10 15:12:05 +090042/* 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
Masahiro Yamada06081822018-07-20 16:46:27 +090088/*
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
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900127static 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{
Masahiro Yamadafee762d2021-10-01 14:32:52 +0900133 int fd;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900134
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300135 /* check overflow: prefix + name + '\0' must fit in buffer. */
136 if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900137 return -1;
138
Masahiro Yamadafee762d2021-10-01 14:32:52 +0900139 strcpy(depfile_path + depfile_prefix_len, name);
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900140
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900141 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Masahiro Yamadafee762d2021-10-01 14:32:52 +0900142 if (fd == -1)
143 return -1;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900144 close(fd);
145
146 return 0;
147}
148
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800149static void conf_warning(const char *fmt, ...)
150 __attribute__ ((format (printf, 1, 2)));
151
Michal Marek42368c32010-08-17 10:21:19 +0200152static void conf_message(const char *fmt, ...)
153 __attribute__ ((format (printf, 1, 2)));
154
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800155static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900156static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800157
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800158static void conf_warning(const char *fmt, ...)
159{
160 va_list ap;
161 va_start(ap, fmt);
162 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
163 vfprintf(stderr, fmt, ap);
164 fprintf(stderr, "\n");
165 va_end(ap);
166 conf_warnings++;
167}
168
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900169static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +0200170{
171 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900172 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +0200173 printf("\n#\n");
174}
175
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900176static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +0200177 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900178void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +0200179{
180 conf_message_callback = fn;
181}
182
183static void conf_message(const char *fmt, ...)
184{
185 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900186 char buf[4096];
187
188 if (!conf_message_callback)
189 return;
Michal Marek42368c32010-08-17 10:21:19 +0200190
191 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900192
193 vsnprintf(buf, sizeof(buf), fmt, ap);
194 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +0000195 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +0200196}
197
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700198const char *conf_get_configname(void)
199{
200 char *name = getenv("KCONFIG_CONFIG");
201
202 return name ? name : ".config";
203}
204
Masahiro Yamada9b9f5942019-05-13 01:00:53 +0900205static const char *conf_get_autoconfig_name(void)
Markus Heidelberg12122f62009-05-18 01:36:54 +0200206{
207 char *name = getenv("KCONFIG_AUTOCONFIG");
208
209 return name ? name : "include/config/auto.conf";
210}
211
Masahiro Yamada8499f2d2021-10-01 14:32:49 +0900212static const char *conf_get_autoheader_name(void)
213{
214 char *name = getenv("KCONFIG_AUTOHEADER");
215
216 return name ? name : "include/generated/autoconf.h";
217}
218
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100219static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
220{
221 char *p2;
222
223 switch (sym->type) {
224 case S_TRISTATE:
225 if (p[0] == 'm') {
226 sym->def[def].tri = mod;
227 sym->flags |= def_flags;
228 break;
229 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400230 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100231 case S_BOOLEAN:
232 if (p[0] == 'y') {
233 sym->def[def].tri = yes;
234 sym->flags |= def_flags;
235 break;
236 }
237 if (p[0] == 'n') {
238 sym->def[def].tri = no;
239 sym->flags |= def_flags;
240 break;
241 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200242 if (def != S_DEF_AUTO)
243 conf_warning("symbol value '%s' invalid for %s",
244 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400245 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100246 case S_STRING:
Masahiro Yamada129ab0d2021-12-14 11:53:53 +0900247 /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
248 if (def != S_DEF_AUTO) {
249 if (*p++ != '"')
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100250 break;
Masahiro Yamada129ab0d2021-12-14 11:53:53 +0900251 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
252 if (*p2 == '"') {
253 *p2 = 0;
254 break;
255 }
256 memmove(p2, p2 + 1, strlen(p2));
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100257 }
Masahiro Yamada129ab0d2021-12-14 11:53:53 +0900258 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200259 conf_warning("invalid string found");
Masahiro Yamada129ab0d2021-12-14 11:53:53 +0900260 return 1;
261 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100262 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400263 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100264 case S_INT:
265 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100266 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900267 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100268 sym->flags |= def_flags;
269 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200270 if (def != S_DEF_AUTO)
271 conf_warning("symbol value '%s' invalid for %s",
272 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100273 return 1;
274 }
275 break;
276 default:
277 ;
278 }
279 return 0;
280}
281
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700282#define LINE_GROWTH 16
283static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
284{
285 char *nline;
286 size_t new_size = slen + 1;
287 if (new_size > *n) {
288 new_size += LINE_GROWTH - 1;
289 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900290 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700291 if (!nline)
292 return -1;
293
294 *lineptr = nline;
295 *n = new_size;
296 }
297
298 (*lineptr)[slen] = c;
299
300 return 0;
301}
302
303static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
304{
305 char *line = *lineptr;
306 size_t slen = 0;
307
308 for (;;) {
309 int c = getc(stream);
310
311 switch (c) {
312 case '\n':
313 if (add_byte(c, &line, slen, n) < 0)
314 goto e_out;
315 slen++;
316 /* fall through */
317 case EOF:
318 if (add_byte('\0', &line, slen, n) < 0)
319 goto e_out;
320 *lineptr = line;
321 if (slen == 0)
322 return -1;
323 return slen;
324 default:
325 if (add_byte(c, &line, slen, n) < 0)
326 goto e_out;
327 slen++;
328 }
329 }
330
331e_out:
332 line[slen-1] = '\0';
333 *lineptr = line;
334 return -1;
335}
336
Roman Zippel669bfad92006-06-08 22:12:42 -0700337int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700340 char *line = NULL;
341 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700344 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (name) {
347 in = zconf_fopen(name);
348 } else {
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900349 char *env;
Roman Zippelface4372006-06-08 22:12:45 -0700350
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700351 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700352 in = zconf_fopen(name);
353 if (in)
354 goto load;
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900355 conf_set_changed(true);
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900356
357 env = getenv("KCONFIG_DEFCONFIG_LIST");
358 if (!env)
Roman Zippelface4372006-06-08 22:12:45 -0700359 return 1;
360
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900361 while (1) {
362 bool is_last;
363
364 while (isspace(*env))
365 env++;
366
367 if (!*env)
368 break;
369
370 p = env;
371 while (*p && !isspace(*p))
372 p++;
373
374 is_last = (*p == '\0');
375
376 *p = '\0';
377
378 in = zconf_fopen(env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200380 conf_message("using defaults found in %s",
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900381 env);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700382 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900384
385 if (is_last)
386 break;
387
388 env = p + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!in)
392 return 1;
393
Roman Zippelddc97ca2006-06-08 22:12:38 -0700394load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800395 conf_filename = name;
396 conf_lineno = 0;
397 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800398
Roman Zippel669bfad92006-06-08 22:12:42 -0700399 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700401 sym->flags |= SYMBOL_CHANGED;
402 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200403 if (sym_is_choice(sym))
404 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 switch (sym->type) {
406 case S_INT:
407 case S_HEX:
408 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700409 if (sym->def[def].val)
410 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400411 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700413 sym->def[def].val = NULL;
414 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 }
417
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700418 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800419 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400421 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400422 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400424 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (!p)
426 continue;
427 *p++ = 0;
428 if (strncmp(p, "is not set", 10))
429 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700430 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400431 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200432 if (!sym) {
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900433 conf_set_changed(true);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900434 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200435 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700436 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400437 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700438 if (sym->type == S_UNKNOWN)
439 sym->type = S_BOOLEAN;
440 }
441 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100442 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 switch (sym->type) {
445 case S_BOOLEAN:
446 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700447 sym->def[def].tri = no;
448 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 default:
451 ;
452 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400453 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
454 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!p)
456 continue;
457 *p++ = 0;
458 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600459 if (p2) {
460 *p2-- = 0;
461 if (*p2 == '\r')
462 *p2 = 0;
463 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900464
465 sym = sym_find(line + strlen(CONFIG_));
466 if (!sym) {
467 if (def == S_DEF_AUTO)
468 /*
469 * Reading from include/config/auto.conf
470 * If CONFIG_FOO previously existed in
471 * auto.conf but it is missing now,
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300472 * include/config/FOO must be touched.
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900473 */
474 conf_touch_dep(line + strlen(CONFIG_));
475 else
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900476 conf_set_changed(true);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900477 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700478 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900479
Roman Zippel669bfad92006-06-08 22:12:42 -0700480 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100481 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100483 if (conf_set_sym_val(sym, def, def_flags, p))
484 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400485 } else {
486 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100487 conf_warning("unexpected data: %.*s",
488 (int)strcspn(line, "\r\n"), line);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 continue;
491 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (sym && sym_is_choice_value(sym)) {
494 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700495 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 case no:
497 break;
498 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700499 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800500 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200501 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100505 if (cs->def[def].tri != no)
506 conf_warning("override: %s changes choice state", sym->name);
507 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100510 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700513 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800515 return 0;
516}
517
518int conf_read(const char *name)
519{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500520 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900521 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500522 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800523
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900524 conf_set_changed(false);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700525
Al Viro6b87b702016-01-14 18:13:49 +0000526 if (conf_read_simple(name, S_DEF_USER)) {
527 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800528 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000529 }
530
531 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 for_all_symbols(i, sym) {
534 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200535 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500536 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800537 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
538 /* check that calculated value agrees with saved value */
539 switch (sym->type) {
540 case S_BOOLEAN:
541 case S_TRISTATE:
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900542 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500543 continue;
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900544 break;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800545 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700546 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500547 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800548 break;
549 }
550 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
551 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500552 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800553 conf_unsaved++;
554 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Roman Zippeld8982ba2007-07-09 11:43:58 -0700557 for_all_symbols(i, sym) {
558 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
559 /* Reset values of generates values, so they'll appear
560 * as new, if they should become visible, but that
561 * doesn't quite work if the Kconfig and the saved
562 * configuration disagree.
563 */
564 if (sym->visible == no && !conf_unsaved)
565 sym->flags &= ~SYMBOL_DEF_USER;
566 switch (sym->type) {
567 case S_STRING:
568 case S_INT:
569 case S_HEX:
570 /* Reset a string value if it's out of range */
571 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
572 break;
573 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
574 conf_unsaved++;
575 break;
576 default:
577 break;
578 }
579 }
580 }
581
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900582 if (conf_warnings || conf_unsaved)
583 conf_set_changed(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 return 0;
586}
587
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900588struct comment_style {
589 const char *decoration;
590 const char *prefix;
591 const char *postfix;
592};
593
594static const struct comment_style comment_style_pound = {
595 .decoration = "#",
596 .prefix = "#",
597 .postfix = "#",
598};
599
600static const struct comment_style comment_style_c = {
601 .decoration = " *",
602 .prefix = "/*",
603 .postfix = " */",
604};
605
606static void conf_write_heading(FILE *fp, const struct comment_style *cs)
607{
608 fprintf(fp, "%s\n", cs->prefix);
609
610 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
611 cs->decoration);
612
613 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
614
615 fprintf(fp, "%s\n", cs->postfix);
616}
617
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900618/* The returned pointer must be freed on the caller side */
619static char *escape_string_value(const char *in)
620{
621 const char *p;
622 char *out;
623 size_t len;
624
625 len = strlen(in) + strlen("\"\"") + 1;
626
627 p = in;
628 while (1) {
629 p += strcspn(p, "\"\\");
630
631 if (p[0] == '\0')
632 break;
633
634 len++;
635 p++;
636 }
637
638 out = xmalloc(len);
639 out[0] = '\0';
640
641 strcat(out, "\"");
642
643 p = in;
644 while (1) {
645 len = strcspn(p, "\"\\");
646 strncat(out, p, len);
647 p += len;
648
649 if (p[0] == '\0')
650 break;
651
652 strcat(out, "\\");
653 strncat(out, p++, 1);
654 }
655
656 strcat(out, "\"");
657
658 return out;
659}
660
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400661/*
662 * Kconfig configuration printer
663 *
664 * This printer is used when generating the resulting configuration after
665 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
666 * passing a non-NULL argument to the printer.
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400667 */
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900668enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
Sam Ravnborg49192f22010-07-31 23:35:33 +0200669
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900670static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
671 bool escape_string)
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400672{
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900673 const char *val;
674 char *escaped = NULL;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400675
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900676 if (sym->type == S_UNKNOWN)
677 return;
678
679 val = sym_get_string_value(sym);
680
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900681 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
682 output_n != OUTPUT_N && *val == 'n') {
683 if (output_n == OUTPUT_N_AS_UNSET)
684 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
685 return;
686 }
687
688 if (sym->type == S_STRING && escape_string) {
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900689 escaped = escape_string_value(val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900690 val = escaped;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200691 }
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900692
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900693 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
694
695 free(escaped);
696}
697
698static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
699{
700 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
701}
702
703static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
704{
Masahiro Yamada129ab0d2021-12-14 11:53:53 +0900705 __print_symbol(fp, sym, OUTPUT_N_NONE, false);
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900706}
707
Masahiro Yamada51d792c2021-10-01 14:32:47 +0900708void print_symbol_for_listconfig(struct symbol *sym)
709{
710 __print_symbol(stdout, sym, OUTPUT_N, true);
711}
712
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900713static void print_symbol_for_c(FILE *fp, struct symbol *sym)
714{
715 const char *val;
716 const char *sym_suffix = "";
717 const char *val_prefix = "";
718 char *escaped = NULL;
719
720 if (sym->type == S_UNKNOWN)
721 return;
722
723 val = sym_get_string_value(sym);
724
725 switch (sym->type) {
726 case S_BOOLEAN:
727 case S_TRISTATE:
728 switch (*val) {
729 case 'n':
730 return;
731 case 'm':
732 sym_suffix = "_MODULE";
733 /* fall through */
734 default:
735 val = "1";
736 }
737 break;
738 case S_HEX:
739 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
740 val_prefix = "0x";
741 break;
742 case S_STRING:
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900743 escaped = escape_string_value(val);
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900744 val = escaped;
745 default:
746 break;
747 }
748
749 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
750 val_prefix, val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900751
752 free(escaped);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200753}
754
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200755/*
756 * Write out a minimal config.
757 * All values that has default values are skipped as this is redundant.
758 */
759int conf_write_defconfig(const char *filename)
760{
761 struct symbol *sym;
762 struct menu *menu;
763 FILE *out;
764
765 out = fopen(filename, "w");
766 if (!out)
767 return 1;
768
769 sym_clear_all_valid();
770
771 /* Traverse all menus to find all relevant symbols */
772 menu = rootmenu.list;
773
774 while (menu != NULL)
775 {
776 sym = menu->sym;
777 if (sym == NULL) {
778 if (!menu_is_visible(menu))
779 goto next_menu;
780 } else if (!sym_is_choice(sym)) {
781 sym_calc_value(sym);
782 if (!(sym->flags & SYMBOL_WRITE))
783 goto next_menu;
784 sym->flags &= ~SYMBOL_WRITE;
785 /* If we cannot change the symbol - skip */
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200786 if (!sym_is_changeable(sym))
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200787 goto next_menu;
788 /* If symbol equals to default value - skip */
789 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
790 goto next_menu;
791
792 /*
793 * If symbol is a choice value and equals to the
794 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200795 * But only if value is bool and equal to "y" and
796 * choice is not "optional".
797 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200798 */
799 if (sym_is_choice_value(sym)) {
800 struct symbol *cs;
801 struct symbol *ds;
802
803 cs = prop_get_symbol(sym_get_choice_prop(sym));
804 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200805 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200806 if ((sym->type == S_BOOLEAN) &&
807 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200808 goto next_menu;
809 }
810 }
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900811 print_symbol_for_dotconfig(out, sym);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200812 }
813next_menu:
814 if (menu->list != NULL) {
815 menu = menu->list;
816 }
817 else if (menu->next != NULL) {
818 menu = menu->next;
819 } else {
820 while ((menu = menu->parent)) {
821 if (menu->next != NULL) {
822 menu = menu->next;
823 break;
824 }
825 }
826 }
827 }
828 fclose(out);
829 return 0;
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832int conf_write(const char *name)
833{
Roman Zippelc955cca2006-06-08 22:12:39 -0700834 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct symbol *sym;
836 struct menu *menu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 const char *str;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900838 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 char *env;
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400840 int i;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300841 bool need_newline = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900843 if (!name)
844 name = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900846 if (!*name) {
847 fprintf(stderr, "config name is empty\n");
848 return -1;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900851 if (is_dir(name)) {
852 fprintf(stderr, "%s: Is a directory\n", name);
853 return -1;
854 }
855
Masahiro Yamada580c5b32019-05-11 01:56:01 +0900856 if (make_parent_dir(name))
857 return -1;
858
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700859 env = getenv("KCONFIG_OVERWRITECONFIG");
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900860 if (env && *env) {
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700861 *tmpname = 0;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900862 out = fopen(name, "w");
863 } else {
864 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
865 name, (int)getpid());
866 out = fopen(tmpname, "w");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (!out)
869 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700870
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900871 conf_write_heading(out, &comment_style_pound);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Karsten Wieseb3214292006-12-13 00:34:06 -0800873 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 sym_clear_all_valid();
875
876 menu = rootmenu.list;
877 while (menu) {
878 sym = menu->sym;
879 if (!sym) {
880 if (!menu_is_visible(menu))
881 goto next;
882 str = menu_get_prompt(menu);
883 fprintf(out, "\n"
884 "#\n"
885 "# %s\n"
886 "#\n", str);
Alexander Popovaff11cd2019-05-17 22:42:22 +0300887 need_newline = false;
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900888 } else if (!(sym->flags & SYMBOL_CHOICE) &&
889 !(sym->flags & SYMBOL_WRITTEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sym_calc_value(sym);
891 if (!(sym->flags & SYMBOL_WRITE))
892 goto next;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300893 if (need_newline) {
894 fprintf(out, "\n");
895 need_newline = false;
896 }
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900897 sym->flags |= SYMBOL_WRITTEN;
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900898 print_symbol_for_dotconfig(out, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
Sam Ravnborg49192f22010-07-31 23:35:33 +0200901next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (menu->list) {
903 menu = menu->list;
904 continue;
905 }
906 if (menu->next)
907 menu = menu->next;
908 else while ((menu = menu->parent)) {
Alexander Popovaff11cd2019-05-17 22:42:22 +0300909 if (!menu->sym && menu_is_visible(menu) &&
910 menu != &rootmenu) {
911 str = menu_get_prompt(menu);
912 fprintf(out, "# end of %s\n", str);
913 need_newline = true;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (menu->next) {
916 menu = menu->next;
917 break;
918 }
919 }
920 }
921 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700922
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400923 for_all_symbols(i, sym)
924 sym->flags &= ~SYMBOL_WRITTEN;
925
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700926 if (*tmpname) {
Masahiro Yamada67424f62019-05-10 15:12:05 +0900927 if (is_same(name, tmpname)) {
928 conf_message("No change to %s", name);
929 unlink(tmpname);
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900930 conf_set_changed(false);
Masahiro Yamada67424f62019-05-10 15:12:05 +0900931 return 0;
932 }
933
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900934 snprintf(oldname, sizeof(oldname), "%s.old", name);
935 rename(name, oldname);
936 if (rename(tmpname, name))
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900940 conf_message("configuration written to %s", name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700941
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900942 conf_set_changed(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return 0;
945}
Roman Zippelc955cca2006-06-08 22:12:39 -0700946
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900947/* write a dependency file as used by kbuild to track dependencies */
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900948static int conf_write_autoconf_cmd(const char *autoconf_name)
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900949{
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900950 char name[PATH_MAX], tmp[PATH_MAX];
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900951 struct file *file;
952 FILE *out;
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900953 int ret;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900954
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900955 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
956 if (ret >= sizeof(name)) /* check truncation */
957 return -1;
Masahiro Yamada79123b12018-07-20 16:46:29 +0900958
959 if (make_parent_dir(name))
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900960 return -1;
961
962 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
963 if (ret >= sizeof(tmp)) /* check truncation */
964 return -1;
965
966 out = fopen(tmp, "w");
967 if (!out) {
968 perror("fopen");
969 return -1;
970 }
971
972 fprintf(out, "deps_config := \\\n");
973 for (file = file_list; file; file = file->next)
974 fprintf(out, "\t%s \\\n", file->name);
975
976 fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
977
978 env_write_dep(out, autoconf_name);
979
980 fprintf(out, "\n$(deps_config): ;\n");
981
982 if (ferror(out)) /* error check for all fprintf() calls */
983 return -1;
984
985 fclose(out);
986
987 if (rename(tmp, name)) {
988 perror("rename");
989 return -1;
990 }
991
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900992 return 0;
993}
994
Masahiro Yamada0849d212018-11-30 18:15:49 +0900995static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700996{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200997 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700998 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900999 int res, i;
1000
1001 strcpy(depfile_path, "include/config/");
1002 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001003
Markus Heidelberg12122f62009-05-18 01:36:54 +02001004 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -07001005 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +00001006 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001007
Roman Zippel2e3646e2006-06-08 22:12:42 -07001008 for_all_symbols(i, sym) {
1009 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +02001010 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -07001011 continue;
1012 if (sym->flags & SYMBOL_WRITE) {
1013 if (sym->flags & SYMBOL_DEF_AUTO) {
1014 /*
1015 * symbol has old and new value,
1016 * so compare them...
1017 */
1018 switch (sym->type) {
1019 case S_BOOLEAN:
1020 case S_TRISTATE:
1021 if (sym_get_tristate_value(sym) ==
1022 sym->def[S_DEF_AUTO].tri)
1023 continue;
1024 break;
1025 case S_STRING:
1026 case S_HEX:
1027 case S_INT:
1028 if (!strcmp(sym_get_string_value(sym),
1029 sym->def[S_DEF_AUTO].val))
1030 continue;
1031 break;
1032 default:
1033 break;
1034 }
1035 } else {
1036 /*
1037 * If there is no old value, only 'no' (unset)
1038 * is allowed as new value.
1039 */
1040 switch (sym->type) {
1041 case S_BOOLEAN:
1042 case S_TRISTATE:
1043 if (sym_get_tristate_value(sym) == no)
1044 continue;
1045 break;
1046 default:
1047 break;
1048 }
1049 }
1050 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1051 /* There is neither an old nor a new value. */
1052 continue;
1053 /* else
1054 * There is an old value, but no new value ('no' (unset)
1055 * isn't saved in auto.conf, so the old value is always
1056 * different from 'no').
1057 */
1058
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001059 res = conf_touch_dep(sym->name);
1060 if (res)
1061 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001062 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001063
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001064 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001065}
1066
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001067static int __conf_write_autoconf(const char *filename,
1068 void (*print_symbol)(FILE *, struct symbol *),
1069 const struct comment_style *comment_style)
1070{
1071 char tmp[PATH_MAX];
1072 FILE *file;
1073 struct symbol *sym;
1074 int ret, i;
1075
1076 if (make_parent_dir(filename))
1077 return -1;
1078
1079 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1080 if (ret >= sizeof(tmp)) /* check truncation */
1081 return -1;
1082
1083 file = fopen(tmp, "w");
1084 if (!file) {
1085 perror("fopen");
1086 return -1;
1087 }
1088
1089 conf_write_heading(file, comment_style);
1090
1091 for_all_symbols(i, sym)
1092 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1093 print_symbol(file, sym);
1094
1095 /* check possible errors in conf_write_heading() and print_symbol() */
1096 if (ferror(file))
1097 return -1;
1098
1099 fclose(file);
1100
1101 if (rename(tmp, filename)) {
1102 perror("rename");
1103 return -1;
1104 }
1105
1106 return 0;
1107}
1108
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001109int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001110{
1111 struct symbol *sym;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001112 const char *autoconf_name = conf_get_autoconfig_name();
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001113 int ret, i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001114
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001115 if (!overwrite && is_present(autoconf_name))
1116 return 0;
1117
Masahiro Yamada00d674c2021-10-01 14:32:51 +09001118 ret = conf_write_autoconf_cmd(autoconf_name);
1119 if (ret)
1120 return -1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001121
Masahiro Yamada0849d212018-11-30 18:15:49 +09001122 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001123 return 1;
1124
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001125 for_all_symbols(i, sym)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001126 sym_calc_value(sym);
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001127
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001128 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1129 print_symbol_for_c,
1130 &comment_style_c);
1131 if (ret)
1132 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001133
Roman Zippelc955cca2006-06-08 22:12:39 -07001134 /*
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001135 * Create include/config/auto.conf. This must be the last step because
1136 * Kbuild has a dependency on auto.conf and this marks the successful
1137 * completion of the previous steps.
Roman Zippelc955cca2006-06-08 22:12:39 -07001138 */
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001139 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1140 print_symbol_for_autoconf,
1141 &comment_style_pound);
1142 if (ret)
1143 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001144
1145 return 0;
1146}
Karsten Wieseb3214292006-12-13 00:34:06 -08001147
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001148static bool conf_changed;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001149static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001150
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001151void conf_set_changed(bool val)
Karsten Wiesebfc10002006-12-13 00:34:07 -08001152{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001153 if (conf_changed_callback && conf_changed != val)
Karsten Wiese3b354c52006-12-13 00:34:08 -08001154 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001155
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001156 conf_changed = val;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001157}
1158
Karsten Wieseb3214292006-12-13 00:34:06 -08001159bool conf_get_changed(void)
1160{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001161 return conf_changed;
Karsten Wieseb3214292006-12-13 00:34:06 -08001162}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001163
1164void conf_set_changed_callback(void (*fn)(void))
1165{
1166 conf_changed_callback = fn;
1167}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001168
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001169void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001170{
1171 struct property *prop;
1172 struct symbol *sym;
1173 struct expr *e;
1174
1175 prop = sym_get_choice_prop(csym);
1176
1177 /*
1178 * Set all non-assinged choice values to no
1179 */
1180 expr_list_for_each_sym(prop->expr, e, sym) {
1181 if (!sym_has_value(sym))
1182 sym->def[S_DEF_USER].tri = no;
1183 }
1184 csym->flags |= SYMBOL_DEF_USER;
1185 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001186 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001187}