depmaker.c 6.0 KB

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