1
0

depmaker.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * depmaker - create package/Depends.mk for OpenADK buildsystem
  3. *
  4. * Copyright (C) 2010-2014 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;
  59. char temp[MAXLINE];
  60. string[strlen(string)-1] = '\0';
  61. if ((key = strtok(string, ":=")) == NULL) {
  62. perror("Can not get key from string.");
  63. exit(EXIT_FAILURE);
  64. }
  65. if (checksym == 1) {
  66. /* extract symbol */
  67. if ((key_sym = malloc(MAXLINE)) != NULL)
  68. memset(key_sym, 0, MAXLINE);
  69. else {
  70. perror("Can not allocate memory.");
  71. exit(EXIT_FAILURE);
  72. }
  73. if (system == 0) {
  74. if (pprefix == 0) {
  75. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0)
  76. perror("Can not create string variable.");
  77. } else {
  78. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0)
  79. perror("Can not create string variable.");
  80. }
  81. strncat(key_sym, key+6, strlen(key)-6);
  82. } else {
  83. if (snprintf(key_sym, MAXLINE, "ADK_TARGET_SYSTEM_%s", pkgvar) < 0)
  84. perror("Can not create string variable.");
  85. }
  86. if (check_symbol(key_sym) != 0) {
  87. free(key_sym);
  88. return(NULL);
  89. }
  90. free(key_sym);
  91. }
  92. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  93. memset(pkgdeps, 0, MAXLINE);
  94. else {
  95. perror("Can not allocate memory.");
  96. exit(EXIT_FAILURE);
  97. }
  98. value = strtok(NULL, "=\t");
  99. dep = strtok(value, " ");
  100. while (dep != NULL) {
  101. if (*prefixp == 0) {
  102. *prefixp = 1;
  103. if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0)
  104. perror("Can not create string variable.");
  105. } else {
  106. if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0)
  107. perror("Can not create string variable.");
  108. }
  109. strncat(pkgdeps, temp, strlen(temp));
  110. dep = strtok(NULL, " ");
  111. }
  112. return(pkgdeps);
  113. }
  114. int main() {
  115. DIR *pkgdir;
  116. struct dirent *pkgdirp;
  117. FILE *pkg;
  118. char buf[MAXLINE];
  119. char path[MAXPATH];
  120. char *string, *pkgvar, *pkgdeps, *hpkgdeps = NULL, *tmp, *fpkg, *cpkg, *spkg, *key, *check, *dpkg;
  121. char *stringtmp;
  122. int i;
  123. spkg = NULL;
  124. cpkg = NULL;
  125. fpkg = NULL;
  126. /* read Makefile's for all packages */
  127. pkgdir = opendir("package");
  128. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  129. /* skip dotfiles */
  130. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  131. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  132. perror("Can not create string variable.");
  133. pkg = fopen(path, "r");
  134. if (pkg == NULL)
  135. continue;
  136. /* transform to uppercase variable name */
  137. pkgvar = strdup(pkgdirp->d_name);
  138. for (i=0; i<(int)strlen(pkgvar); i++) {
  139. if (pkgvar[i] == '+')
  140. pkgvar[i] = 'X';
  141. if (pkgvar[i] == '-')
  142. pkgvar[i] = '_';
  143. pkgvar[i] = toupper(pkgvar[i]);
  144. }
  145. /* exclude manual maintained packages from package/Makefile */
  146. if (
  147. !(strncmp(pkgdirp->d_name, "libpthread", 10) == 0 && strlen(pkgdirp->d_name) == 10) &&
  148. !(strncmp(pkgdirp->d_name, "uclibc++", 8) == 0) &&
  149. !(strncmp(pkgdirp->d_name, "uclibc", 6) == 0) &&
  150. !(strncmp(pkgdirp->d_name, "musl", 4) == 0) &&
  151. !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) {
  152. /* print result to stdout */
  153. printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name);
  154. printf("hostpackage-$(ADK_HOST_BUILD_%s) += %s\n", pkgvar, pkgdirp->d_name);
  155. }
  156. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  157. memset(pkgdeps, 0, MAXLINE);
  158. else {
  159. perror("Can not allocate memory.");
  160. exit(EXIT_FAILURE);
  161. }
  162. prefix = 0;
  163. hprefix = 0;
  164. /* generate build dependencies */
  165. while (fgets(buf, MAXLINE, pkg) != NULL) {
  166. if ((tmp = malloc(MAXLINE)) != NULL)
  167. memset(tmp, 0 , MAXLINE);
  168. else {
  169. perror("Can not allocate memory.");
  170. exit(EXIT_FAILURE);
  171. }
  172. /* just read variables prefixed with PKG */
  173. if (strncmp(buf, "PKG", 3) == 0) {
  174. string = strstr(buf, "PKG_BUILDDEP:=");
  175. if (string != NULL) {
  176. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
  177. if (tmp != NULL) {
  178. strncat(pkgdeps, tmp, strlen(tmp));
  179. }
  180. }
  181. string = strstr(buf, "PKG_BUILDDEP+=");
  182. if (string != NULL) {
  183. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
  184. if (tmp != NULL)
  185. strncat(pkgdeps, tmp, strlen(tmp));
  186. }
  187. // We need to find the system name here
  188. string = strstr(buf, "PKG_BUILDDEP_");
  189. if (string != NULL) {
  190. check = strstr(buf, ":=");
  191. if (check != NULL) {
  192. stringtmp = strdup(string);
  193. string[strlen(string)-1] = '\0';
  194. key = strtok(string, ":=");
  195. dpkg = strdup(key+13);
  196. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1, &prefix);
  197. if (tmp != NULL)
  198. strncat(pkgdeps, tmp, strlen(tmp));
  199. }
  200. }
  201. // We need to find the subpackage name here
  202. string = strstr(buf, "PKG_FLAVOURS_");
  203. if (string != NULL) {
  204. check = strstr(buf, ":=");
  205. if (check != NULL) {
  206. string[strlen(string)-1] = '\0';
  207. key = strtok(string, ":=");
  208. fpkg = strdup(key+13);
  209. }
  210. }
  211. string = strstr(buf, "PKGFB_");
  212. if (string != NULL) {
  213. tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0, &prefix);
  214. if (tmp != NULL)
  215. strncat(pkgdeps, tmp, strlen(tmp));
  216. }
  217. // We need to find the subpackage name here
  218. string = strstr(buf, "PKG_CHOICES_");
  219. if (string != NULL) {
  220. check = strstr(buf, ":=");
  221. if (check != NULL) {
  222. string[strlen(string)-1] = '\0';
  223. key = strtok(string, ":=");
  224. cpkg = strdup(key+12);
  225. }
  226. }
  227. string = strstr(buf, "PKGCB_");
  228. if (string != NULL) {
  229. tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0, &prefix);
  230. if (tmp != NULL)
  231. strncat(pkgdeps, tmp, strlen(tmp));
  232. }
  233. // We need to find the subpackage name here
  234. string = strstr(buf, "PKG_SUBPKGS_");
  235. if (string != NULL) {
  236. check = strstr(buf, ":=");
  237. if (check != NULL) {
  238. string[strlen(string)-1] = '\0';
  239. key = strtok(string, ":=");
  240. spkg = strdup(key+12);
  241. }
  242. }
  243. string = strstr(buf, "PKGSB_");
  244. if (string != NULL) {
  245. tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0, &prefix);
  246. if (tmp != NULL) {
  247. strncat(pkgdeps, tmp, strlen(tmp));
  248. }
  249. }
  250. } else if (strncmp(buf, "HOST_BUILDDEP", 13) == 0) {
  251. asprintf(&string, "%s-host", pkgdirp->d_name);
  252. // check retval; string for NULL
  253. tmp = parse_line(string, NULL, buf, 0, 0, 0, &hprefix);
  254. if (tmp && *tmp) {
  255. asprintf(&string, "%s%s",
  256. hpkgdeps ? hpkgdeps : "",
  257. tmp);
  258. free(hpkgdeps);
  259. hpkgdeps = string;
  260. }
  261. }
  262. free(tmp);
  263. }
  264. if (strlen(pkgdeps) != 0)
  265. printf("%s\n", pkgdeps);
  266. if (hpkgdeps && *hpkgdeps)
  267. printf("%s\n", hpkgdeps);
  268. free(hpkgdeps);
  269. hpkgdeps = NULL;
  270. free(pkgdeps);
  271. free(pkgvar);
  272. if (fclose(pkg) != 0)
  273. perror("Closing file stream failed");
  274. }
  275. }
  276. if (closedir(pkgdir) != 0)
  277. perror("Closing directory stream failed");
  278. return(0);
  279. }