depmaker.c 10 KB

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