pkgmaker.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * pkgmaker - create package meta-data for OpenADK buildsystem
  3. *
  4. * Copyright (C) 2010,2011 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_need_cxx, *pkg_need_java;
  196. char *pkg_host_depends, *pkg_arch_depends, *pkg_flavours, *pkg_choices, *pseudo_name;
  197. char *packages, *pkg_name_u, *pkgs;
  198. char *saveptr, *p_ptr, *s_ptr;
  199. int result;
  200. StrMap *pkgmap, *sectionmap;
  201. pkg_name = NULL;
  202. pkg_descr = NULL;
  203. pkg_section = NULL;
  204. pkg_url = NULL;
  205. pkg_depends = NULL;
  206. pkg_flavours = NULL;
  207. pkg_choices = NULL;
  208. pkg_subpkgs = NULL;
  209. pkg_arch_depends = NULL;
  210. pkg_host_depends = NULL;
  211. pkg_cxx = NULL;
  212. pkg_dflt = NULL;
  213. pkg_cfline = NULL;
  214. pkg_multi = NULL;
  215. pkg_need_cxx = NULL;
  216. pkg_need_java = NULL;
  217. p_ptr = NULL;
  218. s_ptr = NULL;
  219. unlink("package/Config.in.auto");
  220. /* open global sectionfile */
  221. menuglobal = fopen("package/Config.in.auto.global", "w");
  222. if (menuglobal == NULL)
  223. fatal_error("global section file not writable.");
  224. /* read section list and create a hash table */
  225. section = fopen("package/section.lst", "r");
  226. if (section == NULL)
  227. fatal_error("section listfile is missing");
  228. sectionmap = strmap_new(HASHSZ);
  229. while (fgets(tbuf, MAXPATH, section) != NULL) {
  230. key = strtok(tbuf, "\t");
  231. value = strtok(NULL, "\t");
  232. strmap_put(sectionmap, key, value);
  233. }
  234. fclose(section);
  235. if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0)
  236. fatal_error("creation of package/pkgconfigs.d failed.");
  237. if (mkdir("package/pkglist.d", S_IRWXU) > 0)
  238. fatal_error("creation of package/pkglist.d failed.");
  239. /* read Makefile's for all packages */
  240. pkgdir = opendir("package");
  241. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  242. /* skip dotfiles */
  243. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  244. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  245. fatal_error("can not create path variable.");
  246. pkg = fopen(path, "r");
  247. if (pkg == NULL)
  248. continue;
  249. /* skip manually maintained packages */
  250. if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0)
  251. fatal_error("can not create path variable.");
  252. if (!access(path, F_OK)) {
  253. while (fgets(buf, MAXPATH, pkg) != NULL) {
  254. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  255. continue;
  256. }
  257. memset(hvalue, 0 , MAXVALUE);
  258. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  259. if (result == 1) {
  260. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  261. fatal_error("can not create path variable.");
  262. section = fopen(spath, "a");
  263. if (section != NULL) {
  264. fprintf(section, "%s@\n", pkgdirp->d_name);
  265. fclose(section);
  266. }
  267. } else
  268. fatal_error("Can not find section description for package.");
  269. fclose(pkg);
  270. continue;
  271. }
  272. nobinpkgs = 0;
  273. /* create output directories */
  274. if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0)
  275. fatal_error("can not create dir variable.");
  276. if (mkdir(dir, S_IRWXU) > 0)
  277. fatal_error("can not create directory.");
  278. /* allocate memory */
  279. hkey = malloc(MAXVAR);
  280. memset(hkey, 0, MAXVAR);
  281. memset(variable, 0, 2*MAXVAR);
  282. pkgmap = strmap_new(HASHSZ);
  283. /* parse package Makefile */
  284. while (fgets(buf, MAXPATH, pkg) != NULL) {
  285. /* just read variables prefixed with PKG */
  286. if (strncmp(buf, "PKG", 3) == 0) {
  287. if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0)
  288. continue;
  289. if (pkg_name != NULL)
  290. pkg_name_u = toupperstr(pkg_name);
  291. else
  292. pkg_name_u = toupperstr(pkgdirp->d_name);
  293. snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u);
  294. if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0)
  295. continue;
  296. snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u);
  297. if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0)
  298. continue;
  299. if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0)
  300. continue;
  301. if ((parse_var(buf, "PKG_ARCH_DEPENDS", NULL, &pkg_arch_depends)) == 0)
  302. continue;
  303. if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0)
  304. continue;
  305. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  306. continue;
  307. if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0)
  308. continue;
  309. if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0)
  310. continue;
  311. if ((parse_var(buf, "PKG_NEED_CXX", NULL, &pkg_need_cxx)) == 0)
  312. continue;
  313. if ((parse_var(buf, "PKG_NEED_JAVA", NULL, &pkg_need_java)) == 0)
  314. continue;
  315. if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0)
  316. continue;
  317. if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0)
  318. continue;
  319. if ((parse_var(buf, "PKG_FLAVOURS", pkg_flavours, &pkg_flavours)) == 0)
  320. continue;
  321. if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0)
  322. continue;
  323. if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0)
  324. continue;
  325. if ((parse_var(buf, "PKG_CHOICES", pkg_choices, &pkg_choices)) == 0)
  326. continue;
  327. if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0)
  328. continue;
  329. if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0)
  330. continue;
  331. if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0)
  332. continue;
  333. if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0)
  334. continue;
  335. if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0)
  336. continue;
  337. if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0)
  338. continue;
  339. }
  340. }
  341. /* end of package Makefile parsing */
  342. if (fclose(pkg) != 0)
  343. perror("Failed to close file stream for Makefile");
  344. /*
  345. if (pkg_name != NULL)
  346. fprintf(stderr, "Package name is %s\n", pkg_name);
  347. if (pkg_section != NULL)
  348. fprintf(stderr, "Package section is %s\n", pkg_section);
  349. if (pkg_descr != NULL)
  350. fprintf(stderr, "Package description is %s\n", pkg_descr);
  351. if (pkg_depends != NULL)
  352. fprintf(stderr, "Package dependencies are %s\n", pkg_depends);
  353. if (pkg_subpkgs != NULL)
  354. fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs);
  355. if (pkg_flavours != NULL)
  356. fprintf(stderr, "Package flavours are %s\n", pkg_flavours);
  357. if (pkg_choices != NULL)
  358. fprintf(stderr, "Package choices are %s\n", pkg_choices);
  359. if (pkg_url != NULL)
  360. fprintf(stderr, "Package homepage is %s\n", pkg_url);
  361. if (pkg_cfline != NULL)
  362. fprintf(stderr, "Package cfline is %s\n", pkg_cfline);
  363. if (pkg_multi != NULL)
  364. fprintf(stderr, "Package multi is %s\n", pkg_multi);
  365. strmap_enum(pkgmap, iter_debug, NULL);
  366. */
  367. /* generate master source Config.in file */
  368. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0)
  369. fatal_error("path variable creation failed.");
  370. fprintf(menuglobal, "source \"%s\"\n", path);
  371. /* recreating file is faster than truncating with w+ */
  372. unlink(path);
  373. cfg = fopen(path, "w");
  374. if (cfg == NULL)
  375. continue;
  376. pkgs = NULL;
  377. if (pkg_subpkgs != NULL)
  378. pkgs = strdup(pkg_subpkgs);
  379. fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  380. fprintf(cfg, "\ttristate\n");
  381. if (nobinpkgs == 0) {
  382. fprintf(cfg, "\tdepends on ");
  383. if (pkgs != NULL) {
  384. if (pkg_multi != NULL)
  385. if (strncmp(pkg_multi, "1", 1) == 0)
  386. fprintf(cfg, "ADK_HAVE_DOT_CONFIG || ");
  387. token = strtok(pkgs, " ");
  388. fprintf(cfg, "ADK_PACKAGE_%s", token);
  389. token = strtok(NULL, " ");
  390. while (token != NULL) {
  391. fprintf(cfg, " || ADK_PACKAGE_%s", token);
  392. token = strtok(NULL, " ");
  393. }
  394. fprintf(cfg, "\n");
  395. } else {
  396. fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  397. }
  398. }
  399. fprintf(cfg, "\tdefault n\n");
  400. fclose(cfg);
  401. free(pkgs);
  402. /* skip packages without binary package output */
  403. if (nobinpkgs == 1)
  404. continue;
  405. /* generate binary package specific Config.in files */
  406. if (pkg_subpkgs != NULL)
  407. packages = tolowerstr(pkg_subpkgs);
  408. else
  409. packages = strdup(pkgdirp->d_name);
  410. token = strtok_r(packages, " ", &p_ptr);
  411. while (token != NULL) {
  412. strncat(hkey, "PKGSC_", 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_section = strdup(hvalue);
  419. strncat(hkey, "PKGSD_", 6);
  420. strncat(hkey, toupperstr(token), strlen(token));
  421. memset(hvalue, 0 , MAXVALUE);
  422. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  423. memset(hkey, 0 , MAXVAR);
  424. if (result == 1)
  425. pkg_descr = strdup(hvalue);
  426. pseudo_name = malloc(MAXLINE);
  427. memset(pseudo_name, 0, MAXLINE);
  428. strncat(pseudo_name, token, strlen(token));
  429. while (strlen(pseudo_name) < 20)
  430. strncat(pseudo_name, ".", 1);
  431. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0)
  432. fatal_error("failed to create path variable.");
  433. /* create temporary section files */
  434. memset(hvalue, 0 , MAXVALUE);
  435. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  436. if (result == 1) {
  437. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  438. fatal_error("failed to create path variable.");
  439. section = fopen(spath, "a");
  440. if (section != NULL) {
  441. fprintf(section, "%s |%s\n", token, pkgdirp->d_name);
  442. fclose(section);
  443. }
  444. } else
  445. fatal_error("Can not find section description for package");
  446. unlink(path);
  447. cfg = fopen(path, "w");
  448. if (cfg == NULL)
  449. perror("Can not open Config.in file");
  450. fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token));
  451. fprintf(cfg, "\tprompt \"%s... %s\"\n", pseudo_name, pkg_descr);
  452. fprintf(cfg, "\ttristate\n");
  453. if (pkg_multi != NULL)
  454. if (strncmp(pkg_multi, "1", 1) == 0)
  455. if (strncmp(toupperstr(token), toupperstr(pkgdirp->d_name), strlen(token)) != 0)
  456. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  457. free(pseudo_name);
  458. /* print custom cf line */
  459. if (pkg_cfline != NULL) {
  460. cftoken = strtok_r(pkg_cfline, "@", &saveptr);
  461. while (cftoken != NULL) {
  462. fprintf(cfg, "\t%s\n", cftoken);
  463. cftoken = strtok_r(NULL, "@", &saveptr);
  464. }
  465. }
  466. /* add sub package dependencies */
  467. strncat(hkey, "PKGSS_", 6);
  468. strncat(hkey, toupperstr(token), strlen(token));
  469. memset(hvalue, 0, MAXVALUE);
  470. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  471. if (result == 1) {
  472. val = strtok_r(hvalue, " ", &saveptr);
  473. while (val != NULL) {
  474. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  475. val = strtok_r(NULL, " ", &saveptr);
  476. }
  477. }
  478. memset(hkey, 0, MAXVAR);
  479. /* create package host dependency information */
  480. if (pkg_host_depends != NULL) {
  481. token = strtok(pkg_host_depends, " ");
  482. fprintf(cfg, "\tdepends on ");
  483. sp = "";
  484. while (token != NULL) {
  485. if(strncmp(token, "!", 1) == 0) {
  486. fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token));
  487. sp = " && ";
  488. } else {
  489. fprintf(cfg, "%sADK_HOST_%s", sp, toupperstr(token));
  490. sp = " || ";
  491. }
  492. token = strtok(NULL, " ");
  493. }
  494. fprintf(cfg, "\n");
  495. }
  496. /* create package target architecture dependency information */
  497. if (pkg_arch_depends != NULL) {
  498. token = strtok(pkg_arch_depends, " ");
  499. fprintf(cfg, "\tdepends on ");
  500. sp = "";
  501. while (token != NULL) {
  502. if(strncmp(token, "!", 1) == 0) {
  503. fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token));
  504. sp = " && ";
  505. } else {
  506. fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token));
  507. sp = " || ";
  508. }
  509. token = strtok(NULL, " ");
  510. }
  511. fprintf(cfg, "\n");
  512. }
  513. /* create package dependency information */
  514. if (pkg_depends != NULL) {
  515. token = strtok(pkg_depends, " ");
  516. while (token != NULL) {
  517. if (strncmp(token, "kmod", 4) == 0)
  518. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token));
  519. else
  520. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token));
  521. token = strtok(NULL, " ");
  522. }
  523. free(pkg_depends);
  524. pkg_depends = NULL;
  525. }
  526. if (pkg_need_cxx != NULL) {
  527. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_CXX");
  528. }
  529. if (pkg_need_java != NULL) {
  530. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_JAVA");
  531. }
  532. fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  533. if (pkg_dflt != NULL) {
  534. fprintf(cfg, "\tdefault %s\n", pkg_dflt);
  535. pkg_dflt = NULL;
  536. } else {
  537. fprintf(cfg, "\tdefault n\n");
  538. }
  539. fprintf(cfg, "\thelp\n");
  540. fprintf(cfg, "\t %s\n\n", pkg_descr);
  541. if (pkg_url != NULL)
  542. fprintf(cfg, "\t WWW: %s\n", pkg_url);
  543. /* handle special C++ packages */
  544. if (pkg_cxx != NULL) {
  545. fprintf(cfg, "\nchoice\n");
  546. fprintf(cfg, "prompt \"C++ library to use\"\n");
  547. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  548. fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC || ADK_TARGET_LIB_EGLIBC\n", pkg_cxx);
  549. fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx);
  550. fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx);
  551. fprintf(cfg, "\tbool \"GNU C++ library\"\n");
  552. fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n");
  553. fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx);
  554. fprintf(cfg, "\tbool \"uClibc++ library\"\n");
  555. fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n");
  556. fprintf(cfg, "endchoice\n");
  557. free(pkg_cxx);
  558. pkg_cxx = NULL;
  559. }
  560. /* package flavours */
  561. if (pkg_flavours != NULL) {
  562. token = strtok(pkg_flavours, " ");
  563. while (token != NULL) {
  564. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name),
  565. toupperstr(token));
  566. fprintf(cfg, "\tbool ");
  567. strncat(hkey, "PKGFD_", 6);
  568. strncat(hkey, token, strlen(token));
  569. memset(hvalue, 0 , MAXVALUE);
  570. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  571. memset(hkey, 0 , MAXVAR);
  572. pkg_fd = strdup(hvalue);
  573. fprintf(cfg, "\"%s\"\n", pkg_fd);
  574. fprintf(cfg, "\tdefault n\n");
  575. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  576. strncat(hkey, "PKGFS_", 6);
  577. strncat(hkey, token, strlen(token));
  578. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  579. if (result == 1) {
  580. val = strtok_r(hvalue, " ", &saveptr);
  581. while (val != NULL) {
  582. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  583. val = strtok_r(NULL, " ", &saveptr);
  584. }
  585. }
  586. memset(hkey, 0, MAXVAR);
  587. fprintf(cfg, "\thelp\n");
  588. fprintf(cfg, "\t %s\n", pkg_fd);
  589. token = strtok(NULL, " ");
  590. }
  591. free(pkg_flavours);
  592. pkg_flavours = NULL;
  593. }
  594. /* package choices */
  595. if (pkg_choices != NULL) {
  596. fprintf(cfg, "\nchoice\n");
  597. fprintf(cfg, "prompt \"Package flavour choice\"\n");
  598. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  599. token = strtok(pkg_choices, " ");
  600. while (token != NULL) {
  601. fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", toupperstr(pkgdirp->d_name),
  602. toupperstr(token));
  603. fprintf(cfg, "\tbool ");
  604. strncat(hkey, "PKGCD_", 6);
  605. strncat(hkey, token, strlen(token));
  606. memset(hvalue, 0 , MAXVALUE);
  607. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  608. memset(hkey, 0 , MAXVAR);
  609. fprintf(cfg, "\"%s\"\n", hvalue);
  610. strncat(hkey, "PKGCS_", 6);
  611. strncat(hkey, token, strlen(token));
  612. memset(hvalue, 0, MAXVALUE);
  613. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  614. if (result == 1) {
  615. val = strtok_r(hvalue, " ", &saveptr);
  616. while (val != NULL) {
  617. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  618. val = strtok_r(NULL, " ", &saveptr);
  619. }
  620. }
  621. memset(hkey, 0, MAXVAR);
  622. token = strtok(NULL, " ");
  623. }
  624. fprintf(cfg, "\nendchoice\n");
  625. free(pkg_choices);
  626. pkg_choices = NULL;
  627. }
  628. /* close file descriptor, parse next package */
  629. fclose(cfg);
  630. token = strtok_r(NULL, " ", &p_ptr);
  631. }
  632. /* end of package output generation */
  633. free(packages);
  634. packages = NULL;
  635. /* reset flags, free memory */
  636. free(pkg_name);
  637. free(pkg_descr);
  638. free(pkg_section);
  639. free(pkg_url);
  640. free(pkg_depends);
  641. free(pkg_flavours);
  642. free(pkg_choices);
  643. free(pkg_subpkgs);
  644. free(pkg_arch_depends);
  645. free(pkg_host_depends);
  646. free(pkg_cxx);
  647. free(pkg_need_cxx);
  648. free(pkg_need_java);
  649. free(pkg_dflt);
  650. free(pkg_cfline);
  651. free(pkg_multi);
  652. pkg_name = NULL;
  653. pkg_descr = NULL;
  654. pkg_section = NULL;
  655. pkg_url = NULL;
  656. pkg_depends = NULL;
  657. pkg_flavours = NULL;
  658. pkg_choices = NULL;
  659. pkg_subpkgs = NULL;
  660. pkg_arch_depends = NULL;
  661. pkg_host_depends = NULL;
  662. pkg_cxx = NULL;
  663. pkg_dflt = NULL;
  664. pkg_cfline = NULL;
  665. pkg_multi = NULL;
  666. strmap_delete(pkgmap);
  667. nobinpkgs = 0;
  668. free(hkey);
  669. }
  670. }
  671. /* create Config.in.auto */
  672. strmap_enum(sectionmap, iter, NULL);
  673. strmap_delete(sectionmap);
  674. fclose(menuglobal);
  675. closedir(pkgdir);
  676. /* remove temporary section files */
  677. pkglistdir = opendir("package/pkglist.d");
  678. while ((pkgdirp = readdir(pkglistdir)) != NULL) {
  679. if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) {
  680. if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0)
  681. fatal_error("creating path variable failed.");
  682. if (unlink(path) < 0)
  683. fatal_error("removing file failed.");
  684. }
  685. }
  686. closedir(pkglistdir);
  687. return(0);
  688. }