depmaker.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. }
  155. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  156. memset(pkgdeps, 0, MAXLINE);
  157. else {
  158. perror("Can not allocate memory.");
  159. exit(EXIT_FAILURE);
  160. }
  161. prefix = 0;
  162. hprefix = 0;
  163. /* generate build dependencies */
  164. while (fgets(buf, MAXLINE, pkg) != NULL) {
  165. if ((tmp = malloc(MAXLINE)) != NULL)
  166. memset(tmp, 0 , MAXLINE);
  167. else {
  168. perror("Can not allocate memory.");
  169. exit(EXIT_FAILURE);
  170. }
  171. /* just read variables prefixed with PKG */
  172. if (strncmp(buf, "PKG", 3) == 0) {
  173. string = strstr(buf, "PKG_BUILDDEP:=");
  174. if (string != NULL) {
  175. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
  176. if (tmp != NULL) {
  177. strncat(pkgdeps, tmp, strlen(tmp));
  178. }
  179. }
  180. string = strstr(buf, "PKG_BUILDDEP+=");
  181. if (string != NULL) {
  182. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
  183. if (tmp != NULL)
  184. strncat(pkgdeps, tmp, strlen(tmp));
  185. }
  186. // We need to find the system name here
  187. string = strstr(buf, "PKG_BUILDDEP_");
  188. if (string != NULL) {
  189. check = strstr(buf, ":=");
  190. if (check != NULL) {
  191. stringtmp = strdup(string);
  192. string[strlen(string)-1] = '\0';
  193. key = strtok(string, ":=");
  194. dpkg = strdup(key+13);
  195. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1, &prefix);
  196. if (tmp != NULL)
  197. strncat(pkgdeps, tmp, strlen(tmp));
  198. }
  199. }
  200. // We need to find the subpackage name here
  201. string = strstr(buf, "PKG_FLAVOURS_");
  202. if (string != NULL) {
  203. check = strstr(buf, ":=");
  204. if (check != NULL) {
  205. string[strlen(string)-1] = '\0';
  206. key = strtok(string, ":=");
  207. fpkg = strdup(key+13);
  208. }
  209. }
  210. string = strstr(buf, "PKGFB_");
  211. if (string != NULL) {
  212. tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0, &prefix);
  213. if (tmp != NULL)
  214. strncat(pkgdeps, tmp, strlen(tmp));
  215. }
  216. // We need to find the subpackage name here
  217. string = strstr(buf, "PKG_CHOICES_");
  218. if (string != NULL) {
  219. check = strstr(buf, ":=");
  220. if (check != NULL) {
  221. string[strlen(string)-1] = '\0';
  222. key = strtok(string, ":=");
  223. cpkg = strdup(key+12);
  224. }
  225. }
  226. string = strstr(buf, "PKGCB_");
  227. if (string != NULL) {
  228. tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0, &prefix);
  229. if (tmp != NULL)
  230. strncat(pkgdeps, tmp, strlen(tmp));
  231. }
  232. // We need to find the subpackage name here
  233. string = strstr(buf, "PKG_SUBPKGS_");
  234. if (string != NULL) {
  235. check = strstr(buf, ":=");
  236. if (check != NULL) {
  237. string[strlen(string)-1] = '\0';
  238. key = strtok(string, ":=");
  239. spkg = strdup(key+12);
  240. }
  241. }
  242. string = strstr(buf, "PKGSB_");
  243. if (string != NULL) {
  244. tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0, &prefix);
  245. if (tmp != NULL) {
  246. strncat(pkgdeps, tmp, strlen(tmp));
  247. }
  248. }
  249. } else if (strncmp(buf, "HOST_BUILDDEP", 13) == 0) {
  250. asprintf(&string, "%s-host", pkgdirp->d_name);
  251. // check retval; string for NULL
  252. tmp = parse_line(string, NULL, buf, 0, 0, 0, &hprefix);
  253. if (tmp && *tmp) {
  254. asprintf(&string, "%s%s",
  255. hpkgdeps ? hpkgdeps : "",
  256. tmp);
  257. free(hpkgdeps);
  258. hpkgdeps = string;
  259. }
  260. }
  261. free(tmp);
  262. }
  263. if (strlen(pkgdeps) != 0)
  264. printf("%s\n", pkgdeps);
  265. if (hpkgdeps && *hpkgdeps)
  266. printf("%s\n", hpkgdeps);
  267. free(hpkgdeps);
  268. hpkgdeps = NULL;
  269. free(pkgdeps);
  270. free(pkgvar);
  271. if (fclose(pkg) != 0)
  272. perror("Closing file stream failed");
  273. }
  274. }
  275. if (closedir(pkgdir) != 0)
  276. perror("Closing directory stream failed");
  277. return(0);
  278. }