pkgrebuild.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * pkgrebuild - recognize required package rebuilds in OpenADK
  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 <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);
  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);
  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;
  84. char *pkg_name, *keystr;
  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. string[strlen(string)-1] = '\0';
  149. key = strtok(string, ":=");
  150. value = strtok(NULL, "=\t");
  151. token = strtok(value, " ");
  152. while (token != NULL) {
  153. keystr = malloc(256);
  154. memset(keystr, 0, 256);
  155. strncat(keystr, "ADK_PACKAGE_", 12);
  156. strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
  157. strncat(keystr, "_", 1);
  158. strncat(keystr, token, strlen(token));
  159. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  160. token = strtok(NULL, " ");
  161. free(keystr);
  162. keystr = NULL;
  163. }
  164. }
  165. string = strstr(pbuf, "PKG_CHOICES:=");
  166. if (string != NULL) {
  167. string[strlen(string)-1] = '\0';
  168. key = strtok(string, ":=");
  169. value = strtok(NULL, "=\t");
  170. token = strtok(value, " ");
  171. while (token != NULL) {
  172. keystr = malloc(256);
  173. memset(keystr, 0, 256);
  174. strncat(keystr, "ADK_PACKAGE_", 12);
  175. strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
  176. strncat(keystr, "_", 1);
  177. strncat(keystr, token, strlen(token));
  178. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  179. token = strtok(NULL, " ");
  180. free(keystr);
  181. keystr = NULL;
  182. }
  183. }
  184. string = strstr(pbuf, "PKG_FLAVOURS+=");
  185. if (string != NULL) {
  186. string[strlen(string)-1] = '\0';
  187. key = strtok(string, "+=");
  188. value = strtok(NULL, "=\t");
  189. token = strtok(value, " ");
  190. while (token != NULL) {
  191. keystr = malloc(256);
  192. memset(keystr, 0, 256);
  193. strncat(keystr, "ADK_PACKAGE_", 12);
  194. strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
  195. strncat(keystr, "_", 1);
  196. strncat(keystr, token, strlen(token));
  197. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  198. token = strtok(NULL, " ");
  199. free(keystr);
  200. keystr = NULL;
  201. }
  202. }
  203. }
  204. }
  205. fclose(pkg);
  206. }
  207. }
  208. closedir(pkgdir);
  209. config = fopen(".config", "r");
  210. if (config == NULL)
  211. perror(".config is missing.");
  212. configmap = strmap_new(1024);
  213. while (fgets(buf, 128, config) != NULL) {
  214. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  215. key = strtok(buf, "=");
  216. value = strtok(NULL, "=");
  217. strmap_put(configmap, key, value);
  218. }
  219. }
  220. fclose(config);
  221. configold = fopen(".config.old", "r");
  222. if (configold == NULL)
  223. perror(".config.old is missing.");
  224. configoldmap = strmap_new(1024);
  225. while (fgets(buf, 128, configold) != NULL) {
  226. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  227. key = strtok(buf, "=");
  228. value = strtok(NULL, "=");
  229. strmap_put(configoldmap, key, value);
  230. }
  231. }
  232. fclose(configold);
  233. //fprintf(stdout, "Config Count: %d\n", strmap_get_count(configmap));
  234. //fprintf(stdout, "Config Old Count: %d\n", strmap_get_count(configoldmap));
  235. strmap_enum(configoldmap, iter_disabled, NULL);
  236. strmap_enum(configmap, iter_enabled, NULL);
  237. //strmap_enum(pkgmap, iter, NULL);
  238. strmap_delete(pkgmap);
  239. strmap_delete(configmap);
  240. strmap_delete(configoldmap);
  241. return(0);
  242. }