blob: 39dfe463de15a76a5314581bf0645ad1b85a842a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
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>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040010#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <unistd.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "lkc.h"
18
Masahiro Yamada06081822018-07-20 16:46:27 +090019/* return true if 'path' exists, false otherwise */
20static bool is_present(const char *path)
21{
22 struct stat st;
23
24 return !stat(path, &st);
25}
26
27/* return true if 'path' exists and it is a directory, false otherwise */
28static bool is_dir(const char *path)
29{
30 struct stat st;
31
32 if (stat(path, &st))
33 return 0;
34
35 return S_ISDIR(st.st_mode);
36}
37
38/*
39 * Create the parent directory of the given path.
40 *
41 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
42 */
43static int make_parent_dir(const char *path)
44{
45 char tmp[PATH_MAX + 1];
46 char *p;
47
48 strncpy(tmp, path, sizeof(tmp));
49 tmp[sizeof(tmp) - 1] = 0;
50
51 /* Remove the base name. Just return if nothing is left */
52 p = strrchr(tmp, '/');
53 if (!p)
54 return 0;
55 *(p + 1) = 0;
56
57 /* Just in case it is an absolute path */
58 p = tmp;
59 while (*p == '/')
60 p++;
61
62 while ((p = strchr(p, '/'))) {
63 *p = 0;
64
65 /* skip if the directory exists */
66 if (!is_dir(tmp) && mkdir(tmp, 0755))
67 return -1;
68
69 *p = '/';
70 while (*p == '/')
71 p++;
72 }
73
74 return 0;
75}
76
Masahiro Yamada1508fec2018-11-30 18:15:50 +090077static char depfile_path[PATH_MAX];
78static size_t depfile_prefix_len;
79
80/* touch depfile for symbol 'name' */
81static int conf_touch_dep(const char *name)
82{
83 int fd, ret;
84 const char *s;
85 char *d, c;
86
87 /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
88 if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
89 return -1;
90
91 d = depfile_path + depfile_prefix_len;
92 s = name;
93
94 while ((c = *s++))
95 *d++ = (c == '_') ? '/' : tolower(c);
96 strcpy(d, ".h");
97
98 /* Assume directory path already exists. */
99 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
100 if (fd == -1) {
101 if (errno != ENOENT)
102 return -1;
103
104 ret = make_parent_dir(depfile_path);
105 if (ret)
106 return ret;
107
108 /* Try it again. */
109 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
110 if (fd == -1)
111 return -1;
112 }
113 close(fd);
114
115 return 0;
116}
117
Michal Marekad8d40c2015-02-24 16:37:13 +0100118struct conf_printer {
119 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
120 void (*print_comment)(FILE *, const char *, void *);
121};
122
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800123static void conf_warning(const char *fmt, ...)
124 __attribute__ ((format (printf, 1, 2)));
125
Michal Marek42368c32010-08-17 10:21:19 +0200126static void conf_message(const char *fmt, ...)
127 __attribute__ ((format (printf, 1, 2)));
128
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800129static const char *conf_filename;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900130static int conf_lineno, conf_warnings;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800131
Masahiro Yamada104daea2018-05-28 18:21:40 +0900132const char conf_defname[] = "arch/$(ARCH)/defconfig";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800134static void conf_warning(const char *fmt, ...)
135{
136 va_list ap;
137 va_start(ap, fmt);
138 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
139 vfprintf(stderr, fmt, ap);
140 fprintf(stderr, "\n");
141 va_end(ap);
142 conf_warnings++;
143}
144
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900145static void conf_default_message_callback(const char *s)
Michal Marek42368c32010-08-17 10:21:19 +0200146{
147 printf("#\n# ");
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900148 printf("%s", s);
Michal Marek42368c32010-08-17 10:21:19 +0200149 printf("\n#\n");
150}
151
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900152static void (*conf_message_callback)(const char *s) =
Michal Marek42368c32010-08-17 10:21:19 +0200153 conf_default_message_callback;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900154void conf_set_message_callback(void (*fn)(const char *s))
Michal Marek42368c32010-08-17 10:21:19 +0200155{
156 conf_message_callback = fn;
157}
158
159static void conf_message(const char *fmt, ...)
160{
161 va_list ap;
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900162 char buf[4096];
163
164 if (!conf_message_callback)
165 return;
Michal Marek42368c32010-08-17 10:21:19 +0200166
167 va_start(ap, fmt);
Masahiro Yamada5accd7f2018-07-05 11:46:12 +0900168
169 vsnprintf(buf, sizeof(buf), fmt, ap);
170 conf_message_callback(buf);
Colin Ian Kingb6a2ab22015-01-12 13:18:26 +0000171 va_end(ap);
Michal Marek42368c32010-08-17 10:21:19 +0200172}
173
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700174const char *conf_get_configname(void)
175{
176 char *name = getenv("KCONFIG_CONFIG");
177
178 return name ? name : ".config";
179}
180
Markus Heidelberg12122f62009-05-18 01:36:54 +0200181const char *conf_get_autoconfig_name(void)
182{
183 char *name = getenv("KCONFIG_AUTOCONFIG");
184
185 return name ? name : "include/config/auto.conf";
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188char *conf_get_default_confname(void)
189{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 static char fullname[PATH_MAX+1];
191 char *env, *name;
192
Masahiro Yamada104daea2018-05-28 18:21:40 +0900193 name = expand_string(conf_defname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 env = getenv(SRCTREE);
195 if (env) {
196 sprintf(fullname, "%s/%s", env, name);
Masahiro Yamada06081822018-07-20 16:46:27 +0900197 if (is_present(fullname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return fullname;
199 }
200 return name;
201}
202
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100203static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
204{
205 char *p2;
206
207 switch (sym->type) {
208 case S_TRISTATE:
209 if (p[0] == 'm') {
210 sym->def[def].tri = mod;
211 sym->flags |= def_flags;
212 break;
213 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400214 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100215 case S_BOOLEAN:
216 if (p[0] == 'y') {
217 sym->def[def].tri = yes;
218 sym->flags |= def_flags;
219 break;
220 }
221 if (p[0] == 'n') {
222 sym->def[def].tri = no;
223 sym->flags |= def_flags;
224 break;
225 }
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200226 if (def != S_DEF_AUTO)
227 conf_warning("symbol value '%s' invalid for %s",
228 p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400229 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100230 case S_OTHER:
231 if (*p != '"') {
232 for (p2 = p; *p2 && !isspace(*p2); p2++)
233 ;
234 sym->type = S_STRING;
235 goto done;
236 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400237 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100238 case S_STRING:
239 if (*p++ != '"')
240 break;
241 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
242 if (*p2 == '"') {
243 *p2 = 0;
244 break;
245 }
246 memmove(p2, p2 + 1, strlen(p2));
247 }
248 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200249 if (def != S_DEF_AUTO)
250 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100251 return 1;
252 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400253 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100254 case S_INT:
255 case S_HEX:
256 done:
257 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900258 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100259 sym->flags |= def_flags;
260 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200261 if (def != S_DEF_AUTO)
262 conf_warning("symbol value '%s' invalid for %s",
263 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100264 return 1;
265 }
266 break;
267 default:
268 ;
269 }
270 return 0;
271}
272
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700273#define LINE_GROWTH 16
274static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
275{
276 char *nline;
277 size_t new_size = slen + 1;
278 if (new_size > *n) {
279 new_size += LINE_GROWTH - 1;
280 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900281 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700282 if (!nline)
283 return -1;
284
285 *lineptr = nline;
286 *n = new_size;
287 }
288
289 (*lineptr)[slen] = c;
290
291 return 0;
292}
293
294static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
295{
296 char *line = *lineptr;
297 size_t slen = 0;
298
299 for (;;) {
300 int c = getc(stream);
301
302 switch (c) {
303 case '\n':
304 if (add_byte(c, &line, slen, n) < 0)
305 goto e_out;
306 slen++;
307 /* fall through */
308 case EOF:
309 if (add_byte('\0', &line, slen, n) < 0)
310 goto e_out;
311 *lineptr = line;
312 if (slen == 0)
313 return -1;
314 return slen;
315 default:
316 if (add_byte(c, &line, slen, n) < 0)
317 goto e_out;
318 slen++;
319 }
320 }
321
322e_out:
323 line[slen-1] = '\0';
324 *lineptr = line;
325 return -1;
326}
327
Roman Zippel669bfad92006-06-08 22:12:42 -0700328int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700331 char *line = NULL;
332 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700335 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (name) {
338 in = zconf_fopen(name);
339 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700340 struct property *prop;
341
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700342 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700343 in = zconf_fopen(name);
344 if (in)
345 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800346 sym_add_change_count(1);
Al Viro6b87b702016-01-14 18:13:49 +0000347 if (!sym_defconfig_list)
Roman Zippelface4372006-06-08 22:12:45 -0700348 return 1;
349
350 for_all_defaults(sym_defconfig_list, prop) {
351 if (expr_calc_value(prop->visible.expr) == no ||
352 prop->expr->type != E_SYMBOL)
353 continue;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900354 sym_calc_value(prop->expr->left.sym);
355 name = sym_get_string_value(prop->expr->left.sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 in = zconf_fopen(name);
357 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200358 conf_message("using defaults found in %s",
Michal Marek42368c32010-08-17 10:21:19 +0200359 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700360 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 }
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!in)
365 return 1;
366
Roman Zippelddc97ca2006-06-08 22:12:38 -0700367load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800368 conf_filename = name;
369 conf_lineno = 0;
370 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800371
Roman Zippel669bfad92006-06-08 22:12:42 -0700372 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700374 sym->flags |= SYMBOL_CHANGED;
375 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200376 if (sym_is_choice(sym))
377 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 switch (sym->type) {
379 case S_INT:
380 case S_HEX:
381 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700382 if (sym->def[def].val)
383 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400384 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700386 sym->def[def].val = NULL;
387 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 }
390
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700391 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800392 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400394 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400395 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400397 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!p)
399 continue;
400 *p++ = 0;
401 if (strncmp(p, "is not set", 10))
402 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700403 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400404 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200405 if (!sym) {
406 sym_add_change_count(1);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900407 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200408 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700409 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400410 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700411 if (sym->type == S_UNKNOWN)
412 sym->type = S_BOOLEAN;
413 }
414 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100415 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 switch (sym->type) {
418 case S_BOOLEAN:
419 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700420 sym->def[def].tri = no;
421 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 default:
424 ;
425 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400426 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
427 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!p)
429 continue;
430 *p++ = 0;
431 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600432 if (p2) {
433 *p2-- = 0;
434 if (*p2 == '\r')
435 *p2 = 0;
436 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700437 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400438 sym = sym_find(line + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200439 if (!sym) {
440 sym_add_change_count(1);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900441 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200442 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700443 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400444 sym = sym_lookup(line + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700445 if (sym->type == S_UNKNOWN)
446 sym->type = S_OTHER;
447 }
448 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100449 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100451 if (conf_set_sym_val(sym, def, def_flags, p))
452 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400453 } else {
454 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100455 conf_warning("unexpected data: %.*s",
456 (int)strcspn(line, "\r\n"), line);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 continue;
459 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (sym && sym_is_choice_value(sym)) {
462 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700463 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case no:
465 break;
466 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700467 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800468 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200469 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100473 if (cs->def[def].tri != no)
474 conf_warning("override: %s changes choice state", sym->name);
475 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100478 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700481 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800483 return 0;
484}
485
486int conf_read(const char *name)
487{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500488 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900489 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500490 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800491
Karsten Wiesebfc10002006-12-13 00:34:07 -0800492 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700493
Al Viro6b87b702016-01-14 18:13:49 +0000494 if (conf_read_simple(name, S_DEF_USER)) {
495 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800496 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000497 }
498
499 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 for_all_symbols(i, sym) {
502 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200503 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500504 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800505 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
506 /* check that calculated value agrees with saved value */
507 switch (sym->type) {
508 case S_BOOLEAN:
509 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700510 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800511 break;
512 if (!sym_is_choice(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500513 continue;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400514 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800515 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700516 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500517 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800518 break;
519 }
520 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
521 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500522 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800523 conf_unsaved++;
524 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Roman Zippeld8982ba2007-07-09 11:43:58 -0700527 for_all_symbols(i, sym) {
528 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
529 /* Reset values of generates values, so they'll appear
530 * as new, if they should become visible, but that
531 * doesn't quite work if the Kconfig and the saved
532 * configuration disagree.
533 */
534 if (sym->visible == no && !conf_unsaved)
535 sym->flags &= ~SYMBOL_DEF_USER;
536 switch (sym->type) {
537 case S_STRING:
538 case S_INT:
539 case S_HEX:
540 /* Reset a string value if it's out of range */
541 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
542 break;
543 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
544 conf_unsaved++;
545 break;
546 default:
547 break;
548 }
549 }
550 }
551
Karsten Wiesebfc10002006-12-13 00:34:07 -0800552 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return 0;
555}
556
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400557/*
558 * Kconfig configuration printer
559 *
560 * This printer is used when generating the resulting configuration after
561 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
562 * passing a non-NULL argument to the printer.
563 *
564 */
565static void
566kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200567{
Sam Ravnborg49192f22010-07-31 23:35:33 +0200568
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500569 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200570 case S_BOOLEAN:
571 case S_TRISTATE:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400572 if (*value == 'n') {
573 bool skip_unset = (arg != NULL);
574
575 if (!skip_unset)
576 fprintf(fp, "# %s%s is not set\n",
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400577 CONFIG_, sym->name);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400578 return;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200579 }
580 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400581 default:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200582 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400583 }
584
585 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
586}
587
588static void
589kconfig_print_comment(FILE *fp, const char *value, void *arg)
590{
591 const char *p = value;
592 size_t l;
593
594 for (;;) {
595 l = strcspn(p, "\n");
596 fprintf(fp, "#");
597 if (l) {
598 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400599 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400600 p += l;
601 }
602 fprintf(fp, "\n");
603 if (*p++ == '\0')
604 break;
605 }
606}
607
608static struct conf_printer kconfig_printer_cb =
609{
610 .print_symbol = kconfig_print_symbol,
611 .print_comment = kconfig_print_comment,
612};
613
614/*
615 * Header printer
616 *
617 * This printer is used when generating the `include/generated/autoconf.h' file.
618 */
619static void
620header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
621{
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400622
623 switch (sym->type) {
624 case S_BOOLEAN:
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400625 case S_TRISTATE: {
626 const char *suffix = "";
627
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400628 switch (*value) {
629 case 'n':
Michal Marek2a11c8e2011-07-20 17:38:57 +0200630 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400631 case 'm':
632 suffix = "_MODULE";
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400633 /* fall through */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400634 default:
Michal Marek2a11c8e2011-07-20 17:38:57 +0200635 fprintf(fp, "#define %s%s%s 1\n",
636 CONFIG_, sym->name, suffix);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400637 }
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400638 break;
639 }
640 case S_HEX: {
641 const char *prefix = "";
642
643 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
644 prefix = "0x";
645 fprintf(fp, "#define %s%s %s%s\n",
646 CONFIG_, sym->name, prefix, value);
647 break;
648 }
649 case S_STRING:
650 case S_INT:
651 fprintf(fp, "#define %s%s %s\n",
652 CONFIG_, sym->name, value);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200653 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400654 default:
655 break;
656 }
657
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400658}
659
660static void
661header_print_comment(FILE *fp, const char *value, void *arg)
662{
663 const char *p = value;
664 size_t l;
665
666 fprintf(fp, "/*\n");
667 for (;;) {
668 l = strcspn(p, "\n");
669 fprintf(fp, " *");
670 if (l) {
671 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400672 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400673 p += l;
674 }
675 fprintf(fp, "\n");
676 if (*p++ == '\0')
677 break;
678 }
679 fprintf(fp, " */\n");
680}
681
682static struct conf_printer header_printer_cb =
683{
684 .print_symbol = header_print_symbol,
685 .print_comment = header_print_comment,
686};
687
688/*
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400689 * Tristate printer
690 *
691 * This printer is used when generating the `include/config/tristate.conf' file.
692 */
693static void
694tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
695{
696
697 if (sym->type == S_TRISTATE && *value != 'n')
698 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
699}
700
701static struct conf_printer tristate_printer_cb =
702{
703 .print_symbol = tristate_print_symbol,
704 .print_comment = kconfig_print_comment,
705};
706
707static void conf_write_symbol(FILE *fp, struct symbol *sym,
708 struct conf_printer *printer, void *printer_arg)
709{
710 const char *str;
711
712 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200713 case S_OTHER:
714 case S_UNKNOWN:
715 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400716 case S_STRING:
717 str = sym_get_string_value(sym);
718 str = sym_escape_string_value(str);
719 printer->print_symbol(fp, sym, str, printer_arg);
720 free((void *)str);
721 break;
722 default:
723 str = sym_get_string_value(sym);
724 printer->print_symbol(fp, sym, str, printer_arg);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200725 }
726}
727
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400728static void
729conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
730{
731 char buf[256];
732
733 snprintf(buf, sizeof(buf),
734 "\n"
735 "Automatically generated file; DO NOT EDIT.\n"
736 "%s\n",
737 rootmenu.prompt->text);
738
739 printer->print_comment(fp, buf, printer_arg);
740}
741
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200742/*
743 * Write out a minimal config.
744 * All values that has default values are skipped as this is redundant.
745 */
746int conf_write_defconfig(const char *filename)
747{
748 struct symbol *sym;
749 struct menu *menu;
750 FILE *out;
751
752 out = fopen(filename, "w");
753 if (!out)
754 return 1;
755
756 sym_clear_all_valid();
757
758 /* Traverse all menus to find all relevant symbols */
759 menu = rootmenu.list;
760
761 while (menu != NULL)
762 {
763 sym = menu->sym;
764 if (sym == NULL) {
765 if (!menu_is_visible(menu))
766 goto next_menu;
767 } else if (!sym_is_choice(sym)) {
768 sym_calc_value(sym);
769 if (!(sym->flags & SYMBOL_WRITE))
770 goto next_menu;
771 sym->flags &= ~SYMBOL_WRITE;
772 /* If we cannot change the symbol - skip */
773 if (!sym_is_changable(sym))
774 goto next_menu;
775 /* If symbol equals to default value - skip */
776 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
777 goto next_menu;
778
779 /*
780 * If symbol is a choice value and equals to the
781 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200782 * But only if value is bool and equal to "y" and
783 * choice is not "optional".
784 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200785 */
786 if (sym_is_choice_value(sym)) {
787 struct symbol *cs;
788 struct symbol *ds;
789
790 cs = prop_get_symbol(sym_get_choice_prop(sym));
791 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200792 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200793 if ((sym->type == S_BOOLEAN) &&
794 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200795 goto next_menu;
796 }
797 }
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400798 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200799 }
800next_menu:
801 if (menu->list != NULL) {
802 menu = menu->list;
803 }
804 else if (menu->next != NULL) {
805 menu = menu->next;
806 } else {
807 while ((menu = menu->parent)) {
808 if (menu->next != NULL) {
809 menu = menu->next;
810 break;
811 }
812 }
813 }
814 }
815 fclose(out);
816 return 0;
817}
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819int conf_write(const char *name)
820{
Roman Zippelc955cca2006-06-08 22:12:39 -0700821 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct symbol *sym;
823 struct menu *menu;
824 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 const char *str;
Nathan Chancellor2ae89c72018-06-02 09:02:09 -0700826 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 char *env;
828
829 dirname[0] = 0;
830 if (name && name[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 char *slash;
832
Masahiro Yamada06081822018-07-20 16:46:27 +0900833 if (is_dir(name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 strcpy(dirname, name);
835 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700836 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else if ((slash = strrchr(name, '/'))) {
838 int size = slash - name + 1;
839 memcpy(dirname, name, size);
840 dirname[size] = 0;
841 if (slash[1])
842 basename = slash + 1;
843 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700844 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 } else
846 basename = name;
847 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700848 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700850 sprintf(newname, "%s%s", dirname, basename);
851 env = getenv("KCONFIG_OVERWRITECONFIG");
852 if (!env || !*env) {
853 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
854 out = fopen(tmpname, "w");
855 } else {
856 *tmpname = 0;
857 out = fopen(newname, "w");
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!out)
860 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700861
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400862 conf_write_heading(out, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Karsten Wieseb3214292006-12-13 00:34:06 -0800864 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 sym_clear_all_valid();
866
867 menu = rootmenu.list;
868 while (menu) {
869 sym = menu->sym;
870 if (!sym) {
871 if (!menu_is_visible(menu))
872 goto next;
873 str = menu_get_prompt(menu);
874 fprintf(out, "\n"
875 "#\n"
876 "# %s\n"
877 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else if (!(sym->flags & SYMBOL_CHOICE)) {
879 sym_calc_value(sym);
880 if (!(sym->flags & SYMBOL_WRITE))
881 goto next;
882 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400883
884 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
Sam Ravnborg49192f22010-07-31 23:35:33 +0200887next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (menu->list) {
889 menu = menu->list;
890 continue;
891 }
892 if (menu->next)
893 menu = menu->next;
894 else while ((menu = menu->parent)) {
895 if (menu->next) {
896 menu = menu->next;
897 break;
898 }
899 }
900 }
901 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700902
903 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200904 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700905 strcat(dirname, ".old");
906 rename(newname, dirname);
907 if (rename(tmpname, newname))
908 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200911 conf_message("configuration written to %s", newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700912
Karsten Wiesebfc10002006-12-13 00:34:07 -0800913 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 return 0;
916}
Roman Zippelc955cca2006-06-08 22:12:39 -0700917
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900918/* write a dependency file as used by kbuild to track dependencies */
919static int conf_write_dep(const char *name)
920{
921 struct file *file;
922 FILE *out;
923
924 if (!name)
925 name = ".kconfig.d";
926 out = fopen("..config.tmp", "w");
927 if (!out)
928 return 1;
929 fprintf(out, "deps_config := \\\n");
930 for (file = file_list; file; file = file->next) {
931 if (file->next)
932 fprintf(out, "\t%s \\\n", file->name);
933 else
934 fprintf(out, "\t%s\n", file->name);
935 }
936 fprintf(out, "\n%s: \\\n"
937 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
938
939 env_write_dep(out, conf_get_autoconfig_name());
940
941 fprintf(out, "\n$(deps_config): ;\n");
942 fclose(out);
Masahiro Yamada79123b12018-07-20 16:46:29 +0900943
944 if (make_parent_dir(name))
945 return 1;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900946 rename("..config.tmp", name);
947 return 0;
948}
949
Masahiro Yamada0849d212018-11-30 18:15:49 +0900950static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700951{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200952 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700953 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900954 int res, i;
955
956 strcpy(depfile_path, "include/config/");
957 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700958
Markus Heidelberg12122f62009-05-18 01:36:54 +0200959 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700960 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000961 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700962
Roman Zippel2e3646e2006-06-08 22:12:42 -0700963 for_all_symbols(i, sym) {
964 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200965 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700966 continue;
967 if (sym->flags & SYMBOL_WRITE) {
968 if (sym->flags & SYMBOL_DEF_AUTO) {
969 /*
970 * symbol has old and new value,
971 * so compare them...
972 */
973 switch (sym->type) {
974 case S_BOOLEAN:
975 case S_TRISTATE:
976 if (sym_get_tristate_value(sym) ==
977 sym->def[S_DEF_AUTO].tri)
978 continue;
979 break;
980 case S_STRING:
981 case S_HEX:
982 case S_INT:
983 if (!strcmp(sym_get_string_value(sym),
984 sym->def[S_DEF_AUTO].val))
985 continue;
986 break;
987 default:
988 break;
989 }
990 } else {
991 /*
992 * If there is no old value, only 'no' (unset)
993 * is allowed as new value.
994 */
995 switch (sym->type) {
996 case S_BOOLEAN:
997 case S_TRISTATE:
998 if (sym_get_tristate_value(sym) == no)
999 continue;
1000 break;
1001 default:
1002 break;
1003 }
1004 }
1005 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1006 /* There is neither an old nor a new value. */
1007 continue;
1008 /* else
1009 * There is an old value, but no new value ('no' (unset)
1010 * isn't saved in auto.conf, so the old value is always
1011 * different from 'no').
1012 */
1013
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001014 res = conf_touch_dep(sym->name);
1015 if (res)
1016 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001017 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001018
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001019 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001020}
1021
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001022int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001023{
1024 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001025 const char *name;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001026 const char *autoconf_name = conf_get_autoconfig_name();
Michal Marekbc081dd2009-12-07 16:38:33 +01001027 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +02001028 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001029
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001030 if (!overwrite && is_present(autoconf_name))
1031 return 0;
1032
Roman Zippel2e3646e2006-06-08 22:12:42 -07001033 sym_clear_all_valid();
1034
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001035 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -07001036
Masahiro Yamada0849d212018-11-30 18:15:49 +09001037 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001038 return 1;
1039
Roman Zippelc955cca2006-06-08 22:12:39 -07001040 out = fopen(".tmpconfig", "w");
1041 if (!out)
1042 return 1;
1043
Michal Marekbc081dd2009-12-07 16:38:33 +01001044 tristate = fopen(".tmpconfig_tristate", "w");
1045 if (!tristate) {
1046 fclose(out);
1047 return 1;
1048 }
1049
Roman Zippelc955cca2006-06-08 22:12:39 -07001050 out_h = fopen(".tmpconfig.h", "w");
1051 if (!out_h) {
1052 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001053 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001054 return 1;
1055 }
1056
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001057 conf_write_heading(out, &kconfig_printer_cb, NULL);
1058
1059 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1060
1061 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001062
Roman Zippelc955cca2006-06-08 22:12:39 -07001063 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001064 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001065 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001066 continue;
1067
Paul Gortmakera9596132012-04-12 19:46:33 -04001068 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001069 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001070
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001071 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1072
1073 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001074 }
1075 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001076 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001077 fclose(out_h);
1078
1079 name = getenv("KCONFIG_AUTOHEADER");
1080 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001081 name = "include/generated/autoconf.h";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001082 if (make_parent_dir(name))
1083 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001084 if (rename(".tmpconfig.h", name))
1085 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001086
Michal Marekbc081dd2009-12-07 16:38:33 +01001087 name = getenv("KCONFIG_TRISTATE");
1088 if (!name)
1089 name = "include/config/tristate.conf";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001090 if (make_parent_dir(name))
1091 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001092 if (rename(".tmpconfig_tristate", name))
1093 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001094
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001095 if (make_parent_dir(autoconf_name))
Masahiro Yamada79123b12018-07-20 16:46:29 +09001096 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001097 /*
1098 * This must be the last step, kbuild has a dependency on auto.conf
1099 * and this marks the successful completion of the previous steps.
1100 */
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001101 if (rename(".tmpconfig", autoconf_name))
Roman Zippelc955cca2006-06-08 22:12:39 -07001102 return 1;
1103
1104 return 0;
1105}
Karsten Wieseb3214292006-12-13 00:34:06 -08001106
Karsten Wiesebfc10002006-12-13 00:34:07 -08001107static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001108static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001109
1110void sym_set_change_count(int count)
1111{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001112 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001113 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001114 if (conf_changed_callback &&
1115 (bool)_sym_change_count != (bool)count)
1116 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001117}
1118
1119void sym_add_change_count(int count)
1120{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001121 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001122}
1123
Karsten Wieseb3214292006-12-13 00:34:06 -08001124bool conf_get_changed(void)
1125{
1126 return sym_change_count;
1127}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001128
1129void conf_set_changed_callback(void (*fn)(void))
1130{
1131 conf_changed_callback = fn;
1132}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001133
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001134static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001135{
1136 struct property *prop;
1137 struct symbol *sym;
1138 struct expr *e;
1139 int cnt, def;
1140
1141 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001142 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001143 * and if no then no-one.
1144 * In both cases stop.
1145 */
1146 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001147 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001148
1149 prop = sym_get_choice_prop(csym);
1150
1151 /* count entries in choice block */
1152 cnt = 0;
1153 expr_list_for_each_sym(prop->expr, e, sym)
1154 cnt++;
1155
1156 /*
1157 * find a random value and set it to yes,
1158 * set the rest to no so we have only one set
1159 */
1160 def = (rand() % cnt);
1161
1162 cnt = 0;
1163 expr_list_for_each_sym(prop->expr, e, sym) {
1164 if (def == cnt++) {
1165 sym->def[S_DEF_USER].tri = yes;
1166 csym->def[S_DEF_USER].val = sym;
1167 }
1168 else {
1169 sym->def[S_DEF_USER].tri = no;
1170 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001171 sym->flags |= SYMBOL_DEF_USER;
1172 /* clear VALID to get value calculated */
1173 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001174 }
1175 csym->flags |= SYMBOL_DEF_USER;
1176 /* clear VALID to get value calculated */
1177 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001178
1179 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001180}
1181
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001182void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001183{
1184 struct property *prop;
1185 struct symbol *sym;
1186 struct expr *e;
1187
1188 prop = sym_get_choice_prop(csym);
1189
1190 /*
1191 * Set all non-assinged choice values to no
1192 */
1193 expr_list_for_each_sym(prop->expr, e, sym) {
1194 if (!sym_has_value(sym))
1195 sym->def[S_DEF_USER].tri = no;
1196 }
1197 csym->flags |= SYMBOL_DEF_USER;
1198 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001199 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001200}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001201
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001202bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001203{
1204 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001205 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001206 * pty: probability of tristate = y
1207 * ptm: probability of tristate = m
1208 */
1209
1210 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1211 * below, otherwise gcc whines about
1212 * -Wmaybe-uninitialized */
1213 if (mode == def_random) {
1214 int n, p[3];
1215 char *env = getenv("KCONFIG_PROBABILITY");
1216 n = 0;
1217 while( env && *env ) {
1218 char *endp;
1219 int tmp = strtol( env, &endp, 10 );
1220 if( tmp >= 0 && tmp <= 100 ) {
1221 p[n++] = tmp;
1222 } else {
1223 errno = ERANGE;
1224 perror( "KCONFIG_PROBABILITY" );
1225 exit( 1 );
1226 }
1227 env = (*endp == ':') ? endp+1 : endp;
1228 if( n >=3 ) {
1229 break;
1230 }
1231 }
1232 switch( n ) {
1233 case 1:
1234 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1235 break;
1236 case 2:
1237 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1238 break;
1239 case 3:
1240 pby = p[0]; pty = p[1]; ptm = p[2];
1241 break;
1242 }
1243
1244 if( pty+ptm > 100 ) {
1245 errno = ERANGE;
1246 perror( "KCONFIG_PROBABILITY" );
1247 exit( 1 );
1248 }
1249 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001250 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001251
1252 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001253 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001254 continue;
1255 switch (sym_get_type(sym)) {
1256 case S_BOOLEAN:
1257 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001258 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001259 switch (mode) {
1260 case def_yes:
1261 sym->def[S_DEF_USER].tri = yes;
1262 break;
1263 case def_mod:
1264 sym->def[S_DEF_USER].tri = mod;
1265 break;
1266 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001267 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1268 sym->def[S_DEF_USER].tri = yes;
1269 else
1270 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001271 break;
1272 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001273 sym->def[S_DEF_USER].tri = no;
1274 cnt = rand() % 100;
1275 if (sym->type == S_TRISTATE) {
1276 if (cnt < pty)
1277 sym->def[S_DEF_USER].tri = yes;
1278 else if (cnt < (pty+ptm))
1279 sym->def[S_DEF_USER].tri = mod;
1280 } else if (cnt < pby)
1281 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001282 break;
1283 default:
1284 continue;
1285 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001286 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001287 sym->flags |= SYMBOL_DEF_USER;
1288 break;
1289 default:
1290 break;
1291 }
1292
1293 }
1294
Al Viroce97e132008-10-26 05:12:34 +00001295 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001296
Sam Ravnborg184832c2009-03-15 11:05:12 +01001297 /*
1298 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001299 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001300 * choice symbols in one block.
1301 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001302 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001303 * selected in a choice block and we set it to yes,
1304 * and the rest to no.
1305 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001306 if (mode != def_random) {
1307 for_all_symbols(i, csym) {
1308 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1309 sym_is_choice_value(csym))
1310 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1311 }
1312 }
1313
Roman Zippeldc7862e2008-05-06 04:55:55 +02001314 for_all_symbols(i, csym) {
1315 if (sym_has_value(csym) || !sym_is_choice(csym))
1316 continue;
1317
1318 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001319 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001320 has_changed = randomize_choice_values(csym);
1321 else {
1322 set_all_choice_values(csym);
1323 has_changed = true;
1324 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001325 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001326
1327 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001328}