kconfig: integrate split config into silentoldconfig
[linux-3.10.git] / scripts / kconfig / confdata.c
1 /*
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>
8 #include <fcntl.h>
9 #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
18 static void conf_warning(const char *fmt, ...)
19         __attribute__ ((format (printf, 1, 2)));
20
21 static const char *conf_filename;
22 static int conf_lineno, conf_warnings, conf_unsaved;
23
24 const char conf_def_filename[] = ".config";
25
26 const char conf_defname[] = "arch/$ARCH/defconfig";
27
28 const char *conf_confnames[] = {
29         ".config",
30         "/lib/modules/$UNAME_RELEASE/.config",
31         "/etc/kernel-config",
32         "/boot/config-$UNAME_RELEASE",
33         conf_defname,
34         NULL,
35 };
36
37 static void conf_warning(const char *fmt, ...)
38 {
39         va_list ap;
40         va_start(ap, fmt);
41         fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
42         vfprintf(stderr, fmt, ap);
43         fprintf(stderr, "\n");
44         va_end(ap);
45         conf_warnings++;
46 }
47
48 static char *conf_expand_value(const char *in)
49 {
50         struct symbol *sym;
51         const char *src;
52         static char res_value[SYMBOL_MAXLENGTH];
53         char *dst, name[SYMBOL_MAXLENGTH];
54
55         res_value[0] = 0;
56         dst = name;
57         while ((src = strchr(in, '$'))) {
58                 strncat(res_value, in, src - in);
59                 src++;
60                 dst = name;
61                 while (isalnum(*src) || *src == '_')
62                         *dst++ = *src++;
63                 *dst = 0;
64                 sym = sym_lookup(name, 0);
65                 sym_calc_value(sym);
66                 strcat(res_value, sym_get_string_value(sym));
67                 in = src;
68         }
69         strcat(res_value, in);
70
71         return res_value;
72 }
73
74 char *conf_get_default_confname(void)
75 {
76         struct stat buf;
77         static char fullname[PATH_MAX+1];
78         char *env, *name;
79
80         name = conf_expand_value(conf_defname);
81         env = getenv(SRCTREE);
82         if (env) {
83                 sprintf(fullname, "%s/%s", env, name);
84                 if (!stat(fullname, &buf))
85                         return fullname;
86         }
87         return name;
88 }
89
90 int conf_read_simple(const char *name, int def)
91 {
92         FILE *in = NULL;
93         char line[1024];
94         char *p, *p2;
95         struct symbol *sym;
96         int i, def_flags;
97
98         if (name) {
99                 in = zconf_fopen(name);
100         } else {
101                 const char **names = conf_confnames;
102                 name = *names++;
103                 if (!name)
104                         return 1;
105                 in = zconf_fopen(name);
106                 if (in)
107                         goto load;
108                 sym_change_count++;
109                 while ((name = *names++)) {
110                         name = conf_expand_value(name);
111                         in = zconf_fopen(name);
112                         if (in) {
113                                 printf(_("#\n"
114                                          "# using defaults found in %s\n"
115                                          "#\n"), name);
116                                 goto load;
117                         }
118                 }
119         }
120         if (!in)
121                 return 1;
122
123 load:
124         conf_filename = name;
125         conf_lineno = 0;
126         conf_warnings = 0;
127         conf_unsaved = 0;
128
129         def_flags = SYMBOL_DEF << def;
130         for_all_symbols(i, sym) {
131                 sym->flags |= SYMBOL_CHANGED;
132                 sym->flags &= ~(def_flags|SYMBOL_VALID);
133                 if (sym_is_choice(sym))
134                         sym->flags |= def_flags;
135                 switch (sym->type) {
136                 case S_INT:
137                 case S_HEX:
138                 case S_STRING:
139                         if (sym->def[def].val)
140                                 free(sym->def[def].val);
141                 default:
142                         sym->def[def].val = NULL;
143                         sym->def[def].tri = no;
144                 }
145         }
146
147         while (fgets(line, sizeof(line), in)) {
148                 conf_lineno++;
149                 sym = NULL;
150                 switch (line[0]) {
151                 case '#':
152                         if (memcmp(line + 2, "CONFIG_", 7))
153                                 continue;
154                         p = strchr(line + 9, ' ');
155                         if (!p)
156                                 continue;
157                         *p++ = 0;
158                         if (strncmp(p, "is not set", 10))
159                                 continue;
160                         if (def == S_DEF_USER) {
161                                 sym = sym_find(line + 9);
162                                 if (!sym) {
163                                         conf_warning("trying to assign nonexistent symbol %s", line + 9);
164                                         break;
165                                 }
166                         } else {
167                                 sym = sym_lookup(line + 9, 0);
168                                 if (sym->type == S_UNKNOWN)
169                                         sym->type = S_BOOLEAN;
170                         }
171                         if (sym->flags & def_flags) {
172                                 conf_warning("trying to reassign symbol %s", sym->name);
173                                 break;
174                         }
175                         switch (sym->type) {
176                         case S_BOOLEAN:
177                         case S_TRISTATE:
178                                 sym->def[def].tri = no;
179                                 sym->flags |= def_flags;
180                                 break;
181                         default:
182                                 ;
183                         }
184                         break;
185                 case 'C':
186                         if (memcmp(line, "CONFIG_", 7)) {
187                                 conf_warning("unexpected data");
188                                 continue;
189                         }
190                         p = strchr(line + 7, '=');
191                         if (!p)
192                                 continue;
193                         *p++ = 0;
194                         p2 = strchr(p, '\n');
195                         if (p2)
196                                 *p2 = 0;
197                         if (def == S_DEF_USER) {
198                                 sym = sym_find(line + 7);
199                                 if (!sym) {
200                                         conf_warning("trying to assign nonexistent symbol %s", line + 7);
201                                         break;
202                                 }
203                         } else {
204                                 sym = sym_lookup(line + 7, 0);
205                                 if (sym->type == S_UNKNOWN)
206                                         sym->type = S_OTHER;
207                         }
208                         if (sym->flags & def_flags) {
209                                 conf_warning("trying to reassign symbol %s", sym->name);
210                                 break;
211                         }
212                         switch (sym->type) {
213                         case S_TRISTATE:
214                                 if (p[0] == 'm') {
215                                         sym->def[def].tri = mod;
216                                         sym->flags |= def_flags;
217                                         break;
218                                 }
219                         case S_BOOLEAN:
220                                 if (p[0] == 'y') {
221                                         sym->def[def].tri = yes;
222                                         sym->flags |= def_flags;
223                                         break;
224                                 }
225                                 if (p[0] == 'n') {
226                                         sym->def[def].tri = no;
227                                         sym->flags |= def_flags;
228                                         break;
229                                 }
230                                 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
231                                 break;
232                         case S_OTHER:
233                                 if (*p != '"') {
234                                         for (p2 = p; *p2 && !isspace(*p2); p2++)
235                                                 ;
236                                         sym->type = S_STRING;
237                                         goto done;
238                                 }
239                         case S_STRING:
240                                 if (*p++ != '"')
241                                         break;
242                                 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
243                                         if (*p2 == '"') {
244                                                 *p2 = 0;
245                                                 break;
246                                         }
247                                         memmove(p2, p2 + 1, strlen(p2));
248                                 }
249                                 if (!p2) {
250                                         conf_warning("invalid string found");
251                                         continue;
252                                 }
253                         case S_INT:
254                         case S_HEX:
255                         done:
256                                 if (sym_string_valid(sym, p)) {
257                                         sym->def[def].val = strdup(p);
258                                         sym->flags |= def_flags;
259                                 } else {
260                                         conf_warning("symbol value '%s' invalid for %s", p, sym->name);
261                                         continue;
262                                 }
263                                 break;
264                         default:
265                                 ;
266                         }
267                         break;
268                 case '\n':
269                         break;
270                 default:
271                         conf_warning("unexpected data");
272                         continue;
273                 }
274                 if (sym && sym_is_choice_value(sym)) {
275                         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
276                         switch (sym->def[def].tri) {
277                         case no:
278                                 break;
279                         case mod:
280                                 if (cs->def[def].tri == yes) {
281                                         conf_warning("%s creates inconsistent choice state", sym->name);
282                                         cs->flags &= ~def_flags;
283                                 }
284                                 break;
285                         case yes:
286                                 if (cs->def[def].tri != no) {
287                                         conf_warning("%s creates inconsistent choice state", sym->name);
288                                         cs->flags &= ~def_flags;
289                                 } else
290                                         cs->def[def].val = sym;
291                                 break;
292                         }
293                         cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
294                 }
295         }
296         fclose(in);
297
298         if (modules_sym)
299                 sym_calc_value(modules_sym);
300         return 0;
301 }
302
303 int conf_read(const char *name)
304 {
305         struct symbol *sym;
306         struct property *prop;
307         struct expr *e;
308         int i, flags;
309
310         sym_change_count = 0;
311
312         if (conf_read_simple(name, S_DEF_USER))
313                 return 1;
314
315         for_all_symbols(i, sym) {
316                 sym_calc_value(sym);
317                 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
318                         goto sym_ok;
319                 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
320                         /* check that calculated value agrees with saved value */
321                         switch (sym->type) {
322                         case S_BOOLEAN:
323                         case S_TRISTATE:
324                                 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
325                                         break;
326                                 if (!sym_is_choice(sym))
327                                         goto sym_ok;
328                         default:
329                                 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
330                                         goto sym_ok;
331                                 break;
332                         }
333                 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
334                         /* no previous value and not saved */
335                         goto sym_ok;
336                 conf_unsaved++;
337                 /* maybe print value in verbose mode... */
338         sym_ok:
339                 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
340                         if (sym->visible == no)
341                                 sym->flags &= ~SYMBOL_DEF_USER;
342                         switch (sym->type) {
343                         case S_STRING:
344                         case S_INT:
345                         case S_HEX:
346                                 if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
347                                         sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
348                         default:
349                                 break;
350                         }
351                 }
352                 if (!sym_is_choice(sym))
353                         continue;
354                 prop = sym_get_choice_prop(sym);
355                 flags = sym->flags;
356                 for (e = prop->expr; e; e = e->left.expr)
357                         if (e->right.sym->visible != no)
358                                 flags &= e->right.sym->flags;
359                 sym->flags |= flags & SYMBOL_DEF_USER;
360         }
361
362         sym_change_count += conf_warnings || conf_unsaved;
363
364         return 0;
365 }
366
367 int conf_write(const char *name)
368 {
369         FILE *out;
370         struct symbol *sym;
371         struct menu *menu;
372         const char *basename;
373         char dirname[128], tmpname[128], newname[128];
374         int type, l;
375         const char *str;
376         time_t now;
377         int use_timestamp = 1;
378         char *env;
379
380         dirname[0] = 0;
381         if (name && name[0]) {
382                 struct stat st;
383                 char *slash;
384
385                 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
386                         strcpy(dirname, name);
387                         strcat(dirname, "/");
388                         basename = conf_def_filename;
389                 } else if ((slash = strrchr(name, '/'))) {
390                         int size = slash - name + 1;
391                         memcpy(dirname, name, size);
392                         dirname[size] = 0;
393                         if (slash[1])
394                                 basename = slash + 1;
395                         else
396                                 basename = conf_def_filename;
397                 } else
398                         basename = name;
399         } else
400                 basename = conf_def_filename;
401
402         sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
403         out = fopen(newname, "w");
404         if (!out)
405                 return 1;
406         sym = sym_lookup("KERNELVERSION", 0);
407         sym_calc_value(sym);
408         time(&now);
409         env = getenv("KCONFIG_NOTIMESTAMP");
410         if (env && *env)
411                 use_timestamp = 0;
412
413         fprintf(out, _("#\n"
414                        "# Automatically generated make config: don't edit\n"
415                        "# Linux kernel version: %s\n"
416                        "%s%s"
417                        "#\n"),
418                      sym_get_string_value(sym),
419                      use_timestamp ? "# " : "",
420                      use_timestamp ? ctime(&now) : "");
421
422         if (!sym_change_count)
423                 sym_clear_all_valid();
424
425         menu = rootmenu.list;
426         while (menu) {
427                 sym = menu->sym;
428                 if (!sym) {
429                         if (!menu_is_visible(menu))
430                                 goto next;
431                         str = menu_get_prompt(menu);
432                         fprintf(out, "\n"
433                                      "#\n"
434                                      "# %s\n"
435                                      "#\n", str);
436                 } else if (!(sym->flags & SYMBOL_CHOICE)) {
437                         sym_calc_value(sym);
438                         if (!(sym->flags & SYMBOL_WRITE))
439                                 goto next;
440                         sym->flags &= ~SYMBOL_WRITE;
441                         type = sym->type;
442                         if (type == S_TRISTATE) {
443                                 sym_calc_value(modules_sym);
444                                 if (modules_sym->curr.tri == no)
445                                         type = S_BOOLEAN;
446                         }
447                         switch (type) {
448                         case S_BOOLEAN:
449                         case S_TRISTATE:
450                                 switch (sym_get_tristate_value(sym)) {
451                                 case no:
452                                         fprintf(out, "# CONFIG_%s is not set\n", sym->name);
453                                         break;
454                                 case mod:
455                                         fprintf(out, "CONFIG_%s=m\n", sym->name);
456                                         break;
457                                 case yes:
458                                         fprintf(out, "CONFIG_%s=y\n", sym->name);
459                                         break;
460                                 }
461                                 break;
462                         case S_STRING:
463                                 str = sym_get_string_value(sym);
464                                 fprintf(out, "CONFIG_%s=\"", sym->name);
465                                 while (1) {
466                                         l = strcspn(str, "\"\\");
467                                         if (l) {
468                                                 fwrite(str, l, 1, out);
469                                                 str += l;
470                                         }
471                                         if (!*str)
472                                                 break;
473                                         fprintf(out, "\\%c", *str++);
474                                 }
475                                 fputs("\"\n", out);
476                                 break;
477                         case S_HEX:
478                                 str = sym_get_string_value(sym);
479                                 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
480                                         fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
481                                         break;
482                                 }
483                         case S_INT:
484                                 str = sym_get_string_value(sym);
485                                 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
486                                 break;
487                         }
488                 }
489
490         next:
491                 if (menu->list) {
492                         menu = menu->list;
493                         continue;
494                 }
495                 if (menu->next)
496                         menu = menu->next;
497                 else while ((menu = menu->parent)) {
498                         if (menu->next) {
499                                 menu = menu->next;
500                                 break;
501                         }
502                 }
503         }
504         fclose(out);
505         if (!name || basename != conf_def_filename) {
506                 if (!name)
507                         name = conf_def_filename;
508                 sprintf(tmpname, "%s.old", name);
509                 rename(name, tmpname);
510         }
511         sprintf(tmpname, "%s%s", dirname, basename);
512         if (rename(newname, tmpname))
513                 return 1;
514
515         printf(_("#\n"
516                  "# configuration written to %s\n"
517                  "#\n"), tmpname);
518
519         sym_change_count = 0;
520
521         return 0;
522 }
523
524 int conf_split_config(void)
525 {
526         char *name, path[128];
527         char *s, *d, c;
528         struct symbol *sym;
529         struct stat sb;
530         int res, i, fd;
531
532         name = getenv("KCONFIG_AUTOCONFIG");
533         if (!name)
534                 name = "include/config/auto.conf";
535         conf_read_simple(name, S_DEF_AUTO);
536
537         if (chdir("include/config"))
538                 return 1;
539
540         res = 0;
541         for_all_symbols(i, sym) {
542                 sym_calc_value(sym);
543                 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
544                         continue;
545                 if (sym->flags & SYMBOL_WRITE) {
546                         if (sym->flags & SYMBOL_DEF_AUTO) {
547                                 /*
548                                  * symbol has old and new value,
549                                  * so compare them...
550                                  */
551                                 switch (sym->type) {
552                                 case S_BOOLEAN:
553                                 case S_TRISTATE:
554                                         if (sym_get_tristate_value(sym) ==
555                                             sym->def[S_DEF_AUTO].tri)
556                                                 continue;
557                                         break;
558                                 case S_STRING:
559                                 case S_HEX:
560                                 case S_INT:
561                                         if (!strcmp(sym_get_string_value(sym),
562                                                     sym->def[S_DEF_AUTO].val))
563                                                 continue;
564                                         break;
565                                 default:
566                                         break;
567                                 }
568                         } else {
569                                 /*
570                                  * If there is no old value, only 'no' (unset)
571                                  * is allowed as new value.
572                                  */
573                                 switch (sym->type) {
574                                 case S_BOOLEAN:
575                                 case S_TRISTATE:
576                                         if (sym_get_tristate_value(sym) == no)
577                                                 continue;
578                                         break;
579                                 default:
580                                         break;
581                                 }
582                         }
583                 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
584                         /* There is neither an old nor a new value. */
585                         continue;
586                 /* else
587                  *      There is an old value, but no new value ('no' (unset)
588                  *      isn't saved in auto.conf, so the old value is always
589                  *      different from 'no').
590                  */
591
592                 /* Replace all '_' and append ".h" */
593                 s = sym->name;
594                 d = path;
595                 while ((c = *s++)) {
596                         c = tolower(c);
597                         *d++ = (c == '_') ? '/' : c;
598                 }
599                 strcpy(d, ".h");
600
601                 /* Assume directory path already exists. */
602                 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
603                 if (fd == -1) {
604                         if (errno != ENOENT) {
605                                 res = 1;
606                                 break;
607                         }
608                         /*
609                          * Create directory components,
610                          * unless they exist already.
611                          */
612                         d = path;
613                         while ((d = strchr(d, '/'))) {
614                                 *d = 0;
615                                 if (stat(path, &sb) && mkdir(path, 0755)) {
616                                         res = 1;
617                                         goto out;
618                                 }
619                                 *d++ = '/';
620                         }
621                         /* Try it again. */
622                         fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
623                         if (fd == -1) {
624                                 res = 1;
625                                 break;
626                         }
627                 }
628                 close(fd);
629         }
630 out:
631         if (chdir("../.."))
632                 return 1;
633
634         return res;
635 }
636
637 int conf_write_autoconf(void)
638 {
639         struct symbol *sym;
640         const char *str;
641         char *name;
642         FILE *out, *out_h;
643         time_t now;
644         int i, l;
645
646         sym_clear_all_valid();
647
648         file_write_dep("include/config/auto.conf.cmd");
649
650         if (conf_split_config())
651                 return 1;
652
653         out = fopen(".tmpconfig", "w");
654         if (!out)
655                 return 1;
656
657         out_h = fopen(".tmpconfig.h", "w");
658         if (!out_h) {
659                 fclose(out);
660                 return 1;
661         }
662
663         sym = sym_lookup("KERNELVERSION", 0);
664         sym_calc_value(sym);
665         time(&now);
666         fprintf(out, "#\n"
667                      "# Automatically generated make config: don't edit\n"
668                      "# Linux kernel version: %s\n"
669                      "# %s"
670                      "#\n",
671                      sym_get_string_value(sym), ctime(&now));
672         fprintf(out_h, "/*\n"
673                        " * Automatically generated C config: don't edit\n"
674                        " * Linux kernel version: %s\n"
675                        " * %s"
676                        " */\n"
677                        "#define AUTOCONF_INCLUDED\n",
678                        sym_get_string_value(sym), ctime(&now));
679
680         for_all_symbols(i, sym) {
681                 sym_calc_value(sym);
682                 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
683                         continue;
684                 switch (sym->type) {
685                 case S_BOOLEAN:
686                 case S_TRISTATE:
687                         switch (sym_get_tristate_value(sym)) {
688                         case no:
689                                 break;
690                         case mod:
691                                 fprintf(out, "CONFIG_%s=m\n", sym->name);
692                                 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
693                                 break;
694                         case yes:
695                                 fprintf(out, "CONFIG_%s=y\n", sym->name);
696                                 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
697                                 break;
698                         }
699                         break;
700                 case S_STRING:
701                         str = sym_get_string_value(sym);
702                         fprintf(out, "CONFIG_%s=\"", sym->name);
703                         fprintf(out_h, "#define CONFIG_%s \"", sym->name);
704                         while (1) {
705                                 l = strcspn(str, "\"\\");
706                                 if (l) {
707                                         fwrite(str, l, 1, out);
708                                         fwrite(str, l, 1, out_h);
709                                         str += l;
710                                 }
711                                 if (!*str)
712                                         break;
713                                 fprintf(out, "\\%c", *str);
714                                 fprintf(out_h, "\\%c", *str);
715                                 str++;
716                         }
717                         fputs("\"\n", out);
718                         fputs("\"\n", out_h);
719                         break;
720                 case S_HEX:
721                         str = sym_get_string_value(sym);
722                         if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
723                                 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
724                                 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
725                                 break;
726                         }
727                 case S_INT:
728                         str = sym_get_string_value(sym);
729                         fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
730                         fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
731                         break;
732                 default:
733                         break;
734                 }
735         }
736         fclose(out);
737         fclose(out_h);
738
739         name = getenv("KCONFIG_AUTOHEADER");
740         if (!name)
741                 name = "include/linux/autoconf.h";
742         if (rename(".tmpconfig.h", name))
743                 return 1;
744         name = getenv("KCONFIG_AUTOCONFIG");
745         if (!name)
746                 name = "include/config/auto.conf";
747         /*
748          * This must be the last step, kbuild has a dependency on auto.conf
749          * and this marks the successful completion of the previous steps.
750          */
751         if (rename(".tmpconfig", name))
752                 return 1;
753
754         return 0;
755 }