LingYun IoT Studio NB-IoT research project
guowenxue
2018-11-20 e3bc01f494ddd7cabb8a3ab020fb46e588978744
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#*********************************************************************************
#      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*