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/>.
26 #include <sys/types.h>
31 static int prefix
= 0;
32 static int hprefix
= 0;
34 static int check_symbol(char *symbol
) {
41 if ((sym
= malloc(strlen(symbol
) + 2)) != NULL
)
42 memset(sym
, 0, strlen(symbol
) + 2);
44 perror("Can not allocate memory.");
48 strncat(sym
, symbol
, strlen(symbol
));
50 if ((config
= fopen(".config", "r")) == NULL
) {
51 perror("Can not open file \".config\".");
56 while (fgets(buf
, MAXLINE
, config
) != NULL
) {
57 if (strncmp(buf
, sym
, strlen(sym
)) == 0)
62 if (fclose(config
) != 0)
63 perror("Closing file stream failed");
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
;
75 string
[strlen(string
)-1] = '\0';
76 if ((key
= strtok(string
, ":=")) == NULL
) {
77 perror("Can not get key from string.");
83 if ((key_sym
= malloc(MAXLINE
)) != NULL
)
84 memset(key_sym
, 0, MAXLINE
);
86 perror("Can not allocate memory.");
92 if (snprintf(key_sym
, MAXLINE
, "ADK_PACKAGE_%s_", pkgvar
) < 0)
93 perror("Can not create string variable.");
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);
101 if (snprintf(key_sym
, MAXLINE
, "ADK_TARGET_SYSTEM_%s", pkgvar
) < 0)
102 perror("Can not create string variable.");
105 if (snprintf(key_sym
, MAXLINE
, "ADK_TARGET_LIB_%s", pkgvar
) < 0)
106 perror("Can not create string variable.");
109 if (check_symbol(key_sym
) != 0) {
116 if ((pkgdeps
= malloc(MAXLINE
)) != NULL
)
117 memset(pkgdeps
, 0, MAXLINE
);
119 perror("Can not allocate memory.");
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 */
128 if ((depvar
= malloc(MAXLINE
)) != NULL
)
129 memset(depvar
, 0, MAXLINE
);
131 perror("Can not allocate memory.");
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
] == '+')
148 if (depvar
[i
] == '-')
150 depvar
[i
] = toupper(depvar
[i
]);
154 if ((key_sym
= malloc(MAXLINE
)) != NULL
)
155 memset(key_sym
, 0, MAXLINE
);
157 perror("Can not allocate memory.");
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) {
174 if (snprintf(temp
, MAXLINE
, "%s-compile: %s-compile", package
, dep
) < 0)
175 perror("Can not create string variable.");
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
, " ");
189 struct dirent
*pkgdirp
;
193 char *string
, *pkgvar
, *pkgdeps
, *hpkgdeps
= NULL
, *tmp
, *fpkg
, *cpkg
, *spkg
, *key
, *check
, *dpkg
;
201 /* read Makefile's for all packages */
202 pkgdir
= opendir("package");
203 while ((pkgdirp
= readdir(pkgdir
)) != NULL
) {
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");
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
] == '+')
217 if (pkgvar
[i
] == '-')
219 pkgvar
[i
] = toupper(pkgvar
[i
]);
222 /* exclude manual maintained packages from package/Makefile */
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
);
235 perror("Can not allocate memory.");
241 /* generate build dependencies */
242 while (fgets(buf
, MAXLINE
, pkg
) != NULL
) {
243 if ((tmp
= malloc(MAXLINE
)) != NULL
)
244 memset(tmp
, 0 , MAXLINE
);
246 perror("Can not allocate memory.");
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
);
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
);
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
, ":=");
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
);
282 tmp
= parse_line(pkgdirp
->d_name
, dpkg
, stringtmp
, 1, 0, 1, &prefix
);
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
, ":=");
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
);
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
, ":=");
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
);
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
, ":=");
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
);
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
);
347 asprintf(&string
, "%s%s",
348 hpkgdeps
? hpkgdeps
: "",
356 if (strlen(pkgdeps
) != 0)
357 printf("%s\n", pkgdeps
);
358 if (hpkgdeps
&& *hpkgdeps
)
359 printf("%s\n", hpkgdeps
);
364 if (fclose(pkg
) != 0)
365 perror("Closing file stream failed");
368 if (closedir(pkgdir
) != 0)
369 perror("Closing directory stream failed");