conf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <[email protected]>
  4. */
  5. #include <ctype.h>
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. #include <getopt.h>
  13. #include <sys/time.h>
  14. #include <errno.h>
  15. #include "lkc.h"
  16. static void conf(struct menu *menu);
  17. static void check_conf(struct menu *menu);
  18. enum input_mode {
  19. oldaskconfig,
  20. syncconfig,
  21. oldconfig,
  22. allnoconfig,
  23. allyesconfig,
  24. allmodconfig,
  25. alldefconfig,
  26. randconfig,
  27. defconfig,
  28. savedefconfig,
  29. listnewconfig,
  30. helpnewconfig,
  31. olddefconfig,
  32. yes2modconfig,
  33. mod2yesconfig,
  34. mod2noconfig,
  35. fatalrecursive,
  36. };
  37. static enum input_mode input_mode = oldaskconfig;
  38. static int input_mode_opt;
  39. static int indent = 1;
  40. static int tty_stdio;
  41. static int sync_kconfig;
  42. static int conf_cnt;
  43. static char line[PATH_MAX];
  44. static struct menu *rootEntry;
  45. static void print_help(struct menu *menu)
  46. {
  47. struct gstr help = str_new();
  48. menu_get_ext_help(menu, &help);
  49. printf("\n%s\n", str_get(&help));
  50. str_free(&help);
  51. }
  52. static void strip(char *str)
  53. {
  54. char *p = str;
  55. int l;
  56. while ((isspace(*p)))
  57. p++;
  58. l = strlen(p);
  59. if (p != str)
  60. memmove(str, p, l + 1);
  61. if (!l)
  62. return;
  63. p = str + l - 1;
  64. while ((isspace(*p)))
  65. *p-- = 0;
  66. }
  67. /* Helper function to facilitate fgets() by Jean Sacren. */
  68. static void xfgets(char *str, int size, FILE *in)
  69. {
  70. if (!fgets(str, size, in))
  71. fprintf(stderr, "\nError in reading or end of file.\n");
  72. if (!tty_stdio)
  73. printf("%s", str);
  74. }
  75. static void set_randconfig_seed(void)
  76. {
  77. unsigned int seed;
  78. char *env;
  79. bool seed_set = false;
  80. env = getenv("KCONFIG_SEED");
  81. if (env && *env) {
  82. char *endp;
  83. seed = strtol(env, &endp, 0);
  84. if (*endp == '\0')
  85. seed_set = true;
  86. }
  87. if (!seed_set) {
  88. struct timeval now;
  89. /*
  90. * Use microseconds derived seed, compensate for systems where it may
  91. * be zero.
  92. */
  93. gettimeofday(&now, NULL);
  94. seed = (now.tv_sec + 1) * (now.tv_usec + 1);
  95. }
  96. printf("KCONFIG_SEED=0x%X\n", seed);
  97. srand(seed);
  98. }
  99. static bool randomize_choice_values(struct symbol *csym)
  100. {
  101. struct property *prop;
  102. struct symbol *sym;
  103. struct expr *e;
  104. int cnt, def;
  105. /*
  106. * If choice is mod then we may have more items selected
  107. * and if no then no-one.
  108. * In both cases stop.
  109. */
  110. if (csym->curr.tri != yes)
  111. return false;
  112. prop = sym_get_choice_prop(csym);
  113. /* count entries in choice block */
  114. cnt = 0;
  115. expr_list_for_each_sym(prop->expr, e, sym)
  116. cnt++;
  117. /*
  118. * find a random value and set it to yes,
  119. * set the rest to no so we have only one set
  120. */
  121. def = rand() % cnt;
  122. cnt = 0;
  123. expr_list_for_each_sym(prop->expr, e, sym) {
  124. if (def == cnt++) {
  125. sym->def[S_DEF_USER].tri = yes;
  126. csym->def[S_DEF_USER].val = sym;
  127. } else {
  128. sym->def[S_DEF_USER].tri = no;
  129. }
  130. sym->flags |= SYMBOL_DEF_USER;
  131. /* clear VALID to get value calculated */
  132. sym->flags &= ~SYMBOL_VALID;
  133. }
  134. csym->flags |= SYMBOL_DEF_USER;
  135. /* clear VALID to get value calculated */
  136. csym->flags &= ~SYMBOL_VALID;
  137. return true;
  138. }
  139. enum conf_def_mode {
  140. def_default,
  141. def_yes,
  142. def_mod,
  143. def_no,
  144. def_random
  145. };
  146. static bool conf_set_all_new_symbols(enum conf_def_mode mode)
  147. {
  148. struct symbol *sym, *csym;
  149. int i, cnt;
  150. /*
  151. * can't go as the default in switch-case below, otherwise gcc whines
  152. * about -Wmaybe-uninitialized
  153. */
  154. int pby = 50; /* probability of bool = y */
  155. int pty = 33; /* probability of tristate = y */
  156. int ptm = 33; /* probability of tristate = m */
  157. bool has_changed = false;
  158. if (mode == def_random) {
  159. int n, p[3];
  160. char *env = getenv("KCONFIG_PROBABILITY");
  161. n = 0;
  162. while (env && *env) {
  163. char *endp;
  164. int tmp = strtol(env, &endp, 10);
  165. if (tmp >= 0 && tmp <= 100) {
  166. p[n++] = tmp;
  167. } else {
  168. errno = ERANGE;
  169. perror("KCONFIG_PROBABILITY");
  170. exit(1);
  171. }
  172. env = (*endp == ':') ? endp + 1 : endp;
  173. if (n >= 3)
  174. break;
  175. }
  176. switch (n) {
  177. case 1:
  178. pby = p[0];
  179. ptm = pby / 2;
  180. pty = pby - ptm;
  181. break;
  182. case 2:
  183. pty = p[0];
  184. ptm = p[1];
  185. pby = pty + ptm;
  186. break;
  187. case 3:
  188. pby = p[0];
  189. pty = p[1];
  190. ptm = p[2];
  191. break;
  192. }
  193. if (pty + ptm > 100) {
  194. errno = ERANGE;
  195. perror("KCONFIG_PROBABILITY");
  196. exit(1);
  197. }
  198. }
  199. sym_clear_all_valid();
  200. for_all_symbols(i, sym) {
  201. if (sym_has_value(sym) || sym->flags & SYMBOL_VALID)
  202. continue;
  203. switch (sym_get_type(sym)) {
  204. case S_BOOLEAN:
  205. case S_TRISTATE:
  206. has_changed = true;
  207. switch (mode) {
  208. case def_yes:
  209. sym->def[S_DEF_USER].tri = yes;
  210. break;
  211. case def_mod:
  212. sym->def[S_DEF_USER].tri = mod;
  213. break;
  214. case def_no:
  215. sym->def[S_DEF_USER].tri = no;
  216. break;
  217. case def_random:
  218. sym->def[S_DEF_USER].tri = no;
  219. cnt = rand() % 100;
  220. if (sym->type == S_TRISTATE) {
  221. if (cnt < pty)
  222. sym->def[S_DEF_USER].tri = yes;
  223. else if (cnt < pty + ptm)
  224. sym->def[S_DEF_USER].tri = mod;
  225. } else if (cnt < pby)
  226. sym->def[S_DEF_USER].tri = yes;
  227. break;
  228. default:
  229. continue;
  230. }
  231. if (!(sym_is_choice(sym) && mode == def_random))
  232. sym->flags |= SYMBOL_DEF_USER;
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. /*
  239. * We have different type of choice blocks.
  240. * If curr.tri equals to mod then we can select several
  241. * choice symbols in one block.
  242. * In this case we do nothing.
  243. * If curr.tri equals yes then only one symbol can be
  244. * selected in a choice block and we set it to yes,
  245. * and the rest to no.
  246. */
  247. if (mode != def_random) {
  248. for_all_symbols(i, csym) {
  249. if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
  250. sym_is_choice_value(csym))
  251. csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
  252. }
  253. }
  254. for_all_symbols(i, csym) {
  255. if (sym_has_value(csym) || !sym_is_choice(csym))
  256. continue;
  257. sym_calc_value(csym);
  258. if (mode == def_random)
  259. has_changed |= randomize_choice_values(csym);
  260. else {
  261. set_all_choice_values(csym);
  262. has_changed = true;
  263. }
  264. }
  265. return has_changed;
  266. }
  267. static void conf_rewrite_tristates(tristate old_val, tristate new_val)
  268. {
  269. struct symbol *sym;
  270. int i;
  271. for_all_symbols(i, sym) {
  272. if (sym_get_type(sym) == S_TRISTATE &&
  273. sym->def[S_DEF_USER].tri == old_val)
  274. sym->def[S_DEF_USER].tri = new_val;
  275. }
  276. sym_clear_all_valid();
  277. }
  278. static int conf_askvalue(struct symbol *sym, const char *def)
  279. {
  280. if (!sym_has_value(sym))
  281. printf("(NEW) ");
  282. line[0] = '\n';
  283. line[1] = 0;
  284. if (!sym_is_changeable(sym)) {
  285. printf("%s\n", def);
  286. line[0] = '\n';
  287. line[1] = 0;
  288. return 0;
  289. }
  290. switch (input_mode) {
  291. case oldconfig:
  292. case syncconfig:
  293. if (sym_has_value(sym)) {
  294. printf("%s\n", def);
  295. return 0;
  296. }
  297. /* fall through */
  298. default:
  299. fflush(stdout);
  300. xfgets(line, sizeof(line), stdin);
  301. break;
  302. }
  303. return 1;
  304. }
  305. static int conf_string(struct menu *menu)
  306. {
  307. struct symbol *sym = menu->sym;
  308. const char *def;
  309. while (1) {
  310. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  311. printf("(%s) ", sym->name);
  312. def = sym_get_string_value(sym);
  313. if (def)
  314. printf("[%s] ", def);
  315. if (!conf_askvalue(sym, def))
  316. return 0;
  317. switch (line[0]) {
  318. case '\n':
  319. break;
  320. case '?':
  321. /* print help */
  322. if (line[1] == '\n') {
  323. print_help(menu);
  324. def = NULL;
  325. break;
  326. }
  327. /* fall through */
  328. default:
  329. line[strlen(line)-1] = 0;
  330. def = line;
  331. }
  332. if (def && sym_set_string_value(sym, def))
  333. return 0;
  334. }
  335. }
  336. static int conf_sym(struct menu *menu)
  337. {
  338. struct symbol *sym = menu->sym;
  339. tristate oldval, newval;
  340. while (1) {
  341. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  342. if (sym->name)
  343. printf("(%s) ", sym->name);
  344. putchar('[');
  345. oldval = sym_get_tristate_value(sym);
  346. switch (oldval) {
  347. case no:
  348. putchar('N');
  349. break;
  350. case mod:
  351. putchar('M');
  352. break;
  353. case yes:
  354. putchar('Y');
  355. break;
  356. }
  357. if (oldval != no && sym_tristate_within_range(sym, no))
  358. printf("/n");
  359. if (oldval != mod && sym_tristate_within_range(sym, mod))
  360. printf("/m");
  361. if (oldval != yes && sym_tristate_within_range(sym, yes))
  362. printf("/y");
  363. printf("/?] ");
  364. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  365. return 0;
  366. strip(line);
  367. switch (line[0]) {
  368. case 'n':
  369. case 'N':
  370. newval = no;
  371. if (!line[1] || !strcmp(&line[1], "o"))
  372. break;
  373. continue;
  374. case 'm':
  375. case 'M':
  376. newval = mod;
  377. if (!line[1])
  378. break;
  379. continue;
  380. case 'y':
  381. case 'Y':
  382. newval = yes;
  383. if (!line[1] || !strcmp(&line[1], "es"))
  384. break;
  385. continue;
  386. case 0:
  387. newval = oldval;
  388. break;
  389. case '?':
  390. goto help;
  391. default:
  392. continue;
  393. }
  394. if (sym_set_tristate_value(sym, newval))
  395. return 0;
  396. help:
  397. print_help(menu);
  398. }
  399. }
  400. static int conf_choice(struct menu *menu)
  401. {
  402. struct symbol *sym, *def_sym;
  403. struct menu *child;
  404. bool is_new;
  405. sym = menu->sym;
  406. is_new = !sym_has_value(sym);
  407. if (sym_is_changeable(sym)) {
  408. conf_sym(menu);
  409. sym_calc_value(sym);
  410. switch (sym_get_tristate_value(sym)) {
  411. case no:
  412. return 1;
  413. case mod:
  414. return 0;
  415. case yes:
  416. break;
  417. }
  418. } else {
  419. switch (sym_get_tristate_value(sym)) {
  420. case no:
  421. return 1;
  422. case mod:
  423. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  424. return 0;
  425. case yes:
  426. break;
  427. }
  428. }
  429. while (1) {
  430. int cnt, def;
  431. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  432. def_sym = sym_get_choice_value(sym);
  433. cnt = def = 0;
  434. line[0] = 0;
  435. for (child = menu->list; child; child = child->next) {
  436. if (!menu_is_visible(child))
  437. continue;
  438. if (!child->sym) {
  439. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  440. continue;
  441. }
  442. cnt++;
  443. if (child->sym == def_sym) {
  444. def = cnt;
  445. printf("%*c", indent, '>');
  446. } else
  447. printf("%*c", indent, ' ');
  448. printf(" %d. %s", cnt, menu_get_prompt(child));
  449. if (child->sym->name)
  450. printf(" (%s)", child->sym->name);
  451. if (!sym_has_value(child->sym))
  452. printf(" (NEW)");
  453. printf("\n");
  454. }
  455. printf("%*schoice", indent - 1, "");
  456. if (cnt == 1) {
  457. printf("[1]: 1\n");
  458. goto conf_childs;
  459. }
  460. printf("[1-%d?]: ", cnt);
  461. switch (input_mode) {
  462. case oldconfig:
  463. case syncconfig:
  464. if (!is_new) {
  465. cnt = def;
  466. printf("%d\n", cnt);
  467. break;
  468. }
  469. /* fall through */
  470. case oldaskconfig:
  471. fflush(stdout);
  472. xfgets(line, sizeof(line), stdin);
  473. strip(line);
  474. if (line[0] == '?') {
  475. print_help(menu);
  476. continue;
  477. }
  478. if (!line[0])
  479. cnt = def;
  480. else if (isdigit(line[0]))
  481. cnt = atoi(line);
  482. else
  483. continue;
  484. break;
  485. default:
  486. break;
  487. }
  488. conf_childs:
  489. for (child = menu->list; child; child = child->next) {
  490. if (!child->sym || !menu_is_visible(child))
  491. continue;
  492. if (!--cnt)
  493. break;
  494. }
  495. if (!child)
  496. continue;
  497. if (line[0] && line[strlen(line) - 1] == '?') {
  498. print_help(child);
  499. continue;
  500. }
  501. sym_set_tristate_value(child->sym, yes);
  502. for (child = child->list; child; child = child->next) {
  503. indent += 2;
  504. conf(child);
  505. indent -= 2;
  506. }
  507. return 1;
  508. }
  509. }
  510. static void conf(struct menu *menu)
  511. {
  512. struct symbol *sym;
  513. struct property *prop;
  514. struct menu *child;
  515. if (!menu_is_visible(menu))
  516. return;
  517. sym = menu->sym;
  518. prop = menu->prompt;
  519. if (prop) {
  520. const char *prompt;
  521. switch (prop->type) {
  522. case P_MENU:
  523. /*
  524. * Except in oldaskconfig mode, we show only menus that
  525. * contain new symbols.
  526. */
  527. if (input_mode != oldaskconfig && rootEntry != menu) {
  528. check_conf(menu);
  529. return;
  530. }
  531. /* fall through */
  532. case P_COMMENT:
  533. prompt = menu_get_prompt(menu);
  534. if (prompt)
  535. printf("%*c\n%*c %s\n%*c\n",
  536. indent, '*',
  537. indent, '*', prompt,
  538. indent, '*');
  539. default:
  540. ;
  541. }
  542. }
  543. if (!sym)
  544. goto conf_childs;
  545. if (sym_is_choice(sym)) {
  546. conf_choice(menu);
  547. if (sym->curr.tri != mod)
  548. return;
  549. goto conf_childs;
  550. }
  551. switch (sym->type) {
  552. case S_INT:
  553. case S_HEX:
  554. case S_STRING:
  555. conf_string(menu);
  556. break;
  557. default:
  558. conf_sym(menu);
  559. break;
  560. }
  561. conf_childs:
  562. if (sym)
  563. indent += 2;
  564. for (child = menu->list; child; child = child->next)
  565. conf(child);
  566. if (sym)
  567. indent -= 2;
  568. }
  569. static void check_conf(struct menu *menu)
  570. {
  571. struct symbol *sym;
  572. struct menu *child;
  573. if (!menu_is_visible(menu))
  574. return;
  575. sym = menu->sym;
  576. if (sym && !sym_has_value(sym) &&
  577. (sym_is_changeable(sym) ||
  578. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
  579. switch (input_mode) {
  580. case listnewconfig:
  581. if (sym->name)
  582. print_symbol_for_listconfig(sym);
  583. break;
  584. case helpnewconfig:
  585. printf("-----\n");
  586. print_help(menu);
  587. printf("-----\n");
  588. break;
  589. default:
  590. if (!conf_cnt++)
  591. printf("*\n* Restart config...\n*\n");
  592. rootEntry = menu_get_parent_menu(menu);
  593. conf(rootEntry);
  594. break;
  595. }
  596. }
  597. for (child = menu->list; child; child = child->next)
  598. check_conf(child);
  599. }
  600. static const struct option long_opts[] = {
  601. {"help", no_argument, NULL, 'h'},
  602. {"silent", no_argument, NULL, 's'},
  603. {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
  604. {"oldconfig", no_argument, &input_mode_opt, oldconfig},
  605. {"syncconfig", no_argument, &input_mode_opt, syncconfig},
  606. {"defconfig", required_argument, &input_mode_opt, defconfig},
  607. {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
  608. {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
  609. {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
  610. {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
  611. {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
  612. {"randconfig", no_argument, &input_mode_opt, randconfig},
  613. {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
  614. {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
  615. {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
  616. {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
  617. {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
  618. {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig},
  619. {"fatalrecursive",no_argument, &input_mode_opt, fatalrecursive},
  620. {NULL, 0, NULL, 0}
  621. };
  622. static void conf_usage(const char *progname)
  623. {
  624. printf("Usage: %s [options] <kconfig-file>\n", progname);
  625. printf("\n");
  626. printf("Generic options:\n");
  627. printf(" -h, --help Print this message and exit.\n");
  628. printf(" -r <file> Read <file> as input.\n");
  629. printf(" -s, --silent Do not print log.\n");
  630. printf(" -w <file> Write config to <file>.\n");
  631. printf(" --fatalrecursive Treat recursive dependency as error.\n");
  632. printf("\n");
  633. printf("Mode options:\n");
  634. printf(" --listnewconfig List new options\n");
  635. printf(" --helpnewconfig List new options and help text\n");
  636. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  637. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  638. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  639. " include/{generated/,config/}\n");
  640. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  641. printf(" --defconfig <file> New config with default defined in <file>\n");
  642. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  643. printf(" --allnoconfig New config where all options are answered with no\n");
  644. printf(" --allyesconfig New config where all options are answered with yes\n");
  645. printf(" --allmodconfig New config where all options are answered with mod\n");
  646. printf(" --alldefconfig New config with all symbols set to default\n");
  647. printf(" --randconfig New config with random answer to all options\n");
  648. printf(" --yes2modconfig Change answers from yes to mod if possible\n");
  649. printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
  650. printf(" --mod2noconfig Change answers from mod to no if possible\n");
  651. printf(" (If none of the above is given, --oldaskconfig is the default)\n");
  652. }
  653. int main(int ac, char **av)
  654. {
  655. const char *progname = av[0];
  656. int opt;
  657. const char *name, *defconfig_file = NULL /* gcc uninit */;
  658. const char *input_file = NULL, *output_file = NULL;
  659. int no_conf_write = 0;
  660. tty_stdio = isatty(0) && isatty(1);
  661. while ((opt = getopt_long(ac, av, "hr:w:s", long_opts, NULL)) != -1) {
  662. switch (opt) {
  663. case 'h':
  664. conf_usage(progname);
  665. exit(1);
  666. break;
  667. case 'r':
  668. input_file = optarg;
  669. break;
  670. case 's':
  671. conf_set_message_callback(NULL);
  672. break;
  673. case 'w':
  674. output_file = optarg;
  675. break;
  676. case 0:
  677. switch (input_mode_opt) {
  678. case syncconfig:
  679. /*
  680. * syncconfig is invoked during the build stage.
  681. * Suppress distracting
  682. * "configuration written to ..."
  683. */
  684. conf_set_message_callback(NULL);
  685. sync_kconfig = 1;
  686. break;
  687. case defconfig:
  688. case savedefconfig:
  689. defconfig_file = optarg;
  690. break;
  691. case randconfig:
  692. set_randconfig_seed();
  693. break;
  694. case fatalrecursive:
  695. recursive_is_error = 1;
  696. continue;
  697. default:
  698. break;
  699. }
  700. input_mode = input_mode_opt;
  701. default:
  702. break;
  703. }
  704. }
  705. if (ac == optind) {
  706. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  707. conf_usage(progname);
  708. exit(1);
  709. }
  710. conf_parse(av[optind]);
  711. //zconfdump(stdout);
  712. switch (input_mode) {
  713. case defconfig:
  714. if (conf_read(defconfig_file)) {
  715. fprintf(stderr,
  716. "***\n"
  717. "*** Can't find default configuration \"%s\"!\n"
  718. "***\n",
  719. defconfig_file);
  720. exit(1);
  721. }
  722. break;
  723. case savedefconfig:
  724. case syncconfig:
  725. case oldaskconfig:
  726. case oldconfig:
  727. case listnewconfig:
  728. case helpnewconfig:
  729. case olddefconfig:
  730. case yes2modconfig:
  731. case mod2yesconfig:
  732. case mod2noconfig:
  733. case allnoconfig:
  734. case allyesconfig:
  735. case allmodconfig:
  736. case alldefconfig:
  737. case randconfig:
  738. conf_read(input_file);
  739. break;
  740. default:
  741. break;
  742. }
  743. if (sync_kconfig) {
  744. name = getenv("KCONFIG_NOSILENTUPDATE");
  745. if (name && *name) {
  746. if (conf_get_changed()) {
  747. fprintf(stderr,
  748. "\n*** The configuration requires explicit update.\n\n");
  749. return 1;
  750. }
  751. no_conf_write = 1;
  752. }
  753. }
  754. switch (input_mode) {
  755. case allnoconfig:
  756. conf_set_all_new_symbols(def_no);
  757. break;
  758. case allyesconfig:
  759. conf_set_all_new_symbols(def_yes);
  760. break;
  761. case allmodconfig:
  762. conf_set_all_new_symbols(def_mod);
  763. break;
  764. case alldefconfig:
  765. conf_set_all_new_symbols(def_default);
  766. break;
  767. case randconfig:
  768. /* Really nothing to do in this loop */
  769. while (conf_set_all_new_symbols(def_random)) ;
  770. break;
  771. case defconfig:
  772. conf_set_all_new_symbols(def_default);
  773. break;
  774. case savedefconfig:
  775. break;
  776. case yes2modconfig:
  777. conf_rewrite_tristates(yes, mod);
  778. break;
  779. case mod2yesconfig:
  780. conf_rewrite_tristates(mod, yes);
  781. break;
  782. case mod2noconfig:
  783. conf_rewrite_tristates(mod, no);
  784. break;
  785. case oldaskconfig:
  786. rootEntry = &rootmenu;
  787. conf(&rootmenu);
  788. input_mode = oldconfig;
  789. /* fall through */
  790. case oldconfig:
  791. case listnewconfig:
  792. case helpnewconfig:
  793. case syncconfig:
  794. /* Update until a loop caused no more changes */
  795. do {
  796. conf_cnt = 0;
  797. check_conf(&rootmenu);
  798. } while (conf_cnt);
  799. break;
  800. case olddefconfig:
  801. default:
  802. break;
  803. }
  804. if (input_mode == savedefconfig) {
  805. if (conf_write_defconfig(defconfig_file)) {
  806. fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
  807. defconfig_file);
  808. return 1;
  809. }
  810. } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
  811. if ((output_file || !no_conf_write) &&
  812. conf_write(output_file)) {
  813. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  814. exit(1);
  815. }
  816. /*
  817. * Create auto.conf if it does not exist.
  818. * This prevents GNU Make 4.1 or older from emitting
  819. * "include/config/auto.conf: No such file or directory"
  820. * in the top-level Makefile
  821. *
  822. * syncconfig always creates or updates auto.conf because it is
  823. * used during the build.
  824. */
  825. if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
  826. fprintf(stderr,
  827. "\n*** Error during sync of the configuration.\n\n");
  828. return 1;
  829. }
  830. }
  831. return 0;
  832. }