PLATFORM=x86

# 判断当前平台是否为x86
ifeq ($(PLATFORM),x86)
SEL_DIR=out
else
SEL_DIR=bin
endif

SRC_DIR = .
SRC_EXP_DIR = .
# ../logical
TARGET = ./$(SEL_DIR)/$(notdir $(CURDIR))

# 定义输出目录
#OUTDIR := ./out
#OBJDIR := ./$(OUTDIR)/obj
OUTDIR := ./out
OBJDIR := ./$(OUTDIR)/obj/
##$(notdir $(CURDIR))

AllDIRS := $(shell ls -R .| grep '^\./.*:$$' | awk '{gsub(":","");print}') .

CSRCS = $(foreach n,$(AllDIRS) , $(wildcard $(n)/*.c)) 		#$(wildcard *.c)

CXXSRCS = $(foreach n,$(AllDIRS) , $(wildcard $(n)/*.cpp)) 	#$(wildcard *.cpp)

OBJS := $(CSRCS:.c=.o) $(CXXSRCS:.cpp=.o)
DEPS := $(CSRCS:.c=.d) $(CXXSRCS:.cpp=.d)

# 根据源文件列表,将.c文件和.cpp文件生成对应的.o文件列表
OBJS := $(patsubst %, $(OBJDIR)/%, $(CSRCS:.c=.o)) $(patsubst %, $(OBJDIR)/%, $(CXXSRCS:.cpp=.o))



LIBRARY_AR_PATH = #../../../../lib_src/a_src
LIBRARY_SO_PATH = ../../../../lib_src/so_src

STATIC_LIBS_DIR  = ../../../../lib/$(PLATFORM)/lib_a
SHARED_LIBS_DIR = ../../../../lib/$(PLATFORM)/lib_so

LIBRARY_NAME = "xxx"


# 头文件必须放在include目录下才能自动识别
INCDIRS= $(SRC_DIR)/../../../../include/general_type \
		 $(shell find ${SRC_DIR} -type d -name inc)  \
		 $(shell find ${LIBRARY_AR_PATH} -type d -name inc) \
		 $(shell find ${LIBRARY_SO_PATH} -type d -name inc) \


INC_PATH = $(foreach path,$(INCDIRS),-I$(path))


# .so
LIB_SO_PATH = ../../../../lib/$(PLATFORM)/lib_so \
             -lprlog \
			-Wl,-rpath=./../../../../lib/$(PLATFORM)/lib_so \


# .a
LIB_AR_PATH = ../lib/$(PLATFORM)/lib_a \
			-l prlog \
			-l prxml \
			-l prgeneral \
#			-l stdc++ \
#			-l dl \
#			-l pthread

DEBUG_FLAG = $(DBGFLAGS)

# DEBUG_FLAG := -g

$(OBJDIR)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(DEBUG_FLAG) $< -c -o $@ $(INC_PATH) $(SHARED_LIBS) -L$(LIB_AR_PATH) -L$(LIB_SO_PATH)

$(OBJDIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(DEBUG_FLAG) $< -c -o $@ $(INC_PATH) $(SHARED_LIBS) -L$(LIB_AR_PATH) -L$(LIB_SO_PATH)

#test:
#	@echo "LIBRARY_AR_PATH = $(LIBRARY_AR_PATH)"
#	@echo "INC_PATH = $(INC_PATH)"

BUILD_DIR = build
#prepare1:
#	@if [ -d $(BUILD_DIR) ]; then \
#		echo "Removing existing $(BUILD_DIR) directory..."; \
#		rm -rf $(BUILD_DIR); \
#	fi

$(TARGET):  $(OBJS)
#	@if [ -d ./build ];then rm -rf build/ && make;  fi
	@echo "CROSS_COMPILE = $(CROSS_COMPILE)"
	@echo "PLATFORM = $(PLATFORM)"
	@echo
	$(CXX) $(DEBUG_FLAG) $^ -o $@ $(INC_PATH) -L$(LIB_AR_PATH) -L$(LIB_SO_PATH)
	@echo -e "\e[32m >>> build success: $(TARGET) \e[0m"

.PHONY: clean all sh rebuild env

all: $(TARGET)
#	source /opt/fsl-imx-wayland/5.10-hardknott/environment-setup-cortexa53-crypto-poky-linux

rebuild: clean $(TARGET)

clean:
	-$(RM) $(OBJS) $(TARGET)
	rm -rf build
	rm -rf $(OBJDIR)
	rm -rf $(notdir $(CURDIR)).log

# Standard GNU target
distclean:
	-$(RM) *.o  $(TARGET)
	rm -rf $(OUTDIR)

# modify *.h, compile  again
h: touch_cpp all

# find the newest .c | .cpp and touch it
touch_cpp:
	find . -type f \( -name "*.c" -o -name "*.cpp"  -name "main*" \)|xargs ls -t|head -n1|xargs touch

r: run
run: all check

# Standard GNU target
check:
#$(TARGET)
	@echo "#### $(LIB_SO_PATH)"
	@echo "sh  $(TARGET)"
	$(TARGET)
	@pwd
#	@echo -e "call: sqlite3 $(shell find ../board_dir/datebase/ -name '*.db' | xargs ls -t | head -n1) .dump \e[32m"
#	@sqlite3 $(shell find ../board_dir/datebase/ -name "*.db" | xargs ls -t | head -n1) .dump
	@echo -e "\e[0m"

sh:
	@./build.sh $@

shr:
	@./build.sh $@ && ../board_dir/$(SEL_DIR)/$(notdir $(CURDIR))

env:
	@echo $(SEL_DIR)
	@echo $(GCC)
	@echo $(CPP)
	@echo "CROSS_COMPILE = $(CROSS_COMPILE)"
	@echo "PLATFORM = $(PLATFORM)"
	@echo
	#$(CXX) #$(DEBUG_FLAG) $^ -o $@ $(INC_PATH) -L$(LIB_AR_PATH) -L$(LIB_SO_PATH)
	@echo -e "\e[32m >>> build success: $(TARGET) \e[0m"
