pkgmaker.c 23 KB

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