blob: caab7336abc1f654175b077b2158914a2c30f2f8 [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>
8#include <ctype.h>
Arnaud Lacombe94bedec2010-08-17 01:40:20 -04009#include <errno.h>
Roman Zippel2e3646e2006-06-08 22:12:42 -070010#include <fcntl.h>
Masahiro Yamada558e78e2018-12-21 17:33:04 +090011#include <limits.h>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040012#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <time.h>
17#include <unistd.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "lkc.h"
20
Masahiro Yamada06081822018-07-20 16:46:27 +090021/* return true if 'path' exists, false otherwise */
22static bool is_present(const char *path)
23{
24 struct stat st;
25
26 return !stat(path, &st);
27}
28
29/* return true if 'path' exists and it is a directory, false otherwise */
30static bool is_dir(const char *path)
31{
32 struct stat st;
33
34 if (stat(path, &st))
35 return 0;
36
37 return S_ISDIR(st.st_mode);
38}
39
Masahiro Yamada67424f62019-05-10 15:12:05 +090040/* return true if the given two files are the same, false otherwise */
41static bool is_same(const char *file1, const char *file2)
42{
43 int fd1, fd2;
44 struct stat st1, st2;
45 void *map1, *map2;
46 bool ret = false;
47
48 fd1 = open(file1, O_RDONLY);
49 if (fd1 < 0)
50 return ret;
51
52 fd2 = open(file2, O_RDONLY);
53 if (fd2 < 0)
54 goto close1;
55
56 ret = fstat(fd1, &st1);
57 if (ret)
58 goto close2;
59 ret = fstat(fd2, &st2);
60 if (ret)
61 goto close2;
62
63 if (st1.st_size != st2.st_size)
64 goto close2;
65
66 map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
67 if (map1 == MAP_FAILED)
68 goto close2;
69
70 map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
71 if (map2 == MAP_FAILED)
72 goto close2;
73
74 if (bcmp(map1, map2, st1.st_size))
75 goto close2;
76
77 ret = true;
78close2:
79 close(fd2);
80close1:
81 close(fd1);
82
83 return ret;
84}
85
Masahiro Yamada06081822018-07-20 16:46:27 +090086/*
87 * Create the parent directory of the given path.
88 *
89 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
90 */
91static int make_parent_dir(const char *path)
92{
93 char tmp[PATH_MAX + 1];
94 char *p;
95
96 strncpy(tmp, path, sizeof(tmp));
97 tmp[sizeof(tmp) - 1] = 0;
98
99 /* Remove the base name. Just return if nothing is left */
100 p = strrchr(tmp, '/');
101 if (!p)
102 return 0;
103 *(p + 1) = 0;
104
105 /* Just in case it is an absolute path */
106 p = tmp;
107 while (*p == '/')
108 p++;
109
110 while ((p = strchr(p, '/'))) {
111 *p = 0;
112
113 /* skip if the directory exists */
114 if (!is_dir(tmp) && mkdir(tmp, 0755))
115 return -1;
116
117 *p = '/';
118 while (*p == '/')
119 p++;
120 }
121
122 return 0;
123}
124
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900125static char depfile_path[PATH_MAX];
126static size_t depfile_prefix_len;
127
128/* touch depfile for symbol 'name' */
129static int conf_touch_dep(const char *name)
130{
131 int fd, ret;
132 const char *s;
133 char *d, c;
134
135 /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
136 if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
137 return -1;
138
139 d = depfile_path + depfile_prefix_len;
140 s = name;
141
142 while ((c = *s++))
143 *d++ = (c == '_') ? '/' : tolower(c);
144 strcpy(d, ".h");
145
146 /* Assume directory path already exists. */
147 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
148 if (fd == -1) {
149 if (errno != ENOENT)
150 return -1;
151
152 ret = make_parent_dir(depfile_path);
153 if (ret)
154 return ret;
155
156 /* Try it again. */
157 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
158 if (fd == -1)
159 return -1;
160 }
161 close(fd);
162
163 return 0;
164}
165
Michal Marekad8d40c2015-02-24 16:37:13 +0100166struct conf_printer {
167 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
168 void (*print_comment)(FILE *, const char *, void *);
169};
170
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800171static void conf_warning(const char *fmt, ...)
172 __attribute__ ((format (printf, 1, 2)));
173
Michal Marek42368c32010-08-17 10:21:19 +0200174static void conf_message(const char *fmt, ...)
175 __attribute__ ((format (printf, 1, 2)));
176
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800177static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900178static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800179
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800180static void conf_warning(const char *fmt, ...)
181{
182 va_list ap;
183 va_start(ap, fmt);
184 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
185 vfprintf(stderr, fmt, ap);
186 fprintf(stderr, "\n");
187 va_end(ap);
188 conf_warnings++;
189}
190
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900191static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +0200192{
193 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900194 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +0200195 printf("\n#\n");
196}
197
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900198static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +0200199 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900200void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +0200201{
202 conf_message_callback = fn;
203}
204
205static void conf_message(const char *fmt, ...)
206{
207 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900208 char buf[4096];
209
210 if (!conf_message_callback)
211 return;
Michal Marek42368c32010-08-17 10:21:19 +0200212
213 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900214
215 vsnprintf(buf, sizeof(buf), fmt, ap);
216 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +0000217 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +0200218}
219
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700220const char *conf_get_configname(void)
221{
222 char *name = getenv("KCONFIG_CONFIG");
223
224 return name ? name : ".config";
225}
226
Masahiro Yamada9b9f5942019-05-13 01:00:53 +0900227static const char *conf_get_autoconfig_name(void)
Markus Heidelberg12122f62009-05-18 01:36:54 +0200228{
229 char *name = getenv("KCONFIG_AUTOCONFIG");
230
231 return name ? name : "include/config/auto.conf";
232}
233
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100234static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
235{
236 char *p2;
237
238 switch (sym->type) {
239 case S_TRISTATE:
240 if (p[0] == 'm') {
241 sym->def[def].tri = mod;
242 sym->flags |= def_flags;
243 break;
244 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400245 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100246 case S_BOOLEAN:
247 if (p[0] == 'y') {
248 sym->def[def].tri = yes;
249 sym->flags |= def_flags;
250 break;
251 }
252 if (p[0] == 'n') {
253 sym->def[def].tri = no;
254 sym->flags |= def_flags;
255 break;
256 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200257 if (def != S_DEF_AUTO)
258 conf_warning("symbol value '%s' invalid for %s",
259 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400260 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100261 case S_STRING:
262 if (*p++ != '"')
263 break;
264 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
265 if (*p2 == '"') {
266 *p2 = 0;
267 break;
268 }
269 memmove(p2, p2 + 1, strlen(p2));
270 }
271 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200272 if (def != S_DEF_AUTO)
273 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100274 return 1;
275 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400276 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100277 case S_INT:
278 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100279 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900280 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100281 sym->flags |= def_flags;
282 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200283 if (def != S_DEF_AUTO)
284 conf_warning("symbol value '%s' invalid for %s",
285 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100286 return 1;
287 }
288 break;
289 default:
290 ;
291 }
292 return 0;
293}
294
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700295#define LINE_GROWTH 16
296static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
297{
298 char *nline;
299 size_t new_size = slen + 1;
300 if (new_size > *n) {
301 new_size += LINE_GROWTH - 1;
302 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900303 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700304 if (!nline)
305 return -1;
306
307 *lineptr = nline;
308 *n = new_size;
309 }
310
311 (*lineptr)[slen] = c;
312
313 return 0;
314}
315
316static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
317{
318 char *line = *lineptr;
319 size_t slen = 0;
320
321 for (;;) {
322 int c = getc(stream);
323
324 switch (c) {
325 case '\n':
326 if (add_byte(c, &line, slen, n) < 0)
327 goto e_out;
328 slen++;
329 /* fall through */
330 case EOF:
331 if (add_byte('\0', &line, slen, n) < 0)
332 goto e_out;
333 *lineptr = line;
334 if (slen == 0)
335 return -1;
336 return slen;
337 default:
338 if (add_byte(c, &line, slen, n) < 0)
339 goto e_out;
340 slen++;
341 }
342 }
343
344e_out:
345 line[slen-1] = '\0';
346 *lineptr = line;
347 return -1;
348}
349
Roman Zippel669bfad92006-06-08 22:12:42 -0700350int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700353 char *line = NULL;
354 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700357 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (name) {
360 in = zconf_fopen(name);
361 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700362 struct property *prop;
363
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700364 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700365 in = zconf_fopen(name);
366 if (in)
367 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800368 sym_add_change_count(1);
Al Viro6b87b702016-01-14 18:13:49 +0000369 if (!sym_defconfig_list)
Roman Zippelface4372006-06-08 22:12:45 -0700370 return 1;
371
372 for_all_defaults(sym_defconfig_list, prop) {
373 if (expr_calc_value(prop->visible.expr) == no ||
374 prop->expr->type != E_SYMBOL)
375 continue;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900376 sym_calc_value(prop->expr->left.sym);
377 name = sym_get_string_value(prop->expr->left.sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 in = zconf_fopen(name);
379 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200380 conf_message("using defaults found in %s",
Michal Marek42368c32010-08-17 10:21:19 +0200381 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700382 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!in)
387 return 1;
388
Roman Zippelddc97ca2006-06-08 22:12:38 -0700389load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800390 conf_filename = name;
391 conf_lineno = 0;
392 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800393
Roman Zippel669bfad92006-06-08 22:12:42 -0700394 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700396 sym->flags |= SYMBOL_CHANGED;
397 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200398 if (sym_is_choice(sym))
399 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 switch (sym->type) {
401 case S_INT:
402 case S_HEX:
403 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700404 if (sym->def[def].val)
405 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400406 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700408 sym->def[def].val = NULL;
409 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
412
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700413 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800414 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400416 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400417 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400419 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!p)
421 continue;
422 *p++ = 0;
423 if (strncmp(p, "is not set", 10))
424 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700425 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400426 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200427 if (!sym) {
428 sym_add_change_count(1);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900429 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200430 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700431 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400432 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700433 if (sym->type == S_UNKNOWN)
434 sym->type = S_BOOLEAN;
435 }
436 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100437 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 switch (sym->type) {
440 case S_BOOLEAN:
441 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700442 sym->def[def].tri = no;
443 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445 default:
446 ;
447 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400448 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
449 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (!p)
451 continue;
452 *p++ = 0;
453 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600454 if (p2) {
455 *p2-- = 0;
456 if (*p2 == '\r')
457 *p2 = 0;
458 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900459
460 sym = sym_find(line + strlen(CONFIG_));
461 if (!sym) {
462 if (def == S_DEF_AUTO)
463 /*
464 * Reading from include/config/auto.conf
465 * If CONFIG_FOO previously existed in
466 * auto.conf but it is missing now,
467 * include/config/foo.h must be touched.
468 */
469 conf_touch_dep(line + strlen(CONFIG_));
470 else
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200471 sym_add_change_count(1);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900472 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700473 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900474
Roman Zippel669bfad92006-06-08 22:12:42 -0700475 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100476 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100478 if (conf_set_sym_val(sym, def, def_flags, p))
479 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400480 } else {
481 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100482 conf_warning("unexpected data: %.*s",
483 (int)strcspn(line, "\r\n"), line);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 continue;
486 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (sym && sym_is_choice_value(sym)) {
489 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700490 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 case no:
492 break;
493 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700494 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800495 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200496 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100500 if (cs->def[def].tri != no)
501 conf_warning("override: %s changes choice state", sym->name);
502 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100505 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700508 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800510 return 0;
511}
512
513int conf_read(const char *name)
514{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500515 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900516 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500517 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800518
Karsten Wiesebfc10002006-12-13 00:34:07 -0800519 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700520
Al Viro6b87b702016-01-14 18:13:49 +0000521 if (conf_read_simple(name, S_DEF_USER)) {
522 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800523 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000524 }
525
526 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for_all_symbols(i, sym) {
529 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200530 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500531 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800532 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
533 /* check that calculated value agrees with saved value */
534 switch (sym->type) {
535 case S_BOOLEAN:
536 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700537 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800538 break;
539 if (!sym_is_choice(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500540 continue;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400541 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800542 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700543 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500544 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800545 break;
546 }
547 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
548 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500549 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800550 conf_unsaved++;
551 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
Roman Zippeld8982ba2007-07-09 11:43:58 -0700554 for_all_symbols(i, sym) {
555 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
556 /* Reset values of generates values, so they'll appear
557 * as new, if they should become visible, but that
558 * doesn't quite work if the Kconfig and the saved
559 * configuration disagree.
560 */
561 if (sym->visible == no && !conf_unsaved)
562 sym->flags &= ~SYMBOL_DEF_USER;
563 switch (sym->type) {
564 case S_STRING:
565 case S_INT:
566 case S_HEX:
567 /* Reset a string value if it's out of range */
568 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
569 break;
570 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
571 conf_unsaved++;
572 break;
573 default:
574 break;
575 }
576 }
577 }
578
Karsten Wiesebfc10002006-12-13 00:34:07 -0800579 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return 0;
582}
583
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400584/*
585 * Kconfig configuration printer
586 *
587 * This printer is used when generating the resulting configuration after
588 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
589 * passing a non-NULL argument to the printer.
590 *
591 */
592static void
593kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200594{
Sam Ravnborg49192f22010-07-31 23:35:33 +0200595
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500596 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200597 case S_BOOLEAN:
598 case S_TRISTATE:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400599 if (*value == 'n') {
600 bool skip_unset = (arg != NULL);
601
602 if (!skip_unset)
603 fprintf(fp, "# %s%s is not set\n",
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400604 CONFIG_, sym->name);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400605 return;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200606 }
607 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400608 default:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200609 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400610 }
611
612 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
613}
614
615static void
616kconfig_print_comment(FILE *fp, const char *value, void *arg)
617{
618 const char *p = value;
619 size_t l;
620
621 for (;;) {
622 l = strcspn(p, "\n");
623 fprintf(fp, "#");
624 if (l) {
625 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400626 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400627 p += l;
628 }
629 fprintf(fp, "\n");
630 if (*p++ == '\0')
631 break;
632 }
633}
634
635static struct conf_printer kconfig_printer_cb =
636{
637 .print_symbol = kconfig_print_symbol,
638 .print_comment = kconfig_print_comment,
639};
640
641/*
642 * Header printer
643 *
644 * This printer is used when generating the `include/generated/autoconf.h' file.
645 */
646static void
647header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
648{
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400649
650 switch (sym->type) {
651 case S_BOOLEAN:
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400652 case S_TRISTATE: {
653 const char *suffix = "";
654
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400655 switch (*value) {
656 case 'n':
Michal Marek2a11c8e2011-07-20 17:38:57 +0200657 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400658 case 'm':
659 suffix = "_MODULE";
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400660 /* fall through */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400661 default:
Michal Marek2a11c8e2011-07-20 17:38:57 +0200662 fprintf(fp, "#define %s%s%s 1\n",
663 CONFIG_, sym->name, suffix);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400664 }
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400665 break;
666 }
667 case S_HEX: {
668 const char *prefix = "";
669
670 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
671 prefix = "0x";
672 fprintf(fp, "#define %s%s %s%s\n",
673 CONFIG_, sym->name, prefix, value);
674 break;
675 }
676 case S_STRING:
677 case S_INT:
678 fprintf(fp, "#define %s%s %s\n",
679 CONFIG_, sym->name, value);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200680 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400681 default:
682 break;
683 }
684
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400685}
686
687static void
688header_print_comment(FILE *fp, const char *value, void *arg)
689{
690 const char *p = value;
691 size_t l;
692
693 fprintf(fp, "/*\n");
694 for (;;) {
695 l = strcspn(p, "\n");
696 fprintf(fp, " *");
697 if (l) {
698 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400699 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400700 p += l;
701 }
702 fprintf(fp, "\n");
703 if (*p++ == '\0')
704 break;
705 }
706 fprintf(fp, " */\n");
707}
708
709static struct conf_printer header_printer_cb =
710{
711 .print_symbol = header_print_symbol,
712 .print_comment = header_print_comment,
713};
714
715/*
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400716 * Tristate printer
717 *
718 * This printer is used when generating the `include/config/tristate.conf' file.
719 */
720static void
721tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
722{
723
724 if (sym->type == S_TRISTATE && *value != 'n')
725 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
726}
727
728static struct conf_printer tristate_printer_cb =
729{
730 .print_symbol = tristate_print_symbol,
731 .print_comment = kconfig_print_comment,
732};
733
734static void conf_write_symbol(FILE *fp, struct symbol *sym,
735 struct conf_printer *printer, void *printer_arg)
736{
737 const char *str;
738
739 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200740 case S_UNKNOWN:
741 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400742 case S_STRING:
743 str = sym_get_string_value(sym);
744 str = sym_escape_string_value(str);
745 printer->print_symbol(fp, sym, str, printer_arg);
746 free((void *)str);
747 break;
748 default:
749 str = sym_get_string_value(sym);
750 printer->print_symbol(fp, sym, str, printer_arg);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200751 }
752}
753
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400754static void
755conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
756{
757 char buf[256];
758
759 snprintf(buf, sizeof(buf),
760 "\n"
761 "Automatically generated file; DO NOT EDIT.\n"
762 "%s\n",
763 rootmenu.prompt->text);
764
765 printer->print_comment(fp, buf, printer_arg);
766}
767
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200768/*
769 * Write out a minimal config.
770 * All values that has default values are skipped as this is redundant.
771 */
772int conf_write_defconfig(const char *filename)
773{
774 struct symbol *sym;
775 struct menu *menu;
776 FILE *out;
777
778 out = fopen(filename, "w");
779 if (!out)
780 return 1;
781
782 sym_clear_all_valid();
783
784 /* Traverse all menus to find all relevant symbols */
785 menu = rootmenu.list;
786
787 while (menu != NULL)
788 {
789 sym = menu->sym;
790 if (sym == NULL) {
791 if (!menu_is_visible(menu))
792 goto next_menu;
793 } else if (!sym_is_choice(sym)) {
794 sym_calc_value(sym);
795 if (!(sym->flags & SYMBOL_WRITE))
796 goto next_menu;
797 sym->flags &= ~SYMBOL_WRITE;
798 /* If we cannot change the symbol - skip */
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200799 if (!sym_is_changeable(sym))
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200800 goto next_menu;
801 /* If symbol equals to default value - skip */
802 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
803 goto next_menu;
804
805 /*
806 * If symbol is a choice value and equals to the
807 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200808 * But only if value is bool and equal to "y" and
809 * choice is not "optional".
810 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200811 */
812 if (sym_is_choice_value(sym)) {
813 struct symbol *cs;
814 struct symbol *ds;
815
816 cs = prop_get_symbol(sym_get_choice_prop(sym));
817 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200818 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200819 if ((sym->type == S_BOOLEAN) &&
820 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200821 goto next_menu;
822 }
823 }
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400824 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200825 }
826next_menu:
827 if (menu->list != NULL) {
828 menu = menu->list;
829 }
830 else if (menu->next != NULL) {
831 menu = menu->next;
832 } else {
833 while ((menu = menu->parent)) {
834 if (menu->next != NULL) {
835 menu = menu->next;
836 break;
837 }
838 }
839 }
840 }
841 fclose(out);
842 return 0;
843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845int conf_write(const char *name)
846{
Roman Zippelc955cca2006-06-08 22:12:39 -0700847 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct symbol *sym;
849 struct menu *menu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 const char *str;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900851 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 char *env;
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
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400883 conf_write_heading(out, &kconfig_printer_cb, NULL);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 } else if (!(sym->flags & SYMBOL_CHOICE)) {
901 sym_calc_value(sym);
902 if (!(sym->flags & SYMBOL_WRITE))
903 goto next;
Alexander Popovaff11cd2019-05-17 22:42:22 +0300904 if (need_newline) {
905 fprintf(out, "\n");
906 need_newline = false;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400909 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
Sam Ravnborg49192f22010-07-31 23:35:33 +0200912next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (menu->list) {
914 menu = menu->list;
915 continue;
916 }
917 if (menu->next)
918 menu = menu->next;
919 else while ((menu = menu->parent)) {
Alexander Popovaff11cd2019-05-17 22:42:22 +0300920 if (!menu->sym && menu_is_visible(menu) &&
921 menu != &rootmenu) {
922 str = menu_get_prompt(menu);
923 fprintf(out, "# end of %s\n", str);
924 need_newline = true;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (menu->next) {
927 menu = menu->next;
928 break;
929 }
930 }
931 }
932 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700933
934 if (*tmpname) {
Masahiro Yamada67424f62019-05-10 15:12:05 +0900935 if (is_same(name, tmpname)) {
936 conf_message("No change to %s", name);
937 unlink(tmpname);
938 sym_set_change_count(0);
939 return 0;
940 }
941
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900942 snprintf(oldname, sizeof(oldname), "%s.old", name);
943 rename(name, oldname);
944 if (rename(tmpname, name))
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700945 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900948 conf_message("configuration written to %s", name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700949
Karsten Wiesebfc10002006-12-13 00:34:07 -0800950 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 return 0;
953}
Roman Zippelc955cca2006-06-08 22:12:39 -0700954
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900955/* write a dependency file as used by kbuild to track dependencies */
956static int conf_write_dep(const char *name)
957{
958 struct file *file;
959 FILE *out;
960
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900961 out = fopen("..config.tmp", "w");
962 if (!out)
963 return 1;
964 fprintf(out, "deps_config := \\\n");
965 for (file = file_list; file; file = file->next) {
966 if (file->next)
967 fprintf(out, "\t%s \\\n", file->name);
968 else
969 fprintf(out, "\t%s\n", file->name);
970 }
971 fprintf(out, "\n%s: \\\n"
972 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
973
974 env_write_dep(out, conf_get_autoconfig_name());
975
976 fprintf(out, "\n$(deps_config): ;\n");
977 fclose(out);
Masahiro Yamada79123b12018-07-20 16:46:29 +0900978
979 if (make_parent_dir(name))
980 return 1;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900981 rename("..config.tmp", name);
982 return 0;
983}
984
Masahiro Yamada0849d212018-11-30 18:15:49 +0900985static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700986{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200987 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700988 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900989 int res, i;
990
991 strcpy(depfile_path, "include/config/");
992 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700993
Markus Heidelberg12122f62009-05-18 01:36:54 +0200994 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700995 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000996 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700997
Roman Zippel2e3646e2006-06-08 22:12:42 -0700998 for_all_symbols(i, sym) {
999 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +02001000 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -07001001 continue;
1002 if (sym->flags & SYMBOL_WRITE) {
1003 if (sym->flags & SYMBOL_DEF_AUTO) {
1004 /*
1005 * symbol has old and new value,
1006 * so compare them...
1007 */
1008 switch (sym->type) {
1009 case S_BOOLEAN:
1010 case S_TRISTATE:
1011 if (sym_get_tristate_value(sym) ==
1012 sym->def[S_DEF_AUTO].tri)
1013 continue;
1014 break;
1015 case S_STRING:
1016 case S_HEX:
1017 case S_INT:
1018 if (!strcmp(sym_get_string_value(sym),
1019 sym->def[S_DEF_AUTO].val))
1020 continue;
1021 break;
1022 default:
1023 break;
1024 }
1025 } else {
1026 /*
1027 * If there is no old value, only 'no' (unset)
1028 * is allowed as new value.
1029 */
1030 switch (sym->type) {
1031 case S_BOOLEAN:
1032 case S_TRISTATE:
1033 if (sym_get_tristate_value(sym) == no)
1034 continue;
1035 break;
1036 default:
1037 break;
1038 }
1039 }
1040 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1041 /* There is neither an old nor a new value. */
1042 continue;
1043 /* else
1044 * There is an old value, but no new value ('no' (unset)
1045 * isn't saved in auto.conf, so the old value is always
1046 * different from 'no').
1047 */
1048
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001049 res = conf_touch_dep(sym->name);
1050 if (res)
1051 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001052 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001053
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001054 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001055}
1056
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001057int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001058{
1059 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001060 const char *name;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001061 const char *autoconf_name = conf_get_autoconfig_name();
Michal Marekbc081dd2009-12-07 16:38:33 +01001062 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +02001063 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001064
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001065 if (!overwrite && is_present(autoconf_name))
1066 return 0;
1067
Roman Zippel2e3646e2006-06-08 22:12:42 -07001068 sym_clear_all_valid();
1069
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001070 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -07001071
Masahiro Yamada0849d212018-11-30 18:15:49 +09001072 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001073 return 1;
1074
Roman Zippelc955cca2006-06-08 22:12:39 -07001075 out = fopen(".tmpconfig", "w");
1076 if (!out)
1077 return 1;
1078
Michal Marekbc081dd2009-12-07 16:38:33 +01001079 tristate = fopen(".tmpconfig_tristate", "w");
1080 if (!tristate) {
1081 fclose(out);
1082 return 1;
1083 }
1084
Roman Zippelc955cca2006-06-08 22:12:39 -07001085 out_h = fopen(".tmpconfig.h", "w");
1086 if (!out_h) {
1087 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001088 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001089 return 1;
1090 }
1091
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001092 conf_write_heading(out, &kconfig_printer_cb, NULL);
1093
1094 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1095
1096 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001097
Roman Zippelc955cca2006-06-08 22:12:39 -07001098 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001099 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001100 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001101 continue;
1102
Paul Gortmakera9596132012-04-12 19:46:33 -04001103 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001104 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001105
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001106 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1107
1108 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001109 }
1110 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001111 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001112 fclose(out_h);
1113
1114 name = getenv("KCONFIG_AUTOHEADER");
1115 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001116 name = "include/generated/autoconf.h";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001117 if (make_parent_dir(name))
1118 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001119 if (rename(".tmpconfig.h", name))
1120 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001121
Michal Marekbc081dd2009-12-07 16:38:33 +01001122 name = getenv("KCONFIG_TRISTATE");
1123 if (!name)
1124 name = "include/config/tristate.conf";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001125 if (make_parent_dir(name))
1126 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001127 if (rename(".tmpconfig_tristate", name))
1128 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001129
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001130 if (make_parent_dir(autoconf_name))
Masahiro Yamada79123b12018-07-20 16:46:29 +09001131 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001132 /*
1133 * This must be the last step, kbuild has a dependency on auto.conf
1134 * and this marks the successful completion of the previous steps.
1135 */
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001136 if (rename(".tmpconfig", autoconf_name))
Roman Zippelc955cca2006-06-08 22:12:39 -07001137 return 1;
1138
1139 return 0;
1140}
Karsten Wieseb3214292006-12-13 00:34:06 -08001141
Karsten Wiesebfc10002006-12-13 00:34:07 -08001142static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001143static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001144
1145void sym_set_change_count(int count)
1146{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001147 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001148 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001149 if (conf_changed_callback &&
1150 (bool)_sym_change_count != (bool)count)
1151 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001152}
1153
1154void sym_add_change_count(int count)
1155{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001156 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001157}
1158
Karsten Wieseb3214292006-12-13 00:34:06 -08001159bool conf_get_changed(void)
1160{
1161 return sym_change_count;
1162}
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
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001169static bool randomize_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 int cnt, def;
1175
1176 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001177 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001178 * and if no then no-one.
1179 * In both cases stop.
1180 */
1181 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001182 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001183
1184 prop = sym_get_choice_prop(csym);
1185
1186 /* count entries in choice block */
1187 cnt = 0;
1188 expr_list_for_each_sym(prop->expr, e, sym)
1189 cnt++;
1190
1191 /*
1192 * find a random value and set it to yes,
1193 * set the rest to no so we have only one set
1194 */
1195 def = (rand() % cnt);
1196
1197 cnt = 0;
1198 expr_list_for_each_sym(prop->expr, e, sym) {
1199 if (def == cnt++) {
1200 sym->def[S_DEF_USER].tri = yes;
1201 csym->def[S_DEF_USER].val = sym;
1202 }
1203 else {
1204 sym->def[S_DEF_USER].tri = no;
1205 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001206 sym->flags |= SYMBOL_DEF_USER;
1207 /* clear VALID to get value calculated */
1208 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001209 }
1210 csym->flags |= SYMBOL_DEF_USER;
1211 /* clear VALID to get value calculated */
1212 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001213
1214 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001215}
1216
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001217void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001218{
1219 struct property *prop;
1220 struct symbol *sym;
1221 struct expr *e;
1222
1223 prop = sym_get_choice_prop(csym);
1224
1225 /*
1226 * Set all non-assinged choice values to no
1227 */
1228 expr_list_for_each_sym(prop->expr, e, sym) {
1229 if (!sym_has_value(sym))
1230 sym->def[S_DEF_USER].tri = no;
1231 }
1232 csym->flags |= SYMBOL_DEF_USER;
1233 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001234 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001235}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001236
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001237bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001238{
1239 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001240 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001241 * pty: probability of tristate = y
1242 * ptm: probability of tristate = m
1243 */
1244
1245 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1246 * below, otherwise gcc whines about
1247 * -Wmaybe-uninitialized */
1248 if (mode == def_random) {
1249 int n, p[3];
1250 char *env = getenv("KCONFIG_PROBABILITY");
1251 n = 0;
1252 while( env && *env ) {
1253 char *endp;
1254 int tmp = strtol( env, &endp, 10 );
1255 if( tmp >= 0 && tmp <= 100 ) {
1256 p[n++] = tmp;
1257 } else {
1258 errno = ERANGE;
1259 perror( "KCONFIG_PROBABILITY" );
1260 exit( 1 );
1261 }
1262 env = (*endp == ':') ? endp+1 : endp;
1263 if( n >=3 ) {
1264 break;
1265 }
1266 }
1267 switch( n ) {
1268 case 1:
1269 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1270 break;
1271 case 2:
1272 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1273 break;
1274 case 3:
1275 pby = p[0]; pty = p[1]; ptm = p[2];
1276 break;
1277 }
1278
1279 if( pty+ptm > 100 ) {
1280 errno = ERANGE;
1281 perror( "KCONFIG_PROBABILITY" );
1282 exit( 1 );
1283 }
1284 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001285 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001286
1287 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001288 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001289 continue;
1290 switch (sym_get_type(sym)) {
1291 case S_BOOLEAN:
1292 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001293 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001294 switch (mode) {
1295 case def_yes:
1296 sym->def[S_DEF_USER].tri = yes;
1297 break;
1298 case def_mod:
1299 sym->def[S_DEF_USER].tri = mod;
1300 break;
1301 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001302 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1303 sym->def[S_DEF_USER].tri = yes;
1304 else
1305 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001306 break;
1307 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001308 sym->def[S_DEF_USER].tri = no;
1309 cnt = rand() % 100;
1310 if (sym->type == S_TRISTATE) {
1311 if (cnt < pty)
1312 sym->def[S_DEF_USER].tri = yes;
1313 else if (cnt < (pty+ptm))
1314 sym->def[S_DEF_USER].tri = mod;
1315 } else if (cnt < pby)
1316 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001317 break;
1318 default:
1319 continue;
1320 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001321 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001322 sym->flags |= SYMBOL_DEF_USER;
1323 break;
1324 default:
1325 break;
1326 }
1327
1328 }
1329
Al Viroce97e132008-10-26 05:12:34 +00001330 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001331
Sam Ravnborg184832c2009-03-15 11:05:12 +01001332 /*
1333 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001334 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001335 * choice symbols in one block.
1336 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001337 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001338 * selected in a choice block and we set it to yes,
1339 * and the rest to no.
1340 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001341 if (mode != def_random) {
1342 for_all_symbols(i, csym) {
1343 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1344 sym_is_choice_value(csym))
1345 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1346 }
1347 }
1348
Roman Zippeldc7862e2008-05-06 04:55:55 +02001349 for_all_symbols(i, csym) {
1350 if (sym_has_value(csym) || !sym_is_choice(csym))
1351 continue;
1352
1353 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001354 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001355 has_changed = randomize_choice_values(csym);
1356 else {
1357 set_all_choice_values(csym);
1358 has_changed = true;
1359 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001360 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001361
1362 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001363}