Home | History | Annotate | Line # | Download | only in mkdep
mkdep.c revision 1.37
      1 /* $NetBSD: mkdep.c,v 1.37 2011/05/30 22:52:12 christos 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     36 #include <sys/cdefs.h>
     37 #if !defined(lint)
     38 __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\
     39  All rights reserved.");
     40 __RCSID("$NetBSD: mkdep.c,v 1.37 2011/05/30 22:52:12 christos Exp $");
     41 #endif /* not lint */
     42 
     43 #include <sys/mman.h>
     44 #include <sys/param.h>
     45 #include <sys/wait.h>
     46 #include <ctype.h>
     47 #include <err.h>
     48 #include <fcntl.h>
     49 #include <getopt.h>
     50 #include <locale.h>
     51 #include <paths.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 #include "findcc.h"
     58 
     59 typedef struct opt opt_t;
     60 struct opt {
     61 	opt_t	*left;
     62 	opt_t	*right;
     63 	int	len;
     64 	int	count;
     65 	char	name[4];
     66 };
     67 
     68 typedef struct suff_list {
     69 	size_t	len;
     70 	char	*suff;
     71 	struct suff_list *next;
     72 } suff_list_t;
     73 
     74 /* tree of includes for -o processing */
     75 opt_t *opt;
     76 int width;
     77 
     78 #define DEFAULT_PATH		_PATH_DEFPATH
     79 #define DEFAULT_FILENAME	".depend"
     80 
     81 static void save_for_optional(const char *, const char *);
     82 static int write_optional(int, opt_t *, int);
     83 
     84 
     85 static inline void *
     86 deconst(const void *p)
     87 {
     88 	return (const char *)p - (const char *)0 + (char *)0;
     89 }
     90 
     91 static void
     92 usage(void)
     93 {
     94 	(void)fprintf(stderr,
     95 	    "usage: %s [-aDdopq] [-f file] [-s suffixes] -- [flags] file ...\n",
     96 	    getprogname());
     97 	exit(EXIT_FAILURE);
     98 }
     99 
    100 static int
    101 run_cc(int argc, char **argv, const char **fname)
    102 {
    103 	const char *CC, *tmpdir;
    104 	char * volatile pathname;
    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 		err(EXIT_FAILURE,  "Unable to create temporary file %s",
    131 		    tmpfilename);
    132 	(void)unlink(tmpfilename);
    133 	*fname = tmpfilename;
    134 
    135 	switch (cpid = vfork()) {
    136 	case 0:
    137 		(void)dup2(tmpfd, STDOUT_FILENO);
    138 		(void)close(tmpfd);
    139 
    140 		(void)execv(pathname, args);
    141 		_exit(EXIT_FAILURE);
    142 		/* NOTREACHED */
    143 
    144 	case -1:
    145 		err(EXIT_FAILURE, "unable to fork");
    146 	}
    147 
    148 	free(pathname);
    149 	free(args);
    150 
    151 	while (((pid = wait(&status)) != cpid) && (pid >= 0))
    152 		continue;
    153 
    154 	if (status)
    155 		errx(EXIT_FAILURE, "compile failed.");
    156 
    157 	return tmpfd;
    158 }
    159 
    160 static const char *
    161 read_fname(void)
    162 {
    163 	static char *fbuf;
    164 	static int fbuflen;
    165 	int len, ch;
    166 
    167 	for (len = 0; (ch = getchar()) != EOF; len++) {
    168 		if (isspace(ch)) {
    169 			if (len != 0)
    170 				break;
    171 			len--;
    172 			continue;
    173 		}
    174 		if (len >= fbuflen - 1) {
    175 			fbuf = realloc(fbuf, fbuflen += 32);
    176 			if (fbuf == NULL)
    177 				err(EXIT_FAILURE, "no memory");
    178 		}
    179 		fbuf[len] = ch;
    180 	}
    181 	if (len == 0)
    182 		return NULL;
    183 	fbuf[len] = 0;
    184 	return fbuf;
    185 }
    186 
    187 static struct option longopt[] = {
    188 	{ "sysroot", 1, NULL, 'R' },
    189 	{ NULL, 0, NULL, '\0' },
    190 };
    191 
    192 static void
    193 addsuff(suff_list_t **l, const char *s, size_t len)
    194 {
    195 	suff_list_t *p = calloc(1, sizeof(*p));
    196 	if (p == NULL)
    197 		err(1, "calloc");
    198 	p->suff = malloc(len + 1);
    199 	if (p->suff == NULL)
    200 		err(1, "malloc");
    201 	memcpy(p->suff, s, len);
    202 	p->suff[len] = '\0';
    203 	p->len = len;
    204 	p->next = *l;
    205 	*l = p;
    206 }
    207 
    208 int
    209 main(int argc, char **argv)
    210 {
    211 	int 	aflag, dflag, oflag, qflag;
    212 	const char *filename;
    213 	int	dependfile;
    214 	char	*buf, *lim, *ptr, *line, *suf, *colon, *eol;
    215 	int	ok_ind, ch;
    216 	size_t	sz;
    217 	int	fd;
    218 	size_t  slen;
    219 	const char *fname;
    220 	const char *suffixes = NULL, *s;
    221 	suff_list_t *suff_list = NULL, *sl;
    222 
    223 	suf = NULL;		/* XXXGCC -Wuninitialized [sun2] */
    224 	sl = NULL;		/* XXXGCC -Wuninitialized [sun2] */
    225 
    226 	setlocale(LC_ALL, "");
    227 	setprogname(argv[0]);
    228 
    229 	aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC;
    230 	dflag = 0;
    231 	oflag = 0;
    232 	qflag = 0;
    233 	filename = DEFAULT_FILENAME;
    234 	dependfile = -1;
    235 
    236 	opterr = 0;	/* stop getopt() bleating about errors. */
    237 	for (;;) {
    238 		ok_ind = optind;
    239 		ch = getopt_long(argc, argv, "aDdf:opqRs:", longopt, NULL);
    240 		switch (ch) {
    241 		case -1:
    242 			ok_ind = optind;
    243 			break;
    244 		case 'a':	/* Append to output file */
    245 			aflag &= ~O_TRUNC;
    246 			continue;
    247 		case 'D':	/* Process *.d files (don't run cc -M) */
    248 			dflag = 2;	/* Read names from stdin */
    249 			opterr = 1;
    250 			continue;
    251 		case 'd':	/* Process *.d files (don't run cc -M) */
    252 			dflag = 1;
    253 			opterr = 1;
    254 			continue;
    255 		case 'f':	/* Name of output file */
    256 			filename = optarg;
    257 			continue;
    258 		case 'o':	/* Mark dependant files .OPTIONAL */
    259 			oflag = 1;
    260 			continue;
    261 		case 'p':	/* Program mode (x.o: -> x:) */
    262 			suffixes = "";
    263 			continue;
    264 		case 'q':	/* Quiet */
    265 			qflag = 1;
    266 			continue;
    267 		case 'R':
    268 			/* sysroot = optarg */
    269 			continue;
    270 		case 's':	/* Suffix list */
    271 			suffixes = optarg;
    272 			continue;
    273 		default:
    274 			if (dflag)
    275 				usage();
    276 			/* Unknown arguments are passed to "${CC} -M" */
    277 			break;
    278 		}
    279 		break;
    280 	}
    281 
    282 	argc -= ok_ind;
    283 	argv += ok_ind;
    284 	if ((argc == 0 && !dflag) || (argc != 0 && dflag == 2))
    285 		usage();
    286 
    287 	if (suffixes != NULL) {
    288 		if (*suffixes) {
    289 			for (s = suffixes; (sz = strcspn(s, ", ")) != 0;) {
    290 				addsuff(&suff_list, s, sz);
    291 				s += sz;
    292 				while (*s && strchr(", ", *s))
    293 					s++;
    294 			}
    295 		} else
    296 			addsuff(&suff_list, "", 0);
    297 	}
    298 
    299 	dependfile = open(filename, aflag, 0666);
    300 	if (dependfile == -1)
    301 		err(EXIT_FAILURE, "unable to %s to file %s\n",
    302 		    aflag & O_TRUNC ? "write" : "append", filename);
    303 
    304 	while (dflag == 2 || *argv != NULL) {
    305 		if (dflag) {
    306 			if (dflag == 2) {
    307 				fname = read_fname();
    308 				if (fname == NULL)
    309 					break;
    310 			} else
    311 				fname = *argv++;
    312 			fd = open(fname, O_RDONLY, 0);
    313 			if (fd == -1) {
    314 				if (!qflag)
    315 					warn("ignoring %s", fname);
    316 				continue;
    317 			}
    318 		} else {
    319 			fd = run_cc(argc, argv, &fname);
    320 			/* consume all args... */
    321 			argv += argc;
    322 		}
    323 
    324 		sz = lseek(fd, 0, SEEK_END);
    325 		if (sz == 0) {
    326 			close(fd);
    327 			continue;
    328 		}
    329 		buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
    330 		close(fd);
    331 
    332 		if (buf == MAP_FAILED)
    333 			err(EXIT_FAILURE, "unable to mmap file %s", fname);
    334 		lim = buf + sz - 1;
    335 
    336 		/* Remove leading "./" from filenames */
    337 		for (ptr = buf; ptr < lim; ptr++) {
    338 			if (ptr[1] != '.' || ptr[2] != '/'
    339 			    || !isspace((unsigned char)ptr[0]))
    340 				continue;
    341 			ptr[1] = ' ';
    342 			ptr[2] = ' ';
    343 		}
    344 
    345 		for (line = eol = buf; eol <= lim;) {
    346 			while (eol <= lim && *eol++ != '\n')
    347 				/* Find end of this line */
    348 				continue;
    349 			if (line == eol - 1) {
    350 				/* empty line - ignore */
    351 				line = eol;
    352 				continue;
    353 			}
    354 			if (eol[-2] == '\\')
    355 				/* Assemble continuation lines */
    356 				continue;
    357 			for (colon = line; *colon != ':'; colon++) {
    358 				if (colon >= eol) {
    359 					colon = NULL;
    360 					break;
    361 				}
    362 			}
    363 			if (isspace((unsigned char)*line) || colon == NULL) {
    364 				/* No dependency - just transcribe line */
    365 				write(dependfile, line, eol - line);
    366 				line = eol;
    367 				continue;
    368 			}
    369 			if (suff_list != NULL) {
    370 				/* Find the .o: */
    371 				/* First allow for any whitespace */
    372 				for (suf = colon; suf > buf; suf--) {
    373 					if (!isspace((unsigned char)suf[-1]))
    374 						break;
    375 				}
    376 				if (suf == buf)
    377 					errx(EXIT_FAILURE,
    378 					    "Corrupted file `%s'", fname);
    379 				/* Then look for any valid suffix */
    380 				for (sl = suff_list; sl != NULL;
    381 				    sl = sl->next) {
    382 					if (sl->len &&
    383 					    !memcmp(suf - sl->len, sl->suff,
    384 						    sl->len))
    385 						break;
    386 				}
    387 				/*
    388 				 * Not found, check for .o, since the
    389 				 * original file will have it.
    390 				 */
    391 				if (sl == NULL) {
    392 					if (memcmp(suf - 2, ".o", 2) == 0)
    393 						slen = 2;
    394 					else
    395 						slen = 0;
    396 				} else
    397 					slen = sl->len;
    398 			}
    399 			if (suff_list != NULL && slen != 0) {
    400 				suf -= slen;
    401 				for (sl = suff_list; sl != NULL; sl = sl->next)
    402 				{
    403 					if (sl != suff_list)
    404 						write(dependfile, " ", 1);
    405 					write(dependfile, line, suf - line);
    406 					write(dependfile, sl->suff, sl->len);
    407 				}
    408 				write(dependfile, colon, eol - colon);
    409 			} else
    410 				write(dependfile, line, eol - line);
    411 
    412 			if (oflag)
    413 				save_for_optional(colon + 1, eol);
    414 			line = eol;
    415 		}
    416 		munmap(buf, sz);
    417 	}
    418 
    419 	if (oflag && opt != NULL) {
    420 		write(dependfile, ".OPTIONAL:", 10);
    421 		width = 9;
    422 		sz = write_optional(dependfile, opt, 0);
    423 		/* 'depth' is about 39 for an i386 kernel */
    424 		/* fprintf(stderr, "Recursion depth %d\n", sz); */
    425 	}
    426 	close(dependfile);
    427 
    428 	exit(EXIT_SUCCESS);
    429 }
    430 
    431 
    432 /*
    433  * Only save each file once - the kernel .depend is 3MB and there is
    434  * no point doubling its size.
    435  * The data seems to be 'random enough' so the simple binary tree
    436  * only has a reasonable depth.
    437  */
    438 static void
    439 save_for_optional(const char *start, const char *limit)
    440 {
    441 	opt_t **l, *n;
    442 	const char *name, *end;
    443 	int c;
    444 
    445 	while (start < limit && strchr(" \t\n\\", *start))
    446 		start++;
    447 	for (name = start; ; name = end) {
    448 		while (name < limit && strchr(" \t\n\\", *name))
    449 			name++;
    450 		for (end = name; end < limit && !strchr(" \t\n\\", *end);)
    451 			end++;
    452 		if (name >= limit)
    453 			break;
    454 		if (end[-1] == 'c' && end[-2] == '.' && name == start)
    455 			/* ignore dependency on the files own .c */
    456 			continue;
    457 		for (l = &opt;;) {
    458 			n = *l;
    459 			if (n == NULL) {
    460 				n = malloc(sizeof *n + (end - name));
    461 				n->left = n->right = 0;
    462 				n->len = end - name;
    463 				n->count = 1;
    464 				n->name[0] = ' ';
    465 				memcpy(n->name + 1, name, end - name);
    466 				*l = n;
    467 				break;
    468 			}
    469 			c = (end - name) - n->len;
    470 			if (c == 0)
    471 				c = memcmp(n->name + 1, name, (end - name));
    472 			if (c == 0) {
    473 				/* Duplicate */
    474 				n->count++;
    475 				break;
    476 			}
    477 			if (c < 0)
    478 				l = &n->left;
    479 			else
    480 				l = &n->right;
    481 		}
    482 	}
    483 }
    484 
    485 static int
    486 write_optional(int fd, opt_t *node, int depth)
    487 {
    488 	int d1 = ++depth;
    489 
    490 	if (node->left)
    491 		d1 = write_optional(fd, node->left, d1);
    492 	if (width > 76 - node->len) {
    493 		write(fd, " \\\n ", 4);
    494 		width = 1;
    495 	}
    496 	width += 1 + node->len;
    497 	write(fd, node->name, 1 + node->len);
    498 	if (node->right)
    499 		depth = write_optional(fd, node->right, depth);
    500 	return d1 > depth ? d1 : depth;
    501 }
    502