#############################################################################
##
##  Makefile for CODEPEND sample code associated with the technical 
##  article "Mechanics of Dynamic Linking"
##
##  Description
##  -----------
##  Suite of simple applications and dynamic link libraries that 
##  illustrate some aspects of the dynamic linking mechanism:  
##  module usage, task and instance handles, load and unload order,
##  and inter-module dependency.
##
##  App1.exe calls function in Lib1.dll, which calls function in Lib2.dll.
##  App2.exe calls function in Lib2.dll, which calls function in Lib3.dll.
##  App3.exe calls function in Lib3.dll, which calls function in Lib2.dll.
##  
##  Build Options
##  -------------
##  NMAKE Builds everything that is out-of-date. 
##  NMAKE CLEAN - deletes all generated files (*.exe, *.dll, *.obj, etc.). 
##
##  Set EXEHDR=1 to generate <filename>.HDR listings for each executable.
##
##  Assumptions
##  -----------
##  This makefile assumes Microsoft C/C++ version 7.0 or higher. No C 
##  runtime libraries are used in these samples (thus the use of link
##  libraries SNOCRTW.LIB for apps and SNOCRTDW.LIB for dlls).
##
##  Unusual Features
##  ----------------
##  The file MODREPORT.C is used by both the applications and the 
##  libraries.  The apps compile MODREPORT.C into MODREPORTA.OBJ (the
##  "A" is for "App"), while the DLLs compile MODREPORT.C into 
##  MODREPORTD.OBJ (the "D" is for "DLL").  This is because the compile
##  flags differ between apps and libraries.
##
#############################################################################

##### Define the targets #####
all:    app1.exe app2.exe app3.exe lib1.dll lib2.dll lib3.dll

##### Enable/Disable EXEHDR listings (files are .HDR) #####
EXEHDR = 0

##### Define compiler, assembler and linker switches
CL_APP   = $(CLOPT) -GA -AS
CL_DLL   = $(CLOPT) -GD -ASw
CLOPT    = -nologo -c -G2s -W3 -Zip -Od 
MOPT     = -Zi
LOPT     = /CO/M/NOD/NOE/A:16
APP_LIBS = libw snocrtw
DLL_LIBS = libw snocrtdw

##### Rules for DLLs ############################################

##### Rules to build import libraries from .DEF files...
lib1.lib:   lib1.def
    implib lib1.lib lib1.def

lib2.lib:   lib2.def
    implib lib2.lib lib2.def

lib3.lib:   lib3.def
    implib lib3.lib lib3.def

##### Rules to build DLL .OBJ files...
lib1.obj:   lib1.c lib2.h modrept.h
    cl $(CL_DLL) $*.c

lib2.obj:   lib2.c lib3.h modrept.h
    cl $(CL_DLL) $*.c

lib3.obj:   lib3.c lib2.h modrept.h
    cl $(CL_DLL) $*.c

### Compile modrept.c for use by DLLs, renaming output to modreptd.obj
modreptd.obj:    modrept.c modrept.h
    cl $(CL_DLL) -Fomodreptd modrept.c

##### Rules to link the DLLs...
lib1.dll: lib1.obj lib1.def modreptd.obj lib2.lib 
    link $(LOPT) $* libentry modreptd, $*.dll,, lib2 $(DLL_LIBS), $*.def
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.dll > $*.hdr
!endif
    
lib2.dll: lib2.obj lib2.def modreptd.obj lib3.lib
    link $(LOPT) $* libentry modreptd, $*.dll,, lib3 $(DLL_LIBS), $*.def
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.dll > $*.hdr
!endif

lib3.dll: lib3.obj lib3.def modreptd.obj lib2.lib 
    link $(LOPT) $* libentry modreptd, $*.dll,, lib2 $(DLL_LIBS), $*.def
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.dll > $*.hdr
!endif

##### Rules for Apps ############################################

##### Rules to compile the applications...
app1.obj: app1.c lib1.h modrept.h
    cl $(CL_APP) $*.c

app2.obj: app2.c lib2.h modrept.h
    cl $(CL_APP) $*.c

app3.obj: app3.c lib3.h modrept.h
    cl $(CL_APP) $*.c

### Compile modrept.c for use by applications, and rename obj to modrepta.obj
modrepta.obj:    modrept.c
    cl $(CL_APP) -Fomodrepta modrept.c

##### Rules to link the applications...
app1.exe:   app1.obj app1.def modrepta.obj lib1.lib lib3.lib
    link $(LOPT) $* modrepta,,, lib1 $(APP_LIBS), $*
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.exe > $*.hdr
!endif

app2.exe:   app2.obj app2.def modrepta.obj lib2.lib 
    link $(LOPT) $* modrepta,,, lib2 $(APP_LIBS), $*
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.exe > $*.hdr
!endif

app3.exe:   app3.obj app3.def modrepta.obj lib3.lib
    link $(LOPT) $* modrepta,,, lib3 $(APP_LIBS), $*
    mapsym $*
!if $(EXEHDR)
    exehdr /v $*.exe > $*.hdr
!endif

##### Clean directory ############################################
clean:
    -del *.obj
    -del *.exe
    -del *.res
    -del *.sym
    -del *.map
    -del *.dll
    -del *.lib
!if $(EXEHDR)
    -del *.hdr
!endif
