guowenxue
2021-11-03 c2c9e808120ec584603130c8d9f087ca0cf5e74f
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
 
CROSSTOOL=
INST_PATH=`pwd`/install
 
LYFTP_SRC=ftp://master.iot-yun.club/src/
 
export CFLAGS=
export LDFLAGS=
 
 
function msg_banner()
    echo "" 
    echo "+-----------------------------------------------------------------------" 
    echo "|  $1 " 
    echo "+-----------------------------------------------------------------------"
    echo ""
}
 
function check_result()
{
           if [ $? != 0 ] ; then
               echo ""
               echo "+-----------------------------------------------------------------------"
               echo "|  $1 "
               echo "+-----------------------------------------------------------------------"
               echo ""
               exit ;
           fi
}
 
 
function export_cross()
{
    msg_banner "export cross compiler tools"
 
    # export cross toolchain
    export CC=${CROSSTOOL}gcc
    export AS=${CROSSTOOL}as
    export AR=${CROSSTOOL}ar
    export LD=${CROSSTOOL}ld
    export NM=${CROSSTOOL}nm
    export RANLIB=${CROSSTOOL}ranlib
    export OBJDUMP=${CROSSTOOL}objdump
    export STRIP=${CROSSTOOL}strip
 
    # export cross configure 
    export CONFIG_CROSS=" --build=i686-pc-linux --host=arm-linux "
 
    # Clear LDFLAGS and CFLAGS 
    export LDFLAGS= 
    export CFLAGS= 
}
 
function compile_libgpiod()
{
    PREFIX_PATH=${INST_PATH}
    SRC_NAME=libgpiod-1.6.3
    PACK_SUFIX=tar.gz
    IMG_NAME=libgpiod.so
 
    if [ -f ${PREFIX_PATH}/lib/${IMG_NAME} ] ; then
        msg_banner "$SRC_NAME already compile and installed"
        return 0;
    fi
 
    msg_banner "Start compile $SRC_NAME "
 
    if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then
        #wget https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/snapshot/$SRC_NAME.$PACK_SUFIX
        wget $LYFTP_SRC/$SRC_NAME.$PACK_SUFIX 
        check_result "ERROR: download ${SRC_NAME} failure"
    fi
 
    tar -xzf ${SRC_NAME}.${PACK_SUFIX}
 
    cd ${SRC_NAME}
 
    ./autogen.sh
 
    echo "ac_cv_func_malloc_0_nonnull=yes" >arm-linux.cache
    ./configure --enable-tools=yes --prefix=${PREFIX_PATH} ${CONFIG_CROSS} --cache-file=arm-linux.cache
    make && make install
 
    cd -
}
 
function compile_cJson()
{
    PREFIX_PATH=${INST_PATH}
    SRC_NAME=cJSON-1.7.14
    PACK_SUFIX=tar.gz
    IMG_NAME=libcjson.so
 
    if [ -f ${PREFIX_PATH}/lib/${IMG_NAME} ] ; then
        msg_banner "$SRC_NAME already compile and installed"
        return 0;
    fi
 
    msg_banner "Start compile $SRC_NAME "
 
    if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then
        # official site: https://github.com/DaveGamble/cJSON
        wget $LYFTP_SRC/$SRC_NAME.$PACK_SUFIX 
        check_result "ERROR: download ${SRC_NAME} failure"
    fi
 
 
    tar -xzf ${SRC_NAME}.${PACK_SUFIX}
 
    cd ${SRC_NAME}
 
        #sed -i -e "s|^CC =.*|CC = ${CC} -std=c89|g" Makefile
 
    make && make PREFIX=$PREFIX_PATH install
    cd -
}
 
 
function compile_mosquitto()
{
    PREFIX_PATH=${INST_PATH}
    SRC_NAME=mosquitto-2.0.10
    PACK_SUFIX=tar.gz
    IMG_NAME=libmosquitto.so
 
    if [ -f ${PREFIX_PATH}/lib/${IMG_NAME} ] ; then
        msg_banner "$SRC_NAME already compile and installed"
        return 0;
    fi
 
    # mosquitto depends on openssl 
    if [ ! -n "${CROSSTOOL}" ] ; then
       msg_banner "Using apt install openssl library now..."
       sudo apt install -y libssl-dev
    fi
 
 
    msg_banner "Start compile $SRC_NAME "
    if [ ! -f ${SRC_NAME}.${PACK_SUFIX} ] ; then
        wget $LYFTP_SRC/$SRC_NAME.$PACK_SUFIX 
        #wget https://mosquitto.org/files/source/$SRC_NAME.$PACK_SUFIX 
        check_result "ERROR: download ${SRC_NAME} failure"
    fi
 
 
    tar -xzf ${SRC_NAME}.${PACK_SUFIX}
 
    cd ${SRC_NAME}
 
    export CFLAGS=-I${PREFIX_PATH}/include
    export LDFLAGS=-L${PREFIX_PATH}/lib
 
    make  LDFLAGS=${LDFLAGS} && make DESTDIR=${PREFIX_PATH} prefix=/ install
 
    cd -
}
 
 
if [[ $# == 1 ]] && [[ $1 == clean ]] ; then
    rm -rf install
    rm -rf libgpiod-1.6.3
    rm -rf cJSON-1.7.14
    rm -rf mosquitto-2.0.10
    exit 0;
fi
 
 
if [ -n "${CROSSTOOL}" ] ; then
    export_cross
fi
 
compile_libgpiod
 
compile_cJson
 
compile_mosquitto