ifeq ($(makefile_defs),"1")
else
makefile_defs="1"
export makefile_defs

GIT := $(shell which git)

#ifneq ($(GIT),)
#   SCM_REV := $(shell if git --help describe |grep dirty 2>&1 >/dev/null ; \
#        then git describe --always --dirty 2>/dev/null; \
#        else git describe --always 2>/dev/null; fi)
#endif

#version number
VERSION = 1
PATCHLEVEL = 6
SUBLEVEL = 0

ifneq ($(SCM_REV),)
RELEASE := $(SCM_REV)
else
REL_VERSION := $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
RELEASE := $(REL_VERSION)$(EXTRAVERSION)
endif

APP_NAME ?= sems
SYSTEM_SAMPLECLOCK_RATE ?= 32000LL

CPPFLAGS += -D_DEBUG \
	  -D_THREAD_SAFE -D_REENTRANT \
	  -DSEMS_VERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"'\
	  -DOS='"$(OS)"' 	\
	  -DSYSTEM_SAMPLECLOCK_RATE=$(SYSTEM_SAMPLECLOCK_RATE) \
          -DSEMS_APP_NAME='"$(APP_NAME)"'
#         -DMAX_RTP_SESSIONS=8192 \
#	  -DSUPPORT_IPV6 \
#	  -DNO_THREADID_LOG \
#         -DLOG_PRETTY_FUNCTION \
#         -DLOG_LOC_DATA_ATEND


# compile with session thread pool support?
#      use this for very high concurrent call count 
#      applications (e.g. for signaling only)
#      if compiled with thread pool, there will be a 
#      thread pool of configurable size processing the
#      signaling and application logic of the calls.
#      if compiled without thread pool support, every
#      session will have its own thread.
#
#USE_THREADPOOL = yes

# compile with spandsp DTMF detection? see soft-switch.org
#   this needs a fairly new version of spandsp - tested with 0.0.4pre11
#   will not work with spandsp 0.0.2 .
#   (which means that current debian and gentoo packages don't work)
#   
#USE_SPANDSP    = yes
# statically link spandsp library? 
#   (might need adjusting spandsp lib path LIBSPANDSP_LDIR - may be /usr/lib)
#LIBSPANDSP_STATIC = yes
#LIBSPANDSP_LDIR = /usr/local/lib/

# compile with sample rate conversion from secret rabbit code? 
# (see http://www.mega-nerd.com/SRC/)
#
#USE_LIBSAMPLERATE = yes

# compile with internal resampler?
#
#
USE_INTERNAL_RESAMPLER = yes

#
# ZRTP support? (see zfoneproject.com)
#WITH_ZRTP = yes

#
# exclude some modules from compilation?
# e.g. python modules:

exclude_core_modules = silk
#exclude_apps_modules = py_sems ivr mailbox pin_collect conf_auth mp3 examples

# build in support for monitoring?
#
#
USE_MONITORING=no

# are we compiling for the test environment?
# if so, then this will exclude a list of applications
# which require a linkage with remote unvisible to us libraries.
TEST_ENVIRONMENT ?= no

# Support for long debug messages? (useful for debugging SIP messages' contents)
#
# disable for slight performance gain
LONG_DEBUG_MESSAGE=yes

# Is this a debug build or not?
debug=no

# Propagate all unparsed SIP headers into AmSipReply. Disabled by default
# because only special applications might need this and it brings higher memory
# demands and affects performance.
#PROPAGATE_UNPARSED_REPLY_HEADERS=no


################### end of configuration section #######################

LDFLAGS += -lm

GETOS=$(COREPATH)/compat/getos
GETARCH=$(COREPATH)/compat/getarch


OS   := $(shell if [ -f $(GETOS) ] ; then $(GETOS); else  $(CC) $(EXTRA_CFLAGS) -o $(GETOS) $(GETOS).c && $(GETOS); fi)
ARCH := $(shell if [ -f $(GETARCH) ] ; then $(GETARCH); else $(CC) $(EXTRA_CFLAGS) -o $(GETARCH) $(GETARCH).c && $(GETARCH); fi)

ifdef USE_THREADPOOL
CPPFLAGS += -DSESSION_THREADPOOL
endif

ifndef USE_SPANDSP
exclude_core_modules += g722
endif

ifdef USE_MONITORING
CPPFLAGS += -DUSE_MONITORING
endif

ifndef LONG_DEBUG_MESSAGE
CPPFLAGS += -DLOG_BUFFER_LEN=2048
endif

ifeq ($(PROPAGATE_UNPARSED_REPLY_HEADERS),yes)
CPPFLAGS += -DPROPAGATE_UNPARSED_REPLY_HEADERS
endif

# Additions for Solaris support.
ifeq ($(OS),solaris)
	CPPFLAGS += -DHAVE_SYS_SOCKIO_H -DBSD_COMP -fPIC -include compat/solaris.h
        CFLAGS += -DHAVE_SYS_SOCKIO_H -DBSD_COMP -fPIC -include compat/solaris.h
        # For nanosleep.
        LDFLAGS += -lrt
        # For inet_aton.
        LDFLAGS += -lresolv

	# I don't have libspeex installed.
	# binrpcctrl does some really weird header stuff that doesn't work.
	exclude_core_modules += binrpcctrl ilbc speex
endif




# fix sparc -> sparc64
ifeq ($(ARCH),sparc)
	ifeq ($(shell uname -m),sun4u)
		ARCH := sparc64
	endif
endif

# need OS specific for this ?
CXX = g++
CC = gcc
LD = $(CC)

CXXFLAGS += -std=c++20
CXXFLAGS += -Wall -Wno-reorder -Wno-unused -fPIC -g $(EXTRA_CXXFLAGS) 
CFLAGS   += -Wall -fPIC -g $(EXTRA_CFLAGS)

# only optimize if releasing, as it slows down the build process
ifneq ($(debug),yes)
	CXXFLAGS += -O2
	CFLAGS += -O2
endif

ifeq ($(DEBUG_PLAYOUT), yes)
CPPFLAGS +=  -DDEBUG_PLAYOUTBUF
endif

ifdef WITH_ZRTP
CPPFLAGS += -DWITH_ZRTP \
	    -DBUILD_ZRTP_MUTEXES \
	    -DBUILD_DEFAULT_CACHE -DBUILD_DEFAULT_TIMER -DUNIX -DBUILD_ZRTP_MUTEXES \
	    -I/usr/local/include/zrtp 
endif

TARGET=
LIB_LDFLAGS = -shared

ifeq ($(LD), clang)
LDFLAG_DYN=
LDFLAG_PTHREAD=
else
LDFLAG_DYN = -rdynamic
LDFLAG_PTHREAD = -lpthread
endif

ifeq ($(OS), linux)
	LDFLAGS +=  -ldl $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
	CPPFLAGS += -DPROPAGATE_COREDUMP_SETTINGS
else
ifeq ($(OS), freebsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), openbsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), netbsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), macosx)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
	LIB_LDFLAGS = -flat_namespace -undefined suppress -bundle
	CXXFLAGS += -fno-common
	CFLAGS += -fno-common

	#necessary for sa_len|ss_len|sin_len
	# may be needed on other *BSD
	CPPFLAGS += -DBSD44SOCKETS

	# add the DarwinPorts directory
	ifneq ($(ARCH), iphone)
	      CPPFLAGS += -D__DARWIN_UNIX03
	      CPPFLAGS += -I /opt/local/include
	      LDFLAGS += -L/opt/local/lib
	endif	      
else
ifeq ($(OS), solaris)
       	LDFLAGS+= -fPIC -ldl -lsocket -lnsl $(LDFLAG_PTHREAD)
	TARGET=solaris
	# These don't build on Solaris because of missing Python, invalid make args, or other.
	exclude_app_modules += py_sems py_comp pin_collect mailbox ivr diameter_client conf_auth mp3 cc_acc_xmlrpc xmlrpc2di

endif
endif
endif
endif
endif
endif

LDFLAGS += -lstdc++ 

ifeq ($(ARCH),iphone)
LDFLAGS += -lgcc_eh
endif

ifdef WITH_ZRTP
LDFLAGS +=-lzrtp -lbn
endif

LIB_LDFLAGS += $(LDFLAGS)


# install path is $(basedir) $(prefix) 
# example:
#  creating a bin. archive in /tmp, which unpacks in /usr/local
#  basedir=/tmp
#  prefix=/usr/local

DESTDIR ?= 
PREFIX ?= /usr/local
prefix ?= $(PREFIX)
BASEDIR ?=
basedir = $(BASEDIR)

# Redhat users should use sems.redhat instead of sems
# the file will be to $(bin_prefix)/$(bin_dir)/sems copied.
start_script = sems.redhat

# install prefixes for various stuff
cfg_prefix = $(basedir)$(prefix)
bin_prefix = $(basedir)$(prefix)
modules_prefix = $(basedir)$(prefix)
doc_prefix = $(basedir)$(prefix)
audio_prefix = $(basedir)$(prefix)
lib_prefix = $(basedir)$(prefix)

# dirs
cfg_dir = etc/$(APP_NAME)/

bin_dir = sbin/
modules_dir = lib/$(APP_NAME)/plug-in/
audio_dir = lib/$(APP_NAME)/audio/
lib_dir = lib/$(APP_NAME)/

ifeq ($(OS), linux)
	doc_dir = share/doc/$(APP_NAME)/
	man_dir = share/man/
else
ifeq ($(OS), freebsd)
	doc_dir = share/doc/$(APP_NAME)/
	man_dir = man/
else
ifeq ($(OS), openbsd)
	doc_dir = share/doc/$(APP_NAME)/
	man_dir = man/
else
ifeq ($(OS), netbsd)
	doc_dir = share/doc/$(APP_NAME)
	man_dir = man/
else
	doc_dir = doc/$(APP_NAME)/
	man_dir = man/
endif
endif
endif
endif

# target dirs for various stuff
bin_target = $(prefix)/$(bin_dir)
cfg_target ?= $(prefix)/$(cfg_dir)
app_cfg_target = $(cfg_target)etc/
modules_target = $(prefix)/$(modules_dir)
audio_target = $(prefix)/$(audio_dir)
lib_target = $(prefix)/$(lib_dir)

ifeq ($(OS), solaris)
#use GNU versions
INSTALL ?= ginstall
TAR ?= gtar
else
INSTALL ?= install 
TAR ?= tar
endif

INSTALL_CFG = $(INSTALL) -m 644
INSTALL_BIN = $(INSTALL) -m 755
INSTALL_MODULES = $(INSTALL) -m 755
INSTALL_DOC = $(INSTALL) -m 644
INSTALL_AUDIO = $(INSTALL) -m 644

#export stuff to sub-makes
export REL_VERSION RELEASE OS ARCH
export CPPFLAGS CXXFLAGS LDFLAGS CFLAGS LIB_LDFLAGS
export CXX CC LD
export DESTDIR PREFIX prefix basedir ser_prefix
export start_script
export cfg_prefix bin_prefix modules_prefix doc_prefix audio_prefix lib_prefix
export cfg_dir bin_dir modules_dir audio_dir lib_dir doc_dir man_dir
export bin_target cfg_target app_cfg_target modules_target audio_target lib_target
export INSTALL TAR INSTALL_CFG INSTALL_BIN INSTALL_MODULES INSTALL_DOC INSTALL_AUDIO
export APP_NAME

export USE_SPANDSP LIBSPANDSP_STATIC LIBSPANDSP_LDIR 
export USE_LIBSAMPLERATE USE_INTERNAL_RESAMPLER
export WITH_ZRTP
export USE_MONITORING
export exclude_core_modules exclude_app_modules
endif  # ifeq ($(makefile_defs, 1)

mk-install-dirs: $(DESTDIR)$(cfg_target) \
		 $(DESTDIR)$(app_cfg_target) \
		 $(DESTDIR)$(bin_prefix)/$(bin_dir) \
		 $(DESTDIR)$(modules_prefix)/$(modules_dir) \
		 $(DESTDIR)$(audio_prefix)/$(audio_dir) \
		 $(DESTDIR)$(doc_prefix)/$(doc_dir)

$(DESTDIR)$(cfg_target):
	mkdir -p $(DESTDIR)$(cfg_target)

$(DESTDIR)$(app_cfg_target):
	mkdir -p $(DESTDIR)$(app_cfg_target)

$(DESTDIR)$(bin_prefix)/$(bin_dir):
	mkdir -p $(DESTDIR)$(bin_prefix)/$(bin_dir)

$(DESTDIR)$(modules_prefix)/$(modules_dir):
	mkdir -p $(DESTDIR)$(modules_prefix)/$(modules_dir)

$(DESTDIR)$(audio_prefix)/$(audio_dir):
	mkdir -p $(DESTDIR)$(audio_prefix)/$(audio_dir)

$(DESTDIR)$(doc_prefix)/$(doc_dir):
	mkdir -p $(DESTDIR)$(doc_prefix)/$(doc_dir)
