pkgmaker.c 29 KB

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