blob: 2ee48c377b66b5e3f01ba752ad1944f303e22ad9 [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
J.A. Magallon48b9d032005-06-25 14:59:22 -070044static char *conf_expand_value(const char *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct symbol *sym;
J.A. Magallon48b9d032005-06-25 14:59:22 -070047 const char *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 static char res_value[SYMBOL_MAXLENGTH];
49 char *dst, name[SYMBOL_MAXLENGTH];
50
51 res_value[0] = 0;
52 dst = name;
53 while ((src = strchr(in, '$'))) {
54 strncat(res_value, in, src - in);
55 src++;
56 dst = name;
57 while (isalnum(*src) || *src == '_')
58 *dst++ = *src++;
59 *dst = 0;
60 sym = sym_lookup(name, 0);
61 sym_calc_value(sym);
62 strcat(res_value, sym_get_string_value(sym));
63 in = src;
64 }
65 strcat(res_value, in);
66
67 return res_value;
68}
69
70char *conf_get_default_confname(void)
71{
72 struct stat buf;
73 static char fullname[PATH_MAX+1];
74 char *env, *name;
75
76 name = conf_expand_value(conf_defname);
77 env = getenv(SRCTREE);
78 if (env) {
79 sprintf(fullname, "%s/%s", env, name);
80 if (!stat(fullname, &buf))
81 return fullname;
82 }
83 return name;
84}
85
Roman Zippel669bfad2006-06-08 22:12:42 -070086int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 FILE *in = NULL;
89 char line[1024];
90 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct symbol *sym;
Roman Zippel669bfad2006-06-08 22:12:42 -070092 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (name) {
95 in = zconf_fopen(name);
96 } else {
Roman Zippelface4372006-06-08 22:12:45 -070097 struct property *prop;
98
Roman Zippel14cdd3c2006-06-08 22:12:51 -070099 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700100 in = zconf_fopen(name);
101 if (in)
102 goto load;
103 sym_change_count++;
Roman Zippelface4372006-06-08 22:12:45 -0700104 if (!sym_defconfig_list)
105 return 1;
106
107 for_all_defaults(sym_defconfig_list, prop) {
108 if (expr_calc_value(prop->visible.expr) == no ||
109 prop->expr->type != E_SYMBOL)
110 continue;
111 name = conf_expand_value(prop->expr->left.sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 in = zconf_fopen(name);
113 if (in) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700114 printf(_("#\n"
Roman Zippelddc97ca2006-06-08 22:12:38 -0700115 "# using defaults found in %s\n"
116 "#\n"), name);
117 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!in)
122 return 1;
123
Roman Zippelddc97ca2006-06-08 22:12:38 -0700124load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800125 conf_filename = name;
126 conf_lineno = 0;
127 conf_warnings = 0;
128 conf_unsaved = 0;
129
Roman Zippel669bfad2006-06-08 22:12:42 -0700130 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 for_all_symbols(i, sym) {
Roman Zippel669bfad2006-06-08 22:12:42 -0700132 sym->flags |= SYMBOL_CHANGED;
133 sym->flags &= ~(def_flags|SYMBOL_VALID);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800134 if (sym_is_choice(sym))
Roman Zippel669bfad2006-06-08 22:12:42 -0700135 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 switch (sym->type) {
137 case S_INT:
138 case S_HEX:
139 case S_STRING:
Roman Zippel669bfad2006-06-08 22:12:42 -0700140 if (sym->def[def].val)
141 free(sym->def[def].val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 default:
Roman Zippel669bfad2006-06-08 22:12:42 -0700143 sym->def[def].val = NULL;
144 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 }
147
148 while (fgets(line, sizeof(line), in)) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800149 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sym = NULL;
151 switch (line[0]) {
152 case '#':
153 if (memcmp(line + 2, "CONFIG_", 7))
154 continue;
155 p = strchr(line + 9, ' ');
156 if (!p)
157 continue;
158 *p++ = 0;
159 if (strncmp(p, "is not set", 10))
160 continue;
Roman Zippel669bfad2006-06-08 22:12:42 -0700161 if (def == S_DEF_USER) {
162 sym = sym_find(line + 9);
163 if (!sym) {
164 conf_warning("trying to assign nonexistent symbol %s", line + 9);
165 break;
166 }
167 } else {
168 sym = sym_lookup(line + 9, 0);
169 if (sym->type == S_UNKNOWN)
170 sym->type = S_BOOLEAN;
171 }
172 if (sym->flags & def_flags) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800173 conf_warning("trying to reassign symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 }
176 switch (sym->type) {
177 case S_BOOLEAN:
178 case S_TRISTATE:
Roman Zippel669bfad2006-06-08 22:12:42 -0700179 sym->def[def].tri = no;
180 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 default:
183 ;
184 }
185 break;
186 case 'C':
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800187 if (memcmp(line, "CONFIG_", 7)) {
188 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 p = strchr(line + 7, '=');
192 if (!p)
193 continue;
194 *p++ = 0;
195 p2 = strchr(p, '\n');
196 if (p2)
197 *p2 = 0;
Roman Zippel669bfad2006-06-08 22:12:42 -0700198 if (def == S_DEF_USER) {
199 sym = sym_find(line + 7);
200 if (!sym) {
201 conf_warning("trying to assign nonexistent symbol %s", line + 7);
202 break;
203 }
204 } else {
205 sym = sym_lookup(line + 7, 0);
206 if (sym->type == S_UNKNOWN)
207 sym->type = S_OTHER;
208 }
209 if (sym->flags & def_flags) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800210 conf_warning("trying to reassign symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 }
213 switch (sym->type) {
214 case S_TRISTATE:
215 if (p[0] == 'm') {
Roman Zippel669bfad2006-06-08 22:12:42 -0700216 sym->def[def].tri = mod;
217 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220 case S_BOOLEAN:
221 if (p[0] == 'y') {
Roman Zippel669bfad2006-06-08 22:12:42 -0700222 sym->def[def].tri = yes;
223 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 }
226 if (p[0] == 'n') {
Roman Zippel669bfad2006-06-08 22:12:42 -0700227 sym->def[def].tri = no;
228 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230 }
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800231 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
Roman Zippel669bfad2006-06-08 22:12:42 -0700233 case S_OTHER:
234 if (*p != '"') {
235 for (p2 = p; *p2 && !isspace(*p2); p2++)
236 ;
237 sym->type = S_STRING;
238 goto done;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case S_STRING:
241 if (*p++ != '"')
242 break;
243 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
244 if (*p2 == '"') {
245 *p2 = 0;
246 break;
247 }
248 memmove(p2, p2 + 1, strlen(p2));
249 }
250 if (!p2) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800251 conf_warning("invalid string found");
252 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 case S_INT:
255 case S_HEX:
Roman Zippel669bfad2006-06-08 22:12:42 -0700256 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (sym_string_valid(sym, p)) {
Roman Zippel669bfad2006-06-08 22:12:42 -0700258 sym->def[def].val = strdup(p);
259 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800261 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
262 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 break;
265 default:
266 ;
267 }
268 break;
269 case '\n':
270 break;
271 default:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800272 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 continue;
274 }
275 if (sym && sym_is_choice_value(sym)) {
276 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad2006-06-08 22:12:42 -0700277 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 case no:
279 break;
280 case mod:
Roman Zippel669bfad2006-06-08 22:12:42 -0700281 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800282 conf_warning("%s creates inconsistent choice state", sym->name);
Roman Zippel669bfad2006-06-08 22:12:42 -0700283 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 case yes:
Roman Zippel669bfad2006-06-08 22:12:42 -0700287 if (cs->def[def].tri != no) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800288 conf_warning("%s creates inconsistent choice state", sym->name);
Roman Zippel669bfad2006-06-08 22:12:42 -0700289 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800290 } else
Roman Zippel669bfad2006-06-08 22:12:42 -0700291 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293 }
Roman Zippel669bfad2006-06-08 22:12:42 -0700294 cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 }
297 fclose(in);
298
299 if (modules_sym)
300 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800301 return 0;
302}
303
304int conf_read(const char *name)
305{
306 struct symbol *sym;
307 struct property *prop;
308 struct expr *e;
Roman Zippel669bfad2006-06-08 22:12:42 -0700309 int i, flags;
Roman Zippel90389162005-11-08 21:34:49 -0800310
Roman Zippelddc97ca2006-06-08 22:12:38 -0700311 sym_change_count = 0;
312
Roman Zippel669bfad2006-06-08 22:12:42 -0700313 if (conf_read_simple(name, S_DEF_USER))
Roman Zippel90389162005-11-08 21:34:49 -0800314 return 1;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for_all_symbols(i, sym) {
317 sym_calc_value(sym);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800318 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
319 goto sym_ok;
320 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
321 /* check that calculated value agrees with saved value */
322 switch (sym->type) {
323 case S_BOOLEAN:
324 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700325 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800326 break;
327 if (!sym_is_choice(sym))
328 goto sym_ok;
329 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700330 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800331 goto sym_ok;
332 break;
333 }
334 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
335 /* no previous value and not saved */
336 goto sym_ok;
337 conf_unsaved++;
338 /* maybe print value in verbose mode... */
339 sym_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
341 if (sym->visible == no)
Roman Zippel669bfad2006-06-08 22:12:42 -0700342 sym->flags &= ~SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 switch (sym->type) {
344 case S_STRING:
345 case S_INT:
346 case S_HEX:
Roman Zippel669bfad2006-06-08 22:12:42 -0700347 if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
348 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 default:
350 break;
351 }
352 }
353 if (!sym_is_choice(sym))
354 continue;
355 prop = sym_get_choice_prop(sym);
Roman Zippel669bfad2006-06-08 22:12:42 -0700356 flags = sym->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 for (e = prop->expr; e; e = e->left.expr)
358 if (e->right.sym->visible != no)
Roman Zippel669bfad2006-06-08 22:12:42 -0700359 flags &= e->right.sym->flags;
360 sym->flags |= flags & SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Roman Zippelddc97ca2006-06-08 22:12:38 -0700363 sym_change_count += conf_warnings || conf_unsaved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 return 0;
366}
367
368int conf_write(const char *name)
369{
Roman Zippelc955cca2006-06-08 22:12:39 -0700370 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct symbol *sym;
372 struct menu *menu;
373 const char *basename;
374 char dirname[128], tmpname[128], newname[128];
375 int type, l;
376 const char *str;
377 time_t now;
378 int use_timestamp = 1;
379 char *env;
380
381 dirname[0] = 0;
382 if (name && name[0]) {
383 struct stat st;
384 char *slash;
385
386 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
387 strcpy(dirname, name);
388 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700389 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else if ((slash = strrchr(name, '/'))) {
391 int size = slash - name + 1;
392 memcpy(dirname, name, size);
393 dirname[size] = 0;
394 if (slash[1])
395 basename = slash + 1;
396 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700397 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else
399 basename = name;
400 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700401 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700403 sprintf(newname, "%s%s", dirname, basename);
404 env = getenv("KCONFIG_OVERWRITECONFIG");
405 if (!env || !*env) {
406 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
407 out = fopen(tmpname, "w");
408 } else {
409 *tmpname = 0;
410 out = fopen(newname, "w");
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!out)
413 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700414
Sam Ravnborg2244cbd2006-01-16 12:12:12 +0100415 sym = sym_lookup("KERNELVERSION", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sym_calc_value(sym);
417 time(&now);
418 env = getenv("KCONFIG_NOTIMESTAMP");
419 if (env && *env)
420 use_timestamp = 0;
421
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700422 fprintf(out, _("#\n"
423 "# Automatically generated make config: don't edit\n"
424 "# Linux kernel version: %s\n"
425 "%s%s"
426 "#\n"),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 sym_get_string_value(sym),
428 use_timestamp ? "# " : "",
429 use_timestamp ? ctime(&now) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (!sym_change_count)
432 sym_clear_all_valid();
433
434 menu = rootmenu.list;
435 while (menu) {
436 sym = menu->sym;
437 if (!sym) {
438 if (!menu_is_visible(menu))
439 goto next;
440 str = menu_get_prompt(menu);
441 fprintf(out, "\n"
442 "#\n"
443 "# %s\n"
444 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else if (!(sym->flags & SYMBOL_CHOICE)) {
446 sym_calc_value(sym);
447 if (!(sym->flags & SYMBOL_WRITE))
448 goto next;
449 sym->flags &= ~SYMBOL_WRITE;
450 type = sym->type;
451 if (type == S_TRISTATE) {
452 sym_calc_value(modules_sym);
453 if (modules_sym->curr.tri == no)
454 type = S_BOOLEAN;
455 }
456 switch (type) {
457 case S_BOOLEAN:
458 case S_TRISTATE:
459 switch (sym_get_tristate_value(sym)) {
460 case no:
461 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463 case mod:
464 fprintf(out, "CONFIG_%s=m\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case yes:
467 fprintf(out, "CONFIG_%s=y\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 }
470 break;
471 case S_STRING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 str = sym_get_string_value(sym);
473 fprintf(out, "CONFIG_%s=\"", sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700474 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 l = strcspn(str, "\"\\");
476 if (l) {
477 fwrite(str, l, 1, out);
Roman Zippelc955cca2006-06-08 22:12:39 -0700478 str += l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700480 if (!*str)
481 break;
482 fprintf(out, "\\%c", *str++);
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 fputs("\"\n", out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 case S_HEX:
487 str = sym_get_string_value(sym);
488 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
489 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 }
492 case S_INT:
493 str = sym_get_string_value(sym);
494 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 }
497 }
498
499 next:
500 if (menu->list) {
501 menu = menu->list;
502 continue;
503 }
504 if (menu->next)
505 menu = menu->next;
506 else while ((menu = menu->parent)) {
507 if (menu->next) {
508 menu = menu->next;
509 break;
510 }
511 }
512 }
513 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700514
515 if (*tmpname) {
516 strcat(dirname, name ? name : conf_get_configname());
517 strcat(dirname, ".old");
518 rename(newname, dirname);
519 if (rename(tmpname, newname))
520 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Roman Zippelddc97ca2006-06-08 22:12:38 -0700523 printf(_("#\n"
524 "# configuration written to %s\n"
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700525 "#\n"), newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sym_change_count = 0;
528
529 return 0;
530}
Roman Zippelc955cca2006-06-08 22:12:39 -0700531
Roman Zippel2e3646e2006-06-08 22:12:42 -0700532int conf_split_config(void)
533{
534 char *name, path[128];
535 char *s, *d, c;
536 struct symbol *sym;
537 struct stat sb;
538 int res, i, fd;
539
540 name = getenv("KCONFIG_AUTOCONFIG");
541 if (!name)
542 name = "include/config/auto.conf";
543 conf_read_simple(name, S_DEF_AUTO);
544
545 if (chdir("include/config"))
546 return 1;
547
548 res = 0;
549 for_all_symbols(i, sym) {
550 sym_calc_value(sym);
551 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
552 continue;
553 if (sym->flags & SYMBOL_WRITE) {
554 if (sym->flags & SYMBOL_DEF_AUTO) {
555 /*
556 * symbol has old and new value,
557 * so compare them...
558 */
559 switch (sym->type) {
560 case S_BOOLEAN:
561 case S_TRISTATE:
562 if (sym_get_tristate_value(sym) ==
563 sym->def[S_DEF_AUTO].tri)
564 continue;
565 break;
566 case S_STRING:
567 case S_HEX:
568 case S_INT:
569 if (!strcmp(sym_get_string_value(sym),
570 sym->def[S_DEF_AUTO].val))
571 continue;
572 break;
573 default:
574 break;
575 }
576 } else {
577 /*
578 * If there is no old value, only 'no' (unset)
579 * is allowed as new value.
580 */
581 switch (sym->type) {
582 case S_BOOLEAN:
583 case S_TRISTATE:
584 if (sym_get_tristate_value(sym) == no)
585 continue;
586 break;
587 default:
588 break;
589 }
590 }
591 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
592 /* There is neither an old nor a new value. */
593 continue;
594 /* else
595 * There is an old value, but no new value ('no' (unset)
596 * isn't saved in auto.conf, so the old value is always
597 * different from 'no').
598 */
599
600 /* Replace all '_' and append ".h" */
601 s = sym->name;
602 d = path;
603 while ((c = *s++)) {
604 c = tolower(c);
605 *d++ = (c == '_') ? '/' : c;
606 }
607 strcpy(d, ".h");
608
609 /* Assume directory path already exists. */
610 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
611 if (fd == -1) {
612 if (errno != ENOENT) {
613 res = 1;
614 break;
615 }
616 /*
617 * Create directory components,
618 * unless they exist already.
619 */
620 d = path;
621 while ((d = strchr(d, '/'))) {
622 *d = 0;
623 if (stat(path, &sb) && mkdir(path, 0755)) {
624 res = 1;
625 goto out;
626 }
627 *d++ = '/';
628 }
629 /* Try it again. */
630 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
631 if (fd == -1) {
632 res = 1;
633 break;
634 }
635 }
636 close(fd);
637 }
638out:
639 if (chdir("../.."))
640 return 1;
641
642 return res;
643}
644
Roman Zippelc955cca2006-06-08 22:12:39 -0700645int conf_write_autoconf(void)
646{
647 struct symbol *sym;
648 const char *str;
649 char *name;
650 FILE *out, *out_h;
651 time_t now;
652 int i, l;
653
Roman Zippel2e3646e2006-06-08 22:12:42 -0700654 sym_clear_all_valid();
655
Roman Zippelc955cca2006-06-08 22:12:39 -0700656 file_write_dep("include/config/auto.conf.cmd");
657
Roman Zippel2e3646e2006-06-08 22:12:42 -0700658 if (conf_split_config())
659 return 1;
660
Roman Zippelc955cca2006-06-08 22:12:39 -0700661 out = fopen(".tmpconfig", "w");
662 if (!out)
663 return 1;
664
665 out_h = fopen(".tmpconfig.h", "w");
666 if (!out_h) {
667 fclose(out);
668 return 1;
669 }
670
671 sym = sym_lookup("KERNELVERSION", 0);
672 sym_calc_value(sym);
673 time(&now);
674 fprintf(out, "#\n"
675 "# Automatically generated make config: don't edit\n"
676 "# Linux kernel version: %s\n"
677 "# %s"
678 "#\n",
679 sym_get_string_value(sym), ctime(&now));
680 fprintf(out_h, "/*\n"
681 " * Automatically generated C config: don't edit\n"
682 " * Linux kernel version: %s\n"
683 " * %s"
684 " */\n"
685 "#define AUTOCONF_INCLUDED\n",
686 sym_get_string_value(sym), ctime(&now));
687
Roman Zippelc955cca2006-06-08 22:12:39 -0700688 for_all_symbols(i, sym) {
689 sym_calc_value(sym);
690 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
691 continue;
692 switch (sym->type) {
693 case S_BOOLEAN:
694 case S_TRISTATE:
695 switch (sym_get_tristate_value(sym)) {
696 case no:
697 break;
698 case mod:
699 fprintf(out, "CONFIG_%s=m\n", sym->name);
700 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
701 break;
702 case yes:
703 fprintf(out, "CONFIG_%s=y\n", sym->name);
704 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
705 break;
706 }
707 break;
708 case S_STRING:
709 str = sym_get_string_value(sym);
710 fprintf(out, "CONFIG_%s=\"", sym->name);
711 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
712 while (1) {
713 l = strcspn(str, "\"\\");
714 if (l) {
715 fwrite(str, l, 1, out);
716 fwrite(str, l, 1, out_h);
717 str += l;
718 }
719 if (!*str)
720 break;
721 fprintf(out, "\\%c", *str);
722 fprintf(out_h, "\\%c", *str);
723 str++;
724 }
725 fputs("\"\n", out);
726 fputs("\"\n", out_h);
727 break;
728 case S_HEX:
729 str = sym_get_string_value(sym);
730 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
731 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
732 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
733 break;
734 }
735 case S_INT:
736 str = sym_get_string_value(sym);
737 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
738 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
739 break;
740 default:
741 break;
742 }
743 }
744 fclose(out);
745 fclose(out_h);
746
747 name = getenv("KCONFIG_AUTOHEADER");
748 if (!name)
749 name = "include/linux/autoconf.h";
750 if (rename(".tmpconfig.h", name))
751 return 1;
752 name = getenv("KCONFIG_AUTOCONFIG");
753 if (!name)
754 name = "include/config/auto.conf";
755 /*
756 * This must be the last step, kbuild has a dependency on auto.conf
757 * and this marks the successful completion of the previous steps.
758 */
759 if (rename(".tmpconfig", name))
760 return 1;
761
762 return 0;
763}