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/>.
25 #include <sys/types.h>
30 static int prefix
= 0;
32 static int check_symbol(char *symbol
) {
39 if ((sym
= malloc(strlen(symbol
) + 2)) != NULL
)
40 memset(sym
, 0, strlen(symbol
) + 2);
42 perror("Can not allocate memory.");
46 strncat(sym
, symbol
, strlen(symbol
));
48 if ((config
= fopen(".config", "r")) == NULL
) {
49 perror("Can not open file \".config\".");
54 while (fgets(buf
, MAXLINE
, config
) != NULL
) {
55 if (strncmp(buf
, sym
, strlen(sym
)) == 0)
60 if (fclose(config
) != 0)
61 perror("Closing file stream failed");
67 static char *parse_line(char *package
, char *pkgvar
, char *string
, int checksym
, int pprefix
) {
69 char *key
, *value
, *dep
, *key_sym
, *pkgdeps
;
72 string
[strlen(string
)-1] = '\0';
73 if ((key
= strtok(string
, ":=")) == NULL
) {
74 perror("Can not get key from string.");
80 if ((key_sym
= malloc(MAXLINE
)) != NULL
)
81 memset(key_sym
, 0, MAXLINE
);
83 perror("Can not allocate memory.");
87 if (snprintf(key_sym
, MAXLINE
, "ADK_PACKAGE_%s_", pkgvar
) < 0)
88 perror("Can not create string variable.");
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) {
102 if ((pkgdeps
= malloc(MAXLINE
)) != NULL
)
103 memset(pkgdeps
, 0, MAXLINE
);
105 perror("Can not allocate memory.");
109 value
= strtok(NULL
, "=\t");
110 dep
= strtok(value
, " ");
111 while (dep
!= NULL
) {
114 if (snprintf(temp
, MAXLINE
, "%s-compile: %s-compile", package
, dep
) < 0)
115 perror("Can not create string variable.");
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
, " ");
129 struct dirent
*pkgdirp
;
133 char *string
, *pkgvar
, *pkgdeps
, *tmp
, *fpkg
, *cpkg
, *spkg
, *key
, *check
;
136 /* read Makefile's for all packages */
137 pkgdir
= opendir("package");
138 while ((pkgdirp
= readdir(pkgdir
)) != NULL
) {
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");
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
] == '+')
152 if (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
);
171 perror("Can not allocate memory.");
176 /* generate build dependencies */
177 while (fgets(buf
, MAXLINE
, pkg
) != NULL
) {
178 if ((tmp
= malloc(MAXLINE
)) != NULL
)
179 memset(tmp
, 0 , MAXLINE
);
181 perror("Can not allocate memory.");
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);
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);
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
, ":=");
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);
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
, ":=");
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);
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
, ":=");
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);
253 strncat(pkgdeps
, tmp
, strlen(tmp
));
259 if (strlen(pkgdeps
) != 0)
260 printf("%s\n", pkgdeps
);
263 if (fclose(pkg
) != 0)
264 perror("Closing file stream failed");
267 if (closedir(pkgdir
) != 0)
268 perror("Closing directory stream failed");