depmaker.c 8.7 KB

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