# $(call source-to-object,source-file-list($1))
source-to-object = $(subst .c,.o,$(filter %.c,$1))

# 产生库文件相对路径,被每个库程序的module.mk文件使用.
# $(subdirectory)
subdirectory = $(patsubst $(SOURCE_DIR)/%/module.mk,%,          \
                 $(word                                         \
                   $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))

# 产生库文件规则,被每个库程序的module.mk文件使用.
$(call make-library,library-name($1),source-file-list($2))
define make-library
  libraries += $1
  sources += $2
  
  $1: $(call source-to-object,$2)
	$(QUIET)$(AR) $(ARFLAGS) $$@ $$^ $(ENULL)
	@echo "$$(INFO_C)AR $$@ done";echo
endef

# 产生依赖文件.
# $(call make-depend,source-file($1),object-file($2),depend-file($3))
define make-depend
  $(CC) -MM -MF $3 -MP -MT $2 $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) $1
endef

SOURCE_DIR := ../app
PRODUCT := $(MAKECMDGOALS)
CONFIG_FILE := $(SOURCE_DIR)/include/config.h
VERSION_FILE := $(SOURCE_DIR)/include/version.h
MQTTLIB_DIR := $(SOURCE_DIR)/lib/l_library
VERSION_LIB := version.a
DATE_STRING := `date "+%Y.%m.%d %k:%M:%S"`
VERSION_STRING := "6.2.1.14"

MV := mv -f
RM := rm -rf
SED := sed
TEST := test
MKDIR := mkdir -p

# 交叉编译设置
#DEFARCH = PC
DEFARCH = MYiR

ifeq ($(DEFARCH), PC)
  CROSS_COMPILE =
  CC = $(CROSS_COMPILE)gcc
  AR = $(CROSS_COMPILE)ar
  CFLAGS = -g -Wall -funwind-tables -rdynamic -DHAVE_CONFIG_H
else ifeq ($(DEFARCH), MYiR)
  CFLAGS += -g -Wall -funwind-tables -rdynamic -DHAVE_CONFIG_H
endif

LDLIB := -lreadline -lncurses -pthread -lrt -lsqlite3 -lm -lMQTT_AGENT -lpaho-mqtt3as -lcjson -lssl -lcrypto 

# 这里如果是‘@’则隐藏具体的编译命令
QUIET := @
ENULL := > /dev/null
INFO_C := "\\33[32mInfo\\33[0m "
ERROR_C := "\\33[31mInfo\\33[0m "

modules := $(patsubst $(SOURCE_DIR)/%/module.mk,%,              \
             $(shell find $(SOURCE_DIR) -name module.mk))

# 该变量仅用于产生输出目录,不做任何其他使用.			 
create-output-directories := $(shell for f in $(modules);              \
                                     do                                \
                                       $(TEST) -d $$f || $(MKDIR) $$f; \
                                     done)
programs :=
sources :=
libraries :=

objects = $(call source-to-object,$(sources))
dependencies = $(subst .o,.d,$(objects))

include_dirs := $(SOURCE_DIR)/include
LIBPATH := $(MQTTLIB_DIR)
CPPFLAGS += $(addprefix -I ,$(include_dirs))
vpath %.h $(include_dirs)
vpath %.c $(SOURCE_DIR)

.PHONY: empty
empty:
	@echo "Please explicitly specify the Makefile target!!!Example \"make PDMonitor\"."

include $(patsubst %,$(SOURCE_DIR)/%/module.mk,$(modules))

$(VERSION_LIB): $(CONFIG_FILE) $(libraries) version.c
	$(QUIET)$(RM) $(VERSION_FILE)
	@echo "/* WARNING: Don't modify this file anywhere!!! */" >> $(VERSION_FILE)
	@echo "#ifndef _VERSION_H_" >> $(VERSION_FILE)
	@echo "#define _VERSION_H_" >> $(VERSION_FILE)
	@echo "" >> $(VERSION_FILE)

	@echo "#define VERSION \"$(VERSION_STRING)\"" >> $(VERSION_FILE)
	@echo "#define DATE \"$(DATE_STRING)\"" >> $(VERSION_FILE)

	@echo "" >> $(VERSION_FILE)
	@echo "char* version_get();" >> $(VERSION_FILE)
	@echo "char* version_date_get();" >> $(VERSION_FILE)

	@echo "" >> $(VERSION_FILE)
	@echo "#endif" >> $(VERSION_FILE)
	@echo "/* WARNING: Don't modify this file anywhere!!! */" >> $(VERSION_FILE)
	@echo "COMPILE version.o"
	$(QUIET)$(COMPILE.c) -o version.o version.c
	$(QUIET)$(AR) $(ARFLAGS) $(VERSION_LIB) version.o $(ENULL)
	@echo "$(INFO_C)AR $@ done";echo

Cable: $(CONFIG_FILE) $(libraries) $(VERSION_LIB)
    # 使用两次$(libraries)避免库文件之间的交叉引用问题.
	$(QUIET)$(LINK.o) -rdynamic $(libraries) $(VERSION_LIB) $(LDLIB) -L $(LIBPATH) -o $@
	@echo "$(INFO_C)LINK $@ done";echo
    # $(QUIET)cp $@ //home//embed//LandPower//

Cable_MQTT: $(CONFIG_FILE) $(libraries) $(VERSION_LIB)
    # 使用两次$(libraries)避免库文件之间的交叉引用问题.
	$(QUIET)$(LINK.o) -rdynamic $(libraries) $(VERSION_LIB) $(LDLIB) -o $@
	@echo "$(INFO_C)LINK $@ done";echo
    # $(QUIET)cp $@ //home//embed//LandPower//

.PHONY: libraries
libraries: $(libraries)

.PHONY: clean
clean:
	$(RM) $(modules) $(CONFIG_FILE) $(VERSION_FILE) Cable Cable_MQTT $(VERSION_LIB) version.o

ifneq "$(MAKECMDGOALS)" "clean"
  -include $(dependencies)
  include config.mk
endif

%.o: %.c
	@echo "COMPILE $@"
	$(QUIET)$(call make-depend,$<,$@,$(subst .o,.d,$@))
	$(QUIET)$(COMPILE.c) -o $@ $<
