2 * Copyright (c) 2006-2008 Ed Schouten <ed@80386.nl>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/types.h>
38 struct header_depend
*depends
;
42 struct header_depend
{
43 struct header
*header
;
44 struct header_depend
*next
;
47 struct header
*headerlist
= NULL
;
50 depend_add(struct header
*from
, struct header
*to
)
52 struct header_depend
*hd
, *hdp
, *hdn
;
54 /* We can't depend on ourself. Prevent duplicates. */
57 for (hd
= to
->depends
; hd
!= NULL
; hd
= hd
->next
) {
58 if (hd
->header
== from
)
63 hd
= malloc(sizeof(struct header_depend
));
67 if (hdp
== NULL
|| strcmp(hd
->header
->filename
,
68 hdp
->header
->filename
) < 0) {
69 /* Add it to the beginning */
73 /* Add it after an existing item */
74 for (hdp
= to
->depends
; hdp
!= NULL
; hdp
= hdp
->next
) {
76 if (hdn
== NULL
|| strcmp(hd
->header
->filename
,
77 hdn
->header
->filename
) < 0) {
78 /* Insert it in between */
90 depend_copy(struct header
*from
, struct header
*to
)
92 struct header_depend
*hd
;
94 if (depend_add(from
, to
) != 0)
96 for (hd
= from
->depends
; hd
!= NULL
; hd
= hd
->next
)
97 depend_add(hd
->header
, to
);
100 static struct header
*
101 file_scan(const char *filename
)
104 struct header
*h
, *hp
, *hn
, *hs
;
105 char fbuf
[2048], *eol
;
108 if (strcmp(filename
, "conftest.c") == 0 ||
109 strcmp(filename
, "stdinc.h") == 0)
114 cmp
= strcmp(filename
, hp
->filename
);
117 } else if (cmp
< 0) {
118 /* Item cannot be in the list - add to beginning */
121 /* Item may be in the list - look it up */
122 for (hp
= headerlist
; hp
!= NULL
; hp
= hp
->next
) {
126 cmp
= strcmp(filename
, hn
->filename
);
137 fp
= fopen(filename
, "r");
139 fprintf(stderr
, "Failed to open %s\n", filename
);
143 /* We can read it - we must track it */
144 h
= malloc(sizeof(struct header
));
145 h
->filename
= strdup(filename
);
149 /* Add it alphabetically to the list */
151 h
->next
= headerlist
;
159 while (fgets(fbuf
, sizeof fbuf
, fp
) != NULL
) {
160 if (strncmp(fbuf
, "#include \"", 10) == 0) {
161 eol
= strchr(fbuf
+ 10, '"');
166 hs
= file_scan(fbuf
+ 10);
176 main(int argc
, char *argv
[])
184 struct header_depend
*hd
;
186 srcdir
= opendir(".");
187 if (srcdir
== NULL
) {
188 fprintf(stderr
, "Failed to open current directory\n");
192 while ((fent
= readdir(srcdir
)) != NULL
) {
193 /* Only scan C source code */
194 ext
= strchr(fent
->d_name
, '.');
195 if (ext
== NULL
|| strcmp(ext
, ".c") != 0)
197 file_scan(fent
->d_name
);
201 out
= fopen("../depends", "w");
203 fprintf(stderr
, "Failed to open output file\n");
207 for (h
= headerlist
; h
!= NULL
; h
= h
->next
) {
208 /* Only print C source code - skip conftest */
209 ext
= strchr(h
->filename
, '.');
210 if (ext
== NULL
|| strcmp(ext
, ".c") != 0)
212 fprintf(out
, "DEPENDS_");
213 fwrite(h
->filename
, 1, ext
- h
->filename
, out
);
216 for (hd
= h
->depends
; hd
!= NULL
; hd
= hd
->next
) {
217 /* Only print header files */
218 ext
= strchr(hd
->header
->filename
, '.');
219 if (ext
== NULL
|| strcmp(ext
, ".h") != 0)
225 fwrite(hd
->header
->filename
, 1,
226 ext
- hd
->header
->filename
, out
);
228 fprintf(out
, "\"\n");
231 /* Regular Makefile-style output */
232 ext
= strchr(h
->filename
, '.');
233 if (ext
== NULL
|| strcmp(ext
, ".c") != 0)
235 fwrite(h
->filename
, 1, ext
- h
->filename
, out
);
236 fprintf(out
, ".o: %s", h
->filename
);
237 for (hd
= h
->depends
; hd
!= NULL
; hd
= hd
->next
)
238 fprintf(out
, " %s", hd
->header
->filename
);