first version.
[build-srcpkg.git] / res / builder / build-srcpkg / cmplib-bak.shlib
blobe4fd5fdc6bacdcb260c4147f84526f973184d102
3 # features:
4 # @ src list compile
5 # @ gen src list from a subdir, and compile
6 # @ compile a subdir by invoke build in subdir.
7 # @ compile & link for a target in target list.
9 # @ EXT_OBJ_LIST
13 # default param
15 CC="gcc"
16 LINK="gcc"
17 AR="ar"
18 O2LIB_CMD_EVL='${AR} ${ARFLAGS} ${outputfile}.a $(echo ${OBJ_LIST})'
19 ARFLAGS=" -vcsr"
20 DST_FMT='${dst}'
21 OUTDIR_FMT='${PWD}/output/'
22 [[ -z $OUTDIR ]] && export OUTDIR="$PWD/output/" && echo XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  # it must have a '/' at last.
23 OBJDIR_FMT='${OUTDIR}obj/'
24 OBJDIR="${OUTDIR}obj/"
25 BAKDIR='../'
27 scriptversion=v0.1.0-231025
29 version="cmpl.sh $scriptversion
31 Copyright 2020 Free Software Foundation, Inc.
32 There is NO warranty.  You may redistribute this software
33 under the terms of the GNU General Public License.
34 For more information about these matters, see the files named COPYING."
36 usage="Usage: $prog [OPTION]
38 It's a file of compile.
39   https://repo.or.cz/devutils.git
41 Options:
42   -f           force to re-compile again even if code is compiled before.
43   -v           version info.
44   -h           help info.
46 sub-cmd:
47         clean      clean output files.
48         bak        clean and backup a tarball in the upper dir.
49         
50   --help       display help info in detail.
51   --version    display version information in detail.
53 Simple example:
54 $ $prog -f
55 $ $prog
56 $ $prog clean
57 $ $prog bak
59 Email bug reports or enhancement requests to skeletone@126.com .
62 cmd_opt()
64         while test $# -gt 0; do
65           case $1 in
66                         -f)          force=1;;
67                         -v)          shift; outdir=$1;;
68                         -h)          shift; dirargs="$dirargs -I '$1'"; dirs="$dirs $1";;
69                         clean)       echo cleaning ...; flag_clean=1;;
70                         bak)
71                                 $0 clean
72                                 dir="$(basename $PWD)"
73                                 file="${dir}-$(date +%Y%m%d).zip"
74                                 cd ..
75                                 rm ${file}
76                                 zip -r ${file} ${dir}
77                                 cd - 2>/dev/null
78                                 [[ $BAKDIR != '../' && $BAKDIR != '..' ]] && mkdir -pv $BAKDIR && cp ../$file ${BAKDIR}/ -f
79                                 exit 0
80                         ;;
81                         gz)         echo TBD; exit 0;;
82                         bz)         echo TBD; exit 0;;
83                         xz)         echo TBD; exit 0;;
84                         
85                         -v)          echo "version: $scriptversion"; exit 0;;
86                         -h)          echo "$usage"; exit 0;;
87                         
88                         --version)   echo "$version"; exit 0;;
89                         --help)      echo "$usage"; exit 0;;
90                         -*)
91                                 echo "$0: Unknown option \`$1'." >&2
92                                 echo "$0: Try \`--help' for more information." >&2
93                                 exit 1;;
94                         *)
95                                 if test -z "$PACKAGE"; then
96                                         PACKAGE=$1
97                                 elif test -z "$MANUAL_TITLE"; then
98                                         MANUAL_TITLE=$1
99                                 else
100                                         echo "$0: extra non-option argument \`$1'." >&2
101                                         exit 1
102                                 fi;;
103                 esac
104                 shift
105         done
108 build_init()
110         [[ ! -z $@ ]] && force=1
112         OBJ_LIST=""
113         OBJDIR="$(eval echo ${OBJDIR_FMT})"
114         mkdir -pv $OUTDIR $OBJDIR
117 srclist ()
119         SRC_LIST="$(
120                 echo
121                 while read line; do
122                         if [[ -n $line ]]; then
123                                 echo ${line}.c
124                                 [[ -f "${line}_test.c" ]] && echo ${line}_test.c
125                         fi
126                 done <<< "$DST_LIST"
127                 echo
128         )"
131 dirbuild ()
133         [[ -z $SUB_SRCDIR_LIST ]] && return
135         for subdir in $SUB_SRCDIR_LIST; do
136                 cd $subdir
137                 [[ ! -f "cmpl.sh" ]] && echo "err: there no cmpl.sh file exist in subdir '$subdir'." && exit -1
139                 # running in subscript
140                 ./cmpl.sh "$@"
141                 [[ $? != 0 ]] && echo "err: builld dir $subdir failed." && exit -1
142                 
143                 cd - 2>/dev/null
144                 SLIB_LIST+=" ${OUTDIR}lib${subdir##*/}.a"
145         done
148 compile ()
150         OBJDIR="$(eval echo ${OBJDIR_FMT})"
151         
152         for srcfile in $SRC_LIST; do
153                 objfile=${srcfile##*/}
154                 objfile="${OBJDIR}${objfile//\.c/\.o}"
155                 echo $srcfile gcc compile to $objfile ...
156                 
157                 if [[ $srcfile -nt $objfile
158 #                       || cmpl.sh -nt $srcfile
159                         || $force == 1
160                         || ! -f $objfile ]]; then
161         #               touch $srcfile
162                         mkdir -p $(dirname $objfile)
163                         $CC ${CFLAGS} -c $srcfile -o $objfile
164                         [[ $? != 0 ]] && echo "gcc($CC) compile ($objfile) failed." && exit -1
165                         size $objfile
166                         force=1
167                 fi
168         done
170         OBJ_LIST=""
171         for srcfile in $SRC_LIST; do
172                 objfile=${srcfile##*/}
173                 objfile="$OBJDIR${objfile//\.c/\.o}"
174                 OBJ_LIST+=" $objfile"
175         done
178 staticlib ()
180         OBJDIR="$(eval echo ${OBJDIR_FMT})"
182         if [[ "${DST_FMT}" == '${dst}' ]]; then
183                 outputfile="${DST_LIST%%[[:blank:]]}"
184                 outputfile="${OUTDIR}lib${outputfile#*[[:blank:]]}"
185                 
186                 OBJ_LIST=""
187                 flag_link=""
188                 for srcfile in $SRC_LIST; do
189                         objfile=${srcfile##*/}
190                         objfile="${OBJDIR}${objfile//\.c/\.o}"
191                         OBJ_LIST+=" $objfile"
192                         [[ -z $flag_link && "$srcfile" -nt "$outputfile" ]] && flag_link=1 \
193                                 #&& echo $srcfile '-nt' $outputfile && ls -l $srcfile $outputfile
194                         [[ -z $flag_link && "$objfile" -nt "$outputfile" ]] && flag_link=1 \
195                                 #&& echo $objfile '-nt' $outputfile && ls -l $objfile $outputfile
196                 done
197                 [[ $flag_link != 1 ]] && return
198                 
199                 echo $outputfile gcc link ...
200                 echo "########################################################################"
201                 echo "$O2LIB_CMD_EVL"
202                 eval O2LIB_CMD="\"$O2LIB_CMD_EVL\""
203                 echo "$O2LIB_CMD"
204                 eval $O2LIB_CMD
205 #               $LINK $LDFLAGS -O2 $OBJ_LIST -o $outputfile
206                 [[ $? != 0 ]] && echo "gcc link ($(echo $outputfile)) failed." && exit -1
207                 size ${outputfile}.a
208                 ls -l ${outputfile}.a
209                 
210                 return
211         fi
214 link ()
216         OBJDIR="$(eval echo ${OBJDIR_FMT})"
218         if [[ "${DST_FMT}" == '${dst}' ]]; then
219                 outputfile="${DST_LIST%%[[:blank:]]}"
220                 outputfile="${OUTDIR}${outputfile#*[[:blank:]]}"
221                 
222                 OBJ_LIST=""
223                 flag_link=""
224                 for srcfile in $SRC_LIST; do
225                         objfile=${srcfile##*/}
226                         objfile="${OBJDIR}${objfile//\.c/\.o}"
227                         OBJ_LIST+=" $objfile"
228                         [[ -z $flag_link && "$srcfile" -nt "$outputfile" ]] && flag_link=1 \
229                                 #&& echo $srcfile '-nt' $outputfile && ls -l $srcfile $outputfile
230                         [[ -z $flag_link && "$objfile" -nt "$outputfile" ]] && flag_link=1 \
231                                 #&& echo $objfile '-nt' $outputfile && ls -l $objfile $outputfile
232                 done
233                 [[ $flag_link != 1 ]] && return
234                 
235                 echo $outputfile gcc link ...
236                 echo $SLIB_LIST
237 #               echo "$LINK $LDFLAGS -O2 $SLIB_LIST $OBJ_LIST -o $outputfile"
238                 $LINK $LDFLAGS -O2 $OBJ_LIST $SLIB_LIST -o $outputfile
239                 [[ $? != 0 ]] && echo "gcc link ($(echo $outputfile)) failed." && exit -1
240                 size $outputfile
241                 ls -l $outputfile
242                 
243                 return
244         fi
245         
246         for dst in $DST_LIST; do
247                 outputfile="${OUTDIR}$(eval echo ${DST_FMT})"
248                 echo $outputfile gcc build ...
249                 
250                 objfile1="${OBJDIR}${dst}.o"
251                 objfile2="${OBJDIR}${dst}_test.o"
252                 if [[ $outputfile -nt $objfile1 || $outputfile -nt $objfile2
253                         || build.sh -nt $outputfile
254                         || $force == 1
255                         || ! -f $outputfile ]]; then
256                         echo "$LINK $LDFLAGS -O2 $SLIB_LIST $objfile1 $objfile2 -o $outputfile"
257                         $LINK $LDFLAGS -O2 $SLIB_LIST $objfile1 $objfile2 -o $outputfile
258                         [[ $? != 0 ]] && echo "gcc($CC) link ($outputfile) failed." && exit -1
259                         size $outputfile
260                 fi
261         done
264 update_param ()
266         CFLAGS="${MACRO_DEF} -O2 ${INC_PATHS} "
267         LDFLAGS="${INC_LIBS} ${MISC_LDFLAGS} "
270 build_clean ()
272         OBJDIR="$(eval echo ${OBJDIR_FMT})"
274         rm $OBJ_LIST ${OBJDIR} $OUTDIR -rf
275         
276         while read dst; do
277                 [[ -z $dst ]] && continue
278                 eval echo rm "${OUTDIR}${DST_FMT}" -f
279                 eval rm ${OUTDIR}${DST_FMT} -f
280         done <<< "$DST_LIST"