depmaker.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 check_symbol(char *symbol) {
  29. FILE *config;
  30. char buf[MAXLINE];
  31. char *sym;
  32. int ret;
  33. if ((sym = malloc(strlen(symbol) + 2)) != NULL)
  34. memset(sym, 0, strlen(symbol) + 2);
  35. else {
  36. perror("Can not allocate memory.");
  37. exit(EXIT_FAILURE);
  38. }
  39. strncat(sym, symbol, strlen(symbol));
  40. strncat(sym, "=", 1);
  41. if ((config = fopen(".config", "r")) == NULL) {
  42. perror("Can not open file \".config\".");
  43. exit(EXIT_FAILURE);
  44. }
  45. ret = 1;
  46. while (fgets(buf, MAXLINE, config) != NULL) {
  47. if (strncmp(buf, sym, strlen(sym)) == 0)
  48. ret = 0;
  49. }
  50. free(sym);
  51. if (fclose(config) != 0)
  52. perror("Closing file stream failed");
  53. return(ret);
  54. }
  55. /*@null@*/
  56. static char *parse_line(char *package, char *pkgvar, char *string, int checksym, int pprefix, int system) {
  57. char *key, *value, *dep, *key_sym, *pkgdeps;
  58. char temp[MAXLINE];
  59. string[strlen(string)-1] = '\0';
  60. if ((key = strtok(string, ":=")) == NULL) {
  61. perror("Can not get key from string.");
  62. exit(EXIT_FAILURE);
  63. }
  64. if (checksym == 1) {
  65. /* extract symbol */
  66. if ((key_sym = malloc(MAXLINE)) != NULL)
  67. memset(key_sym, 0, MAXLINE);
  68. else {
  69. perror("Can not allocate memory.");
  70. exit(EXIT_FAILURE);
  71. }
  72. if (system == 0) {
  73. if (pprefix == 0) {
  74. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0)
  75. perror("Can not create string variable.");
  76. } else {
  77. if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0)
  78. perror("Can not create string variable.");
  79. }
  80. strncat(key_sym, key+6, strlen(key)-6);
  81. } else {
  82. if (snprintf(key_sym, MAXLINE, "ADK_TARGET_SYSTEM_%s", pkgvar) < 0)
  83. perror("Can not create string variable.");
  84. }
  85. if (check_symbol(key_sym) != 0) {
  86. free(key_sym);
  87. return(NULL);
  88. }
  89. free(key_sym);
  90. }
  91. if ((pkgdeps = malloc(MAXLINE)) != NULL)
  92. memset(pkgdeps, 0, MAXLINE);
  93. else {
  94. perror("Can not allocate memory.");
  95. exit(EXIT_FAILURE);
  96. }
  97. value = strtok(NULL, "=\t");
  98. dep = strtok(value, " ");
  99. while (dep != NULL) {
  100. if (prefix == 0) {
  101. prefix = 1;
  102. if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0)
  103. perror("Can not create string variable.");
  104. } else {
  105. if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0)
  106. perror("Can not create string variable.");
  107. }
  108. strncat(pkgdeps, temp, strlen(temp));
  109. dep = strtok(NULL, " ");
  110. }
  111. return(pkgdeps);
  112. }
  113. int main() {
  114. DIR *pkgdir;
  115. struct dirent *pkgdirp;
  116. FILE *pkg;
  117. char buf[MAXLINE];
  118. char path[MAXPATH];
  119. char *string, *pkgvar, *pkgdeps, *tmp, *fpkg, *cpkg, *spkg, *key, *check, *dpkg;
  120. char *stringtmp;
  121. int i;
  122. spkg = NULL;
  123. cpkg = NULL;
  124. fpkg = NULL;
  125. /* read Makefile's for all packages */
  126. pkgdir = opendir("package");
  127. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  128. /* skip dotfiles */
  129. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  130. if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
  131. perror("Can not create string variable.");
  132. pkg = fopen(path, "r");
  133. if (pkg == NULL)
  134. continue;
  135. /* transform to uppercase variable name */
  136. pkgvar = strdup(pkgdirp->d_name);
  137. for (i=0; i<(int)strlen(pkgvar); i++) {
  138. if (pkgvar[i] == '+')
  139. pkgvar[i] = 'X';
  140. if (pkgvar[i] == '-')
  141. pkgvar[i] = '_';
  142. pkgvar[i] = toupper(pkgvar[i]);
  143. }
  144. /* exclude manual maintained packages from package/Makefile */
  145. if (
  146. !(strncmp(pkgdirp->d_name, "libc", 4) == 0 && strlen(pkgdirp->d_name) == 4) &&
  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. /* generate build dependencies */
  163. while (fgets(buf, MAXLINE, pkg) != NULL) {
  164. if ((tmp = malloc(MAXLINE)) != NULL)
  165. memset(tmp, 0 , MAXLINE);
  166. else {
  167. perror("Can not allocate memory.");
  168. exit(EXIT_FAILURE);
  169. }
  170. /* just read variables prefixed with PKG */
  171. if (strncmp(buf, "PKG", 3) == 0) {
  172. string = strstr(buf, "PKG_BUILDDEP:=");
  173. if (string != NULL) {
  174. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0);
  175. if (tmp != NULL) {
  176. strncat(pkgdeps, tmp, strlen(tmp));
  177. }
  178. }
  179. string = strstr(buf, "PKG_BUILDDEP+=");
  180. if (string != NULL) {
  181. tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0);
  182. if (tmp != NULL)
  183. strncat(pkgdeps, tmp, strlen(tmp));
  184. }
  185. // We need to find the system name here
  186. string = strstr(buf, "PKG_BUILDDEP_");
  187. if (string != NULL) {
  188. check = strstr(buf, ":=");
  189. if (check != NULL) {
  190. stringtmp = strdup(string);
  191. string[strlen(string)-1] = '\0';
  192. key = strtok(string, ":=");
  193. dpkg = strdup(key+13);
  194. tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1);
  195. if (tmp != NULL)
  196. strncat(pkgdeps, tmp, strlen(tmp));
  197. }
  198. }
  199. // We need to find the subpackage name here
  200. string = strstr(buf, "PKG_FLAVOURS_");
  201. if (string != NULL) {
  202. check = strstr(buf, ":=");
  203. if (check != NULL) {
  204. string[strlen(string)-1] = '\0';
  205. key = strtok(string, ":=");
  206. fpkg = strdup(key+13);
  207. }
  208. }
  209. string = strstr(buf, "PKGFB_");
  210. if (string != NULL) {
  211. tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0);
  212. if (tmp != NULL)
  213. strncat(pkgdeps, tmp, strlen(tmp));
  214. }
  215. // We need to find the subpackage name here
  216. string = strstr(buf, "PKG_CHOICES_");
  217. if (string != NULL) {
  218. check = strstr(buf, ":=");
  219. if (check != NULL) {
  220. string[strlen(string)-1] = '\0';
  221. key = strtok(string, ":=");
  222. cpkg = strdup(key+12);
  223. }
  224. }
  225. string = strstr(buf, "PKGCB_");
  226. if (string != NULL) {
  227. tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0);
  228. if (tmp != NULL)
  229. strncat(pkgdeps, tmp, strlen(tmp));
  230. }
  231. // We need to find the subpackage name here
  232. string = strstr(buf, "PKG_SUBPKGS_");
  233. if (string != NULL) {
  234. check = strstr(buf, ":=");
  235. if (check != NULL) {
  236. string[strlen(string)-1] = '\0';
  237. key = strtok(string, ":=");
  238. spkg = strdup(key+12);
  239. }
  240. }
  241. string = strstr(buf, "PKGSB_");
  242. if (string != NULL) {
  243. tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0);
  244. if (tmp != NULL) {
  245. strncat(pkgdeps, tmp, strlen(tmp));
  246. }
  247. }
  248. }
  249. free(tmp);
  250. }
  251. if (strlen(pkgdeps) != 0)
  252. printf("%s\n", pkgdeps);
  253. free(pkgdeps);
  254. free(pkgvar);
  255. if (fclose(pkg) != 0)
  256. perror("Closing file stream failed");
  257. }
  258. }
  259. if (closedir(pkgdir) != 0)
  260. perror("Closing directory stream failed");
  261. return(0);
  262. }