#*********************************************************************************
|
# Copyright: (C) 2018 LingYun IoT Studio
|
# All rights reserved.
|
#
|
# Filename: Makefile
|
# Description: This is the common top Makefile which used to call function to
|
# Cross compile the subdir source code and link all the libs to
|
# excutable binary file.
|
#
|
# Version: 1.0.0
|
# Author: Guo Wenxue <guowenxue@gmail.com>
|
# ChangeLog: 1, Release initial version on "2018/11/20 11:29:33 AM"
|
#
|
#********************************************************************************/
|
|
APP_BINARY_NAME=iotd
|
INST_PATH=/tftp
|
TOP_PATH=$(shell pwd)
|
LIBS_PATH=${TOP_PATH}/.libs
|
|
#Excutable binary file runs on platform
|
#ARCH?=arm
|
ARCH?=x86
|
|
#Static linked or Dynamic link
|
LINK_MODE=STATIC
|
|
CFLAGS+=-Werror -Wall
|
|
ifeq ("${LINK_MODE}", "STATIC")
|
CFLAGS+=--static
|
LDFLAGS+=-static
|
else
|
LDFLAGS+=-ldl
|
endif
|
|
# ==================================
|
# ** Cross Compiler Setting **
|
# ==================================
|
#
|
ifeq ("${ARCH}", "x86")
|
export CROSS_COMPILE=
|
else
|
export CROSS_COMPILE=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
|
endif
|
|
|
# Get alll the source code folder, all the source code in the subdir will be compiled and
|
# archive to a static library, named lib${subdir_name}.a
|
OBJFILES = $(patsubst %.c,%,$(wildcard *.c))
|
SUBSRCS = $(shell find . -follow -maxdepth 1 -type d|sed -n 's/.\///p'|grep -v 'libs' \
|
|grep -v 'etc' | grep -v '.svn'|grep -v '.git'|grep -v 'include'|grep -v 'bin')
|
|
# Add the subdir compiled static library into LDFLAGS
|
#LDFLAGS+=$(patsubst %,-L%,$(SUBSRCS))
|
#LDFLAGS+=$(patsubst %,-l%,$(SUBSRCS))
|
|
LDFLAGS+=-L${LIBS_PATH}
|
LDFLAGS+=$(patsubst %,-l%,$(SUBSRCS))
|
LIBS=$(patsubst %,${LIBS_PATH}/lib%.a,$(SUBSRCS))
|
LDFLAGS+=-Xlinker "-(" $(LIBS) -Xlinker "-)"
|
|
CFLAGS+=$(patsubst %,-I${TOP_PATH}/%,$(SUBSRCS))
|
CFLAGS+=-I$(TOP_PATH)
|
LDFLAGS+=-lpthread
|
|
export CC=${CROSS_COMPILE}gcc
|
export CXX=${CROSS_COMPILE}gcc
|
export AR=${CROSS_COMPILE}ar
|
export AS=${CROSS_COMPILE}as
|
export LD=${CROSS_COMPILE}ld
|
export RANLIB=${CROSS_COMPILE}ranlib
|
export STRIP=${CROSS_COMPILE}strip
|
export CFLAGS
|
export LDFLAGS
|
export ARCH
|
export LINK_MODE
|
export LIBS_PATH
|
|
#A function used to goes into the sub-folder to compile
|
MAKEFUNC = @MakeSubDir() \
|
{ \
|
for DIR in ${SUBSRCS}; do \
|
if [ -d $${DIR} ] ; then \
|
cd $${DIR}; \
|
MakeSubDir; \
|
if [ -f makefile -o -f Makefile ]; then \
|
pwd; \
|
make $(1) CROSS_COMPILE=${CROSS_COMPILE}; \
|
if [ "$$?" != "0" ]; then \
|
exit 1; \
|
fi; \
|
fi; \
|
cd ..; \
|
fi; \
|
done; \
|
if [ -f makefile -o -f Makefile ]; then \
|
make $(1); \
|
if [ "$$?" != "0" ]; then \
|
exit 1; \
|
fi; \
|
fi; \
|
}; \
|
|
MAKEME = cd $(2); MakeSubDir $(1); cd ..;
|
LOOPMAKEFUNC = $(MAKEFUNC) $(foreach dir,$(SUBSRCS),$(call MAKEME,$(1),$(dir)))
|
|
.PHONY: all
|
|
all: entry prepare binary install
|
|
prepare:
|
@if [ ! -L cp_library ] ; then \
|
ln -s ../cp_library; \
|
fi
|
|
entry:
|
@echo $(SUBSRCS)
|
@echo " ";
|
@echo " =========================================================";
|
@echo " ** Come into compile ${APP_BINARY_NAME} for ARCH $(ARCH) ";
|
@echo " =========================================================";
|
@echo " ";
|
@make clean
|
|
subdir:
|
@$(call LOOPMAKEFUNC,all)
|
@echo " ";
|
@echo " =========================================================";
|
@echo " ** Compile and linking ${APP_BINARY_NAME} for ${ARCH} now";
|
@echo " =========================================================";
|
|
%: %.c
|
$(CC) $< $(CFLAGS) -c -o $@.o
|
|
binary: subdir $(OBJFILES)
|
$(CC) $(CFLAGS) -o $(APP_BINARY_NAME) *.o $(LDFLAGS)
|
$(STRIP) $(APP_BINARY_NAME)
|
|
tag:
|
@cscope -Rbq
|
@ctags --c-kinds=+defglmnstuvx --langmap=c:.c.h.ho.hem.het.hec.hev.him.hit.hic.hiv -R .
|
|
install:
|
cp $(APP_BINARY_NAME) ${INST_PATH}
|
|
uninstall:
|
rm -f ${INST_PATH}/$(APP_BINARY_NAME)
|
|
clean:
|
@$(call LOOPMAKEFUNC,clean)
|
@rm -rf ${LIBS_PATH}
|
@rm -f *.o $(APP_BINARY_NAME)
|
@rm -f *.elf*
|
@rm -f *.gdb
|
@rm -f cp_library
|
|
distclean: clean
|
@$(call LOOPMAKEFUNC,distclean)
|
|
clear: distclean
|
@rm -f tags cscope*
|