1
0

pkgrebuild.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * pkgrebuild - recognize required package rebuilds in OpenADK
  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 <fcntl.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include "strmap.h"
  29. StrMap *configmap, *configoldmap, *pkgmap;
  30. /*
  31. static void iter(const char *key, const char *value, const void *obj) {
  32. fprintf(stderr, "key: %s value: %s\n", key, value);
  33. }
  34. */
  35. static void iter_disabled(const char *key, const char *value, const void *obj) {
  36. char hvalue[256];
  37. char tfile[256];
  38. int fd;
  39. memset(hvalue, 0, 256);
  40. if (strmap_exists(configmap, key) == 0) {
  41. //fprintf(stderr, "disabled variables: %s\n", key);
  42. if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) {
  43. //fprintf(stderr, "Symbol is a flavour/choice: %s\n", hvalue);
  44. if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0)
  45. perror("can not create file variable.");
  46. fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  47. close(fd);
  48. }
  49. }
  50. }
  51. static void iter_enabled(const char *key, const char *value, const void *obj) {
  52. char hvalue[256];
  53. char tfile[256];
  54. int fd;
  55. memset(hvalue, 0, 256);
  56. if (strmap_exists(configoldmap, key) == 0) {
  57. //fprintf(stderr, "enabled variables: %s\n", key);
  58. if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) {
  59. //fprintf(stderr, "Symbol is a flavour/choice\n");
  60. if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0)
  61. perror("can not create file variable.");
  62. fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  63. close(fd);
  64. }
  65. }
  66. }
  67. static char *toupperstr(char *string) {
  68. int i;
  69. char *str;
  70. /* transform to uppercase variable name */
  71. str = strdup(string);
  72. for (i=0; i<(int)strlen(str); i++) {
  73. if (str[i] == '+')
  74. str[i] = 'X';
  75. if (str[i] == '-')
  76. str[i] = '_';
  77. str[i] = toupper(str[i]);
  78. }
  79. return(str);
  80. }
  81. int main() {
  82. FILE *config, *configold, *pkg;
  83. char *key, *value, *string, *token, *check;
  84. char *pkg_name, *keystr, *realpkgname;
  85. char buf[128];
  86. char path[320];
  87. char pbuf[320];
  88. DIR *pkgdir;
  89. struct dirent *pkgdirp;
  90. pkg_name = NULL;
  91. /* read Makefile's for all packages */
  92. pkgmap = strmap_new(1024);
  93. pkgdir = opendir("package");
  94. while ((pkgdirp = readdir(pkgdir)) != NULL) {
  95. /* skip dotfiles */
  96. if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
  97. if (snprintf(path, 320, "package/%s/Makefile", pkgdirp->d_name) < 0)
  98. perror("can not create path variable.");
  99. pkg = fopen(path, "r");
  100. if (pkg == NULL)
  101. continue;
  102. while (fgets(pbuf, 320, pkg) != NULL) {
  103. if (strncmp(pbuf, "PKG", 3) == 0) {
  104. string = strstr(pbuf, "PKG_NAME:=");
  105. if (string != NULL) {
  106. string[strlen(string)-1] = '\0';
  107. key = strtok(string, ":=");
  108. value = strtok(NULL, "=\t");
  109. if (value != NULL)
  110. pkg_name = strdup(value);
  111. }
  112. string = strstr(pbuf, "PKG_SUBPKGS:=");
  113. if (string != NULL) {
  114. string[strlen(string)-1] = '\0';
  115. key = strtok(string, ":=");
  116. value = strtok(NULL, "=\t");
  117. token = strtok(value, " ");
  118. while (token != NULL) {
  119. keystr = malloc(256);
  120. memset(keystr, 0, 256);
  121. strncat(keystr, "ADK_PACKAGE_", 12);
  122. strncat(keystr, token, strlen(token));
  123. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  124. token = strtok(NULL, " ");
  125. free(keystr);
  126. keystr = NULL;
  127. }
  128. }
  129. string = strstr(pbuf, "PKG_SUBPKGS+=");
  130. if (string != NULL) {
  131. string[strlen(string)-1] = '\0';
  132. key = strtok(string, "+=");
  133. value = strtok(NULL, "=\t");
  134. token = strtok(value, " ");
  135. while (token != NULL) {
  136. keystr = malloc(256);
  137. memset(keystr, 0, 256);
  138. strncat(keystr, "ADK_PACKAGE_", 12);
  139. strncat(keystr, token, strlen(token));
  140. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  141. token = strtok(NULL, " ");
  142. free(keystr);
  143. keystr = NULL;
  144. }
  145. }
  146. string = strstr(pbuf, "PKG_FLAVOURS_");
  147. if (string != NULL) {
  148. check = strstr(pbuf, ":=");
  149. if (check != NULL) {
  150. string[strlen(string)-1] = '\0';
  151. key = strtok(string, ":=");
  152. realpkgname = strdup(key+13);
  153. value = strtok(NULL, "=\t");
  154. token = strtok(value, " ");
  155. while (token != NULL) {
  156. keystr = malloc(256);
  157. memset(keystr, 0, 256);
  158. strncat(keystr, "ADK_PACKAGE_", 12);
  159. strncat(keystr, realpkgname, strlen(realpkgname));
  160. strncat(keystr, "_", 1);
  161. strncat(keystr, token, strlen(token));
  162. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  163. token = strtok(NULL, " ");
  164. free(keystr);
  165. keystr = NULL;
  166. }
  167. } else {
  168. string[strlen(string)-1] = '\0';
  169. key = strtok(string, "+=");
  170. realpkgname = strdup(key+13);
  171. value = strtok(NULL, "=\t");
  172. token = strtok(value, " ");
  173. while (token != NULL) {
  174. keystr = malloc(256);
  175. memset(keystr, 0, 256);
  176. strncat(keystr, "ADK_PACKAGE_", 12);
  177. strncat(keystr, realpkgname, strlen(realpkgname));
  178. strncat(keystr, "_", 1);
  179. strncat(keystr, token, strlen(token));
  180. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  181. token = strtok(NULL, " ");
  182. free(keystr);
  183. keystr = NULL;
  184. }
  185. }
  186. }
  187. string = strstr(pbuf, "PKG_CHOICES_");
  188. if (string != NULL) {
  189. string[strlen(string)-1] = '\0';
  190. key = strtok(string, ":=");
  191. value = strtok(NULL, "=\t");
  192. token = strtok(value, " ");
  193. while (token != NULL) {
  194. keystr = malloc(256);
  195. memset(keystr, 0, 256);
  196. strncat(keystr, "ADK_PACKAGE_", 12);
  197. strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
  198. strncat(keystr, "_", 1);
  199. strncat(keystr, token, strlen(token));
  200. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  201. token = strtok(NULL, " ");
  202. free(keystr);
  203. keystr = NULL;
  204. }
  205. }
  206. }
  207. }
  208. fclose(pkg);
  209. }
  210. }
  211. closedir(pkgdir);
  212. config = fopen(".config", "r");
  213. if (config == NULL)
  214. perror(".config is missing.");
  215. configmap = strmap_new(1024);
  216. while (fgets(buf, 128, config) != NULL) {
  217. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  218. key = strtok(buf, "=");
  219. value = strtok(NULL, "=");
  220. strmap_put(configmap, key, value);
  221. }
  222. }
  223. fclose(config);
  224. configold = fopen(".config.old", "r");
  225. if (configold == NULL)
  226. perror(".config.old is missing.");
  227. configoldmap = strmap_new(1024);
  228. while (fgets(buf, 128, configold) != NULL) {
  229. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  230. key = strtok(buf, "=");
  231. value = strtok(NULL, "=");
  232. strmap_put(configoldmap, key, value);
  233. }
  234. }
  235. fclose(configold);
  236. //fprintf(stdout, "Config Count: %d\n", strmap_get_count(configmap));
  237. //fprintf(stdout, "Config Old Count: %d\n", strmap_get_count(configoldmap));
  238. strmap_enum(configoldmap, iter_disabled, NULL);
  239. strmap_enum(configmap, iter_enabled, NULL);
  240. //strmap_enum(pkgmap, iter, NULL);
  241. strmap_delete(pkgmap);
  242. strmap_delete(configmap);
  243. strmap_delete(configoldmap);
  244. return(0);
  245. }