1
0

pkgmaker.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * pkgmaker - create package meta-data for OpenADK buildsystem
  3. *
  4. * Copyright (C) 2010 Waldemar Brodkorb <wbx@openadk.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <ctype.h>
  20. #include <dirent.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include "sortfile.h"
  29. #include "strmap.h"
  30. #define MAXLINE 4096
  31. #define MAXVALUE 168
  32. #define MAXVAR 64
  33. #define MAXPATH 320
  34. #define HASHSZ 32
  35. static int nobinpkgs;
  36. static void fatal_error(const char *message) {
  37. fprintf(stderr, "Fatal error. %s\n", message);
  38. exit(1);
  39. }
  40. static int parse_var_hash(char *buf, const char *varname, StrMap *strmap) {
  41. char *key, *value, *string;
  42. string = strstr(buf, varname);
  43. if (string != NULL) {
  44. string[strlen(string)-1] = '\0';
  45. key = strtok(string, ":=");
  46. value = strtok(NULL, "=\t");
  47. if (value != NULL)
  48. strmap_put(strmap, key, value);
  49. return(0);
  50. }
  51. return(1);
  52. }
  53. static int parse_var(char *buf, const char *varname, char *pvalue, char **result) {
  54. char *pkg_var;
  55. char *key, *value, *string;
  56. char pkg_str[MAXVAR];
  57. if ((pkg_var = malloc(MAXLINE)) != NULL)
  58. memset(pkg_var, 0, MAXLINE);
  59. else {
  60. perror("Can not allocate memory");
  61. exit(EXIT_FAILURE);
  62. }
  63. if (snprintf(pkg_str, MAXVAR, "%s:=", varname) < 0)
  64. perror("can not create path variable.");
  65. string = strstr(buf, pkg_str);
  66. if (string != NULL) {
  67. string[strlen(string)-1] = '\0';
  68. key = strtok(string, ":=");
  69. value = strtok(NULL, "=\t");
  70. if (value != NULL) {
  71. strncat(pkg_var, value, strlen(value));
  72. *result = strdup(pkg_var);
  73. } else {
  74. nobinpkgs = 1;
  75. *result = NULL;
  76. }
  77. free(pkg_var);
  78. return(0);
  79. } else {
  80. if (snprintf(pkg_str, MAXVAR, "%s+=", varname) < 0)
  81. perror("can not create path variable.");
  82. string = strstr(buf, pkg_str);
  83. if (string != NULL) {
  84. string[strlen(string)-1] = '\0';
  85. key = strtok(string, "+=");
  86. value = strtok(NULL, "=\t");
  87. if (pvalue != NULL)
  88. strncat(pkg_var, pvalue, strlen(pvalue));
  89. strncat(pkg_var, " ", 1);
  90. if (value != NULL)
  91. strncat(pkg_var, value, strlen(value));
  92. *result = strdup(pkg_var);
  93. free(pkg_var);
  94. return(0);
  95. }
  96. }
  97. free(pkg_var);
  98. return(1);
  99. }
  100. /*
  101. static void iter_debug(const char *key, const char *value, const void *obj) {
  102. fprintf(stderr, "HASHMAP key: %s value: %s\n", key, value);
  103. }
  104. */
  105. static int hash_str(char *string) {
  106. int i;
  107. int hash;
  108. hash = 0;
  109. for (i=0; i<(int)strlen(string); i++) {
  110. hash += string[i];
  111. }
  112. return(hash);
  113. }
  114. static void iter(const char *key, const char *value, const void *obj) {
  115. FILE *config, *section;
  116. int hash;
  117. char *valuestr, *pkg, *subpkg;
  118. char buf[MAXPATH];
  119. char infile[MAXPATH];
  120. char outfile[MAXPATH];
  121. valuestr = strdup(value);
  122. config = fopen("package/Config.in.auto", "a");
  123. if (config == NULL)
  124. fatal_error("Can not open file package/Config.in.auto");
  125. hash = hash_str(valuestr);
  126. snprintf(infile, MAXPATH, "package/pkglist.d/sectionlst.%d", hash);
  127. snprintf(outfile, MAXPATH, "package/pkglist.d/sectionlst.%d.sorted", hash);
  128. if (access(infile, F_OK) == 0) {
  129. valuestr[strlen(valuestr)-1] = '\0';
  130. fprintf(config, "menu \"%s\"\n", valuestr);
  131. sortfile(infile, outfile);
  132. /* avoid duplicate section entries */
  133. unlink(infile);
  134. section = fopen(outfile, "r");
  135. while (fgets(buf, MAXPATH, section) != NULL) {
  136. buf[strlen(buf)-1] = '\0';
  137. if (buf[strlen(buf)-1] == '@') {
  138. buf[strlen(buf)-1] = '\0';
  139. fprintf(config, "source \"package/%s/Config.in.manual\"\n", buf);
  140. } else {
  141. subpkg = strtok(buf, "|");
  142. subpkg[strlen(subpkg)-1] = '\0';
  143. pkg = strtok(NULL, "|");
  144. fprintf(config, "source \"package/pkgconfigs.d/%s/Config.in.%s\"\n", pkg, subpkg);
  145. }
  146. }
  147. fprintf(config, "endmenu\n\n");
  148. fclose(section);
  149. }
  150. fclose(config);
  151. }
  152. static char *tolowerstr(char *string) {
  153. int i;
  154. char *str;
  155. /* transform to lowercase variable name */
  156. str = strdup(string);
  157. for (i=0; i<(int)strlen(str); i++) {
  158. if (str[i] == '_')
  159. str[i] = '-';
  160. str[i] = tolower(str[i]);
  161. }
  162. return(str);
  163. }
  164. static char *toupperstr(char *string) {
  165. int i;
  166. char *str;
  167. /* transform to uppercase variable name */
  168. str = strdup(string);
  169. for (i=0; i<(int)strlen(str); i++) {
  170. if (str[i] == '+')
  171. str[i] = 'X';
  172. if (str[i] == '-')
  173. str[i] = '_';
  174. /* remove negation here, useful for package host depends */
  175. if (str[i] == '!')
  176. str[i] = '_';
  177. str[i] = toupper(str[i]);
  178. }
  179. return(str);
  180. }
  181. int main() {
  182. DIR *pkgdir, *pkglistdir;
  183. struct dirent *pkgdirp;
  184. FILE *pkg, *cfg, *menuglobal, *section;
  185. char hvalue[MAXVALUE];
  186. char buf[MAXPATH];
  187. char tbuf[MAXPATH];
  188. char path[MAXPATH];
  189. char spath[MAXPATH];
  190. char dir[MAXPATH];
  191. char variable[2*MAXVAR];
  192. char *key, *value, *token, *cftoken, *sp, *hkey, *val, *pkg_fd;
  193. char *pkg_name, *pkg_depends, *pkg_section, *pkg_descr, *pkg_url;
  194. char *pkg_cxx, *pkg_subpkgs, *pkg_cfline, *pkg_dflt, *pkg_multi;
  195. char *pkg_host_depends, *pkg_arch_depends, *pkg_flavours, *pkg_choices, *pseudo_name;
  196. char *packages, *pkg_name_u, *pkgs;
  197. char *saveptr, *p_ptr, *s_ptr;
  198. int result;
  199. StrMap *pkgmap, *sectionmap;
  200. pkg_name = NULL;
  201. pkg_descr = NULL;
  202. pkg_section = NULL;
  203. pkg_url = NULL;
  204. pkg_depends = NULL;
  205. pkg_flavours = NULL;
  206. pkg_choices = NULL;
  207. pkg_subpkgs = NULL;
  208. pkg_arch_depends = NULL;
  209. pkg_host_depends = NULL;
  210. pkg_cxx = NULL;
  211. pkg_dflt = NULL;
  212. pkg_cfline = NULL;
  213. pkg_multi = NULL;
  214. p_ptr = NULL;
  215. s_ptr = NULL;
  216. unlink("package/Config.in.auto");
  217. /* open global sectionfile */
  218. menuglobal = fopen("package/Config.in.auto.global", "w");
  219. if (menuglobal == NULL)
  220. fatal_error("global section file not writable.");
  221. /* read section list and create a hash table */
  222. section = fopen("package/section.lst", "r");
  223. if (section == NULL)
  224. fatal_error("section listfile is missing");
  225. sectionmap = strmap_new(HASHSZ);
  226. while (fgets(tbuf, MAXPATH, section) != NULL) {
  227. key = strtok(tbuf, "\t");
  228. value = strtok(NULL, "\t");
  229. strmap_put(sectionmap, key, value);
  230. }
  231. fclose(section);
  232. if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0)
  233. fatal_error("creation of package/pkgconfigs.d failed.");
  234. if (mkdir("package/pkglist.d", S_IRWXU) > 0)
  235. fatal_error("creation of package/pkglist.d failed.");
  236. /* read Makefile's for all packages */
  237. pkgdir = opendir("package");
  238. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  239. /* skip dotfiles */
  240. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  241. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  242. fatal_error("can not create path variable.");
  243. pkg = fopen(path, "r");
  244. if (pkg == NULL)
  245. continue;
  246. /* skip manually maintained packages */
  247. if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0)
  248. fatal_error("can not create path variable.");
  249. if (!access(path, F_OK)) {
  250. while (fgets(buf, MAXPATH, pkg) != NULL) {
  251. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  252. continue;
  253. }
  254. memset(hvalue, 0 , MAXVALUE);
  255. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  256. if (result == 1) {
  257. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  258. fatal_error("can not create path variable.");
  259. section = fopen(spath, "a");
  260. if (section != NULL) {
  261. fprintf(section, "%s@\n", pkgdirp->d_name);
  262. fclose(section);
  263. }
  264. } else
  265. fatal_error("Can not find section description for package.");
  266. fclose(pkg);
  267. continue;
  268. }
  269. nobinpkgs = 0;
  270. /* create output directories */
  271. if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0)
  272. fatal_error("can not create dir variable.");
  273. if (mkdir(dir, S_IRWXU) > 0)
  274. fatal_error("can not create directory.");
  275. /* allocate memory */
  276. hkey = malloc(MAXVAR);
  277. memset(hkey, 0, MAXVAR);
  278. memset(variable, 0, 2*MAXVAR);
  279. pkgmap = strmap_new(HASHSZ);
  280. /* parse package Makefile */
  281. while (fgets(buf, MAXPATH, pkg) != NULL) {
  282. /* just read variables prefixed with PKG */
  283. if (strncmp(buf, "PKG", 3) == 0) {
  284. if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0)
  285. continue;
  286. if (pkg_name != NULL)
  287. pkg_name_u = toupperstr(pkg_name);
  288. else
  289. pkg_name_u = toupperstr(pkgdirp->d_name);
  290. snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u);
  291. if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0)
  292. continue;
  293. snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u);
  294. if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0)
  295. continue;
  296. if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0)
  297. continue;
  298. if ((parse_var(buf, "PKG_ARCH_DEPENDS", NULL, &pkg_arch_depends)) == 0)
  299. continue;
  300. if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0)
  301. continue;
  302. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  303. continue;
  304. if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0)
  305. continue;
  306. if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0)
  307. continue;
  308. if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0)
  309. continue;
  310. if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0)
  311. continue;
  312. if ((parse_var(buf, "PKG_FLAVOURS", pkg_flavours, &pkg_flavours)) == 0)
  313. continue;
  314. if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0)
  315. continue;
  316. if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0)
  317. continue;
  318. if ((parse_var(buf, "PKG_CHOICES", pkg_choices, &pkg_choices)) == 0)
  319. continue;
  320. if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0)
  321. continue;
  322. if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0)
  323. continue;
  324. if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0)
  325. continue;
  326. if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0)
  327. continue;
  328. if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0)
  329. continue;
  330. if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0)
  331. continue;
  332. }
  333. }
  334. /* end of package Makefile parsing */
  335. if (fclose(pkg) != 0)
  336. perror("Failed to close file stream for Makefile");
  337. /*
  338. if (pkg_name != NULL)
  339. fprintf(stderr, "Package name is %s\n", pkg_name);
  340. if (pkg_section != NULL)
  341. fprintf(stderr, "Package section is %s\n", pkg_section);
  342. if (pkg_descr != NULL)
  343. fprintf(stderr, "Package description is %s\n", pkg_descr);
  344. if (pkg_depends != NULL)
  345. fprintf(stderr, "Package dependencies are %s\n", pkg_depends);
  346. if (pkg_subpkgs != NULL)
  347. fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs);
  348. if (pkg_flavours != NULL)
  349. fprintf(stderr, "Package flavours are %s\n", pkg_flavours);
  350. if (pkg_choices != NULL)
  351. fprintf(stderr, "Package choices are %s\n", pkg_choices);
  352. if (pkg_url != NULL)
  353. fprintf(stderr, "Package homepage is %s\n", pkg_url);
  354. if (pkg_cfline != NULL)
  355. fprintf(stderr, "Package cfline is %s\n", pkg_cfline);
  356. if (pkg_multi != NULL)
  357. fprintf(stderr, "Package multi is %s\n", pkg_multi);
  358. strmap_enum(pkgmap, iter_debug, NULL);
  359. */
  360. /* generate master source Config.in file */
  361. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0)
  362. fatal_error("path variable creation failed.");
  363. fprintf(menuglobal, "source \"%s\"\n", path);
  364. /* recreating file is faster than truncating with w+ */
  365. unlink(path);
  366. cfg = fopen(path, "w");
  367. if (cfg == NULL)
  368. continue;
  369. pkgs = NULL;
  370. if (pkg_subpkgs != NULL)
  371. pkgs = strdup(pkg_subpkgs);
  372. fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  373. fprintf(cfg, "\ttristate\n");
  374. if (nobinpkgs == 0) {
  375. fprintf(cfg, "\tdepends on ");
  376. if (pkgs != NULL) {
  377. if (pkg_multi != NULL)
  378. if (strncmp(pkg_multi, "1", 1) == 0)
  379. fprintf(cfg, "ADK_HAVE_DOT_CONFIG || ");
  380. token = strtok(pkgs, " ");
  381. fprintf(cfg, "ADK_PACKAGE_%s", token);
  382. token = strtok(NULL, " ");
  383. while (token != NULL) {
  384. fprintf(cfg, " || ADK_PACKAGE_%s", token);
  385. token = strtok(NULL, " ");
  386. }
  387. fprintf(cfg, "\n");
  388. } else {
  389. fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  390. }
  391. }
  392. fprintf(cfg, "\tdefault n\n");
  393. fclose(cfg);
  394. free(pkgs);
  395. /* skip packages without binary package output */
  396. if (nobinpkgs == 1)
  397. continue;
  398. /* generate binary package specific Config.in files */
  399. if (pkg_subpkgs != NULL)
  400. packages = tolowerstr(pkg_subpkgs);
  401. else
  402. packages = strdup(pkgdirp->d_name);
  403. token = strtok_r(packages, " ", &p_ptr);
  404. while (token != NULL) {
  405. strncat(hkey, "PKGSC_", 6);
  406. strncat(hkey, toupperstr(token), strlen(token));
  407. memset(hvalue, 0 , MAXVALUE);
  408. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  409. memset(hkey, 0 , MAXVAR);
  410. if (result == 1)
  411. pkg_section = strdup(hvalue);
  412. strncat(hkey, "PKGSD_", 6);
  413. strncat(hkey, toupperstr(token), strlen(token));
  414. memset(hvalue, 0 , MAXVALUE);
  415. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  416. memset(hkey, 0 , MAXVAR);
  417. if (result == 1)
  418. pkg_descr = strdup(hvalue);
  419. pseudo_name = malloc(MAXLINE);
  420. memset(pseudo_name, 0, MAXLINE);
  421. strncat(pseudo_name, token, strlen(token));
  422. while (strlen(pseudo_name) < 20)
  423. strncat(pseudo_name, ".", 1);
  424. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0)
  425. fatal_error("failed to create path variable.");
  426. /* create temporary section files */
  427. memset(hvalue, 0 , MAXVALUE);
  428. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  429. if (result == 1) {
  430. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  431. fatal_error("failed to create path variable.");
  432. section = fopen(spath, "a");
  433. if (section != NULL) {
  434. fprintf(section, "%s |%s\n", token, pkgdirp->d_name);
  435. fclose(section);
  436. }
  437. } else
  438. fatal_error("Can not find section description for package");
  439. unlink(path);
  440. cfg = fopen(path, "w");
  441. if (cfg == NULL)
  442. perror("Can not open Config.in file");
  443. fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token));
  444. fprintf(cfg, "\tprompt \"%s... %s\"\n", pseudo_name, pkg_descr);
  445. fprintf(cfg, "\ttristate\n");
  446. if (pkg_multi != NULL)
  447. if (strncmp(pkg_multi, "1", 1) == 0)
  448. if (strncmp(toupperstr(token), toupperstr(pkgdirp->d_name), strlen(token)) != 0)
  449. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  450. free(pseudo_name);
  451. /* print custom cf line */
  452. if (pkg_cfline != NULL) {
  453. cftoken = strtok_r(pkg_cfline, "@", &saveptr);
  454. while (cftoken != NULL) {
  455. fprintf(cfg, "\t%s\n", cftoken);
  456. cftoken = strtok_r(NULL, "@", &saveptr);
  457. }
  458. }
  459. /* add sub package dependencies */
  460. strncat(hkey, "PKGSS_", 6);
  461. strncat(hkey, toupperstr(token), strlen(token));
  462. memset(hvalue, 0, MAXVALUE);
  463. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  464. if (result == 1) {
  465. val = strtok_r(hvalue, " ", &saveptr);
  466. while (val != NULL) {
  467. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  468. val = strtok_r(NULL, " ", &saveptr);
  469. }
  470. }
  471. memset(hkey, 0, MAXVAR);
  472. /* create package host dependency information */
  473. if (pkg_host_depends != NULL) {
  474. token = strtok(pkg_host_depends, " ");
  475. fprintf(cfg, "\tdepends on ");
  476. sp = "";
  477. while (token != NULL) {
  478. if(strncmp(token, "!", 1) == 0) {
  479. fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token));
  480. sp = " && ";
  481. } else {
  482. fprintf(cfg, "%sADK_HOST_%s", sp, toupperstr(token));
  483. sp = " || ";
  484. }
  485. token = strtok(NULL, " ");
  486. }
  487. fprintf(cfg, "\n");
  488. }
  489. /* create package target architecture dependency information */
  490. if (pkg_arch_depends != NULL) {
  491. token = strtok(pkg_arch_depends, " ");
  492. fprintf(cfg, "\tdepends on ");
  493. sp = "";
  494. while (token != NULL) {
  495. if(strncmp(token, "!", 1) == 0) {
  496. fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token));
  497. sp = " && ";
  498. } else {
  499. fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token));
  500. sp = " || ";
  501. }
  502. token = strtok(NULL, " ");
  503. }
  504. fprintf(cfg, "\n");
  505. }
  506. /* create package dependency information */
  507. if (pkg_depends != NULL) {
  508. token = strtok(pkg_depends, " ");
  509. while (token != NULL) {
  510. if (strncmp(token, "kmod", 4) == 0)
  511. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token));
  512. else
  513. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token));
  514. token = strtok(NULL, " ");
  515. }
  516. free(pkg_depends);
  517. pkg_depends = NULL;
  518. }
  519. fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  520. if (pkg_dflt != NULL) {
  521. fprintf(cfg, "\tdefault %s\n", pkg_dflt);
  522. pkg_dflt = NULL;
  523. } else {
  524. fprintf(cfg, "\tdefault n\n");
  525. }
  526. fprintf(cfg, "\thelp\n");
  527. fprintf(cfg, "\t %s\n\n", pkg_descr);
  528. if (pkg_url != NULL)
  529. fprintf(cfg, "\t WWW: %s\n", pkg_url);
  530. /* handle special C++ packages */
  531. if (pkg_cxx != NULL) {
  532. fprintf(cfg, "\nchoice\n");
  533. fprintf(cfg, "prompt \"C++ library to use\"\n");
  534. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  535. fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC || ADK_TARGET_LIB_EGLIBC\n", pkg_cxx);
  536. fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx);
  537. fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx);
  538. fprintf(cfg, "\tbool \"GNU C++ library\"\n");
  539. fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n");
  540. fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx);
  541. fprintf(cfg, "\tbool \"uClibc++ library\"\n");
  542. fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n");
  543. fprintf(cfg, "endchoice\n");
  544. free(pkg_cxx);
  545. pkg_cxx = NULL;
  546. }
  547. /* package flavours */
  548. if (pkg_flavours != NULL) {
  549. token = strtok(pkg_flavours, " ");
  550. while (token != NULL) {
  551. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name),
  552. toupperstr(token));
  553. fprintf(cfg, "\tbool ");
  554. strncat(hkey, "PKGFD_", 6);
  555. strncat(hkey, token, strlen(token));
  556. memset(hvalue, 0 , MAXVALUE);
  557. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  558. memset(hkey, 0 , MAXVAR);
  559. pkg_fd = strdup(hvalue);
  560. fprintf(cfg, "\"%s\"\n", pkg_fd);
  561. fprintf(cfg, "\tdefault n\n");
  562. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  563. strncat(hkey, "PKGFS_", 6);
  564. strncat(hkey, token, strlen(token));
  565. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  566. if (result == 1) {
  567. val = strtok_r(hvalue, " ", &saveptr);
  568. while (val != NULL) {
  569. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  570. val = strtok_r(NULL, " ", &saveptr);
  571. }
  572. }
  573. memset(hkey, 0, MAXVAR);
  574. fprintf(cfg, "\thelp\n");
  575. fprintf(cfg, "\t %s\n", pkg_fd);
  576. token = strtok(NULL, " ");
  577. }
  578. free(pkg_flavours);
  579. pkg_flavours = NULL;
  580. }
  581. /* package choices */
  582. if (pkg_choices != NULL) {
  583. fprintf(cfg, "\nchoice\n");
  584. fprintf(cfg, "prompt \"Package flavour choice\"\n");
  585. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  586. token = strtok(pkg_choices, " ");
  587. while (token != NULL) {
  588. fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name),
  589. toupperstr(token));
  590. fprintf(cfg, "\tbool ");
  591. strncat(hkey, "PKGCD_", 6);
  592. strncat(hkey, token, strlen(token));
  593. memset(hvalue, 0 , MAXVALUE);
  594. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  595. memset(hkey, 0 , MAXVAR);
  596. fprintf(cfg, "\"%s\"\n", hvalue);
  597. strncat(hkey, "PKGCS_", 6);
  598. strncat(hkey, token, strlen(token));
  599. memset(hvalue, 0, MAXVALUE);
  600. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  601. if (result == 1) {
  602. val = strtok_r(hvalue, " ", &saveptr);
  603. while (val != NULL) {
  604. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  605. val = strtok_r(NULL, " ", &saveptr);
  606. }
  607. }
  608. memset(hkey, 0, MAXVAR);
  609. token = strtok(NULL, " ");
  610. }
  611. fprintf(cfg, "\nendchoice\n");
  612. free(pkg_choices);
  613. pkg_choices = NULL;
  614. }
  615. /* close file descriptor, parse next package */
  616. fclose(cfg);
  617. token = strtok_r(NULL, " ", &p_ptr);
  618. }
  619. /* end of package output generation */
  620. free(packages);
  621. packages = NULL;
  622. /* reset flags, free memory */
  623. free(pkg_name);
  624. free(pkg_descr);
  625. free(pkg_section);
  626. free(pkg_url);
  627. free(pkg_depends);
  628. free(pkg_flavours);
  629. free(pkg_choices);
  630. free(pkg_subpkgs);
  631. free(pkg_arch_depends);
  632. free(pkg_host_depends);
  633. free(pkg_cxx);
  634. free(pkg_dflt);
  635. free(pkg_cfline);
  636. free(pkg_multi);
  637. pkg_name = NULL;
  638. pkg_descr = NULL;
  639. pkg_section = NULL;
  640. pkg_url = NULL;
  641. pkg_depends = NULL;
  642. pkg_flavours = NULL;
  643. pkg_choices = NULL;
  644. pkg_subpkgs = NULL;
  645. pkg_arch_depends = NULL;
  646. pkg_host_depends = NULL;
  647. pkg_cxx = NULL;
  648. pkg_dflt = NULL;
  649. pkg_cfline = NULL;
  650. pkg_multi = NULL;
  651. strmap_delete(pkgmap);
  652. nobinpkgs = 0;
  653. free(hkey);
  654. }
  655. }
  656. /* create Config.in.auto */
  657. strmap_enum(sectionmap, iter, NULL);
  658. strmap_delete(sectionmap);
  659. fclose(menuglobal);
  660. closedir(pkgdir);
  661. /* remove temporary section files */
  662. pkglistdir = opendir("package/pkglist.d");
  663. while ((pkgdirp = readdir(pkglistdir)) != NULL) {
  664. if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) {
  665. if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0)
  666. fatal_error("creating path variable failed.");
  667. if (unlink(path) < 0)
  668. fatal_error("removing file failed.");
  669. }
  670. }
  671. closedir(pkglistdir);
  672. return(0);
  673. }