fix target system names
[openadk.git] / tools / adk / depmaker.c
blob21958ffaea2689161bbda0483f95357da6a949a3
1 /*
2 * depmaker - create package/Depends.mk for OpenADK buildsystem
4 * Copyright (C) 2010 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 512
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;
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) &&
160 !(strncmp(pkgdirp->d_name, "libpthread", 10) == 0) &&
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 string = strstr(buf, "PKGFB_");
204 if (string != NULL) {
205 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1, 0);
206 if (tmp != NULL)
207 strncat(pkgdeps, tmp, strlen(tmp));
210 string = strstr(buf, "PKGCB_");
211 if (string != NULL) {
212 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1, 0);
213 if (tmp != NULL)
214 strncat(pkgdeps, tmp, strlen(tmp));
217 string = strstr(buf, "PKGSB_");
218 if (string != NULL) {
219 tmp = parse_line(pkgdirp->d_name, pkgvar, string, 1, 1);
220 if (tmp != NULL) {
221 strncat(pkgdeps, tmp, strlen(tmp));
225 free(tmp);
227 if (strlen(pkgdeps) != 0)
228 printf("%s\n", pkgdeps);
229 free(pkgdeps);
230 free(pkgvar);
231 if (fclose(pkg) != 0)
232 perror("Closing file stream failed");
235 if (closedir(pkgdir) != 0)
236 perror("Closing directory stream failed");
238 return(0);