1
0

depmaker.c 8.8 KB

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