#
# makefile for vile on WIN32 using Microsoft Visual C++
#
# $Header: /usr/build/vile/vile/RCS/makefile.wnt,v 1.109 2014/03/30 19:56:44 tom Exp $
#
#
!include <ntwin32.mak>

###############################################################################
# These are the symbols that you may wish to set on the command line (do not set
# nmake symbols directly, since that disallows updating those within the makefile,
# e.g., see how $(MY_CCOPT) is built up):
#	CFG - primary configuration (console vs windows)
#	DBG - nonempty for debugging
#	FLT - nonempty for built-in filters
#	LEX - name of flex/lex program, if any (needed to make most filters).
#	OPT - secondary configuration (extra features)
#	ICON - winvile's icon
#	WIDE - nonempty for Unicode build
#	TRACE - nonempty to suppress tracing when building a debug-executable.
#	PROGNAME - vile/winvile $progname value
#	VILEDEFS - other options, as needed
###############################################################################

# you can override the icon to suit your taste (e.g,. vile, kitchen-sink,
# or V-eye pumpkin).

ICON	= vile

!IF "$(CFG)" == ""
CFG=vile
!MESSAGE No configuration (CFG) specified.  Defaulting to vile (console)
!ENDIF

!IF ("$(CFG)" != "vile" && "$(CFG)" != "winvile"  &&                     \
     "$(CFG)" != "vile-with-perl" && "$(CFG)" != "winvile-with-perl") || \
    ("$(OPT)" != "" && "$(OPT)" != "ole-auto") ||                        \
        ("$(OPT)" == "ole-auto" &&                                       \
        ("$(CFG)" == "vile" || "$(CFG)" == "vile-with-perl"))
!MESSAGE Invalid configuration specified!
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE on this makefile
!MESSAGE by defining a CFG and/or OPT macro on the command line.  Possible
!MESSAGE configuration choices are:
!MESSAGE
!MESSAGE vile              - console
!MESSAGE vile-with-perl    - console and embedded perl
!MESSAGE winvile           - gui
!MESSAGE winvile-with-perl - gui and embedded perl
!MESSAGE
!MESSAGE The 'with-perl' configurations have two preconditions which must be
!MESSAGE met prior to initiating the build.  Refer to the file "mkprlenv.wnt"
!MESSAGE for more details.
!MESSAGE
!MESSAGE The only supported OPT value is:
!MESSAGE
!MESSAGE ole-auto          - add basic ole automation to winvile
!MESSAGE
!MESSAGE Examples:
!MESSAGE
!MESSAGE /* build console editor */
!MESSAGE nmake -f makefile.wnt
!MESSAGE
!MESSAGE /* build gui editor with embedded perl and basic ole automation */
!MESSAGE nmake -f makefile.wnt CFG=winvile-with-perl OPT=ole-auto
!MESSAGE
!ERROR An invalid configuration is specified.
!ENDIF

# $(cflags), defined via ntwin32.mak, is a critical compiler resource.
# This variable controls what OS features, such as a mouse wheel, can be
# accessed via include files.
CFLWIN32     = $(cflags:-W3=)      # strip out warning option
CFLWIN32     = $(CFLWIN32:-c=)     # strip out -c flag

# remove the "#" to get debugging info (and a larger executable)
CC_DEBUG     = -W4 #$(cdebug)
LD_DEBUG     = #$(ldebug)

# Other vars
CON_LDFLAGS  = -nologo -pdb:none
DEVNULL      = nul
PERLCFG      = $(DEVNULL)
THE_PERLCFG  = perl_env.inc

!IF EXIST("$(THE_PERLCFG)")
!INCLUDE "$(THE_PERLCFG)"
!ENDIF

# debug/release configurations
!IF  "$(DBG)" != ""
MY_CCOPT = $(cdebug) -DVILE_ERROR_ABORT
MY_LDOPT = $(ldebug)
!IF "$(TRACE)" == ""
DBGOBJS  = trace.obj
MY_CCOPT = $(MY_CCOPT) -DOPT_TRACE
!ENDIF
!else
MY_LDOPT = -pdb:none
!endif

# options common to all configurations
MY_CCOPT = $(MY_CCOPT) -DOPT_LOCALE -I. -Ifilters

!IF  "$(VILEDEFS)" != ""
MY_CCOPT = $(MY_CCOPT) $(VILEDEFS)
!endif

!IF  "$(WIDE)" != ""
MY_CCOPT = $(MY_CCOPT) -DUNICODE
!endif

!IF  "$(CFG)" == "vile" || "$(CFG)" == "vile-with-perl"
DISP_DEF = -DDISP_NTCONS
TARGET   = vile
CFLAGS   = $(cvarsmt) $(CC_DEBUG) -nologo -DWIN32_LEAN_AND_MEAN \
	   -Dscrn_chosen $(DISP_DEF) $(PERLOPTS) $(MY_CCOPT) $(CFLWIN32)
LDFLAGS  = -nologo $(MY_LDOPT)
SCREEN   = ntconio
LIBS     = $(LD_DEBUG) $(conlflags) $(conlibsmt) user32.lib comdlg32.lib \
           ole32.lib shell32.lib $(VILE_PERL_LIB)
EXTRAOBJ = dumbterm.obj
!endif

!IF  "$(CFG)" == "winvile" || "$(CFG)" == "winvile-with-perl"
DISP_DEF = -DDISP_NTWIN
TARGET   = winvile
CFLAGS   = $(cvarsmt) $(CC_DEBUG) -nologo -DWIN32_LEAN_AND_MEAN \
	   -Dscrn_chosen $(DISP_DEF) $(PERLOPTS) $(MY_CCOPT) $(CFLWIN32)
LDFLAGS  = -nologo /subsystem:windows /incremental:no $(MY_LDOPT)
SCREEN   = ntwinio
LIBS     = $(LD_DEBUG) $(guilflags) $(guilibsmt) ole32.lib shell32.lib \
           $(VILE_PERL_LIB) $(OLELIBS)
EXTRAOBJ = makeargv.obj
EXTRA    = winvile.res
!endif

!IF "$(PROGNAME)" != "" && "$(PROGNAME)" != "$(TARGET)"
!MESSAGE Overriding target name to $(PROGNAME)
TARGET   = $(PROGNAME)
CFLAGS   = $(CFLAGS) -DPROGRAM_NAME=\"$(PROGNAME)\"
!endif

MAPFILE  = $(TARGET).map
ZIPFILE  = $(TARGET).zip
EXEFILE  = $(TARGET).exe

!IF "$(OPT)" == "ole-auto"
CFLAGS   = $(CFLAGS) -DVILE_OLE
OLEOBJS  = w32ole.obj w32reg.obj w32ole.res
OLESRCS  = w32ole.cpp w32reg.c
WRAPUTIL = wvwrap.exe
!IF  "$(WIDE)" != ""
OLELIBS  = uuid.lib oleaut32.lib comsuppw.lib
!else
OLELIBS  = uuid.lib oleaut32.lib comsuppw.lib
!endif
EXTRA    =
!endif

!IF "$(CFG)" == "winvile-with-perl" || "$(CFG)" == "vile-with-perl"
#
# CPAN's most recent X86 binary perl distribution, dated 12-Nov-97, does
# not export perl's record separator symbol ($/).  Consequently, the
# preprocessor macro HAVE_BROKEN_PERL_RS must be defined to permit
# successful linking of perl-embedded versions of the editor.  When this
# macro is defined, READLINE performance in perl.xs may be adversely
# affected.  Comment out the makefile macro "PERLFIX" if your version of
# perl properly exports $/ .
#
PERLFIX  = -DHAVE_BROKEN_PERL_RS
PERLOPTS = -DOPT_PERL=1 -I$(VILE_PERL_INC) $(PERLFIX)
PERLOBJS = api.obj perl.obj watch.obj
PERLSRCS = api.c perl.c watch.c
PERLCFG  = $(THE_PERLCFG)
#
# The following perl scripts, stored in the subdirectory .\perl, are
# known to work under win32:
#
PERL_SCRIPTS = \
	perl/Breadcrumbs.pm \
	perl/CaptHook.pm \
	perl/Glob2re.pm \
	perl/Vile/Manual.pm \
	perl/Vile/exporter.pm \
	perl/Visit.pm \
	perl/directory.pm \
	perl/dirlist.pm \
	perl/hgrep.pm \
	perl/search.pm
!ENDIF

MYLIBS	=

# these headers are built by the mktbls program from the information in cmdtbl
# and in modetbl
MYHDRS	= \
	nebind.h \
	neexec.h \
	nefkeys.h \
	nefsms.h \
	nefunc.h \
	nemode.h \
	nename.h \
	neproto.h \
	nevars.h

FLTLIB	= filters\builtflt.lib

!IF  "$(FLT)" != ""
EXTRA	= $(EXTRA) builtflt.obj
# MYHDRS	= $(MYHDRS) builtflt.h
MYLIBS	= $(MYLIBS) $(FLTLIB)
CFLAGS	= $(CFLAGS) -DOPT_FILTER -Ifilters -I.
!IF  "$(LEX)" != ""
CFLAGS	= $(CFLAGS) -DOPT_LEX_FILTER
!endif
!endif

# these are normal editable headers
HDRS = estruct.h edef.h proto.h dirstuff.h

SRC = 	main.c $(SCREEN).c \
	basic.c bind.c blist.c btree.c buffer.c charsets.c \
	csrch.c display.c eightbit.c encrypt.c eval.c exec.c \
	externs.c fences.c file.c filec.c \
	fileio.c finderr.c glob.c globals.c history.c \
	input.c insert.c itbuff.c isearch.c \
	line.c modes.c msgs.c nullterm.c oneliner.c opers.c \
	path.c random.c regexp.c \
	region.c search.c select.c spawn.c statevar.c \
	tags.c tbuff.c termio.c ucrypt.c undo.c vl_ctype.c \
	version.c window.c word.c wordmov.c map.c \
	w32cbrd.c w32cmd.c w32misc.c w32oo.cpp w32pipe.c \
	wcwidth.c $(PERLSRCS) \
	$(OLESRCS)

OBJ = 	main.obj $(SCREEN).obj \
	basic.obj bind.obj blist.obj btree.obj buffer.obj charsets.obj \
	csrch.obj display.obj eightbit.obj encrypt.obj eval.obj exec.obj \
	externs.obj fences.obj file.obj filec.obj \
	fileio.obj finderr.obj glob.obj globals.obj history.obj \
	input.obj insert.obj itbuff.obj isearch.obj \
	line.obj modes.obj msgs.obj nullterm.obj oneliner.obj opers.obj \
	path.obj random.obj regexp.obj \
	region.obj search.obj select.obj spawn.obj statevar.obj \
	tags.obj tbuff.obj termio.obj ucrypt.obj undo.obj vl_ctype.obj \
	version.obj window.obj word.obj wordmov.obj map.obj \
	w32cbrd.obj w32cmd.obj w32misc.obj w32oo.obj w32pipe.obj \
	wcwidth.obj w32info.res $(PERLOBJS) \
	$(OLEOBJS) $(DBGOBJS) $(EXTRAOBJ) $(EXTRA)

.c.obj:
	$(cc) $(CFLAGS) -DNO_INLINE -c $< -Fo$@

.cpp.obj:
	$(cc) $(CFLAGS) -c $< -Fo$@

all :: $(PERLCFG) $(EXEFILE) $(WRAPUTIL)

sources ::	$(MYHDRS)

$(EXEFILE) : 	$(MYHDRS) $(OBJ) $(MYLIBS)
	$(link) $(LDFLAGS) $(OBJ) $(MYLIBS) $(LIBS) -out:$(EXEFILE) -map:$(MAPFILE)

w32info.res :	w32info.rc
	$(RC) w32info.rc

winvile.res :	winvile.rc winvile.h
	$(RC) -DVILE_ICON="$(ICON).ico" -Iicons winvile.rc

# collect some perl environment info
!if "$(PERLCFG)" != "$(DEVNULL)"
!if !exist("$(PERLCFG)")
$(PERLCFG): $(PERL_SCRIPTS) mkprlenv.wnt
	echo making PERLCFG $(PERLCFG)
!INCLUDE "mkprlenv.wnt"
	echo ...made PERLCFG $(PERLCFG)
!endif
!endif

$(OBJ): estruct.h edef.h proto.h $(MYHDRS)
builtflt.obj : builtflt.h

MAKE_FLT = FLT=$(FLT) LEX="$(LEX)" DBG="$(DBG)" SCREEN="$(SCREEN)" -$(MAKEFLAGS)

builtflt.h :
	cd filters
	$(MAKE) -f makefile.wnt genmake.exe $(MAKE_FLT)
	mk-0th.bat genmake.mak ..\$@
	cd ..

$(FLTLIB) : $(MYHDRS)
	cd filters
	$(MAKE) -f makefile.wnt all $(MAKE_FLT)
	cd ..

nebind.h \
nefkeys.h \
neexec.h \
nefunc.h \
neproto.h \
nename.h :	cmdtbl mktbls.exe
	mktbls.exe cmdtbl

nefsms.h \
nevars.h \
nemode.h:	modetbl mktbls.exe
	mktbls.exe modetbl

perl.c: perl.xs ptypemap
	perl $(VILE_PERL_UTILS)\xsubpp -typemap $(VILE_PERL_UTILS)\typemap \
		-typemap ptypemap perl.xs > $@

vile-perl-api.pod: perl.xs
	perl -lne "print if s/^\s{1,2}#\s{0,1}//" perl.xs > $@

vile-perl-api.doc: vile-perl-api.pod
	perl -e "use Pod::Text; pod2text(\"$?\");" > $@

wvwrap.exe: wvwrap.obj makeargv.obj w32info.res
 	$(link) $(LDFLAGS) wvwrap.obj makeargv.obj w32info.res $(LIBS) -out:$(@)

mktbls.exe:  mktbls.c
	$(cc) $(CFLAGS) mktbls.c -Fomktbls -link $(CON_LDFLAGS)

w32ole.res: w32ole.rc winvile.tlb
	$(RC) $(DISP_DEF) -DVILE_ICON="$(ICON).ico" -Iicons w32ole.rc

winviletlb.h winvile.tlb: winvile.odl
	midl -h winviletlb.h -tlb winvile.tlb winvile.odl

test_btree.exe :	btree.c
	$(CC) -Fo$* -DDEBUG_BTREE $(CPPFLAGS) $(CFLAGS) btree.c $(LDFLAGS)

test_regexp.exe :	regexp.c
	$(CC) -Fo$* -DDEBUG_REGEXP $(CPPFLAGS) $(CFLAGS) -DNO_INLINE regexp.c $(LDFLAGS)

TEST_IO_OBJS	= \
	$(SCREEN).obj \
	nullterm.obj \
	test_io.obj

test_io.exe :		$(TEST_IO_OBJS)
	$(link) $(TEST_IO_OBJS) $(LDFLAGS) $(LIBS) -out:$(@)

clean ::
	-del *.bak
	-del *.exe
	-del *.ilk
	-del *.map
	-del *.pdb
	-del *.obj
	-del ne*.h
	-del $(THE_PERLCFG)
	-del builtflt.h
	-del perl.c
	-del winviletlb.h
	-del winvile_i.c
	-del *.tlb
	-del *.res

dist : $(EXEFILE)
	- del $(ZIPFILE)
	echo "$(TARGET)", vi-like-emacs for win32 >zipnote.txt
	zip $(TARGET).zip readme.pc readme $(EXEFILE) $(MAPFILE) vile.hlp
	zip -z $(ZIPFILE) <zipnote.txt
	- del zipnote.txt
	cd macros
	zip ..\$(ZIPFILE) *.rc
	cd ..\filters
	zip ..\$(ZIPFILE) *.rc
	$(MAKE) -f makefile.wnt $@ $(MAKE_FLT)
	cd ..

all \
clean ::
	cd filters
	$(MAKE) -f makefile.wnt $@ $(MAKE_FLT)
	cd ..

SCRIPTS	= \
	filters\filters.rc \
	filters\spell.rc \
	macros\complete.rc \
	macros\dates.rc \
	macros\digraphs.rc \
	macros\gnugpg.rc \
	macros\loaderrs.rc \
	macros\manpage.rc \
	macros\modes.rc \
	macros\palettes.rc \
	macros\pictmode.rc \
	macros\search.rc \
	macros\shifts.rc \
	macros\showeach.rc \
	macros\vileinit.rc \
	macros\vileperl.rc \
	macros\which.rc

!if "$(VILE_STARTUP_PATH)" == ""
all ::
	@ echo Sorry, you have not set VILE_STARTUP_PATH
!else if !exist("$(VILE_STARTUP_PATH)")
all :: $(SCRIPTS) vile.hlp
	@echo Sorry, these are installed only if VILE_STARTUP_PATH is a single directory
	!@echo "      $(**F)"
!else
all :: $(SCRIPTS) vile.hlp
	echo installing $(SCRIPTS) in $(VILE_STARTUP_PATH)
	- !copy $** "$(VILE_STARTUP_PATH)"\$(**F)
!endif

api.obj :	api.h
bind.obj :	btree.h
blist.obj :	blist.h
btree.obj :	btree.h
display.obj :	pscreen.h
filec.obj :	dirstuff.h
glob.obj :	dirstuff.h
main.obj :	chgdfunc.h
modes.obj :	chgdfunc.h
ntwinio.obj :	winvile.h pscreen.h patchlev.h makeargv.h
path.obj :	dirstuff.h
perl.obj :	api.h
random.obj :	dirstuff.h
statevar.obj :	patchlev.h
tags.obj :	btree.h
version.obj :	patchlev.h
w32cmd.obj :	winvile.h
w32ole.obj :	w32ole.h winviletlb.h w32reg.h
w32oo.obj :	dirstuff.h
w32reg.obj :	w32reg.h
wvwrap.obj :	w32reg.h w32ole.h winviletlb.h makeargv.h
