#
# Makefile for TeX Live XeTeX and LuaTeX
#
version = 2014

.SUFFIXES:

SHELL = sh
MAKE = make
top_srcdir = .
srcdir = .

CC = cl

WD = setargv.obj

# -----------------------------------------------------------------------
#
# Release (with lua52 DLL)
#
CFLAGS = -nologo -O2 -MT -wd4819 -DWIN32=1 -D_CRT_SECURE_NO_DEPRECATE=1 \
-D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_OBSOLETE_NO_DEPRECATE \
-D_SECURE_SCL=0 -DNO_KPSE_DLL=1 -DLUA_BUILD_AS_DLL=1 -I. -I.. -Isynctexdir \
-DLUA_COMPAT_MODULE=1 $(XCFLAGS)

LKDEBUG =

# -----------------------------------------------------------------------
#
# Full Debugging (with lua52 DLL)
#
#CFLAGS = -nologo -Od -Zi -MT -wd4819 -DWIN32=1 -D_CRT_SECURE_NO_DEPRECATE=1 \
#-D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_OBSOLETE_NO_DEPRECATE \
#-D_SECURE_SCL=0 -DNO_KPSE_DLL=1 -DLUA_BUILD_AS_DLL=1 -I. -I.. -Isynctexdir \
#-DLUA_COMPAT_MODULE=1 $(XCFLAGS)
#
#LKDEBUG = /DEBUG

# -----------------------------------------------------------------------

#
# luatex
# not used now
#EXTRA_VERSION=`date +-%Y%m%d%H`
#
#
#
#XCPPFLAGS = -I$(LIBPNGDIR) -I$(LIBLUADIR) -Ilualibs -I$(LIBBSDDIR) \
#-I$(LIBBSDDIR)/.. -Dextra_version_info=$(EXTRA_VERSION)
#

XCPPFLAGS = -I$(LIBPNGDIR) -I$(LIBLUADIR) -Ilualibs -I$(LIBBSDDIR) \
-I$(LIBBSDDIR)/..

CPPFLAGS = $(XCPPFLAGS)

DEFS = -DICUFORXeTeX=1 -DHAVE_CONFIG_H=1 -DNO_KPSE_DLL=1 \
$(XDEFS)

LIBTOOL =

#
ALL_CPPFLAGS = $(DEFS) -I. -I../../libs/fontconfig \
-I../../libs/freetype/include -I$(srcdir) \
-I$(kpathsea_parent) -I$(kpathsea_srcdir_parent) \
$(prog_cflags) $(CPPFLAGS)

ALL_CFLAGS = $(ALL_CPPFLAGS) $(CFLAGS)
compile = $(CC) $(ALL_CFLAGS) -c

.SUFFIXES:
.SUFFIXES: .c .obj
.c.obj:
	$(compile) $<

#
# Creating hard links (use copy)
#
LN = cp

#
# kpathsea
#
kpathsea_parent = ..
kpathsea_dir = $(kpathsea_parent)/kpathsea
kpathsea_srcdir_parent = $(top_srcdir)/..
kpathsea_srcdir = $(kpathsea_srcdir_parent)/kpathsea
kpathsea = $(kpathsea_dir)/libkpathsea.lib

XLDFLAGS = -link /SUBSYSTEM:CONSOLE
LDFLAGS =  $(XLDFLAGS)
LDFLAGSXX = $(XLDFLAGS) /STACK:0x280000

LIBS = wsock32.lib
XLOADLIBES = user32.lib advapi32.lib shell32.lib

ZZLOADLIBES = gdi32.lib

LOADLIBES = $(proglib) $(kpathsea) $(LIBS) $(XLOADLIBES)


CCLD = $(CC)
link_command = $(CCLD) -Fe$@

kpathsea_link = $(link_command)

CXXLD = cl
cxx_link_command = $(CXXLD) -Fe$@
kpathsea_cxx_link = $(cxx_link_command)

x_cppflags = 
x_ldflags = 
x_pre_libs = 
x_extra_libs = 
x_tool_libs = 
x_ext_lib = 
wlibs =

windowlink = $(windowlib) $(x_ldflags) $(x_tool_libs) $(x_pre_libs) \
$(x_ext_lib) $(wlibs) $(x_extra_libs)

socketlibs = 

common_makeargs = $(MFLAGS) CC='$(CC)' CFLAGS='$(CFLAGS)' $(XMAKEARGS)
window_makeargs = x_cppflags='$(x_cppflags)' $(common_makeargs)
install_makeargs = INSTALL='$(INSTALL)' INSTALL_DATA='$(INSTALL_DATA)' \
  INSTALL_PROGRAM='$(INSTALL_PROGRAM)' \
  $(makevars) $(common_makeargs)  

#
# libraries
#
proglib = lib/lib.lib
windowlib = window/window.lib
#

#
# The sh script that does the conversion:
#
web2c = srcdir=$(srcdir) $(shared_env) $(SHELL) $(srcdir)/web2c/convert
#
# Additional dependencies:
#
web2c_aux = web2c/convert web2c/common.defines
web2c_programs = web2c/fixwrites.exe web2c/splitup.exe web2c/web2c.exe

.SUFFIXES: .p .c .ch .p
.p.c: # really depends on $(web2c_programs), and generates .h.
	$(web2c) $*
.ch.p: # really depends on ./tangle; for mf/mp/tex, also generates .pool
	$(shared_env) ./tangle $(srcdir)/$*.web $<

# Prevent Make from deleting the intermediate forms.
.PRECIOUS: %.ch %.p %.c

xetex = xetex.exe
xetexdll = xetex.dll
otangle = otangle.exe

#
# programs
#
programs = tie.exe $(otangle) tangle.exe $(xetex) luatex.dll

#programs = tie.exe $(otangle) tangle.exe $(xetex) luatex.dll pdftex.dll \
#ttf2afm.exe pdftosrc.exe synctex.exe


default all: $(programs)

#
# XeTeX W32
#
xetex_platform_o = XeTeXFontMgr_FC.obj
xetex_images_o = pdfimage.obj bmpimage.obj \
jpegimage.obj mfileio.obj numbers.obj pngimage.obj XeTeX_pic.obj

XETEX_DEFINES = -DXETEX_OTHER=1

CC = cl
CXXX = cl -TP -EHsc

ZLIBDIR=../../libs/zlib
ZLIBCPPFLAGS=-I$(ZLIBDIR)
ZLIBSRCDIR=$(ZLIBDIR)
LIBPNGDIR=../../libs/libpng
LIBPNGSRCDIR=$(LIBPNGDIR)
LIBPNGDEP = $(LIBPNGDIR)/libpng.lib
LDLIBPNG = $(LIBPNGDIR)/libpng.lib
ZLIBDEP = $(ZLIBDIR)/libz.lib
LDZLIB = $(ZLIBDIR)/libz.lib
LIBBSDDIR =
LIBBSDDEP =
LDLIBBSD =

FREETYPE2DIR = ../../libs/freetype
FREETYPE2SRCDIR = ../../libs/freetype

GRAPHITE2DIR = ../../libs/graphite2-1.2.4
GRAPHITE2FLAGS = -DGRAPHITE2_STATIC=1 -I$(GRAPHITE2DIR)/include -I$(GRAPHITE2DIR)/include/graphite2
GRAPHITE2LIB = $(GRAPHITE2DIR)/lib/graphite2.lib

HARFBUZZDIR = ../../libs/harfbuzz/harfbuzz-0.9.40
HARFBUZZFLAGS = -D__HP_cc=1 -I$(HARFBUZZDIR)/src
HARFBUZZLIB = $(HARFBUZZDIR)/src/harfbuzz.lib

LIBPNGCPPFLAGS = -I$(LIBPNGDIR) -I../../libs/zlib

POPPLERDIR = ../../libs/poppler/poppler-0.32.0
LIBPOPPLERCPPFLAGS = -I$(POPPLERDIR) -I$(POPPLERDIR)/poppler \
-I$(POPPLERDIR)/fofi -I$(POPPLERDIR)/goo




POPPLERCPPDIR = $(POPPLERDIR)/cpp
POPPLERCPPLIB = $(POPPLERDIR)/poppler_cpp.lib
POPPLERCPPOBJ = get_poppler_version.obj
POPPLERCPPSRC = xetexdir/get_poppler_version.cpp




POPPLERPNGLIBS = ../../libs/libpng/libpng.lib \
$(LDLIBPOPPLER) \
../../libs/zlib/libz.lib

FREETLIB = ../../libs/freetype/objs/freetype.lib
FONTCONFLIB = ../../libs/fontconfig/src/libfontconfig.lib
EXPATLIB = ../../libs/expat/lib/libexpat.lib

FRAMEWORKS = $(FREETLIB) $(FONTCONFLIB) $(EXPATLIB) $(POPPLERPNGLIBS) \
$(GRAPHITE2LIB) $(HARFBUZZLIB) \
gdi32.lib user32.lib oleaut32.lib advapi32.lib

# FRAMEWORKS = $(FREETLIB) $(FONTCONFLIB) $(EXPATLIB) $(POPPLERPNGLIBS) \
# $(GRAPHITE2LIB) $(HARFBUZZLIB) $(POPPLERCPPLIB) \
# gdi32.lib user32.lib oleaut32.lib advapi32.lib

XDEFS = $(XETEX_DEFINES)

#
# synctex
#
synctex_dir = $(srcdir)/synctexdir
synctex_common_texd = \
		echo "Enabling SyncTeX Support."

### SyncTeX support in xetex
# list of change files
xetex_ch_synctex = \
	$(synctex_dir)/synctex-xe-def.ch0 \
	$(synctex_dir)/synctex-mem.ch0 \
	$(synctex_dir)/synctex-e-mem.ch0 \
	$(synctex_dir)/synctex-e-mem.ch1 \
	$(synctex_dir)/synctex-rec.ch0 \
	$(synctex_dir)/synctex-e-rec.ch0

#xetex_ch_synctex = \
#	$(synctex_dir)/synctex-xe-def.ch0 \
#	$(synctex_dir)/synctex-mem.ch0 \
#	$(synctex_dir)/synctex-e-mem.ch1 \
#	$(synctex_dir)/synctex-rec.ch0 \
#	$(synctex_dir)/synctex-xe-rec.ch0

xetex_ch_srcs-without_synctex =
xetex_post_ch_synctex = \
	$(synctex_dir)/synctex-xe-rec.ch3
xetex_post_ch_srcs-without_synctex =
# The C sources and headers
xetex_o-with_synctex = synctex-xe.obj
xetex_o-without_synctex = 
synctex-xe.h: $(synctex_dir)/synctex.h
	cat $(synctex_dir)/synctex.h >$@
synctex-xetex.h: $(synctex_dir)/synctex-xetex.h
	cat $(synctex_dir)/synctex-xetex.h >synctex-xetex.h
synctex-xe.c: xetexd.h $(synctex_dir)/synctex.c synctex-xetex.h
	cp $(synctex_dir)/synctex.c $@

xetexd.h-with_synctex = if test -z "`grep __SyncTeX__ xetexd.h`";\
	then\
		$(synctex_common_texd);\
		sed -f synctex_sed_command.sed xetexd.h > synctex_xetexd.h;\
 		mv synctex_xetexd.h xetexd.h;\
		if test -z "`grep __SyncTeX__ xetexd.h`";\
		then\
			echo "warning: SyncTeX activation FAILED";\
			exit 1;\
		fi;\
		echo "warning: SyncTeX is enabled";\
 	fi

xetexd.h-without_synctex = echo "warning: SyncTeX is NOT enabled"

#
# The C sources.
#
xetex_c = xetexini.c xetex0.c
xetex_o = xetexini.obj xetex0.obj \
xetexextra.obj trans.obj XeTeX_ext.obj xetex-pool.obj \
$(xetex_o-with_synctex) $(xetex_platform_o) hz.obj

dllxetex_o = xetexini.obj xetex0.obj \
xetexdll.obj trans.obj XeTeX_ext.obj xetex-pool.obj \
$(xetex_o-with_synctex) $(xetex_platform_o) hz.obj

# Layout library sources
xetex_ot_layout_o = \
		XeTeXFontMgr.obj \
		XeTeXLayoutInterface.obj \
		XeTeXFontInst.obj \
		XeTeXOTMath.obj

xetex_ot_layout_cxx = \
		XeTeXFontMgr.cpp \
		XeTeXLayoutInterface.cpp \
		XeTeXFontInst.cpp
#
# icu
#
icudir = icu-551

ICUCFLAGS = \
	-I../../libs/$(icudir)/source/common/unicode \
	-I../../libs/$(icudir)/source/common \
	-I../../libs/$(icudir)/source/layout \
	-I../../libs/$(icudir)/source \
	-DLE_USE_CMEMORY \
	-I. -I..

ALL_CXXFLAGS=$(ALL_CFLAGS)

#
#
#
TECKITFLAGS = -I../../libs

XXLIBS = $(LIBPOPPLERCPPFLAGS) -I../../libs/libpng -I../../libs/zlib
XCFLAGS = -I. -I.. $(TECKITFLAGS) $(XXLIBS) $(ICUCFLAGS) \
$(GRAPHITE2FLAGS) $(HARFBUZZFLAGS)

XETEXINC = -Ixetexdir

FNTCFGFLAGS = -I../../libs/fontconfig
FTFLAGS = $(FNTCFGFLAGS) -I../../libs/freetype -I../../libs/freetype/include \
-Ixetexdir

$(POPPLERCPPOBJ): $(POPPLERCPPSRC)
	$(CXXX) $(ICUCFLAGS) $(ALL_CXXFLAGS) \
-DPOPPLER_STATIC -I$(POPPLERCPPDIR) -c -Fo$@ $<

XeTeXLayoutInterface.obj: $(srcdir)/xetexdir/XeTeXLayoutInterface.cpp
	$(CXXX) $(ICUCFLAGS) $(FTFLAGS) $(GRAPHITE2FLAGS) \
$(HARFBUZZFLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

XeTeXFontMgr.obj: $(srcdir)/xetexdir/XeTeXFontMgr.cpp \
$(srcdir)/xetexdir/XeTeXFontMgr.h
	$(CXXX) $(FTFLAGS) $(HARFBUZZFLAGS) $(ICUCFLAGS) \
$(GRAPHITE2FLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

XeTeXFontMgr_FC.obj: $(srcdir)/xetexdir/XeTeXFontMgr_FC.cpp
	$(CXXX) $(ICUCFLAGS) $(FTFLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

XeTeXFontInst.obj: $(srcdir)/xetexdir/XeTeXFontInst.cpp
	$(CXXX) $(ICUCFLAGS) $(FTFLAGS) \
$(GRAPHITE2FLAGS) $(HARFBUZZFLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

XeTeXFontInst_Mac.obj: $(srcdir)/xetexdir/XeTeXFontInst_Mac.cpp
	$(CXXX) $(ICUCFLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

XeTeXOTMath.obj: $(srcdir)/xetexdir/XeTeXOTMath.cpp
	$(CXXX) $(ICUCFLAGS) $(FTFLAGS) \
$(GRAPHITE2FLAGS) $(HARFBUZZFLAGS) $(ALL_CXXFLAGS) -c -Fo$@ $<

#
#xetexlibs = ../../libs/$(icudir)/lib/iculayoutengine.lib \
#            ../../libs/$(icudir)/lib/icuucommon.lib \
#	     ../../libs/$(icudir)/lib/icudt.lib \
#            ../../libs/teckit/lib/libTECkit.lib
#

xetexlibs = ../../libs/$(icudir)/lib/icuucommon.lib \
            ../../libs/$(icudir)/lib/icudt.lib \
            ../../libs/teckit/lib/libTECkit.lib

# special rules for files that need the TECkit headers as well
XeTeX_ext.obj: $(srcdir)/xetexdir/XeTeX_ext.c xetexd.h
	$(compile) $(ICUCFLAGS) $(FTFLAGS) $(TECKITFLAGS) \
$(GRAPHITE2FLAGS) $(HARFBUZZFLAGS) -Fo$@ $<

XeTeX_mac.obj: $(srcdir)/xetexdir/XeTeX_mac.c xetexd.h
	$(compile) $(ICUCFLAGS) $(TECKITFLAGS) -Fo$@ $<

trans.obj: $(srcdir)/xetexdir/trans.c
	$(compile) -Fo$@ $<

# image support
bmpimage.obj: $(srcdir)/xetexdir/image/bmpimage.c
	$(compile) -I$(srcdir)/xetexdir/image -Fo$@ $<

jpegimage.obj: $(srcdir)/xetexdir/image/jpegimage.c
	$(compile) -I$(srcdir)/xetexdir/image -Fo$@ $<

mfileio.obj: $(srcdir)/xetexdir/image/mfileio.c
	$(compile) -I$(srcdir)/xetexdir/image  -Fo$@ $<

numbers.obj: $(srcdir)/xetexdir/image/numbers.c
	$(compile) -I$(srcdir)/xetexdir/image -Fo$@ $<

pngimage.obj: $(srcdir)/xetexdir/image/pngimage.c
	$(compile) $(LIBPNGCPPFLAGS) -I$(srcdir)/xetexdir/image -Fo$@ $<

XeTeX_pic.obj: $(srcdir)/xetexdir/XeTeX_pic.c
	$(compile) $(TECKITFLAGS) -Fo$@ $<

pdfimage.obj: $(srcdir)/xetexdir/pdfimage.cpp
	$(CXXX) $(ALL_CFLAGS) $(LIBPOPPLERCPPFLAGS) -I../kpathsea -c -Fo$@ $<

hz.obj: $(srcdir)/xetexdir/hz.cpp
	$(CXXX) $(XETEXINC) $(ALL_CFLAGS) $(LIBPOPPLERCPPFLAGS) -c -Fo$@ $<

# sync
synctex-xe.obj: synctex-xe.c
	$(compile) -DSYNCTEX_ENGINE_H='<synctex-xetex.h>' $(TECKITFLAGS) $(FTFLAGS) $(XETEX_DEFINES) -Fo$@ $<

#
# Making xetex.
#
xetex.exe: $(xetex_ot_layout_o) $(xetex_o) $(xetex_images_o) $(xetexlibs)
	cl -Fe$@ $(xetex_o) $(xetex_ot_layout_o) $(xetex_images_o) \
           $(WD) $(socketlibs) $(LOADLIBES) $(xetexlibs) $(FRAMEWORKS) \
           $(LDFLAGSXX) $(LKDEBUG)

xetex.dll: $(xetex_ot_layout_o) $(dllxetex_o) $(xetex_images_o) $(xetexlibs)
	cl -LD -Fe$@ $(dllxetex_o) $(xetex_ot_layout_o) $(xetex_images_o) \
           $(socketlibs) $(LOADLIBES) $(xetexlibs) $(FRAMEWORKS) \
           $(LDFLAGS) $(LKDEBUG)

# C file dependencies
synctex_xetexd = $(synctex_xetexd_on)
$(xetex_c) xetexcoerce.h xetexd.h: xetex.p $(web2c_texmf)
	$(web2c) xetex
	$(xetexd.h-with_synctex)

xetexextra.c: xetexdir/xetexextra.c
	cp xetexdir/xetexextra.c $@

makecpool=./web2c/makecpool.exe

xetex-pool.c: xetex.pool xetexd.h $(makecpool)
	$(makecpool) xetex >$@ || rm -f $@

xetexdll.obj: xetexdll.c lib/texmfmp.c texmfmp.h
xetex-pool.obj: xetex-pool.c $(srcdir)/xetexdir/XeTeX_ext.h

# Tangling
xetex.p xetex.pool: ./otangle.exe xetexdir/xetex.web xetex.ch
	./otangle xetexdir/xetex.web xetex.ch

## Generate xetex.ch
xetex_ch_srcs = \
  $(srcdir)/xetexdir/xetex.web \
  $(srcdir)/xetexdir/tex.ch0 \
  $(srcdir)/tex.ch \
  $(xetex_ch_synctex) \
  $(srcdir)/xetexdir/xetex.ch \
  $(xetex_post_ch_synctex) \
  $(srcdir)/tex-binpool.ch

xetex.ch: tie.exe $(xetex_ch_srcs)
	./tie -c xetex.ch $(xetex_ch_srcs)

Xnew.web: tie.exe xetexdir/xetex.web xetex.ch
	./tie -m Xnew.web xetexdir/xetex.web xetex.ch

# Cleaning up.
clean:: xetex-clean
xetex-clean: # etrip-clean
	rm -f xetex.dll xetex.exe
	rm -f $(xetex_o) $(xetex_c) xetexextra.c xetexcoerce.h xetexd.h
	rm -f xetexdir/xetexextra.h
	rm -f xetex.p xetex.pool xetex.web xetex.ch
	rm -f xetex.fmt xetex.log
	rm -f hello.dvi hello.log xfoo.out openout.log one.two.log uno.log
	rm -f just.log batch.log write18.log mltextst.log texput.log
	rm -f missfont.log
# end of xetex.mk

ZLIBCPPFLAGS=-I$(ZLIBDIR)

#
# luatex
#
native = .
luatex = luatex
luatexdir = luatexdir

LIBLUADIR=lualibs/lua52

#
# DLL
#
LIBLUADEP=$(LIBLUADIR)/lua52.lib

#
# static
#
#LIBLUADEP=$(LIBLUADIR)/liblua.lib

SLNUNICODEDIR=lualibs/slnunicode
SLNUNICODEDEP=$(SLNUNICODEDIR)/slnunico.obj
SLNUNICODESRC=$(SLNUNICODEDIR)/slnunico.c
# zziplib

ZZIPLIBDIR=../../libs/zziplib
ZZIPLIBSRCDIR=$(ZZIPLIBDIR)/zzip
ZZIPLIBDEP = $(ZZIPLIBDIR)/zzip/libzzip.lib

$(ZZIPLIBDEP): $(ZZIPLIBSRCDIR)
	cd $(ZZIPLIBDIR)/zzip && $(MAKE)

# luazip

LUAZIPDIR=lualibs/luazip
LUAZIPSRCDIR=$(LUAZIPDIR)
LUAZIPDEP=$(LUAZIPDIR)/src/luazip.obj
 
$(LUAZIPDEP): $(LUAZIPDIR)/src/luazip.c
	cd $(LUAZIPDIR)/src && $(MAKE)

# luafilesystem

LUAFSDIR=lualibs/luafilesystem
LUAFSSRCDIR=$(LUAFSDIR)
LUAFSDEP=$(LUAFSDIR)/src/lfs.obj
LUAFSINC=-I../../lua52 -I../../..

# luapeg
LUAPEGDIR=lualibs/luapeg
LUAPEGSRCDIR=$(LUAPEGDIR)
LUAPEGDEP=$(LUAPEGDIR)/lpeg.obj
$(LUAPEGDEP): $(LUAPEGDIR)/lpeg.c
	cd $(LUAPEGDIR) && $(CC) -O2 -MT -I../lua52 -I../.. -Folpeg.obj -c lpeg.c

# luamd5
LUAMDVDIR=lualibs/luamd5
LUAMDVSRCDIR=$(LUAMDVDIR)
LUAMDVDEP=$(LUAMDVDIR)/md5lib.obj $(LUAMDVDIR)/md5.obj $(LUAMDVDIR)/md5_lua.obj
$(LUAMDVDEP): $(LUAMDVDIR)/md5lib.c $(LUAMDVDIR)/luamd5.h $(LUAMDVDIR)/md5.c \
              $(LUAMDVDIR)/md5_lua.c
	cd $(LUAMDVDIR) && $(CC) $(CFLAGS) -I../lua52 -I../.. -I../../w2c -Fomd5.obj -c md5.c && $(CC) $(CFLAGS) -I../lua52 -I../.. -I../../w2c -Fomd5lib.obj -c md5lib.c && $(CC) $(CFLAGS) -I../lua52 -I../.. -I../../w2c -Fomd5_lua.obj -c md5_lua.c

$(LUAFSDEP): $(LUAFSDIR)/src/lfs.c $(LUAFSDIR)/src/lfs.h
	cd $(LUAFSDIR)/src && $(CC) $(CFLAGS) $(LUAFSINC) -I../../../w2c -Folfs.obj -c lfs.c


# luasocket

LUASOCKETDIR=lualibs/luasocket
LUASOCKETSRCDIR=$(srcdir)/$(LUASOCKETDIR)
LUASOCKETDEP=$(LUASOCKETDIR)/src/socket.lib

$(LUASOCKETDEP): $(LUASOCKETDIR)/src/luasocket.c $(LUASOCKETDIR)/src/luasocket.h
	cd $(LUASOCKETDIR)/src && $(MAKE)

$(SLNUNICODEDEP): $(SLNUNICODESRC)
	cd $(SLNUNICODEDIR) && $(MAKE)

# luaff
LUAFFDIR=lualibs/luafontloader
LUAFFSRCDIR=$(srcdir)/$(LUAFFDIR)
LUAFFDEP=$(LUAFFSRCDIR)/libff.lib
$(LUAFFDEP): $(LUAFFDIR)/src/luafflib.c
	cd $(LUAFFSRCDIR) && $(MAKE)

# luaprof
LUAPROFDIR=lualibs/luaprofiler
LUAPROFSRCDIR=$(srcdir)/$(LUAPROFDIR)
LUAPROFDEP=$(LUAPROFDIR)/libprofiler.lib
$(LUAPROFDEP): $(LUAPROFDIR)/*.obj
	cd $(LUAPROFDIR) && $(MAKE))


# luazlib
LUAZLIBDIR=lualibs/luazlib
LUAZLIBSRCDIR=$(srcdir)/$(LUAZLIBDIR)
LUAZLIBDEP=$(LUAZLIBDIR)/lgzip.obj $(LUAZLIBDIR)/lzlib.obj
$(LUAZLIBDIR)/lgzip.obj: $(LUAZLIBDIR)/lgzip.c
	cd $(LUAZLIBDIR) && $(MAKE)
$(LUAZLIBDIR)/lzlib.obj: $(LUAZLIBDIR)/lzlib.c
	cd $(LUAZLIBDIR) && $(MAKE)

MPLIBDIR=mplibdir

LIBMPLIBDEP=$(MPLIBDIR)/lmplib.obj $(MPLIBDIR)/libmplib.lib

LDLIBPOPPLER=$(POPPLERDIR)/libpoppler.lib

luatexlibs = \
$(luatexdir)/libluatex.lib \
$(LUASOCKETDEP) $(LUAPEGDEP) \
$(LUAMDVDEP) $(LUAFSDEP) $(SLNUNICODEDEP) $(LUAZIPDEP) \
$(LDLIBPNG) $(LDZLIB) $(LDLIBPOPPLER) \
$(ZZIPLIBDEP) $(LUAZLIBDEP) $(LUAFFDEP) $(LIBBSDDEP) \
$(LUAPROFDEP) $(LIBMPLIBDEP)

luatexdir/libluatex.lib: luatexdir/*.c luatexdir/lua/*.c \
luatexdir/font/*.c luatexdir/image/*.c luatexdir/*.h \
luatexdir/font/*.h luatexdir/image/*.h luatexdir/image/*.cc
	cd luatexdir && $(MAKE) libluatex.lib

luatexlibsdep = $(luatexlibs)

luatex_bin = luatex.dll
linux_build_dir =

# The C sources.
luatex_c = luatexdir/luatex.c
luatexdll_o = luatexdir/luatexfirst.obj
luatex_o = $(luatexdll_o) 

$(luatexdll_o): $(luatex_c)
	$(compile) -DHAVE_SYS_TIMEB_H -Iluatexdir -Iw2c -Fo$@ $(luatex_c)

# Making luatex
luatex.dll: $(luatexdll_o) $(luatexlibsdep)
	cl -LD -Fe$@ $(luatexdll_o) $(luatexlibs) $(socketlibs) \
            $(luatexlibsldextra) $(LOADLIBES) $(ZZLOADLIBES) \
            WS2_32.lib $(LIBLUADEP) $(LDFLAGS) $(LKDEBUG)

# C file dependencies.

clean:: luatex-clean
luatex-clean:
	$(LIBTOOL) --mode=clean $(RM) luatex
	rm -f $(luatex_o) $(luatex_c) luatexextra.c luatexcoerce.h
	rm -f $(luatexdir)/luatexextra.h
	rm -f luatexd.h luatex.p luatex.pool luatex.ch strpool.c
	rm -f luatex.fmt luatex.log
#
# end of luatex
#




#------------------------------------------------------------------------
# Making pdftex etc.
#
pdflib = pdftexdir/libpdf.lib

pdftexlibs = $(pdflib) $(LDLIBPNG) $(LDZLIB) $(LDLIBPOPPLER)
pdftexlibsdep = $(pdflib) $(LIBPNGDEP) $(ZLIBDEP) $(LDLIBPOPPLER) $(makecpool)

ttf2afm = ttf2afm.exe
pdftex = pdftex.dll
#
# synctex
#
synctex_dir = $(srcdir)/synctexdir
synctex_common_texd = \
		echo "Enabling SyncTeX Support."

# ttf2afm
ttf2afm.exe: ttf2afm.obj
	$(kpathsea_link) ttf2afm.obj $(WD) $(kpathsea) \
$(XLOADLIBES) $(LDFLAGS) $(LKDEBUG)

ttf2afm.obj: ttf2afm.c macnames.c
	$(compile) $<
ttf2afm.c: $(srcdir)/pdftexdir/ttf2afm.c
	rm -f ttf2afm.c
	$(LN) $(srcdir)/pdftexdir/ttf2afm.c ttf2afm.c
macnames.c: $(srcdir)/pdftexdir/macnames.c
	rm -f macnames.c
	$(LN) $(srcdir)/pdftexdir/macnames.c macnames.c

# pdftosrc
pdftosrc.exe: pdftexd.h pdftexdir/pdftosrc.obj pdftexdir/libpdf.lib
	$(kpathsea_link) pdftexdir/pdftosrc.obj $(WD) pdftexdir/libpdf.lib \
           $(LDLIBPOPPLER) $(ZLIBDEP) \
           $(LOADLIBES) $(LDFLAGS) $(LKDEBUG)
pdftexdir/pdftosrc.obj:$(srcdir)/pdftexdir/pdftosrc.cc
	cd pdftexdir && make pdftosrc.obj

# Makefile fragment for pdftex and web2c. --infovore@xs4all.nl. Public domain.
# This fragment contains the parts of the makefile that are most likely to
# differ between releases of pdftex.

pdftexdir = pdftexdir
##
## The C sources.
pdftex_c = pdftexini.c pdftex0.c
pdftex_o = pdftexini.obj pdftex0.obj \
            pdftexextra.obj pdftex-pool.obj \
            synctex-pdf.obj
dllpdftex_o = pdftexini.obj pdftex0.obj \
            pdftexextra.obj pdftex-pool.obj \
            synctex-pdf.obj
pdftexextra_o=

#   Sources for pdftex.ch:
pdftex_ch_srcs = $(srcdir)/$(pdftexdir)/pdftex.web \
  $(srcdir)/$(pdftexdir)/tex.ch0 \
  $(srcdir)/tex.ch \
  $(srcdir)/enctex.ch \
  $(pdftex_ch_synctex) \
  $(srcdir)/$(pdftexdir)/pdftex.ch \
  $(srcdir)/tex-binpool.ch

#   Rules:
pdftex.ch: tie.exe $(pdftex_ch_srcs)
	./tie -c pdftex.ch $(pdftex_ch_srcs)

pdftexextra.c: pdftexdir/pdftexextra.c
	cp pdftexdir/pdftexextra.c $@

pdftex.exe: pdftexd.h $(pdftex_o) $(pdftexextra_o) $(pdftexlibsdep)
	$(kpathsea_cxx_link) $(pdftex_o) $(pdftexextra_o) \
             $(pdftexlibs) $(socketlibs) $(WD) $(LOADLIBES) \
             $(LDFLAGS) $(LKDEBUG)
pdftex.dll: pdftexd.h $(dllpdftex_o) $(pdftexextra_o) $(pdftexlibsdep)
	cl -LD -Fe$@ $(dllpdftex_o) $(pdftexextra_o) \
             $(pdftexlibs) $(socketlibs) $(LOADLIBES) \
             $(LDFLAGS) $(LKDEBUG)

pdftex.p pdftex.pool: tangle.exe $(srcdir)/pdftexdir/pdftex.web \
                      pdftex.ch tangle.exe
	$(shared_env) ./tangle $(srcdir)/pdftexdir/pdftex.web pdftex.ch

# Metafont and TeX generate more than .c file.
web2c_texmf = $(web2c_aux) $(web2c_programs) web2c/texmf.defines web2c/coerce.h

#
# syctex for pdftex
#
### SyncTeX support in pdftex
# list of change files
pdftex_ch_synctex = \
	$(synctex_dir)/synctex-def.ch0 \
	$(synctex_dir)/synctex-mem.ch0 \
	$(synctex_dir)/synctex-e-mem.ch0 \
	$(synctex_dir)/synctex-e-mem.ch1 \
	$(synctex_dir)/synctex-rec.ch0 \
	$(synctex_dir)/synctex-rec.ch1 \
	$(synctex_dir)/synctex-e-rec.ch0 \
	$(synctex_dir)/synctex-pdf-rec.ch2

# The C sources and headers
pdftex_o_synctex = synctex-pdf.obj
synctex-pdf.h: $(synctex_dir)/synctex.h
	cat $(synctex_dir)/synctex.h >$@
synctex-pdftex.h: $(synctex_dir)/synctex-pdftex.h
	cat $(synctex_dir)/synctex-pdftex.h >$@
$(pdftex_o_synctex): synctex-pdf.c
	$(compile) -DSYNCTEX_ENGINE_H='<synctex-pdftex.h>' $<
synctex-pdf.c: pdftexd.h $(synctex_dir)/synctex.c synctex-pdftex.h
	cp $(synctex_dir)/synctex.c $@
pdftexd.h-with_synctex = if test -z "`grep __SyncTeX__ pdftexd.h`";\
	then\
		$(synctex_common_texd);\
		sed -f synctex_sed_command.sed pdftexd.h > synctex_pdftexd.h;\
		mv synctex_pdftexd.h pdftexd.h;\
		if test -z "`grep __SyncTeX__ pdftexd.h`";\
		then\
			echo "warning: SyncTeX activation FAILED";\
			exit 1;\
		fi;\
		echo "SyncTeX is enabled";\
	fi
pdftexd.h-without_synctex = echo "warning: SyncTeX is NOT enabled"

# the synctex tool
synctex_parser.obj: $(synctex_dir)/synctex_parser.c $(synctex_dir)/synctex_parser.h
	$(compile) -c -DSYNCTEX_USE_LOCAL_HEADER -I$(synctex_dir) $(ZLIBCPPFLAGS) -Fo$@ $<

synctex_parser_utils.obj: $(synctex_dir)/synctex_parser_utils.c $(synctex_dir)/synctex_parser_utils.h
	$(compile) -c -DSYNCTEX_USE_LOCAL_HEADER -I$(synctex_dir) $(ZLIBCPPFLAGS) -Fo$@ $<

synctex_main.obj: $(synctex_dir)/synctex_main.c $(synctex_dir)/synctex_parser.h
	$(compile) -c -I$(synctex_dir) $(ZLIBCPPFLAGS) -Fo$@ $<

synctex.exe:synctex_main.obj synctex_parser.obj synctex_parser_utils.obj $(ZLIBDEP)
	$(link_command) synctex_main.obj synctex_parser.obj synctex_parser_utils.obj $(LDZLIB) $(LIBS) $(XLOADLIBES) $(kpathsea)

$(pdftex_c) pdftexcoerce.h pdftexd.h: pdftex.p $(web2c_texmf) \
$(srcdir)/pdftexdir/pdftex.defines
	$(web2c) pdftex
	$(pdftexd.h-with_synctex)

pdftex-pool.c: pdftex.pool pdftexd.h $(makecpool)
	$(makecpool) pdftex >pdftex-pool.c

pdflib_sources = $(srcdir)/pdftexdir/*.c $(srcdir)/pdftexdir/*.cc \
  $(srcdir)/pdftexdir/*.h
pdftexdir/libpdf.lib: pdftexd.h $(pdflib_sources)
	cd pdftexdir && $(MAKE) $(common_makeargs) libpdf.lib

#
# end of pdftex
#
#------------------------------------------------------------------------







otangle.web: omegaware/otangle.web
	rm -f $@
	$(LN) $(srcdir)/omegaware/otangle.web $@
otangle.ch: omegaware/otangle.ch
	rm -f $@
	$(LN) $(srcdir)/omegaware/otangle.ch $@

# Bootstrapping otangle requires making it with itself.
otangle.exe: otangle.obj
	$(kpathsea_link) otangle.obj $(WD) $(LOADLIBES) $(LDFLAGS) $(LKDEBUG)

otangle.p: tangle.exe otangle.web otangle.ch
	$(shared_env) ./tangle otangle.web otangle.ch

tie.exe: tie.obj
	$(kpathsea_link) tie.obj $(WD) $(LOADLIBES) $(LDFLAGS) $(LKDEBUG)

# Metafont and TeX generate more than .c file.
web2c_texmf = $(web2c_aux) $(web2c_programs) web2c/texmf.defines web2c/coerce.h

# A special rule for tie
tie.c: ctangle.exe tiedir/tie.w tiedir/tie-w2c.ch
	-rm -f $@
	./ctangle $(srcdir)/tiedir/tie.w $(srcdir)/tiedir/tie-w2c.ch

# Additional dependencies for relinking.
$(web2c_programs) $(programs) otangle.exe otangleboot.exe tangle.exe \
tangleboot.exe ctangleboot.exe : $(kpathsea) $(proglib)

# Bootstrapping tangle requires making it with itself.  We use the opportunity
# to create an up-to-date tangleboot as well.
tangle.exe: tangle.obj
	$(kpathsea_link) tangle.obj $(WD) $(LOADLIBES) $(LDFLAGS) $(LKDEBUG)
	$(MAKE) $(common_makeargs) tangleboot.p

# tangle.p is a special case, since it is needed to compile itself.  We
# convert and compile the (distributed) tangleboot.p to make a tangle
# which we use to make the other programs.
tangle.p: tangleboot.exe tangle.web tangle.ch
	$(shared_env) ./tangleboot $(srcdir)/tangle.web $(srcdir)/tangle.ch
tangle.web:
	@echo "You seem to be missing tangle.web, perhaps because you" >&2
	@echo "didn't retrieve web.tar.gz, only web2c.tar.gz." >&2
	@echo "You need both." >&2
	@echo >&2
	@echo "web.tar.gz should be available from the" >&2
	@echo "same place that you got web2c.tar.gz." >&2
	@echo "In any case, you can get it from" >&2
	@echo "ftp://ftp.tug.org/tex/web.tar.gz." >&2
	false

tangleboot.exe: tangleboot.obj
	$(kpathsea_link) tangleboot.obj $(WD) $(LOADLIBES) $(LDFLAGS) $(LKDEBUG)

tangleboot.c tangleboot.h: stamp-tangle $(web2c_programs) $(web2c_aux)
	$(web2c) tangleboot
# tangleboot.p is in the distribution
stamp-tangle: tangleboot.p
	date >stamp-tangle
# This is not run unless tangle.web or tangle.ch is changed.
tangleboot.p: tangle.web tangle.ch
	$(shared_env) ./tangle $(srcdir)/tangle.web $(srcdir)/tangle.ch
	mv tangle.p tangleboot.p
	date >stamp-tangle
	$(MAKE) $(common_makeargs) tangle.exe

# Bootstrapping ctangle requires making it with itself.  We use the opportunity
# to create an up-to-date ctangleboot as well.
ctangle.exe: ctangle.obj cweb.obj
	$(kpathsea_link) ctangle.obj cweb.obj $(WD) $(LOADLIBES) \
          $(LDFLAGS) $(LKDEBUG)
	$(MAKE) $(common_makeargs) ctangleboot.c cwebboot.c

# ctangle.c is a special case, since it is needed to compile itself.
ctangle.c: ctangleboot.exe cwebdir/ctangle.w cwebdir/ctang-w2c.ch
	CWEBINPUTS=".;$(srcdir)/cwebdir" ./ctangleboot ctangle.w ctang-w2c.ch
cweb.c: ctangleboot.exe cwebdir/common.w cwebdir/comm-w2c.ch help.h
	CWEBINPUTS=".;$(srcdir)/cwebdir" ./ctangleboot common.w comm-w2c.ch cweb.c

ctangleboot.exe: ctangleboot.obj cwebboot.obj
	$(kpathsea_link) ctangleboot.obj cwebboot.obj $(WD) $(LOADLIBES) \
          $(LDFLAGS) $(LKDEBUG)

# ctangleboot.c is in the distribution
stamp-ctangle: ctangleboot.c cwebboot.c
# This is not run unless tangle.web or tangle.ch is changed.
# Only try to run ./tangle if it actually exists, otherwise
# just touch tangleboot.p and build tangle.
ctangleboot.c: ctangleboot.cin
	cp ctangleboot.cin $@
	date >stamp-ctangle
	$(MAKE) $(common_makeargs) ctangle.exe
cwebboot.c: cwebboot.cin
	cp cwebboot.cin $@
	date >stamp-ctangle
	$(MAKE) $(common_makeargs) ctangle.exe

# Even web2c itself uses the library.
# It's annoying to have to give all the filenames here, 
# but texmfmp.c is an exception.
lib_sources = lib/alloca.c lib/basechsuffix.c lib/chartostring.c \
  lib/eofeoln.c lib/fprintreal.c lib/input2int.c lib/inputint.c lib/main.c \
  lib/openclose.c lib/printversion.c lib/uexit.c lib/usage.c lib/version.c \
  lib/zround.c 
$(proglib): $(lib_sources)
	cd lib && $(MAKE) $(common_makeargs)

# No exceptions in this library.
$(LIBPNGDIR)/libpng.lib: $(LIBPNGSRCDIR)/*.c
	cd $(LIBPNGDIR) && $(MAKE) $(common_makeargs) libpng.lib
$(ZLIBDIR)/libz.lib: $(ZLIBSRCDIR)
	cd $(ZLIBDIR) && $(MAKE) $(common_makeargs) libz.lib
$(LIBTIFFDIR)/libtiff.lib: $(LIBTIFFSRCDIR)/*.c
	cd $(LIBTIFFDIR) && $(MAKE) $(common_makeargs) libtiff.lib

# The web2c program consists of several executables.
web2c/fixwrites.exe: web2c/fixwrites.c
	cd web2c && $(MAKE) $(common_makeargs) fixwrites.exe
web2c/splitup.exe: web2c/splitup.c
	cd web2c && $(MAKE) $(common_makeargs) splitup.exe
web2c/web2c.exe: web2c/main.c web2c/web2c.h web2c/web2c-parser.c \
web2c/web2c-lexer.c
	cd web2c && $(MAKE) $(common_makeargs) web2c.exe

# tkpathsea.mk -- target for remaking kpathsea.
makeargs = $(MFLAGS) CC='$(CC)' CFLAGS='$(CFLAGS)' $(XMAKEARGS)

# This is wrong: the library doesn't depend on kpsewhich.c or
# acconfig.h.  But what to do?
#
#$(kpathsea): $(kpathsea_srcdir)/*.c $(kpathsea_srcdir)/*.h \
#	     $(kpathsea_srcdir)/texmf.cnf $(top_srcdir)/../make/paths.mk
#	cd $(kpathsea_dir) && $(MAKE) $(makeargs)
#
# End of tkpathsea.mk.

# Let's stick a rule for TAGS here, just in case someone wants them.
# (We don't put them in the distributions, to keep them smaller.)
TAGS: *.c *.h
	pwd | grep kpathsea >nul && append=../kpathsea/TAGS; \
	  etags $$append *.[ch]

# Prevent GNU make 3.[59,63) from overflowing arg limit on system V.
.NOEXPORT:

install: xetex.exe luatex.dll
	cp -p luatex.dll xetex.exe c:/usr/local/bin/
	cp -p xetex.exe c:/usr/work/edrive/wk/dist/XETEX/bin/
	cp -p luatex.dll c:/usr/work/edrive/wk/dist/LUATEX/bin/

installluatex: luatex.dll
	cp -p luatex.dll c:/usr/local/bin/
	cp -p luatex.dll c:/usr/work/edrive/wk/dist/LUATEX/bin/

installxetex: xetex.exe
	cp -p xetex.exe c:/usr/local/bin/
	cp -p xetex.exe c:/usr/work/edrive/wk/dist/XETEX/bin/
#
# XeTeX build date (obsolete)
#

# XETEX_BLD_DATE=`cat xetex_build_date.txt`

#xetexextra.obj: xetexextra.c lib/texmfmp.c xetexdir/xetexextra.h \
# xetexdir/xetex_version.h etexdir/etex_version.h texmfmp.h \
# xetex_build_date.txt
#	$(compile) -DXETEX_BUILD_DATE="$(XETEX_BLD_DATE)" xetexextra.c

xetexextra.obj: xetexextra.c lib/texmfmp.c xetexdir/xetexextra.h \
 xetexdir/xetex_version.h etexdir/etex_version.h texmfmp.h
	$(compile) xetexextra.c

#xetex_build_date.txt: xetex0.obj
#	$(SHELL) mkxetexbuilddate.sh
