[comp.sys.ibm.pc] Make depend for microsoft C and ndmake

velte@mimsy.UUCP (05/15/87)

Summary:
Expires:
References:
Sender:

i have written a make depend for the microsoft C compiler that is patterned
after the shell script found in most makefiles here at the university.
this will work with any c compiler that has a preprocessor pass.

make depend

this is a adaptation of the unix shell script that produces
dependencies for c makefiles.  this make utility uses don kneller's
ndmake, decus public domain lex, bob brodt's bawk (a public
domain/shareware awk), any sort which eliminates duplicates entries,
a `egrep' that can match patterns (pgrep used here) and a c
preprocessor (microsoft c has a preprocessor option [unfortunately,
this is by far the slowest part of the make depend script.])  these
programs are available on bob blacher's computer connection bbs at
(202) 547-2008.

to use:
enter the place where the bawkrul1 lives in the depend.bat file and 
where the bawkrul2 file lives in the makefile.  enter where the 
depend.bat lives in the makefile.  tell ndmake where
you want your temporary files to live (ramdisk recommended.)
make a macro in the makefile called CSRC equal to the names of all
the c sources.  determine which compiler switches your c compiler uses
to preprocess the file, without compiling the file and enter them in
the depend.bat file.  type make depend.

if you are going to modify this, i recommend that every step use its own
temporary file.  this will make it easier to tell where the program is
going wrong.

the original version of decus lex that i found on the bbs was compiled with
a compiler that did not parse the command line arguments correctly, which
made lex appear to have a broken -e command.  recompiling lex with
ms c v. 4 fixed the problem.  i uploaded the fixed version to bob blacher's
bbs.

written by jack velte <velte@mimsy.umd.edu>, based on code by chris torek
<chris@mimsy.umd.edu>.

---------------------------
this is the depend.bat file.  this should go in the makefile, but the make 
program (ndmake41) currently does not allow the `#' symbol to be quoted; it 
always starts a comment. 

        REM turn echo to watch the fun
echo on
        REM echo the file name, piping it into sed which strips any forward
        REM slashes with forward slash backslash / -> \/, and then replace
        REM the `.c' with a `.obj: ', creating a file called temp1 on the
        REM given temporary disk
echo %1 | sed -e "s,/,\\\\/,g" -e "s/\.c/\.obj: /" > %2temp1
        REM preprocess the c file, running the output through a grep which 
        REM finds only the lines starting with `#', pipe that through sed which
        REM replaces the string `#line [anynumbers here]' with nothing, the
        REM string `"./' with `"', and remove double quotes. 
        REM one note:  you will also need to include any directories not given
        REM by the environment variable here on the cl command line.
        REM e.g. -I../h  (that is what %3 is for...)
cl -E %3 %1 | pgrep -n -c "^#" |sed -e "s/#line [0-9]* //" -e "s,\"./,\"," -e "s/\"\(.*\)\"/\1/" > %2temp2
        REM sort, discarding duplicates.
usort %2temp2 >> %2temp1
        REM marks end of file's includes
echo ----- >> %2temp1
        REM format the output and append to makedep file
bawk \prog\bin\bawkrul1 %2temp1 >> %2makedep
---------------------------

this is the first bawk rule file.  this might also go directly into the
makefile, except that bawk does not allow commands on the command line.
this rule echoes lines to the stdout, until the line length is greater
than 50, and there is still a new thing to print. in that case, print a 
backslash on the end of the line.
BEGIN
{
        linelen = 0
}

{
        if (0 != strcmp($0, "----")) {
                if (linelen > 50) {
                        if (match($0, @[a-zA-Z]+@)) {
                                printf("\\\\n\t");
                                linelen = 0;
                        }
                }
                linelen = strlen($1) + linelen + 1;
                if (match($0, @[a-zA-Z]+@)) 
                        printf("%s ", $1);
        }
}
END
{
        printf("\n");
}
--------------------------------
this is the second bawk rule.  echo to stdout until the matching string is
found.
BEGIN
{
        flag = 0;
}

{
        if (0 == strcmp($0,
                   "# DO NOT DELETE THIS LINE -- make depend uses it")) {
                flag = 1;
        }

        if (flag == 0)
                printf("%s\n", $0);
}

END
{
        printf("# DO NOT DELETE THIS LINE -- make depend uses it\n\n\n");
}
------------------------------
this is the makefile.

#makefile for the don kneller's shareware ndmake

# this requires decus sed, the public domain `bawk' (unix awk
# like program), a sort that eliminates redundant entries (usort), and a c
# preprocessor (microsoft c used here).  it generates the dependencies for
# each c file: what files this c file depends on.
# all the temporary files go onto the ``temp'' disk - preferably a ram disk
# the ${CSRC} is a macro equal to all the .c files that need to
# have make depend run on them.
depend: ${CSRC}
# get rid of any existing temporary files
       -(if exist ${TMP}makedep del ${TMP}makedep; \
        touch ${TMP}makedep; copy \prog\bin\depend.bat ${TMP})
# loop over each file as given by ${CSRC} running the depend program [batch
# file] over it.  the location of the temporary files are passed as a 
# parameter
       -! (${TMP}depend $? ${TMP})
# make back up of old makefile
        copy Makefile Makefile.bak
# run bawk over old makefile, copy everything up to the string `DO NOT 
# DELETE THIS LINE' to the new makefile
        -(bawk \prog\bin\bawkrul2 makefile.bak > makefile)
# append the makedep file (created by the depend batch file) to the makefile)
        type ${TMP}makedep >> makefile
# delete temporary files
        (del ${TMP}makedep; del ${TMP}temp?)


# anything after this line will disappear.
# DO NOT DELETE THIS LINE -- make depend uses it



# these are the dependency rules.  the .obj file depends on all the other
# files being older than it; if any are newer, then it should be
# recompiled.  for larger programs, this script automates an important part
# of makefile generation.
sedcomp.obj: c:/include/stdio.h debug.h sed.h sedcomp.c 
sedexec.obj: c:/include/ctype.h c:/include/stdio.h \
        debug.h sed.h sedexec.c 

---------------------

# i have a file called `makedep' which includes just the following lines.  
# when i need to include the makedep i ``type \bin\makedep >> makefile''
depend: ${CSRC}
       -(if exist ${TMP}makedep del ${TMP}makedep; \
        touch ${TMP}makedep; copy \prog\bin\depend.bat ${TMP})
       -! (${TMP}depend $? ${TMP})
        copy Makefile Makefile.bak
        -(bawk \prog\bin\bawkrul2 makefile.bak > makefile)
        type ${TMP}makedep >> makefile
        (del ${TMP}makedep; del ${TMP}temp?)


# anything after this line will disappear.
# DO NOT DELETE THIS LINE -- make depend uses it

-- 
jack velte     university of MD, laboratory for parallel computation
domain:	velte@mimsy.umd.edu	path: seismo!mimsy!velte