# The olsr.org Optimized Link-State Routing daemon (olsrd)
#
# (c) by the OLSR project
#
# See our Git repository to find out who worked on this file
# and thus is a copyright holder on it.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
# * Neither the name of olsr.org, olsrd nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Visit http://www.olsr.org for more information.
#
# If you find this software useful feel free to make a donation
# to the project. For more information see the website or contact
# the copyright holders.
#

#
# WINDOWS32 SPECIFIC CONFIGURATION
#

DESTDIR ?=
EXENAME = olsrd.exe

SRCS +=		$(sort $(wildcard src/win32/*.c))
HDRS +=		$(sort $(wildcard src/win32/*.h))

CPPFLAGS += 	-D_WIN32_WINNT=0x0600
LIBS +=		-lws2_32 -liphlpapi
CPPFLAGS += 	-I$(TOPDIR)/src/win32

PLUGIN_SONAME ?= $(PLUGIN_NAME)
PLUGIN_FULLNAME ?= $(PLUGIN_NAME).dll
INSTALL_LIB =	mkdir -p $(LIBDIR);cp $(PLUGIN_FULLNAME) $(LIBDIR)/$(PLUGIN_FULLNAME)
UNINSTALL_LIB =	del /F $(LIBDIR)/$(PLUGIN_FULLNAME)

ifdef OLSRD_PLUGIN
LDFLAGS +=	-L$(TOPDIR)
#LIBS += 	$(TOPDIR)/src/win32/compat.o
LIBS +=		-lolsrd
endif

OS_LIB_PTHREAD =
OS_CFLAG_PTHREAD =
OS_LIB_DYNLOAD =


# Notes for compiling olsrd.exe under Windows using MinGW
# ----------------------------------------------------
# You can build olsrd.exe using MinGW on either Windows or GNU/Linux.
# For MinGW on Windows, run this in the msys shell:
#
#   cd olsrd
#   make clean_all
#   make build_all OS=win32
#
# MinGW also runs on GNU/Linux so you can build Windows binaries on
# any GNU/Linux machine.  It is especially easy on a
# Debian/Ubuntu/Mint system:
#
#   sudo apt-get install mingw32 flex bison make
#   cd olsrd
#   make clean_all
#   CC=i586-mingw32msvc-gcc make build_all OS=win32

MINGW_VERSION = $(shell "$(TOPDIR)/make/getmingwversion.bash" "$(CC)")

MINGW_VERSION_OK := $(shell expr "$(MINGW_VERSION)" \>= 40600)
ifneq "$(MINGW_VERSION_OK)" "1"
  CFLAGS += -I$(TOPDIR)/src/win32/compat
endif

CFLAGS += -DMINGW_VERSION=$(MINGW_VERSION)

# Notes for compiling olsrd.exe under Windows using Cygwin
# ----------------------------------------------------
# You need at least Cygwin installed. Start the Cygwin setup and select
# flex,bison,make,mingw-core. Then run "make all libs" to compile olsrd.exe
# and the plugin DLLs. To compile the GUI, you need MSVC6, PlatformSDK and
# the NSIS installer. Adapt the path names below and run "make win32setup".
# Alternatively, set environment vars e.g. in the Windows registry under
# HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment e.g.
#
# set PATH=C:\PROGRA~1\MICROS~4\VC98\BIN;C:\PROGRA~1\MIFD68~1\BIN;C:\PROGRA~1\NSIS;%PATH%
# set LIB=C:\PROGRA~1\MIFD68~1\LIB;C:\PROGRA~1\MICROS~4\VC98\LIB;C:\PROGRA~1\MICROS~4\VC98\MFC\LIB
# set INCLUDE=C:\PROGRA~1\MIFD68~1\INCLUDE;C:\PROGRA~1\MICROS~4\VC98\INCLUDE;C:\PROGRA~1\MICROS~4\VC98\MFC\INCLUDE
#
# You can install Cygwin under Wine. Then "wineconsole make all libs win32lib"
# does it's job (slowly), but there may be issues while re-invoking MSVC from
# bash.exe during "wineconsole win32setup". Note, that Wine on EXT2/EXT3 prevents
# the Cygwin "!<symlink>" files from working somehow. You may need to install Cygwin
# under Windows to get a correct gcc/mingw setup, then copy to your Linux
# box, then replace all "!<symlink>" files by real symlinks, e.g.
#
# cd ~/.wine/drive_c/Programme/cygwin
# for src in $(grep '!<symlink>' . -rl 2>&-);do
#  if [ $(stat -c "%s" $src) -lt 100 ];then
#   dst=$(sed -e's/^\!<symlink>//' $src)
#   echo "ln -sf $dst $src"
#  fi
# done
#
# Required software versions:
# MSVC6 GUI (with MFC!) runs under Wine (copy MFC42.DLL & MSVCP60.DLL)
# Platform SDK, search for "February 2003 SDK"
# Cygwin 1.5.x and Wine-1.1.0 or above

PROGDIR = c:/Programme
PROGCYG = /cygdrive/c/Programme
PSDKDIR = $(PROGDIR)/Microsoft SDK
PSDKCYG = $(PROGCYG)/Microsoft\ SDK
MSVCDIR = $(PROGDIR)/Microsoft Visual Studio
MSVCCYG = $(PROGCYG)/Microsoft\ Visual\ Studio
NSISDIR = $(PROGDIR)/NSIS
NSISCYG = $(PROGCYG)/NSIS

# Necessary for running cygwin under wine. Both the "-C" option and MAKELEVEL>0 will oops...
MAKE := sh -c 'cmd=$$0;while [ 0 -lt $$\# ];do case $$1 in -C) cmd="cd $$2 && $$cmd";shift 2;; *) cmd="$$cmd \"$$1\"";shift;;esac;done;sh -c "export MAKELEVEL=0 && $$cmd"' $(MAKE)

gui/win32/Shim/Release/shim.obj: gui/win32/Shim/shim.c
		test -d $(dir $@) || mkdir -p $(dir $@)
		PATH=$(MSVCCYG)/VC98/Bin:$$PATH && CL.EXE \
			/I"$(PSDKDIR)/Include" /I"$(MSVCDIR)/VC98/Include" \
			/nologo /ML /W3 /GX /O2 /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" \
			/Fp"$(dir $@)/Shim.pch" /YX /Fo"$(dir $@)/" /Fd"$(dir $@)/" /FD /c "$<"

gui/win32/Shim/Release/Shim.exe: gui/win32/Shim/Release/shim.obj
		PATH=$(MSVCCYG)/VC98/Bin:$$PATH && LINK.EXE \
			/LIBPATH:"$(PSDKDIR)/Lib" /LIBPATH:"$(MSVCDIR)/VC98/Lib" \
			kernel32.lib user32.lib \
			/nologo /subsystem:console /incremental:no /machine:I386 /out:"$@" "$^"

WIN32FILES+=Frontend
WIN32FILES+=FrontendDlg
WIN32FILES+=HnaEntry
WIN32FILES+=MidEntry
WIN32FILES+=MprEntry
WIN32FILES+=MyDialog1
WIN32FILES+=MyDialog2
WIN32FILES+=MyDialog3
WIN32FILES+=MyDialog4
WIN32FILES+=MyEdit
WIN32FILES+=MyTabCtrl
WIN32FILES+=NodeEntry
WIN32FILES+=StdAfx
WIN32FILES+=TrayIcon

WIN32CPPFILES=$(addprefix gui/win32/Main/, $(addsuffix .cpp,  $(WIN32FILES)))
WIN32OBJFILES=$(addprefix gui/win32/Main/Release/, $(addsuffix .obj,  $(WIN32FILES)))

gui/win32/Main/Release/Frontend.res: gui/win32/Main/Frontend.rc
		test -d $(dir $@) || mkdir -p $(dir $@)
		PATH=$(PSDKCYG)/Bin:$(MSVCCYG)/Common/MSDev98/Bin:$$PATH && RC.EXE \
			/l 0x407 /i"$(PSDKDIR)/Include" /i"$(MSVCDIR)/VC98/MFC/Include" /fo"$@" /d NDEBUG "$<"

gui/win32/Main/Release/Switch.exe: gui/win32/Main/olsrd_cfgparser.lib gui/win32/Main/Release/Frontend.res $(WIN32CPPFILES)
		test -d $(dir $@) || mkdir -p $(dir $@)
		PATH=$(MSVCCYG)/VC98/Bin:$$PATH && CL.EXE \
			/I"$(PSDKDIR)/Include" /I"$(MSVCDIR)/VC98/Include" /I"$(MSVCDIR)/VC98/MFC/Include" /I"src" \
			/nologo /MT /W4 /GX /O2 /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" \
			/Fp"$(dir $@)/Frontend.pch" /YX"stdafx.h" /Fo"$(dir $@)/" /Fd"$(dir $@)/" /FD /c \
			$(WIN32CPPFILES)
		PATH=$(MSVCCYG)/VC98/Bin:$$PATH && LINK.EXE \
			/LIBPATH:"$(PSDKDIR)/Lib" /LIBPATH:"$(MSVCDIR)/VC98/Lib" /LIBPATH:"$(MSVCDIR)/VC98/MFC/Lib" \
			ws2_32.lib iphlpapi.lib \
			gui/win32/Main/olsrd_cfgparser.lib gui/win32/Main/Release/Frontend.res \
			/nologo /subsystem:windows /incremental:no /machine:I386 /out:"$@" \
			$(WIN32OBJFILES)

win32setup: gui/win32/Inst/installer.nsi default_target libs gui/win32/Shim/Release/Shim.exe gui/win32/Main/Release/Switch.exe
		PATH=$(NSISCYG):$$PATH && makensis.exe $<

win32lib: gui/win32/Main/olsrd_cfgparser.lib

gui/win32/Main/olsrd_cfgparser.lib: cfgparser
		@# Using the cygwin dlltool does not work (missing functions hooks)
		@# dlltool --input-def src/cfgparser/olsrd_cfgparser.def --dllname src/cfgparser/olsrd_cfgparser.dll --output-lib $@
		@# Use the Microsoft Lib Tool instead
		PATH=$(MSVCCYG)/VC98/Bin:$$PATH && LIB.EXE \
			/machine:i386 /def:src/cfgparser/olsrd_cfgparser.def /out:gui/win32/Main/olsrd_cfgparser.lib

# Local Variables:
# mode: makefile
# End:
