pkgrebuild.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_FLAVOURS:=");
  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, toupperstr(pkg_name), strlen(pkg_name));
  121. strncat(keystr, "_", 1);
  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_CHOICES:=");
  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, toupperstr(pkg_name), strlen(pkg_name));
  140. strncat(keystr, "_", 1);
  141. strncat(keystr, token, strlen(token));
  142. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  143. token = strtok(NULL, " ");
  144. free(keystr);
  145. keystr = NULL;
  146. }
  147. }
  148. string = strstr(pbuf, "PKG_FLAVOURS+=");
  149. if (string != NULL) {
  150. string[strlen(string)-1] = '\0';
  151. key = strtok(string, "+=");
  152. value = strtok(NULL, "=\t");
  153. token = strtok(value, " ");
  154. while (token != NULL) {
  155. keystr = malloc(256);
  156. memset(keystr, 0, 256);
  157. strncat(keystr, "ADK_PACKAGE_", 12);
  158. strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
  159. strncat(keystr, "_", 1);
  160. strncat(keystr, token, strlen(token));
  161. strmap_put(pkgmap, keystr, pkgdirp->d_name);
  162. token = strtok(NULL, " ");
  163. free(keystr);
  164. keystr = NULL;
  165. }
  166. }
  167. }
  168. }
  169. fclose(pkg);
  170. }
  171. }
  172. closedir(pkgdir);
  173. config = fopen(".config", "r");
  174. if (config == NULL)
  175. perror(".config is missing.");
  176. configmap = strmap_new(1024);
  177. while (fgets(buf, 128, config) != NULL) {
  178. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  179. key = strtok(buf, "=");
  180. value = strtok(NULL, "=");
  181. strmap_put(configmap, key, value);
  182. }
  183. }
  184. fclose(config);
  185. configold = fopen(".config.old", "r");
  186. if (configold == NULL)
  187. perror(".config.old is missing.");
  188. configoldmap = strmap_new(1024);
  189. while (fgets(buf, 128, configold) != NULL) {
  190. if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
  191. key = strtok(buf, "=");
  192. value = strtok(NULL, "=");
  193. strmap_put(configoldmap, key, value);
  194. }
  195. }
  196. fclose(configold);
  197. //fprintf(stdout, "Config Count: %d\n", strmap_get_count(configmap));
  198. //fprintf(stdout, "Config Old Count: %d\n", strmap_get_count(configoldmap));
  199. strmap_enum(configoldmap, iter_disabled, NULL);
  200. strmap_enum(configmap, iter_enabled, NULL);
  201. //strmap_enum(pkgmap, iter, NULL);
  202. strmap_delete(pkgmap);
  203. strmap_delete(configmap);
  204. strmap_delete(configoldmap);
  205. return(0);
  206. }