the optional host-tool detection support is still broken :(
[openadk.git] / adk / tools / depmaker.c
blobad2328d9e2ed90adc520cc89cb18d486514d9b0c
1 /*
2 * depmaker - create package/Depends.mk for OpenADK buildsystem
4 * Copyright (C) 2010-2015 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 #define _GNU_SOURCE
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #define MAXLINE 1024
29 #define MAXPATH 128
31 static int prefix = 0;
32 static int hprefix = 0;
34 static int check_symbol(char *symbol) {
36 FILE *config;
37 char buf[MAXLINE];
38 char *sym;
39 int ret;
41 if ((sym = malloc(strlen(symbol) + 2)) != NULL)
42 memset(sym, 0, strlen(symbol) + 2);
43 else {
44 perror("Can not allocate memory.");
45 exit(EXIT_FAILURE);
48 strncat(sym, symbol, strlen(symbol));
49 strncat(sym, "=", 1);
50 if ((config = fopen(".config", "r")) == NULL) {
51 perror("Can not open file \".config\".");
52 exit(EXIT_FAILURE);
55 ret = 1;
56 while (fgets(buf, MAXLINE, config) != NULL) {
57 if (strncmp(buf, sym, strlen(sym)) == 0)
58 ret = 0;
61 free(sym);
62 if (fclose(config) != 0)
63 perror("Closing file stream failed");
65 return(ret);
68 /*@null@*/
69 static char *parse_line(char *package, char *pkgvar, char *string, int checksym, int pprefix, int system, int *prefixp) {
71 char *key, *value, *dep, *key_sym, *pkgdeps, *depvar;
72 char temp[MAXLINE];
73 int i;
75 string[strlen(string)-1] = '\0';
76 if ((key = strtok(string, ":=")) == NULL) {
77 perror("Can not get key from string.");
78 exit(EXIT_FAILURE);
81 if (checksym == 1) {
82 /* extract symbol */
83 if ((key_sym = malloc(MAXLINE)) != NULL)
84 memset(key_sym, 0, MAXLINE);
85 else {
86 perror("Can not allocate memory.");
87 exit(EXIT_FAILURE);
89 switch(system) {
90 case 0:
91 if (pprefix == 0) {
92 if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0)
93 perror("Can not create string variable.");
94 } else {
95 if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0)
96 perror("Can not create string variable.");
98 strncat(key_sym, key+6, strlen(key)-6);
99 break;
100 case 1:
101 if (snprintf(key_sym, MAXLINE, "ADK_TARGET_SYSTEM_%s", pkgvar) < 0)
102 perror("Can not create string variable.");
103 break;
104 case 2:
105 if (snprintf(key_sym, MAXLINE, "ADK_TARGET_LIB_%s", pkgvar) < 0)
106 perror("Can not create string variable.");
107 break;
109 if (check_symbol(key_sym) != 0) {
110 free(key_sym);
111 return(NULL);
113 free(key_sym);
116 if ((pkgdeps = malloc(MAXLINE)) != NULL)
117 memset(pkgdeps, 0, MAXLINE);
118 else {
119 perror("Can not allocate memory.");
120 exit(EXIT_FAILURE);
123 value = strtok(NULL, "=\t");
124 dep = strtok(value, " ");
125 while (dep != NULL) {
126 /* check only for optional host tools, if they are required to build */
127 if (checksym == 2) {
128 if ((depvar = malloc(MAXLINE)) != NULL)
129 memset(depvar, 0, MAXLINE);
130 else {
131 perror("Can not allocate memory.");
132 exit(EXIT_FAILURE);
134 strncat(depvar, dep, strlen(dep)-5);
135 if ((strncmp(depvar, "bc", 2) == 0) ||
136 (strncmp(depvar, "bzip2", 5) == 0) ||
137 (strncmp(depvar, "file", 4) == 0) ||
138 (strncmp(depvar, "gawk", 4) == 0) ||
139 (strncmp(depvar, "grep", 4) == 0) ||
140 (strncmp(depvar, "patch", 5) == 0) ||
141 (strncmp(depvar, "sed", 3) == 0) ||
142 (strncmp(depvar, "xz", 2) == 0)) {
144 /* transform to uppercase variable name */
145 for (i=0; i<(int)strlen(depvar); i++) {
146 if (depvar[i] == '+')
147 depvar[i] = 'X';
148 if (depvar[i] == '-')
149 depvar[i] = '_';
150 depvar[i] = toupper(depvar[i]);
153 /* extract symbol */
154 if ((key_sym = malloc(MAXLINE)) != NULL)
155 memset(key_sym, 0, MAXLINE);
156 else {
157 perror("Can not allocate memory.");
158 exit(EXIT_FAILURE);
160 if (snprintf(key_sym, MAXLINE, "ADK_HOST_BUILD_%s", depvar) < 0)
161 perror("Can not create string variable.");
163 if (check_symbol(key_sym) != 0) {
164 free(key_sym);
165 free(depvar);
166 return(NULL);
168 free(key_sym);
169 free(depvar);
172 if (*prefixp == 0) {
173 *prefixp = 1;
174 if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0)
175 perror("Can not create string variable.");
176 } else {
177 if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0)
178 perror("Can not create string variable.");
180 strncat(pkgdeps, temp, strlen(temp));
181 dep = strtok(NULL, " ");
183 return(pkgdeps);
186 int main() {
188 DIR *pkgdir;
189 struct dirent *pkgdirp;
190 FILE *pkg;
191 char buf[MAXLINE];
192 char path[MAXPATH];
193 char *string, *pkgvar, *pkgdeps, *hpkgdeps = NULL, *tmp, *fpkg, *cpkg, *spkg, *key, *check, *dpkg;
194 char *stringtmp;
195 int i;
197 spkg = NULL;
198 cpkg = NULL;
199 fpkg = NULL;
201 /* read Makefile's for all packages */
202 pkgdir = opendir("package");
203 while ((pkgdirp = readdir(pkgdir)) != NULL) {
204 /* skip dotfiles */
205 if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
206 if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
207 perror("Can not create string variable.");
208 pkg = fopen(path, "r");
209 if (pkg == NULL)
210 continue;
212 /* transform to uppercase variable name */
213 pkgvar = strdup(pkgdirp->d_name);
214 for (i=0; i<(int)strlen(pkgvar); i++) {
215 if (pkgvar[i] == '+')
216 pkgvar[i] = 'X';
217 if (pkgvar[i] == '-')
218 pkgvar[i] = '_';
219 pkgvar[i] = toupper(pkgvar[i]);
222 /* exclude manual maintained packages from package/Makefile */
223 if (
224 !(strncmp(pkgdirp->d_name, "uclibc-ng", 9) == 0 && strlen(pkgdirp->d_name) == 9) &&
225 !(strncmp(pkgdirp->d_name, "musl", 4) == 0) &&
226 !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) {
227 /* print result to stdout */
228 printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name);
229 printf("hostpackage-$(ADK_HOST_BUILD_%s) += %s\n", pkgvar, pkgdirp->d_name);
232 if ((pkgdeps = malloc(MAXLINE)) != NULL)
233 memset(pkgdeps, 0, MAXLINE);
234 else {
235 perror("Can not allocate memory.");
236 exit(EXIT_FAILURE);
238 prefix = 0;
239 hprefix = 0;
241 /* generate build dependencies */
242 while (fgets(buf, MAXLINE, pkg) != NULL) {
243 if ((tmp = malloc(MAXLINE)) != NULL)
244 memset(tmp, 0 , MAXLINE);
245 else {
246 perror("Can not allocate memory.");
247 exit(EXIT_FAILURE);
250 /* just read variables prefixed with PKG */
251 if (strncmp(buf, "PKG", 3) == 0) {
253 string = strstr(buf, "PKG_BUILDDEP:=");
254 if (string != NULL) {
255 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
256 if (tmp != NULL) {
257 strncat(pkgdeps, tmp, strlen(tmp));
261 string = strstr(buf, "PKG_BUILDDEP+=");
262 if (string != NULL) {
263 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0, 0, &prefix);
264 if (tmp != NULL)
265 strncat(pkgdeps, tmp, strlen(tmp));
268 // We need to find the system or libc name here
269 string = strstr(buf, "PKG_BUILDDEP_");
270 if (string != NULL) {
271 check = strstr(buf, ":=");
272 if (check != NULL) {
273 stringtmp = strdup(string);
274 string[strlen(string)-1] = '\0';
275 key = strtok(string, ":=");
276 dpkg = strdup(key+13);
277 if (strncmp("UCLIBC_NG", dpkg, 9) == 0) {
278 tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 2, &prefix);
279 } else if (strncmp("MUSL", dpkg, 4) == 0) {
280 tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 2, &prefix);
281 } else {
282 tmp = parse_line(pkgdirp->d_name, dpkg, stringtmp, 1, 0, 1, &prefix);
284 if (tmp != NULL)
285 strncat(pkgdeps, tmp, strlen(tmp));
289 // We need to find the subpackage name here
290 string = strstr(buf, "PKG_FLAVOURS_");
291 if (string != NULL) {
292 check = strstr(buf, ":=");
293 if (check != NULL) {
294 string[strlen(string)-1] = '\0';
295 key = strtok(string, ":=");
296 fpkg = strdup(key+13);
300 string = strstr(buf, "PKGFB_");
301 if (string != NULL) {
302 tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0, 0, &prefix);
303 if (tmp != NULL)
304 strncat(pkgdeps, tmp, strlen(tmp));
307 // We need to find the subpackage name here
308 string = strstr(buf, "PKG_CHOICES_");
309 if (string != NULL) {
310 check = strstr(buf, ":=");
311 if (check != NULL) {
312 string[strlen(string)-1] = '\0';
313 key = strtok(string, ":=");
314 cpkg = strdup(key+12);
317 string = strstr(buf, "PKGCB_");
318 if (string != NULL) {
319 tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0, 0, &prefix);
320 if (tmp != NULL)
321 strncat(pkgdeps, tmp, strlen(tmp));
324 // We need to find the subpackage name here
325 string = strstr(buf, "PKG_SUBPKGS_");
326 if (string != NULL) {
327 check = strstr(buf, ":=");
328 if (check != NULL) {
329 string[strlen(string)-1] = '\0';
330 key = strtok(string, ":=");
331 spkg = strdup(key+12);
335 string = strstr(buf, "PKGSB_");
336 if (string != NULL) {
337 tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1, 0, &prefix);
338 if (tmp != NULL) {
339 strncat(pkgdeps, tmp, strlen(tmp));
342 } else if (strncmp(buf, "HOST_BUILDDEP", 13) == 0) {
343 asprintf(&string, "%s-host", pkgdirp->d_name);
344 // check retval; string for NULL
345 tmp = parse_line(string, NULL, buf, 2, 0, 0, &hprefix);
346 if (tmp && *tmp) {
347 asprintf(&string, "%s%s",
348 hpkgdeps ? hpkgdeps : "",
349 tmp);
350 free(hpkgdeps);
351 hpkgdeps = string;
354 free(tmp);
356 if (strlen(pkgdeps) != 0)
357 printf("%s\n", pkgdeps);
358 if (hpkgdeps && *hpkgdeps)
359 printf("%s\n", hpkgdeps);
360 free(hpkgdeps);
361 hpkgdeps = NULL;
362 free(pkgdeps);
363 free(pkgvar);
364 if (fclose(pkg) != 0)
365 perror("Closing file stream failed");
368 if (closedir(pkgdir) != 0)
369 perror("Closing directory stream failed");
371 return(0);