mkdep.c revision 1.24
1/* $NetBSD: mkdep.c,v 1.24 2005/06/07 09:33:37 he 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.24 2005/06/07 09:33:37 he 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 66typedef struct opt opt_t; 67struct opt { 68 opt_t *left; 69 opt_t *right; 70 int len; 71 int count; 72 char name[4]; 73}; 74 75typedef struct { 76 int len; 77 char suff[12]; 78} suff_list_t; 79 80/* tree of includes for -o processing */ 81opt_t *opt; 82int width; 83 84#define DEFAULT_PATH _PATH_DEFPATH 85#define DEFAULT_FILENAME ".depend" 86 87static void save_for_optional(const char *, const char *); 88static int write_optional(int, opt_t *, int); 89 90 91static inline void * 92deconst(const void *p) 93{ 94 return (const char *)p - (const char *)0 + (char *)0; 95} 96 97static void 98usage(void) 99{ 100 (void)fprintf(stderr, 101 "usage: %s [-adopq] [-f file] [-s suffixes] [flags] file ...\n", 102 getprogname()); 103 exit(EXIT_FAILURE); 104} 105 106static int 107run_cc(int argc, char **argv, const char **fname) 108{ 109 const char *CC, *pathname, *tmpdir; 110 static char tmpfilename[MAXPATHLEN]; 111 char **args; 112 int tmpfd; 113 pid_t pid, cpid; 114 int status; 115 116 if ((CC = getenv("CC")) == NULL) 117 CC = DEFAULT_CC; 118 if ((pathname = findcc(CC)) == NULL) 119 if (!setenv("PATH", DEFAULT_PATH, 1)) 120 pathname = findcc(CC); 121 if (pathname == NULL) 122 err(EXIT_FAILURE, "%s: not found", CC); 123 if ((args = malloc((argc + 3) * sizeof(char *))) == NULL) 124 err(EXIT_FAILURE, "malloc"); 125 126 args[0] = deconst(CC); 127 args[1] = deconst("-M"); 128 (void)memcpy(&args[2], argv, (argc + 1) * sizeof(char *)); 129 130 if ((tmpdir = getenv("TMPDIR")) == NULL) 131 tmpdir = _PATH_TMP; 132 (void)snprintf(tmpfilename, sizeof (tmpfilename), "%s/%s", tmpdir, 133 "mkdepXXXXXX"); 134 if ((tmpfd = mkstemp(tmpfilename)) < 0) { 135 warn("unable to create temporary file %s", tmpfilename); 136 exit(EXIT_FAILURE); 137 } 138 (void)unlink(tmpfilename); 139 *fname = tmpfilename; 140 141 switch (cpid = vfork()) { 142 case 0: 143 (void)dup2(tmpfd, STDOUT_FILENO); 144 (void)close(tmpfd); 145 146 (void)execv(pathname, args); 147 _exit(EXIT_FAILURE); 148 /* NOTREACHED */ 149 150 case -1: 151 err(EXIT_FAILURE, "unable to fork"); 152 } 153 154 while (((pid = wait(&status)) != cpid) && (pid >= 0)) 155 continue; 156 157 if (status) 158 errx(EXIT_FAILURE, "compile failed."); 159 160 return tmpfd; 161} 162 163int 164main(int argc, char **argv) 165{ 166 int aflag, dflag, oflag, qflag; 167 const char *filename; 168 int dependfile; 169 char *buf, *lim, *ptr, *line, *suf, *colon, *eol; 170 int ok_ind, ch; 171 int sz; 172 int fd; 173 const char *fname; 174 const char *suffixes = NULL, *s; 175 suff_list_t *suff_list = NULL, *sl; 176 177 suf = NULL; /* XXXGCC -Wuninitialized [sun2] */ 178 sl = NULL; /* XXXGCC -Wuninitialized [sun2] */ 179 180 setlocale(LC_ALL, ""); 181 setprogname(argv[0]); 182 183 aflag = O_WRONLY | O_APPEND | O_CREAT | O_TRUNC; 184 dflag = 0; 185 oflag = 0; 186 qflag = 0; 187 filename = DEFAULT_FILENAME; 188 dependfile = -1; 189 190 opterr = 0; /* stop getopt() bleating about errors. */ 191 for (;;) { 192 ok_ind = optind; 193 ch = getopt(argc, argv, "adf:opqs:"); 194 switch (ch) { 195 case -1: 196 ok_ind = optind; 197 break; 198 case 'a': /* Append to output file */ 199 aflag &= ~O_TRUNC; 200 continue; 201 case 'd': /* Process *.d files (don't run cc -M) */ 202 dflag = 1; 203 opterr = 1; 204 continue; 205 case 'f': /* Name of output file */ 206 filename = optarg; 207 continue; 208 case 'o': /* Mark dependant files .OPTIONAL */ 209 oflag = 1; 210 continue; 211 case 'p': /* Program mode (x.o: -> x:) */ 212 suffixes = ""; 213 continue; 214 case 'q': /* Quiet */ 215 qflag = 1; 216 continue; 217 case 's': /* Suffix list */ 218 suffixes = optarg; 219 continue; 220 default: 221 if (dflag) 222 usage(); 223 /* Unknown arguments are passed to "${CC} -M" */ 224 break; 225 } 226 break; 227 } 228 229 argc -= ok_ind; 230 argv += ok_ind; 231 if (argc == 0 && !dflag) 232 usage(); 233 234 if (suffixes != NULL) { 235 /* parse list once and save names and lengths */ 236 /* allocate an extra entry to mark end of list */ 237 for (sz = 1, s = suffixes; *s != 0; s++) 238 if (*s == '.') 239 sz++; 240 suff_list = calloc(sz, sizeof *suff_list); 241 if (suff_list == NULL) 242 err(2, "malloc"); 243 sl = suff_list; 244 for (s = suffixes; (s = strchr(s, '.')); s += sz, sl++ ) { 245 sz = strcspn(s, ", "); 246 if (sz > sizeof sl->suff) 247 errx(2, "suffix too long"); 248 sl->len = sz; 249 memcpy(sl->suff, s, sz); 250 } 251 } 252 253 dependfile = open(filename, aflag, 0666); 254 if (dependfile == -1) 255 err(EXIT_FAILURE, "unable to %s to file %s\n", 256 aflag & O_TRUNC ? "write" : "append", filename); 257 258 for (; *argv != NULL; argv++) { 259 if (dflag) { 260 fname = *argv; 261 fd = open(fname, O_RDONLY, 0); 262 if (fd == -1) { 263 if (!qflag) 264 warn("ignoring %s", fname); 265 continue; 266 } 267 } else { 268 fd = run_cc(argc, argv, &fname); 269 /* consume all args... */ 270 argv += argc - 1; 271 } 272 273 sz = lseek(fd, 0, SEEK_END); 274 if (sz == 0) { 275 close(fd); 276 continue; 277 } 278 buf = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); 279 close(fd); 280 281 if (buf == MAP_FAILED) 282 err(EXIT_FAILURE, "unable to mmap file %s", *argv); 283 lim = buf + sz - 1; 284 285 /* Remove leading "./" from filenames */ 286 for (ptr = buf; ptr < lim; ptr++) { 287 if (ptr[1] != '.' || ptr[2] != '/' 288 || !isspace((unsigned char)ptr[0])) 289 continue; 290 ptr[1] = ' '; 291 ptr[2] = ' '; 292 } 293 294 for (line = eol = buf; eol <= lim;) { 295 while (eol <= lim && *eol++ != '\n') 296 /* Find end of this line */ 297 continue; 298 if (line == eol - 1) { 299 /* empty line - ignore */ 300 line = eol; 301 continue; 302 } 303 if (eol[-2] == '\\') 304 /* Assemble continuation lines */ 305 continue; 306 for (colon = line; *colon != ':'; colon++) { 307 if (colon >= eol) { 308 colon = NULL; 309 break; 310 } 311 } 312 if (isspace((unsigned char)*line) || colon == NULL) { 313 /* No dependency - just transcribe line */ 314 write(dependfile, line, eol - line); 315 line = eol; 316 continue; 317 } 318 if (suff_list != NULL) { 319 /* Find the .o: */ 320 /* First allow for any whitespace */ 321 for (suf = colon; ; suf--) { 322 if (!isspace((unsigned char)suf[-1])) 323 break; 324 } 325 /* Then look for any valid suffix */ 326 for (sl = suff_list; sl->len != 0; sl++) { 327 if (!memcmp(suf - sl->len, sl->suff, 328 sl->len)) 329 break; 330 } 331 } 332 if (suff_list != NULL && sl->len != 0) { 333 suf -= sl->len; 334 for (sl = suff_list; sl->len != 0; sl++) { 335 if (sl != suff_list) 336 write(dependfile, " ", 1); 337 write(dependfile, line, suf - line); 338 write(dependfile, sl->suff, sl->len); 339 } 340 write(dependfile, colon, eol - colon); 341 } else 342 write(dependfile, line, eol - line); 343 344 if (oflag) 345 save_for_optional(colon + 1, eol); 346 line = eol; 347 } 348 munmap(buf, sz); 349 } 350 351 if (oflag && opt != NULL) { 352 write(dependfile, ".OPTIONAL:", 10); 353 width = 9; 354 sz = write_optional(dependfile, opt, 0); 355 /* 'depth' is about 39 for an i386 kernel */ 356 /* fprintf(stderr, "Recursion depth %d\n", sz); */ 357 } 358 close(dependfile); 359 360 exit(EXIT_SUCCESS); 361} 362 363 364/* 365 * Only save each file once - the kernel .depend is 3MB and there is 366 * no point doubling its size. 367 * The data seems to be 'random enough' so the simple binary tree 368 * only has a reasonable depth. 369 */ 370static void 371save_for_optional(const char *start, const char *limit) 372{ 373 opt_t **l, *n; 374 const char *name, *end; 375 int c; 376 377 while (start < limit && strchr(" \t\n\\", *start)) 378 start++; 379 for (name = start; ; name = end) { 380 while (name < limit && strchr(" \t\n\\", *name)) 381 name++; 382 for (end = name; end < limit && !strchr(" \t\n\\", *end);) 383 end++; 384 if (name >= limit) 385 break; 386 if (end[-1] == 'c' && end[-2] == '.' && name == start) 387 /* ignore dependency on the files own .c */ 388 continue; 389 for (l = &opt;;) { 390 n = *l; 391 if (n == NULL) { 392 n = malloc(sizeof *n + (end - name)); 393 n->left = n->right = 0; 394 n->len = end - name; 395 n->count = 1; 396 n->name[0] = ' '; 397 memcpy(n->name + 1, name, end - name); 398 *l = n; 399 break; 400 } 401 c = (end - name) - n->len; 402 if (c == 0) 403 c = memcmp(n->name + 1, name, (end - name)); 404 if (c == 0) { 405 /* Duplicate */ 406 n->count++; 407 break; 408 } 409 if (c < 0) 410 l = &n->left; 411 else 412 l = &n->right; 413 } 414 } 415} 416 417static int 418write_optional(int fd, opt_t *node, int depth) 419{ 420 int d1 = ++depth; 421 422 if (node->left) 423 d1 = write_optional(fd, node->left, d1); 424 if (width > 76 - node->len) { 425 write(fd, " \\\n ", 4); 426 width = 1; 427 } 428 width += 1 + node->len; 429 write(fd, node->name, 1 + node->len); 430 if (node->right) 431 depth = write_optional(fd, node->right, depth); 432 return d1 > depth ? d1 : depth; 433} 434