# This file gathers compilation directive shared between lte-softmodem and oaisim
export COMMON_UTILS_DIR    = $(OPENAIR_DIR)/common/utils
export UE_NAS_DIR          = $(OPENAIR_DIR)/openair3/NAS
export S1AP_DIR            = $(OPENAIR_DIR)/openair3/S1AP
export X2AP_DIR            = $(OPENAIR_DIR)/openair2/X2AP
export SCTP_DIR            = $(OPENAIR_DIR)/openair3/SCTP
export UDP_DIR             = $(OPENAIR_DIR)/openair3/UDP
export GTPV1U_DIR          = $(OPENAIR_DIR)/openair3/GTPV1-U
export SECU_DIR            = $(OPENAIR_DIR)/openair3/SECU

UE_NAS_OBJ_DIR      = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(UE_NAS_DIR))
SECU_OBJ_DIR        = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(SECU_DIR))
S1AP_OBJ_DIR        = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(S1AP_DIR))
X2AP_OBJ_DIR        = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(X2AP_DIR))
SCTP_OBJ_DIR        = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(SCTP_DIR))
UDP_OBJ_DIR         = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(UDP_DIR))
GTPV1U_OBJ_DIR      = $(subst $(OPENAIR_DIR),$(OBJS_DIR),$(GTPV1U_DIR))

#Export common cflags (between softmodem and oaisim)
COMMON_CFLAGS		= \
	-D'FIRMWARE_VERSION="$(SVN_REV) - $(DATE_REV)"' \
	-Wall                       \
	-fno-strict-aliasing        \
	-O2                         \
	-Werror=implicit-function-declaration

# the code already uses C99 integer type (like int32_t)
# switch on full support for C99 + GNU extensions
CFLAGS += -std=gnu99

ITTI_MESSAGES_H		= messages_xml.h
ITTI_MESSAGES_XML	= messages.xml
ITTI_MESSAGES_FILE	= $(ITTI_DIR)/intertask_interface_types.h

include $(COMMON_UTILS_DIR)/Makefile.inc
include $(OPENAIR_TARGETS)/COMMON/Makefile.inc
include $(OPENAIR2_DIR)/NAS/Makefile.inc

ifeq ($(SECU), 1)	
ifeq ($(NETTLE_FOUND), 0)
@(warning "Nettle library >= 2.5 is not installed on your system, nettle-dev lib needed, continuing with security disabled")
SECU=0
else
ifeq ($(OPENSSL_FOUND), 0)
@(warning "openssl library is not installed on your system, openssl lib needed, continuing with security disabled")
SECU=0
else
CFLAGS += -DENABLE_SECURITY
LIBS += $(OPENSSL_LIBS) $(NETTLE_LIBS)
endif
endif
endif

CFLAGS += -DJUMBO_FRAME

ifdef MAX_NUM_CCs
CFLAGS           += -DMAX_NUM_CCs=$(MAX_NUM_CCs)
COMMON_CFLAGS    += -DMAX_NUM_CCs=$(MAX_NUM_CCs)
else
CFLAGS           += -DMAX_NUM_CCs=1
COMMON_CFLAGS    += -DMAX_NUM_CCs=1
endif

ifeq ($(ENABLE_RAL), 1)
CFLAGS += -DENABLE_RAL
endif

ifeq ($(PDCP_USE_NETLINK),1)  
CFLAGS += -DPDCP_USE_NETLINK
endif
ifeq ($(LIBCONFIG_LONG),1)	
CFLAGS += -DLIBCONFIG_LONG
endif

# add R9
ifeq ($(USE_MME), R10)	
COMMON_CFLAGS		+= -DENABLE_USE_MME
ENABLE_ITTI = 1
ifeq ($(LINK_ENB_PDCP_TO_GTPV1U), 1)	
    COMMON_CFLAGS       += -DLINK_ENB_PDCP_TO_GTPV1U
# COMMON_CFLAGS       += -I$(UDP_DIR)
# COMMON_CFLAGS       += -I$(GTPV1U_DIR)
# COMMON_CFLAGS       += -I$(GTPV1U_DIR)/nw-gtpv1u/shared
endif
endif

ifeq ($(ENABLE_ITTI),1)
COMMON_CFLAGS		+= -DENABLE_ITTI
COMMON_CFLAGS		+= -DUSER_MODE
COMMON_CFLAGS		+= -I$(OPENAIR1_DIR)
COMMON_CFLAGS		+= -I$(OPENAIR2_DIR)/NAS
COMMON_CFLAGS 	 	+= $(L2_incl)
COMMON_CFLAGS		+= $(UTILS_incl)
SHARED_DEPENDENCIES += $(ITTI_MESSAGES_H)
endif

ifeq ($(USE_MME), R10)	
LIBS			+= $(UE_NAS_OBJ_DIR)/libuenas.a $(SECU_OBJ_DIR)/libsecu.a 
LIBS			+= $(S1AP_OBJ_DIR)/libs1ap.a $(SCTP_OBJ_DIR)/libsctp.a -lsctp -lcrypt
LIBS			+= $(UDP_OBJ_DIR)/libudp.a $(GTPV1U_OBJ_DIR)/libgtpv1u.a 

ifdef X2AP
LIBS			+= $(X2AP_OBJ_DIR)/libx2ap.a
SHARED_DEPENDENCIES	+= $(UE_NAS_OBJ_DIR)/libuenas.a $(SECU_OBJ_DIR)/libsecu.a $(X2AP_OBJ_DIR)/libx2ap.a $(S1AP_OBJ_DIR)/libs1ap.a $(SCTP_OBJ_DIR)/libsctp.a  $(UDP_OBJ_DIR)/libudp.a $(GTPV1U_OBJ_DIR)/libgtpv1u.a
else 
SHARED_DEPENDENCIES	+= $(UE_NAS_OBJ_DIR)/libuenas.a $(SECU_OBJ_DIR)/libsecu.a $(S1AP_OBJ_DIR)/libs1ap.a $(SCTP_OBJ_DIR)/libsctp.a  $(UDP_OBJ_DIR)/libudp.a $(GTPV1U_OBJ_DIR)/libgtpv1u.a
endif 

COMMON_CFLAGS		+= -DLOG_NO_THREAD 
#-DEMIT_ASN_DEBUG

openair_cn_available := $(shell if [ -d "$(UE_NAS_DIR)" ]; then echo "0" ; else  echo "1" ; fi )
ifeq 	($(openair_cn_available), 0)
COMMON_CFLAGS		+= -DENABLE_NAS_UE_LOGGING
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/api/network
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/include
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/ies
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/emm/msg
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/esm/msg
COMMON_CFLAGS		+= -I$(OPENAIR_DIR)/openair3/NAS/EURECOM-NAS/src/util
endif

UENAS_CFLAGS		= $(COMMON_CFLAGS)
UENAS_CFLAGS		+= -I$(SECU_DIR)
export UENAS_CFLAGS

SECU_CFLAGS			= $(COMMON_CFLAGS)
SECU_CFLAGS			+= -I$(OPENAIR_DIR)/openair3/COMMON
SECU_CFLAGS			+= -I$(OPENAIR_DIR)/openair3/UTILS
SECU_CFLAGS			+= -I$(SECU_DIR)
export SECU_CFLAGS

COMMON_MME_CFLAGS	 = -I$(SECU_DIR)
COMMON_MME_CFLAGS       += -I$(SCTP_DIR)
COMMON_MME_CFLAGS	+= -I$(X2AP_DIR) 
COMMON_MME_CFLAGS	+= -I$(S1AP_DIR)
COMMON_MME_CFLAGS	+= -I$(UDP_DIR)
COMMON_MME_CFLAGS	+= -I$(GTPV1U_DIR)

S1AP_CFLAGS          = $(COMMON_CFLAGS) $(COMMON_MME_CFLAGS)
S1AP_CFLAGS         += -DENB_MODE
S1AP_CFLAGS         += -I$(TOP_DIR)
S1AP_CFLAGS         += $(UTIL_incl)
export S1AP_CFLAGS

X2AP_CFLAGS          = $(COMMON_CFLAGS) 
X2AP_CFLAGS         += -DENB_MODE
X2AP_CFLAGS         += -I$(TOP_DIR)
X2AP_CFLAGS         += $(UTIL_incl)
export X2AP_CFLAGS

UDP_CFLAGS	 		 = $(COMMON_CFLAGS) $(COMMON_MME_CFLAGS)
UDP_CFLAGS	 		+= -DENB_MODE
UDP_CFLAGS	 		+= -I$(TOP_DIR)
UDP_CFLAGS	 		+= $(UTIL_incl)
UDP_CFLAGS	 		+= -I$(OPENAIR_DIR)/openair2/ENB_APP
export UDP_CFLAGS

GTPV1U_CFLAGS            = $(COMMON_CFLAGS) $(COMMON_MME_CFLAGS)
GTPV1U_CFLAGS           += -DENB_MODE
GTPV1U_CFLAGS           += -I$(TOP_DIR)
GTPV1U_CFLAGS           += -I$(GTPV1U_DIR)/nw-gtpv1u/include
GTPV1U_CFLAGS           += -I$(GTPV1U_DIR)/nw-gtpv1u/shared
GTPV1U_CFLAGS           += -I$(OPENAIR_DIR)/openair2/ENB_APP
GTPV1U_CFLAGS           += $(UTIL_incl) -I$(OPENAIR3_DIR)/UTILS
export GTPV1U_CFLAGS

GTPV1U_ENB_CFLAGS            = $(COMMON_CFLAGS)
GTPV1U_ENB_CFLAGS           +=  $(OPENAIR_DIR)/openair2/COMMON
GTPV1U_ENB_CFLAGS           += -I$(X2AP_DIR)
GTPV1U_ENB_CFLAGS           += -I$(S1AP_DIR)
GTPV1U_ENB_CFLAGS           += -I$(UDP_DIR)
GTPV1U_ENB_CFLAGS           += -I$(GTPV1U_DIR)
GTPV1U_ENB_CFLAGS           += -DENB_MODE
GTPV1U_ENB_CFLAGS           += -I$(TOP_DIR)
GTPV1U_ENB_CFLAGS           += -I$(GTPV1U_DIR)/nw-gtpv1u/include
GTPV1U_ENB_CFLAGS           += -I$(GTPV1U_DIR)/nw-gtpv1u/shared
GTPV1U_ENB_CFLAGS           += -I$(OPENAIR_DIR)/openair2/ENB_APP
GTPV1U_ENB_CFLAGS           += $(UTIL_incl) -I$(OPENAIR3_DIR)/UTILS
export GTPV1U_ENB_CFLAGS


$(UE_NAS_OBJ_DIR)/libuenas.a: force_look
	@$(MAKE) -C $(UE_NAS_DIR) -f Makefile.UE $(UE_NAS_OBJ_DIR)/libuenas.a OUTDIR=$(UE_NAS_OBJ_DIR)
$(SECU_OBJ_DIR)/libsecu.a: force_look
	@$(MAKE) -C $(SECU_DIR) -f Makefile.eNB $(SECU_OBJ_DIR)/libsecu.a OUTDIR=$(SECU_OBJ_DIR)
$(X2AP_OBJ_DIR)/libx2ap.a: force_look
	@$(MAKE) -C $(X2AP_DIR) -f Makefile.inc $(X2AP_OBJ_DIR)/libx2ap.a OUTDIR=$(X2AP_OBJ_DIR)
$(S1AP_OBJ_DIR)/libs1ap.a: force_look
	@$(MAKE) -C $(S1AP_DIR) -f Makefile.eNB $(S1AP_OBJ_DIR)/libs1ap.a OUTDIR=$(S1AP_OBJ_DIR)
$(SCTP_OBJ_DIR)/libsctp.a: force_look
	@$(MAKE) -C $(SCTP_DIR) -f Makefile.eNB $(SCTP_OBJ_DIR)/libsctp.a OUTDIR=$(SCTP_OBJ_DIR)
$(UDP_OBJ_DIR)/libudp.a: force_look
	@$(MAKE) -C $(UDP_DIR) -f Makefile.eNB $(UDP_OBJ_DIR)/libudp.a OUTDIR=$(UDP_OBJ_DIR)
$(GTPV1U_OBJ_DIR)/libgtpv1u.a: force_look
	@$(MAKE) -C $(GTPV1U_DIR) -f Makefile.eNB $(GTPV1U_OBJ_DIR)/libgtpv1u.a OUTDIR=$(GTPV1U_OBJ_DIR)

OBJ = $(NAS_UE_OBJS) $(SECU_OBJS)
endif

export COMMON_CFLAGS

ifeq ($(ENABLE_ITTI),1)	
CFLAGS += $(COMMON_CFLAGS) $(COMMON_MME_CFLAGS) $(TARGETS_COMMON_incl)
OBJ += $(TARGETS_COMMON_OBJS)

gccxml_available = $(shell if [ `gccxml --version | grep GCC-XML -c` = "0" ]; then  echo "0" ; else  echo "1" ; fi )
ifeq 	($(gccxml_available), 0)
$(error gccxml is missing, please install)
endif
CFLAGS += -I$(OPENAIR2_DIR)/COMMON -DENABLE_ITTI $(UTILS_incl)
endif

$(ITTI_MESSAGES_XML): $(ITTI_MESSAGES_FILE)
	@echo "Generating messages.xml ..."
	@gccxml $(COMMON_CFLAGS) $< -fxml=$@ -I$(ITTI_DIR)
	@$(CC) -MM $(COMMON_CFLAGS) $< > $(basename $@).d
	@mv -f $(basename $@).d $(basename $@).d.tmp
	@sed -e 's|.*:|$@:|' < $(basename $@).d.tmp > $(basename $@).d
	@sed -e 's/.*://' -e 's/\\$$//' < $(basename $@).d.tmp | fmt -1 | \
	sed -e 's/^ *//' -e 's/$$/:/' >> $(basename $@).d
	@rm -f $(basename $@).d.tmp

$(ITTI_MESSAGES_H): $(ITTI_MESSAGES_XML)
	@echo "Generating messages_xml.h ..."
	@sed -e 's/[ ]*//' -e 's/"/\\"/g' -e 's/^/"/' -e 's/$$/\\n"/' $< > $@

force_look:
	@true

common-clean:
	@$(RM_F_V) $(ITTI_MESSAGES_H) $(ITTI_MESSAGES_XML) $(ITTI_MESSAGES_XML:.xml=.d)
	@$(MAKE) -C $(LFDS_DIR) -f makefile.linux clean OUTDIR=$(LFDS_OBJ_DIR)
	@if [ -d $(UE_NAS_OBJ_DIR) ]; then $(MAKE) -C $(UE_NAS_DIR) -f Makefile.UE clean OUTDIR=$(UE_NAS_OBJ_DIR); fi
	@if [ -d $(SECU_OBJ_DIR) ]; then $(MAKE) -C $(SECU_DIR) -f Makefile.eNB clean OUTDIR=$(SECU_OBJ_DIR); fi
	@if [ -d $(X2AP_OBJ_DIR) ]; then $(MAKE) -C $(X2AP_DIR) -f Makefile.inc clean OUTDIR=$(X2AP_OBJ_DIR); fi
	@if [ -d $(S1AP_OBJ_DIR) ]; then $(MAKE) -C $(S1AP_DIR) -f Makefile.eNB clean OUTDIR=$(S1AP_OBJ_DIR); fi
	@if [ -d $(SCTP_OBJ_DIR) ]; then $(MAKE) -C $(SCTP_DIR) -f Makefile.eNB clean OUTDIR=$(SCTP_OBJ_DIR); fi
	@if [ -d $(UDP_OBJ_DIR) ]; then $(MAKE) -C $(UDP_DIR) -f Makefile.eNB clean OUTDIR=$(UDP_OBJ_DIR); fi
	@if [ -d $(GTPV1U_OBJ_DIR) ]; then $(MAKE) -C $(GTPV1U_DIR) -f Makefile.eNB clean OUTDIR=$(GTPV1U_OBJ_DIR); fi

common-cleanall:
	@if [ -d $(UE_NAS_OBJ_DIR) ]; then $(MAKE) -C $(UE_NAS_DIR) -f Makefile.UE cleanall OUTDIR=$(UE_NAS_OBJ_DIR); fi
	@if [ -d $(SECU_OBJ_DIR) ]; then $(MAKE) -C $(SECU_DIR) -f Makefile.eNB cleanall OUTDIR=$(SECU_OBJ_DIR); fi
	@if [ -d $(X2AP_OBJ_DIR) ]; then $(MAKE) -C $(X2AP_DIR) -f Makefile.inc cleanall OUTDIR=$(X2AP_OBJ_DIR); fi
	@if [ -d $(S1AP_OBJ_DIR) ]; then $(MAKE) -C $(S1AP_DIR) -f Makefile.eNB cleanall OUTDIR=$(S1AP_OBJ_DIR); fi
	@if [ -d $(SCTP_OBJ_DIR) ]; then $(MAKE) -C $(SCTP_DIR) -f Makefile.eNB cleanall OUTDIR=$(SCTP_OBJ_DIR); fi
	@if [ -d $(UDP_OBJ_DIR) ]; then $(MAKE) -C $(UDP_DIR) -f Makefile.eNB cleanall OUTDIR=$(UDP_OBJ_DIR); fi
	@if [ -d $(GTPV1U_OBJ_DIR) ]; then $(MAKE) -C $(GTPV1U_DIR) -f Makefile.eNB cleanall OUTDIR=$(GTPV1U_OBJ_DIR); fi
	
