depmaker.c 7.1 KB

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