pkgmaker.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. #define fatal_error(...) { \
  37. fprintf(stderr, "Fatal error. "); \
  38. fprintf(stderr, __VA_ARGS__); \
  39. fprintf(stderr, "\n"); \
  40. exit(1); \
  41. }
  42. static int parse_var_hash(char *buf, const char *varname, StrMap *strmap) {
  43. char *key, *value, *string;
  44. string = strstr(buf, varname);
  45. if (string != NULL) {
  46. string[strlen(string)-1] = '\0';
  47. key = strtok(string, ":=");
  48. value = strtok(NULL, "=\t");
  49. if (value != NULL)
  50. strmap_put(strmap, key, value);
  51. return(0);
  52. }
  53. return(1);
  54. }
  55. static int parse_var(char *buf, const char *varname, char *pvalue, char **result) {
  56. char *pkg_var;
  57. char *key, *value, *string;
  58. char pkg_str[MAXVAR];
  59. if ((pkg_var = malloc(MAXLINE)) != NULL)
  60. memset(pkg_var, 0, MAXLINE);
  61. else {
  62. perror("Can not allocate memory");
  63. exit(EXIT_FAILURE);
  64. }
  65. if (snprintf(pkg_str, MAXVAR, "%s:=", varname) < 0)
  66. perror("can not create path variable.");
  67. string = strstr(buf, pkg_str);
  68. if (string != NULL) {
  69. string[strlen(string)-1] = '\0';
  70. key = strtok(string, ":=");
  71. value = strtok(NULL, "=\t");
  72. if (value != NULL) {
  73. strncat(pkg_var, value, strlen(value));
  74. *result = strdup(pkg_var);
  75. } else {
  76. nobinpkgs = 1;
  77. *result = NULL;
  78. }
  79. free(pkg_var);
  80. return(0);
  81. } else {
  82. if (snprintf(pkg_str, MAXVAR, "%s+=", varname) < 0)
  83. perror("can not create path variable.");
  84. string = strstr(buf, pkg_str);
  85. if (string != NULL) {
  86. string[strlen(string)-1] = '\0';
  87. key = strtok(string, "+=");
  88. value = strtok(NULL, "=\t");
  89. if (pvalue != NULL)
  90. strncat(pkg_var, pvalue, strlen(pvalue));
  91. strncat(pkg_var, " ", 1);
  92. if (value != NULL)
  93. strncat(pkg_var, value, strlen(value));
  94. *result = strdup(pkg_var);
  95. free(pkg_var);
  96. return(0);
  97. }
  98. }
  99. free(pkg_var);
  100. return(1);
  101. }
  102. static int parse_var_with_pkg(char *buf, const char *varname, char *pvalue, char **result, char **pkgname, int varlen) {
  103. char *pkg_var, *check;
  104. char *key, *value, *string;
  105. if ((pkg_var = malloc(MAXLINE)) != NULL)
  106. memset(pkg_var, 0, MAXLINE);
  107. else {
  108. perror("Can not allocate memory");
  109. exit(EXIT_FAILURE);
  110. }
  111. check = strstr(buf, ":=");
  112. if (check != NULL) {
  113. string = strstr(buf, varname);
  114. if (string != NULL) {
  115. string[strlen(string)-1] = '\0';
  116. key = strtok(string, ":=");
  117. *pkgname = strdup(key+varlen);
  118. value = strtok(NULL, "=\t");
  119. if (value != NULL) {
  120. strncat(pkg_var, value, strlen(value));
  121. *result = strdup(pkg_var);
  122. }
  123. free(pkg_var);
  124. return(0);
  125. }
  126. } else {
  127. string = strstr(buf, varname);
  128. if (string != NULL) {
  129. string[strlen(string)-1] = '\0';
  130. key = strtok(string, "+=");
  131. value = strtok(NULL, "=\t");
  132. if (pvalue != NULL)
  133. strncat(pkg_var, pvalue, strlen(pvalue));
  134. strncat(pkg_var, " ", 1);
  135. if (value != NULL)
  136. strncat(pkg_var, value, strlen(value));
  137. *result = strdup(pkg_var);
  138. free(pkg_var);
  139. return(0);
  140. }
  141. }
  142. free(pkg_var);
  143. return(1);
  144. }
  145. /*
  146. static void iter_debug(const char *key, const char *value, const void *obj) {
  147. fprintf(stderr, "HASHMAP key: %s value: %s\n", key, value);
  148. }
  149. */
  150. static int hash_str(char *string) {
  151. int i;
  152. int hash;
  153. hash = 0;
  154. for (i=0; i<(int)strlen(string); i++) {
  155. hash += string[i];
  156. }
  157. return(hash);
  158. }
  159. static void iter(const char *key, const char *value, const void *obj) {
  160. FILE *config, *section;
  161. int hash;
  162. char *valuestr, *pkg, *subpkg;
  163. char buf[MAXPATH];
  164. char infile[MAXPATH];
  165. char outfile[MAXPATH];
  166. valuestr = strdup(value);
  167. config = fopen("package/Config.in.auto", "a");
  168. if (config == NULL)
  169. fatal_error("Can not open file package/Config.in.auto");
  170. hash = hash_str(valuestr);
  171. snprintf(infile, MAXPATH, "package/pkglist.d/sectionlst.%d", hash);
  172. snprintf(outfile, MAXPATH, "package/pkglist.d/sectionlst.%d.sorted", hash);
  173. if (access(infile, F_OK) == 0) {
  174. valuestr[strlen(valuestr)-1] = '\0';
  175. fprintf(config, "menu \"%s\"\n", valuestr);
  176. sortfile(infile, outfile);
  177. /* avoid duplicate section entries */
  178. unlink(infile);
  179. section = fopen(outfile, "r");
  180. while (fgets(buf, MAXPATH, section) != NULL) {
  181. buf[strlen(buf)-1] = '\0';
  182. if (buf[strlen(buf)-1] == '@') {
  183. buf[strlen(buf)-1] = '\0';
  184. fprintf(config, "source \"package/%s/Config.in.manual\"\n", buf);
  185. } else {
  186. subpkg = strtok(buf, "|");
  187. subpkg[strlen(subpkg)-1] = '\0';
  188. pkg = strtok(NULL, "|");
  189. fprintf(config, "source \"package/pkgconfigs.d/%s/Config.in.%s\"\n", pkg, subpkg);
  190. }
  191. }
  192. fprintf(config, "endmenu\n\n");
  193. fclose(section);
  194. }
  195. fclose(config);
  196. }
  197. static char *tolowerstr(char *string) {
  198. int i;
  199. char *str;
  200. /* transform to lowercase variable name */
  201. str = strdup(string);
  202. for (i=0; i<(int)strlen(str); i++) {
  203. if (str[i] == '_')
  204. str[i] = '-';
  205. str[i] = tolower(str[i]);
  206. }
  207. return(str);
  208. }
  209. static char *toupperstr(char *string) {
  210. int i;
  211. char *str;
  212. /* transform to uppercase variable name */
  213. str = strdup(string);
  214. for (i=0; i<(int)strlen(str); i++) {
  215. if (str[i] == '+')
  216. str[i] = 'X';
  217. if (str[i] == '-')
  218. str[i] = '_';
  219. /* remove negation here, useful for package host depends */
  220. if (str[i] == '!')
  221. str[i] = '_';
  222. str[i] = toupper(str[i]);
  223. }
  224. return(str);
  225. }
  226. int main() {
  227. DIR *pkgdir, *pkglistdir;
  228. struct dirent *pkgdirp;
  229. FILE *pkg, *cfg, *menuglobal, *section;
  230. char hvalue[MAXVALUE];
  231. char buf[MAXPATH];
  232. char tbuf[MAXPATH];
  233. char path[MAXPATH];
  234. char spath[MAXPATH];
  235. char dir[MAXPATH];
  236. char variable[2*MAXVAR];
  237. char *key, *value, *token, *cftoken, *sp, *hkey, *val, *pkg_fd;
  238. char *pkg_name, *pkg_depends, *pkg_section, *pkg_descr, *pkg_url;
  239. char *pkg_cxx, *pkg_subpkgs, *pkg_cfline, *pkg_dflt, *pkg_multi;
  240. char *pkg_need_cxx, *pkg_need_java, *pkgname;
  241. char *pkg_host_depends, *pkg_arch_depends, *pkg_flavours, *pkg_flavours_string, *pkg_choices, *pseudo_name;
  242. char *packages, *pkg_name_u, *pkgs;
  243. char *saveptr, *p_ptr, *s_ptr;
  244. int result;
  245. StrMap *pkgmap, *sectionmap;
  246. pkg_name = NULL;
  247. pkg_descr = NULL;
  248. pkg_section = NULL;
  249. pkg_url = NULL;
  250. pkg_depends = NULL;
  251. pkg_flavours = NULL;
  252. pkg_flavours_string = NULL;
  253. pkg_choices = NULL;
  254. pkg_subpkgs = NULL;
  255. pkg_arch_depends = NULL;
  256. pkg_host_depends = NULL;
  257. pkg_cxx = NULL;
  258. pkg_dflt = NULL;
  259. pkg_cfline = NULL;
  260. pkg_multi = NULL;
  261. pkg_need_cxx = NULL;
  262. pkg_need_java = NULL;
  263. pkgname = NULL;
  264. p_ptr = NULL;
  265. s_ptr = NULL;
  266. unlink("package/Config.in.auto");
  267. /* open global sectionfile */
  268. menuglobal = fopen("package/Config.in.auto.global", "w");
  269. if (menuglobal == NULL)
  270. fatal_error("global section file not writable.");
  271. /* read section list and create a hash table */
  272. section = fopen("package/section.lst", "r");
  273. if (section == NULL)
  274. fatal_error("section listfile is missing");
  275. sectionmap = strmap_new(HASHSZ);
  276. while (fgets(tbuf, MAXPATH, section) != NULL) {
  277. key = strtok(tbuf, "\t");
  278. value = strtok(NULL, "\t");
  279. strmap_put(sectionmap, key, value);
  280. }
  281. fclose(section);
  282. if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0)
  283. fatal_error("creation of package/pkgconfigs.d failed.");
  284. if (mkdir("package/pkglist.d", S_IRWXU) > 0)
  285. fatal_error("creation of package/pkglist.d failed.");
  286. /* read Makefile's for all packages */
  287. pkgdir = opendir("package");
  288. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  289. /* skip dotfiles */
  290. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  291. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  292. fatal_error("can not create path variable.");
  293. pkg = fopen(path, "r");
  294. if (pkg == NULL)
  295. continue;
  296. /* skip manually maintained packages */
  297. if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0)
  298. fatal_error("can not create path variable.");
  299. if (!access(path, F_OK)) {
  300. while (fgets(buf, MAXPATH, pkg) != NULL) {
  301. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  302. continue;
  303. }
  304. memset(hvalue, 0 , MAXVALUE);
  305. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  306. if (result == 1) {
  307. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  308. fatal_error("can not create path variable.");
  309. section = fopen(spath, "a");
  310. if (section != NULL) {
  311. fprintf(section, "%s@\n", pkgdirp->d_name);
  312. fclose(section);
  313. }
  314. } else
  315. fatal_error("Can not find section description for package %s.",
  316. pkgdirp->d_name);
  317. fclose(pkg);
  318. continue;
  319. }
  320. nobinpkgs = 0;
  321. /* create output directories */
  322. if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0)
  323. fatal_error("can not create dir variable.");
  324. if (mkdir(dir, S_IRWXU) > 0)
  325. fatal_error("can not create directory.");
  326. /* allocate memory */
  327. hkey = malloc(MAXVAR);
  328. memset(hkey, 0, MAXVAR);
  329. memset(variable, 0, 2*MAXVAR);
  330. pkgmap = strmap_new(HASHSZ);
  331. /* parse package Makefile */
  332. while (fgets(buf, MAXPATH, pkg) != NULL) {
  333. /* just read variables prefixed with PKG */
  334. if (strncmp(buf, "PKG", 3) == 0) {
  335. if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0)
  336. continue;
  337. if (pkg_name != NULL)
  338. pkg_name_u = toupperstr(pkg_name);
  339. else
  340. pkg_name_u = toupperstr(pkgdirp->d_name);
  341. snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u);
  342. if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0)
  343. continue;
  344. snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u);
  345. if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0)
  346. continue;
  347. if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0)
  348. continue;
  349. if ((parse_var(buf, "PKG_ARCH_DEPENDS", NULL, &pkg_arch_depends)) == 0)
  350. continue;
  351. if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0)
  352. continue;
  353. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  354. continue;
  355. if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0)
  356. continue;
  357. if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0)
  358. continue;
  359. if ((parse_var(buf, "PKG_NEED_CXX", NULL, &pkg_need_cxx)) == 0)
  360. continue;
  361. if ((parse_var(buf, "PKG_NEED_JAVA", NULL, &pkg_need_java)) == 0)
  362. continue;
  363. if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0)
  364. continue;
  365. if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0)
  366. continue;
  367. if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_STRING_", pkg_flavours_string, &pkg_flavours_string, &pkgname, 20)) == 0)
  368. continue;
  369. if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_", pkg_flavours, &pkg_flavours, &pkgname, 13)) == 0)
  370. continue;
  371. if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0)
  372. continue;
  373. if ((parse_var_hash(buf, "PKGFX_", pkgmap)) == 0)
  374. continue;
  375. if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0)
  376. continue;
  377. if ((parse_var_hash(buf, "PKGFC_", pkgmap)) == 0)
  378. continue;
  379. if ((parse_var_with_pkg(buf, "PKG_CHOICES_", pkg_choices, &pkg_choices, &pkgname, 12)) == 0)
  380. continue;
  381. if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0)
  382. continue;
  383. if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0)
  384. continue;
  385. if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0)
  386. continue;
  387. if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0)
  388. continue;
  389. if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0)
  390. continue;
  391. if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0)
  392. continue;
  393. }
  394. }
  395. /* end of package Makefile parsing */
  396. if (fclose(pkg) != 0)
  397. perror("Failed to close file stream for Makefile");
  398. #if 0
  399. if (pkg_name != NULL)
  400. fprintf(stderr, "Package name is %s\n", pkg_name);
  401. if (pkg_section != NULL)
  402. fprintf(stderr, "Package section is %s\n", pkg_section);
  403. if (pkg_descr != NULL)
  404. fprintf(stderr, "Package description is %s\n", pkg_descr);
  405. if (pkg_depends != NULL)
  406. fprintf(stderr, "Package dependencies are %s\n", pkg_depends);
  407. if (pkg_subpkgs != NULL)
  408. fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs);
  409. if (pkg_flavours != NULL && pkgname != NULL)
  410. fprintf(stderr, "Package flavours for %s are %s\n", pkgname, pkg_flavours);
  411. if (pkg_flavours_string != NULL && pkgname != NULL)
  412. fprintf(stderr, "Package string flavours for %s are %s\n", pkgname, pkg_flavours_string);
  413. if (pkg_choices != NULL && pkgname != NULL)
  414. fprintf(stderr, "Package choices for %s are %s\n", pkgname, pkg_choices);
  415. if (pkg_url != NULL)
  416. fprintf(stderr, "Package homepage is %s\n", pkg_url);
  417. if (pkg_cfline != NULL)
  418. fprintf(stderr, "Package cfline is %s\n", pkg_cfline);
  419. if (pkg_multi != NULL)
  420. fprintf(stderr, "Package multi is %s\n", pkg_multi);
  421. strmap_enum(pkgmap, iter_debug, NULL);
  422. #endif
  423. /* generate master source Config.in file */
  424. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0)
  425. fatal_error("path variable creation failed.");
  426. fprintf(menuglobal, "source \"%s\"\n", path);
  427. /* recreating file is faster than truncating with w+ */
  428. unlink(path);
  429. cfg = fopen(path, "w");
  430. if (cfg == NULL)
  431. continue;
  432. pkgs = NULL;
  433. if (pkg_subpkgs != NULL)
  434. pkgs = strdup(pkg_subpkgs);
  435. fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  436. fprintf(cfg, "\ttristate\n");
  437. if (nobinpkgs == 0) {
  438. fprintf(cfg, "\tdepends on ");
  439. if (pkgs != NULL) {
  440. if (pkg_multi != NULL)
  441. if (strncmp(pkg_multi, "1", 1) == 0)
  442. fprintf(cfg, "ADK_HAVE_DOT_CONFIG || ");
  443. token = strtok(pkgs, " ");
  444. fprintf(cfg, "ADK_PACKAGE_%s", token);
  445. token = strtok(NULL, " ");
  446. while (token != NULL) {
  447. fprintf(cfg, " || ADK_PACKAGE_%s", token);
  448. token = strtok(NULL, " ");
  449. }
  450. fprintf(cfg, "\n");
  451. } else {
  452. fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  453. }
  454. }
  455. fprintf(cfg, "\tdefault n\n");
  456. fclose(cfg);
  457. free(pkgs);
  458. /* skip packages without binary package output */
  459. if (nobinpkgs == 1)
  460. continue;
  461. /* generate binary package specific Config.in files */
  462. if (pkg_subpkgs != NULL)
  463. packages = tolowerstr(pkg_subpkgs);
  464. else
  465. packages = strdup(pkgdirp->d_name);
  466. token = strtok_r(packages, " ", &p_ptr);
  467. while (token != NULL) {
  468. strncat(hkey, "PKGSC_", 6);
  469. strncat(hkey, toupperstr(token), strlen(token));
  470. memset(hvalue, 0 , MAXVALUE);
  471. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  472. memset(hkey, 0 , MAXVAR);
  473. if (result == 1)
  474. pkg_section = strdup(hvalue);
  475. strncat(hkey, "PKGSD_", 6);
  476. strncat(hkey, toupperstr(token), strlen(token));
  477. memset(hvalue, 0 , MAXVALUE);
  478. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  479. memset(hkey, 0 , MAXVAR);
  480. if (result == 1)
  481. pkg_descr = strdup(hvalue);
  482. pseudo_name = malloc(MAXLINE);
  483. memset(pseudo_name, 0, MAXLINE);
  484. strncat(pseudo_name, token, strlen(token));
  485. while (strlen(pseudo_name) < 20)
  486. strncat(pseudo_name, ".", 1);
  487. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0)
  488. fatal_error("failed to create path variable.");
  489. /* create temporary section files */
  490. memset(hvalue, 0 , MAXVALUE);
  491. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  492. if (result == 1) {
  493. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  494. fatal_error("failed to create path variable.");
  495. section = fopen(spath, "a");
  496. if (section != NULL) {
  497. fprintf(section, "%s |%s\n", token, pkgdirp->d_name);
  498. fclose(section);
  499. }
  500. } else
  501. fatal_error("Can not find section description for package %s.", pkgdirp->d_name);
  502. unlink(path);
  503. cfg = fopen(path, "w");
  504. if (cfg == NULL)
  505. perror("Can not open Config.in file");
  506. if (pkg_need_cxx != NULL) {
  507. fprintf(cfg, "comment \"%s... %s (disabled, c++ missing)\"\n", token, pkg_descr);
  508. fprintf(cfg, "depends on !ADK_TOOLCHAIN_GCC_CXX\n\n");
  509. }
  510. fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token));
  511. fprintf(cfg, "\tprompt \"%s. %s\"\n", pseudo_name, pkg_descr);
  512. fprintf(cfg, "\ttristate\n");
  513. if (pkg_multi != NULL)
  514. if (strncmp(pkg_multi, "1", 1) == 0)
  515. if (strncmp(toupperstr(token), toupperstr(pkgdirp->d_name), strlen(token)) != 0)
  516. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  517. free(pseudo_name);
  518. /* print custom cf line */
  519. if (pkg_cfline != NULL) {
  520. cftoken = strtok_r(pkg_cfline, "@", &saveptr);
  521. while (cftoken != NULL) {
  522. fprintf(cfg, "\t%s\n", cftoken);
  523. cftoken = strtok_r(NULL, "@", &saveptr);
  524. }
  525. free(pkg_cfline);
  526. pkg_cfline = NULL;
  527. }
  528. /* add sub package dependencies */
  529. strncat(hkey, "PKGSS_", 6);
  530. strncat(hkey, toupperstr(token), strlen(token));
  531. memset(hvalue, 0, MAXVALUE);
  532. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  533. if (result == 1) {
  534. val = strtok_r(hvalue, " ", &saveptr);
  535. while (val != NULL) {
  536. if (strncmp(val, "kmod", 4) == 0)
  537. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  538. else
  539. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  540. val = strtok_r(NULL, " ", &saveptr);
  541. }
  542. }
  543. memset(hkey, 0, MAXVAR);
  544. /* create package host dependency information */
  545. if (pkg_host_depends != NULL) {
  546. token = strtok(pkg_host_depends, " ");
  547. fprintf(cfg, "\tdepends on ");
  548. sp = "";
  549. while (token != NULL) {
  550. if(strncmp(token, "!", 1) == 0) {
  551. fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token));
  552. sp = " && ";
  553. } else {
  554. fprintf(cfg, "%sADK_HOST_%s", sp, toupperstr(token));
  555. sp = " || ";
  556. }
  557. token = strtok(NULL, " ");
  558. }
  559. fprintf(cfg, "\n");
  560. }
  561. /* create package target architecture dependency information */
  562. if (pkg_arch_depends != NULL) {
  563. token = strtok(pkg_arch_depends, " ");
  564. fprintf(cfg, "\tdepends on ");
  565. sp = "";
  566. while (token != NULL) {
  567. if(strncmp(token, "!", 1) == 0) {
  568. fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token));
  569. sp = " && ";
  570. } else {
  571. fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token));
  572. sp = " || ";
  573. }
  574. token = strtok(NULL, " ");
  575. }
  576. fprintf(cfg, "\n");
  577. }
  578. /* create package dependency information */
  579. if (pkg_depends != NULL) {
  580. token = strtok(pkg_depends, " ");
  581. while (token != NULL) {
  582. if (strncmp(token, "kmod", 4) == 0)
  583. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token));
  584. else
  585. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token));
  586. token = strtok(NULL, " ");
  587. }
  588. free(pkg_depends);
  589. pkg_depends = NULL;
  590. }
  591. if (pkg_need_cxx != NULL) {
  592. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_CXX\n");
  593. }
  594. if (pkg_need_java != NULL) {
  595. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_JAVA\n");
  596. pkg_need_java = NULL;
  597. }
  598. fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  599. if (pkg_dflt != NULL) {
  600. fprintf(cfg, "\tdefault %s\n", pkg_dflt);
  601. pkg_dflt = NULL;
  602. } else {
  603. fprintf(cfg, "\tdefault n\n");
  604. }
  605. fprintf(cfg, "\thelp\n");
  606. fprintf(cfg, "\t %s\n\n", pkg_descr);
  607. if (pkg_url != NULL)
  608. fprintf(cfg, "\t WWW: %s\n", pkg_url);
  609. /* handle special C++ packages */
  610. if (pkg_cxx != NULL) {
  611. fprintf(cfg, "\nchoice\n");
  612. fprintf(cfg, "prompt \"C++ library to use\"\n");
  613. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  614. fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC || ADK_TARGET_LIB_EGLIBC\n", pkg_cxx);
  615. fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx);
  616. fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx);
  617. fprintf(cfg, "\tbool \"GNU C++ library\"\n");
  618. fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n");
  619. fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx);
  620. fprintf(cfg, "\tbool \"uClibc++ library\"\n");
  621. fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n");
  622. fprintf(cfg, "endchoice\n");
  623. free(pkg_cxx);
  624. pkg_cxx = NULL;
  625. }
  626. /* package flavours */
  627. if (pkg_flavours != NULL) {
  628. token = strtok(pkg_flavours, " ");
  629. while (token != NULL) {
  630. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  631. // process default value
  632. strncat(hkey, "PKGFX_", 6);
  633. strncat(hkey, token, strlen(token));
  634. memset(hvalue, 0 , MAXVALUE);
  635. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  636. memset(hkey, 0 , MAXVAR);
  637. pkg_fd = strdup(hvalue);
  638. if (strlen(pkg_fd) > 0)
  639. fprintf(cfg, "\tdefault %s\n", pkg_fd);
  640. else
  641. fprintf(cfg, "\tdefault n\n");
  642. // process flavour cfline
  643. strncat(hkey, "PKGFC_", 6);
  644. strncat(hkey, token, strlen(token));
  645. memset(hvalue, 0 , MAXVALUE);
  646. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  647. memset(hkey, 0 , MAXVAR);
  648. pkg_fd = strdup(hvalue);
  649. if (strlen(pkg_fd) > 0)
  650. fprintf(cfg, "\t%s\n", pkg_fd);
  651. fprintf(cfg, "\tboolean ");
  652. strncat(hkey, "PKGFD_", 6);
  653. strncat(hkey, token, strlen(token));
  654. memset(hvalue, 0 , MAXVALUE);
  655. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  656. memset(hkey, 0 , MAXVAR);
  657. pkg_fd = strdup(hvalue);
  658. fprintf(cfg, "\"%s\"\n", pkg_fd);
  659. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname);
  660. strncat(hkey, "PKGFS_", 6);
  661. strncat(hkey, token, strlen(token));
  662. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  663. if (result == 1) {
  664. val = strtok_r(hvalue, " ", &saveptr);
  665. while (val != NULL) {
  666. if (strncmp(val, "kmod", 4) == 0)
  667. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  668. else
  669. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  670. val = strtok_r(NULL, " ", &saveptr);
  671. }
  672. }
  673. memset(hkey, 0, MAXVAR);
  674. fprintf(cfg, "\thelp\n");
  675. fprintf(cfg, "\t %s\n", pkg_fd);
  676. token = strtok(NULL, " ");
  677. }
  678. free(pkg_flavours);
  679. pkg_flavours = NULL;
  680. }
  681. /* package flavours string */
  682. if (pkg_flavours_string != NULL) {
  683. token = strtok(pkg_flavours_string, " ");
  684. while (token != NULL) {
  685. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  686. // process default value
  687. strncat(hkey, "PKGFX_", 6);
  688. strncat(hkey, token, strlen(token));
  689. memset(hvalue, 0 , MAXVALUE);
  690. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  691. memset(hkey, 0 , MAXVAR);
  692. pkg_fd = strdup(hvalue);
  693. if (strlen(pkg_fd) > 0)
  694. fprintf(cfg, "\tdefault \"%s\"\n", pkg_fd);
  695. // process flavour cfline
  696. strncat(hkey, "PKGFC_", 6);
  697. strncat(hkey, token, strlen(token));
  698. memset(hvalue, 0 , MAXVALUE);
  699. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  700. memset(hkey, 0 , MAXVAR);
  701. pkg_fd = strdup(hvalue);
  702. if (strlen(pkg_fd) > 0)
  703. fprintf(cfg, "\t%s\n", pkg_fd);
  704. fprintf(cfg, "\tstring ");
  705. strncat(hkey, "PKGFD_", 6);
  706. strncat(hkey, token, strlen(token));
  707. memset(hvalue, 0 , MAXVALUE);
  708. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  709. memset(hkey, 0 , MAXVAR);
  710. pkg_fd = strdup(hvalue);
  711. fprintf(cfg, "\"%s\"\n", pkg_fd);
  712. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname);
  713. strncat(hkey, "PKGFS_", 6);
  714. strncat(hkey, token, strlen(token));
  715. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  716. if (result == 1) {
  717. val = strtok_r(hvalue, " ", &saveptr);
  718. while (val != NULL) {
  719. if (strncmp(val, "kmod", 4) == 0)
  720. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  721. else
  722. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  723. val = strtok_r(NULL, " ", &saveptr);
  724. }
  725. }
  726. memset(hkey, 0, MAXVAR);
  727. fprintf(cfg, "\thelp\n");
  728. fprintf(cfg, "\t %s\n", pkg_fd);
  729. token = strtok(NULL, " ");
  730. }
  731. free(pkg_flavours_string);
  732. pkg_flavours_string = NULL;
  733. }
  734. /* package choices */
  735. if (pkg_choices != NULL) {
  736. fprintf(cfg, "\nchoice\n");
  737. fprintf(cfg, "prompt \"Package flavour choice\"\n");
  738. fprintf(cfg, "depends on ADK_PACKAGE_%s\n\n", pkgname);
  739. token = strtok(pkg_choices, " ");
  740. while (token != NULL) {
  741. fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  742. fprintf(cfg, "\tbool ");
  743. strncat(hkey, "PKGCD_", 6);
  744. strncat(hkey, token, strlen(token));
  745. memset(hvalue, 0 , MAXVALUE);
  746. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  747. memset(hkey, 0 , MAXVAR);
  748. fprintf(cfg, "\"%s\"\n", hvalue);
  749. strncat(hkey, "PKGCS_", 6);
  750. strncat(hkey, token, strlen(token));
  751. memset(hvalue, 0, MAXVALUE);
  752. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  753. if (result == 1) {
  754. val = strtok_r(hvalue, " ", &saveptr);
  755. while (val != NULL) {
  756. if (strncmp(val, "kmod", 4) == 0)
  757. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  758. else
  759. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  760. val = strtok_r(NULL, " ", &saveptr);
  761. }
  762. }
  763. memset(hkey, 0, MAXVAR);
  764. token = strtok(NULL, " ");
  765. }
  766. fprintf(cfg, "\nendchoice\n");
  767. free(pkg_choices);
  768. pkg_choices = NULL;
  769. }
  770. /* close file descriptor, parse next package */
  771. fclose(cfg);
  772. token = strtok_r(NULL, " ", &p_ptr);
  773. }
  774. /* end of package output generation */
  775. free(packages);
  776. packages = NULL;
  777. pkg_need_cxx = NULL;
  778. pkg_need_java = NULL;
  779. /* reset flags, free memory */
  780. free(pkg_name);
  781. free(pkg_descr);
  782. free(pkg_section);
  783. free(pkg_url);
  784. free(pkg_depends);
  785. free(pkg_flavours);
  786. free(pkg_flavours_string);
  787. free(pkg_choices);
  788. free(pkg_subpkgs);
  789. free(pkg_arch_depends);
  790. free(pkg_host_depends);
  791. free(pkg_cxx);
  792. free(pkg_dflt);
  793. free(pkg_cfline);
  794. free(pkg_multi);
  795. pkg_name = NULL;
  796. pkg_descr = NULL;
  797. pkg_section = NULL;
  798. pkg_url = NULL;
  799. pkg_depends = NULL;
  800. pkg_flavours = NULL;
  801. pkg_flavours_string = NULL;
  802. pkg_choices = NULL;
  803. pkg_subpkgs = NULL;
  804. pkg_arch_depends = NULL;
  805. pkg_host_depends = NULL;
  806. pkg_cxx = NULL;
  807. pkg_dflt = NULL;
  808. pkg_cfline = NULL;
  809. pkg_multi = NULL;
  810. strmap_delete(pkgmap);
  811. nobinpkgs = 0;
  812. free(hkey);
  813. }
  814. }
  815. /* create Config.in.auto */
  816. strmap_enum(sectionmap, iter, NULL);
  817. strmap_delete(sectionmap);
  818. fclose(menuglobal);
  819. closedir(pkgdir);
  820. /* remove temporary section files */
  821. pkglistdir = opendir("package/pkglist.d");
  822. while ((pkgdirp = readdir(pkglistdir)) != NULL) {
  823. if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) {
  824. if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0)
  825. fatal_error("creating path variable failed.");
  826. if (unlink(path) < 0)
  827. fatal_error("removing file failed.");
  828. }
  829. }
  830. closedir(pkglistdir);
  831. return(0);
  832. }