blob: 42bc56ee238c8f5f9318f5d6cd9d8f1b57ba2b8d [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:
247 if (*p++ != '"')
248 break;
249 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
250 if (*p2 == '"') {
251 *p2 = 0;
252 break;
253 }
254 memmove(p2, p2 + 1, strlen(p2));
255 }
256 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200257 if (def != S_DEF_AUTO)
258 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100259 return 1;
260 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400261 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100262 case S_INT:
263 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100264 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900265 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100266 sym->flags |= def_flags;
267 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200268 if (def != S_DEF_AUTO)
269 conf_warning("symbol value '%s' invalid for %s",
270 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100271 return 1;
272 }
273 break;
274 default:
275 ;
276 }
277 return 0;
278}
279
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700280#define LINE_GROWTH 16
281static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
282{
283 char *nline;
284 size_t new_size = slen + 1;
285 if (new_size > *n) {
286 new_size += LINE_GROWTH - 1;
287 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900288 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700289 if (!nline)
290 return -1;
291
292 *lineptr = nline;
293 *n = new_size;
294 }
295
296 (*lineptr)[slen] = c;
297
298 return 0;
299}
300
301static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
302{
303 char *line = *lineptr;
304 size_t slen = 0;
305
306 for (;;) {
307 int c = getc(stream);
308
309 switch (c) {
310 case '\n':
311 if (add_byte(c, &line, slen, n) < 0)
312 goto e_out;
313 slen++;
314 /* fall through */
315 case EOF:
316 if (add_byte('\0', &line, slen, n) < 0)
317 goto e_out;
318 *lineptr = line;
319 if (slen == 0)
320 return -1;
321 return slen;
322 default:
323 if (add_byte(c, &line, slen, n) < 0)
324 goto e_out;
325 slen++;
326 }
327 }
328
329e_out:
330 line[slen-1] = '\0';
331 *lineptr = line;
332 return -1;
333}
334
Roman Zippel669bfad92006-06-08 22:12:42 -0700335int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700338 char *line = NULL;
339 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700342 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 if (name) {
345 in = zconf_fopen(name);
346 } else {
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900347 char *env;
Roman Zippelface4372006-06-08 22:12:45 -0700348
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700349 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700350 in = zconf_fopen(name);
351 if (in)
352 goto load;
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900353 conf_set_changed(true);
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900354
355 env = getenv("KCONFIG_DEFCONFIG_LIST");
356 if (!env)
Roman Zippelface4372006-06-08 22:12:45 -0700357 return 1;
358
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900359 while (1) {
360 bool is_last;
361
362 while (isspace(*env))
363 env++;
364
365 if (!*env)
366 break;
367
368 p = env;
369 while (*p && !isspace(*p))
370 p++;
371
372 is_last = (*p == '\0');
373
374 *p = '\0';
375
376 in = zconf_fopen(env);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200378 conf_message("using defaults found in %s",
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900379 env);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700380 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
Masahiro Yamadab75b0a82021-03-14 04:48:32 +0900382
383 if (is_last)
384 break;
385
386 env = p + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!in)
390 return 1;
391
Roman Zippelddc97ca2006-06-08 22:12:38 -0700392load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800393 conf_filename = name;
394 conf_lineno = 0;
395 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800396
Roman Zippel669bfad92006-06-08 22:12:42 -0700397 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700399 sym->flags |= SYMBOL_CHANGED;
400 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200401 if (sym_is_choice(sym))
402 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 switch (sym->type) {
404 case S_INT:
405 case S_HEX:
406 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700407 if (sym->def[def].val)
408 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400409 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700411 sym->def[def].val = NULL;
412 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700416 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800417 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400419 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400420 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400422 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!p)
424 continue;
425 *p++ = 0;
426 if (strncmp(p, "is not set", 10))
427 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700428 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400429 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200430 if (!sym) {
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900431 conf_set_changed(true);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900432 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200433 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700434 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400435 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700436 if (sym->type == S_UNKNOWN)
437 sym->type = S_BOOLEAN;
438 }
439 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100440 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 switch (sym->type) {
443 case S_BOOLEAN:
444 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700445 sym->def[def].tri = no;
446 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 default:
449 ;
450 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400451 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
452 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!p)
454 continue;
455 *p++ = 0;
456 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600457 if (p2) {
458 *p2-- = 0;
459 if (*p2 == '\r')
460 *p2 = 0;
461 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900462
463 sym = sym_find(line + strlen(CONFIG_));
464 if (!sym) {
465 if (def == S_DEF_AUTO)
466 /*
467 * Reading from include/config/auto.conf
468 * If CONFIG_FOO previously existed in
469 * auto.conf but it is missing now,
Alexey Dobriyan0e0345b2021-04-15 20:36:07 +0300470 * include/config/FOO must be touched.
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900471 */
472 conf_touch_dep(line + strlen(CONFIG_));
473 else
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900474 conf_set_changed(true);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900475 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700476 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900477
Roman Zippel669bfad92006-06-08 22:12:42 -0700478 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100479 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100481 if (conf_set_sym_val(sym, def, def_flags, p))
482 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400483 } else {
484 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100485 conf_warning("unexpected data: %.*s",
486 (int)strcspn(line, "\r\n"), line);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 continue;
489 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (sym && sym_is_choice_value(sym)) {
492 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700493 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 case no:
495 break;
496 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700497 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800498 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200499 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100503 if (cs->def[def].tri != no)
504 conf_warning("override: %s changes choice state", sym->name);
505 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100508 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700511 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800513 return 0;
514}
515
516int conf_read(const char *name)
517{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500518 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900519 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500520 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800521
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900522 conf_set_changed(false);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700523
Al Viro6b87b702016-01-14 18:13:49 +0000524 if (conf_read_simple(name, S_DEF_USER)) {
525 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800526 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000527 }
528
529 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for_all_symbols(i, sym) {
532 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200533 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500534 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800535 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
536 /* check that calculated value agrees with saved value */
537 switch (sym->type) {
538 case S_BOOLEAN:
539 case S_TRISTATE:
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900540 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500541 continue;
Masahiro Yamadae3cd5132019-07-11 16:33:17 +0900542 break;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800543 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700544 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500545 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800546 break;
547 }
548 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
549 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500550 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800551 conf_unsaved++;
552 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Roman Zippeld8982ba2007-07-09 11:43:58 -0700555 for_all_symbols(i, sym) {
556 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
557 /* Reset values of generates values, so they'll appear
558 * as new, if they should become visible, but that
559 * doesn't quite work if the Kconfig and the saved
560 * configuration disagree.
561 */
562 if (sym->visible == no && !conf_unsaved)
563 sym->flags &= ~SYMBOL_DEF_USER;
564 switch (sym->type) {
565 case S_STRING:
566 case S_INT:
567 case S_HEX:
568 /* Reset a string value if it's out of range */
569 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
570 break;
571 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
572 conf_unsaved++;
573 break;
574 default:
575 break;
576 }
577 }
578 }
579
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900580 if (conf_warnings || conf_unsaved)
581 conf_set_changed(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 return 0;
584}
585
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900586struct comment_style {
587 const char *decoration;
588 const char *prefix;
589 const char *postfix;
590};
591
592static const struct comment_style comment_style_pound = {
593 .decoration = "#",
594 .prefix = "#",
595 .postfix = "#",
596};
597
598static const struct comment_style comment_style_c = {
599 .decoration = " *",
600 .prefix = "/*",
601 .postfix = " */",
602};
603
604static void conf_write_heading(FILE *fp, const struct comment_style *cs)
605{
606 fprintf(fp, "%s\n", cs->prefix);
607
608 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
609 cs->decoration);
610
611 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
612
613 fprintf(fp, "%s\n", cs->postfix);
614}
615
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900616/* The returned pointer must be freed on the caller side */
617static char *escape_string_value(const char *in)
618{
619 const char *p;
620 char *out;
621 size_t len;
622
623 len = strlen(in) + strlen("\"\"") + 1;
624
625 p = in;
626 while (1) {
627 p += strcspn(p, "\"\\");
628
629 if (p[0] == '\0')
630 break;
631
632 len++;
633 p++;
634 }
635
636 out = xmalloc(len);
637 out[0] = '\0';
638
639 strcat(out, "\"");
640
641 p = in;
642 while (1) {
643 len = strcspn(p, "\"\\");
644 strncat(out, p, len);
645 p += len;
646
647 if (p[0] == '\0')
648 break;
649
650 strcat(out, "\\");
651 strncat(out, p++, 1);
652 }
653
654 strcat(out, "\"");
655
656 return out;
657}
658
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400659/*
660 * Kconfig configuration printer
661 *
662 * This printer is used when generating the resulting configuration after
663 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
664 * passing a non-NULL argument to the printer.
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400665 */
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900666enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
Sam Ravnborg49192f22010-07-31 23:35:33 +0200667
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900668static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
669 bool escape_string)
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400670{
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900671 const char *val;
672 char *escaped = NULL;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400673
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900674 if (sym->type == S_UNKNOWN)
675 return;
676
677 val = sym_get_string_value(sym);
678
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900679 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
680 output_n != OUTPUT_N && *val == 'n') {
681 if (output_n == OUTPUT_N_AS_UNSET)
682 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
683 return;
684 }
685
686 if (sym->type == S_STRING && escape_string) {
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900687 escaped = escape_string_value(val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900688 val = escaped;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200689 }
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900690
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900691 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
692
693 free(escaped);
694}
695
696static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
697{
698 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
699}
700
701static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
702{
703 __print_symbol(fp, sym, OUTPUT_N_NONE, true);
704}
705
Masahiro Yamada51d792c2021-10-01 14:32:47 +0900706void print_symbol_for_listconfig(struct symbol *sym)
707{
708 __print_symbol(stdout, sym, OUTPUT_N, true);
709}
710
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900711static void print_symbol_for_c(FILE *fp, struct symbol *sym)
712{
713 const char *val;
714 const char *sym_suffix = "";
715 const char *val_prefix = "";
716 char *escaped = NULL;
717
718 if (sym->type == S_UNKNOWN)
719 return;
720
721 val = sym_get_string_value(sym);
722
723 switch (sym->type) {
724 case S_BOOLEAN:
725 case S_TRISTATE:
726 switch (*val) {
727 case 'n':
728 return;
729 case 'm':
730 sym_suffix = "_MODULE";
731 /* fall through */
732 default:
733 val = "1";
734 }
735 break;
736 case S_HEX:
737 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
738 val_prefix = "0x";
739 break;
740 case S_STRING:
Masahiro Yamada80f7bc72021-10-01 14:32:48 +0900741 escaped = escape_string_value(val);
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900742 val = escaped;
743 default:
744 break;
745 }
746
747 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
748 val_prefix, val);
Masahiro Yamada229d0cf2021-10-01 14:32:44 +0900749
750 free(escaped);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200751}
752
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200753/*
754 * Write out a minimal config.
755 * All values that has default values are skipped as this is redundant.
756 */
757int conf_write_defconfig(const char *filename)
758{
759 struct symbol *sym;
760 struct menu *menu;
761 FILE *out;
762
763 out = fopen(filename, "w");
764 if (!out)
765 return 1;
766
767 sym_clear_all_valid();
768
769 /* Traverse all menus to find all relevant symbols */
770 menu = rootmenu.list;
771
772 while (menu != NULL)
773 {
774 sym = menu->sym;
775 if (sym == NULL) {
776 if (!menu_is_visible(menu))
777 goto next_menu;
778 } else if (!sym_is_choice(sym)) {
779 sym_calc_value(sym);
780 if (!(sym->flags & SYMBOL_WRITE))
781 goto next_menu;
782 sym->flags &= ~SYMBOL_WRITE;
783 /* If we cannot change the symbol - skip */
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200784 if (!sym_is_changeable(sym))
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200785 goto next_menu;
786 /* If symbol equals to default value - skip */
787 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
788 goto next_menu;
789
790 /*
791 * If symbol is a choice value and equals to the
792 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200793 * But only if value is bool and equal to "y" and
794 * choice is not "optional".
795 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200796 */
797 if (sym_is_choice_value(sym)) {
798 struct symbol *cs;
799 struct symbol *ds;
800
801 cs = prop_get_symbol(sym_get_choice_prop(sym));
802 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200803 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200804 if ((sym->type == S_BOOLEAN) &&
805 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200806 goto next_menu;
807 }
808 }
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900809 print_symbol_for_dotconfig(out, sym);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200810 }
811next_menu:
812 if (menu->list != NULL) {
813 menu = menu->list;
814 }
815 else if (menu->next != NULL) {
816 menu = menu->next;
817 } else {
818 while ((menu = menu->parent)) {
819 if (menu->next != NULL) {
820 menu = menu->next;
821 break;
822 }
823 }
824 }
825 }
826 fclose(out);
827 return 0;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830int conf_write(const char *name)
831{
Roman Zippelc955cca2006-06-08 22:12:39 -0700832 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct symbol *sym;
834 struct menu *menu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 const char *str;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900836 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 char *env;
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400838 int i;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300839 bool need_newline = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900841 if (!name)
842 name = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900844 if (!*name) {
845 fprintf(stderr, "config name is empty\n");
846 return -1;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900849 if (is_dir(name)) {
850 fprintf(stderr, "%s: Is a directory\n", name);
851 return -1;
852 }
853
Masahiro Yamada580c5b32019-05-11 01:56:01 +0900854 if (make_parent_dir(name))
855 return -1;
856
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700857 env = getenv("KCONFIG_OVERWRITECONFIG");
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900858 if (env && *env) {
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700859 *tmpname = 0;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900860 out = fopen(name, "w");
861 } else {
862 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
863 name, (int)getpid());
864 out = fopen(tmpname, "w");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!out)
867 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700868
Masahiro Yamadaca51b262021-10-01 14:32:45 +0900869 conf_write_heading(out, &comment_style_pound);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Karsten Wieseb3214292006-12-13 00:34:06 -0800871 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 sym_clear_all_valid();
873
874 menu = rootmenu.list;
875 while (menu) {
876 sym = menu->sym;
877 if (!sym) {
878 if (!menu_is_visible(menu))
879 goto next;
880 str = menu_get_prompt(menu);
881 fprintf(out, "\n"
882 "#\n"
883 "# %s\n"
884 "#\n", str);
Alexander Popovaff11cd2019-05-17 22:42:22 +0300885 need_newline = false;
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900886 } else if (!(sym->flags & SYMBOL_CHOICE) &&
887 !(sym->flags & SYMBOL_WRITTEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 sym_calc_value(sym);
889 if (!(sym->flags & SYMBOL_WRITE))
890 goto next;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300891 if (need_newline) {
892 fprintf(out, "\n");
893 need_newline = false;
894 }
Masahiro Yamada8e2442a2019-07-12 15:07:09 +0900895 sym->flags |= SYMBOL_WRITTEN;
Masahiro Yamada6ce45a92021-10-01 14:32:46 +0900896 print_symbol_for_dotconfig(out, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Sam Ravnborg49192f22010-07-31 23:35:33 +0200899next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (menu->list) {
901 menu = menu->list;
902 continue;
903 }
904 if (menu->next)
905 menu = menu->next;
906 else while ((menu = menu->parent)) {
Alexander Popovaff11cd2019-05-17 22:42:22 +0300907 if (!menu->sym && menu_is_visible(menu) &&
908 menu != &rootmenu) {
909 str = menu_get_prompt(menu);
910 fprintf(out, "# end of %s\n", str);
911 need_newline = true;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (menu->next) {
914 menu = menu->next;
915 break;
916 }
917 }
918 }
919 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700920
M. Vefa Bicakci0c5b6c282019-08-03 06:02:12 -0400921 for_all_symbols(i, sym)
922 sym->flags &= ~SYMBOL_WRITTEN;
923
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700924 if (*tmpname) {
Masahiro Yamada67424f62019-05-10 15:12:05 +0900925 if (is_same(name, tmpname)) {
926 conf_message("No change to %s", name);
927 unlink(tmpname);
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900928 conf_set_changed(false);
Masahiro Yamada67424f62019-05-10 15:12:05 +0900929 return 0;
930 }
931
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900932 snprintf(oldname, sizeof(oldname), "%s.old", name);
933 rename(name, oldname);
934 if (rename(tmpname, name))
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700935 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900938 conf_message("configuration written to %s", name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700939
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900940 conf_set_changed(false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 return 0;
943}
Roman Zippelc955cca2006-06-08 22:12:39 -0700944
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900945/* write a dependency file as used by kbuild to track dependencies */
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900946static int conf_write_autoconf_cmd(const char *autoconf_name)
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900947{
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900948 char name[PATH_MAX], tmp[PATH_MAX];
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900949 struct file *file;
950 FILE *out;
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900951 int ret;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900952
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900953 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
954 if (ret >= sizeof(name)) /* check truncation */
955 return -1;
Masahiro Yamada79123b12018-07-20 16:46:29 +0900956
957 if (make_parent_dir(name))
Masahiro Yamada00d674c2021-10-01 14:32:51 +0900958 return -1;
959
960 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
961 if (ret >= sizeof(tmp)) /* check truncation */
962 return -1;
963
964 out = fopen(tmp, "w");
965 if (!out) {
966 perror("fopen");
967 return -1;
968 }
969
970 fprintf(out, "deps_config := \\\n");
971 for (file = file_list; file; file = file->next)
972 fprintf(out, "\t%s \\\n", file->name);
973
974 fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
975
976 env_write_dep(out, autoconf_name);
977
978 fprintf(out, "\n$(deps_config): ;\n");
979
980 if (ferror(out)) /* error check for all fprintf() calls */
981 return -1;
982
983 fclose(out);
984
985 if (rename(tmp, name)) {
986 perror("rename");
987 return -1;
988 }
989
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900990 return 0;
991}
992
Masahiro Yamada0849d212018-11-30 18:15:49 +0900993static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700994{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200995 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700996 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900997 int res, i;
998
999 strcpy(depfile_path, "include/config/");
1000 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001001
Markus Heidelberg12122f62009-05-18 01:36:54 +02001002 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -07001003 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +00001004 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -07001005
Roman Zippel2e3646e2006-06-08 22:12:42 -07001006 for_all_symbols(i, sym) {
1007 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +02001008 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -07001009 continue;
1010 if (sym->flags & SYMBOL_WRITE) {
1011 if (sym->flags & SYMBOL_DEF_AUTO) {
1012 /*
1013 * symbol has old and new value,
1014 * so compare them...
1015 */
1016 switch (sym->type) {
1017 case S_BOOLEAN:
1018 case S_TRISTATE:
1019 if (sym_get_tristate_value(sym) ==
1020 sym->def[S_DEF_AUTO].tri)
1021 continue;
1022 break;
1023 case S_STRING:
1024 case S_HEX:
1025 case S_INT:
1026 if (!strcmp(sym_get_string_value(sym),
1027 sym->def[S_DEF_AUTO].val))
1028 continue;
1029 break;
1030 default:
1031 break;
1032 }
1033 } else {
1034 /*
1035 * If there is no old value, only 'no' (unset)
1036 * is allowed as new value.
1037 */
1038 switch (sym->type) {
1039 case S_BOOLEAN:
1040 case S_TRISTATE:
1041 if (sym_get_tristate_value(sym) == no)
1042 continue;
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1049 /* There is neither an old nor a new value. */
1050 continue;
1051 /* else
1052 * There is an old value, but no new value ('no' (unset)
1053 * isn't saved in auto.conf, so the old value is always
1054 * different from 'no').
1055 */
1056
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001057 res = conf_touch_dep(sym->name);
1058 if (res)
1059 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001060 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001061
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001062 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001063}
1064
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001065static int __conf_write_autoconf(const char *filename,
1066 void (*print_symbol)(FILE *, struct symbol *),
1067 const struct comment_style *comment_style)
1068{
1069 char tmp[PATH_MAX];
1070 FILE *file;
1071 struct symbol *sym;
1072 int ret, i;
1073
1074 if (make_parent_dir(filename))
1075 return -1;
1076
1077 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1078 if (ret >= sizeof(tmp)) /* check truncation */
1079 return -1;
1080
1081 file = fopen(tmp, "w");
1082 if (!file) {
1083 perror("fopen");
1084 return -1;
1085 }
1086
1087 conf_write_heading(file, comment_style);
1088
1089 for_all_symbols(i, sym)
1090 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1091 print_symbol(file, sym);
1092
1093 /* check possible errors in conf_write_heading() and print_symbol() */
1094 if (ferror(file))
1095 return -1;
1096
1097 fclose(file);
1098
1099 if (rename(tmp, filename)) {
1100 perror("rename");
1101 return -1;
1102 }
1103
1104 return 0;
1105}
1106
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001107int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001108{
1109 struct symbol *sym;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001110 const char *autoconf_name = conf_get_autoconfig_name();
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001111 int ret, i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001112
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001113 if (!overwrite && is_present(autoconf_name))
1114 return 0;
1115
Masahiro Yamada00d674c2021-10-01 14:32:51 +09001116 ret = conf_write_autoconf_cmd(autoconf_name);
1117 if (ret)
1118 return -1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001119
Masahiro Yamada0849d212018-11-30 18:15:49 +09001120 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001121 return 1;
1122
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001123 for_all_symbols(i, sym)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001124 sym_calc_value(sym);
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001125
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001126 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1127 print_symbol_for_c,
1128 &comment_style_c);
1129 if (ret)
1130 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001131
Roman Zippelc955cca2006-06-08 22:12:39 -07001132 /*
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001133 * Create include/config/auto.conf. This must be the last step because
1134 * Kbuild has a dependency on auto.conf and this marks the successful
1135 * completion of the previous steps.
Roman Zippelc955cca2006-06-08 22:12:39 -07001136 */
Masahiro Yamada57ddd072021-10-01 14:32:50 +09001137 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1138 print_symbol_for_autoconf,
1139 &comment_style_pound);
1140 if (ret)
1141 return ret;
Roman Zippelc955cca2006-06-08 22:12:39 -07001142
1143 return 0;
1144}
Karsten Wieseb3214292006-12-13 00:34:06 -08001145
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001146static bool conf_changed;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001147static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001148
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001149void conf_set_changed(bool val)
Karsten Wiesebfc10002006-12-13 00:34:07 -08001150{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001151 if (conf_changed_callback && conf_changed != val)
Karsten Wiese3b354c52006-12-13 00:34:08 -08001152 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001153
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001154 conf_changed = val;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001155}
1156
Karsten Wieseb3214292006-12-13 00:34:06 -08001157bool conf_get_changed(void)
1158{
Masahiro Yamada5ee54652021-04-10 15:57:22 +09001159 return conf_changed;
Karsten Wieseb3214292006-12-13 00:34:06 -08001160}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001161
1162void conf_set_changed_callback(void (*fn)(void))
1163{
1164 conf_changed_callback = fn;
1165}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001166
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001167void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001168{
1169 struct property *prop;
1170 struct symbol *sym;
1171 struct expr *e;
1172
1173 prop = sym_get_choice_prop(csym);
1174
1175 /*
1176 * Set all non-assinged choice values to no
1177 */
1178 expr_list_for_each_sym(prop->expr, e, sym) {
1179 if (!sym_has_value(sym))
1180 sym->def[S_DEF_USER].tri = no;
1181 }
1182 csym->flags |= SYMBOL_DEF_USER;
1183 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001184 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001185}