blob: 08ba146a83c547db9e5607f407de08128656bc40 [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
6#include <sys/stat.h>
7#include <ctype.h>
Arnaud Lacombe94bedec2010-08-17 01:40:20 -04008#include <errno.h>
Roman Zippel2e3646e2006-06-08 22:12:42 -07009#include <fcntl.h>
Masahiro Yamada558e78e2018-12-21 17:33:04 +090010#include <limits.h>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040011#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <time.h>
16#include <unistd.h>
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "lkc.h"
19
Masahiro Yamada06081822018-07-20 16:46:27 +090020/* return true if 'path' exists, false otherwise */
21static bool is_present(const char *path)
22{
23 struct stat st;
24
25 return !stat(path, &st);
26}
27
28/* return true if 'path' exists and it is a directory, false otherwise */
29static bool is_dir(const char *path)
30{
31 struct stat st;
32
33 if (stat(path, &st))
34 return 0;
35
36 return S_ISDIR(st.st_mode);
37}
38
39/*
40 * Create the parent directory of the given path.
41 *
42 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
43 */
44static int make_parent_dir(const char *path)
45{
46 char tmp[PATH_MAX + 1];
47 char *p;
48
49 strncpy(tmp, path, sizeof(tmp));
50 tmp[sizeof(tmp) - 1] = 0;
51
52 /* Remove the base name. Just return if nothing is left */
53 p = strrchr(tmp, '/');
54 if (!p)
55 return 0;
56 *(p + 1) = 0;
57
58 /* Just in case it is an absolute path */
59 p = tmp;
60 while (*p == '/')
61 p++;
62
63 while ((p = strchr(p, '/'))) {
64 *p = 0;
65
66 /* skip if the directory exists */
67 if (!is_dir(tmp) && mkdir(tmp, 0755))
68 return -1;
69
70 *p = '/';
71 while (*p == '/')
72 p++;
73 }
74
75 return 0;
76}
77
Masahiro Yamada1508fec2018-11-30 18:15:50 +090078static char depfile_path[PATH_MAX];
79static size_t depfile_prefix_len;
80
81/* touch depfile for symbol 'name' */
82static int conf_touch_dep(const char *name)
83{
84 int fd, ret;
85 const char *s;
86 char *d, c;
87
88 /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
89 if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
90 return -1;
91
92 d = depfile_path + depfile_prefix_len;
93 s = name;
94
95 while ((c = *s++))
96 *d++ = (c == '_') ? '/' : tolower(c);
97 strcpy(d, ".h");
98
99 /* Assume directory path already exists. */
100 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
101 if (fd == -1) {
102 if (errno != ENOENT)
103 return -1;
104
105 ret = make_parent_dir(depfile_path);
106 if (ret)
107 return ret;
108
109 /* Try it again. */
110 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
111 if (fd == -1)
112 return -1;
113 }
114 close(fd);
115
116 return 0;
117}
118
Michal Marekad8d40c2015-02-24 16:37:13 +0100119struct conf_printer {
120 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
121 void (*print_comment)(FILE *, const char *, void *);
122};
123
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800124static void conf_warning(const char *fmt, ...)
125 __attribute__ ((format (printf, 1, 2)));
126
Michal Marek42368c32010-08-17 10:21:19 +0200127static void conf_message(const char *fmt, ...)
128 __attribute__ ((format (printf, 1, 2)));
129
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800130static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900131static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800132
Masahiro Yamada104daea2018-05-28 18:21:40 +0900133const char conf_defname[] = "arch/$(ARCH)/defconfig";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800135static void conf_warning(const char *fmt, ...)
136{
137 va_list ap;
138 va_start(ap, fmt);
139 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
140 vfprintf(stderr, fmt, ap);
141 fprintf(stderr, "\n");
142 va_end(ap);
143 conf_warnings++;
144}
145
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900146static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +0200147{
148 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900149 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +0200150 printf("\n#\n");
151}
152
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900153static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +0200154 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900155void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +0200156{
157 conf_message_callback = fn;
158}
159
160static void conf_message(const char *fmt, ...)
161{
162 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900163 char buf[4096];
164
165 if (!conf_message_callback)
166 return;
Michal Marek42368c32010-08-17 10:21:19 +0200167
168 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900169
170 vsnprintf(buf, sizeof(buf), fmt, ap);
171 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +0000172 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +0200173}
174
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700175const char *conf_get_configname(void)
176{
177 char *name = getenv("KCONFIG_CONFIG");
178
179 return name ? name : ".config";
180}
181
Markus Heidelberg12122f62009-05-18 01:36:54 +0200182const char *conf_get_autoconfig_name(void)
183{
184 char *name = getenv("KCONFIG_AUTOCONFIG");
185
186 return name ? name : "include/config/auto.conf";
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189char *conf_get_default_confname(void)
190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 static char fullname[PATH_MAX+1];
192 char *env, *name;
193
Masahiro Yamada104daea2018-05-28 18:21:40 +0900194 name = expand_string(conf_defname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 env = getenv(SRCTREE);
196 if (env) {
197 sprintf(fullname, "%s/%s", env, name);
Masahiro Yamada06081822018-07-20 16:46:27 +0900198 if (is_present(fullname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return fullname;
200 }
201 return name;
202}
203
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100204static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
205{
206 char *p2;
207
208 switch (sym->type) {
209 case S_TRISTATE:
210 if (p[0] == 'm') {
211 sym->def[def].tri = mod;
212 sym->flags |= def_flags;
213 break;
214 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400215 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100216 case S_BOOLEAN:
217 if (p[0] == 'y') {
218 sym->def[def].tri = yes;
219 sym->flags |= def_flags;
220 break;
221 }
222 if (p[0] == 'n') {
223 sym->def[def].tri = no;
224 sym->flags |= def_flags;
225 break;
226 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200227 if (def != S_DEF_AUTO)
228 conf_warning("symbol value '%s' invalid for %s",
229 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400230 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100231 case S_STRING:
232 if (*p++ != '"')
233 break;
234 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
235 if (*p2 == '"') {
236 *p2 = 0;
237 break;
238 }
239 memmove(p2, p2 + 1, strlen(p2));
240 }
241 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200242 if (def != S_DEF_AUTO)
243 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100244 return 1;
245 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400246 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100247 case S_INT:
248 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100249 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900250 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100251 sym->flags |= def_flags;
252 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200253 if (def != S_DEF_AUTO)
254 conf_warning("symbol value '%s' invalid for %s",
255 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100256 return 1;
257 }
258 break;
259 default:
260 ;
261 }
262 return 0;
263}
264
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700265#define LINE_GROWTH 16
266static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
267{
268 char *nline;
269 size_t new_size = slen + 1;
270 if (new_size > *n) {
271 new_size += LINE_GROWTH - 1;
272 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900273 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700274 if (!nline)
275 return -1;
276
277 *lineptr = nline;
278 *n = new_size;
279 }
280
281 (*lineptr)[slen] = c;
282
283 return 0;
284}
285
286static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
287{
288 char *line = *lineptr;
289 size_t slen = 0;
290
291 for (;;) {
292 int c = getc(stream);
293
294 switch (c) {
295 case '\n':
296 if (add_byte(c, &line, slen, n) < 0)
297 goto e_out;
298 slen++;
299 /* fall through */
300 case EOF:
301 if (add_byte('\0', &line, slen, n) < 0)
302 goto e_out;
303 *lineptr = line;
304 if (slen == 0)
305 return -1;
306 return slen;
307 default:
308 if (add_byte(c, &line, slen, n) < 0)
309 goto e_out;
310 slen++;
311 }
312 }
313
314e_out:
315 line[slen-1] = '\0';
316 *lineptr = line;
317 return -1;
318}
319
Roman Zippel669bfad92006-06-08 22:12:42 -0700320int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700323 char *line = NULL;
324 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700327 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (name) {
330 in = zconf_fopen(name);
331 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700332 struct property *prop;
333
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700334 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700335 in = zconf_fopen(name);
336 if (in)
337 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800338 sym_add_change_count(1);
Al Viro6b87b702016-01-14 18:13:49 +0000339 if (!sym_defconfig_list)
Roman Zippelface4372006-06-08 22:12:45 -0700340 return 1;
341
342 for_all_defaults(sym_defconfig_list, prop) {
343 if (expr_calc_value(prop->visible.expr) == no ||
344 prop->expr->type != E_SYMBOL)
345 continue;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900346 sym_calc_value(prop->expr->left.sym);
347 name = sym_get_string_value(prop->expr->left.sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 in = zconf_fopen(name);
349 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200350 conf_message("using defaults found in %s",
Michal Marek42368c32010-08-17 10:21:19 +0200351 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700352 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!in)
357 return 1;
358
Roman Zippelddc97ca2006-06-08 22:12:38 -0700359load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800360 conf_filename = name;
361 conf_lineno = 0;
362 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800363
Roman Zippel669bfad92006-06-08 22:12:42 -0700364 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700366 sym->flags |= SYMBOL_CHANGED;
367 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200368 if (sym_is_choice(sym))
369 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 switch (sym->type) {
371 case S_INT:
372 case S_HEX:
373 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700374 if (sym->def[def].val)
375 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400376 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700378 sym->def[def].val = NULL;
379 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 }
382
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700383 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800384 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400386 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400387 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400389 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!p)
391 continue;
392 *p++ = 0;
393 if (strncmp(p, "is not set", 10))
394 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700395 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400396 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200397 if (!sym) {
398 sym_add_change_count(1);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900399 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200400 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700401 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400402 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700403 if (sym->type == S_UNKNOWN)
404 sym->type = S_BOOLEAN;
405 }
406 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100407 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 switch (sym->type) {
410 case S_BOOLEAN:
411 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700412 sym->def[def].tri = no;
413 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415 default:
416 ;
417 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400418 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
419 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!p)
421 continue;
422 *p++ = 0;
423 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600424 if (p2) {
425 *p2-- = 0;
426 if (*p2 == '\r')
427 *p2 = 0;
428 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900429
430 sym = sym_find(line + strlen(CONFIG_));
431 if (!sym) {
432 if (def == S_DEF_AUTO)
433 /*
434 * Reading from include/config/auto.conf
435 * If CONFIG_FOO previously existed in
436 * auto.conf but it is missing now,
437 * include/config/foo.h must be touched.
438 */
439 conf_touch_dep(line + strlen(CONFIG_));
440 else
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200441 sym_add_change_count(1);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900442 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700443 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900444
Roman Zippel669bfad92006-06-08 22:12:42 -0700445 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100446 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100448 if (conf_set_sym_val(sym, def, def_flags, p))
449 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400450 } else {
451 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100452 conf_warning("unexpected data: %.*s",
453 (int)strcspn(line, "\r\n"), line);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 continue;
456 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (sym && sym_is_choice_value(sym)) {
459 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700460 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case no:
462 break;
463 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700464 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800465 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200466 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100470 if (cs->def[def].tri != no)
471 conf_warning("override: %s changes choice state", sym->name);
472 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100475 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700478 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800480 return 0;
481}
482
483int conf_read(const char *name)
484{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500485 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900486 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500487 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800488
Karsten Wiesebfc10002006-12-13 00:34:07 -0800489 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700490
Al Viro6b87b702016-01-14 18:13:49 +0000491 if (conf_read_simple(name, S_DEF_USER)) {
492 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800493 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000494 }
495
496 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 for_all_symbols(i, sym) {
499 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200500 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500501 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800502 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
503 /* check that calculated value agrees with saved value */
504 switch (sym->type) {
505 case S_BOOLEAN:
506 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700507 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800508 break;
509 if (!sym_is_choice(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500510 continue;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400511 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800512 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700513 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500514 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800515 break;
516 }
517 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
518 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500519 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800520 conf_unsaved++;
521 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Roman Zippeld8982ba2007-07-09 11:43:58 -0700524 for_all_symbols(i, sym) {
525 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
526 /* Reset values of generates values, so they'll appear
527 * as new, if they should become visible, but that
528 * doesn't quite work if the Kconfig and the saved
529 * configuration disagree.
530 */
531 if (sym->visible == no && !conf_unsaved)
532 sym->flags &= ~SYMBOL_DEF_USER;
533 switch (sym->type) {
534 case S_STRING:
535 case S_INT:
536 case S_HEX:
537 /* Reset a string value if it's out of range */
538 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
539 break;
540 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
541 conf_unsaved++;
542 break;
543 default:
544 break;
545 }
546 }
547 }
548
Karsten Wiesebfc10002006-12-13 00:34:07 -0800549 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return 0;
552}
553
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400554/*
555 * Kconfig configuration printer
556 *
557 * This printer is used when generating the resulting configuration after
558 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
559 * passing a non-NULL argument to the printer.
560 *
561 */
562static void
563kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200564{
Sam Ravnborg49192f22010-07-31 23:35:33 +0200565
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500566 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200567 case S_BOOLEAN:
568 case S_TRISTATE:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400569 if (*value == 'n') {
570 bool skip_unset = (arg != NULL);
571
572 if (!skip_unset)
573 fprintf(fp, "# %s%s is not set\n",
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400574 CONFIG_, sym->name);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400575 return;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200576 }
577 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400578 default:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200579 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400580 }
581
582 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
583}
584
585static void
586kconfig_print_comment(FILE *fp, const char *value, void *arg)
587{
588 const char *p = value;
589 size_t l;
590
591 for (;;) {
592 l = strcspn(p, "\n");
593 fprintf(fp, "#");
594 if (l) {
595 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400596 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400597 p += l;
598 }
599 fprintf(fp, "\n");
600 if (*p++ == '\0')
601 break;
602 }
603}
604
605static struct conf_printer kconfig_printer_cb =
606{
607 .print_symbol = kconfig_print_symbol,
608 .print_comment = kconfig_print_comment,
609};
610
611/*
612 * Header printer
613 *
614 * This printer is used when generating the `include/generated/autoconf.h' file.
615 */
616static void
617header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
618{
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400619
620 switch (sym->type) {
621 case S_BOOLEAN:
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400622 case S_TRISTATE: {
623 const char *suffix = "";
624
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400625 switch (*value) {
626 case 'n':
Michal Marek2a11c8e2011-07-20 17:38:57 +0200627 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400628 case 'm':
629 suffix = "_MODULE";
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400630 /* fall through */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400631 default:
Michal Marek2a11c8e2011-07-20 17:38:57 +0200632 fprintf(fp, "#define %s%s%s 1\n",
633 CONFIG_, sym->name, suffix);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400634 }
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400635 break;
636 }
637 case S_HEX: {
638 const char *prefix = "";
639
640 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
641 prefix = "0x";
642 fprintf(fp, "#define %s%s %s%s\n",
643 CONFIG_, sym->name, prefix, value);
644 break;
645 }
646 case S_STRING:
647 case S_INT:
648 fprintf(fp, "#define %s%s %s\n",
649 CONFIG_, sym->name, value);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200650 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400651 default:
652 break;
653 }
654
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400655}
656
657static void
658header_print_comment(FILE *fp, const char *value, void *arg)
659{
660 const char *p = value;
661 size_t l;
662
663 fprintf(fp, "/*\n");
664 for (;;) {
665 l = strcspn(p, "\n");
666 fprintf(fp, " *");
667 if (l) {
668 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400669 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400670 p += l;
671 }
672 fprintf(fp, "\n");
673 if (*p++ == '\0')
674 break;
675 }
676 fprintf(fp, " */\n");
677}
678
679static struct conf_printer header_printer_cb =
680{
681 .print_symbol = header_print_symbol,
682 .print_comment = header_print_comment,
683};
684
685/*
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400686 * Tristate printer
687 *
688 * This printer is used when generating the `include/config/tristate.conf' file.
689 */
690static void
691tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
692{
693
694 if (sym->type == S_TRISTATE && *value != 'n')
695 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
696}
697
698static struct conf_printer tristate_printer_cb =
699{
700 .print_symbol = tristate_print_symbol,
701 .print_comment = kconfig_print_comment,
702};
703
704static void conf_write_symbol(FILE *fp, struct symbol *sym,
705 struct conf_printer *printer, void *printer_arg)
706{
707 const char *str;
708
709 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200710 case S_UNKNOWN:
711 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400712 case S_STRING:
713 str = sym_get_string_value(sym);
714 str = sym_escape_string_value(str);
715 printer->print_symbol(fp, sym, str, printer_arg);
716 free((void *)str);
717 break;
718 default:
719 str = sym_get_string_value(sym);
720 printer->print_symbol(fp, sym, str, printer_arg);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200721 }
722}
723
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400724static void
725conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
726{
727 char buf[256];
728
729 snprintf(buf, sizeof(buf),
730 "\n"
731 "Automatically generated file; DO NOT EDIT.\n"
732 "%s\n",
733 rootmenu.prompt->text);
734
735 printer->print_comment(fp, buf, printer_arg);
736}
737
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200738/*
739 * Write out a minimal config.
740 * All values that has default values are skipped as this is redundant.
741 */
742int conf_write_defconfig(const char *filename)
743{
744 struct symbol *sym;
745 struct menu *menu;
746 FILE *out;
747
748 out = fopen(filename, "w");
749 if (!out)
750 return 1;
751
752 sym_clear_all_valid();
753
754 /* Traverse all menus to find all relevant symbols */
755 menu = rootmenu.list;
756
757 while (menu != NULL)
758 {
759 sym = menu->sym;
760 if (sym == NULL) {
761 if (!menu_is_visible(menu))
762 goto next_menu;
763 } else if (!sym_is_choice(sym)) {
764 sym_calc_value(sym);
765 if (!(sym->flags & SYMBOL_WRITE))
766 goto next_menu;
767 sym->flags &= ~SYMBOL_WRITE;
768 /* If we cannot change the symbol - skip */
769 if (!sym_is_changable(sym))
770 goto next_menu;
771 /* If symbol equals to default value - skip */
772 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
773 goto next_menu;
774
775 /*
776 * If symbol is a choice value and equals to the
777 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200778 * But only if value is bool and equal to "y" and
779 * choice is not "optional".
780 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200781 */
782 if (sym_is_choice_value(sym)) {
783 struct symbol *cs;
784 struct symbol *ds;
785
786 cs = prop_get_symbol(sym_get_choice_prop(sym));
787 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200788 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200789 if ((sym->type == S_BOOLEAN) &&
790 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200791 goto next_menu;
792 }
793 }
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400794 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200795 }
796next_menu:
797 if (menu->list != NULL) {
798 menu = menu->list;
799 }
800 else if (menu->next != NULL) {
801 menu = menu->next;
802 } else {
803 while ((menu = menu->parent)) {
804 if (menu->next != NULL) {
805 menu = menu->next;
806 break;
807 }
808 }
809 }
810 }
811 fclose(out);
812 return 0;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815int conf_write(const char *name)
816{
Roman Zippelc955cca2006-06-08 22:12:39 -0700817 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct symbol *sym;
819 struct menu *menu;
820 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 const char *str;
Nathan Chancellor2ae89c72018-06-02 09:02:09 -0700822 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 char *env;
824
825 dirname[0] = 0;
826 if (name && name[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 char *slash;
828
Masahiro Yamada06081822018-07-20 16:46:27 +0900829 if (is_dir(name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 strcpy(dirname, name);
831 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700832 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else if ((slash = strrchr(name, '/'))) {
834 int size = slash - name + 1;
835 memcpy(dirname, name, size);
836 dirname[size] = 0;
837 if (slash[1])
838 basename = slash + 1;
839 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700840 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } else
842 basename = name;
843 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700844 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700846 sprintf(newname, "%s%s", dirname, basename);
847 env = getenv("KCONFIG_OVERWRITECONFIG");
848 if (!env || !*env) {
849 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
850 out = fopen(tmpname, "w");
851 } else {
852 *tmpname = 0;
853 out = fopen(newname, "w");
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!out)
856 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700857
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400858 conf_write_heading(out, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Karsten Wieseb3214292006-12-13 00:34:06 -0800860 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 sym_clear_all_valid();
862
863 menu = rootmenu.list;
864 while (menu) {
865 sym = menu->sym;
866 if (!sym) {
867 if (!menu_is_visible(menu))
868 goto next;
869 str = menu_get_prompt(menu);
870 fprintf(out, "\n"
871 "#\n"
872 "# %s\n"
873 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 } else if (!(sym->flags & SYMBOL_CHOICE)) {
875 sym_calc_value(sym);
876 if (!(sym->flags & SYMBOL_WRITE))
877 goto next;
878 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400879
880 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Sam Ravnborg49192f22010-07-31 23:35:33 +0200883next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (menu->list) {
885 menu = menu->list;
886 continue;
887 }
888 if (menu->next)
889 menu = menu->next;
890 else while ((menu = menu->parent)) {
891 if (menu->next) {
892 menu = menu->next;
893 break;
894 }
895 }
896 }
897 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700898
899 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200900 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700901 strcat(dirname, ".old");
902 rename(newname, dirname);
903 if (rename(tmpname, newname))
904 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200907 conf_message("configuration written to %s", newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700908
Karsten Wiesebfc10002006-12-13 00:34:07 -0800909 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return 0;
912}
Roman Zippelc955cca2006-06-08 22:12:39 -0700913
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900914/* write a dependency file as used by kbuild to track dependencies */
915static int conf_write_dep(const char *name)
916{
917 struct file *file;
918 FILE *out;
919
920 if (!name)
921 name = ".kconfig.d";
922 out = fopen("..config.tmp", "w");
923 if (!out)
924 return 1;
925 fprintf(out, "deps_config := \\\n");
926 for (file = file_list; file; file = file->next) {
927 if (file->next)
928 fprintf(out, "\t%s \\\n", file->name);
929 else
930 fprintf(out, "\t%s\n", file->name);
931 }
932 fprintf(out, "\n%s: \\\n"
933 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
934
935 env_write_dep(out, conf_get_autoconfig_name());
936
937 fprintf(out, "\n$(deps_config): ;\n");
938 fclose(out);
Masahiro Yamada79123b12018-07-20 16:46:29 +0900939
940 if (make_parent_dir(name))
941 return 1;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900942 rename("..config.tmp", name);
943 return 0;
944}
945
Masahiro Yamada0849d212018-11-30 18:15:49 +0900946static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700947{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200948 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700949 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900950 int res, i;
951
952 strcpy(depfile_path, "include/config/");
953 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700954
Markus Heidelberg12122f62009-05-18 01:36:54 +0200955 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700956 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000957 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700958
Roman Zippel2e3646e2006-06-08 22:12:42 -0700959 for_all_symbols(i, sym) {
960 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200961 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700962 continue;
963 if (sym->flags & SYMBOL_WRITE) {
964 if (sym->flags & SYMBOL_DEF_AUTO) {
965 /*
966 * symbol has old and new value,
967 * so compare them...
968 */
969 switch (sym->type) {
970 case S_BOOLEAN:
971 case S_TRISTATE:
972 if (sym_get_tristate_value(sym) ==
973 sym->def[S_DEF_AUTO].tri)
974 continue;
975 break;
976 case S_STRING:
977 case S_HEX:
978 case S_INT:
979 if (!strcmp(sym_get_string_value(sym),
980 sym->def[S_DEF_AUTO].val))
981 continue;
982 break;
983 default:
984 break;
985 }
986 } else {
987 /*
988 * If there is no old value, only 'no' (unset)
989 * is allowed as new value.
990 */
991 switch (sym->type) {
992 case S_BOOLEAN:
993 case S_TRISTATE:
994 if (sym_get_tristate_value(sym) == no)
995 continue;
996 break;
997 default:
998 break;
999 }
1000 }
1001 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1002 /* There is neither an old nor a new value. */
1003 continue;
1004 /* else
1005 * There is an old value, but no new value ('no' (unset)
1006 * isn't saved in auto.conf, so the old value is always
1007 * different from 'no').
1008 */
1009
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001010 res = conf_touch_dep(sym->name);
1011 if (res)
1012 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001013 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001014
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001015 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001016}
1017
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001018int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001019{
1020 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001021 const char *name;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001022 const char *autoconf_name = conf_get_autoconfig_name();
Michal Marekbc081dd2009-12-07 16:38:33 +01001023 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +02001024 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001025
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001026 if (!overwrite && is_present(autoconf_name))
1027 return 0;
1028
Roman Zippel2e3646e2006-06-08 22:12:42 -07001029 sym_clear_all_valid();
1030
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001031 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -07001032
Masahiro Yamada0849d212018-11-30 18:15:49 +09001033 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001034 return 1;
1035
Roman Zippelc955cca2006-06-08 22:12:39 -07001036 out = fopen(".tmpconfig", "w");
1037 if (!out)
1038 return 1;
1039
Michal Marekbc081dd2009-12-07 16:38:33 +01001040 tristate = fopen(".tmpconfig_tristate", "w");
1041 if (!tristate) {
1042 fclose(out);
1043 return 1;
1044 }
1045
Roman Zippelc955cca2006-06-08 22:12:39 -07001046 out_h = fopen(".tmpconfig.h", "w");
1047 if (!out_h) {
1048 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001049 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001050 return 1;
1051 }
1052
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001053 conf_write_heading(out, &kconfig_printer_cb, NULL);
1054
1055 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1056
1057 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001058
Roman Zippelc955cca2006-06-08 22:12:39 -07001059 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001060 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001061 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001062 continue;
1063
Paul Gortmakera9596132012-04-12 19:46:33 -04001064 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001065 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001066
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001067 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1068
1069 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001070 }
1071 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001072 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001073 fclose(out_h);
1074
1075 name = getenv("KCONFIG_AUTOHEADER");
1076 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001077 name = "include/generated/autoconf.h";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001078 if (make_parent_dir(name))
1079 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001080 if (rename(".tmpconfig.h", name))
1081 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001082
Michal Marekbc081dd2009-12-07 16:38:33 +01001083 name = getenv("KCONFIG_TRISTATE");
1084 if (!name)
1085 name = "include/config/tristate.conf";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001086 if (make_parent_dir(name))
1087 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001088 if (rename(".tmpconfig_tristate", name))
1089 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001090
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001091 if (make_parent_dir(autoconf_name))
Masahiro Yamada79123b12018-07-20 16:46:29 +09001092 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001093 /*
1094 * This must be the last step, kbuild has a dependency on auto.conf
1095 * and this marks the successful completion of the previous steps.
1096 */
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001097 if (rename(".tmpconfig", autoconf_name))
Roman Zippelc955cca2006-06-08 22:12:39 -07001098 return 1;
1099
1100 return 0;
1101}
Karsten Wieseb3214292006-12-13 00:34:06 -08001102
Karsten Wiesebfc10002006-12-13 00:34:07 -08001103static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001104static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001105
1106void sym_set_change_count(int count)
1107{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001108 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001109 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001110 if (conf_changed_callback &&
1111 (bool)_sym_change_count != (bool)count)
1112 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001113}
1114
1115void sym_add_change_count(int count)
1116{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001117 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001118}
1119
Karsten Wieseb3214292006-12-13 00:34:06 -08001120bool conf_get_changed(void)
1121{
1122 return sym_change_count;
1123}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001124
1125void conf_set_changed_callback(void (*fn)(void))
1126{
1127 conf_changed_callback = fn;
1128}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001129
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001130static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001131{
1132 struct property *prop;
1133 struct symbol *sym;
1134 struct expr *e;
1135 int cnt, def;
1136
1137 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001138 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001139 * and if no then no-one.
1140 * In both cases stop.
1141 */
1142 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001143 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001144
1145 prop = sym_get_choice_prop(csym);
1146
1147 /* count entries in choice block */
1148 cnt = 0;
1149 expr_list_for_each_sym(prop->expr, e, sym)
1150 cnt++;
1151
1152 /*
1153 * find a random value and set it to yes,
1154 * set the rest to no so we have only one set
1155 */
1156 def = (rand() % cnt);
1157
1158 cnt = 0;
1159 expr_list_for_each_sym(prop->expr, e, sym) {
1160 if (def == cnt++) {
1161 sym->def[S_DEF_USER].tri = yes;
1162 csym->def[S_DEF_USER].val = sym;
1163 }
1164 else {
1165 sym->def[S_DEF_USER].tri = no;
1166 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001167 sym->flags |= SYMBOL_DEF_USER;
1168 /* clear VALID to get value calculated */
1169 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001170 }
1171 csym->flags |= SYMBOL_DEF_USER;
1172 /* clear VALID to get value calculated */
1173 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001174
1175 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001176}
1177
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001178void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001179{
1180 struct property *prop;
1181 struct symbol *sym;
1182 struct expr *e;
1183
1184 prop = sym_get_choice_prop(csym);
1185
1186 /*
1187 * Set all non-assinged choice values to no
1188 */
1189 expr_list_for_each_sym(prop->expr, e, sym) {
1190 if (!sym_has_value(sym))
1191 sym->def[S_DEF_USER].tri = no;
1192 }
1193 csym->flags |= SYMBOL_DEF_USER;
1194 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001195 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001196}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001197
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001198bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001199{
1200 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001201 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001202 * pty: probability of tristate = y
1203 * ptm: probability of tristate = m
1204 */
1205
1206 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1207 * below, otherwise gcc whines about
1208 * -Wmaybe-uninitialized */
1209 if (mode == def_random) {
1210 int n, p[3];
1211 char *env = getenv("KCONFIG_PROBABILITY");
1212 n = 0;
1213 while( env && *env ) {
1214 char *endp;
1215 int tmp = strtol( env, &endp, 10 );
1216 if( tmp >= 0 && tmp <= 100 ) {
1217 p[n++] = tmp;
1218 } else {
1219 errno = ERANGE;
1220 perror( "KCONFIG_PROBABILITY" );
1221 exit( 1 );
1222 }
1223 env = (*endp == ':') ? endp+1 : endp;
1224 if( n >=3 ) {
1225 break;
1226 }
1227 }
1228 switch( n ) {
1229 case 1:
1230 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1231 break;
1232 case 2:
1233 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1234 break;
1235 case 3:
1236 pby = p[0]; pty = p[1]; ptm = p[2];
1237 break;
1238 }
1239
1240 if( pty+ptm > 100 ) {
1241 errno = ERANGE;
1242 perror( "KCONFIG_PROBABILITY" );
1243 exit( 1 );
1244 }
1245 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001246 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001247
1248 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001249 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001250 continue;
1251 switch (sym_get_type(sym)) {
1252 case S_BOOLEAN:
1253 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001254 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001255 switch (mode) {
1256 case def_yes:
1257 sym->def[S_DEF_USER].tri = yes;
1258 break;
1259 case def_mod:
1260 sym->def[S_DEF_USER].tri = mod;
1261 break;
1262 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001263 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1264 sym->def[S_DEF_USER].tri = yes;
1265 else
1266 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001267 break;
1268 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001269 sym->def[S_DEF_USER].tri = no;
1270 cnt = rand() % 100;
1271 if (sym->type == S_TRISTATE) {
1272 if (cnt < pty)
1273 sym->def[S_DEF_USER].tri = yes;
1274 else if (cnt < (pty+ptm))
1275 sym->def[S_DEF_USER].tri = mod;
1276 } else if (cnt < pby)
1277 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001278 break;
1279 default:
1280 continue;
1281 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001282 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001283 sym->flags |= SYMBOL_DEF_USER;
1284 break;
1285 default:
1286 break;
1287 }
1288
1289 }
1290
Al Viroce97e132008-10-26 05:12:34 +00001291 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001292
Sam Ravnborg184832c2009-03-15 11:05:12 +01001293 /*
1294 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001295 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001296 * choice symbols in one block.
1297 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001298 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001299 * selected in a choice block and we set it to yes,
1300 * and the rest to no.
1301 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001302 if (mode != def_random) {
1303 for_all_symbols(i, csym) {
1304 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1305 sym_is_choice_value(csym))
1306 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1307 }
1308 }
1309
Roman Zippeldc7862e2008-05-06 04:55:55 +02001310 for_all_symbols(i, csym) {
1311 if (sym_has_value(csym) || !sym_is_choice(csym))
1312 continue;
1313
1314 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001315 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001316 has_changed = randomize_choice_values(csym);
1317 else {
1318 set_all_choice_values(csym);
1319 has_changed = true;
1320 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001321 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001322
1323 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001324}