blob: 9fd6430c93d230be74b1b1a65197e4ac50c12a8c [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 const char *str;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900821 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 char *env;
823
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900824 if (!name)
825 name = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900827 if (!*name) {
828 fprintf(stderr, "config name is empty\n");
829 return -1;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900832 if (is_dir(name)) {
833 fprintf(stderr, "%s: Is a directory\n", name);
834 return -1;
835 }
836
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700837 env = getenv("KCONFIG_OVERWRITECONFIG");
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900838 if (env && *env) {
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700839 *tmpname = 0;
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900840 out = fopen(name, "w");
841 } else {
842 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
843 name, (int)getpid());
844 out = fopen(tmpname, "w");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!out)
847 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700848
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400849 conf_write_heading(out, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Karsten Wieseb3214292006-12-13 00:34:06 -0800851 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 sym_clear_all_valid();
853
854 menu = rootmenu.list;
855 while (menu) {
856 sym = menu->sym;
857 if (!sym) {
858 if (!menu_is_visible(menu))
859 goto next;
860 str = menu_get_prompt(menu);
861 fprintf(out, "\n"
862 "#\n"
863 "# %s\n"
864 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 } else if (!(sym->flags & SYMBOL_CHOICE)) {
866 sym_calc_value(sym);
867 if (!(sym->flags & SYMBOL_WRITE))
868 goto next;
869 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400870
871 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Sam Ravnborg49192f22010-07-31 23:35:33 +0200874next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (menu->list) {
876 menu = menu->list;
877 continue;
878 }
879 if (menu->next)
880 menu = menu->next;
881 else while ((menu = menu->parent)) {
882 if (menu->next) {
883 menu = menu->next;
884 break;
885 }
886 }
887 }
888 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700889
890 if (*tmpname) {
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900891 snprintf(oldname, sizeof(oldname), "%s.old", name);
892 rename(name, oldname);
893 if (rename(tmpname, name))
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700894 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Masahiro Yamadaceb7f322019-05-10 15:12:04 +0900897 conf_message("configuration written to %s", name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700898
Karsten Wiesebfc10002006-12-13 00:34:07 -0800899 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 return 0;
902}
Roman Zippelc955cca2006-06-08 22:12:39 -0700903
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900904/* write a dependency file as used by kbuild to track dependencies */
905static int conf_write_dep(const char *name)
906{
907 struct file *file;
908 FILE *out;
909
910 if (!name)
911 name = ".kconfig.d";
912 out = fopen("..config.tmp", "w");
913 if (!out)
914 return 1;
915 fprintf(out, "deps_config := \\\n");
916 for (file = file_list; file; file = file->next) {
917 if (file->next)
918 fprintf(out, "\t%s \\\n", file->name);
919 else
920 fprintf(out, "\t%s\n", file->name);
921 }
922 fprintf(out, "\n%s: \\\n"
923 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
924
925 env_write_dep(out, conf_get_autoconfig_name());
926
927 fprintf(out, "\n$(deps_config): ;\n");
928 fclose(out);
Masahiro Yamada79123b12018-07-20 16:46:29 +0900929
930 if (make_parent_dir(name))
931 return 1;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900932 rename("..config.tmp", name);
933 return 0;
934}
935
Masahiro Yamada0849d212018-11-30 18:15:49 +0900936static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700937{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200938 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700939 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900940 int res, i;
941
942 strcpy(depfile_path, "include/config/");
943 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700944
Markus Heidelberg12122f62009-05-18 01:36:54 +0200945 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700946 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000947 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700948
Roman Zippel2e3646e2006-06-08 22:12:42 -0700949 for_all_symbols(i, sym) {
950 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200951 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700952 continue;
953 if (sym->flags & SYMBOL_WRITE) {
954 if (sym->flags & SYMBOL_DEF_AUTO) {
955 /*
956 * symbol has old and new value,
957 * so compare them...
958 */
959 switch (sym->type) {
960 case S_BOOLEAN:
961 case S_TRISTATE:
962 if (sym_get_tristate_value(sym) ==
963 sym->def[S_DEF_AUTO].tri)
964 continue;
965 break;
966 case S_STRING:
967 case S_HEX:
968 case S_INT:
969 if (!strcmp(sym_get_string_value(sym),
970 sym->def[S_DEF_AUTO].val))
971 continue;
972 break;
973 default:
974 break;
975 }
976 } else {
977 /*
978 * If there is no old value, only 'no' (unset)
979 * is allowed as new value.
980 */
981 switch (sym->type) {
982 case S_BOOLEAN:
983 case S_TRISTATE:
984 if (sym_get_tristate_value(sym) == no)
985 continue;
986 break;
987 default:
988 break;
989 }
990 }
991 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
992 /* There is neither an old nor a new value. */
993 continue;
994 /* else
995 * There is an old value, but no new value ('no' (unset)
996 * isn't saved in auto.conf, so the old value is always
997 * different from 'no').
998 */
999
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001000 res = conf_touch_dep(sym->name);
1001 if (res)
1002 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001003 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001004
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001005 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001006}
1007
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001008int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001009{
1010 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001011 const char *name;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001012 const char *autoconf_name = conf_get_autoconfig_name();
Michal Marekbc081dd2009-12-07 16:38:33 +01001013 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +02001014 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001015
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001016 if (!overwrite && is_present(autoconf_name))
1017 return 0;
1018
Roman Zippel2e3646e2006-06-08 22:12:42 -07001019 sym_clear_all_valid();
1020
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001021 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -07001022
Masahiro Yamada0849d212018-11-30 18:15:49 +09001023 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001024 return 1;
1025
Roman Zippelc955cca2006-06-08 22:12:39 -07001026 out = fopen(".tmpconfig", "w");
1027 if (!out)
1028 return 1;
1029
Michal Marekbc081dd2009-12-07 16:38:33 +01001030 tristate = fopen(".tmpconfig_tristate", "w");
1031 if (!tristate) {
1032 fclose(out);
1033 return 1;
1034 }
1035
Roman Zippelc955cca2006-06-08 22:12:39 -07001036 out_h = fopen(".tmpconfig.h", "w");
1037 if (!out_h) {
1038 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001039 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001040 return 1;
1041 }
1042
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001043 conf_write_heading(out, &kconfig_printer_cb, NULL);
1044
1045 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1046
1047 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001048
Roman Zippelc955cca2006-06-08 22:12:39 -07001049 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001050 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001051 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001052 continue;
1053
Paul Gortmakera9596132012-04-12 19:46:33 -04001054 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001055 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001056
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001057 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1058
1059 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001060 }
1061 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001062 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001063 fclose(out_h);
1064
1065 name = getenv("KCONFIG_AUTOHEADER");
1066 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001067 name = "include/generated/autoconf.h";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001068 if (make_parent_dir(name))
1069 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001070 if (rename(".tmpconfig.h", name))
1071 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001072
Michal Marekbc081dd2009-12-07 16:38:33 +01001073 name = getenv("KCONFIG_TRISTATE");
1074 if (!name)
1075 name = "include/config/tristate.conf";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001076 if (make_parent_dir(name))
1077 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001078 if (rename(".tmpconfig_tristate", name))
1079 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001080
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001081 if (make_parent_dir(autoconf_name))
Masahiro Yamada79123b12018-07-20 16:46:29 +09001082 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001083 /*
1084 * This must be the last step, kbuild has a dependency on auto.conf
1085 * and this marks the successful completion of the previous steps.
1086 */
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001087 if (rename(".tmpconfig", autoconf_name))
Roman Zippelc955cca2006-06-08 22:12:39 -07001088 return 1;
1089
1090 return 0;
1091}
Karsten Wieseb3214292006-12-13 00:34:06 -08001092
Karsten Wiesebfc10002006-12-13 00:34:07 -08001093static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001094static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001095
1096void sym_set_change_count(int count)
1097{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001098 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001099 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001100 if (conf_changed_callback &&
1101 (bool)_sym_change_count != (bool)count)
1102 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001103}
1104
1105void sym_add_change_count(int count)
1106{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001107 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001108}
1109
Karsten Wieseb3214292006-12-13 00:34:06 -08001110bool conf_get_changed(void)
1111{
1112 return sym_change_count;
1113}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001114
1115void conf_set_changed_callback(void (*fn)(void))
1116{
1117 conf_changed_callback = fn;
1118}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001119
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001120static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001121{
1122 struct property *prop;
1123 struct symbol *sym;
1124 struct expr *e;
1125 int cnt, def;
1126
1127 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001128 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001129 * and if no then no-one.
1130 * In both cases stop.
1131 */
1132 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001133 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001134
1135 prop = sym_get_choice_prop(csym);
1136
1137 /* count entries in choice block */
1138 cnt = 0;
1139 expr_list_for_each_sym(prop->expr, e, sym)
1140 cnt++;
1141
1142 /*
1143 * find a random value and set it to yes,
1144 * set the rest to no so we have only one set
1145 */
1146 def = (rand() % cnt);
1147
1148 cnt = 0;
1149 expr_list_for_each_sym(prop->expr, e, sym) {
1150 if (def == cnt++) {
1151 sym->def[S_DEF_USER].tri = yes;
1152 csym->def[S_DEF_USER].val = sym;
1153 }
1154 else {
1155 sym->def[S_DEF_USER].tri = no;
1156 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001157 sym->flags |= SYMBOL_DEF_USER;
1158 /* clear VALID to get value calculated */
1159 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001160 }
1161 csym->flags |= SYMBOL_DEF_USER;
1162 /* clear VALID to get value calculated */
1163 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001164
1165 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001166}
1167
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001168void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001169{
1170 struct property *prop;
1171 struct symbol *sym;
1172 struct expr *e;
1173
1174 prop = sym_get_choice_prop(csym);
1175
1176 /*
1177 * Set all non-assinged choice values to no
1178 */
1179 expr_list_for_each_sym(prop->expr, e, sym) {
1180 if (!sym_has_value(sym))
1181 sym->def[S_DEF_USER].tri = no;
1182 }
1183 csym->flags |= SYMBOL_DEF_USER;
1184 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001185 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001186}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001187
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001188bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001189{
1190 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001191 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001192 * pty: probability of tristate = y
1193 * ptm: probability of tristate = m
1194 */
1195
1196 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1197 * below, otherwise gcc whines about
1198 * -Wmaybe-uninitialized */
1199 if (mode == def_random) {
1200 int n, p[3];
1201 char *env = getenv("KCONFIG_PROBABILITY");
1202 n = 0;
1203 while( env && *env ) {
1204 char *endp;
1205 int tmp = strtol( env, &endp, 10 );
1206 if( tmp >= 0 && tmp <= 100 ) {
1207 p[n++] = tmp;
1208 } else {
1209 errno = ERANGE;
1210 perror( "KCONFIG_PROBABILITY" );
1211 exit( 1 );
1212 }
1213 env = (*endp == ':') ? endp+1 : endp;
1214 if( n >=3 ) {
1215 break;
1216 }
1217 }
1218 switch( n ) {
1219 case 1:
1220 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1221 break;
1222 case 2:
1223 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1224 break;
1225 case 3:
1226 pby = p[0]; pty = p[1]; ptm = p[2];
1227 break;
1228 }
1229
1230 if( pty+ptm > 100 ) {
1231 errno = ERANGE;
1232 perror( "KCONFIG_PROBABILITY" );
1233 exit( 1 );
1234 }
1235 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001236 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001237
1238 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001239 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001240 continue;
1241 switch (sym_get_type(sym)) {
1242 case S_BOOLEAN:
1243 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001244 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001245 switch (mode) {
1246 case def_yes:
1247 sym->def[S_DEF_USER].tri = yes;
1248 break;
1249 case def_mod:
1250 sym->def[S_DEF_USER].tri = mod;
1251 break;
1252 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001253 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1254 sym->def[S_DEF_USER].tri = yes;
1255 else
1256 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001257 break;
1258 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001259 sym->def[S_DEF_USER].tri = no;
1260 cnt = rand() % 100;
1261 if (sym->type == S_TRISTATE) {
1262 if (cnt < pty)
1263 sym->def[S_DEF_USER].tri = yes;
1264 else if (cnt < (pty+ptm))
1265 sym->def[S_DEF_USER].tri = mod;
1266 } else if (cnt < pby)
1267 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001268 break;
1269 default:
1270 continue;
1271 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001272 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001273 sym->flags |= SYMBOL_DEF_USER;
1274 break;
1275 default:
1276 break;
1277 }
1278
1279 }
1280
Al Viroce97e132008-10-26 05:12:34 +00001281 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001282
Sam Ravnborg184832c2009-03-15 11:05:12 +01001283 /*
1284 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001285 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001286 * choice symbols in one block.
1287 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001288 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001289 * selected in a choice block and we set it to yes,
1290 * and the rest to no.
1291 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001292 if (mode != def_random) {
1293 for_all_symbols(i, csym) {
1294 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1295 sym_is_choice_value(csym))
1296 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1297 }
1298 }
1299
Roman Zippeldc7862e2008-05-06 04:55:55 +02001300 for_all_symbols(i, csym) {
1301 if (sym_has_value(csym) || !sym_is_choice(csym))
1302 continue;
1303
1304 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001305 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001306 has_changed = randomize_choice_values(csym);
1307 else {
1308 set_all_choice_values(csym);
1309 has_changed = true;
1310 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001311 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001312
1313 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001314}