# $Id$
#
# makefile defs (CC, LD,a.s.o)
#


#version number
VERSION = 0
PATCHLEVEL = 8
SUBLEVEL = 7
EXTRAVERSION = -13-2

RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
OS = $(shell uname -s)
ARCH = $(shell uname -m |sed -e s/i.86/i386/ -e s/sun4u/sparc64/ )

prefix = /usr/local
cfg-prefix = $(prefix)
bin-prefix = $(prefix)
modules-prefix= $(prefix)
doc-prefix = $(prefix)
man-prefix = $(prefix)

# dirs
cfg-dir = etc/ser/
bin-dir = sbin/
modules-dir = lib/ser/modules/
doc-dir = share/doc/ser/
man-dir = share/man/

INSTALL = install 
INSTALL-CFG = $(INSTALL) -m 644
INSTALL-BIN = $(INSTALL) -m 755
INSTALL-MODULES = $(INSTALL) -m 755
INSTALL-DOC = $(INSTALL) -m 644
INSTALL-MAN = $(INSTALL) -m 644


# compile-time options
#
# -DSTATS
#		allows to print out number of packets processed on CTRL-C;
# 		implementation still nasty and reports per-process
# -DNO_DEBUG
#		turns off some of the debug messages (DBG(...)).
# -DNO_LOG
#		completely turns of all the logging (and DBG(...))
# -DEXTRA_DEBUG
#		compiles in some extra debugging code
# -DDNS_IP_HACK
#		faster ip address resolver for ip strings (e.g "127.0.0.1")
# -DSHM_MEM
#		compiles in shared mem. support, needed by some modules and
#		by USE_SHM_MEM
# -DSHM_MMAP
#		use mmap instead of SYSV shared memory
# -DPKG_MALLOC
#		uses a faster malloc (exclusive w/ USE_SHM_MEM)
# -DUSE_SHM_MEM
#		all pkg_malloc => shm_malloc (most mallocs use a common sh.
#		mem. segment); don't define PKG_MALLOC if you want this!
# -DDBG_QM_MALLOC
#		qm_malloc debug code, will cause pkg_malloc and shm_malloc
#		to keep and display lot of debuging information: file name,
#		function, line number of malloc/free call for each block,
#		extra error checking (trying to free the same pointer
#		twice, trying to free a pointer alloc'ed with a different
#		malloc etc.)
# -DVQ_MALLOC
#		additional option to PKG_MALLOC which utilizes a fater then
#		qm version
#		(not true anymore, q_malloc performs approx. the same)
# -DF_MALLOC
#		an even faster malloc, not recommended for debugging
# -DDBG_MALLOC
#		issues additional debugging information if lock/unlock is called
# -DFAST_LOCK
#		uses fast arhitecture specific locking (see the arh. specific section)
# -DBUSY_WAIT
#		uses busy waiting on the lock
# -DADAPTIVE_WAIT
#		try busy waiting for a while and if the lock is still held go to
#		force reschedule
# -DADAPTIVE_WAIT_LOOPS=number
#		number of loops we busy wait, after "number" loops have elapsed we 
#		force a reschedule
# -DNOSMP
#		don't use smp compliant locking (faster but won't work on SMP machines)
#		(not yet enabled)
# -DNO_PINGTEL_TAG_HACK
#		if enabled, To-header-field will be less liberal and will not accept
#		'tag=' (tag parameter with equal sign and without value); it is called
#		this way because such message was sighted from a Pingtel phone
# -DWITH_SNMP_MOD
#  		if enabled, allows forking of the snmp agent just before child
#  		forking (done at the top of main_loop). Needed if you want
#  		to use the snmp module.

DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
	 -DOS='"$(OS)"' -DCOMPILER='"$(CC_VER)"'\
	 -DPKG_MALLOC \
	 -DSHM_MEM  -DSHM_MMAP \
	 -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 \
	 -DUSE_IPV6 \
	 # -DF_MALLOC \
	 #-DEXTRA_DEBUG \
	 #-DDBG_QM_MALLOC \
	 #-DVQ_MALLOC  
	 #-DCONTACT_BUG
	 #-DDBG_LOCK
	 #-DNO_DEBUG \
	 #-DNOSMP \
	 #-DEXTRA_DEBUG \
	 #-DUSE_SHM_MEM \
	 #-DSTATS \
	 #-DWITH_SNMP_MOD \
	 #-DNO_LOG

#PROFILE=  -pg #set this if you want profiling
#mode = debug
mode = release

# platform dependent settings


#common
CC=gcc
LD= $(CC)
CC_LONGVER=$(shell if  $(CC) -v 2>/dev/null; then \
						$(CC) -v 2>&1 ;\
					else \
						$(CC) -V 2>&1 ; \
					fi )
MKTAGS=ctags -R .

#find-out the compiler's name

ifneq (,$(findstring gcc, $(CC_LONGVER)))
	CC_NAME=gcc
	CC_VER=$(CC) $(shell $(CC) --version|head -1| \
				 sed -e 's/.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/' -e 's/[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/')
	# sun sed is a little brain damaged => this complicated expression
	MKDEP=$(CC) -MM $(DEFS)
	#transform gcc version into 2.9x or 3.0
	CC_SHORTVER=$(shell echo "$(CC_VER)" | cut -d" " -f 2| \
				 sed -e 's/[^0-9]*-\(.*\)/\1/'| \
				 sed -e 's/2\.9.*/2.9x/' -e 's/3\..\..*/3.0/' -e \
				 	's/3\../3.0/')
endif

ifneq (, $(findstring Sun, $(CC_LONGVER)))
	CC_NAME=suncc
	CC_SHORTVER=$(shell echo "$(CC_LONGVER)"|head -1| \
					sed -e 's/.*\([0-9]\.[0-9]\).*/\1/g' )
	CC_VER=$(CC) $(CC_SHORTVER)
	MKDEP=$(CC) -xM1 $(DEFS)
endif

ifneq (, $(findstring Intel(R) C++ Compiler, $(CC_LONGVER)))
	# very nice: gcc compatible
	CC_NAME=icc
	CC_FULLVER=$(shell echo "$(CC_LONGVER)"|head -1| \
					sed -e 's/.*Version \([0-9]\.[0-9]\.[0-9]*\).*/\1/g' )
	CC_SHORTVER=$(shell echo "$(CC_FULLVER)" | cut -d. -f1,2 )
	CC_VER=$(CC) $(CC_FULLVER)
	MKDEP=$(CC) -MM $(DEFS)
endif


ifeq (,$(CC_NAME))
#not found
	CC_NAME=$(CC)
	CC_SHORTVER=unknown
	CC_VER=unknown
	MKDEP=gcc -MM $(DEFS)
$(warning	Unknown compiler $(CC)\; supported compilers: \
			gcc, sun cc, intel icc )
endif

# find ldname (gnu or solaris)
ifeq ($(ARCH), sparc64)
ifeq ($(CC_NAME), gcc)
		LDGCC=$(shell $(CC) -v 2>&1 | grep with-ld| \
				   sed -e 's/.*--with-ld=\([^ ][^ ]*\).*/\1/' )
		LDPATH=$(shell  if [ -z "$(LDGCC)" ] ; then echo "ld" ;\
						else \
						if $(LDGCC) -V 2>/dev/null 1>/dev/null; then \
							echo $(LDGCC); \
						else echo "ld" ; \
						fi\
						fi)
						
		LDTYPE=$(shell if $(LDPATH) -V 1>/dev/null  2>/dev/null; then \
							if $(LDPATH) -V 2>&1|grep GNU >/dev/null; \
							then echo gnu; \
							else \
							if $(LDPATH) -V 2>&1|grep Solaris >/dev/null;\
							then echo solaris; \
							else \
								echo unknown ; \
							fi \
							fi \
						fi)
endif
endif

# arh. specific definitions
ifeq ($(ARCH), i386)
	DEFS+= -DFAST_LOCK
endif

ifeq ($(ARCH), sparc64)
ifeq 	($(CC_NAME), gcc)
		DEFS+= -DFAST_LOCK
endif
endif

ifeq ($(ARCH), armv4l)
	DEFS+= -DFAST_LOCK
endif



# setting CFLAGS
ifeq ($(mode), release)
	#if i386
ifeq	($(ARCH), i386)
		# if gcc 
ifeq		($(CC_NAME), gcc)
				#common stuff
				CFLAGS=-O9 -funroll-loops  -Wcast-align $(PROFILE) -Winline\
					-Wall   \
			#if gcc 3.0
ifeq			($(CC_SHORTVER), 3.0)
					CFLAGS+=-minline-all-stringops -malign-double \
							-falign-loops \
							-march=athlon \
							#-mcpu=athlon
else
ifeq			($(CC_SHORTVER), 2.9x) #older gcc version (2.9[1-5])
$(warning 			Old gcc detected ($(CC_SHORTVER)), use  gcc 3.0.x \
					for better results)
					
					CFLAGS+=-m486 \
							-malign-loops=4
else
				#really old version
$(warning			You are using an old and unsupported gcc \
					 version  ($(CC_SHORTVER)), compile at your own risk!)

endif			# CC_SHORTVER, 2.9x
endif			# CC_SHORTVER, 3.0

else		# CC_NAME, gcc
ifeq		($(CC_NAME), icc)
			CFLAGS=-O3  -ipo -ipo_obj -unroll  $(PROFILE) \
					 -tpp6 -xK  #-openmp  #optimize for PIII 
				# -prefetch doesn't seem to work
				#( ty to inline acroos files, unroll loops,prefetch,
				# optimize for PIII, use PIII instructions & vect.,
				# mutlithread loops)
else
				#other compilers
$(error 			Unsupported compiler ($(CC):$(CC_NAME)), try gcc)
endif		#CC_NAME, icc
endif		#CC_NAME, gcc
endif	#ARCH, i386


	#if sparc64
ifeq	($(ARCH), sparc64)
			#if gcc
ifeq		($(CC_NAME), gcc)
				#common stuff
				CFLAGS=-O9 -funroll-loops  $(PROFILE) -Winline\
					-Wall\
					#-Wcast-align \
					#-Wmissing-prototypes 
				#if gcc 3.0
ifeq			($(CC_SHORTVER), 3.0)
					#use 32bit for now
					CFLAGS+= -mcpu=ultrasparc -mtune=ultrasparc  -m32  \
					# -mcpu=v9 or ultrasparc? # -mtune implied by -mcpu
					#-mno-epilogue #try to inline function exit code
					#-mflat # omit save/restore
					#-,faster-structs #faster non Sparc ABI structure copy ops
else			# CC_SHORTVER, 3.0
ifeq			($(CC_SHORTVER), 2.9x) #older gcc version (2.9[1-5])
$(warning 			Old gcc detected ($(CC_SHORTVER)), use  gcc 3.0.x \
					for better results)
					
					CFLAGS+= -mv8 -Wa,-xarch=v8plus
else			#CC_SHORTVER, 2.9x
				#really old version
$(warning			You are using an old and unsupported gcc \
					 version ($(CC_SHORTVER)), compile at your own risk!)
					
					CFLAGS+= -mv8 -Wa,-xarch=v8plus
endif			#CC_SHORTVER, 2.9x
endif			#CC_SHORTVER, 3.0

else		#CC_NAME, gcc
ifeq		($(CC_NAME), suncc)
			CFLAGS+=-xO5 -fast -native -xarch=v8plusa -xCC \
					-xc99 # C99 support
			# -Dinline="" # add this if cc < 5.3 (define inline as null)
else
				#other compilers
$(error 			Unsupported compiler ($(CC):$(CC_NAME)), try gcc)
endif		#CC_NAME, suncc
endif		#CC_NAME, gcc
endif	#ARCH, sparc64

	#if ipaq/netwinder
ifeq	($(ARCH), armv4l)
		# if gcc 
ifeq		($(CC_NAME), gcc)
				#common stuff
				CFLAGS=-O9 -funroll-loops  -Wcast-align $(PROFILE) -Winline\
					-Wall   \
			#if gcc 3.0
ifeq			($(CC_SHORTVER), 3.0)
					CFLAGS+= -mcpu=strongarm1100
							#-mcpu=athlon
else
ifeq			($(CC_SHORTVER), 2.9x) #older gcc version (2.9[1-5])
$(warning 			Old gcc detected ($(CC_SHORTVER)), use  gcc 3.0.x \
					for better results)
					
					CFLAGS+=
else
				#really old version
$(warning			You are using an old and unsupported gcc \
					 version ($(CC_SHORTVER)), compile at your own risk!)

endif			# CC_SHORTVER, 2.9x
endif			# CC_SHORTVER, 3.0

else		# CC_NAME, gcc
				#other compilers
$(error 			Unsupported compiler ($(CC):$(CC_NAME)), try gcc)
endif		#CC_NAME, gcc
endif	#ARCH, i386


	


# setting LDFLAGS
ifeq	($(CC_NAME), gcc)
ifeq		($(ARCH), sparc64)
ifeq			($(LDTYPE), solaris)
					# solaris ld
					LDFLAGS+=-O2 $(PROFILE)
					MOD_LDFLAGS=-G $(LDFLAGS)
else			
					#gcc and maybe others, on solaris, with gnu ld
					LDFLAGS+=-Wl,-O2 -Wl,-E $(PROFILE) 
					MOD_LDFLAGS=-shared $(LDFLAGS)
endif
else
			#gcc and maybe others
			LDFLAGS+=-Wl,-O2 -Wl,-E $(PROFILE) 
			MOD_LDFLAGS=-shared $(LDFLAGS)
endif
endif
ifeq	($(CC_NAME), icc)
	#gcc and maybe others
	LDFLAGS+=-Wl,-O2 -Wl,-E $(PROFILE) 
	MOD_LDFLAGS=-shared $(LDFLAGS)
endif
ifeq	($(CC_NAME), suncc)
	LDFLAGS+=-xO5 $(PROFILE)
	MOD_LDFLAGS=-G $(LDFLAGS)
endif
	# we need -fPIC -DPIC only for shared objects, we don't need them for
	# the executable file, because it's always loaded at a fixed address
	# -andrei
else	#mode,release
ifeq	($(CC_NAME), gcc)
		CFLAGS=-g -Wcast-align -Winline $(PROFILE)
ifeq		($(ARCH), sparc64)
			CFLAGS+= -mcpu=ultrasparc 
endif
ifeq		($(LDTYPE), solaris)
			#solaris ld
			LDFLAGS+=-g $(PROFILE)
			MOD_LDFLAGS=-G $(LDFLAGS)
else
			#gnu or other ld type
			LDFLAGS+=-g -Wl,-E $(PROFILE)
			MOD_LDFLAGS=-shared $(LDFLAGS)
endif
endif
ifeq	($(CC_NAME), icc)
		CFLAGS=-g  $(PROFILE)
		LDFLAGS+=-g -Wl,-E $(PROFILE)
		MOD_LDFLAGS=-shared $(LDFLAGS)
endif
ifeq	($(CC_NAME), suncc)
		CFLAGS= -g $(PROFILE)
		LDFLAGS+=-g $(PROFILE)
		MOD_LDFLAGS=-G $(LDFLAGS)
endif

endif #mode=release


#*FLAGS used for compiling the modules
ifeq	($(CC_NAME), gcc)
MOD_CFLAGS=-fPIC -DPIC $(CFLAGS)
endif
ifeq	($(CC_NAME), icc)
MOD_CFLAGS=-Kpic $(CFLAGS)
endif
ifeq	($(CC_NAME), suncc)
MOD_CFLAGS=-Kpic  $(CFLAGS)
endif

LEX=flex
YACC=bison
YACC_FLAGS=-d -b cfg
# on linux and freebsd keep it empty (e.g. LIBS= )
# on solaris add -lxnet (e.g. LIBS= -lxnet)
LIBS=-lfl -ldl -lresolv


#os specific stuff
ifeq ($(OS), Linux)
	#same as common
endif

ifeq  ($(OS), SunOS)
	ifeq ($(mode), release)
		#use these only if you're using gcc with Solaris ld
		#LDFLAGS=-O2 $(PROFILE)
		#MOD_LDFLAGS=-O2 -G
	else
		#LDFLAGS=-g $(PROFILE)
		#MOD_LDFLAGS=-g -G
	endif

	YACC=yacc

ifeq ($(CC_NAME), suncc)
	LIBS=-lfast
endif
	LIBS+=-ldl -L/usr/local/lib -L/usr/lib/mysql -lfl -lxnet -lrt -lnsl
	# -lrt needed for sched_yield
endif

ifeq ($(OS), FreeBSD)

	YACC=yacc
	LIBS= -lfl  #dlopen is in libc

endif

ifneq (,$(findstring CYGWIN, $(OS)))
	#cygwin is the same as common
endif