improve of cmpl.
[bush.git] / tools / build-srcpkg-bak-20240503 / bin / cmpl
blob7e7be939ae68a98e7ea35c52ef97f8d25b1b5343
1 #!/bin/bash
2 ############################################################
3 # source: cmpl
4 # author: CottonCandyOwner(CottonCandyOwner@126.com)
5 # date: 2024-02-01
6 ############################################################
7 # Copyright (C) 2022- Free Software Foundation, Inc.
8 # This configure script is free software; the Free Software
9 # Foundation gives unlimited permission to copy, distribute
10 # and modify it.
11 ############################################################
12 # note:
13 # build all things in srcpkg, just like a Makefile.
14 # but does not contain develop operations.
15 # this program only do operation in output dir, and
16 # does not modify any thing in srcpkg.
17 ############################################################
21 # todoļ¼š
22 # @ args
23 # # error in dev.
24 # @
25 # @ envchk
26 # @ deplib
27 # @ doc(man/info/um/html/pdf/md)
28 # @ genpkg & etz
29 # @ install & dirs
30 # @ debug/release build
34 # @ programs & opts & features trim
35 # # opts, txt align
36 # # on_xxx & test
37 # # memo txt.
38 # # helper txt chk(dev/cmpl)
39 # # each feature for one opt, with one test-item
43 # pkg type:
44 # @ <non>, main program & mandoc
45 # @ -dev-, dev lib for compile
46 # @ -umdoc-, umdoc
47 # @ -intl-, multi-lang
48 # @ -test-, test program
49 # @ -eg-, example(eg)
53 # enable alias feature in script.
54 shopt -s expand_aliases
57 # ATTENTION:
58 # source operation in codeblock of function or if statement,
59 # it will leading error.
61 source shlibinc 2>&1 >/dev/null
63 alias include >/dev/null 2>&1
64 if test $? = 0 ; then
65 # put include code here if needed.
67 else
72 # code below include shlib in absolute name.
74 source tools/build-srcpkg/shlib/incfile.shlib
76 loadimi tools/build-srcpkg/info/SrcPkgDirs.imi
78 inc tools/build-srcpkg/shlib/fname.shlib
79 inc tools/build-srcpkg/shlib/toolchain.shlib
80 inc tools/build-srcpkg/shlib/param-load.shlib
81 inc tools/build-srcpkg/shlib/worklist.shlib
83 #inc build/shlib/pre_build.shlib
85 inc tools/build-srcpkg/shlib/args.shlib
89 ##############################
90 # section: public comment info
91 ##############################
95 ##############################
96 # section: variable define
97 ##############################
100 # this comment is used for version id auto update.
102 ## prog-ver-begin
103 readonly CMPL_V1=0
104 readonly CMPL_V2=1
105 readonly CMPL_V3=0
106 readonly CMPL_VEXT=
107 readonly CMPL_VER_DATE=20240201
108 ## prog-ver-end
110 readonly CMPL_PROG=`basename $0`
112 readonly CMPL_PROG_VERSION="v${CMPL_V1}.${CMPL_V2}\${CMPL_V3:+\".$CMPL_V3\"}\${CMPL_VEXT:+\"$CMPL_VEXT\"}\${CMPL_VER_DATE:+\"-$CMPL_VER_DATE\"}"
113 CMPL_PROG_VERSION_STR="`eval echo $CMPL_PROG_VERSION`
114 Copyright (C) 2024- Free Software Foundation, Inc.
115 This configure script is free software; the Free Software
116 Foundation gives unlimited permission to copy, distribute
117 and modify it.
119 Writen by CottenCandyOwner(CottenCandyOwner@126.com).
122 CMPL_PROG_BANNER="$CMPL_PROG $CMPL_PROG_VERSION_STR"
123 CMPL_PROG_SYNTAX='Usage: $CMPL_PROG -[${p_shortparam//\|/}]'
124 CMPL_PROG_DESC=" build all things in srcpkg, just like a Makefile.
127 CMPL_PROG_OTHER_DESC="
128 EXAMPLES:
129 @ standard cmd compatiable with Makefile.
130 cmpl clean
131 cmpl all
132 cmpl test
133 cmpl install
135 @ seperated steps in cmpl all, and others.
136 cmpl defconf
137 cmpl default
138 cmpl example
139 cmpl doc
140 cmpl genpkg
143 CMPL_USAGE_FMT='$CMPL_PROG_BANNER
144 ${CMPL_PROG_SYNTAX}
145 $CMPL_PROG_DESC
146 ${helper}
147 $CMPL_PROG_OTHER_DESC'
151 # do not delete this comment, it is used for var define code intert.
153 ## args-var-define-begin
154 ## args-var-define-end
158 # PARAMETER DEFINE in DESCRIPTION STRING.
160 # this desc str would be referenced in script, consider about opt name space.
161 # @ desc-str can be defined more then one. and some of them maybe re-used
162 # for other program by 'source xxx --loadshlib'.
163 # @ '#' in desc-str is also supported as comment, and can be used to disable
164 # some option have not been implemented.
165 # @ =, param name for opt.
166 # @ %, var name if this opt is enabled.
167 # @ &, proc func if opt trigged.
169 cmpl_desc_hdr_str="
170 |prog $0 'build all things in srcpkg, just like a Makefile.'
174 # still errors in multi-line.
176 tmp="
177 |blank '[build check]'
178 |param |- |--envchk|---envchk |= |%<envchk> |&<on_envchk>
179 |'execute environment check for compile.'
180 |param |- |-- |---depchk |= |%<depchk> |&<on_depchk>
181 |'dependence inc file & lib file check.'
182 |param |- |-- |---buildep |= |%<buildep> |&<on_buildep>
183 |'dependence pkg download & build.'
185 |blank '[build misc]'
189 # TBD: envchk, depchk, buildep,
190 # doc, umdoc, mandoc, infodoc, tutorial, pdfdoc, htmldoc,
191 # genpkg, install, gendevpkg, devinstall,
192 # setting, release, debug,
194 # NOTE:
195 # @ if there is a newline char in '' string, it will appears error, dispaly
196 # string append blanks in length of pname_with.
197 cmpl_desc_str="
198 |blank '[build check]'
199 |param |- |-- |---envchk |= |%<envchk> |&<on_envchk> |'check compiling environment.'
200 |param |- |-- |---depchk |= |%<depchk> |&<on_depchk> |'dependent inc file & lib file check.'
201 |param |- |-- |---buildep |= |%<buildep> |&<on_buildep> |'dependent pkg download & build.'
203 |blank '[main build cmd]'
204 |param |- |-- |---srcbak |= |%<srcbak> |&<on_srcbak> |'distclean src pkg, and make an archive to backup dir. it is used for src code publish, or daily code backup.'
205 |param |- |-- |---clean |= |%<clean> |&<on_clean> |'clean files in compile procedure.'
206 |param |- |-- |---config |= |%<config> |&<on_config> |'feature config in src pkg.'
207 |param |- |-- |---all |= |%<all> |&<on_all> |'build in step of defconf, default, example, test, doc, umdoc, genpkg.'
208 |param |- |-- |---defconf |= |%<defconf> |&<on_defconf> |'set by default config.'
209 |param |- |-- |---default |= |%<default> |&<on_default> |'compile all dest in a src pkg.'
210 |param |- |-- |---example |= |%<example> |&<on_example> |'build example code if it exist.'
211 |param |- |-- |---test |= |%<test> |&<on_test> |'run test script for current src pkg.'
212 |param |- |-- |---doc |= |%<doc> |&<on_doc> |'build man and info helper doc.'
213 |param |- |-- |---umdoc |= |%<umdoc> |&<on_umdoc> |'build user manual doc in various kinds of doc type.'
214 |param |- |-- |---genpkg |=[pkgtype] |%<genpkg> |&<on_genpkg> |'build relative executables into a specified install pkg. if no pkgtype specified, it generate main/dev/umdoc/intl/test/eg pkgs.'
215 |param |- |-- |---install |=[pkgtype] |%<install> |&<on_install> |'install specified pkgs.'
217 |blank '[other build cmd]'
218 |param |- |-- |---dest |=[dest] |%<dest> |&<on_dest> |'build one of specified executables.'
219 |param |- |-- |---full |= |%<full> |&<on_full> |'compile all platform setted cpu platform in compiler toolchain.'
220 |param |- |-- |---setarch |=<arch> |%<setarch> |&<on_setarch> |'specified cross platform, and save.'
221 |param |- |-- |---cross |= |%<cross> |&<on_cross> |'build default cross platform setted '
222 |'insrc-pkg. it is setted by subcmd of setarch.'
223 |param |- |-- |---crosspkg |= |%<crosspkg> |&<on_crosspkg> |'build cross executables to a package.'
224 |param |- |-- |---gendevpkg |=[pkgname] |%<gendevpkg> |&<on_gendevpkg> |'generating pkg with dev lib.'
225 |param |- |-- |---devinstall |= |%<devinstall> |&<on_devinstall>|'install dev pkg.'
227 |blank '[build doc]'
228 |param |- |-- |---mandoc |= |%<mandoc> |&<on_mandoc> |'build man doc.'
229 |param |- |-- |---infodoc |= |%<infodoc> |&<on_infodoc> |'build info doc.'
230 |param |- |-- |---tutorial |= |%<tutorial> |&<on_tutorial> |'build tutorial doc. it is an example guide doc.'
231 |param |- |-- |---pdfdoc |= |%<pdfdoc> |&<on_pdfdoc> |'build pdf user manual doc.'
232 |param |- |-- |---htmldoc |= |%<htmldoc> |&<on_htmldoc> |'build html user manual doc.'
234 |blank '[build src-pkg setting]'
235 |param |- |-- |---setting |= |%<setting> |&<on_setting> |'set src pkg build paramters.'
236 |param |- |-- |---release |= |%<release> |&<on_release> |'build release version.'
237 |param |- |-- |---debug |= |%<debug> |&<on_debug> |'build debug version, and genpkg with dev lib, install pkg by devinstall.'
239 |blank
242 cmpl_other_desc_str="
244 |blank '[Version & Helper & Debug]'
245 |param |- |--version |---version |= |%<version> |&<on_version> |'output version info of this program.'
246 |param |-h |--help |---help |= |%<help> |&<on_help> |'this helper text doc.'
252 # application relative
254 SRCBAK_DIR=${SRCBAK_DIR='../'}
255 buildlib_loaded=0
257 load_shlib_on_build ()
259 # check if loaded, or not in root of srcpkg.
260 test ! -d "build" && test ! -d "tools/build-srcpkg" && test buildlib_loaded = 1 &&
261 echo "'build' & 'tools/build-srcpkg' are not exist in current dir." &&
262 echo "is it the root dir of srcpkg?" >&2 &&
263 exit -1
265 SRCPKG_DIR_FULL=$PWD
267 . tools/build-srcpkg/shlib/incfile.shlib
269 #loadimi ${SRCPKG_DIR_FULL}/tools/build-srcpkg/info/SrcPkgDirs.imi
270 loadimi tools/build-srcpkg/info/SrcPkgDirs.imi
272 inc tools/build-srcpkg/shlib/worklist.shlib
274 # inc tools/build-srcpkg/shlib/cmdline.shlib
275 #inc build/shlib/pre_build.shlib
277 buildlib_loaded=1
280 ##############################
281 # section: private function
282 ##############################
286 ############################################################################
287 # arg proc func
288 ############################################################################
291 # fsyntax: on_envchk
292 # fdesc: on envchk sub-cmd.
294 on_envchk ()
296 # TBD:
298 echo xxxxxxxxxxxx
302 # fsyntax: on_depchk
303 # fdesc: on depchk sub-cmd.
305 on_depchk ()
307 # TBD:
309 echo xxxxxxxxxxxx
313 # fsyntax: on_buildep
314 # fdesc: on buildep sub-cmd.
316 on_buildep ()
318 # TBD:
323 # fsyntax: on_srcbak
324 # fdesc: on srcbak sub-cmd.
326 on_srcbak ()
328 on_clean
330 dir="$(basename $PWD)"
331 file="${dir}-$(date +%Y%m%d).zip"
333 cd ..
334 mkdir -pv $(dirname $file)
335 rm ${file} 2>/dev/null
336 echo "zip -r ${file} ${dir}"
337 zip -r ${file} ${dir}
338 cd - 2>/dev/null
340 [[ $SRCBAK_DIR != '../' && $SRCBAK_DIR != '..' ]] && mkdir -pv $SRCBAK_DIR && mv ../$file ${SRCBAK_DIR}/ -f
342 exit 0
346 # fsyntax: on_clean
347 # fdesc: on help sub-cmd.
349 on_clean ()
351 echo cleaning ...
353 OBJDIR="$(eval echo ${OBJDIR_FMT})"
355 rm $OBJ_LIST ${OBJDIR} $OUTDIR -rf
357 while read dst; do
358 test -z $dst && continue
359 eval echo rm "${OUTDIR}${DST_FMT}" -rf
360 eval rm ${OUTDIR}${DST_FMT} -rf
361 done <<< "$DST_LIST"
363 # dirbuild "$@"
367 # fsyntax: on_config
368 # fdesc: on help sub-cmd.
370 on_config ()
372 test -f Config.in || test -f Kconfig &&
373 build-config "$@" && return $?
374 return -1
378 # fsyntax: on_all
379 # fdesc: on all sub-cmd. build with defconf, default, example, test,
380 # doc, umdoc, genpkg.
382 on_all ()
384 on_defconf "$@"
385 test $? != 0 && return $?
387 pre_build
388 # g_pre_build
389 test $? != 0 && return $?
391 on_default "$@"
392 test $? != 0 && return $?
394 on_example "$@"
395 test $? != 0 && return $?
397 on_test "$@"
398 test $? != 0 && return $?
400 on_doc "$@"
401 test $? != 0 && return $?
403 on_umdoc "$@"
404 test $? != 0 && return $?
406 on_genpkg "$@"
407 test $? != 0 && return $?
411 # fsyntax: on_defconf
412 # fdesc: on defconf sub-cmd.
414 on_defconf ()
416 test -e build/default.config && cp build/default.config .config
417 return $?
421 # fsyntax: on_default
422 # fdesc: on help sub-cmd.
424 on_default ()
426 load_shlib_on_build
428 construct_init
430 # build src to executables
431 TASK_RUNNING "STEP_BUILD_DEST"
432 test $? != 0 && return $?
436 # fsyntax: on_example
437 # fdesc: on help sub-cmd.
439 on_example ()
441 load_shlib_on_build
443 construct_init
445 TASK_RUNNING "STEP_EXAMPLE"
446 return $?
450 # fsyntax: on_test
451 # fdesc: on test sub-cmd.
453 on_test ()
455 scripttest
456 return $?
460 # fsyntax: on_doc
461 # fdesc: on doc sub-cmd.
463 on_doc ()
465 load_shlib_on_build
467 construct_init
469 TASK_RUNNING "STEP_DOC"
470 return $?
474 # fsyntax: on_umdoc
475 # fdesc: on umdoc sub-cmd.
477 on_umdoc ()
479 load_shlib_on_build
481 construct_init
483 TASK_RUNNING "STEP_UMDOC"
484 return $?
488 # fsyntax: on_genpkg
489 # fdesc: on genpkg sub-cmd.
491 on_genpkg ()
493 load_shlib_on_build
495 construct_init
497 TASK_RUNNING "STEP_INSTPKG"
498 return $?
502 # fsyntax: on_install
503 # fdesc: on help sub-cmd.
505 on_install ()
507 # TBD:
512 # fsyntax: on_dest
513 # fdesc: on dest sub-cmd.
515 on_dest ()
517 # TBD:
522 # fsyntax: on_full
523 # fdesc: on help sub-cmd.
525 on_full ()
527 local i=
529 for i in $(cat ); do
530 echo "##################################################"
531 echo "##################################################"
532 echo "# build for arch '$i' ..."
534 ARCH=$i on_all "$@"
535 done
539 # fsyntax: on_setarch
540 # fdesc: on setarch sub-cmd.
542 on_setarch ()
544 echo "ARCH=$1" > build/output/arch.imi
548 # fsyntax: on_cross
549 # fdesc: on cross sub-cmd.
551 on_cross ()
553 test -f build/output/arch.imi &&
554 echo "[err] arch.imi does not existing. run 'cmpl setarch <arch>' first." >&2 &&
555 exit
557 loadimi build/output/arch.imi
559 on_all "$@"
563 # fsyntax: on_crosspkg
564 # fdesc: on crosspkg sub-cmd.
566 on_crosspkg ()
568 test -f build/output/arch.imi &&
569 echo "[err] arch.imi does not existing. run 'cmpl setarch <arch>' first." >&2 &&
570 exit
572 loadimi build/output/arch.imi
574 on_genpkg "$@"
578 # fsyntax: on_gendevpkg
579 # fdesc: on gendevpkg sub-cmd.
581 on_gendevpkg ()
583 # TBD:
585 echo xxxxxxxxxxxx "$@"
589 # fsyntax: on_devinstall
590 # fdesc: on devinstall sub-cmd.
592 on_devinstall ()
594 # TBD:
599 # fsyntax: on_mandoc
600 # fdesc: on mandoc sub-cmd.
602 on_mandoc ()
604 # TBD:
609 # fsyntax: on_infodoc
610 # fdesc: on help sub-cmd.
612 on_infodoc ()
614 # TBD:
619 # fsyntax: on_tutorial
620 # fdesc: on help sub-cmd.
622 on_tutorial ()
624 # TBD:
629 # fsyntax: on_pdfdoc
630 # fdesc: on help sub-cmd.
632 on_pdfdoc ()
634 # TBD:
639 # fsyntax: on_htmldoc
640 # fdesc: on help sub-cmd.
642 on_htmldoc ()
644 # TBD:
649 # fsyntax: on_setting
650 # fdesc: on help sub-cmd.
652 on_setting ()
654 # TBD:
659 # fsyntax: on_release
660 # fdesc: on help sub-cmd.
662 on_release ()
664 # TBD:
669 # fsyntax: on_debug
670 # fdesc: on help sub-cmd.
672 on_debug ()
674 # TBD:
679 # fsyntax: on_version
680 # fdesc: on version sub-cmd.
682 on_version ()
684 eval echo "$CMPL_PROG_VERSION"
686 exit
690 # fsyntax: on_help
691 # fdesc: on help sub-cmd.
693 on_help ()
695 test -z $term_width && term_width=80
697 helper="$(opt_helper)"
699 eval CMPL_PROG_SYNTAX="\"$CMPL_PROG_SYNTAX\""
700 eval echo -ne "\"$CMPL_USAGE_FMT\""
702 exit
707 ############################################################################
709 ############################################################################
712 # main function
714 main ()
716 expr match "$@" '--load' >/dev/null 2>&1
717 test "$?" = 0 && return
720 # default operation just compile src code.
722 test $# = 0 && set -- "$@" default
725 # args parsing
727 # prog_opt_dispatch "$@"
728 opt_desc_str_dispatch cmpl_desc_hdr_str
729 opt_desc_str_dispatch cmpl_desc_str
730 opt_desc_str_dispatch cmpl_other_desc_str
731 prog_opt_proc "$@"
733 # init_dbglogout 2 testing 20000
734 # set_output_prefix info ""
735 # set_auto_newline ""
738 # running action list function for options after init.
739 # the paramter for init will be effact, then execute the
740 # action function.
742 action_list_exec
744 exit $?
747 ##############################
748 # section: public function
749 ##############################
752 expr match "$@" '--debug' >/dev/null 2>&1
753 if test "$?" = 0; then
754 DBGOUTD_OUTPUT=1
756 # does not need to shift args if the --debug option is not the
757 # first option. it will be ignored in process.
758 # shift
761 expr match "$@" '--loadshlib' >/dev/null 2>&1
762 if test "$?" != 0; then
763 main "$@"
764 else
765 shift
768 unset DBGOUTD_OUTPUT
770 ##############################
771 # section: file tail
772 ##############################
778 if false; then
780 # copy code from term.shlib in shlib, do not use whole lib.
782 readonly CNORMAL="\033[0m" # restore to normal mode
783 readonly FCGREEN="\033[32m" # font green
784 readonly FCMAGENTA="\033[35m" # font magenta
785 readonly FCRED="\033[31m" # font red
786 readonly FCCYAN="\033[36m" # font cyan
787 readonly CREV="\033[7m" # reverse
789 FCCOLOR=$FCGREEN
790 IFCCOLOR=$FCMAGENTA
791 EFCCOLOR=$FCRED
792 INFOCOLOR=$FCGREEN