
ifeq ($(D),1)
    DEBUG  = -DDEBUG
else
    DEBUG  =
endif

ifeq ($(U),1)
    UNIT_TEST = -DUNIT_TEST
else
    UNIT_TEST =
endif

ifeq ($(OPENWRT),1)
    EXTRA_CFLAGS = -DOPENWRT
    STATIC_CXXFLAGS = -static-libstdc++
else
    EXTRA_CFLAGS =
    STATIC_CXXFLAGS = -static
endif

OUT = out
SRC = src

ALL = ${OUT}/libmipc_msg.so
ALL += ${OUT}/libmipc_api.so

#CC = /mtkeda/Bach/mt6297/20180310/arm/buildroot-2018.02/output/host/bin/arm-linux-gcc
#CXX = /mtkeda/Bach/mt6297/20180310/arm/buildroot-2018.02/output/host/bin/arm-linux-g++
#AR = /mtkeda/Bach/mt6297/20180310/arm/buildroot-2018.02/output/host/bin/arm-linux-ar
CC = gcc
CXX = g++
AR = ar

################################################################################
all: $(ALL)

doc:
	@mkdir -p out
	@doxygen doxygen.conf

style:
	@astyle --style=linux -s4 --recursive --pad-header --suffix=none -p ./*.c ./*.h ./*.cpp

clean:
	@rm -rf $(OUT)
################################################################################
MIPC_COMMON_SRCDIR = ${SRC}/common
MIPC_COMMON_OUTDIR = ${OUT}/common
MIPC_COMMON_SRC = $(filter-out ${MIPC_COMMON_SRCDIR}/platform_windows.c, $(wildcard ${MIPC_COMMON_SRCDIR}/*.c))
MIPC_COMMON_OBJ = $(patsubst %.c,${MIPC_COMMON_OUTDIR}/%.o,$(notdir ${MIPC_COMMON_SRC}))
MIPC_COMMON_CFLAGS = ${CFLAGS} ${EXTRA_CFLAGS} ${DEBUG} -D_LINUX -fPIC -Iinclude/common -Iinclude/api -Iinclude/msg

${MIPC_COMMON_OUTDIR}/%.o: ${MIPC_COMMON_SRCDIR}/%.c
	@mkdir -p ${MIPC_COMMON_OUTDIR}
	@${CC} ${MIPC_COMMON_CFLAGS} -o $@ -c $<
################################################################################
MIPC_MSG_SRCDIR = ${SRC}/msg
MIPC_MSG_OUTDIR = ${OUT}/msg
MIPC_MSG_SRC = $(wildcard ${MIPC_MSG_SRCDIR}/*.c)
MIPC_MSG_OBJ = $(patsubst %.c,${MIPC_MSG_OUTDIR}/%.o,$(notdir ${MIPC_MSG_SRC}))
MIPC_MSG_CFLAGS = ${CFLAGS} ${EXTRA_CFLAGS} ${DEBUG} ${UNIT_TEST} -D_LINUX -fPIC -Iinclude/common -Iinclude/api -Iinclude/msg
MIPC_MSG_SHARED_LIB = ${OUT}/libmipc_msg.so
MIPC_MSG_STATIC_LIB = ${OUT}/libmipc_msg.a

${MIPC_MSG_OUTDIR}/%.o: ${MIPC_MSG_SRCDIR}/%.c
	@mkdir -p ${MIPC_MSG_OUTDIR}
	@${CC} ${MIPC_MSG_CFLAGS} -o $@ -c $<

${MIPC_MSG_SHARED_LIB}: ${MIPC_MSG_OBJ} ${MIPC_COMMON_OBJ}
	@mkdir -p ${OUT}
	@${CC} -o $@ -shared $^

${MIPC_MSG_STATIC_LIB}: ${MIPC_MSG_OBJ} ${MIPC_COMMON_OBJ}
	@mkdir -p ${OUT}
	@${AR} rcs $@ $^

################################################################################
MIPC_API_SRCDIR = ${SRC}/api
MIPC_API_OUTDIR = ${OUT}/api
MIPC_API_SRC = $(wildcard ${MIPC_API_SRCDIR}/*.c)
MIPC_API_OBJ = $(patsubst %.c,${MIPC_API_OUTDIR}/%.o,$(notdir ${MIPC_API_SRC}))
MIPC_API_CFLAGS = ${CFLAGS} ${EXTRA_CFLAGS} ${DEBUG} -D_LINUX -fPIC -Iinclude/common -Iinclude/api -Iinclude/msg
MIPC_API_SHARED_LIB = ${OUT}/libmipc_api.so
MIPC_API_STATIC_LIB = ${OUT}/libmipc_api.a

${MIPC_API_OUTDIR}/%.o: ${MIPC_API_SRCDIR}/%.c
	@mkdir -p ${MIPC_API_OUTDIR}
	@${CXX} ${MIPC_API_CFLAGS} -o $@ -c $< -nostdlib

${MIPC_API_SHARED_LIB}: ${MIPC_API_OBJ}
	@mkdir -p ${OUT}
	@${CXX} -o $@ -shared $? -nostdlib

${MIPC_API_STATIC_LIB}: ${MIPC_API_OBJ}
	@mkdir -p ${OUT}
	@${AR} rcs $@ $?

################################################################################
MIPC_TEST_MSG_CFLAGS = ${CFLAGS} ${EXTRA_CFLAGS} ${DEBUG} -D_LINUX -Iinclude/common -Iinclude/api -Iinclude/msg

${OUT}/test_msg_shared: test/test_msg.c ${MIPC_MSG_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/test_msg.c -lmipc_msg -lpthread
${OUT}/test_msg: test/test_msg.c ${MIPC_MSG_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/test_msg.c -lmipc_msg -lpthread -lc -lgcc_eh -static

${OUT}/generate: test/generate.c ${MIPC_MSG_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/generate.c -lmipc_msg -lpthread -lc -lgcc_eh -static

${OUT}/sms_gen: test/sms_gen.c ${MIPC_MSG_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/sms_gen.c -lmipc_msg -lpthread -lc -lgcc_eh -static

${OUT}/sample_shared: test/sample.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/sample.c -lmipc_msg -lpthread
${OUT}/sample: test/sample.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/sample.c -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_netagent_shared: test/sample_netagent.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/sample_netagent.c -lmipc_msg -lpthread
${OUT}/sample_netagent: test/sample_netagent.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} test/sample_netagent.c -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_wwan_shared: test/msg/sample_wwan.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_wwan: test/msg/sample_wwan.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_sys_shared: test/msg/sample_sys.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_sys: test/msg/sample_sys.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_sim_shared: test/msg/sample_sim.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_sim: test/msg/sample_sim.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_sms_shared: test/msg/sample_sms.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_sms: test/msg/sample_sms.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_ho_shared: test/msg/sample_ho.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_ho: test/msg/sample_ho.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_act_data_call_shared: test/msg/sample_act_data_call.c ${MIPC_API_SHARED_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread
${OUT}/sample_act_data_call: test/msg/sample_act_data_call.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static
${OUT}/sample_set_cfg: test/msg/sample_set_cfg.c ${MIPC_API_STATIC_LIB}
	@${CC} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread -lc -lgcc_eh -static

${OUT}/test_sys: test/test_sys.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test_thermal: test/test_thermal.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test_nw: test/test_nw.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test_nw_sanity: test/test_nw_sanity.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/system_shared: test/system.cpp ${MIPC_MSG_SHARED_LIB} ${MIPC_API_SHARED_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread
${OUT}/system: test/system.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/reboot: test/reboot.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/fcc_lock: test/fcc_lock.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -I/usr/include/openssl -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread -lcrypto ${STATIC_CXXFLAGS}
${OUT}/sys_conf: test/sys_conf.c ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test_data: test/test_data.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/data: test/data.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test_sanity_tc016: test/test_sanity_tc016.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/test: test/test.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/pin_info_list: test/pin_info_list.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sim_status_req: test/sim_status_req.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sim: test/sim.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sim_ind: test/sim_ind.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sim_switch: test/sim_switch.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/enable: test/enable.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/verify: test/verify.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sys_info: test/sys_info.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/gtnet_ia: test/gtnet_ia.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/gtnet_act_data: test/gtnet_act_data.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/gtnet_deact_data: test/gtnet_deact_data.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/gtnet_get_data: test/gtnet_get_data.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/enable_pin: test/enable_pin.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/disable_pin: test/disable_pin.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}

${OUT}/sanity_radio: test/sanity/radio.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sanity_plmn_list: test/sanity/plmn_list.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sanity_ia: test/sanity/ericsson_ia.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sanity_act_data_call: test/sanity/ericsson_act_data_call.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}
${OUT}/sanity_deact_data_call: test/sanity/ericsson_deact_data_call.cpp ${MIPC_MSG_STATIC_LIB} ${MIPC_API_STATIC_LIB}
	@${CXX} ${MIPC_TEST_MSG_CFLAGS} -o $@ -L${OUT} $< -lmipc_api -lmipc_msg -lpthread ${STATIC_CXXFLAGS}

