pkgmaker.c 28 KB

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