1*c41df9f6Schristos#!/bin/sh -e 20f74e101Schristos# 30f74e101Schristos# Copyright (c) 1994, 1996 40f74e101Schristos# The Regents of the University of California. All rights reserved. 50f74e101Schristos# 60f74e101Schristos# Redistribution and use in source and binary forms are permitted 70f74e101Schristos# provided that this notice is preserved and that due credit is given 80f74e101Schristos# to the University of California at Berkeley. The name of the University 90f74e101Schristos# may not be used to endorse or promote products derived from this 100f74e101Schristos# software without specific prior written permission. This software 110f74e101Schristos# is provided ``as is'' without express or implied warranty. 120f74e101Schristos# 130f74e101Schristos# @(#)mkdep.sh 5.11 (Berkeley) 5/5/88 140f74e101Schristos# 150f74e101Schristos 160f74e101SchristosMAKE=Makefile # default makefile name is "Makefile" 170f74e101SchristosCC=cc # default C compiler is "cc" 18026d7285SchristosDEPENDENCY_CFLAG=-M # default dependency-generation flag is -M 19d881c474SchristosSOURCE_DIRECTORY=. # default source directory is the current directory 200f74e101Schristos 21d881c474Schristos# No command-line flags seen yet. 22d881c474Schristosflags="" 230f74e101Schristoswhile : 240f74e101Schristos do case "$1" in 250f74e101Schristos # -c allows you to specify the C compiler 260f74e101Schristos -c) 270f74e101Schristos CC=$2 280f74e101Schristos shift; shift ;; 290f74e101Schristos 300f74e101Schristos # -f allows you to select a makefile name 310f74e101Schristos -f) 320f74e101Schristos MAKE=$2 330f74e101Schristos shift; shift ;; 340f74e101Schristos 35026d7285Schristos # -m allows you to specify the dependency-generation flag 36026d7285Schristos -m) 37026d7285Schristos DEPENDENCY_CFLAG=$2 38026d7285Schristos shift; shift ;; 39026d7285Schristos 400f74e101Schristos # the -p flag produces "program: program.c" style dependencies 410f74e101Schristos # so .o's don't get produced 420f74e101Schristos -p) 430f74e101Schristos SED='s;\.o;;' 440f74e101Schristos shift ;; 45d881c474Schristos 46d881c474Schristos # -s allows you to specify the source directory 47d881c474Schristos -s) 48d881c474Schristos SOURCE_DIRECTORY=$2 49d881c474Schristos shift; shift ;; 50d881c474Schristos 51d881c474Schristos # other command-line flag 52d881c474Schristos -*) 53d881c474Schristos flags="$flags $1" 54d881c474Schristos shift ;; 55d881c474Schristos 560f74e101Schristos *) 570f74e101Schristos break ;; 580f74e101Schristos esac 590f74e101Schristosdone 600f74e101Schristos 610f74e101Schristosif [ $# = 0 ] ; then 62d881c474Schristos echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...' 630f74e101Schristos exit 1 640f74e101Schristosfi 650f74e101Schristos 66*c41df9f6Schristosif [ ! -w "$MAKE" ]; then 670f74e101Schristos echo "mkdep: no writeable file \"$MAKE\"" 680f74e101Schristos exit 1 690f74e101Schristosfi 700f74e101Schristos 71*c41df9f6SchristosTMP=${TMPDIR:-/tmp}/mkdep$$ 720f74e101Schristos 73*c41df9f6Schristostrap 'rm -f "$TMP" ; exit 1' HUP INT QUIT PIPE TERM 740f74e101Schristos 75*c41df9f6Schristoscp "$MAKE" "${MAKE}.bak" 760f74e101Schristos 77*c41df9f6Schristossed -e '/DO NOT DELETE THIS LINE/,$d' < "$MAKE" > "$TMP" 780f74e101Schristos 79*c41df9f6Schristoscat << _EOF_ >> "$TMP" 800f74e101Schristos# DO NOT DELETE THIS LINE -- mkdep uses it. 810f74e101Schristos# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY. 820f74e101Schristos 830f74e101Schristos_EOF_ 840f74e101Schristos 850f74e101Schristos# If your compiler doesn't have -M, add it. If you can't, the next two 860f74e101Schristos# lines will try and replace the "cc -M". The real problem is that this 870f74e101Schristos# hack can't deal with anything that requires a search path, and doesn't 880f74e101Schristos# even try for anything using bracket (<>) syntax. 890f74e101Schristos# 90d881c474Schristos# grep -E '^#include[[:blank:]]*".*"' /dev/null $* | 910f74e101Schristos# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' | 920f74e101Schristos 93d881c474Schristos# 94d881c474Schristos# Construct a list of source files with paths relative to the source directory. 95d881c474Schristos# 96d881c474Schristossources="" 97*c41df9f6Schristosfor srcfile in "$@" 98d881c474Schristosdo 99d881c474Schristos sources="$sources $SOURCE_DIRECTORY/$srcfile" 100d881c474Schristosdone 101d881c474Schristos 1020f74e101Schristos# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait" 103*c41df9f6Schristos# $flags and $sources are meant to expand 104*c41df9f6Schristos# shellcheck disable=SC2086 105*c41df9f6Schristos"$CC" "$DEPENDENCY_CFLAG" $flags $sources | 1060f74e101Schristossed " 1070f74e101Schristos s; \./; ;g 108*c41df9f6Schristos $SED" >> "$TMP" 1090f74e101Schristos 110*c41df9f6Schristoscat << _EOF_ >> "$TMP" 1110f74e101Schristos 1120f74e101Schristos# IF YOU PUT ANYTHING HERE IT WILL GO AWAY 1130f74e101Schristos_EOF_ 1140f74e101Schristos 1150f74e101Schristos# copy to preserve permissions 116*c41df9f6Schristoscp "$TMP" "$MAKE" 117*c41df9f6Schristosrm -f "${MAKE}.bak" "$TMP" 1180f74e101Schristosexit 0 119