lvm: fix cross-compile on Darwin
[openadk.git] / adk / tools / pkgrebuild.c
blobb4a63d89cc2513a3f3dae22a2f8decca20e2a1dc
1 /*
2 * pkgrebuild - recognize required package rebuilds in OpenADK
4 * Copyright (C) 2010,2011 Waldemar Brodkorb <wbx@openadk.org>
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.
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.
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/>.
20 #include <ctype.h>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
30 #include "strmap.h"
32 StrMap *configmap, *configoldmap, *pkgmap;
35 static void iter(const char *key, const char *value, const void *obj) {
36 fprintf(stderr, "key: %s value: %s\n", key, value);
40 static void iter_disabled(const char *key, const char *value, const void *obj) {
42 char hvalue[256];
43 char tfile[256];
44 int fd;
46 memset(hvalue, 0, 256);
47 if (strmap_exists(configmap, key) == 0) {
48 //fprintf(stderr, "disabled variables: %s\n", key);
49 if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) {
50 //fprintf(stderr, "Symbol is a flavour/choice: %s\n", hvalue);
51 if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0)
52 perror("can not create file variable.");
53 fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
54 close(fd);
60 static void iter_enabled(const char *key, const char *value, const void *obj) {
62 char hvalue[256];
63 char tfile[256];
64 int fd;
66 memset(hvalue, 0, 256);
67 if (strmap_exists(configoldmap, key) == 0) {
68 //fprintf(stderr, "enabled variables: %s\n", key);
69 if (strmap_get(pkgmap, key, hvalue, sizeof(hvalue)) == 1) {
70 //fprintf(stderr, "Symbol is a flavour/choice\n");
71 if (snprintf(tfile, 256, ".rebuild.%s", hvalue) < 0)
72 perror("can not create file variable.");
73 fd = open(tfile, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
74 close(fd);
79 static char *toupperstr(char *string) {
81 int i;
82 char *str;
84 /* transform to uppercase variable name */
85 str = strdup(string);
86 for (i=0; i<(int)strlen(str); i++) {
87 if (str[i] == '+')
88 str[i] = 'X';
89 if (str[i] == '-')
90 str[i] = '_';
91 str[i] = toupper(str[i]);
93 return(str);
98 int main() {
100 FILE *config, *configold, *pkg;
101 char *key, *value, *string, *token, *check;
102 char *pkg_name, *keystr, *realpkgname;
103 char buf[128];
104 char path[320];
105 char pbuf[320];
106 DIR *pkgdir;
107 struct dirent *pkgdirp;
109 pkg_name = NULL;
110 /* read Makefile's for all packages */
111 pkgmap = strmap_new(1024);
112 pkgdir = opendir("package");
113 while ((pkgdirp = readdir(pkgdir)) != NULL) {
114 /* skip dotfiles */
115 if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
116 if (snprintf(path, 320, "package/%s/Makefile", pkgdirp->d_name) < 0)
117 perror("can not create path variable.");
118 pkg = fopen(path, "r");
119 if (pkg == NULL)
120 continue;
122 while (fgets(pbuf, 320, pkg) != NULL) {
123 if (strncmp(pbuf, "PKG", 3) == 0) {
124 string = strstr(pbuf, "PKG_NAME:=");
125 if (string != NULL) {
126 string[strlen(string)-1] = '\0';
127 key = strtok(string, ":=");
128 value = strtok(NULL, "=\t");
129 if (value != NULL)
130 pkg_name = strdup(value);
132 string = strstr(pbuf, "PKG_SUBPKGS:=");
133 if (string != NULL) {
134 string[strlen(string)-1] = '\0';
135 key = strtok(string, ":=");
136 value = strtok(NULL, "=\t");
137 token = strtok(value, " ");
138 while (token != NULL) {
139 keystr = malloc(256);
140 memset(keystr, 0, 256);
141 strncat(keystr, "ADK_PACKAGE_", 12);
142 strncat(keystr, token, strlen(token));
143 strmap_put(pkgmap, keystr, pkgdirp->d_name);
144 token = strtok(NULL, " ");
145 free(keystr);
146 keystr = NULL;
149 string = strstr(pbuf, "PKG_SUBPKGS+=");
150 if (string != NULL) {
151 string[strlen(string)-1] = '\0';
152 key = strtok(string, "+=");
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, token, strlen(token));
160 strmap_put(pkgmap, keystr, pkgdirp->d_name);
161 token = strtok(NULL, " ");
162 free(keystr);
163 keystr = NULL;
166 string = strstr(pbuf, "PKG_FLAVOURS_");
167 if (string != NULL) {
168 check = strstr(pbuf, ":=");
169 if (check != NULL) {
170 string[strlen(string)-1] = '\0';
171 key = strtok(string, ":=");
172 realpkgname = strdup(key+13);
173 value = strtok(NULL, "=\t");
174 token = strtok(value, " ");
175 while (token != NULL) {
176 keystr = malloc(256);
177 memset(keystr, 0, 256);
178 strncat(keystr, "ADK_PACKAGE_", 12);
179 strncat(keystr, realpkgname, strlen(realpkgname));
180 strncat(keystr, "_", 1);
181 strncat(keystr, token, strlen(token));
182 strmap_put(pkgmap, keystr, pkgdirp->d_name);
183 token = strtok(NULL, " ");
184 free(keystr);
185 keystr = NULL;
187 } else {
188 string[strlen(string)-1] = '\0';
189 key = strtok(string, "+=");
190 realpkgname = strdup(key+13);
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, realpkgname, strlen(realpkgname));
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;
207 string = strstr(pbuf, "PKG_CHOICES_");
208 if (string != NULL) {
209 string[strlen(string)-1] = '\0';
210 key = strtok(string, ":=");
211 value = strtok(NULL, "=\t");
212 token = strtok(value, " ");
213 while (token != NULL) {
214 keystr = malloc(256);
215 memset(keystr, 0, 256);
216 strncat(keystr, "ADK_PACKAGE_", 12);
217 strncat(keystr, toupperstr(pkg_name), strlen(pkg_name));
218 strncat(keystr, "_", 1);
219 strncat(keystr, token, strlen(token));
220 strmap_put(pkgmap, keystr, pkgdirp->d_name);
221 token = strtok(NULL, " ");
222 free(keystr);
223 keystr = NULL;
228 fclose(pkg);
231 closedir(pkgdir);
233 config = fopen(".config", "r");
234 if (config == NULL) {
235 perror(".config is missing.");
236 exit(1);
239 configmap = strmap_new(1024);
240 while (fgets(buf, 128, config) != NULL) {
241 if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
242 key = strtok(buf, "=");
243 value = strtok(NULL, "=");
244 strmap_put(configmap, key, value);
247 fclose(config);
249 configold = fopen(".config.old", "r");
250 if (configold == NULL) {
251 perror(".config.old is missing.");
252 exit(1);
255 configoldmap = strmap_new(1024);
256 while (fgets(buf, 128, configold) != NULL) {
257 if (strncmp(buf, "ADK_PACKAGE", 11) == 0) {
258 key = strtok(buf, "=");
259 value = strtok(NULL, "=");
260 strmap_put(configoldmap, key, value);
263 fclose(configold);
265 //fprintf(stdout, "Config Count: %d\n", strmap_get_count(configmap));
266 //fprintf(stdout, "Config Old Count: %d\n", strmap_get_count(configoldmap));
268 strmap_enum(configoldmap, iter_disabled, NULL);
269 strmap_enum(configmap, iter_enabled, NULL);
270 //strmap_enum(pkgmap, iter, NULL);
272 strmap_delete(pkgmap);
273 strmap_delete(configmap);
274 strmap_delete(configoldmap);
276 return(0);