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/>.
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
;
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) &&
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
);
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 string
= strstr(buf
, "PKGFB_");
204 if (string
!= NULL
) {
205 tmp
= parse_line(pkgdirp
->d_name
, pkgvar
, string
, 1, 0);
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);
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);
221 strncat(pkgdeps
, tmp
, strlen(tmp
));
227 if (strlen(pkgdeps
) != 0)
228 printf("%s\n", pkgdeps
);
231 if (fclose(pkg
) != 0)
232 perror("Closing file stream failed");
235 if (closedir(pkgdir
) != 0)
236 perror("Closing directory stream failed");