#
# makefile:
#       makefile for main directory.
#       For use with IBM NMAKE, which comes with the IBM compilers,
#       the Developer's Toolkit, and the DDK.
#
#       All the makefiles have been restructured with V0.9.0.
#
#       Called from:    nowhere. Main makefile.
#
#       Input:          specify the target(s) to be made, which can be:
#
#                       --  backend: compile backend (src\wiarchive\* and src\libz2\*
#                           to bin\*.obj)
#                       --  helpers: compile helpers (src\helpers\* to bin\*.obj)
#                       --  frontend: compile WARPIN.EXE object files (src\frontend\*
#                           to bin\*.obj)
#                       --  wic: compile WIC.EXE object files (src\wic\*
#                           to bin\*.obj)
#                       --  wicpm: compile WICPM.EXE object files (src\wicpm\*
#                           to bin\*.obj)
#                       --  nls: recurse to directory specified by WPI_NLS_CODE (setup.in)
#                           to build NLS resources etc.
#                       --  link: link bin\*.obj to EXEs (WARPIN.EXE, WIC.EXE, WICPM.EXE)
#
#                       Per default, all these targets will be made
#                       ("all:" target below).
#
#                       Use "nmake -a [<targets>] to _re_build the targets.
#
#                       Note: There is a special target named "dlgedit", which
#                       will invoke the dialog editor to edit the WARPIN.EXE
#                       dialogs.
#
#       Output:         All WarpIN files, except documentation.
#                       This makefile calls the other makefiles.
#
#       Edit "setup.in" to set up the make process (compilation flags etc.).
#

# Say hello to yourself.
!if [@echo +++++ Entering $(MAKEDIR)]
!endif

# include setup (compiler options etc.)
!include setup.in

# OUTPUTDIR specifies the directory where all the output .OBJ
# files will be created in.
OUTPUTDIR = $(PROJECT_OUTPUT_DIR)

!if [@md $(OUTPUTDIR) 2> NUL]
!endif
!if [@md $(MAPDIR) 2> NUL]
!endif
!if [@md $(HELPERS_OUTPUT_DIR_MT) 2> NUL]
!endif

# 2001-03-06 PH
# Define variables to call the tools. This actually fixes a problem
# where variables (namely PROJECT_BASE_DIR) was not propagated to
# spawned to child processes correctly.
RUN_SUB_NMAKE=$(MAKE) -nologo PROJECT_BASE_DIR=$(PROJECT_BASE_DIR) "MAINMAKERUNNING=YES"
# V0.9.9 (2001-02-28) [umoeller]: added MAINMAKERUNNING=YES to this thing...
# MAINMAKERUNNING=YES is the general environment variable which tells the
# sub-makefiles that they have been invoked dependently from the main makefile.
# If this is not set, they will recurse into the main makefile.

# Define the suffixes for files which NMAKE will work on.
# .SUFFIXES is a reserved NMAKE keyword ("pseudotarget") for
# defining file extensions that NMAKE will recognize in inference
# rules.
.SUFFIXES: .def .exe .lib .obj

# [tahola]
WIPENGINEOBJS = bin\test.obj bin\wipengine.lib \
                bin\bs_string.obj \
                $(HELPERSLIB)

# all objects and libs for WPI2EXE.EXE
WPI2EXEOBJS=bin\exe_st\wpi2exe.obj $(HELPERSLIB)

# all objects and libs for LAUNCHER.EXE, which will go into
# the resources of WPI2EXE.EXE
LAUNCHEROBJS=bin\exe_st\launcher.obj \
bin\subsys\cp.lib
# bin\subsys\dosh.obj bin\exe_st\prfh.obj bin\exe_st\resh.obj
#bin\exe_st\stringh.obj

I2HOBJS=bin\inf2html.obj

# store current directory so we can change back later
CURRENT_DIR = $(MAKEDIR)

# some variable strings to pass to sub-nmakes;
# this is needed to define the environment of sub-nmakes
TARGET_BASE_STRING="PROJECT_BASE_DIR=$(PROJECT_BASE_DIR)" "PROJECT_INCLUDE=$(PROJECT_INCLUDE)"

# all: base tools backend helpers frontend wic wicpm \
all: helpers nls compile tools link
# run nls before link, because we need the resources first
    @echo ----- Leaving $(MAKEDIR), done!

# "dep": create dependencies.
dep:
    @echo $(MAKEDIR)\makefile: Going for subdir src (dep)
    @cd src
    $(MAKE) -nologo dep "SUBTARGET=dep" "RUNDEPONLY=1" "REALLYALL=1"
    @cd $(CURRENT_DIR)
    @echo $(MAKEDIR)\makefile: Going for src\helpers (dep)
    @cd $(HELPERS_BASE)\src\helpers
    $(MAKE) -nologo dep "NOINCLUDEDEPEND=1" $(TARGET_BASE_STRING)
    @cd $(CURRENT_DIR)
    @echo ----- Leaving $(MAKEDIR)
    @echo Yo, done!

# "COMPILE" PSEUDOTARGETS
# -----------------------

compile:
    @echo $(MAKEDIR)\makefile: Going for subdir src (compile)
    @cd src
    $(MAKE) -nologo dep "SUBTARGET=all"
    @cd $(CURRENT_DIR)

tools:
    @echo $(MAKEDIR)\makefile: Calling tools
    @cd tools
!ifndef EMX
    @$(RUN_SUB_NMAKE) all
!endif
    @cd ..

backend:
    @echo $(MAKEDIR)\makefile: Calling src\libbz2
    @cd src\libbz2
    @$(RUN_SUB_NMAKE) all
    @echo $(MAKEDIR)\makefile: Calling src\wiarchive
    @cd ..\wiarchive
    @$(RUN_SUB_NMAKE) all
    @cd ..\..

base:
    @echo $(MAKEDIR)\makefile: Calling src\base
    @cd src\base
    @$(RUN_SUB_NMAKE) all
    @cd ..\..

makedll: $(OUTPUTDIR)\$(RTL_DLL_NAME)i.lib
    @echo ----- Leaving $(MAKEDIR)

# helpers: this is tricky...
# $(CC_HELPERS) is defined in setup.in
helpers:
    @echo $(MAKEDIR)\makefile: Calling $(HELPERS_BASE)\src\helpers
    @echo TARGET_BASE_STRING is: $(TARGET_BASE_STRING)
    @cd $(HELPERS_BASE)\src\helpers
!ifndef EMX
# for VAC, use "makefile_dll" to create objs only
# (we later link these with the other stuff into the
# runtime DLL)
    @$(RUN_SUB_NMAKE) -f makefile_dll $(TARGET_BASE_STRING) "HELPERS_OUTPUT_DIR=$(HELPERS_OUTPUT_DIR_MT)"
# and build a second version for CP helpers and single-thread libs
    @$(RUN_SUB_NMAKE) cp $(TARGET_BASE_STRING) "HELPERS_OUTPUT_DIR=$(HELPERS_OUTPUT_DIR_ST)" "CC_HELPERS=$(CC_SUBSYS)"
!else
# for EMX, produce OBJs only for static linking
    @$(RUN_SUB_NMAKE) $(TARGET_BASE_STRING) "HELPERS_OUTPUT_DIR=$(HELPERS_OUTPUT_DIR_MT)"
!endif
    @cd $(MAKEDIR)

frontend:
    @echo $(MAKEDIR)\makefile: Calling src\frontend
    @cd src\frontend
    @$(RUN_SUB_NMAKE) all
    @cd ..\..

wic:
    @echo $(MAKEDIR)\makefile: Calling src\wic
    @cd src\wic
    @$(RUN_SUB_NMAKE) all
    @cd ..\..

wicpm:
    @echo $(MAKEDIR)\makefile: Calling src\wicpm
    @cd src\wicpm
!ifndef EMX
# can't get wicpm to compile with EMX...
    @$(RUN_SUB_NMAKE) all
!endif
    @cd ..\..

# [tahola]
wipengine:
    @echo $(MAKEDIR)\makefile: Calling src\wipengine
    @cd src\wipengine
!ifndef EMX
    @nmake -nologo all "MAINMAKERUNNING=YES"
!endif
    @cd ..\..


# [tahola]
wipenginetest:
    @echo $(MAKEDIR)\makefile: Calling src\wipengine\wiptest
    @cd src\wipengine\wiptest
!ifndef EMX
    @nmake -nologo all "MAINMAKERUNNING=YES"
!endif
    @cd ..\..\..

wpi2exe:
    @echo $(MAKEDIR)\makefile: Calling src\wpi2exe
    @cd src\wpi2exe
    @$(RUN_SUB_NMAKE) all
    @cd ..\..

nls:
# this recurses into 001 to build
# resources and documentation; added (99-10-24) [umoeller]
    @echo $(MAKEDIR)\makefile: Calling $(WPI_NLS_CODE)
    @cd .\$(WPI_NLS_CODE)
    @$(RUN_SUB_NMAKE) all
    @cd ..

# V0.9.13 (2001-06-27) [umoeller]
# Teemu, the wip stuff doesn't build here. If you want
# to build it, define WIPTARGET externally.
WIPTARGET =
!ifdef WPI_WIP
    WIPTARGET = test.exe
!endif

link: \
!ifndef EMX
    tools\inf2html.exe $(WIPTARGET)
# wpi2exe.exe
!else

!endif

# Here come the rules for the output files.

tools\inf2html\inf2html.def: include\bl_inf2html.h
        cmd.exe /c .\BuildLevel.cmd $@ include\bl_inf2html.h "INF to HTML converter"

src\wpi2exe\wpi2exe.def: include\bl_wpi2exe.h
        cmd.exe /c .\BuildLevel.cmd $@ include\bl_wpi2exe.h "WarpIN WPI-to-EXE converter"

src\wpi2exe\launcher.def: include\bl_wpi2exe.h
        cmd.exe /c .\BuildLevel.cmd $@ include\bl_wpi2exe.h "Self-extracting WarpIN launcher"

# compose wicpm.exe
wicpm.exe: $(WICPMOBJS) src\wicpm\$(@B).def bin\$(@B).res
    @echo $(MAKEDIR)\makefile: Now linking "$(@B)"
!ifdef EMX
    $(LINK_EXE) -o $(@B).exe src\wicpm\$(@B).def $(WICPMOBJS)
!else
    $(LINK_EXE) /NODEFAULT os2386.lib cppom30o.lib /OUT:$(@B).exe  src\wicpm\$(@B).def /PMTYPE:PM $(WICPMOBJS)
!endif
    $(RC) bin\$(@B).res $(@B).exe
!ifndef WPI_DEBUG
!ifndef EMX
    mapsym -l $(MAPDIR)\$(@B).map
!endif
!endif

# [tahola]
# compose wipengine.lib
test.exe: $(WIPENGINEOBJS) src\wipengine\wiptest\$(@B).def
    @echo $(MAKEDIR)\makefile: Now linking "$(@B)"
!ifdef EMX
    $(LINK) -o $(@B).exe src\wipengine\wiptest\$(@B).def $(WIPENGINEOBJS)
!else
    $(LINK) /DE /NODEFAULT os2386.lib cppom30o.lib /OUT:$(@B).exe src\wipengine\wiptest\$(@B).def $(WIPENGINEOBJS)
!endif
!ifndef WPI_DEBUG
!ifndef EMX
    mapsym -l $(MAPDIR)\$(@B).map
!endif
!endif

# compose launcher.exe
bin\launcher.exe: $(LAUNCHEROBJS) src\wpi2exe\$(@B).def
    @echo $(MAKEDIR)\makefile: Now linking "$(@B)"
!ifdef EMX
    $(LINK_EXE) -o bin\$(@B).exe $(LAUNCHEROBJS) src\wpi2exe\$(@B).def
!else
    $(LINK_EXE) /noe /optfunc /align:1 /OUT:bin\$(@B).exe src\wpi2exe\$(@B).def $(LAUNCHEROBJS)
!ifndef WPI_DEBUG
!ifndef EMX
    mapsym -l $(MAPDIR)\$(@B).map
!endif
!endif
!endif

# compose wpi2exe.exe, which contains launcher.exe
bin\wpi2exe.res: src\wpi2exe\$(@B).rc bin\launcher.exe
    @echo $(MAKEDIR)\makefile: Creating $@
    $(RC) -r src\wpi2exe\$(@B).rc $(OUTPUTDIR)\$(@B).res

# compose wpi2exe.exe
wpi2exe.exe: $(WPI2EXEOBJS) src\wpi2exe\$(@B).def bin\$(@B).res
    @echo $(MAKEDIR)\makefile: Now linking "$(@B)"
!ifdef EMX
    $(LINK_EXE) -o $(@B).exe $(WPI2EXEOBJS) src\wpi2exe\$(@B).def
!else
    $(LINK_EXE) /NODEFAULT os2386.lib cppom30o.lib /OUT:$(@B).exe src\wpi2exe\$(@B).def $(WPI2EXEOBJS)
!ifndef WPI_DEBUG
!ifndef EMX
    mapsym -l $(MAPDIR)\$(@B).map
!endif
!endif
!endif
    $(RC_NOPACK) bin\$(@B).res $(@B).exe

# compose inf2html.exe
tools\inf2html.exe: $(I2HOBJS) tools\inf2html\$(@B).def
    @echo $(MAKEDIR)\makefile: Now linking "$(@B)"
!ifdef EMX
    $(LINK_EXE) -o tools\$(@B).exe tools\inf2html\$(@B).def $(I2HOBJS)
!else
    $(LINK_EXE) /OUT:tools\$(@B).exe tools\inf2html\$(@B).def $(I2HOBJS)
!endif

#
# Special target "dlgedit": this is not called by "all".
# Note that you need DLGEDIT.EXE from the Toolkit for this.
#

dlgedit:
# added (99-10-24) [umoeller]
    @echo $(MAKEDIR)\makefile: Calling DLGEDIT.EXE
    @cd $(WPI_NLS_CODE)\frontend.res
# rebuild RES file in bin
    @$(RUN_SUB_NMAKE) all
# copy RES file to frontend.res so dlgedit finds it
    @cmd.exe /c copy ..\..\bin\warpin.res
    @cmd.exe /c copy ..\..\include\frontend\dlgids.h
# invoke DLGEDIT
    dlgedit warpin.res
# move newly created RES file back to \bin
    @cmd.exe /c copy warpin.res ..\..\bin
    @cmd.exe /c del warpin.res
    @cmd.exe /c del dlgids.h
    @cd ..\..
# rebuild all
    @echo $(MAKEDIR)\makefile: Recursing to ourselves to rebuild frontend
    @$(RUN_SUB_NMAKE) frontend
    @echo $(MAKEDIR)\makefile: Recursing to ourselves to relink resources
    @$(RUN_SUB_NMAKE) link

#
# Special target "release": this is not called by "all",
# but must be set on the NMAKE command line.
#

release: all
!ifndef WPI_RELEASE
!error WPI_RELEASE must be set before calling "make release". Terminating.
!endif
# create directories
!if [@md $(WPI_RELEASE) 2> NUL]
!endif
!if [@md $(WPI_RELEASE)\30000 2> NUL]
!endif
!if [@md $(WPI_RELEASE)\30001 2> NUL]
!endif
!if [@md $(WPI_RELEASE)\30001\test 2> NUL]
!endif
    @echo $(MAKEDIR)\makefile: Now copying files to $(WPI_RELEASE).
    copy COPYING $(WPI_RELEASE)\30001
    copy warpin.exe $(WPI_RELEASE)\30000
    copy wizilla.exe $(WPI_RELEASE)\30001
    copy wic.exe $(WPI_RELEASE)\30001
    copy wicpm.exe $(WPI_RELEASE)\30001
#    copy wpi2exe.exe $(WPI_RELEASE)\30001
    copy wpirtl.dll $(WPI_RELEASE)\30000
    copy wicpm.inf $(WPI_RELEASE)\30001
    copy wpi_prog.inf $(WPI_RELEASE)\30001
    copy wpi_user.inf $(WPI_RELEASE)\30001
    copy warpin.sym $(WPI_RELEASE)\30001
    copy wic.sym $(WPI_RELEASE)\30001
    copy wicpm.sym $(WPI_RELEASE)\30001
    copy wpirtl.sym $(WPI_RELEASE)\30001
    copy warpin.hlp $(WPI_RELEASE)\30000
    copy warpin.tmf $(WPI_RELEASE)\30000
    copy readme.txt $(WPI_RELEASE)\30001
    copy history.txt $(WPI_RELEASE)\30001
    copy envwic.cmd $(WPI_RELEASE)\30001
    copy test\apps.cmd $(WPI_RELEASE)\30001\test
    copy test\apps.wis $(WPI_RELEASE)\30001\test
    copy stub.exe $(WPI_RELEASE)\30001


