Home | History | Annotate | Line # | Download | only in mkdep
mkdep.c revision 1.20
      1 /* $NetBSD: mkdep.c,v 1.20 2003/11/11 10:55:24 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.20 2003/11/11 10:55:24 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 #define DEFAULT_PATH		_PATH_DEFPATH
     67 #define DEFAULT_FILENAME	".depend"
     68 
     69 
     70 static inline void *
     71 deconst(const void *p)
     72 {
     73 	return (const char *)p - (const char *)0 + (char *)0;
     74 }
     75 
     76 static void
     77 usage(void)
     78 {
     79 	(void)fprintf(stderr,
     80 	    "usage: %s [-adopq] [-f file] [-s suffix_list] flags file ...\n",
     81 	    getprogname());
     82 	exit(EXIT_FAILURE);
     83 }
     84 
     85 static int
     86 run_cc(int argc, char **argv, const char **fname)
     87 {
     88 	const char *CC, *pathname, *tmpdir;
     89 	static char tmpfilename[MAXPATHLEN];
     90 	char **args;
     91 	int tmpfd;
     92 	pid_t pid, cpid;
     93 	int status;
     94 
     95 	if ((CC = getenv("CC")) == NULL)
     96 		CC = DEFAULT_CC;
     97 	if ((pathname = findcc(CC)) == NULL)
     98 		if (!setenv("PATH", DEFAULT_PATH, 1))
     99 			pathname = findcc(CC);
    100 	if (pathname == NULL)
    101 		err(EXIT_FAILURE, "%s: not found", CC);
    102 	if ((args = malloc((argc + 3) * sizeof(char *))) == NULL)
    103 		err(EXIT_FAILURE, "malloc");
    104 
    105 	args[0] = deconst(CC);
    106 	args[1] = deconst("-M");
    107 	(void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *));
    108 
    109 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    110 		tmpdir = _PATH_TMP;
    111 	(void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir,
    112 	    "mkdepXXXXXX");
    113 	if ((tmpfd = mkstemp(tmpfilename)) < 0) {
    114 		warn("unable to create temporary file %s", tmpfilename);
    115 		exit(EXIT_FAILURE);
    116 	}
    117 	(void)unlink(tmpfilename);
    118 	*fname = tmpfilename;
    119 
    120 	switch (cpid = vfork()) {
    121 	case 0:
    122 		(void)dup2(tmpfd, STDOUT_FILENO);
    123 		(void)close(tmpfd);
    124 
    125 		(void)execv(pathname, args);
    126 		_exit(EXIT_FAILURE);
    127 		/* NOTREACHED */
    128 
    129 	case -1:
    130 		err(EXIT_FAILURE, "unable to fork");
    131 	}
    132 
    133 	while (((pid = wait(&status)) != cpid) && (pid >= 0))
    134 		continue;
    135 
    136 	if (status)
    137 		errx(EXIT_FAILURE, "compile failed.");
    138 
    139 	return tmpfd;
    140 }
    141 
    142 int
    143 main(int argc, char **argv)
    144 {
    145 	int 	aflag, dflag, oflag, qflag;
    146 	const char *filename;
    147 	int	dependfile;
    148 	char	*buf, *lim, *ptr, *line, *suf, *colon, *eol;
    149 	int	ok_ind, ch;
    150 	int	sz;
    151 	int	fd;
    152 	const char *fname;
    153 	const char *suffixes = NULL, *s, *s1;
    154 
    155 	setlocale(LC_ALL, "");
    156 	setprogname(argv[0]);
    157 
    158 	aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC;
    159 	dflag = 0;
    160 	oflag = 0;
    161 	qflag = 0;
    162 	filename = DEFAULT_FILENAME;
    163 	dependfile = -1;
    164 
    165 	opterr = 0;	/* stop getopt() bleating about errors. */
    166 	for (;;) {
    167 		ok_ind = optind;
    168 		ch = getopt(argc, argv, "adf:opqs:");
    169 		switch (ch) {
    170 		case -1:
    171 			ok_ind = optind;
    172 			break;
    173 		case 'a':	/* Append to output file */
    174 			aflag &= ~O_TRUNC;
    175 			continue;
    176 		case 'd':	/* Process *.d files (don't run cc -M) */
    177 			dflag = 1;
    178 			opterr = 1;
    179 			continue;
    180 		case 'f':	/* Name of output file */
    181 			filename = optarg;
    182 			continue;
    183 		case 'o':	/* Mark dependant files .OPTIONAL */
    184 			oflag = 1;
    185 			continue;
    186 		case 'p':	/* Program mode (x.o: -> x:) */
    187 			suffixes = "";
    188 			continue;
    189 		case 'q':	/* Quiet */
    190 			qflag = 1;
    191 			continue;
    192 		case 's':	/* Suffix list */
    193 			suffixes = optarg;
    194 			continue;
    195 		default:
    196 			if (dflag)
    197 				usage();
    198 			/* Unknown arguments are passed to "${CC} -M" */
    199 			break;
    200 		}
    201 		break;
    202 	}
    203 
    204 	argc -= ok_ind;
    205 	argv += ok_ind;
    206 	if (argc == 0 && !dflag)
    207 		usage();
    208 
    209 	dependfile = open(filename, aflag, 0666);
    210 	if (dependfile == -1)
    211 		err(EXIT_FAILURE, "unable to %s to file %s\n",
    212 		    aflag & O_TRUNC ? "write" : "append", filename);
    213 
    214 	for (; *argv != NULL; argv++) {
    215 		if (dflag) {
    216 			fname = *argv;
    217 			fd = open(fname, O_RDONLY, 0);
    218 			if (fd == -1) {
    219 				if (!qflag)
    220 					warn("ignoring %s", fname);
    221 				continue;
    222 			}
    223 		} else {
    224 			fd = run_cc(argc, argv, &fname);
    225 			/* consume all args... */
    226 			argv += argc - 1;
    227 		}
    228 
    229 		sz = lseek(fd, 0, SEEK_END);
    230 		if (sz == 0) {
    231 			close(fd);
    232 			continue;
    233 		}
    234 		buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
    235 		close(fd);
    236 
    237 		if (buf == MAP_FAILED)
    238 			err(EXIT_FAILURE, "unable to mmap file %s", *argv);
    239 		lim = buf + sz - 1;
    240 
    241 		/* Remove leading "./" from filenames */
    242 		for (ptr = buf; ptr < lim; ptr++) {
    243 			if (ptr[1] != '.' || ptr[2] != '/'
    244 			    || !isspace((unsigned char)ptr[0]))
    245 				continue;
    246 			ptr[1] = ' ';
    247 			ptr[2] = ' ';
    248 		}
    249 
    250 		for (line = eol = buf; eol <= lim;) {
    251 			while (eol <= lim && *eol++ != '\n')
    252 				continue;
    253 			if (line == eol - 1) {
    254 				/* empty line - ignore */
    255 				line = eol;
    256 				continue;
    257 			}
    258 			if (eol[-2] == '\\')
    259 				/* Assemble continuation lines */
    260 				continue;
    261 			for (colon = line; *colon != ':'; colon++) {
    262 				if (colon >= eol) {
    263 					colon = NULL;
    264 					break;
    265 				}
    266 			}
    267 			if (colon != NULL && suffixes != NULL) {
    268 				/* Find the .o: */
    269 				for (suf = colon - 2; ; suf--) {
    270 					if (suf <= line) {
    271 						colon = NULL;
    272 						break;
    273 					}
    274 					if (isspace((unsigned char)suf[1]))
    275 						continue;
    276 					if (suf[0] != '.' || suf[1] != 'o')
    277 						/* not a file.o: line */
    278 						colon = NULL;
    279 					break;
    280 				}
    281 			}
    282 			if (colon == NULL) {
    283 				/* No dependency - just transcribe line */
    284 				write(dependfile, line, eol - line);
    285 				line = eol;
    286 				continue;
    287 			}
    288 			if (suffixes != NULL) {
    289 				for (s = suffixes; ; s = s1 + 1) {
    290 					s1 = strpbrk(s, ", ");
    291 					if (s1 == NULL)
    292 						s1 = s + strlen(s);
    293 					write(dependfile, line, suf - line);
    294 					write(dependfile, s, s1 - s);
    295 					if (*s1 == 0)
    296 						break;
    297 					write(dependfile, " ", 1);
    298 				}
    299 				write(dependfile, colon, eol - colon);
    300 			} else
    301 				write(dependfile, line, eol - line);
    302 
    303 			if (oflag) {
    304 				write(dependfile, ".OPTIONAL", 9);
    305 				write(dependfile, colon, eol - colon);
    306 			}
    307 			line = eol;
    308 		}
    309 		munmap(buf, sz);
    310 	}
    311 	close(dependfile);
    312 
    313 	exit(EXIT_SUCCESS);
    314 }
    315