blob: 6ac53bf7aa64927777f338361975f221bc55e6de [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{
133 int fd, ret;
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300134 char *d;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900135
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300136 /* check overflow: prefix + name + '\0' must fit in buffer. */
137 if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900138 return -1;
139
140 d = depfile_path + depfile_prefix_len;
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300141 strcpy(d, name);
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900142
143 /* Assume directory path already exists. */
144 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
145 if (fd == -1) {
146 if (errno != ENOENT)
147 return -1;
148
149 ret = make_parent_dir(depfile_path);
150 if (ret)
151 return ret;
152
153 /* Try it again. */
154 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
155 if (fd == -1)
156 return -1;
157 }
158 close(fd);
159
160 return 0;
161}
162
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800163static void conf_warning(const char *fmt, ...)
164 __attribute__ ((format (printf, 1, 2)));
165
Michal Marek42368c32010-08-17 10:21:19 +0200166static void conf_message(const char *fmt, ...)
167 __attribute__ ((format (printf, 1, 2)));
168
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800169static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900170static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800171
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800172static void conf_warning(const char *fmt, ...)
173{
174 va_list ap;
175 va_start(ap, fmt);
176 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
177 vfprintf(stderr, fmt, ap);
178 fprintf(stderr, "\n");
179 va_end(ap);
180 conf_warnings++;
181}
182
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900183static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +0200184{
185 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900186 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +0200187 printf("\n#\n");
188}
189
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900190static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +0200191 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900192void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +0200193{
194 conf_message_callback = fn;
195}
196
197static void conf_message(const char *fmt, ...)
198{
199 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900200 char buf[4096];
201
202 if (!conf_message_callback)
203 return;
Michal Marek42368c32010-08-17 10:21:19 +0200204
205 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900206
207 vsnprintf(buf, sizeof(buf), fmt, ap);
208 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +0000209 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +0200210}
211
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700212const char *conf_get_configname(void)
213{
214 char *name = getenv("KCONFIG_CONFIG");
215
216 return name ? name : ".config";
217}
218
Masahiro Yamada9b9f5942019-05-13 01:00:53 +0900219static const char *conf_get_autoconfig_name(void)
Markus Heidelberg12122f62009-05-18 01:36:54 +0200220{
221 char *name = getenv("KCONFIG_AUTOCONFIG");
222
223 return name ? name : "include/config/auto.conf";
224}
225
Masahiro Yamada8499f2d2021-10-01 14:32:49 +0900226static 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
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100233static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
234{
235 char *p2;
236
237 switch (sym->type) {
238 case S_TRISTATE:
239 if (p[0] == 'm') {
240 sym->def[def].tri = mod;
241 sym->flags |= def_flags;
242 break;
243 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400244 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100245 case S_BOOLEAN:
246 if (p[0] == 'y') {
247 sym->def[def].tri = yes;
248 sym->flags |= def_flags;
249 break;
250 }
251 if (p[0] == 'n') {
252 sym->def[def].tri = no;
253 sym->flags |= def_flags;
254 break;
255 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200256 if (def != S_DEF_AUTO)
257 conf_warning("symbol value '%s' invalid for %s",
258 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400259 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100260 case S_STRING:
261 if (*p++ != '"')
262 break;
263 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
264 if (*p2 == '"') {
265 *p2 = 0;
266 break;
267 }
268 memmove(p2, p2 + 1, strlen(p2));
269 }
270 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200271 if (def != S_DEF_AUTO)
272 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100273 return 1;
274 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400275 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100276 case S_INT:
277 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100278 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900279 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100280 sym->flags |= def_flags;
281 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200282 if (def != S_DEF_AUTO)
283 conf_warning("symbol value '%s' invalid for %s",
284 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100285 return 1;
286 }
287 break;
288 default:
289 ;
290 }
291 return 0;
292}
293
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700294#define LINE_GROWTH 16
295static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
296{
297 char *nline;
298 size_t new_size = slen + 1;
299 if (new_size > *n) {
300 new_size += LINE_GROWTH - 1;
301 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900302 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700303 if (!nline)
304 return -1;
305
306 *lineptr = nline;
307 *n = new_size;
308 }
309
310 (*lineptr)[slen] = c;
311
312 return 0;
313}
314
315static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
316{
317 char *line = *lineptr;
318 size_t slen = 0;
319
320 for (;;) {
321 int c = getc(stream);
322
323 switch (c) {
324 case '\n':
325 if (add_byte(c, &line, slen, n) < 0)
326 goto e_out;
327 slen++;
328 /* fall through */
329 case EOF:
330 if (add_byte('\0', &line, slen, n) < 0)
331 goto e_out;
332 *lineptr = line;
333 if (slen == 0)
334 return -1;
335 return slen;
336 default:
337 if (add_byte(c, &line, slen, n) < 0)
338 goto e_out;
339 slen++;
340 }
341 }
342
343e_out:
344 line[slen-1] = '\0';
345 *lineptr = line;
346 return -1;
347}
348
Roman Zippel669bfad92006-06-08 22:12:42 -0700349int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700352 char *line = NULL;
353 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700356 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (name) {
359 in = zconf_fopen(name);
360 } else {
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900361 char *env;
Roman Zippelface4372006-06-08 22:12:45 -0700362
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700363 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700364 in = zconf_fopen(name);
365 if (in)
366 goto load;
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900367 conf_set_changed(true);
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900368
369 env = getenv("KCONFIG_DEFCONFIG_LIST");
370 if (!env)
Roman Zippelface4372006-06-08 22:12:45 -0700371 return 1;
372
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900373 while (1) {
374 bool is_last;
375
376 while (isspace(*env))
377 env++;
378
379 if (!*env)
380 break;
381
382 p = env;
383 while (*p && !isspace(*p))
384 p++;
385
386 is_last = (*p == '\0');
387
388 *p = '\0';
389
390 in = zconf_fopen(env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200392 conf_message("using defaults found in %s",
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900393 env);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700394 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900396
397 if (is_last)
398 break;
399
400 env = p + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!in)
404 return 1;
405
Roman Zippelddc97ca2006-06-08 22:12:38 -0700406load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800407 conf_filename = name;
408 conf_lineno = 0;
409 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800410
Roman Zippel669bfad92006-06-08 22:12:42 -0700411 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700413 sym->flags |= SYMBOL_CHANGED;
414 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200415 if (sym_is_choice(sym))
416 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 switch (sym->type) {
418 case S_INT:
419 case S_HEX:
420 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700421 if (sym->def[def].val)
422 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400423 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700425 sym->def[def].val = NULL;
426 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
429
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700430 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800431 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400433 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400434 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400436 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!p)
438 continue;
439 *p++ = 0;
440 if (strncmp(p, "is not set", 10))
441 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700442 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400443 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200444 if (!sym) {
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900445 conf_set_changed(true);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900446 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200447 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700448 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400449 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700450 if (sym->type == S_UNKNOWN)
451 sym->type = S_BOOLEAN;
452 }
453 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100454 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 switch (sym->type) {
457 case S_BOOLEAN:
458 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700459 sym->def[def].tri = no;
460 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 default:
463 ;
464 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400465 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
466 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!p)
468 continue;
469 *p++ = 0;
470 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600471 if (p2) {
472 *p2-- = 0;
473 if (*p2 == '\r')
474 *p2 = 0;
475 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900476
477 sym = sym_find(line + strlen(CONFIG_));
478 if (!sym) {
479 if (def == S_DEF_AUTO)
480 /*
481 * Reading from include/config/auto.conf
482 * If CONFIG_FOO previously existed in
483 * auto.conf but it is missing now,
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300484 * include/config/FOO must be touched.
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900485 */
486 conf_touch_dep(line + strlen(CONFIG_));
487 else
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900488 conf_set_changed(true);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900489 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700490 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900491
Roman Zippel669bfad92006-06-08 22:12:42 -0700492 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100493 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100495 if (conf_set_sym_val(sym, def, def_flags, p))
496 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400497 } else {
498 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100499 conf_warning("unexpected data: %.*s",
500 (int)strcspn(line, "\r\n"), line);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 continue;
503 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (sym && sym_is_choice_value(sym)) {
506 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700507 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case no:
509 break;
510 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700511 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800512 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200513 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100517 if (cs->def[def].tri != no)
518 conf_warning("override: %s changes choice state", sym->name);
519 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100522 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700525 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800527 return 0;
528}
529
530int conf_read(const char *name)
531{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500532 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900533 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500534 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800535
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900536 conf_set_changed(false);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700537
Al Viro6b87b702016-01-14 18:13:49 +0000538 if (conf_read_simple(name, S_DEF_USER)) {
539 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800540 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000541 }
542
543 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 for_all_symbols(i, sym) {
546 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200547 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500548 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800549 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
550 /* check that calculated value agrees with saved value */
551 switch (sym->type) {
552 case S_BOOLEAN:
553 case S_TRISTATE:
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900554 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500555 continue;
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900556 break;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800557 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700558 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500559 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800560 break;
561 }
562 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
563 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500564 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800565 conf_unsaved++;
566 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Roman Zippeld8982ba2007-07-09 11:43:58 -0700569 for_all_symbols(i, sym) {
570 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
571 /* Reset values of generates values, so they'll appear
572 * as new, if they should become visible, but that
573 * doesn't quite work if the Kconfig and the saved
574 * configuration disagree.
575 */
576 if (sym->visible == no && !conf_unsaved)
577 sym->flags &= ~SYMBOL_DEF_USER;
578 switch (sym->type) {
579 case S_STRING:
580 case S_INT:
581 case S_HEX:
582 /* Reset a string value if it's out of range */
583 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
584 break;
585 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
586 conf_unsaved++;
587 break;
588 default:
589 break;
590 }
591 }
592 }
593
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900594 if (conf_warnings || conf_unsaved)
595 conf_set_changed(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 return 0;
598}
599
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900600struct comment_style {
601 const char *decoration;
602 const char *prefix;
603 const char *postfix;
604};
605
606static const struct comment_style comment_style_pound = {
607 .decoration = "#",
608 .prefix = "#",
609 .postfix = "#",
610};
611
612static const struct comment_style comment_style_c = {
613 .decoration = " *",
614 .prefix = "/*",
615 .postfix = " */",
616};
617
618static void conf_write_heading(FILE *fp, const struct comment_style *cs)
619{
620 fprintf(fp, "%s\n", cs->prefix);
621
622 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
623 cs->decoration);
624
625 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
626
627 fprintf(fp, "%s\n", cs->postfix);
628}
629
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900630/* The returned pointer must be freed on the caller side */
631static char *escape_string_value(const char *in)
632{
633 const char *p;
634 char *out;
635 size_t len;
636
637 len = strlen(in) + strlen("\"\"") + 1;
638
639 p = in;
640 while (1) {
641 p += strcspn(p, "\"\\");
642
643 if (p[0] == '\0')
644 break;
645
646 len++;
647 p++;
648 }
649
650 out = xmalloc(len);
651 out[0] = '\0';
652
653 strcat(out, "\"");
654
655 p = in;
656 while (1) {
657 len = strcspn(p, "\"\\");
658 strncat(out, p, len);
659 p += len;
660
661 if (p[0] == '\0')
662 break;
663
664 strcat(out, "\\");
665 strncat(out, p++, 1);
666 }
667
668 strcat(out, "\"");
669
670 return out;
671}
672
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400673/*
674 * Kconfig configuration printer
675 *
676 * This printer is used when generating the resulting configuration after
677 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
678 * passing a non-NULL argument to the printer.
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400679 */
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900680enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
Sam Ravnborg49192f22010-07-31 23:35:33 +0200681
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900682static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
683 bool escape_string)
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400684{
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900685 const char *val;
686 char *escaped = NULL;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400687
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900688 if (sym->type == S_UNKNOWN)
689 return;
690
691 val = sym_get_string_value(sym);
692
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900693 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
694 output_n != OUTPUT_N && *val == 'n') {
695 if (output_n == OUTPUT_N_AS_UNSET)
696 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
697 return;
698 }
699
700 if (sym->type == S_STRING && escape_string) {
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900701 escaped = escape_string_value(val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900702 val = escaped;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200703 }
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900704
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900705 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
706
707 free(escaped);
708}
709
710static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
711{
712 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
713}
714
715static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
716{
717 __print_symbol(fp, sym, OUTPUT_N_NONE, true);
718}
719
Masahiro Yamada51d792c2021-10-01 14:32:47 +0900720void print_symbol_for_listconfig(struct symbol *sym)
721{
722 __print_symbol(stdout, sym, OUTPUT_N, true);
723}
724
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900725static void print_symbol_for_c(FILE *fp, struct symbol *sym)
726{
727 const char *val;
728 const char *sym_suffix = "";
729 const char *val_prefix = "";
730 char *escaped = NULL;
731
732 if (sym->type == S_UNKNOWN)
733 return;
734
735 val = sym_get_string_value(sym);
736
737 switch (sym->type) {
738 case S_BOOLEAN:
739 case S_TRISTATE:
740 switch (*val) {
741 case 'n':
742 return;
743 case 'm':
744 sym_suffix = "_MODULE";
745 /* fall through */
746 default:
747 val = "1";
748 }
749 break;
750 case S_HEX:
751 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
752 val_prefix = "0x";
753 break;
754 case S_STRING:
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900755 escaped = escape_string_value(val);
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900756 val = escaped;
757 default:
758 break;
759 }
760
761 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
762 val_prefix, val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900763
764 free(escaped);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200765}
766
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200767/*
768 * Write out a minimal config.
769 * All values that has default values are skipped as this is redundant.
770 */
771int conf_write_defconfig(const char *filename)
772{
773 struct symbol *sym;
774 struct menu *menu;
775 FILE *out;
776
777 out = fopen(filename, "w");
778 if (!out)
779 return 1;
780
781 sym_clear_all_valid();
782
783 /* Traverse all menus to find all relevant symbols */
784 menu = rootmenu.list;
785
786 while (menu != NULL)
787 {
788 sym = menu->sym;
789 if (sym == NULL) {
790 if (!menu_is_visible(menu))
791 goto next_menu;
792 } else if (!sym_is_choice(sym)) {
793 sym_calc_value(sym);
794 if (!(sym->flags & SYMBOL_WRITE))
795 goto next_menu;
796 sym->flags &= ~SYMBOL_WRITE;
797 /* If we cannot change the symbol - skip */
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200798 if (!sym_is_changeable(sym))
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200799 goto next_menu;
800 /* If symbol equals to default value - skip */
801 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
802 goto next_menu;
803
804 /*
805 * If symbol is a choice value and equals to the
806 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200807 * But only if value is bool and equal to "y" and
808 * choice is not "optional".
809 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200810 */
811 if (sym_is_choice_value(sym)) {
812 struct symbol *cs;
813 struct symbol *ds;
814
815 cs = prop_get_symbol(sym_get_choice_prop(sym));
816 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200817 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200818 if ((sym->type == S_BOOLEAN) &&
819 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200820 goto next_menu;
821 }
822 }
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900823 print_symbol_for_dotconfig(out, sym);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200824 }
825next_menu:
826 if (menu->list != NULL) {
827 menu = menu->list;
828 }
829 else if (menu->next != NULL) {
830 menu = menu->next;
831 } else {
832 while ((menu = menu->parent)) {
833 if (menu->next != NULL) {
834 menu = menu->next;
835 break;
836 }
837 }
838 }
839 }
840 fclose(out);
841 return 0;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844int conf_write(const char *name)
845{
Roman Zippelc955cca2006-06-08 22:12:39 -0700846 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct symbol *sym;
848 struct menu *menu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 const char *str;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900850 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char *env;
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400852 int i;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300853 bool need_newline = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900855 if (!name)
856 name = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900858 if (!*name) {
859 fprintf(stderr, "config name is empty\n");
860 return -1;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900863 if (is_dir(name)) {
864 fprintf(stderr, "%s: Is a directory\n", name);
865 return -1;
866 }
867
Masahiro Yamada580c5b32019-05-11 01:56:01 +0900868 if (make_parent_dir(name))
869 return -1;
870
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700871 env = getenv("KCONFIG_OVERWRITECONFIG");
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900872 if (env && *env) {
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700873 *tmpname = 0;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900874 out = fopen(name, "w");
875 } else {
876 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
877 name, (int)getpid());
878 out = fopen(tmpname, "w");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!out)
881 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700882
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900883 conf_write_heading(out, &comment_style_pound);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Karsten Wieseb3214292006-12-13 00:34:06 -0800885 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sym_clear_all_valid();
887
888 menu = rootmenu.list;
889 while (menu) {
890 sym = menu->sym;
891 if (!sym) {
892 if (!menu_is_visible(menu))
893 goto next;
894 str = menu_get_prompt(menu);
895 fprintf(out, "\n"
896 "#\n"
897 "# %s\n"
898 "#\n", str);
Alexander Popovaff11cd2019-05-17 22:42:22 +0300899 need_newline = false;
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900900 } else if (!(sym->flags & SYMBOL_CHOICE) &&
901 !(sym->flags & SYMBOL_WRITTEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sym_calc_value(sym);
903 if (!(sym->flags & SYMBOL_WRITE))
904 goto next;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300905 if (need_newline) {
906 fprintf(out, "\n");
907 need_newline = false;
908 }
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900909 sym->flags |= SYMBOL_WRITTEN;
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900910 print_symbol_for_dotconfig(out, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
Sam Ravnborg49192f22010-07-31 23:35:33 +0200913next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (menu->list) {
915 menu = menu->list;
916 continue;
917 }
918 if (menu->next)
919 menu = menu->next;
920 else while ((menu = menu->parent)) {
Alexander Popovaff11cd2019-05-17 22:42:22 +0300921 if (!menu->sym && menu_is_visible(menu) &&
922 menu != &rootmenu) {
923 str = menu_get_prompt(menu);
924 fprintf(out, "# end of %s\n", str);
925 need_newline = true;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (menu->next) {
928 menu = menu->next;
929 break;
930 }
931 }
932 }
933 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700934
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400935 for_all_symbols(i, sym)
936 sym->flags &= ~SYMBOL_WRITTEN;
937
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700938 if (*tmpname) {
Masahiro Yamada67424f62019-05-10 15:12:05 +0900939 if (is_same(name, tmpname)) {
940 conf_message("No change to %s", name);
941 unlink(tmpname);
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900942 conf_set_changed(false);
Masahiro Yamada67424f62019-05-10 15:12:05 +0900943 return 0;
944 }
945
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900946 snprintf(oldname, sizeof(oldname), "%s.old", name);
947 rename(name, oldname);
948 if (rename(tmpname, name))
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700949 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900952 conf_message("configuration written to %s", name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700953
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900954 conf_set_changed(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return 0;
957}
Roman Zippelc955cca2006-06-08 22:12:39 -0700958
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900959/* write a dependency file as used by kbuild to track dependencies */
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900960static int conf_write_autoconf_cmd(const char *autoconf_name)
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900961{
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900962 char name[PATH_MAX], tmp[PATH_MAX];
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900963 struct file *file;
964 FILE *out;
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900965 int ret;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900966
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900967 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
968 if (ret >= sizeof(name)) /* check truncation */
969 return -1;
Masahiro Yamada79123b12018-07-20 16:46:29 +0900970
971 if (make_parent_dir(name))
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900972 return -1;
973
974 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
975 if (ret >= sizeof(tmp)) /* check truncation */
976 return -1;
977
978 out = fopen(tmp, "w");
979 if (!out) {
980 perror("fopen");
981 return -1;
982 }
983
984 fprintf(out, "deps_config := \\\n");
985 for (file = file_list; file; file = file->next)
986 fprintf(out, "\t%s \\\n", file->name);
987
988 fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
989
990 env_write_dep(out, autoconf_name);
991
992 fprintf(out, "\n$(deps_config): ;\n");
993
994 if (ferror(out)) /* error check for all fprintf() calls */
995 return -1;
996
997 fclose(out);
998
999 if (rename(tmp, name)) {
1000 perror("rename");
1001 return -1;
1002 }
1003
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001004 return 0;
1005}
1006
Masahiro Yamada0849d212018-11-30 18:15:49 +09001007static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -07001008{
Markus Heidelberg12122f62009-05-18 01:36:54 +02001009 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001010 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001011 int res, i;
1012
1013 strcpy(depfile_path, "include/config/");
1014 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001015
Markus Heidelberg12122f62009-05-18 01:36:54 +02001016 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -07001017 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +00001018 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001019
Roman Zippel2e3646e2006-06-08 22:12:42 -07001020 for_all_symbols(i, sym) {
1021 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +02001022 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -07001023 continue;
1024 if (sym->flags & SYMBOL_WRITE) {
1025 if (sym->flags & SYMBOL_DEF_AUTO) {
1026 /*
1027 * symbol has old and new value,
1028 * so compare them...
1029 */
1030 switch (sym->type) {
1031 case S_BOOLEAN:
1032 case S_TRISTATE:
1033 if (sym_get_tristate_value(sym) ==
1034 sym->def[S_DEF_AUTO].tri)
1035 continue;
1036 break;
1037 case S_STRING:
1038 case S_HEX:
1039 case S_INT:
1040 if (!strcmp(sym_get_string_value(sym),
1041 sym->def[S_DEF_AUTO].val))
1042 continue;
1043 break;
1044 default:
1045 break;
1046 }
1047 } else {
1048 /*
1049 * If there is no old value, only 'no' (unset)
1050 * is allowed as new value.
1051 */
1052 switch (sym->type) {
1053 case S_BOOLEAN:
1054 case S_TRISTATE:
1055 if (sym_get_tristate_value(sym) == no)
1056 continue;
1057 break;
1058 default:
1059 break;
1060 }
1061 }
1062 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1063 /* There is neither an old nor a new value. */
1064 continue;
1065 /* else
1066 * There is an old value, but no new value ('no' (unset)
1067 * isn't saved in auto.conf, so the old value is always
1068 * different from 'no').
1069 */
1070
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001071 res = conf_touch_dep(sym->name);
1072 if (res)
1073 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001074 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001075
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001076 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001077}
1078
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001079static int __conf_write_autoconf(const char *filename,
1080 void (*print_symbol)(FILE *, struct symbol *),
1081 const struct comment_style *comment_style)
1082{
1083 char tmp[PATH_MAX];
1084 FILE *file;
1085 struct symbol *sym;
1086 int ret, i;
1087
1088 if (make_parent_dir(filename))
1089 return -1;
1090
1091 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1092 if (ret >= sizeof(tmp)) /* check truncation */
1093 return -1;
1094
1095 file = fopen(tmp, "w");
1096 if (!file) {
1097 perror("fopen");
1098 return -1;
1099 }
1100
1101 conf_write_heading(file, comment_style);
1102
1103 for_all_symbols(i, sym)
1104 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1105 print_symbol(file, sym);
1106
1107 /* check possible errors in conf_write_heading() and print_symbol() */
1108 if (ferror(file))
1109 return -1;
1110
1111 fclose(file);
1112
1113 if (rename(tmp, filename)) {
1114 perror("rename");
1115 return -1;
1116 }
1117
1118 return 0;
1119}
1120
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001121int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001122{
1123 struct symbol *sym;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001124 const char *autoconf_name = conf_get_autoconfig_name();
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001125 int ret, i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001126
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001127 if (!overwrite && is_present(autoconf_name))
1128 return 0;
1129
Masahiro Yamada00d674c2021-10-01 14:32:51 +09001130 ret = conf_write_autoconf_cmd(autoconf_name);
1131 if (ret)
1132 return -1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001133
Masahiro Yamada0849d212018-11-30 18:15:49 +09001134 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001135 return 1;
1136
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001137 for_all_symbols(i, sym)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001138 sym_calc_value(sym);
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001139
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001140 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1141 print_symbol_for_c,
1142 &comment_style_c);
1143 if (ret)
1144 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001145
Roman Zippelc955cca2006-06-08 22:12:39 -07001146 /*
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001147 * Create include/config/auto.conf. This must be the last step because
1148 * Kbuild has a dependency on auto.conf and this marks the successful
1149 * completion of the previous steps.
Roman Zippelc955cca2006-06-08 22:12:39 -07001150 */
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001151 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1152 print_symbol_for_autoconf,
1153 &comment_style_pound);
1154 if (ret)
1155 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001156
1157 return 0;
1158}
Karsten Wieseb3214292006-12-13 00:34:06 -08001159
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001160static bool conf_changed;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001161static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001162
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001163void conf_set_changed(bool val)
Karsten Wiesebfc10002006-12-13 00:34:07 -08001164{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001165 if (conf_changed_callback && conf_changed != val)
Karsten Wiese3b354c52006-12-13 00:34:08 -08001166 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001167
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001168 conf_changed = val;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001169}
1170
Karsten Wieseb3214292006-12-13 00:34:06 -08001171bool conf_get_changed(void)
1172{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001173 return conf_changed;
Karsten Wieseb3214292006-12-13 00:34:06 -08001174}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001175
1176void conf_set_changed_callback(void (*fn)(void))
1177{
1178 conf_changed_callback = fn;
1179}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001180
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001181void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001182{
1183 struct property *prop;
1184 struct symbol *sym;
1185 struct expr *e;
1186
1187 prop = sym_get_choice_prop(csym);
1188
1189 /*
1190 * Set all non-assinged choice values to no
1191 */
1192 expr_list_for_each_sym(prop->expr, e, sym) {
1193 if (!sym_has_value(sym))
1194 sym->def[S_DEF_USER].tri = no;
1195 }
1196 csym->flags |= SYMBOL_DEF_USER;
1197 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001198 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001199}