pkgmaker.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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_libc_depends, *pkg_host_depends, *pkg_system_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_system_depends = NULL;
  257. pkg_host_depends = NULL;
  258. pkg_libc_depends = NULL;
  259. pkg_cxx = NULL;
  260. pkg_dflt = NULL;
  261. pkg_cfline = NULL;
  262. pkg_multi = NULL;
  263. pkg_need_cxx = NULL;
  264. pkg_need_java = NULL;
  265. pkgname = NULL;
  266. p_ptr = NULL;
  267. s_ptr = NULL;
  268. unlink("package/Config.in.auto");
  269. /* open global sectionfile */
  270. menuglobal = fopen("package/Config.in.auto.global", "w");
  271. if (menuglobal == NULL)
  272. fatal_error("global section file not writable.");
  273. /* read section list and create a hash table */
  274. section = fopen("package/section.lst", "r");
  275. if (section == NULL)
  276. fatal_error("section listfile is missing");
  277. sectionmap = strmap_new(HASHSZ);
  278. while (fgets(tbuf, MAXPATH, section) != NULL) {
  279. key = strtok(tbuf, "\t");
  280. value = strtok(NULL, "\t");
  281. strmap_put(sectionmap, key, value);
  282. }
  283. fclose(section);
  284. if (mkdir("package/pkgconfigs.d", S_IRWXU) > 0)
  285. fatal_error("creation of package/pkgconfigs.d failed.");
  286. if (mkdir("package/pkglist.d", S_IRWXU) > 0)
  287. fatal_error("creation of package/pkglist.d failed.");
  288. /* read Makefile's for all packages */
  289. pkgdir = opendir("package");
  290. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  291. /* skip dotfiles */
  292. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  293. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  294. fatal_error("can not create path variable.");
  295. pkg = fopen(path, "r");
  296. if (pkg == NULL)
  297. continue;
  298. /* skip manually maintained packages */
  299. if (snprintf(path, MAXPATH, "package/%s/Config.in.manual", pkgdirp->d_name) < 0)
  300. fatal_error("can not create path variable.");
  301. if (!access(path, F_OK)) {
  302. while (fgets(buf, MAXPATH, pkg) != NULL) {
  303. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  304. continue;
  305. }
  306. memset(hvalue, 0 , MAXVALUE);
  307. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  308. if (result == 1) {
  309. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  310. fatal_error("can not create path variable.");
  311. section = fopen(spath, "a");
  312. if (section != NULL) {
  313. fprintf(section, "%s@\n", pkgdirp->d_name);
  314. fclose(section);
  315. }
  316. } else
  317. fatal_error("Can not find section description for package %s.",
  318. pkgdirp->d_name);
  319. fclose(pkg);
  320. continue;
  321. }
  322. nobinpkgs = 0;
  323. /* create output directories */
  324. if (snprintf(dir, MAXPATH, "package/pkgconfigs.d/%s", pkgdirp->d_name) < 0)
  325. fatal_error("can not create dir variable.");
  326. if (mkdir(dir, S_IRWXU) > 0)
  327. fatal_error("can not create directory.");
  328. /* allocate memory */
  329. hkey = malloc(MAXVAR);
  330. memset(hkey, 0, MAXVAR);
  331. memset(variable, 0, 2*MAXVAR);
  332. pkgmap = strmap_new(HASHSZ);
  333. /* parse package Makefile */
  334. while (fgets(buf, MAXPATH, pkg) != NULL) {
  335. /* just read variables prefixed with PKG */
  336. if (strncmp(buf, "PKG", 3) == 0) {
  337. if ((parse_var(buf, "PKG_NAME", NULL, &pkg_name)) == 0)
  338. continue;
  339. if (pkg_name != NULL)
  340. pkg_name_u = toupperstr(pkg_name);
  341. else
  342. pkg_name_u = toupperstr(pkgdirp->d_name);
  343. snprintf(variable, MAXVAR, "PKG_CFLINE_%s", pkg_name_u);
  344. if ((parse_var(buf, variable, pkg_cfline, &pkg_cfline)) == 0)
  345. continue;
  346. snprintf(variable, MAXVAR, "PKG_DFLT_%s", pkg_name_u);
  347. if ((parse_var(buf, variable, NULL, &pkg_dflt)) == 0)
  348. continue;
  349. if ((parse_var(buf, "PKG_LIBC_DEPENDS", NULL, &pkg_libc_depends)) == 0)
  350. continue;
  351. if ((parse_var(buf, "PKG_HOST_DEPENDS", NULL, &pkg_host_depends)) == 0)
  352. continue;
  353. if ((parse_var(buf, "PKG_ARCH_DEPENDS", NULL, &pkg_arch_depends)) == 0)
  354. continue;
  355. if ((parse_var(buf, "PKG_SYSTEM_DEPENDS", NULL, &pkg_system_depends)) == 0)
  356. continue;
  357. if ((parse_var(buf, "PKG_DESCR", NULL, &pkg_descr)) == 0)
  358. continue;
  359. if ((parse_var(buf, "PKG_SECTION", NULL, &pkg_section)) == 0)
  360. continue;
  361. if ((parse_var(buf, "PKG_URL", NULL, &pkg_url)) == 0)
  362. continue;
  363. if ((parse_var(buf, "PKG_CXX", NULL, &pkg_cxx)) == 0)
  364. continue;
  365. if ((parse_var(buf, "PKG_NEED_CXX", NULL, &pkg_need_cxx)) == 0)
  366. continue;
  367. if ((parse_var(buf, "PKG_NEED_JAVA", NULL, &pkg_need_java)) == 0)
  368. continue;
  369. if ((parse_var(buf, "PKG_MULTI", NULL, &pkg_multi)) == 0)
  370. continue;
  371. if ((parse_var(buf, "PKG_DEPENDS", pkg_depends, &pkg_depends)) == 0)
  372. continue;
  373. if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_STRING_", pkg_flavours_string, &pkg_flavours_string, &pkgname, 20)) == 0)
  374. continue;
  375. if ((parse_var_with_pkg(buf, "PKG_FLAVOURS_", pkg_flavours, &pkg_flavours, &pkgname, 13)) == 0)
  376. continue;
  377. if ((parse_var_hash(buf, "PKGFD_", pkgmap)) == 0)
  378. continue;
  379. if ((parse_var_hash(buf, "PKGFX_", pkgmap)) == 0)
  380. continue;
  381. if ((parse_var_hash(buf, "PKGFS_", pkgmap)) == 0)
  382. continue;
  383. if ((parse_var_hash(buf, "PKGFC_", pkgmap)) == 0)
  384. continue;
  385. if ((parse_var_with_pkg(buf, "PKG_CHOICES_", pkg_choices, &pkg_choices, &pkgname, 12)) == 0)
  386. continue;
  387. if ((parse_var_hash(buf, "PKGCD_", pkgmap)) == 0)
  388. continue;
  389. if ((parse_var_hash(buf, "PKGCS_", pkgmap)) == 0)
  390. continue;
  391. if ((parse_var(buf, "PKG_SUBPKGS", pkg_subpkgs, &pkg_subpkgs)) == 0)
  392. continue;
  393. if ((parse_var_hash(buf, "PKGSD_", pkgmap)) == 0)
  394. continue;
  395. if ((parse_var_hash(buf, "PKGSS_", pkgmap)) == 0)
  396. continue;
  397. if ((parse_var_hash(buf, "PKGSC_", pkgmap)) == 0)
  398. continue;
  399. }
  400. }
  401. /* end of package Makefile parsing */
  402. if (fclose(pkg) != 0)
  403. perror("Failed to close file stream for Makefile");
  404. #if 0
  405. if (pkg_name != NULL)
  406. fprintf(stderr, "Package name is %s\n", pkg_name);
  407. if (pkg_section != NULL)
  408. fprintf(stderr, "Package section is %s\n", pkg_section);
  409. if (pkg_descr != NULL)
  410. fprintf(stderr, "Package description is %s\n", pkg_descr);
  411. if (pkg_depends != NULL)
  412. fprintf(stderr, "Package dependencies are %s\n", pkg_depends);
  413. if (pkg_subpkgs != NULL)
  414. fprintf(stderr, "Package subpackages are %s\n", pkg_subpkgs);
  415. if (pkg_flavours != NULL && pkgname != NULL)
  416. fprintf(stderr, "Package flavours for %s are %s\n", pkgname, pkg_flavours);
  417. if (pkg_flavours_string != NULL && pkgname != NULL)
  418. fprintf(stderr, "Package string flavours for %s are %s\n", pkgname, pkg_flavours_string);
  419. if (pkg_choices != NULL && pkgname != NULL)
  420. fprintf(stderr, "Package choices for %s are %s\n", pkgname, pkg_choices);
  421. if (pkg_url != NULL)
  422. fprintf(stderr, "Package homepage is %s\n", pkg_url);
  423. if (pkg_cfline != NULL)
  424. fprintf(stderr, "Package cfline is %s\n", pkg_cfline);
  425. if (pkg_multi != NULL)
  426. fprintf(stderr, "Package multi is %s\n", pkg_multi);
  427. strmap_enum(pkgmap, iter_debug, NULL);
  428. #endif
  429. /* generate master source Config.in file */
  430. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in", pkgdirp->d_name) < 0)
  431. fatal_error("path variable creation failed.");
  432. fprintf(menuglobal, "source \"%s\"\n", path);
  433. /* recreating file is faster than truncating with w+ */
  434. unlink(path);
  435. cfg = fopen(path, "w");
  436. if (cfg == NULL)
  437. continue;
  438. pkgs = NULL;
  439. if (pkg_subpkgs != NULL)
  440. pkgs = strdup(pkg_subpkgs);
  441. fprintf(cfg, "config ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  442. fprintf(cfg, "\ttristate\n");
  443. if (nobinpkgs == 0) {
  444. fprintf(cfg, "\tdepends on ");
  445. if (pkgs != NULL) {
  446. if (pkg_multi != NULL)
  447. if (strncmp(pkg_multi, "1", 1) == 0)
  448. fprintf(cfg, "ADK_HAVE_DOT_CONFIG || ");
  449. token = strtok(pkgs, " ");
  450. fprintf(cfg, "ADK_PACKAGE_%s", token);
  451. token = strtok(NULL, " ");
  452. while (token != NULL) {
  453. fprintf(cfg, " || ADK_PACKAGE_%s", token);
  454. token = strtok(NULL, " ");
  455. }
  456. fprintf(cfg, "\n");
  457. } else {
  458. fprintf(cfg, "ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  459. }
  460. }
  461. fprintf(cfg, "\tdefault n\n");
  462. fclose(cfg);
  463. free(pkgs);
  464. /* skip packages without binary package output */
  465. if (nobinpkgs == 1)
  466. continue;
  467. /* generate binary package specific Config.in files */
  468. if (pkg_subpkgs != NULL)
  469. packages = tolowerstr(pkg_subpkgs);
  470. else
  471. packages = strdup(pkgdirp->d_name);
  472. token = strtok_r(packages, " ", &p_ptr);
  473. while (token != NULL) {
  474. strncat(hkey, "PKGSC_", 6);
  475. strncat(hkey, toupperstr(token), strlen(token));
  476. memset(hvalue, 0 , MAXVALUE);
  477. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  478. memset(hkey, 0 , MAXVAR);
  479. if (result == 1)
  480. pkg_section = strdup(hvalue);
  481. strncat(hkey, "PKGSD_", 6);
  482. strncat(hkey, toupperstr(token), strlen(token));
  483. memset(hvalue, 0 , MAXVALUE);
  484. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  485. memset(hkey, 0 , MAXVAR);
  486. if (result == 1)
  487. pkg_descr = strdup(hvalue);
  488. pseudo_name = malloc(MAXLINE);
  489. memset(pseudo_name, 0, MAXLINE);
  490. strncat(pseudo_name, token, strlen(token));
  491. while (strlen(pseudo_name) < 20)
  492. strncat(pseudo_name, ".", 1);
  493. if (snprintf(path, MAXPATH, "package/pkgconfigs.d/%s/Config.in.%s", pkgdirp->d_name, token) < 0)
  494. fatal_error("failed to create path variable.");
  495. /* create temporary section files */
  496. memset(hvalue, 0 , MAXVALUE);
  497. result = strmap_get(sectionmap, pkg_section, hvalue, sizeof(hvalue));
  498. if (result == 1) {
  499. if (snprintf(spath, MAXPATH, "package/pkglist.d/sectionlst.%d", hash_str(hvalue)) < 0)
  500. fatal_error("failed to create path variable.");
  501. section = fopen(spath, "a");
  502. if (section != NULL) {
  503. fprintf(section, "%s |%s\n", token, pkgdirp->d_name);
  504. fclose(section);
  505. }
  506. } else
  507. fatal_error("Can not find section description for package %s.", pkgdirp->d_name);
  508. unlink(path);
  509. cfg = fopen(path, "w");
  510. if (cfg == NULL)
  511. perror("Can not open Config.in file");
  512. if (pkg_need_cxx != NULL) {
  513. fprintf(cfg, "comment \"%s... %s (disabled, c++ missing)\"\n", token, pkg_descr);
  514. fprintf(cfg, "depends on !ADK_TOOLCHAIN_GCC_CXX\n\n");
  515. }
  516. fprintf(cfg, "config ADK_PACKAGE_%s\n", toupperstr(token));
  517. fprintf(cfg, "\tprompt \"%s. %s\"\n", pseudo_name, pkg_descr);
  518. fprintf(cfg, "\ttristate\n");
  519. if (pkg_multi != NULL)
  520. if (strncmp(pkg_multi, "1", 1) == 0)
  521. if (strncmp(toupperstr(token), toupperstr(pkgdirp->d_name), strlen(token)) != 0)
  522. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", toupperstr(pkgdirp->d_name));
  523. free(pseudo_name);
  524. /* print custom cf line */
  525. if (pkg_cfline != NULL) {
  526. cftoken = strtok_r(pkg_cfline, "@", &saveptr);
  527. while (cftoken != NULL) {
  528. fprintf(cfg, "\t%s\n", cftoken);
  529. cftoken = strtok_r(NULL, "@", &saveptr);
  530. }
  531. free(pkg_cfline);
  532. pkg_cfline = NULL;
  533. }
  534. /* add sub package dependencies */
  535. strncat(hkey, "PKGSS_", 6);
  536. strncat(hkey, toupperstr(token), strlen(token));
  537. memset(hvalue, 0, MAXVALUE);
  538. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  539. if (result == 1) {
  540. val = strtok_r(hvalue, " ", &saveptr);
  541. while (val != NULL) {
  542. if (strncmp(val, "kmod", 4) == 0)
  543. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  544. else
  545. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  546. val = strtok_r(NULL, " ", &saveptr);
  547. }
  548. }
  549. memset(hkey, 0, MAXVAR);
  550. /* create package target system dependency information */
  551. if (pkg_system_depends != NULL) {
  552. token = strtok(pkg_system_depends, " ");
  553. fprintf(cfg, "\tdepends on ");
  554. sp = "";
  555. while (token != NULL) {
  556. if(strncmp(token, "!", 1) == 0) {
  557. fprintf(cfg, "%s!ADK_TARGET_SYSTEM%s", sp, toupperstr(token));
  558. sp = " && ";
  559. } else {
  560. fprintf(cfg, "%sADK_TARGET_SYSTEM_%s", sp, toupperstr(token));
  561. sp = " || ";
  562. }
  563. token = strtok(NULL, " ");
  564. }
  565. fprintf(cfg, "\n");
  566. }
  567. /* create package host dependency information */
  568. if (pkg_host_depends != NULL) {
  569. token = strtok(pkg_host_depends, " ");
  570. fprintf(cfg, "\tdepends on ");
  571. sp = "";
  572. while (token != NULL) {
  573. if(strncmp(token, "!", 1) == 0) {
  574. fprintf(cfg, "%s!ADK_HOST%s", sp, toupperstr(token));
  575. sp = " && ";
  576. } else {
  577. fprintf(cfg, "%sADK_HOST_%s", sp, toupperstr(token));
  578. sp = " || ";
  579. }
  580. token = strtok(NULL, " ");
  581. }
  582. fprintf(cfg, "\n");
  583. }
  584. /* create package libc dependency information */
  585. if (pkg_libc_depends != NULL) {
  586. token = strtok(pkg_libc_depends, " ");
  587. fprintf(cfg, "\tdepends on ");
  588. sp = "";
  589. while (token != NULL) {
  590. if(strncmp(token, "!", 1) == 0) {
  591. fprintf(cfg, "%s!ADK_TARGET_LIB_%s", sp, toupperstr(token));
  592. sp = " && ";
  593. } else {
  594. fprintf(cfg, "%sADK_TARGET_LIB_%s", sp, toupperstr(token));
  595. sp = " || ";
  596. }
  597. token = strtok(NULL, " ");
  598. }
  599. fprintf(cfg, "\n");
  600. }
  601. /* create package target architecture dependency information */
  602. if (pkg_arch_depends != NULL) {
  603. token = strtok(pkg_arch_depends, " ");
  604. fprintf(cfg, "\tdepends on ");
  605. sp = "";
  606. while (token != NULL) {
  607. if(strncmp(token, "!", 1) == 0) {
  608. fprintf(cfg, "%s!ADK_LINUX%s", sp, toupperstr(token));
  609. sp = " && ";
  610. } else {
  611. fprintf(cfg, "%sADK_LINUX_%s", sp, toupperstr(token));
  612. sp = " || ";
  613. }
  614. token = strtok(NULL, " ");
  615. }
  616. fprintf(cfg, "\n");
  617. }
  618. /* create package dependency information */
  619. if (pkg_depends != NULL) {
  620. token = strtok(pkg_depends, " ");
  621. while (token != NULL) {
  622. if (strncmp(token, "kmod", 4) == 0)
  623. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(token));
  624. else
  625. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(token));
  626. token = strtok(NULL, " ");
  627. }
  628. free(pkg_depends);
  629. pkg_depends = NULL;
  630. }
  631. if (pkg_need_cxx != NULL) {
  632. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_CXX\n");
  633. }
  634. if (pkg_need_java != NULL) {
  635. fprintf(cfg, "\tdepends on ADK_TOOLCHAIN_GCC_JAVA\n");
  636. pkg_need_java = NULL;
  637. }
  638. fprintf(cfg, "\tselect ADK_COMPILE_%s\n", toupperstr(pkgdirp->d_name));
  639. if (pkg_dflt != NULL) {
  640. fprintf(cfg, "\tdefault %s\n", pkg_dflt);
  641. pkg_dflt = NULL;
  642. } else {
  643. fprintf(cfg, "\tdefault n\n");
  644. }
  645. fprintf(cfg, "\thelp\n");
  646. fprintf(cfg, "\t %s\n\n", pkg_descr);
  647. if (pkg_url != NULL)
  648. fprintf(cfg, "\t WWW: %s\n", pkg_url);
  649. /* handle special C++ packages */
  650. if (pkg_cxx != NULL) {
  651. fprintf(cfg, "\nchoice\n");
  652. fprintf(cfg, "prompt \"C++ library to use\"\n");
  653. fprintf(cfg, "depends on ADK_COMPILE_%s\n\n", toupperstr(pkgdirp->d_name));
  654. fprintf(cfg, "default ADK_COMPILE_%s_WITH_STDCXX if ADK_TARGET_LIB_GLIBC || ADK_TARGET_LIB_EGLIBC\n", pkg_cxx);
  655. fprintf(cfg, "default ADK_COMPILE_%s_WITH_UCLIBCXX if ADK_TARGET_LIB_UCLIBC\n\n", pkg_cxx);
  656. fprintf(cfg, "config ADK_COMPILE_%s_WITH_STDCXX\n", pkg_cxx);
  657. fprintf(cfg, "\tbool \"GNU C++ library\"\n");
  658. fprintf(cfg, "\tselect ADK_PACKAGE_LIBSTDCXX\n\n");
  659. fprintf(cfg, "config ADK_COMPILE_%s_WITH_UCLIBCXX\n", pkg_cxx);
  660. fprintf(cfg, "\tbool \"uClibc++ library\"\n");
  661. fprintf(cfg, "\tselect ADK_PACKAGE_UCLIBCXX\n\n");
  662. fprintf(cfg, "endchoice\n");
  663. free(pkg_cxx);
  664. pkg_cxx = NULL;
  665. }
  666. /* package flavours */
  667. if (pkg_flavours != NULL) {
  668. token = strtok(pkg_flavours, " ");
  669. while (token != NULL) {
  670. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  671. // process default value
  672. strncat(hkey, "PKGFX_", 6);
  673. strncat(hkey, token, strlen(token));
  674. memset(hvalue, 0 , MAXVALUE);
  675. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  676. memset(hkey, 0 , MAXVAR);
  677. pkg_fd = strdup(hvalue);
  678. if (strlen(pkg_fd) > 0)
  679. fprintf(cfg, "\tdefault %s\n", pkg_fd);
  680. else
  681. fprintf(cfg, "\tdefault n\n");
  682. // process flavour cfline
  683. strncat(hkey, "PKGFC_", 6);
  684. strncat(hkey, token, strlen(token));
  685. memset(hvalue, 0 , MAXVALUE);
  686. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  687. memset(hkey, 0 , MAXVAR);
  688. pkg_fd = strdup(hvalue);
  689. if (strlen(pkg_fd) > 0)
  690. fprintf(cfg, "\t%s\n", pkg_fd);
  691. fprintf(cfg, "\tboolean ");
  692. strncat(hkey, "PKGFD_", 6);
  693. strncat(hkey, token, strlen(token));
  694. memset(hvalue, 0 , MAXVALUE);
  695. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  696. memset(hkey, 0 , MAXVAR);
  697. pkg_fd = strdup(hvalue);
  698. fprintf(cfg, "\"%s\"\n", pkg_fd);
  699. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname);
  700. strncat(hkey, "PKGFS_", 6);
  701. strncat(hkey, token, strlen(token));
  702. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  703. if (result == 1) {
  704. val = strtok_r(hvalue, " ", &saveptr);
  705. while (val != NULL) {
  706. if (strncmp(val, "kmod", 4) == 0)
  707. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  708. else
  709. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  710. val = strtok_r(NULL, " ", &saveptr);
  711. }
  712. }
  713. memset(hkey, 0, MAXVAR);
  714. fprintf(cfg, "\thelp\n");
  715. fprintf(cfg, "\t %s\n", pkg_fd);
  716. token = strtok(NULL, " ");
  717. }
  718. free(pkg_flavours);
  719. pkg_flavours = NULL;
  720. }
  721. /* package flavours string */
  722. if (pkg_flavours_string != NULL) {
  723. token = strtok(pkg_flavours_string, " ");
  724. while (token != NULL) {
  725. fprintf(cfg, "\nconfig ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  726. // process default value
  727. strncat(hkey, "PKGFX_", 6);
  728. strncat(hkey, token, strlen(token));
  729. memset(hvalue, 0 , MAXVALUE);
  730. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  731. memset(hkey, 0 , MAXVAR);
  732. pkg_fd = strdup(hvalue);
  733. if (strlen(pkg_fd) > 0)
  734. fprintf(cfg, "\tdefault \"%s\"\n", pkg_fd);
  735. // process flavour cfline
  736. strncat(hkey, "PKGFC_", 6);
  737. strncat(hkey, token, strlen(token));
  738. memset(hvalue, 0 , MAXVALUE);
  739. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  740. memset(hkey, 0 , MAXVAR);
  741. pkg_fd = strdup(hvalue);
  742. if (strlen(pkg_fd) > 0)
  743. fprintf(cfg, "\t%s\n", pkg_fd);
  744. fprintf(cfg, "\tstring ");
  745. strncat(hkey, "PKGFD_", 6);
  746. strncat(hkey, token, strlen(token));
  747. memset(hvalue, 0 , MAXVALUE);
  748. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  749. memset(hkey, 0 , MAXVAR);
  750. pkg_fd = strdup(hvalue);
  751. fprintf(cfg, "\"%s\"\n", pkg_fd);
  752. fprintf(cfg, "\tdepends on ADK_PACKAGE_%s\n", pkgname);
  753. strncat(hkey, "PKGFS_", 6);
  754. strncat(hkey, token, strlen(token));
  755. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  756. if (result == 1) {
  757. val = strtok_r(hvalue, " ", &saveptr);
  758. while (val != NULL) {
  759. if (strncmp(val, "kmod", 4) == 0)
  760. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  761. else
  762. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  763. val = strtok_r(NULL, " ", &saveptr);
  764. }
  765. }
  766. memset(hkey, 0, MAXVAR);
  767. fprintf(cfg, "\thelp\n");
  768. fprintf(cfg, "\t %s\n", pkg_fd);
  769. token = strtok(NULL, " ");
  770. }
  771. free(pkg_flavours_string);
  772. pkg_flavours_string = NULL;
  773. }
  774. /* package choices */
  775. if (pkg_choices != NULL) {
  776. fprintf(cfg, "\nchoice\n");
  777. fprintf(cfg, "prompt \"Package flavour choice\"\n");
  778. fprintf(cfg, "depends on ADK_PACKAGE_%s\n\n", pkgname);
  779. token = strtok(pkg_choices, " ");
  780. while (token != NULL) {
  781. fprintf(cfg, "config ADK_PACKAGE_%s_%s\n", pkgname, toupperstr(token));
  782. fprintf(cfg, "\tbool ");
  783. strncat(hkey, "PKGCD_", 6);
  784. strncat(hkey, token, strlen(token));
  785. memset(hvalue, 0 , MAXVALUE);
  786. strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  787. memset(hkey, 0 , MAXVAR);
  788. fprintf(cfg, "\"%s\"\n", hvalue);
  789. strncat(hkey, "PKGCS_", 6);
  790. strncat(hkey, token, strlen(token));
  791. memset(hvalue, 0, MAXVALUE);
  792. result = strmap_get(pkgmap, hkey, hvalue, sizeof(hvalue));
  793. if (result == 1) {
  794. val = strtok_r(hvalue, " ", &saveptr);
  795. while (val != NULL) {
  796. if (strncmp(val, "kmod", 4) == 0)
  797. fprintf(cfg, "\tselect ADK_KPACKAGE_%s\n", toupperstr(val));
  798. else
  799. fprintf(cfg, "\tselect ADK_PACKAGE_%s\n", toupperstr(val));
  800. val = strtok_r(NULL, " ", &saveptr);
  801. }
  802. }
  803. memset(hkey, 0, MAXVAR);
  804. token = strtok(NULL, " ");
  805. }
  806. fprintf(cfg, "\nendchoice\n");
  807. free(pkg_choices);
  808. pkg_choices = NULL;
  809. }
  810. /* close file descriptor, parse next package */
  811. fclose(cfg);
  812. token = strtok_r(NULL, " ", &p_ptr);
  813. }
  814. /* end of package output generation */
  815. free(packages);
  816. packages = NULL;
  817. pkg_need_cxx = NULL;
  818. pkg_need_java = NULL;
  819. /* reset flags, free memory */
  820. free(pkg_name);
  821. free(pkg_descr);
  822. free(pkg_section);
  823. free(pkg_url);
  824. free(pkg_depends);
  825. free(pkg_flavours);
  826. free(pkg_flavours_string);
  827. free(pkg_choices);
  828. free(pkg_subpkgs);
  829. free(pkg_arch_depends);
  830. free(pkg_system_depends);
  831. free(pkg_host_depends);
  832. free(pkg_libc_depends);
  833. free(pkg_cxx);
  834. free(pkg_dflt);
  835. free(pkg_cfline);
  836. free(pkg_multi);
  837. pkg_name = NULL;
  838. pkg_descr = NULL;
  839. pkg_section = NULL;
  840. pkg_url = NULL;
  841. pkg_depends = NULL;
  842. pkg_flavours = NULL;
  843. pkg_flavours_string = NULL;
  844. pkg_choices = NULL;
  845. pkg_subpkgs = NULL;
  846. pkg_arch_depends = NULL;
  847. pkg_system_depends = NULL;
  848. pkg_host_depends = NULL;
  849. pkg_libc_depends = NULL;
  850. pkg_cxx = NULL;
  851. pkg_dflt = NULL;
  852. pkg_cfline = NULL;
  853. pkg_multi = NULL;
  854. strmap_delete(pkgmap);
  855. nobinpkgs = 0;
  856. free(hkey);
  857. }
  858. }
  859. /* create Config.in.auto */
  860. strmap_enum(sectionmap, iter, NULL);
  861. strmap_delete(sectionmap);
  862. fclose(menuglobal);
  863. closedir(pkgdir);
  864. /* remove temporary section files */
  865. pkglistdir = opendir("package/pkglist.d");
  866. while ((pkgdirp = readdir(pkglistdir)) != NULL) {
  867. if (strncmp(pkgdirp->d_name, "sectionlst.", 11) == 0) {
  868. if (snprintf(path, MAXPATH, "package/pkglist.d/%s", pkgdirp->d_name) < 0)
  869. fatal_error("creating path variable failed.");
  870. if (unlink(path) < 0)
  871. fatal_error("removing file failed.");
  872. }
  873. }
  874. closedir(pkglistdir);
  875. return(0);
  876. }