blob: 583f6405f01d8c563c963d7cdb16e8c0193b44d1 [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>
Roman Zippel2e3646e2006-06-08 22:12:42 -07008#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13#include <unistd.h>
14
15#define LKC_DIRECT_LINK
16#include "lkc.h"
17
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080018static void conf_warning(const char *fmt, ...)
19 __attribute__ ((format (printf, 1, 2)));
20
21static const char *conf_filename;
22static int conf_lineno, conf_warnings, conf_unsaved;
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024const char conf_defname[] = "arch/$ARCH/defconfig";
25
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080026static void conf_warning(const char *fmt, ...)
27{
28 va_list ap;
29 va_start(ap, fmt);
30 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
31 vfprintf(stderr, fmt, ap);
32 fprintf(stderr, "\n");
33 va_end(ap);
34 conf_warnings++;
35}
36
Roman Zippel14cdd3c2006-06-08 22:12:51 -070037const char *conf_get_configname(void)
38{
39 char *name = getenv("KCONFIG_CONFIG");
40
41 return name ? name : ".config";
42}
43
Markus Heidelberg12122f62009-05-18 01:36:54 +020044const char *conf_get_autoconfig_name(void)
45{
46 char *name = getenv("KCONFIG_AUTOCONFIG");
47
48 return name ? name : "include/config/auto.conf";
49}
50
J.A. Magallon48b9d032005-06-25 14:59:22 -070051static char *conf_expand_value(const char *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct symbol *sym;
J.A. Magallon48b9d032005-06-25 14:59:22 -070054 const char *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 static char res_value[SYMBOL_MAXLENGTH];
56 char *dst, name[SYMBOL_MAXLENGTH];
57
58 res_value[0] = 0;
59 dst = name;
60 while ((src = strchr(in, '$'))) {
61 strncat(res_value, in, src - in);
62 src++;
63 dst = name;
64 while (isalnum(*src) || *src == '_')
65 *dst++ = *src++;
66 *dst = 0;
67 sym = sym_lookup(name, 0);
68 sym_calc_value(sym);
69 strcat(res_value, sym_get_string_value(sym));
70 in = src;
71 }
72 strcat(res_value, in);
73
74 return res_value;
75}
76
77char *conf_get_default_confname(void)
78{
79 struct stat buf;
80 static char fullname[PATH_MAX+1];
81 char *env, *name;
82
83 name = conf_expand_value(conf_defname);
84 env = getenv(SRCTREE);
85 if (env) {
86 sprintf(fullname, "%s/%s", env, name);
87 if (!stat(fullname, &buf))
88 return fullname;
89 }
90 return name;
91}
92
Sam Ravnborg9c900a92007-11-10 20:01:56 +010093static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
94{
95 char *p2;
96
97 switch (sym->type) {
98 case S_TRISTATE:
99 if (p[0] == 'm') {
100 sym->def[def].tri = mod;
101 sym->flags |= def_flags;
102 break;
103 }
104 case S_BOOLEAN:
105 if (p[0] == 'y') {
106 sym->def[def].tri = yes;
107 sym->flags |= def_flags;
108 break;
109 }
110 if (p[0] == 'n') {
111 sym->def[def].tri = no;
112 sym->flags |= def_flags;
113 break;
114 }
115 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
116 break;
117 case S_OTHER:
118 if (*p != '"') {
119 for (p2 = p; *p2 && !isspace(*p2); p2++)
120 ;
121 sym->type = S_STRING;
122 goto done;
123 }
124 case S_STRING:
125 if (*p++ != '"')
126 break;
127 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
128 if (*p2 == '"') {
129 *p2 = 0;
130 break;
131 }
132 memmove(p2, p2 + 1, strlen(p2));
133 }
134 if (!p2) {
135 conf_warning("invalid string found");
136 return 1;
137 }
138 case S_INT:
139 case S_HEX:
140 done:
141 if (sym_string_valid(sym, p)) {
142 sym->def[def].val = strdup(p);
143 sym->flags |= def_flags;
144 } else {
145 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
146 return 1;
147 }
148 break;
149 default:
150 ;
151 }
152 return 0;
153}
154
Roman Zippel669bfad92006-06-08 22:12:42 -0700155int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 FILE *in = NULL;
158 char line[1024];
159 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700161 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (name) {
164 in = zconf_fopen(name);
165 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700166 struct property *prop;
167
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700168 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700169 in = zconf_fopen(name);
170 if (in)
171 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800172 sym_add_change_count(1);
Ulf Magnussonac1ffde2010-07-27 21:57:43 +0200173 if (!sym_defconfig_list) {
174 if (modules_sym)
175 sym_calc_value(modules_sym);
Roman Zippelface4372006-06-08 22:12:45 -0700176 return 1;
Ulf Magnussonac1ffde2010-07-27 21:57:43 +0200177 }
Roman Zippelface4372006-06-08 22:12:45 -0700178
179 for_all_defaults(sym_defconfig_list, prop) {
180 if (expr_calc_value(prop->visible.expr) == no ||
181 prop->expr->type != E_SYMBOL)
182 continue;
183 name = conf_expand_value(prop->expr->left.sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 in = zconf_fopen(name);
185 if (in) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700186 printf(_("#\n"
Roman Zippelddc97ca2006-06-08 22:12:38 -0700187 "# using defaults found in %s\n"
188 "#\n"), name);
189 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 }
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!in)
194 return 1;
195
Roman Zippelddc97ca2006-06-08 22:12:38 -0700196load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800197 conf_filename = name;
198 conf_lineno = 0;
199 conf_warnings = 0;
200 conf_unsaved = 0;
201
Roman Zippel669bfad92006-06-08 22:12:42 -0700202 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700204 sym->flags |= SYMBOL_CHANGED;
205 sym->flags &= ~(def_flags|SYMBOL_VALID);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800206 if (sym_is_choice(sym))
Roman Zippel669bfad92006-06-08 22:12:42 -0700207 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 switch (sym->type) {
209 case S_INT:
210 case S_HEX:
211 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700212 if (sym->def[def].val)
213 free(sym->def[def].val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700215 sym->def[def].val = NULL;
216 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 }
219
220 while (fgets(line, sizeof(line), in)) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800221 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sym = NULL;
223 switch (line[0]) {
224 case '#':
225 if (memcmp(line + 2, "CONFIG_", 7))
226 continue;
227 p = strchr(line + 9, ' ');
228 if (!p)
229 continue;
230 *p++ = 0;
231 if (strncmp(p, "is not set", 10))
232 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700233 if (def == S_DEF_USER) {
234 sym = sym_find(line + 9);
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200235 if (!sym) {
236 sym_add_change_count(1);
Roman Zippel669bfad92006-06-08 22:12:42 -0700237 break;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200238 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700239 } else {
240 sym = sym_lookup(line + 9, 0);
241 if (sym->type == S_UNKNOWN)
242 sym->type = S_BOOLEAN;
243 }
244 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100245 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 switch (sym->type) {
248 case S_BOOLEAN:
249 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700250 sym->def[def].tri = no;
251 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
253 default:
254 ;
255 }
256 break;
257 case 'C':
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800258 if (memcmp(line, "CONFIG_", 7)) {
259 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 p = strchr(line + 7, '=');
263 if (!p)
264 continue;
265 *p++ = 0;
266 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600267 if (p2) {
268 *p2-- = 0;
269 if (*p2 == '\r')
270 *p2 = 0;
271 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700272 if (def == S_DEF_USER) {
273 sym = sym_find(line + 7);
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200274 if (!sym) {
275 sym_add_change_count(1);
Roman Zippel669bfad92006-06-08 22:12:42 -0700276 break;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200277 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700278 } else {
279 sym = sym_lookup(line + 7, 0);
280 if (sym->type == S_UNKNOWN)
281 sym->type = S_OTHER;
282 }
283 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100284 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100286 if (conf_set_sym_val(sym, def, def_flags, p))
287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600289 case '\r':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case '\n':
291 break;
292 default:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800293 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 continue;
295 }
296 if (sym && sym_is_choice_value(sym)) {
297 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700298 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 case no:
300 break;
301 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700302 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800303 conf_warning("%s creates inconsistent choice state", sym->name);
Roman Zippel669bfad92006-06-08 22:12:42 -0700304 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100308 if (cs->def[def].tri != no)
309 conf_warning("override: %s changes choice state", sym->name);
310 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100313 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 }
316 fclose(in);
317
318 if (modules_sym)
319 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800320 return 0;
321}
322
323int conf_read(const char *name)
324{
Roman Zippel7a962922008-01-14 04:50:23 +0100325 struct symbol *sym, *choice_sym;
Roman Zippel90389162005-11-08 21:34:49 -0800326 struct property *prop;
327 struct expr *e;
Roman Zippel669bfad92006-06-08 22:12:42 -0700328 int i, flags;
Roman Zippel90389162005-11-08 21:34:49 -0800329
Karsten Wiesebfc10002006-12-13 00:34:07 -0800330 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700331
Roman Zippel669bfad92006-06-08 22:12:42 -0700332 if (conf_read_simple(name, S_DEF_USER))
Roman Zippel90389162005-11-08 21:34:49 -0800333 return 1;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 for_all_symbols(i, sym) {
336 sym_calc_value(sym);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800337 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
338 goto sym_ok;
339 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
340 /* check that calculated value agrees with saved value */
341 switch (sym->type) {
342 case S_BOOLEAN:
343 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700344 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800345 break;
346 if (!sym_is_choice(sym))
347 goto sym_ok;
348 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700349 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800350 goto sym_ok;
351 break;
352 }
353 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
354 /* no previous value and not saved */
355 goto sym_ok;
356 conf_unsaved++;
357 /* maybe print value in verbose mode... */
358 sym_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!sym_is_choice(sym))
360 continue;
Roman Zippeld8982ba2007-07-09 11:43:58 -0700361 /* The choice symbol only has a set value (and thus is not new)
362 * if all its visible childs have values.
363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 prop = sym_get_choice_prop(sym);
Roman Zippel669bfad92006-06-08 22:12:42 -0700365 flags = sym->flags;
Roman Zippel7a962922008-01-14 04:50:23 +0100366 expr_list_for_each_sym(prop->expr, e, choice_sym)
367 if (choice_sym->visible != no)
368 flags &= choice_sym->flags;
Roman Zippel002d27b2006-07-13 13:22:38 +0200369 sym->flags &= flags | ~SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Roman Zippeld8982ba2007-07-09 11:43:58 -0700372 for_all_symbols(i, sym) {
373 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
374 /* Reset values of generates values, so they'll appear
375 * as new, if they should become visible, but that
376 * doesn't quite work if the Kconfig and the saved
377 * configuration disagree.
378 */
379 if (sym->visible == no && !conf_unsaved)
380 sym->flags &= ~SYMBOL_DEF_USER;
381 switch (sym->type) {
382 case S_STRING:
383 case S_INT:
384 case S_HEX:
385 /* Reset a string value if it's out of range */
386 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
387 break;
388 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
389 conf_unsaved++;
390 break;
391 default:
392 break;
393 }
394 }
395 }
396
Karsten Wiesebfc10002006-12-13 00:34:07 -0800397 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
Sam Ravnborg49192f22010-07-31 23:35:33 +0200402/* Write a S_STRING */
403static void conf_write_string(bool headerfile, const char *name,
404 const char *str, FILE *out)
405{
406 int l;
407 if (headerfile)
408 fprintf(out, "#define CONFIG_%s \"", name);
409 else
410 fprintf(out, "CONFIG_%s=\"", name);
411
412 while (1) {
413 l = strcspn(str, "\"\\");
414 if (l) {
415 fwrite(str, l, 1, out);
416 str += l;
417 }
418 if (!*str)
419 break;
420 fprintf(out, "\\%c", *str++);
421 }
422 fputs("\"\n", out);
423}
424
425static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
426 FILE *out, bool write_no)
427{
428 const char *str;
429
430 switch (type) {
431 case S_BOOLEAN:
432 case S_TRISTATE:
433 switch (sym_get_tristate_value(sym)) {
434 case no:
435 if (write_no)
436 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
437 break;
438 case mod:
439 fprintf(out, "CONFIG_%s=m\n", sym->name);
440 break;
441 case yes:
442 fprintf(out, "CONFIG_%s=y\n", sym->name);
443 break;
444 }
445 break;
446 case S_STRING:
447 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
448 break;
449 case S_HEX:
450 case S_INT:
451 str = sym_get_string_value(sym);
452 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
453 break;
454 case S_OTHER:
455 case S_UNKNOWN:
456 break;
457 }
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460int conf_write(const char *name)
461{
Roman Zippelc955cca2006-06-08 22:12:39 -0700462 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct symbol *sym;
464 struct menu *menu;
465 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 const char *str;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200467 char dirname[128], tmpname[128], newname[128];
468 enum symbol_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 time_t now;
470 int use_timestamp = 1;
471 char *env;
472
473 dirname[0] = 0;
474 if (name && name[0]) {
475 struct stat st;
476 char *slash;
477
478 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
479 strcpy(dirname, name);
480 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700481 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else if ((slash = strrchr(name, '/'))) {
483 int size = slash - name + 1;
484 memcpy(dirname, name, size);
485 dirname[size] = 0;
486 if (slash[1])
487 basename = slash + 1;
488 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700489 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else
491 basename = name;
492 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700493 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700495 sprintf(newname, "%s%s", dirname, basename);
496 env = getenv("KCONFIG_OVERWRITECONFIG");
497 if (!env || !*env) {
498 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
499 out = fopen(tmpname, "w");
500 } else {
501 *tmpname = 0;
502 out = fopen(newname, "w");
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!out)
505 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700506
Sam Ravnborg2244cbd2006-01-16 12:12:12 +0100507 sym = sym_lookup("KERNELVERSION", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 sym_calc_value(sym);
509 time(&now);
510 env = getenv("KCONFIG_NOTIMESTAMP");
511 if (env && *env)
512 use_timestamp = 0;
513
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700514 fprintf(out, _("#\n"
515 "# Automatically generated make config: don't edit\n"
516 "# Linux kernel version: %s\n"
517 "%s%s"
518 "#\n"),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 sym_get_string_value(sym),
520 use_timestamp ? "# " : "",
521 use_timestamp ? ctime(&now) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Karsten Wieseb3214292006-12-13 00:34:06 -0800523 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 sym_clear_all_valid();
525
526 menu = rootmenu.list;
527 while (menu) {
528 sym = menu->sym;
529 if (!sym) {
530 if (!menu_is_visible(menu))
531 goto next;
532 str = menu_get_prompt(menu);
533 fprintf(out, "\n"
534 "#\n"
535 "# %s\n"
536 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else if (!(sym->flags & SYMBOL_CHOICE)) {
538 sym_calc_value(sym);
539 if (!(sym->flags & SYMBOL_WRITE))
540 goto next;
541 sym->flags &= ~SYMBOL_WRITE;
542 type = sym->type;
543 if (type == S_TRISTATE) {
544 sym_calc_value(modules_sym);
545 if (modules_sym->curr.tri == no)
546 type = S_BOOLEAN;
547 }
Sam Ravnborg49192f22010-07-31 23:35:33 +0200548 /* Write config symbol to file */
549 conf_write_symbol(sym, type, out, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Sam Ravnborg49192f22010-07-31 23:35:33 +0200552next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (menu->list) {
554 menu = menu->list;
555 continue;
556 }
557 if (menu->next)
558 menu = menu->next;
559 else while ((menu = menu->parent)) {
560 if (menu->next) {
561 menu = menu->next;
562 break;
563 }
564 }
565 }
566 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700567
568 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200569 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700570 strcat(dirname, ".old");
571 rename(newname, dirname);
572 if (rename(tmpname, newname))
573 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Roman Zippelddc97ca2006-06-08 22:12:38 -0700576 printf(_("#\n"
577 "# configuration written to %s\n"
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700578 "#\n"), newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700579
Karsten Wiesebfc10002006-12-13 00:34:07 -0800580 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return 0;
583}
Roman Zippelc955cca2006-06-08 22:12:39 -0700584
Trevor Keith4356f482009-09-18 12:49:23 -0700585static int conf_split_config(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700586{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200587 const char *name;
588 char path[128];
Roman Zippel2e3646e2006-06-08 22:12:42 -0700589 char *s, *d, c;
590 struct symbol *sym;
591 struct stat sb;
592 int res, i, fd;
593
Markus Heidelberg12122f62009-05-18 01:36:54 +0200594 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700595 conf_read_simple(name, S_DEF_AUTO);
596
597 if (chdir("include/config"))
598 return 1;
599
600 res = 0;
601 for_all_symbols(i, sym) {
602 sym_calc_value(sym);
603 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
604 continue;
605 if (sym->flags & SYMBOL_WRITE) {
606 if (sym->flags & SYMBOL_DEF_AUTO) {
607 /*
608 * symbol has old and new value,
609 * so compare them...
610 */
611 switch (sym->type) {
612 case S_BOOLEAN:
613 case S_TRISTATE:
614 if (sym_get_tristate_value(sym) ==
615 sym->def[S_DEF_AUTO].tri)
616 continue;
617 break;
618 case S_STRING:
619 case S_HEX:
620 case S_INT:
621 if (!strcmp(sym_get_string_value(sym),
622 sym->def[S_DEF_AUTO].val))
623 continue;
624 break;
625 default:
626 break;
627 }
628 } else {
629 /*
630 * If there is no old value, only 'no' (unset)
631 * is allowed as new value.
632 */
633 switch (sym->type) {
634 case S_BOOLEAN:
635 case S_TRISTATE:
636 if (sym_get_tristate_value(sym) == no)
637 continue;
638 break;
639 default:
640 break;
641 }
642 }
643 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
644 /* There is neither an old nor a new value. */
645 continue;
646 /* else
647 * There is an old value, but no new value ('no' (unset)
648 * isn't saved in auto.conf, so the old value is always
649 * different from 'no').
650 */
651
652 /* Replace all '_' and append ".h" */
653 s = sym->name;
654 d = path;
655 while ((c = *s++)) {
656 c = tolower(c);
657 *d++ = (c == '_') ? '/' : c;
658 }
659 strcpy(d, ".h");
660
661 /* Assume directory path already exists. */
662 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
663 if (fd == -1) {
664 if (errno != ENOENT) {
665 res = 1;
666 break;
667 }
668 /*
669 * Create directory components,
670 * unless they exist already.
671 */
672 d = path;
673 while ((d = strchr(d, '/'))) {
674 *d = 0;
675 if (stat(path, &sb) && mkdir(path, 0755)) {
676 res = 1;
677 goto out;
678 }
679 *d++ = '/';
680 }
681 /* Try it again. */
682 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
683 if (fd == -1) {
684 res = 1;
685 break;
686 }
687 }
688 close(fd);
689 }
690out:
691 if (chdir("../.."))
692 return 1;
693
694 return res;
695}
696
Roman Zippelc955cca2006-06-08 22:12:39 -0700697int conf_write_autoconf(void)
698{
699 struct symbol *sym;
700 const char *str;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200701 const char *name;
Michal Marekbc081dd2009-12-07 16:38:33 +0100702 FILE *out, *tristate, *out_h;
Roman Zippelc955cca2006-06-08 22:12:39 -0700703 time_t now;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200704 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -0700705
Roman Zippel2e3646e2006-06-08 22:12:42 -0700706 sym_clear_all_valid();
707
Roman Zippelc955cca2006-06-08 22:12:39 -0700708 file_write_dep("include/config/auto.conf.cmd");
709
Roman Zippel2e3646e2006-06-08 22:12:42 -0700710 if (conf_split_config())
711 return 1;
712
Roman Zippelc955cca2006-06-08 22:12:39 -0700713 out = fopen(".tmpconfig", "w");
714 if (!out)
715 return 1;
716
Michal Marekbc081dd2009-12-07 16:38:33 +0100717 tristate = fopen(".tmpconfig_tristate", "w");
718 if (!tristate) {
719 fclose(out);
720 return 1;
721 }
722
Roman Zippelc955cca2006-06-08 22:12:39 -0700723 out_h = fopen(".tmpconfig.h", "w");
724 if (!out_h) {
725 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100726 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700727 return 1;
728 }
729
730 sym = sym_lookup("KERNELVERSION", 0);
731 sym_calc_value(sym);
732 time(&now);
733 fprintf(out, "#\n"
734 "# Automatically generated make config: don't edit\n"
735 "# Linux kernel version: %s\n"
736 "# %s"
737 "#\n",
738 sym_get_string_value(sym), ctime(&now));
Michal Marekbc081dd2009-12-07 16:38:33 +0100739 fprintf(tristate, "#\n"
740 "# Automatically generated - do not edit\n"
741 "\n");
Roman Zippelc955cca2006-06-08 22:12:39 -0700742 fprintf(out_h, "/*\n"
743 " * Automatically generated C config: don't edit\n"
744 " * Linux kernel version: %s\n"
745 " * %s"
746 " */\n"
747 "#define AUTOCONF_INCLUDED\n",
748 sym_get_string_value(sym), ctime(&now));
749
Roman Zippelc955cca2006-06-08 22:12:39 -0700750 for_all_symbols(i, sym) {
751 sym_calc_value(sym);
752 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
753 continue;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200754
755 /* write symbol to config file */
756 conf_write_symbol(sym, sym->type, out, false);
757
758 /* update autoconf and tristate files */
Roman Zippelc955cca2006-06-08 22:12:39 -0700759 switch (sym->type) {
760 case S_BOOLEAN:
761 case S_TRISTATE:
762 switch (sym_get_tristate_value(sym)) {
763 case no:
764 break;
765 case mod:
Michal Marekbc081dd2009-12-07 16:38:33 +0100766 fprintf(tristate, "CONFIG_%s=M\n", sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700767 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
768 break;
769 case yes:
Michal Marekbc081dd2009-12-07 16:38:33 +0100770 if (sym->type == S_TRISTATE)
771 fprintf(tristate, "CONFIG_%s=Y\n",
772 sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700773 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
774 break;
775 }
776 break;
777 case S_STRING:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200778 conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
Roman Zippelc955cca2006-06-08 22:12:39 -0700779 break;
780 case S_HEX:
781 str = sym_get_string_value(sym);
782 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
Roman Zippelc955cca2006-06-08 22:12:39 -0700783 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
784 break;
785 }
786 case S_INT:
787 str = sym_get_string_value(sym);
Roman Zippelc955cca2006-06-08 22:12:39 -0700788 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
789 break;
790 default:
791 break;
792 }
793 }
794 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100795 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700796 fclose(out_h);
797
798 name = getenv("KCONFIG_AUTOHEADER");
799 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +0200800 name = "include/generated/autoconf.h";
Roman Zippelc955cca2006-06-08 22:12:39 -0700801 if (rename(".tmpconfig.h", name))
802 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +0100803 name = getenv("KCONFIG_TRISTATE");
804 if (!name)
805 name = "include/config/tristate.conf";
806 if (rename(".tmpconfig_tristate", name))
807 return 1;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200808 name = conf_get_autoconfig_name();
Roman Zippelc955cca2006-06-08 22:12:39 -0700809 /*
810 * This must be the last step, kbuild has a dependency on auto.conf
811 * and this marks the successful completion of the previous steps.
812 */
813 if (rename(".tmpconfig", name))
814 return 1;
815
816 return 0;
817}
Karsten Wieseb3214292006-12-13 00:34:06 -0800818
Karsten Wiesebfc10002006-12-13 00:34:07 -0800819static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800820static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800821
822void sym_set_change_count(int count)
823{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800824 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800825 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800826 if (conf_changed_callback &&
827 (bool)_sym_change_count != (bool)count)
828 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -0800829}
830
831void sym_add_change_count(int count)
832{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800833 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800834}
835
Karsten Wieseb3214292006-12-13 00:34:06 -0800836bool conf_get_changed(void)
837{
838 return sym_change_count;
839}
Karsten Wiese3b354c52006-12-13 00:34:08 -0800840
841void conf_set_changed_callback(void (*fn)(void))
842{
843 conf_changed_callback = fn;
844}
Roman Zippeldc7862e2008-05-06 04:55:55 +0200845
846
847void conf_set_all_new_symbols(enum conf_def_mode mode)
848{
849 struct symbol *sym, *csym;
850 struct property *prop;
851 struct expr *e;
852 int i, cnt, def;
853
854 for_all_symbols(i, sym) {
855 if (sym_has_value(sym))
856 continue;
857 switch (sym_get_type(sym)) {
858 case S_BOOLEAN:
859 case S_TRISTATE:
860 switch (mode) {
861 case def_yes:
862 sym->def[S_DEF_USER].tri = yes;
863 break;
864 case def_mod:
865 sym->def[S_DEF_USER].tri = mod;
866 break;
867 case def_no:
868 sym->def[S_DEF_USER].tri = no;
869 break;
870 case def_random:
Peter Korsgaard1244b412010-07-22 14:24:57 +0200871 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
872 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
Roman Zippeldc7862e2008-05-06 04:55:55 +0200873 break;
874 default:
875 continue;
876 }
Sam Ravnborg184832c2009-03-15 11:05:12 +0100877 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +0200878 sym->flags |= SYMBOL_DEF_USER;
879 break;
880 default:
881 break;
882 }
883
884 }
885
Al Viroce97e132008-10-26 05:12:34 +0000886 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +0200887
888 if (mode != def_random)
889 return;
Sam Ravnborg184832c2009-03-15 11:05:12 +0100890 /*
891 * We have different type of choice blocks.
892 * If curr.tri equal to mod then we can select several
893 * choice symbols in one block.
894 * In this case we do nothing.
895 * If curr.tri equal yes then only one symbol can be
896 * selected in a choice block and we set it to yes,
897 * and the rest to no.
898 */
Roman Zippeldc7862e2008-05-06 04:55:55 +0200899 for_all_symbols(i, csym) {
900 if (sym_has_value(csym) || !sym_is_choice(csym))
901 continue;
902
903 sym_calc_value(csym);
Sam Ravnborg184832c2009-03-15 11:05:12 +0100904
905 if (csym->curr.tri != yes)
906 continue;
907
Roman Zippeldc7862e2008-05-06 04:55:55 +0200908 prop = sym_get_choice_prop(csym);
Sam Ravnborg184832c2009-03-15 11:05:12 +0100909
910 /* count entries in choice block */
911 cnt = 0;
912 expr_list_for_each_sym(prop->expr, e, sym)
913 cnt++;
914
915 /*
916 * find a random value and set it to yes,
917 * set the rest to no so we have only one set
918 */
919 def = (rand() % cnt);
920
921 cnt = 0;
922 expr_list_for_each_sym(prop->expr, e, sym) {
923 if (def == cnt++) {
924 sym->def[S_DEF_USER].tri = yes;
925 csym->def[S_DEF_USER].val = sym;
Roman Zippeldc7862e2008-05-06 04:55:55 +0200926 }
Sam Ravnborg184832c2009-03-15 11:05:12 +0100927 else {
928 sym->def[S_DEF_USER].tri = no;
929 }
Roman Zippeldc7862e2008-05-06 04:55:55 +0200930 }
931 csym->flags |= SYMBOL_DEF_USER;
Sam Ravnborg184832c2009-03-15 11:05:12 +0100932 /* clear VALID to get value calculated */
933 csym->flags &= ~(SYMBOL_VALID);
Roman Zippeldc7862e2008-05-06 04:55:55 +0200934 }
935}