# (C) 2003 XDA Developers  itsme@xs4all.nl
#
# $Header$
#
# note: commands should contain forward slashes
#       double quoted strings may contain dos-type backslashes in paths
out=../build
obj=../build/ce

include ../local.mak

# /Zi /Od /Fd"$(out)" for debugging
# /O2      for release
cflags=/Zi /nologo /W3 /O2 /c /Fo"$(obj)/" /I "..\include" /I "../libril" /I "../common" /I "../dll" /EHsc /FI ../common/nowarn.h /I "../include/msvc"
targetdefs=/D "ARM" /D "_ARM_" /D "ARMV4"
pmdef300=/D _WIN32_WCE=300 /D "WIN32_PLATFORM_PSPC=310" /D UNDER_CE=300
pmdef420=/D _WIN32_WCE=420 /D "WIN32_PLATFORM_PSPC=400" /D UNDER_CE=420
cdefs=$(targetdefs) /D "UNICODE" /D "_UNICODE" /D "NDEBUG"
cdefs+=/D _CRT_SECURE_NO_WARNINGS /D _SECURE_SCL=0 /D _HAS_ITERATOR_DEBUGGING=0 /D NOMINMAX
dlldefs=/D "_USRDLL"

# linker flags
lddllflags=/entry:"_DllMainCRTStartup" /dll  /base:"0x00100000"
ldexeflags=/entry:"WinMainCRTStartup" /base:"0x00010000"
ldplatformflags=/subsystem:windowsce,3.00 /align:"4096" 
ldflags=/nologo /debug /stack:0x10000,0x1000 $(celdtargetflags) $(ldplatformflags)
libpaths=/libpath:"$(celib)" /libpath:"$(ppclib)"

# no implicit rules.
.SUFFIXES:

all: rillog.arm.CAB all2

define buildtarget

APPLIST+=$1
CLEANLIST+=$(out)/$1
CLEANLIST+=$(addprefix $(obj)/,$2)

$(out)/$1: $(addprefix $(obj)/,$2)

endef

define buildrules

$(call buildtarget,extendril.dll,extendril.obj debug.obj stringutils.obj rilhook.o)
$(call buildtarget,loadrilextension.exe,loadrilextension.obj debug.obj stringutils.obj)
loadrilextension_libs=toolhelp.lib
$(call buildtarget,unloadrilextension.exe,unloadrilextension.obj debug.obj stringutils.obj)
unloadrilextension_libs=toolhelp.lib
$(call buildtarget,riltest.exe,riltest.obj ril.lib debug.obj stringutils.obj)
$(call buildtarget,rillog.exe,rillog.obj debug.obj stringutils.obj RilClass.obj  ril.lib)
$(call buildtarget,resetgsm.exe,resetgsm.obj debug.obj stringutils.obj RilClass.obj  ril.lib)
$(call buildtarget,hangup.exe,hangup.obj debug.obj stringutils.obj RilClass.obj  ril.lib)
$(call buildtarget,setce_nt.exe,setce_nt.obj debug.obj stringutils.obj RilClass.obj  ril.lib)
$(call buildtarget,tstrilmon.exe,RilStructDecoder.obj RilMonitor.obj tstrilmon.obj debug.obj stringutils.obj ril.lib)
cflags_tstrilmon=/I "$(boost)" /I ../experimental/asyncril  -D_BOOST_LITE
tstrilmon_libs=/libpath:"$(boost)/lib-arm"

$(call buildtarget,readsim.exe,RilStructDecoder.obj RilMonitor.obj readsim.obj debug.obj stringutils.obj vectorutils.obj ril.lib)
cflags_readsim=/I "$(boost)" /I ../experimental/asyncril  -D_BOOST_LITE
readsim_libs=/libpath:"$(boost)/lib-arm"

$(call buildtarget,tstcbst.exe,RilStructDecoder.obj RilMonitor.obj tstcbst.obj debug.obj stringutils.obj ril.lib)
cflags_tstcbst+=-DSAVE_QDATA
$(call buildtarget,tstimei.exe,RilStructDecoder.obj RilMonitor.obj tstimei.obj debug.obj stringutils.obj ril.lib)

endef

$(eval $(buildrules))

all2:  $(addprefix $(out)/,$(APPLIST))

cflags_RilClass=/D "_HAVE_RIL"
cflags_extendril=$(dlldefs)

# use -DBOOST_DISABLE_ASSERTS -DBOOST_THREAD_NO_DATETIME
# when using boost1_35_0 wince patch
# alternatively, use -D_BOOST_LITE
cflags_RilMonitor=/I "$(boost)" /I ../experimental/asyncril -D_BOOST_LITE

cflags_tstcbst+=/I "$(boost)" /I ../experimental/asyncril  -D_BOOST_LITE

cflags_tstimei=/I "$(boost)" /I ../experimental/asyncril -D_BOOST_LITE
tstimei_libs=/libpath:"$(boost)/lib-arm"

vpath .obj $(obj)
$(out)/%.exe: $(obj)/%.obj
	@"$(celd)" $(libpaths) $(ldflags) coredll.lib corelibc.lib $(extralibs) $(ldexeflags) $($(basename $(notdir $@))_libs) /out:$@ $^
	$(SIGNIT) $@
$(out)/%.dll: $(obj)/%.obj
	@"$(celd)" $(libpaths) $(ldflags) coredll.lib corelibc.lib $(extralibs) $(lddllflags) $($(basename $(notdir $@))_libs) /out:$@ $^
	$(SIGNIT) $@


vpath .cpp . ../common/

$(obj)/%.obj: ../common/%.cpp
	@"$(cecc)" $(cflags) $(cdefs) $(pmdef420) $(pb420incs) $^
$(obj)/%.obj: %.cpp
	@"$(cecc)" $(cflags) $(cflags_$(basename $(notdir $@))) $(cdefs) $(pmdef420) $(pb420incs) $^

$(obj)/ril.lib:
	make -C ../libril
$(obj)/rilhook.o: rilhook.S
	arm-wince-pe-gcc -c  $^ -o $@

cabwizwm:=$(wm6sdk)/Tools/CabWiz/cabwiz.exe
romtools=../../romtools
rillog.arm.CAB:    rillog.inf  $(out)/rillog.exe
	@"$(cabwizwm)" $< /err $(obj)/$(basename $(notdir $@)).uerr /nouninstall /cpu arm
	@perl $(romtools)/tounicode.pl -d $(obj)/$(basename $(notdir $@)).uerr $(obj)/$(basename $(notdir $@)).err
	@if grep -v "Warning: Section .DestinationDirs. key .[a-zA-Z]*. is not using the string ..InstallDir..\\|value .[A-Z][a-z]*. has an empty string\\|Section .DefaultInstall. key .AddReg. - there are no section entries to process" $(obj)/$(basename $(notdir $@)).err; then false; else echo created $@; fi
	-@$(SIGNIT) $@
	@$(SIGNIT) $@


clean:
	rm -f $(CLEANLIST)


load: all
	pput -f $(out)/extendril.dll \windows
	pput -f $(out)/loadrilextension.exe \windows
	pdel \loadril.log \extendril.log
	prun loadrilextension

test:
	pput -f $(out)/riltest.exe \windows
	pdel \riltest.log
	prun riltest.exe

get:  getriltest getloadril getextendril

getriltest:
	echo -------------------------------------------
	-pget -f \riltest.log $(out)
	-cat $(out)/riltest.log

getloadril:
	echo -------------------------------------------
	-pget -f \loadril.log $(out)
	-cat $(out)/loadril.log

getextendril:
	echo -------------------------------------------
	-pget -f \extendril.log $(out)
	-cat $(out)/extendril.log

test2:
	pkill rillog.exe
	pput -f $(out)/rillog.exe \windows
	pdel \rillog.log
	prun rillog.exe

get2:
	pget -f \rillog.log $(out)
	cat $(out)/rillog.log


