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