improve of cmpl.
[bush.git] / tools / build-srcpkg-bak-20240503 / shlib / toolchain.shlib
blobf45c417a250ae5d6a3d34d01092d97541b36a470
1 #!/bin/bash
2 ############################################################
3 # source: toolchain.shlib
4 # author: devenkong(18151155@qq.com)
5 # date: 2023-02-15
6 ############################################################
7 # note:
8 # toolchain cmd set. it uses fname.shlib to get output
9 # dirs, and ../platform/toolchain_info.imi for cmd name with
10 # cross compile prefix. before invoke func in this file, load
11 # parameters defined in dest/dest-xxx/* by param-load.shlib.
12 ############################################################
15 # todo:
16 # @ o2drv
17 # @ fhex2fbin/fbin2fhex
21 # features:
22 # @ absolution of toolchain cmd & op.
23 # !# cmd: gcc/ld/ar ...
24 # !# compile, asm2o(), c2o(), cpp2o(),
25 # !# link, o2exe(), o2a(), o2dlib(), o2drv()
26 # !# postage-proc, progstrip()
27 # !# postage-info, prog_size_info(), prog_symbols(), prog_func_symbs(),
28 # prog_var_symbs(), prog_inner_symbs(), prog_ext_symbs(),
29 # # postage-output, exe2bin(), exe2hex(), hex2bin(), bin2hex(),
30 # fhex2bin(), fbin2hex(),
31 # # general log output info, dbg output info.
32 # !@ compile hdr file dep list.
33 # !@ cross(untested)
34 # !@ different cpu arch optimzation opt.(untested)
35 # @ *opt check.
36 # @ gcc -f opt.
37 # @ INVOKE type
38 # @ prev & post invoke
42 # . incfile.shlib
45 # this func copied from incfile.shlib. due to the file is not in a fixed dir,
46 # it can'nt be used by source operation.
48 if [[ -z $(declare -F inc) ]]; then
49 inc ()
51 local pwd=$PWD
53 [[ -z "$1" || ! -f "$1" ]] && return
55 cd $(dirname $1)
56 source $(basename $1)
57 cd $pwd
63 # file name format define
64 # before using,
66 inc fname.shlib
70 ######################
71 # section: common comment info
72 ######################
74 ######################
75 # section: variable define
76 ######################
80 ######################
81 # section: private function
82 ######################
84 toolchain_dbgout ()
86 test "${OPT_VERBOSE_INFO}" == 'Y' && echo "$@"
87 return;
88 echo "$@"
93 ######################
94 # section: public function
95 ######################
97 #########################################
98 # Compile Function
99 #########################################
101 # OPT_OBJSIZE_INFO=Y
102 # OPT_VERBOSE_INFO=Y
103 # OPT_DEPHDR=Y
104 # OPT_CALL_GRAPH=Y
106 cmd_run ()
108 local cmd="$1"
109 local output=
110 local ret=
111 local line=
113 # attention -lncurses paramter must be put after .o file,
114 # or it report err that symbol not found.
115 # cmd="${CC} ${ASFLAGS_OUT} ${ASFLAGS_EXT} ${CFLAGS} ${LDFLAGS} $(echo ${SRC_FILE[@]}) -o ${DST_FILE[0]}"
118 # XXX: if there is blank in quoted "string", it will be shorten to single.
119 # append '\' before it.
121 cmd="$(echo "${cmd//[[:space:]]/ }" | tr -s ' ')"
123 # release var after cmd is filled.
124 unset SRC_FILE DST_FILE
127 # TBD: do parameter checking work here.
131 # build operation
132 # NOTE:
133 # if stdout is redirected by '|' pipe,
134 # it will not display in colorfull.
135 # the commented statement does not display in colorfull.
137 # output="$( eval $cmd 2>&1 )"
138 eval $cmd 2>&1 > /tmp/output.log
139 ret=$?
141 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build-dbgout.log
142 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build.log
144 if test $ret = 0 || ! test -s /tmp/output.log && test "${verbose_mode,,}" != y ; then
145 toolchain_dbgout -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
146 else
147 echo -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
148 cat /tmp/output.log
149 test "${OPT_VERBOSE_INFO}" == 'Y' && cat /tmp/output.log >> ${OUTDIR}/build-dbgout.log
152 if test $ret != 0 ; then
153 return 2
154 else
155 return 0
160 # fsyntax: asm2o <src-file> <output-file>
161 # fdesc: asm汇编程序.S文件编译成.o文件。
163 asm2o ()
165 local cmd=
167 [[ -n "${1}" ]] && SRC_FILE[0]="$1"
168 [[ -n "${2}" ]] && DST_FILE[1]="$2"
170 eval cmd=\"$ASM2O_CMD_FMT\"
171 cmd_run "$cmd"
172 cmd=$?
174 [[ $cmd == 0 ]] && CMPL_OBJ_LIST+="$fdst"
175 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
177 return $cmd
181 # fsyntax: src2dst_filename
182 # fdesc: compile each src file.
184 src2dst_filename ()
186 [[ -z "${SRC_FILE[0]}" ]] && echo -e "[error]: SRC_FILE[0] should not be NULL, and '\n$(declare -p DST_FILE)'.\n" >&2 && exit
188 SRC_FILE="${SRC_FILE//\,/\ }"
190 [[ -z "${DST_FILE[0]}" ]] && fname_src2dst $SRC_FILE
191 [[ -z "${DST_FILE[1]}" ]] && fname_src2obj $SRC_FILE
193 [[ OPT_CALLGRAPH =~ '' ]] &&
195 [[ -z "${DST_FILE[2]}" ]] && fname_src2dep $SRC_FILE
196 [[ -z "${DST_FILE[3]}" ]] && fname_src2graph $SRC_FILE
200 # set those two by cmd opt or envar or DEBUG/RELEASE opt.
202 # OPT_DEPHDR
203 # OPT_CALLGRAPH
206 # fsyntax: c2o <src-file> [ <obj-file> [ <dep-file> [<graph-file>] ] ]
207 # fdesc: .c src file compile to .o file. parameters can be given in func arg,
208 # and envar SRC_FILE & DST_FILE. you can given one param <obj-file> only,
209 # it will generate others in auto.
211 c2o ()
213 local cmd=
214 local fdst=
216 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[c]}$ ]] && SRC_FILE[0]="$1"
217 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
218 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
219 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
220 src2dst_filename
222 fdst="${DST_FILE[1]} "
224 mkdir -p $(dirname ${DST_FILE[1]})
226 # echo \$1=${1}
227 # echo \$2=${2}
228 # echo SRC_FILE=${SRC_FILE[@]}
229 # echo DST_FILE=${DST_FILE[@]}
231 CFLAGS_DEPHDR_Y=""
232 CFLAGS_CALLGRAPH_Y=""
233 if test -n "${DST_FILE[2]}" && test "${OPT_DEPHDR^^}" == 'Y'; then
234 eval "CFLAGS_DEPHDR_${OPT_DEPHDR^^}=\"$CFLAGS_DEPHDR_Y_EVL\""
236 if test -n "${DST_FILE[3]}" && test "${OPT_CALL_GRAPH^^}" == 'Y'; then
237 eval "CFLAGS_CALLGRAPH_${OPT_CALLGRAPH^^}=\"$CFLAGS_CALLGRAPH_Y_EVL\""
240 eval cmd=\"$C2O_CMD_FMT\"
241 cmd_run "$cmd"
242 cmd=$?
244 [[ $cmd == 0 ]] && CMPL_OBJ_LIST+="$fdst"
245 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
247 return $cmd
251 # fsyntax: cxx2o
252 # fdesc: .cc文件编译成.o文件。
253 cxx2o ()
255 local cmd=
256 local fdst=
258 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[cxx]}$ ]] && SRC_FILE[0]="$1"
259 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
260 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
261 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
262 src2dst_filename
263 fdst="${DST_FILE[1]} "
265 mkdir -p $(dirname ${DST_FILE[0]})
267 CFLAGS_DEPHDR_Y=""
268 CFLAGS_CALLGRAPH_Y=""
269 if test -n "${DST_FILE[2]}" && test ${OPT_DEPHDR^^} == 'Y'; then
270 eval "CFLAGS_DEPHDR_${OPT_DEPHDR}=\"$CFLAGS_DEPHDR_Y_EVL\""
272 if test -n "${DST_FILE[3]}" && test ${OPT_CALL_GRAPH^^} == 'Y'; then
273 eval "CFLAGS_CALLGRAPH_${OPT_CALLGRAPH}=\"$CFLAGS_CALLGRAPH_Y_EVL\""
276 eval cmd=\"$CXX2O_CMD_FMT\"
277 cmd_run "$cmd"
278 cmd=$?
280 [[ $cmd == 0 ]] && CMPL_OBJ_LIST+="$fdst"
281 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
283 return $cmd
287 # fsyntax: cpp2o
288 # fdesc: cpp程序.cpp文件编译成.o文件。
290 cpp2o ()
292 local cmd=
293 local fdst=
295 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[cpp]}$ ]] && SRC_FILE[0]="$1"
296 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
297 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
298 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
299 src2dst_filename
300 fdst="${DST_FILE[1]} "
302 mkdir -p $(dirname ${DST_FILE[0]})
304 CFLAGS_DEPHDR_Y=""
305 CFLAGS_CALLGRAPH_Y=""
306 if test -n "${DST_FILE[2]}" && test ${OPT_DEPHDR^^} == 'Y'; then
307 eval "CFLAGS_DEPHDR_${OPT_DEPHDR:+Y}=\"$CFLAGS_DEPHDR_Y_EVL\""
309 if test -n "${DST_FILE[3]}" && test ${OPT_CALL_GRAPH^^} == 'Y'; then
310 eval "CFLAGS_CALLGRAPH_${OPT_CALLGRAPH:+Y}=\"$CFLAGS_CALLGRAPH_Y_EVL\""
313 eval cmd=\"$CPP2O_CMD_FMT\"
314 cmd_run "$cmd"
315 cmd=$?
317 [[ $cmd == 0 ]] && CMPL_OBJ_LIST+="$fdst"
318 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
320 return $cmd
325 # fsyntax: asm2exe
326 # fdesc: .asm文件编译成exe文件。
327 asm2exe ()
329 local cmd=
331 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[asm]}$ ]] && SRC_FILE[0]="$1"
332 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
333 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
334 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
335 src2dst_filename
337 mkdir -p $(dirname ${DST_FILE[0]})
339 eval cmd=\"$ASM2EXE_CMD_FMT\"
340 cmd_run "$cmd"
341 cmd=$?
343 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
345 return $cmd
349 # fsyntax: c2exe
350 # fdesc: c语言程序.c文件编译成.o文件。
351 c2exe ()
353 local cmd=
355 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[c]}$ ]] && SRC_FILE[0]="$1"
356 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
357 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
358 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
359 src2dst_filename
361 mkdir -p $(dirname ${DST_FILE[0]})
363 eval cmd=\"$C2EXE_CMD_FMT\"
364 cmd_run "$cmd"
365 cmd=$?
367 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
369 return $cmd
373 # fsyntax: cxx2exe
374 # fdesc: cpp程序.cpp文件编译成.o文件。
376 cxx2exe ()
378 local cmd=
380 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[cxx]}$ ]] && SRC_FILE[0]="$1"
381 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
382 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
383 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
384 src2dst_filename
386 mkdir -p $(dirname ${DST_FILE[0]})
388 eval cmd=\"$CXX2EXE_CMD_FMT\"
389 cmd_run "$cmd"
390 cmd=$?
392 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
394 return $cmd
398 # fsyntax: cpp2exe
399 # fdesc: cpp程序.cpp文件编译成.o文件。
401 cpp2exe ()
403 local cmd=
405 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[cpp]}$ ]] && SRC_FILE[0]="$1"
406 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && DST_FILE[1]="$2"
407 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
408 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
409 src2dst_filename
411 mkdir -p $(dirname ${DST_FILE[0]})
413 eval cmd=\"$CPP2EXE_CMD_FMT\"
414 cmd_run "$cmd"
415 cmd=$?
417 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
419 return $cmd
423 #########################################
424 # link function
425 #########################################
428 # fsyntax: o2o
429 # fdesc: .o文件链接成.o文件。
431 o2o ()
433 local cmd=
435 [[ -n "${1}" ]] && SRC_FILE[0]="$1"
436 [[ -n "${2}" ]] && DST_FILE[0]="$2"
437 [[ -n "${3}" ]] && DST_FILE[2]="$3"
438 [[ -n "${4}" ]] && DST_FILE[3]="$4"
439 src2dst_filename
441 mkdir -p $(dirname ${DST_FILE[0]})
443 eval cmd=\"$O2O_CMD_FMT\"
444 cmd_run "$cmd"
445 cmd=$?
447 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
449 return $cmd
453 # fsyntax: link_filename_fix
454 # fdesc: compile each src file.
456 link_filename_fix ()
458 local ext=
459 local dst=
461 # eval "ext=\"\${LANG_EXT_NAME[${lang,,}]}\""
462 #"${SRC_FILE[0]##*.}"
464 # obj="${DSTDIR}/${obj//\.$ext/\.o}"
465 # DST_FILE[0]="${DSTDIR}/${DST_FILE[0]}"
467 fname_objdir
468 for ((i=0; i<${#SRC_FILE[@]}; i++)); do
469 if [[ ! "${SRC_FILE[$i]}" =~ ${OBJDIR} ]]; then
470 SRC_FILE[$i]="${OBJDIR}/${SRC_FILE[$i]}"
472 done
474 # DST_FILE[0]="${OUTDIR}/${DST_FILE[0]}"
478 # fsyntax: o2lib
479 # fdesc: .o files link to static library.
481 o2lib ()
483 local cmd=
484 local output=
485 local ret=
486 local file=
488 [[ -n "${1}" && "${!2}" =~ ${EXT_NAME[obj]}$ ]] && SRC_FILE[0]="$1"
489 [[ -n "${2}" && "${!2}" =~ ${EXT_NAME[lib]}$ ]] && DST_FILE[1]="$2"
490 [[ -n "${3}" && "${!2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
491 [[ -n "${4}" && "${!2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
492 link_filename_fix
494 # echo \$2=$2=${!2}
495 # echo DST_FILE=$DST_FILE
497 file=${DST_FILE}
498 mkdir -p $(dirname ${DST_FILE[0]})
500 eval cmd=\"$O2LIB_CMD_FMT\"
501 cmd_run "$cmd"
502 [[ $? != 0 ]] && return $?
504 ranlib $file
505 cmd=$?
507 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
509 return $cmd
513 # fsyntax: o2la
514 # fdesc: .o files link to general static library.
516 o2la ()
518 # TBD:
519 # if test $ret != 0 ; then
520 return 2
521 # else
522 # return 0
523 # fi
527 # fsyntax: o2dll
528 # fdesc: .o files link to dynamic library.
530 o2dll ()
532 local cmd=
534 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && SRC_FILE[0]="$1"
535 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[dll]}$ ]] && DST_FILE[1]="$2"
536 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
537 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
538 link_filename_fix
540 mkdir -p $(dirname ${DST_FILE[0]})
542 eval cmd=\"$O2DLL_CMD_FMT\"
543 cmd_run "$cmd"
544 cmd=$?
546 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
548 return $cmd
552 # fsyntax: o2libdll
553 # fdesc: .o files link to static & dynamic library.
555 o2libdll ()
557 local cmd=
558 local output=
559 local ret=
560 local file=
561 local srcfile=
563 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && SRC_FILE[0]="$1"
564 [[ -n "${2}" && "${2}" =~ (${EXT_NAME[lib]}|${EXT_NAME[dll]})$ ]] && DST_FILE[1]="$2"
565 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
566 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
567 link_filename_fix
569 mkdir -p $(dirname ${DST_FILE[0]})
571 file=$DST_FILE
572 srcfile=( ${SRC_FILE[@]} )
573 DST_FILE="${DST_FILE%\.*}.a"
575 eval cmd=\"$O2LIB_CMD_FMT\"
576 cmd_run "$cmd"
577 [[ $? != 0 ]] && return $?
579 ${RANLIB} ${file}
580 [[ $? != 0 ]] && return $?
582 SRC_FILE=( ${srcfile[@]} )
583 DST_FILE="${file%\.*}.so"
584 eval cmd=\"$O2DLL_CMD_FMT\"
585 cmd_run "$cmd"
586 cmd=$?
588 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
590 return $cmd
594 # fsyntax: o2exe
595 # fdesc: .o files link to executables.
597 o2exe ()
599 local cmd=
601 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && SRC_FILE[0]="$1"
602 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[exe]}$ ]] && DST_FILE[1]="$2"
603 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
604 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
605 link_filename_fix
607 mkdir -p $(dirname ${DST_FILE[0]})
609 eval cmd=\"$O2EXE_CMD_FMT\"
610 cmd_run "$cmd"
611 cmd=$?
613 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
615 return $cmd
619 # fsyntax: o2drv
620 # fdesc: .o文件链接成elf格式linux的drv文件。
622 o2drv ()
624 local cmd=
626 [[ -n "${1}" && "${2}" =~ ${EXT_NAME[obj]}$ ]] && SRC_FILE[0]="$1"
627 [[ -n "${2}" && "${2}" =~ ${EXT_NAME[drv]}$ ]] && DST_FILE[1]="$2"
628 [[ -n "${3}" && "${2}" =~ \.dephdr$ ]] && DST_FILE[2]="$3"
629 [[ -n "${4}" && "${2}" =~ \.cgraph$ ]] && DST_FILE[3]="$4"
630 link_filename_fix
632 mkdir -p $(dirname ${DST_FILE[0]})
634 eval cmd=\"$O2DRV_CMD_FMT\"
635 cmd_run "$cmd"
636 cmd=$?
638 test "${OPT_OBJSIZE_INFO^^}" == 'Y' && eval ${SIZE} ${DST_FILE[1]}
640 return $cmd
643 #########################################
644 # build postage processing & info.
645 # size of .text/.data/.rodata/.bss.
646 # internal symbol of an executable file.
647 # export symbol of an executable file.
650 # nm -D <file>, dynamic symbol. U, imported; T, exported.
651 # nm -g <file>, global symbol, all symbol.
653 # objdump -t <file> | grep -e "\.text", native code symbol.
654 # objdump -t <file> | grep -e "\.data", initialized var symbol.
655 # objdump -t <file> | grep -e "\.rodata", read only var symbol.
656 # objdump -t <file> | grep -e "\.bss", uninitialized var symbol.
658 # size <file>, it display size info in .text/.data(.rodata)/.bss
659 #########################################
662 # fsyntax: progstrip
663 # fdesc: delete debug symbol info .etc .
665 progstrip ()
667 local cmd=
668 local output=
669 local ret=
671 # attention -lncurses paramter must be put after .o file,
672 # or it report err that symbol not found.
673 cmd="${STRIP} $(echo ${DST_FILE[@]})"
675 toolchain_dbgout -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
677 # build operation
678 output="$( eval $cmd 2>&1; )"
679 ret=$?
681 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build-dbgout.log
682 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build.log
684 if test $ret == 0 || test -z "$output" ; then
685 toolchain_dbgout -ne " === ${cmd}\n"
686 else
687 toolchain_dbgout -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
688 toolchain_dbgout "$output"
689 echo "$output" >> ${OUTDIR}/build-dbgout.log
692 if test $ret != 0 ; then
693 return 2
694 else
695 return 0
700 # fsyntax: prog_sizeinfo
701 # fdesc: size of .text/.data/.rodata/.bss.
703 prog_sizeinfo ()
705 local cmd=
706 local output=
707 local ret=
708 local file=
709 local filelist=
711 DST_FILE="${OUTDIR}/${!3}"
713 fname_dstdir
714 # for file in ${SRC_FILE[@]}; do
715 # if test -f "${DSTDIR}/${file}" ; then
716 # filelist+="${DSTDIR}/${file} "
717 # fi
718 # done
719 # filelist+="${DST_FILE}"
721 # attention -lncurses paramter must be put after .o file,
722 # or it report err that symbol not found.
723 cmd="${SIZE} $(basename ${DST_FILE})"
725 toolchain_dbgout -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
727 # build operation
728 cd $(dirname ${DST_FILE})
729 output="$( eval ${cmd} 2>&1; )"
730 ret=$?
731 cd - 2>&1 >/dev/null
733 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build-dbgout.log
734 echo -ne " === ${cmd}\n" >> ${OUTDIR}/build.log
736 if test $ret == 0 || test -z "$output" ; then
737 toolchain_dbgout -ne " === ${cmd}\n"
738 infoo "$output"
739 else
740 toolchain_dbgout -ne "${CHIGHL} === ${cmd}${CNORMAL}\n"
741 infoo "$output"
742 echo "$output" >> ${OUTDIR}/build-dbgout.log
745 if test $ret != 0 ; then
746 return 2
747 else
748 return 0
753 # fsyntax: prog_symboals
754 # fdesc: output .text/.data/.rodata/.bss symbols in executables.
756 prog_symboals ()
758 local ret=
759 local file=
761 while read file; do
762 toolchain_dbgout "[$file]"
763 { ${OBJDUMP} -t $file; ret=$?; } | grep -o -e "\*UND\*.*" -e "\.bss.*" -e "\.rodata.*" -e "\.data.*" -e "\.text.*" | grep -v $'[ |\t]'"\..*" | grep -v \"00000000\" | sort | sed -e "s/\.text/func-i/g; s/\*UND\*/func-e/g; s///g; s/\.data/var-i/g; s/\.bss/var/g; s/\.rodata/var-e/g;"
764 [[ $ret != 0 ]] && break;
765 done <<< "${SRC_FILE[@]}"
767 if test $ret != 0 ; then
768 return 2
769 else
770 return 0
774 SYMB_FILTER_SYS='y'
777 # fsyntax: prog_inner_symbs
778 # fdesc: output internal symbols in executables.
780 prog_inner_symbs ()
782 local ret=
783 local file=
784 local param=
786 [[ $SYMB_FILTER_SYS == 'y' ]] && param="-v \"00000000\""
788 while read file; do
789 toolchain_dbgout "[$file]"
790 { ${OBJDUMP} -t $file; ret=$?; } | grep -o -e "\.text.*"| grep -v $'[ |\t]'"\..*" | sort | sed -e "s/\.text/func-i/g;"
791 done <<< "${SRC_FILE[@]}"
793 if test $ret != 0 ; then
794 return 2
795 else
796 return 0
801 # fsyntax: prog_ext_symbs
802 # fdesc: output external symbols in executables.
804 prog_ext_symbs ()
806 local ret=
807 local file=
809 while read file; do
810 toolchain_dbgout "[$file]"
811 { ${OBJDUMP} -t $file; ret=$?; } | grep -o -e "\*UND\*.*"| grep -v $'[ |\t]'"\..*" | grep -v \"00000000\" | sort | sed -e "s/\*UND\*/func-e/g; s///g;"
812 done <<< "${SRC_FILE[@]}"
814 if test $ret != 0 ; then
815 return 2
816 else
817 return 0
822 # fsyntax: prog_func_symbs
823 # fdesc: output function symbols in executables.
825 prog_func_symbs ()
827 local ret=
828 local file=
830 while read file; do
831 toolchain_dbgout "[$file]"
832 { ${OBJDUMP} -t $file; ret=$?; } | grep -o -e "\*UND\*.*" -e "\.text.*" | grep -v $'[ |\t]'"\..*" | sort | sed -e "s/\.text/func-i/g; s/\*UND\*/func-e/g; s///g;"
833 done <<< "${SRC_FILE[@]}"
835 if test $ret != 0 ; then
836 return 2
837 else
838 return 0
843 # fsyntax: prog_func_symbs
844 # fdesc: output function symbols in executables.
846 prog_var_symbs ()
848 local ret=
849 local file=
851 while read file; do
852 toolchain_dbgout "[$file]"
853 { ${OBJDUMP} -t $file; ret=$?; } | grep -o -e "\.bss.*" -e "\.rodata.*" -e "\.data.*" | grep -v $'[ |\t]'"\..*" | grep -v \"00000000\" | sort | sed -e "s/\.text/func-i/g; s/\*UND\*/func-e/g; s///g;"
854 done <<< "${SRC_FILE[@]}"
856 if test $ret != 0 ; then
857 return 2
858 else
859 return 0
864 #########################################
865 # bin/hex output and transform.
866 #########################################
870 # fsyntax: exe2hex
871 # fdesc: bin二进制文件转换成hex字符串。
873 exe2hex ()
875 exe2bin "$@"
876 bin2hex "$@"
880 # fsyntax: exe2bin
881 # fdesc: elf文件读取code/data的section,保存成bin文件。
882 # 通常用于非操作系统环境下运行的程序。
884 exe2bin ()
886 local ret=
888 { objcopy -t $file | grep -o -e "\.bss.*" -e "\.rodata.*" -e "\.data.*"; ret=$?; } | grep -v $'[ |\t]'"\..*" | grep -v \"00000000\" | sort | sed -e "s/\.text/func-i/g; s/\*UND\*/func-e/g; s///g;"
890 if test $ret != 0 ; then
891 return 2
892 else
893 return 0
898 # fsyntax: bin2hex
899 # fdesc: bin二进制文件转换成hex字符串。
901 bin2hex ()
903 # hexdump -e '/1 "%02x " '
904 xxd -ps -c 16 - 2>/dev/null
908 # fsyntax: hex2bin
909 # fdesc: hex字符串转换成bin二进制数据文件。
911 hex2bin ()
913 xxd -r - $2
917 # fsyntax: fbin2fhex <srcfile> <dst>
918 # fdesc: bin二进制文件转换成hex文件。
920 fbin2fhex ()
922 xxd -ps -c 16 $1 2>/dev/null > $2
926 # fsyntax: fhex2fbin
927 # fdesc: hex文件转换成bin二进制数据文件。
929 fhex2fbin ()
931 xxd -r $1 > $1
935 # or use ':'
937 nullcmd ()
939 echo "[error]: cmd not defined. look at the defination in toolchain/${TOOLCHAIN}.imi, or use another toolchain." >&2
940 exit -1
943 # syntax: without_pfx <pfx>
944 without_pfx ()
946 local name=
947 eval local varlist="\${!${1}*}"
949 for var in ${varlist}; do
950 eval name="\${var#$1}"
951 eval $name=${!var}
952 done
956 # fdesc: get
958 get_toolchain_info ()
960 echo 'int main(){}' > /tmp/dummy.c
962 verbose_mode=y
964 # test the parameters setting in files.
965 # use c2exe() instead of directly invoke, it can test for some 'GCC' cross-compiler.
967 # gcc /tmp/dummy.c -I. -L/tmp/lib -Wl,-rpath,/tmp/lib -rdynamic -lpthread -v -Wl,--verbose &> /tmp/dummy.log
968 LDFLAGS_EXT="-v -Wl,--verbose -lpthread" c2exe /tmp/dummy.c /tmp/dummy > /tmp/dummy.log
969 unset verbose_mode
972 # get include paths for '#include <...>'
974 # echo "===== get include path ====="
975 INC_PATHS=( `grep -B100 '^End of search list.' /tmp/dummy.log | grep -A100 '^#include <...>' | grep -v 'End of search list.' | grep -v '#include <...>'` )
976 # echo INC_PATHS:
977 # echo ${INC_PATHS[@]}
979 # /usr/lib/gcc/i686-linux-gnu/5/include
980 # /usr/local/include
981 # /usr/lib/gcc/i686-linux-gnu/5/include-fixed
982 # /usr/include/i386-linux-gnu
983 # /usr/include
986 # get lib paths in linking
988 # echo "===== get lib path ====="
989 LIB_PATHS=( `grep 'SEARCH.*/usr/lib' /tmp/dummy.log |sed 's|; |\n|g' | sed 's|SEARCH_DIR("=||g' | sed 's|")||g' | sed 's|;||g'` )
990 # echo LIB_PATHS:
991 # echo ${LIB_PATHS[@]}
992 #/usr/local/lib/i386-linux-gnu
993 #/lib/i386-linux-gnu
994 #/usr/lib/i386-linux-gnu
995 #/usr/local/lib32
996 #/lib32
997 #/usr/lib32
998 #/usr/local/lib
999 #/lib
1000 #/usr/lib
1001 #/usr/i686-linux-gnu/lib32
1002 #/usr/i686-linux-gnu/lib
1005 # get lib libpthread linked in.
1007 # echo "===== get libs ====="
1008 LINKED_LIBS=( `grep -e 'succeeded' /tmp/dummy.log | grep -v '\.o[[:blank:]]*' | sed 's|attempt to open ||g' | sed 's| succeeded||g' | xargs file -L 2>/dev/null| grep -E 'ELF|archive' | sed 's|:.*$||g'` )
1009 # echo LINKED_LIBS:
1010 # echo ${LINKED_LIBS[@]}
1011 #/lib/i386-linux-gnu/libpthread.so.0
1012 #/usr/lib/i386-linux-gnu/libpthread_nonshared.a
1014 rm /tmp/dummy.c /tmp/dummy /tmp/dummy.log
1017 toolchain_init ()
1019 #loadimi ../platform/param.imi
1022 # toolchain config info & general define info.
1023 # TBD: config info should be get from cmd param or envar.
1025 loadimi ${SRCPKG_DIR_FULL}/build/toolchain-cfg.imi
1027 echo =========================== MACHTYPE=$MACHTYPE
1029 loadimi ${SRCPKG_DIR_FULL}/tools/build-srcpkg/platform/toolchain_info.imi
1031 echo =========================== MACHTYPE=$MACHTYPE
1033 # other build utilities.
1034 #loadimi ../toolset/toolset.imi
1037 toolchain_default_param_load ()
1039 loadimi tools/build-srcpkg/info/paramters.imi
1044 ######################
1045 # section: file tail
1046 ######################
1048 return 0
1052 # fsyntax: toolchain_chk
1053 # fdesc: check toolchain if it can works in usual. save flags in
1054 # build-srcpkg config file.
1056 toolchain_chk ()
1058 # TBD:
1063 # fsyntax: dev_env_chk
1064 # fdesc: check commands and build env.
1066 dev_env_chk ()
1068 # TBD:
1073 # fsyntax: dep_lib_chk
1074 # fdesc: check external lib file if it exist and can works.
1075 # save checking resualt into build-srcpkg config file.
1077 dep_lib_chk ()
1079 # TBD:
1084 # fsyntax: dep_syshdr_chk
1085 # fdesc: check system .h file if it exist.
1087 dep_syshdr_chk ()
1089 # TBD: