blob: 9ef135735cfa78203a45c727ae9aaae24bce9f91 [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_STRING:
231 if (*p++ != '"')
232 break;
233 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
234 if (*p2 == '"') {
235 *p2 = 0;
236 break;
237 }
238 memmove(p2, p2 + 1, strlen(p2));
239 }
240 if (!p2) {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200241 if (def != S_DEF_AUTO)
242 conf_warning("invalid string found");
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100243 return 1;
244 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400245 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100246 case S_INT:
247 case S_HEX:
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100248 if (sym_string_valid(sym, p)) {
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900249 sym->def[def].val = xstrdup(p);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100250 sym->flags |= def_flags;
251 } else {
Yann E. MORIN04b19b772013-08-06 18:45:07 +0200252 if (def != S_DEF_AUTO)
253 conf_warning("symbol value '%s' invalid for %s",
254 p, sym->name);
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100255 return 1;
256 }
257 break;
258 default:
259 ;
260 }
261 return 0;
262}
263
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700264#define LINE_GROWTH 16
265static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
266{
267 char *nline;
268 size_t new_size = slen + 1;
269 if (new_size > *n) {
270 new_size += LINE_GROWTH - 1;
271 new_size *= 2;
Masahiro Yamadad717f242018-02-09 01:19:07 +0900272 nline = xrealloc(*lineptr, new_size);
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700273 if (!nline)
274 return -1;
275
276 *lineptr = nline;
277 *n = new_size;
278 }
279
280 (*lineptr)[slen] = c;
281
282 return 0;
283}
284
285static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
286{
287 char *line = *lineptr;
288 size_t slen = 0;
289
290 for (;;) {
291 int c = getc(stream);
292
293 switch (c) {
294 case '\n':
295 if (add_byte(c, &line, slen, n) < 0)
296 goto e_out;
297 slen++;
298 /* fall through */
299 case EOF:
300 if (add_byte('\0', &line, slen, n) < 0)
301 goto e_out;
302 *lineptr = line;
303 if (slen == 0)
304 return -1;
305 return slen;
306 default:
307 if (add_byte(c, &line, slen, n) < 0)
308 goto e_out;
309 slen++;
310 }
311 }
312
313e_out:
314 line[slen-1] = '\0';
315 *lineptr = line;
316 return -1;
317}
318
Roman Zippel669bfad92006-06-08 22:12:42 -0700319int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 FILE *in = NULL;
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700322 char *line = NULL;
323 size_t line_asize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700326 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 if (name) {
329 in = zconf_fopen(name);
330 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700331 struct property *prop;
332
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700333 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700334 in = zconf_fopen(name);
335 if (in)
336 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800337 sym_add_change_count(1);
Al Viro6b87b702016-01-14 18:13:49 +0000338 if (!sym_defconfig_list)
Roman Zippelface4372006-06-08 22:12:45 -0700339 return 1;
340
341 for_all_defaults(sym_defconfig_list, prop) {
342 if (expr_calc_value(prop->visible.expr) == no ||
343 prop->expr->type != E_SYMBOL)
344 continue;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900345 sym_calc_value(prop->expr->left.sym);
346 name = sym_get_string_value(prop->expr->left.sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 in = zconf_fopen(name);
348 if (in) {
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200349 conf_message("using defaults found in %s",
Michal Marek42368c32010-08-17 10:21:19 +0200350 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700351 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 }
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!in)
356 return 1;
357
Roman Zippelddc97ca2006-06-08 22:12:38 -0700358load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800359 conf_filename = name;
360 conf_lineno = 0;
361 conf_warnings = 0;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800362
Roman Zippel669bfad92006-06-08 22:12:42 -0700363 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700365 sym->flags |= SYMBOL_CHANGED;
366 sym->flags &= ~(def_flags|SYMBOL_VALID);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200367 if (sym_is_choice(sym))
368 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 switch (sym->type) {
370 case S_INT:
371 case S_HEX:
372 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700373 if (sym->def[def].val)
374 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400375 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700377 sym->def[def].val = NULL;
378 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 }
381
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700382 while (compat_getline(&line, &line_asize, in) != -1) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800383 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400385 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400386 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400388 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!p)
390 continue;
391 *p++ = 0;
392 if (strncmp(p, "is not set", 10))
393 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700394 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400395 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200396 if (!sym) {
397 sym_add_change_count(1);
Masahiro Yamada75889e92018-11-30 18:15:48 +0900398 continue;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200399 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700400 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400401 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700402 if (sym->type == S_UNKNOWN)
403 sym->type = S_BOOLEAN;
404 }
405 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100406 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 switch (sym->type) {
409 case S_BOOLEAN:
410 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700411 sym->def[def].tri = no;
412 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
414 default:
415 ;
416 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400417 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
418 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!p)
420 continue;
421 *p++ = 0;
422 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600423 if (p2) {
424 *p2-- = 0;
425 if (*p2 == '\r')
426 *p2 = 0;
427 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900428
429 sym = sym_find(line + strlen(CONFIG_));
430 if (!sym) {
431 if (def == S_DEF_AUTO)
432 /*
433 * Reading from include/config/auto.conf
434 * If CONFIG_FOO previously existed in
435 * auto.conf but it is missing now,
436 * include/config/foo.h must be touched.
437 */
438 conf_touch_dep(line + strlen(CONFIG_));
439 else
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200440 sym_add_change_count(1);
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900441 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700442 }
Masahiro Yamada2aabbed2018-11-30 18:15:51 +0900443
Roman Zippel669bfad92006-06-08 22:12:42 -0700444 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100445 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100447 if (conf_set_sym_val(sym, def, def_flags, p))
448 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400449 } else {
450 if (line[0] != '\r' && line[0] != '\n')
Paul Bollea4663912016-03-16 21:27:27 +0100451 conf_warning("unexpected data: %.*s",
452 (int)strcspn(line, "\r\n"), line);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 continue;
455 }
Masahiro Yamada75889e92018-11-30 18:15:48 +0900456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (sym && sym_is_choice_value(sym)) {
458 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700459 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case no:
461 break;
462 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700463 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800464 conf_warning("%s creates inconsistent choice state", sym->name);
Yann E. MORIN490f1612013-06-25 23:37:44 +0200465 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100469 if (cs->def[def].tri != no)
470 conf_warning("override: %s changes choice state", sym->name);
471 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100474 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 }
Cody Schafer1a7a8c62012-07-13 11:27:12 -0700477 free(line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 fclose(in);
Roman Zippel90389162005-11-08 21:34:49 -0800479 return 0;
480}
481
482int conf_read(const char *name)
483{
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500484 struct symbol *sym;
Masahiro Yamada84dd95d2018-01-11 22:39:41 +0900485 int conf_unsaved = 0;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500486 int i;
Roman Zippel90389162005-11-08 21:34:49 -0800487
Karsten Wiesebfc10002006-12-13 00:34:07 -0800488 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700489
Al Viro6b87b702016-01-14 18:13:49 +0000490 if (conf_read_simple(name, S_DEF_USER)) {
491 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800492 return 1;
Al Viro6b87b702016-01-14 18:13:49 +0000493 }
494
495 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 for_all_symbols(i, sym) {
498 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200499 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500500 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800501 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
502 /* check that calculated value agrees with saved value */
503 switch (sym->type) {
504 case S_BOOLEAN:
505 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700506 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800507 break;
508 if (!sym_is_choice(sym))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500509 continue;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400510 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800511 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700512 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500513 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800514 break;
515 }
516 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
517 /* no previous value and not saved */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500518 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800519 conf_unsaved++;
520 /* maybe print value in verbose mode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Roman Zippeld8982ba2007-07-09 11:43:58 -0700523 for_all_symbols(i, sym) {
524 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
525 /* Reset values of generates values, so they'll appear
526 * as new, if they should become visible, but that
527 * doesn't quite work if the Kconfig and the saved
528 * configuration disagree.
529 */
530 if (sym->visible == no && !conf_unsaved)
531 sym->flags &= ~SYMBOL_DEF_USER;
532 switch (sym->type) {
533 case S_STRING:
534 case S_INT:
535 case S_HEX:
536 /* Reset a string value if it's out of range */
537 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
538 break;
539 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
540 conf_unsaved++;
541 break;
542 default:
543 break;
544 }
545 }
546 }
547
Karsten Wiesebfc10002006-12-13 00:34:07 -0800548 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return 0;
551}
552
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400553/*
554 * Kconfig configuration printer
555 *
556 * This printer is used when generating the resulting configuration after
557 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
558 * passing a non-NULL argument to the printer.
559 *
560 */
561static void
562kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200563{
Sam Ravnborg49192f22010-07-31 23:35:33 +0200564
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500565 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200566 case S_BOOLEAN:
567 case S_TRISTATE:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400568 if (*value == 'n') {
569 bool skip_unset = (arg != NULL);
570
571 if (!skip_unset)
572 fprintf(fp, "# %s%s is not set\n",
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400573 CONFIG_, sym->name);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400574 return;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200575 }
576 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400577 default:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200578 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400579 }
580
581 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
582}
583
584static void
585kconfig_print_comment(FILE *fp, const char *value, void *arg)
586{
587 const char *p = value;
588 size_t l;
589
590 for (;;) {
591 l = strcspn(p, "\n");
592 fprintf(fp, "#");
593 if (l) {
594 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400595 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400596 p += l;
597 }
598 fprintf(fp, "\n");
599 if (*p++ == '\0')
600 break;
601 }
602}
603
604static struct conf_printer kconfig_printer_cb =
605{
606 .print_symbol = kconfig_print_symbol,
607 .print_comment = kconfig_print_comment,
608};
609
610/*
611 * Header printer
612 *
613 * This printer is used when generating the `include/generated/autoconf.h' file.
614 */
615static void
616header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
617{
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400618
619 switch (sym->type) {
620 case S_BOOLEAN:
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400621 case S_TRISTATE: {
622 const char *suffix = "";
623
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400624 switch (*value) {
625 case 'n':
Michal Marek2a11c8e2011-07-20 17:38:57 +0200626 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400627 case 'm':
628 suffix = "_MODULE";
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400629 /* fall through */
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400630 default:
Michal Marek2a11c8e2011-07-20 17:38:57 +0200631 fprintf(fp, "#define %s%s%s 1\n",
632 CONFIG_, sym->name, suffix);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400633 }
Arnaud Lacombeeb4cf5a2011-07-14 15:31:07 -0400634 break;
635 }
636 case S_HEX: {
637 const char *prefix = "";
638
639 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
640 prefix = "0x";
641 fprintf(fp, "#define %s%s %s%s\n",
642 CONFIG_, sym->name, prefix, value);
643 break;
644 }
645 case S_STRING:
646 case S_INT:
647 fprintf(fp, "#define %s%s %s\n",
648 CONFIG_, sym->name, value);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200649 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400650 default:
651 break;
652 }
653
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400654}
655
656static void
657header_print_comment(FILE *fp, const char *value, void *arg)
658{
659 const char *p = value;
660 size_t l;
661
662 fprintf(fp, "/*\n");
663 for (;;) {
664 l = strcspn(p, "\n");
665 fprintf(fp, " *");
666 if (l) {
667 fprintf(fp, " ");
Peter Foley70cc01e2011-10-22 10:48:49 -0400668 xfwrite(p, l, 1, fp);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400669 p += l;
670 }
671 fprintf(fp, "\n");
672 if (*p++ == '\0')
673 break;
674 }
675 fprintf(fp, " */\n");
676}
677
678static struct conf_printer header_printer_cb =
679{
680 .print_symbol = header_print_symbol,
681 .print_comment = header_print_comment,
682};
683
684/*
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400685 * Tristate printer
686 *
687 * This printer is used when generating the `include/config/tristate.conf' file.
688 */
689static void
690tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
691{
692
693 if (sym->type == S_TRISTATE && *value != 'n')
694 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
695}
696
697static struct conf_printer tristate_printer_cb =
698{
699 .print_symbol = tristate_print_symbol,
700 .print_comment = kconfig_print_comment,
701};
702
703static void conf_write_symbol(FILE *fp, struct symbol *sym,
704 struct conf_printer *printer, void *printer_arg)
705{
706 const char *str;
707
708 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200709 case S_UNKNOWN:
710 break;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400711 case S_STRING:
712 str = sym_get_string_value(sym);
713 str = sym_escape_string_value(str);
714 printer->print_symbol(fp, sym, str, printer_arg);
715 free((void *)str);
716 break;
717 default:
718 str = sym_get_string_value(sym);
719 printer->print_symbol(fp, sym, str, printer_arg);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200720 }
721}
722
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400723static void
724conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
725{
726 char buf[256];
727
728 snprintf(buf, sizeof(buf),
729 "\n"
730 "Automatically generated file; DO NOT EDIT.\n"
731 "%s\n",
732 rootmenu.prompt->text);
733
734 printer->print_comment(fp, buf, printer_arg);
735}
736
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200737/*
738 * Write out a minimal config.
739 * All values that has default values are skipped as this is redundant.
740 */
741int conf_write_defconfig(const char *filename)
742{
743 struct symbol *sym;
744 struct menu *menu;
745 FILE *out;
746
747 out = fopen(filename, "w");
748 if (!out)
749 return 1;
750
751 sym_clear_all_valid();
752
753 /* Traverse all menus to find all relevant symbols */
754 menu = rootmenu.list;
755
756 while (menu != NULL)
757 {
758 sym = menu->sym;
759 if (sym == NULL) {
760 if (!menu_is_visible(menu))
761 goto next_menu;
762 } else if (!sym_is_choice(sym)) {
763 sym_calc_value(sym);
764 if (!(sym->flags & SYMBOL_WRITE))
765 goto next_menu;
766 sym->flags &= ~SYMBOL_WRITE;
767 /* If we cannot change the symbol - skip */
768 if (!sym_is_changable(sym))
769 goto next_menu;
770 /* If symbol equals to default value - skip */
771 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
772 goto next_menu;
773
774 /*
775 * If symbol is a choice value and equals to the
776 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200777 * But only if value is bool and equal to "y" and
778 * choice is not "optional".
779 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200780 */
781 if (sym_is_choice_value(sym)) {
782 struct symbol *cs;
783 struct symbol *ds;
784
785 cs = prop_get_symbol(sym_get_choice_prop(sym));
786 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200787 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200788 if ((sym->type == S_BOOLEAN) &&
789 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200790 goto next_menu;
791 }
792 }
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400793 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200794 }
795next_menu:
796 if (menu->list != NULL) {
797 menu = menu->list;
798 }
799 else if (menu->next != NULL) {
800 menu = menu->next;
801 } else {
802 while ((menu = menu->parent)) {
803 if (menu->next != NULL) {
804 menu = menu->next;
805 break;
806 }
807 }
808 }
809 }
810 fclose(out);
811 return 0;
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814int conf_write(const char *name)
815{
Roman Zippelc955cca2006-06-08 22:12:39 -0700816 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct symbol *sym;
818 struct menu *menu;
819 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 const char *str;
Nathan Chancellor2ae89c72018-06-02 09:02:09 -0700821 char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 char *env;
823
824 dirname[0] = 0;
825 if (name && name[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 char *slash;
827
Masahiro Yamada06081822018-07-20 16:46:27 +0900828 if (is_dir(name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 strcpy(dirname, name);
830 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700831 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 } else if ((slash = strrchr(name, '/'))) {
833 int size = slash - name + 1;
834 memcpy(dirname, name, size);
835 dirname[size] = 0;
836 if (slash[1])
837 basename = slash + 1;
838 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700839 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 } else
841 basename = name;
842 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700843 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700845 sprintf(newname, "%s%s", dirname, basename);
846 env = getenv("KCONFIG_OVERWRITECONFIG");
847 if (!env || !*env) {
848 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
849 out = fopen(tmpname, "w");
850 } else {
851 *tmpname = 0;
852 out = fopen(newname, "w");
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!out)
855 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700856
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400857 conf_write_heading(out, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Karsten Wieseb3214292006-12-13 00:34:06 -0800859 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sym_clear_all_valid();
861
862 menu = rootmenu.list;
863 while (menu) {
864 sym = menu->sym;
865 if (!sym) {
866 if (!menu_is_visible(menu))
867 goto next;
868 str = menu_get_prompt(menu);
869 fprintf(out, "\n"
870 "#\n"
871 "# %s\n"
872 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else if (!(sym->flags & SYMBOL_CHOICE)) {
874 sym_calc_value(sym);
875 if (!(sym->flags & SYMBOL_WRITE))
876 goto next;
877 sym->flags &= ~SYMBOL_WRITE;
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400878
879 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
Sam Ravnborg49192f22010-07-31 23:35:33 +0200882next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (menu->list) {
884 menu = menu->list;
885 continue;
886 }
887 if (menu->next)
888 menu = menu->next;
889 else while ((menu = menu->parent)) {
890 if (menu->next) {
891 menu = menu->next;
892 break;
893 }
894 }
895 }
896 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700897
898 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200899 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700900 strcat(dirname, ".old");
901 rename(newname, dirname);
902 if (rename(tmpname, newname))
903 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Sam Ravnborg694c49a2018-05-22 21:36:12 +0200906 conf_message("configuration written to %s", newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700907
Karsten Wiesebfc10002006-12-13 00:34:07 -0800908 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 return 0;
911}
Roman Zippelc955cca2006-06-08 22:12:39 -0700912
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900913/* write a dependency file as used by kbuild to track dependencies */
914static int conf_write_dep(const char *name)
915{
916 struct file *file;
917 FILE *out;
918
919 if (!name)
920 name = ".kconfig.d";
921 out = fopen("..config.tmp", "w");
922 if (!out)
923 return 1;
924 fprintf(out, "deps_config := \\\n");
925 for (file = file_list; file; file = file->next) {
926 if (file->next)
927 fprintf(out, "\t%s \\\n", file->name);
928 else
929 fprintf(out, "\t%s\n", file->name);
930 }
931 fprintf(out, "\n%s: \\\n"
932 "\t$(deps_config)\n\n", conf_get_autoconfig_name());
933
934 env_write_dep(out, conf_get_autoconfig_name());
935
936 fprintf(out, "\n$(deps_config): ;\n");
937 fclose(out);
Masahiro Yamada79123b12018-07-20 16:46:29 +0900938
939 if (make_parent_dir(name))
940 return 1;
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +0900941 rename("..config.tmp", name);
942 return 0;
943}
944
Masahiro Yamada0849d212018-11-30 18:15:49 +0900945static int conf_touch_deps(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700946{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200947 const char *name;
Roman Zippel2e3646e2006-06-08 22:12:42 -0700948 struct symbol *sym;
Masahiro Yamada1508fec2018-11-30 18:15:50 +0900949 int res, i;
950
951 strcpy(depfile_path, "include/config/");
952 depfile_prefix_len = strlen(depfile_path);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700953
Markus Heidelberg12122f62009-05-18 01:36:54 +0200954 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700955 conf_read_simple(name, S_DEF_AUTO);
Al Viro6b87b702016-01-14 18:13:49 +0000956 sym_calc_value(modules_sym);
Roman Zippel2e3646e2006-06-08 22:12:42 -0700957
Roman Zippel2e3646e2006-06-08 22:12:42 -0700958 for_all_symbols(i, sym) {
959 sym_calc_value(sym);
Dirk Gouders693359f2018-07-03 14:43:31 +0200960 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700961 continue;
962 if (sym->flags & SYMBOL_WRITE) {
963 if (sym->flags & SYMBOL_DEF_AUTO) {
964 /*
965 * symbol has old and new value,
966 * so compare them...
967 */
968 switch (sym->type) {
969 case S_BOOLEAN:
970 case S_TRISTATE:
971 if (sym_get_tristate_value(sym) ==
972 sym->def[S_DEF_AUTO].tri)
973 continue;
974 break;
975 case S_STRING:
976 case S_HEX:
977 case S_INT:
978 if (!strcmp(sym_get_string_value(sym),
979 sym->def[S_DEF_AUTO].val))
980 continue;
981 break;
982 default:
983 break;
984 }
985 } else {
986 /*
987 * If there is no old value, only 'no' (unset)
988 * is allowed as new value.
989 */
990 switch (sym->type) {
991 case S_BOOLEAN:
992 case S_TRISTATE:
993 if (sym_get_tristate_value(sym) == no)
994 continue;
995 break;
996 default:
997 break;
998 }
999 }
1000 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1001 /* There is neither an old nor a new value. */
1002 continue;
1003 /* else
1004 * There is an old value, but no new value ('no' (unset)
1005 * isn't saved in auto.conf, so the old value is always
1006 * different from 'no').
1007 */
1008
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001009 res = conf_touch_dep(sym->name);
1010 if (res)
1011 return res;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001012 }
Roman Zippel2e3646e2006-06-08 22:12:42 -07001013
Masahiro Yamada1508fec2018-11-30 18:15:50 +09001014 return 0;
Roman Zippel2e3646e2006-06-08 22:12:42 -07001015}
1016
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001017int conf_write_autoconf(int overwrite)
Roman Zippelc955cca2006-06-08 22:12:39 -07001018{
1019 struct symbol *sym;
Markus Heidelberg12122f62009-05-18 01:36:54 +02001020 const char *name;
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001021 const char *autoconf_name = conf_get_autoconfig_name();
Michal Marekbc081dd2009-12-07 16:38:33 +01001022 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +02001023 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -07001024
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001025 if (!overwrite && is_present(autoconf_name))
1026 return 0;
1027
Roman Zippel2e3646e2006-06-08 22:12:42 -07001028 sym_clear_all_valid();
1029
Masahiro Yamadaa2ff4042018-07-20 16:46:26 +09001030 conf_write_dep("include/config/auto.conf.cmd");
Roman Zippelc955cca2006-06-08 22:12:39 -07001031
Masahiro Yamada0849d212018-11-30 18:15:49 +09001032 if (conf_touch_deps())
Roman Zippel2e3646e2006-06-08 22:12:42 -07001033 return 1;
1034
Roman Zippelc955cca2006-06-08 22:12:39 -07001035 out = fopen(".tmpconfig", "w");
1036 if (!out)
1037 return 1;
1038
Michal Marekbc081dd2009-12-07 16:38:33 +01001039 tristate = fopen(".tmpconfig_tristate", "w");
1040 if (!tristate) {
1041 fclose(out);
1042 return 1;
1043 }
1044
Roman Zippelc955cca2006-06-08 22:12:39 -07001045 out_h = fopen(".tmpconfig.h", "w");
1046 if (!out_h) {
1047 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001048 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001049 return 1;
1050 }
1051
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001052 conf_write_heading(out, &kconfig_printer_cb, NULL);
1053
1054 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1055
1056 conf_write_heading(out_h, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001057
Roman Zippelc955cca2006-06-08 22:12:39 -07001058 for_all_symbols(i, sym) {
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001059 sym_calc_value(sym);
Paul Gortmakera9596132012-04-12 19:46:33 -04001060 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
Arnaud Lacombe953742c2011-08-16 01:20:20 -04001061 continue;
1062
Paul Gortmakera9596132012-04-12 19:46:33 -04001063 /* write symbol to auto.conf, tristate and header files */
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001064 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
Sam Ravnborg49192f22010-07-31 23:35:33 +02001065
Arnaud Lacombee54e6922011-05-15 23:42:09 -04001066 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1067
1068 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Roman Zippelc955cca2006-06-08 22:12:39 -07001069 }
1070 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +01001071 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -07001072 fclose(out_h);
1073
1074 name = getenv("KCONFIG_AUTOHEADER");
1075 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +02001076 name = "include/generated/autoconf.h";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001077 if (make_parent_dir(name))
1078 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001079 if (rename(".tmpconfig.h", name))
1080 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001081
Michal Marekbc081dd2009-12-07 16:38:33 +01001082 name = getenv("KCONFIG_TRISTATE");
1083 if (!name)
1084 name = "include/config/tristate.conf";
Masahiro Yamada79123b12018-07-20 16:46:29 +09001085 if (make_parent_dir(name))
1086 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +01001087 if (rename(".tmpconfig_tristate", name))
1088 return 1;
Masahiro Yamada79123b12018-07-20 16:46:29 +09001089
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001090 if (make_parent_dir(autoconf_name))
Masahiro Yamada79123b12018-07-20 16:46:29 +09001091 return 1;
Roman Zippelc955cca2006-06-08 22:12:39 -07001092 /*
1093 * This must be the last step, kbuild has a dependency on auto.conf
1094 * and this marks the successful completion of the previous steps.
1095 */
Masahiro Yamada00c864f2018-07-20 16:46:31 +09001096 if (rename(".tmpconfig", autoconf_name))
Roman Zippelc955cca2006-06-08 22:12:39 -07001097 return 1;
1098
1099 return 0;
1100}
Karsten Wieseb3214292006-12-13 00:34:06 -08001101
Karsten Wiesebfc10002006-12-13 00:34:07 -08001102static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001103static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001104
1105void sym_set_change_count(int count)
1106{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001107 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -08001108 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -08001109 if (conf_changed_callback &&
1110 (bool)_sym_change_count != (bool)count)
1111 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -08001112}
1113
1114void sym_add_change_count(int count)
1115{
Karsten Wiese3b354c52006-12-13 00:34:08 -08001116 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -08001117}
1118
Karsten Wieseb3214292006-12-13 00:34:06 -08001119bool conf_get_changed(void)
1120{
1121 return sym_change_count;
1122}
Karsten Wiese3b354c52006-12-13 00:34:08 -08001123
1124void conf_set_changed_callback(void (*fn)(void))
1125{
1126 conf_changed_callback = fn;
1127}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001128
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001129static bool randomize_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001130{
1131 struct property *prop;
1132 struct symbol *sym;
1133 struct expr *e;
1134 int cnt, def;
1135
1136 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001137 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001138 * and if no then no-one.
1139 * In both cases stop.
1140 */
1141 if (csym->curr.tri != yes)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001142 return false;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001143
1144 prop = sym_get_choice_prop(csym);
1145
1146 /* count entries in choice block */
1147 cnt = 0;
1148 expr_list_for_each_sym(prop->expr, e, sym)
1149 cnt++;
1150
1151 /*
1152 * find a random value and set it to yes,
1153 * set the rest to no so we have only one set
1154 */
1155 def = (rand() % cnt);
1156
1157 cnt = 0;
1158 expr_list_for_each_sym(prop->expr, e, sym) {
1159 if (def == cnt++) {
1160 sym->def[S_DEF_USER].tri = yes;
1161 csym->def[S_DEF_USER].val = sym;
1162 }
1163 else {
1164 sym->def[S_DEF_USER].tri = no;
1165 }
Yann E. MORINe6abf122013-04-28 17:33:15 +02001166 sym->flags |= SYMBOL_DEF_USER;
1167 /* clear VALID to get value calculated */
1168 sym->flags &= ~SYMBOL_VALID;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001169 }
1170 csym->flags |= SYMBOL_DEF_USER;
1171 /* clear VALID to get value calculated */
1172 csym->flags &= ~(SYMBOL_VALID);
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001173
1174 return true;
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001175}
1176
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001177void set_all_choice_values(struct symbol *csym)
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001178{
1179 struct property *prop;
1180 struct symbol *sym;
1181 struct expr *e;
1182
1183 prop = sym_get_choice_prop(csym);
1184
1185 /*
1186 * Set all non-assinged choice values to no
1187 */
1188 expr_list_for_each_sym(prop->expr, e, sym) {
1189 if (!sym_has_value(sym))
1190 sym->def[S_DEF_USER].tri = no;
1191 }
1192 csym->flags |= SYMBOL_DEF_USER;
1193 /* clear VALID to get value calculated */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001194 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001195}
Roman Zippeldc7862e2008-05-06 04:55:55 +02001196
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001197bool conf_set_all_new_symbols(enum conf_def_mode mode)
Roman Zippeldc7862e2008-05-06 04:55:55 +02001198{
1199 struct symbol *sym, *csym;
Masahiro Yamadab92d8042017-12-16 00:38:02 +09001200 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
Yann E. MORINe43956e2013-04-13 17:18:36 +02001201 * pty: probability of tristate = y
1202 * ptm: probability of tristate = m
1203 */
1204
1205 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1206 * below, otherwise gcc whines about
1207 * -Wmaybe-uninitialized */
1208 if (mode == def_random) {
1209 int n, p[3];
1210 char *env = getenv("KCONFIG_PROBABILITY");
1211 n = 0;
1212 while( env && *env ) {
1213 char *endp;
1214 int tmp = strtol( env, &endp, 10 );
1215 if( tmp >= 0 && tmp <= 100 ) {
1216 p[n++] = tmp;
1217 } else {
1218 errno = ERANGE;
1219 perror( "KCONFIG_PROBABILITY" );
1220 exit( 1 );
1221 }
1222 env = (*endp == ':') ? endp+1 : endp;
1223 if( n >=3 ) {
1224 break;
1225 }
1226 }
1227 switch( n ) {
1228 case 1:
1229 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1230 break;
1231 case 2:
1232 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1233 break;
1234 case 3:
1235 pby = p[0]; pty = p[1]; ptm = p[2];
1236 break;
1237 }
1238
1239 if( pty+ptm > 100 ) {
1240 errno = ERANGE;
1241 perror( "KCONFIG_PROBABILITY" );
1242 exit( 1 );
1243 }
1244 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001245 bool has_changed = false;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001246
1247 for_all_symbols(i, sym) {
Yann E. MORINcfa98f22013-04-24 22:00:04 +02001248 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001249 continue;
1250 switch (sym_get_type(sym)) {
1251 case S_BOOLEAN:
1252 case S_TRISTATE:
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001253 has_changed = true;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001254 switch (mode) {
1255 case def_yes:
1256 sym->def[S_DEF_USER].tri = yes;
1257 break;
1258 case def_mod:
1259 sym->def[S_DEF_USER].tri = mod;
1260 break;
1261 case def_no:
Josh Triplett5d2acfc2014-04-07 15:39:09 -07001262 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1263 sym->def[S_DEF_USER].tri = yes;
1264 else
1265 sym->def[S_DEF_USER].tri = no;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001266 break;
1267 case def_random:
Yann E. MORINe43956e2013-04-13 17:18:36 +02001268 sym->def[S_DEF_USER].tri = no;
1269 cnt = rand() % 100;
1270 if (sym->type == S_TRISTATE) {
1271 if (cnt < pty)
1272 sym->def[S_DEF_USER].tri = yes;
1273 else if (cnt < (pty+ptm))
1274 sym->def[S_DEF_USER].tri = mod;
1275 } else if (cnt < pby)
1276 sym->def[S_DEF_USER].tri = yes;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001277 break;
1278 default:
1279 continue;
1280 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001281 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001282 sym->flags |= SYMBOL_DEF_USER;
1283 break;
1284 default:
1285 break;
1286 }
1287
1288 }
1289
Al Viroce97e132008-10-26 05:12:34 +00001290 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001291
Sam Ravnborg184832c2009-03-15 11:05:12 +01001292 /*
1293 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001294 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001295 * choice symbols in one block.
1296 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001297 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001298 * selected in a choice block and we set it to yes,
1299 * and the rest to no.
1300 */
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -07001301 if (mode != def_random) {
1302 for_all_symbols(i, csym) {
1303 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1304 sym_is_choice_value(csym))
1305 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1306 }
1307 }
1308
Roman Zippeldc7862e2008-05-06 04:55:55 +02001309 for_all_symbols(i, csym) {
1310 if (sym_has_value(csym) || !sym_is_choice(csym))
1311 continue;
1312
1313 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001314 if (mode == def_random)
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001315 has_changed = randomize_choice_values(csym);
1316 else {
1317 set_all_choice_values(csym);
1318 has_changed = true;
1319 }
Roman Zippeldc7862e2008-05-06 04:55:55 +02001320 }
Yann E. MORIN3b9a19e2013-04-28 22:36:38 +02001321
1322 return has_changed;
Roman Zippeldc7862e2008-05-06 04:55:55 +02001323}