# Host Identity Protocol
# Copyright (C) 2002-05 the Boeing Company
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#  Makefile
#
#  Author:	Jeff Ahrenholz, <jeffrey.m.ahrenholz@boeing.com>
#


CC	  	= gcc 
XMLL = $(shell xml2-config --libs)
XMLI = $(shell xml2-config --cflags)

CFLAGS		= -g -Wall -O2 -DCONFIG_HIP ${XMLI} ${HI3I}
OBJS = hip_globals.o hip_ipsec.o hip_input.o hip_output.o hip_keymat.o hip_util.o hip_cache.o hip_status.o hip_dht.o 
#
# Darwin (OS X) or Linux; Cygwin deprecated
#
ifeq ($(shell uname -s), Darwin) 
all:  darwin
CFLAGS += -D__MACOSX__
OBJS += hip_mac.o
IPSECOBJS = hip_ipsec_win32.o
else
all:	linux
OBJS += hip_stun.o hip_stun_udp.o hip_netlink.o
endif

# compile objects for OS X 
darwin:	start_darwin hip darwinmsg

# compile for Linux w/kernel support
linux:	start_linux hipd hitgen finish

# compile objects for User-Mode HIP in Linux
umh:	start_umh hip umhmsg

# compile Hi3 support in Linux
#   set directory of i3 source here
hi3: I3 = ../../i3/src/i3
hi3: HI3I = -I$(I3) -I$(I3)/i3 -DHIP_I3
hi3: HI3LD = -L$(I3)/i3 -L$(I3)/i3_client -L$(I3)/utils -L$(I3)/aeshash -li3client -li3 -laes -lutils
hi3: OBJS += i3_send.o i3_recv.o
hi3:	start_hi3 i3_send.o i3_recv.o hipd 

#
# build targets
#
# compile HIP objects only, used for Windows and UMH
hip: CFLAGS += -D__UMH__
hip: ${OBJS} hip_main.o hip_ipsec_win32.o hip.h
# compile HIP daemon (hipd) binary
hipd:	${OBJS} hip_main.o
	${CC} ${OBJS} hip_main.o -o hipd -lcrypto ${XMLL} ${HI3LD} -lpthread -lipsec ${CFLAGS}
# compile hitgen binary
hitgen: ${OBJS} hitgen.o hip_ipsec_win32.o 
	@echo \>\> Building hitgen...
	${CC} ${OBJS} hitgen.o hip_ipsec_win32.o -o hitgen -lcrypto ${XMLL} -lpthread -lipsec ${CFLAGS}

#
# source rules
#
hip_globals.o:	hip.h hip_stun.h
hip_main.o:	hip.h hip_stun.h
hip_ipsec.o:	hip.h
hip_input.o:	hip.h
hip_output.o:	hip.h
hip_keymat.o:	hip.h
hip_util.o:	hip.h
hip_netlink.o:	hip.h
hip_cache.o:	hip.h
hip_status.o:	hip.h
hip_dht.o:	hip.h
hip_stun.o:	hip_stun.h hip_stun_udp.h
hip_stun_udp.o: hip_stun.h hip_stun_udp.h
hitgen.o:	hip.h
i3_send.o:	i3_hip.h
i3_recv.o:	i3_hip.h

#
# utility rules
#
start_win:
	@echo \>\> Building objects for HIP for Windows...
start_linux:
	@echo \>\> Building HIP for Linux with kernel support...
start_darwin:
	@echo \>\> Building HIP for OS X with user mode support...
start_umh:
	@echo \>\> Building objects for User-mode HIP...
start_hi3:
	@echo \>\> Building HIP with Hi3 support for Linux...
umhmsg:
	@echo The core HIP objects have been built, now you need to run \'make\'
	@echo from the ../win32 directory to build executables.
darwinmsg:
	@echo The core HIP objects have been built, now you need to run \'make\'
	@echo from the ../win32 directory to build executables.
finish:
	if test ! -d ../bin; then mkdir ../bin; fi
	cp hipd ../bin/; cp hitgen ../bin/
	@echo done.


clean:
	@echo Removing binary files...
	rm -f hipd hitgen 
	rm -f ${OBJS} ${IPSECOBJS} hitgen.o hip_main.o i3_send.o i3_recv.o
	@echo done.
