depmaker.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * depmaker - create package/Depends.mk for OpenADK buildsystem
  3. *
  4. * Copyright (C) 2010-2015 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. #define _GNU_SOURCE
  20. #include <ctype.h>
  21. #include <dirent.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #define MAXLINE 1024
  27. #define MAXPATH 128
  28. static int prefix = 0;
  29. static int hprefix = 0;
  30. static int check_symbol(char *symbol) {
  31. FILE *config;
  32. char buf[MAXLINE];
  33. char *sym;
  34. int ret;
  35. if ((sym = malloc(strlen(symbol) + 2)) != NULL)
  36. memset(sym, 0, strlen(symbol) + 2);
  37. else {
  38. perror("Can not allocate memory.");
  39. exit(EXIT_FAILURE);
  40. }
  41. strncat(sym, symbol, strlen(symbol));
  42. strncat(sym, "=", 1);
  43. if ((config = fopen(".config", "r")) == NULL) {
  44. perror("Can not open file \".config\".");
  45. exit(EXIT_FAILURE);
  46. }
  47. ret = 1;
  48. while (fgets(buf, MAXLINE, config) != NULL) {
  49. if (strncmp(buf, sym, strlen(sym)) == 0)
  50. ret = 0;
  51. }
  52. free(sym);
  53. if (fclose(config) != 0)
  54. perror("Closing file stream failed");
  55. return(ret);
  56. }
  57. /*@null@*/
  58. static char *parse_line(char *package, char *pkgvar, char *string, int checksym, int pprefix, int system, int *prefixp) {
  59. char *key, *value, *dep, *key_sym, *pkgdeps, *depvar;
  60. char temp[MAXLINE];
  61. int i, skip;
  62. string[strlen(string)-1] = '\0';
  63. if ((key = strtok(string, ":=")) == NULL) {
  64. perror("Can not get key from string.");
  65. exit(EXIT_FAILURE);
  66. }
  67. if (checksym == 1) {
  68. /* extract symbol */
  69. if ((key_sym = malloc(MAXLINE)) != NULL)
  70. memset(key_sym, 0, MAXLINE);
  71. else {
  72. perror("Can not allocate memory.");
  73. exit(EXIT_FAILURE);
  74. }
  75. switch(system) {
  76. case 0:
  77. if (pprefix == 0) {
  78. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0)
  79. perror("Can not create string variable.");
  80. } else {
  81. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0)
  82. perror("Can not create string variable.");
  83. }
  84. strncat(key_sym, key+6, strlen(key)-6);
  85. break;
  86. case 1:
  87. if (snprintf(key_sym, MAXLINE, "ADK_TARGET_SYSTEM_%s", pkgvar) < 0)
  88. perror("Can not create string variable.");
  89. break;
  90. case 2:
  91. if (snprintf(key_sym, MAXLINE, "ADK_TARGET_LIB_%s", pkgvar) < 0)
  92. perror("Can not create string variable.");
  93. break;
  94. }
  95. if (check_symbol(key_sym) != 0) {
  96. free(key_sym);
  97. return(NULL);
  98. }
  99. free(key_sym);
  100. }
  101. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  102. memset(pkgdeps, 0, MAXLINE);
  103. else {
  104. perror("Can not allocate memory.");
  105. exit(EXIT_FAILURE);
  106. }
  107. skip=0;
  108. value = strtok(NULL, "=\t");
  109. dep = strtok(value, " ");
  110. while (dep != NULL) {
  111. /* check only for optional host tools, if they are required to build */
  112. if (checksym == 2) {
  113. if ((depvar = malloc(MAXLINE)) != NULL)
  114. memset(depvar, 0, MAXLINE);
  115. else {
  116. perror("Can not allocate memory.");
  117. exit(EXIT_FAILURE);
  118. }
  119. strncat(depvar, dep, strlen(dep)-5);
  120. if ((strncmp(depvar, "bc", 2) == 0) ||
  121. (strncmp(depvar, "bison", 5) == 0) ||
  122. (strncmp(depvar, "bzip2", 5) == 0) ||
  123. (strncmp(depvar, "file", 4) == 0) ||
  124. (strncmp(depvar, "flex", 4) == 0) ||
  125. (strncmp(depvar, "gawk", 4) == 0) ||
  126. (strncmp(depvar, "grep", 4) == 0) ||
  127. (strncmp(depvar, "patch", 5) == 0) ||
  128. (strncmp(depvar, "sed", 3) == 0) ||
  129. (strncmp(depvar, "xz", 2) == 0)) {
  130. /* transform to uppercase variable name */
  131. for (i=0; i<(int)strlen(depvar); i++) {
  132. if (depvar[i] == '+')
  133. depvar[i] = 'X';
  134. if (depvar[i] == '-')
  135. depvar[i] = '_';
  136. depvar[i] = toupper(depvar[i]);
  137. }
  138. /* extract symbol */
  139. if ((key_sym = malloc(MAXLINE)) != NULL)
  140. memset(key_sym, 0, MAXLINE);
  141. else {
  142. perror("Can not allocate memory.");
  143. exit(EXIT_FAILURE);
  144. }
  145. if (snprintf(key_sym, MAXLINE, "ADK_HOST_BUILD_%s", depvar) < 0)
  146. perror("Can not create string variable.");
  147. if (check_symbol(key_sym) != 0)
  148. skip=1;
  149. free(key_sym);
  150. free(depvar);
  151. }
  152. }
  153. if (*prefixp == 0) {
  154. *prefixp = 1;
  155. if (skip == 0) {
  156. if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0)
  157. perror("Can not create string variable.");
  158. strncat(pkgdeps, temp, strlen(temp));
  159. }
  160. } else {
  161. if (skip == 0) {
  162. if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0)
  163. perror("Can not create string variable.");
  164. strncat(pkgdeps, temp, strlen(temp));
  165. }
  166. }
  167. dep = strtok(NULL, " ");
  168. }
  169. return(pkgdeps);
  170. }
  171. int main() {
  172. DIR *pkgdir;
  173. struct dirent *pkgdirp;
  174. FILE *pkg;
  175. char buf[MAXLINE];
  176. char path[MAXPATH];
  177. char *string, *pkgvar, *pkgdeps, *hpkgdeps = NULL, *tmp, *fpkg, *cpkg, *spkg, *key, *check, *dpkg;
  178. char *stringtmp;
  179. int i;
  180. spkg = NULL;
  181. cpkg = NULL;
  182. fpkg = NULL;
  183. /* read Makefile's for all packages */
  184. pkgdir = opendir("package");
  185. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  186. /* skip dotfiles */
  187. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  188. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  189. perror("Can not create string variable.");
  190. pkg = fopen(path, "r");
  191. if (pkg == NULL)
  192. continue;
  193. /* transform to uppercase variable name */
  194. pkgvar = strdup(pkgdirp->d_name);
  195. for (i=0; i<(int)strlen(pkgvar); i++) {
  196. if (pkgvar[i] == '+')
  197. pkgvar[i] = 'X';
  198. if (pkgvar[i] == '-')
  199. pkgvar[i] = '_';
  200. pkgvar[i] = toupper(pkgvar[i]);
  201. }
  202. /* exclude manual maintained packages from package/Makefile */
  203. if (
  204. !(strncmp(pkgdirp->d_name, "uclibc-ng", 9) == 0 && strlen(pkgdirp->d_name) == 9) &&
  205. !(strncmp(pkgdirp->d_name, "musl", 4) == 0) &&
  206. !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) {
  207. /* print result to stdout */
  208. printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name);
  209. printf("hostpackage-$(ADK_HOST_BUILD_%s) += %s\n", pkgvar, pkgdirp->d_name);
  210. }
  211. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  212. memset(pkgdeps, 0, MAXLINE);
  213. else {
  214. perror("Can not allocate memory.");
  215. exit(EXIT_FAILURE);
  216. }
  217. prefix = 0;
  218. hprefix = 0;
  219. /* generate build dependencies */
  220. while (fgets(buf, MAXLINE, pkg) != NULL) {
  221. if ((tmp = malloc(MAXLINE)) != NULL)
  222. memset(tmp, 0 , MAXLINE);
  223. else {
  224. perror("Can not allocate memory.");
  225. exit(EXIT_FAILURE);
  226. }
  227. /* just read variables prefixed with PKG */
  228. if (strncmp(buf, "PKG", 3) == 0) {
  229. string = strstr(buf, "PKG_BUILDDEP:=");
  230. if (string != NULL) {
  231. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 2, 0, 0, &prefix);
  232. if (tmp != NULL) {
  233. strncat(pkgdeps, tmp, strlen(tmp));
  234. }
  235. }
  236. string = strstr(buf, "PKG_BUILDDEP+=");
  237. if (string != NULL) {
  238. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 2, 0, 0, &prefix);
  239. if (tmp != NULL)
  240. strncat(pkgdeps, tmp, strlen(tmp));
  241. }
  242. // We need to find the system or libc name here
  243. string = strstr(buf, "PKG_BUILDDEP_");
  244. if (string != NULL) {
  245. check = strstr(buf, ":=");
  246. if (check != NULL) {
  247. stringtmp = strdup(string);
  248. string[strlen(string)-1] = '\0';
  249. key = strtok(string, ":=");
  250. dpkg = strdup(key+13);
  251. if (strncmp("UCLIBC_NG", dpkg, 9) == 0) {
  252. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 2, &prefix);
  253. } else if (strncmp("MUSL", dpkg, 4) == 0) {
  254. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 2, &prefix);
  255. } else {
  256. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1, &prefix);
  257. }
  258. if (tmp != NULL)
  259. strncat(pkgdeps, tmp, strlen(tmp));
  260. }
  261. }
  262. // We need to find the subpackage name here
  263. string = strstr(buf, "PKG_FLAVOURS_");
  264. if (string != NULL) {
  265. check = strstr(buf, ":=");
  266. if (check != NULL) {
  267. string[strlen(string)-1] = '\0';
  268. key = strtok(string, ":=");
  269. fpkg = strdup(key+13);
  270. }
  271. }
  272. string = strstr(buf, "PKGFB_");
  273. if (string != NULL) {
  274. tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0, &prefix);
  275. if (tmp != NULL)
  276. strncat(pkgdeps, tmp, strlen(tmp));
  277. }
  278. // We need to find the subpackage name here
  279. string = strstr(buf, "PKG_CHOICES_");
  280. if (string != NULL) {
  281. check = strstr(buf, ":=");
  282. if (check != NULL) {
  283. string[strlen(string)-1] = '\0';
  284. key = strtok(string, ":=");
  285. cpkg = strdup(key+12);
  286. }
  287. }
  288. string = strstr(buf, "PKGCB_");
  289. if (string != NULL) {
  290. tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0, &prefix);
  291. if (tmp != NULL)
  292. strncat(pkgdeps, tmp, strlen(tmp));
  293. }
  294. // We need to find the subpackage name here
  295. string = strstr(buf, "PKG_SUBPKGS_");
  296. if (string != NULL) {
  297. check = strstr(buf, ":=");
  298. if (check != NULL) {
  299. string[strlen(string)-1] = '\0';
  300. key = strtok(string, ":=");
  301. spkg = strdup(key+12);
  302. }
  303. }
  304. string = strstr(buf, "PKGSB_");
  305. if (string != NULL) {
  306. tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0, &prefix);
  307. if (tmp != NULL) {
  308. strncat(pkgdeps, tmp, strlen(tmp));
  309. }
  310. }
  311. } else if (strncmp(buf, "HOST_BUILDDEP", 13) == 0) {
  312. asprintf(&string, "%s-host", pkgdirp->d_name);
  313. // check retval; string for NULL
  314. tmp = parse_line(string, NULL, buf, 2, 0, 0, &hprefix);
  315. if (tmp && *tmp) {
  316. asprintf(&string, "%s%s",
  317. hpkgdeps ? hpkgdeps : "",
  318. tmp);
  319. free(hpkgdeps);
  320. hpkgdeps = string;
  321. }
  322. }
  323. free(tmp);
  324. }
  325. if (strlen(pkgdeps) != 0)
  326. printf("%s\n", pkgdeps);
  327. if (hpkgdeps && *hpkgdeps)
  328. printf("%s\n", hpkgdeps);
  329. free(hpkgdeps);
  330. hpkgdeps = NULL;
  331. free(pkgdeps);
  332. free(pkgvar);
  333. if (fclose(pkg) != 0)
  334. perror("Closing file stream failed");
  335. }
  336. }
  337. if (closedir(pkgdir) != 0)
  338. perror("Closing directory stream failed");
  339. return(0);
  340. }