mkdep.c revision 1.21 1 /* $NetBSD: mkdep.c,v 1.21 2003/12/07 20:22:49 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #if !defined(lint)
45 __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
46 All rights reserved.\n");
47 __RCSID("$NetBSD: mkdep.c,v 1.21 2003/12/07 20:22:49 dsl Exp $");
48 #endif /* not lint */
49
50 #include <sys/mman.h>
51 #include <sys/param.h>
52 #include <sys/wait.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <fcntl.h>
56 #include <locale.h>
57 #include <paths.h>
58 #include <signal.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include "findcc.h"
65
66 typedef struct opt opt_t;
67 struct opt {
68 opt_t *left;
69 opt_t *right;
70 int len;
71 int count;
72 char name[4];
73 };
74
75 /* tree of includes for -o processing */
76 opt_t *opt;
77 int width;
78
79 #define DEFAULT_PATH _PATH_DEFPATH
80 #define DEFAULT_FILENAME ".depend"
81
82 static void save_for_optional(const char *, const char *);
83 static int write_optional(int, opt_t *, int);
84
85
86 static inline void *
87 deconst(const void *p)
88 {
89 return (const char *)p - (const char *)0 + (char *)0;
90 }
91
92 static void
93 usage(void)
94 {
95 (void)fprintf(stderr,
96 "usage: %s [-adopq] [-f file] [-s suffix_list] flags file ...\n",
97 getprogname());
98 exit(EXIT_FAILURE);
99 }
100
101 static int
102 run_cc(int argc, char **argv, const char **fname)
103 {
104 const char *CC, *pathname, *tmpdir;
105 static char tmpfilename[MAXPATHLEN];
106 char **args;
107 int tmpfd;
108 pid_t pid, cpid;
109 int status;
110
111 if ((CC = getenv("CC")) == NULL)
112 CC = DEFAULT_CC;
113 if ((pathname = findcc(CC)) == NULL)
114 if (!setenv("PATH", DEFAULT_PATH, 1))
115 pathname = findcc(CC);
116 if (pathname == NULL)
117 err(EXIT_FAILURE, "%s: not found", CC);
118 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL)
119 err(EXIT_FAILURE, "malloc");
120
121 args[0] = deconst(CC);
122 args[1] = deconst("-M");
123 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
124
125 if ((tmpdir = getenv("TMPDIR")) == NULL)
126 tmpdir = _PATH_TMP;
127 (void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
128 "mkdepXXXXXX");
129 if ((tmpfd = mkstemp(tmpfilename)) < 0) {
130 warn("unable to create temporary file %s", tmpfilename);
131 exit(EXIT_FAILURE);
132 }
133 (void)unlink(tmpfilename);
134 *fname = tmpfilename;
135
136 switch (cpid = vfork()) {
137 case 0:
138 (void)dup2(tmpfd, STDOUT_FILENO);
139 (void)close(tmpfd);
140
141 (void)execv(pathname, args);
142 _exit(EXIT_FAILURE);
143 /* NOTREACHED */
144
145 case -1:
146 err(EXIT_FAILURE, "unable to fork");
147 }
148
149 while (((pid = wait(&status)) != cpid) && (pid >= 0))
150 continue;
151
152 if (status)
153 errx(EXIT_FAILURE, "compile failed.");
154
155 return tmpfd;
156 }
157
158 int
159 main(int argc, char **argv)
160 {
161 int aflag, dflag, oflag, qflag;
162 const char *filename;
163 int dependfile;
164 char *buf, *lim, *ptr, *line, *suf, *colon, *eol;
165 int ok_ind, ch;
166 int sz;
167 int fd;
168 const char *fname;
169 const char *suffixes = NULL, *s, *s1;
170
171 setlocale(LC_ALL, "");
172 setprogname(argv[0]);
173
174 aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC;
175 dflag = 0;
176 oflag = 0;
177 qflag = 0;
178 filename = DEFAULT_FILENAME;
179 dependfile = -1;
180
181 opterr = 0; /* stop getopt() bleating about errors. */
182 for (;;) {
183 ok_ind = optind;
184 ch = getopt(argc, argv, "adf:opqs:");
185 switch (ch) {
186 case -1:
187 ok_ind = optind;
188 break;
189 case 'a': /* Append to output file */
190 aflag &= ~O_TRUNC;
191 continue;
192 case 'd': /* Process *.d files (don't run cc -M) */
193 dflag = 1;
194 opterr = 1;
195 continue;
196 case 'f': /* Name of output file */
197 filename = optarg;
198 continue;
199 case 'o': /* Mark dependant files .OPTIONAL */
200 oflag = 1;
201 continue;
202 case 'p': /* Program mode (x.o: -> x:) */
203 suffixes = "";
204 continue;
205 case 'q': /* Quiet */
206 qflag = 1;
207 continue;
208 case 's': /* Suffix list */
209 suffixes = optarg;
210 continue;
211 default:
212 if (dflag)
213 usage();
214 /* Unknown arguments are passed to "${CC} -M" */
215 break;
216 }
217 break;
218 }
219
220 argc -= ok_ind;
221 argv += ok_ind;
222 if (argc == 0 && !dflag)
223 usage();
224
225 dependfile = open(filename, aflag, 0666);
226 if (dependfile == -1)
227 err(EXIT_FAILURE, "unable to %s to file %s\n",
228 aflag & O_TRUNC ? "write" : "append", filename);
229
230 for (; *argv != NULL; argv++) {
231 if (dflag) {
232 fname = *argv;
233 fd = open(fname, O_RDONLY, 0);
234 if (fd == -1) {
235 if (!qflag)
236 warn("ignoring %s", fname);
237 continue;
238 }
239 } else {
240 fd = run_cc(argc, argv, &fname);
241 /* consume all args... */
242 argv += argc - 1;
243 }
244
245 sz = lseek(fd, 0, SEEK_END);
246 if (sz == 0) {
247 close(fd);
248 continue;
249 }
250 buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
251 close(fd);
252
253 if (buf == MAP_FAILED)
254 err(EXIT_FAILURE, "unable to mmap file %s", *argv);
255 lim = buf + sz - 1;
256
257 /* Remove leading "./" from filenames */
258 for (ptr = buf; ptr < lim; ptr++) {
259 if (ptr[1] != '.' || ptr[2] != '/'
260 || !isspace((unsigned char)ptr[0]))
261 continue;
262 ptr[1] = ' ';
263 ptr[2] = ' ';
264 }
265
266 for (line = eol = buf; eol <= lim;) {
267 while (eol <= lim && *eol++ != '\n')
268 continue;
269 if (line == eol - 1) {
270 /* empty line - ignore */
271 line = eol;
272 continue;
273 }
274 if (eol[-2] == '\\')
275 /* Assemble continuation lines */
276 continue;
277 for (colon = line; *colon != ':'; colon++) {
278 if (colon >= eol) {
279 colon = NULL;
280 break;
281 }
282 }
283 if (colon == NULL) {
284 /* No dependency - just transcribe line */
285 write(dependfile, line, eol - line);
286 line = eol;
287 continue;
288 }
289 s = suffixes;
290 if (s != NULL) {
291 /* Find the .o: */
292 for (suf = colon - 2; ; suf--) {
293 if (suf <= line) {
294 colon = NULL;
295 break;
296 }
297 if (isspace((unsigned char)suf[1]))
298 continue;
299 if (suf[0] != '.' || suf[1] != 'o')
300 /* not a file.o: line */
301 s = NULL;
302 break;
303 }
304 }
305 if (s != NULL) {
306 for (; ; s = s1 + 1) {
307 s1 = strpbrk(s, ", ");
308 if (s1 == NULL)
309 s1 = s + strlen(s);
310 write(dependfile, line, suf - line);
311 write(dependfile, s, s1 - s);
312 if (*s1 == 0)
313 break;
314 write(dependfile, " ", 1);
315 }
316 write(dependfile, colon, eol - colon);
317 } else
318 write(dependfile, line, eol - line);
319
320 if (oflag)
321 save_for_optional(colon + 1, eol);
322 line = eol;
323 }
324 munmap(buf, sz);
325 }
326
327 if (oflag && opt != NULL) {
328 write(dependfile, ".OPTIONAL:", 10);
329 width = 9;
330 sz = write_optional(dependfile, opt, 0);
331 /* 'depth' is about 39 for an i386 kernel */
332 /* fprintf(stderr, "Recursion depth %d\n", sz); */
333 }
334 close(dependfile);
335
336 exit(EXIT_SUCCESS);
337 }
338
339
340 /*
341 * Only save each file once - the kernel .depend is 3MB and there is
342 * no point doubling its size.
343 * The data seems to be 'random enough' so the simple binary tree
344 * only has a reasonable depth.
345 */
346 static void
347 save_for_optional(const char *start, const char *limit)
348 {
349 opt_t **l, *n;
350 const char *name, *end;
351 int c;
352
353 while (start < limit && strchr(" \t\n\\", *start))
354 start++;
355 for (name = start; ; name = end) {
356 while (name < limit && strchr(" \t\n\\", *name))
357 name++;
358 for (end = name; end < limit && !strchr(" \t\n\\", *end);)
359 end++;
360 if (name >= limit)
361 break;
362 if (end[-1] == 'c' && end[-2] == '.' && name == start)
363 /* ignore dependency on the files own .c */
364 continue;
365 for (l = &opt;;) {
366 n = *l;
367 if (n == NULL) {
368 n = malloc(sizeof *n + (end - name));
369 n->left = n->right = 0;
370 n->len = end - name;
371 n->count = 1;
372 n->name[0] = ' ';
373 memcpy(n->name + 1, name, end - name);
374 *l = n;
375 break;
376 }
377 c = (end - name) - n->len;
378 if (c == 0)
379 c = memcmp(n->name + 1, name, (end - name));
380 if (c == 0) {
381 /* Duplicate */
382 n->count++;
383 break;
384 }
385 if (c < 0)
386 l = &n->left;
387 else
388 l = &n->right;
389 }
390 }
391 }
392
393 static int
394 write_optional(int fd, opt_t *node, int depth)
395 {
396 int d1 = ++depth;
397
398 if (node->left)
399 d1 = write_optional(fd, node->left, d1);
400 if (width > 76 - node->len) {
401 write(fd, " \\\n ", 4);
402 width = 1;
403 }
404 width += 1 + node->len;
405 write(fd, node->name, 1 + node->len);
406 if (node->right)
407 depth = write_optional(fd, node->right, depth);
408 return d1 > depth ? d1 : depth;
409 }
410