add tools/cdrtools
[openadk.git] / tools / adk / depmaker.c
bloba2291e4b02fd731d76acaae69941e8e2a0beb656
1 /*
2 * depmaker - create package/Depends.mk for OpenADK buildsystem
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 <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
27 #define MAXLINE 1024
28 #define MAXPATH 128
30 static int prefix = 0;
32 static int check_symbol(char *symbol) {
34 FILE *config;
35 char buf[MAXLINE];
36 char *sym;
37 int ret;
39 if ((sym = malloc(strlen(symbol) + 2)) != NULL)
40 memset(sym, 0, strlen(symbol) + 2);
41 else {
42 perror("Can not allocate memory.");
43 exit(EXIT_FAILURE);
46 strncat(sym, symbol, strlen(symbol));
47 strncat(sym, "=", 1);
48 if ((config = fopen(".config", "r")) == NULL) {
49 perror("Can not open file \".config\".");
50 exit(EXIT_FAILURE);
53 ret = 1;
54 while (fgets(buf, MAXLINE, config) != NULL) {
55 if (strncmp(buf, sym, strlen(sym)) == 0)
56 ret = 0;
59 free(sym);
60 if (fclose(config) != 0)
61 perror("Closing file stream failed");
63 return(ret);
66 /*@null@*/
67 static char *parse_line(char *package, char *pkgvar, char *string, int checksym, int pprefix) {
69 char *key, *value, *dep, *key_sym, *pkgdeps;
70 char temp[MAXLINE];
72 string[strlen(string)-1] = '\0';
73 if ((key = strtok(string, ":=")) == NULL) {
74 perror("Can not get key from string.");
75 exit(EXIT_FAILURE);
78 if (checksym == 1) {
79 /* extract symbol */
80 if ((key_sym = malloc(MAXLINE)) != NULL)
81 memset(key_sym, 0, MAXLINE);
82 else {
83 perror("Can not allocate memory.");
84 exit(EXIT_FAILURE);
86 if (pprefix == 0) {
87 if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_%s_", pkgvar) < 0)
88 perror("Can not create string variable.");
89 } else {
90 if (snprintf(key_sym, MAXLINE, "ADK_PACKAGE_") < 0)
91 perror("Can not create string variable.");
94 strncat(key_sym, key+6, strlen(key)-6);
95 if (check_symbol(key_sym) != 0) {
96 free(key_sym);
97 return(NULL);
99 free(key_sym);
102 if ((pkgdeps = malloc(MAXLINE)) != NULL)
103 memset(pkgdeps, 0, MAXLINE);
104 else {
105 perror("Can not allocate memory.");
106 exit(EXIT_FAILURE);
109 value = strtok(NULL, "=\t");
110 dep = strtok(value, " ");
111 while (dep != NULL) {
112 if (prefix == 0) {
113 prefix = 1;
114 if (snprintf(temp, MAXLINE, "%s-compile: %s-compile", package, dep) < 0)
115 perror("Can not create string variable.");
116 } else {
117 if (snprintf(temp, MAXLINE, " %s-compile", dep) < 0)
118 perror("Can not create string variable.");
120 strncat(pkgdeps, temp, strlen(temp));
121 dep = strtok(NULL, " ");
123 return(pkgdeps);
126 int main() {
128 DIR *pkgdir;
129 struct dirent *pkgdirp;
130 FILE *pkg;
131 char buf[MAXLINE];
132 char path[MAXPATH];
133 char *string, *pkgvar, *pkgdeps, *tmp, *fpkg, *cpkg, *spkg, *key, *check;
134 int i;
136 /* read Makefile's for all packages */
137 pkgdir = opendir("package");
138 while ((pkgdirp = readdir(pkgdir)) != NULL) {
139 /* skip dotfiles */
140 if (strncmp(pkgdirp->d_name, ".", 1) > 0) {
141 if (snprintf(path, MAXPATH, "package/%s/Makefile", pkgdirp->d_name) < 0)
142 perror("Can not create string variable.");
143 pkg = fopen(path, "r");
144 if (pkg == NULL)
145 continue;
147 /* transform to uppercase variable name */
148 pkgvar = strdup(pkgdirp->d_name);
149 for (i=0; i<(int)strlen(pkgvar); i++) {
150 if (pkgvar[i] == '+')
151 pkgvar[i] = 'X';
152 if (pkgvar[i] == '-')
153 pkgvar[i] = '_';
154 pkgvar[i] = toupper(pkgvar[i]);
157 /* exclude manual maintained packages from package/Makefile */
158 if (!(strncmp(pkgdirp->d_name, "eglibc", 6) == 0) &&
159 !(strncmp(pkgdirp->d_name, "libc", 4) == 0 && strlen(pkgdirp->d_name) == 4) &&
160 !(strncmp(pkgdirp->d_name, "libpthread", 10) == 0 && strlen(pkgdirp->d_name) == 10) &&
161 !(strncmp(pkgdirp->d_name, "uclibc++", 8) == 0) &&
162 !(strncmp(pkgdirp->d_name, "uclibc", 6) == 0) &&
163 !(strncmp(pkgdirp->d_name, "glibc", 5) == 0)) {
164 /* print result to stdout */
165 printf("package-$(ADK_COMPILE_%s) += %s\n", pkgvar, pkgdirp->d_name);
168 if ((pkgdeps = malloc(MAXLINE)) != NULL)
169 memset(pkgdeps, 0, MAXLINE);
170 else {
171 perror("Can not allocate memory.");
172 exit(EXIT_FAILURE);
174 prefix = 0;
176 /* generate build dependencies */
177 while (fgets(buf, MAXLINE, pkg) != NULL) {
178 if ((tmp = malloc(MAXLINE)) != NULL)
179 memset(tmp, 0 , MAXLINE);
180 else {
181 perror("Can not allocate memory.");
182 exit(EXIT_FAILURE);
185 /* just read variables prefixed with PKG */
186 if (strncmp(buf, "PKG", 3) == 0) {
188 string = strstr(buf, "PKG_BUILDDEP:=");
189 if (string != NULL) {
190 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0);
191 if (tmp != NULL) {
192 strncat(pkgdeps, tmp, strlen(tmp));
196 string = strstr(buf, "PKG_BUILDDEP+=");
197 if (string != NULL) {
198 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 0, 0);
199 if (tmp != NULL)
200 strncat(pkgdeps, tmp, strlen(tmp));
203 // WE need to find the subpackage name here
204 string = strstr(buf, "PKG_FLAVOURS_");
205 if (string != NULL) {
206 check = strstr(buf, ":=");
207 if (check != NULL) {
208 string[strlen(string)-1] = '\0';
209 key = strtok(string, ":=");
210 fpkg = strdup(key+13);
214 string = strstr(buf, "PKGFB_");
215 if (string != NULL) {
216 tmp = parse_line(pkgdirp->d_name, fpkg, string, 1, 0);
217 if (tmp != NULL)
218 strncat(pkgdeps, tmp, strlen(tmp));
221 // WE need to find the subpackage name here
222 string = strstr(buf, "PKG_CHOICES_");
223 if (string != NULL) {
224 check = strstr(buf, ":=");
225 if (check != NULL) {
226 string[strlen(string)-1] = '\0';
227 key = strtok(string, ":=");
228 cpkg = strdup(key+12);
231 string = strstr(buf, "PKGCB_");
232 if (string != NULL) {
233 tmp = parse_line(pkgdirp->d_name, cpkg, string, 1, 0);
234 if (tmp != NULL)
235 strncat(pkgdeps, tmp, strlen(tmp));
238 // WE need to find the subpackage name here
239 string = strstr(buf, "PKG_SUBPKGS_");
240 if (string != NULL) {
241 check = strstr(buf, ":=");
242 if (check != NULL) {
243 string[strlen(string)-1] = '\0';
244 key = strtok(string, ":=");
245 spkg = strdup(key+12);
249 string = strstr(buf, "PKGSB_");
250 if (string != NULL) {
251 tmp = parse_line(pkgdirp->d_name, spkg, string, 1, 1);
252 if (tmp != NULL) {
253 strncat(pkgdeps, tmp, strlen(tmp));
257 free(tmp);
259 if (strlen(pkgdeps) != 0)
260 printf("%s\n", pkgdeps);
261 free(pkgdeps);
262 free(pkgvar);
263 if (fclose(pkg) != 0)
264 perror("Closing file stream failed");
267 if (closedir(pkgdir) != 0)
268 perror("Closing directory stream failed");
270 return(0);