pkgrebuild.c 7.4 KB

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