pkgmaker.c 23 KB

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