first version.
[build-srcpkg.git] / bin / cmpl
blobcad24bc6ed4f7030a08e4d7c372a94a9cd37839f
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 |- |-- |---clean |= |%<clean> |&<on_clean> |'clean files in compile procedure.'
205 |param |- |-- |---config |= |%<config> |&<on_config> |'feature config in src pkg.'
206 |param |- |-- |---all |= |%<all> |&<on_all> |'build in step of defconf, default, example, test, doc, umdoc, genpkg.'
207 |param |- |-- |---defconf |= |%<defconf> |&<on_defconf> |'set by default config.'
208 |param |- |-- |---default |= |%<default> |&<on_default> |'compile all dest in a src pkg.'
209 |param |- |-- |---example |= |%<example> |&<on_example> |'build example code if it exist.'
210 |param |- |-- |---test |= |%<test> |&<on_test> |'run test script for current src pkg.'
211 |param |- |-- |---doc |= |%<doc> |&<on_doc> |'build man and info helper doc.'
212 |param |- |-- |---umdoc |= |%<umdoc> |&<on_umdoc> |'build user manual doc in various kinds of doc type.'
213 |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.'
214 |param |- |-- |---install |=[pkgtype] |%<install> |&<on_install> |'install specified pkgs.'
216 |blank '[other build cmd]'
217 |param |- |-- |---dest |=[dest] |%<dest> |&<on_dest> |'build one of specified executables.'
218 |param |- |-- |---full |= |%<full> |&<on_full> |'compile all platform setted cpu platform in compiler toolchain.'
219 |param |- |-- |---setarch |=<arch> |%<setarch> |&<on_setarch> |'specified cross platform, and save.'
220 |param |- |-- |---cross |= |%<cross> |&<on_cross> |'build default cross platform setted '
221 |'insrc-pkg. it is setted by subcmd of setarch.'
222 |param |- |-- |---crosspkg |= |%<crosspkg> |&<on_crosspkg> |'build cross executables to a package.'
223 |param |- |-- |---gendevpkg |=[pkgname] |%<gendevpkg> |&<on_gendevpkg> |'generating pkg with dev lib.'
224 |param |- |-- |---devinstall |= |%<devinstall> |&<on_devinstall>|'install dev pkg.'
226 |blank '[build doc]'
227 |param |- |-- |---mandoc |= |%<mandoc> |&<on_mandoc> |'build man doc.'
228 |param |- |-- |---infodoc |= |%<infodoc> |&<on_infodoc> |'build info doc.'
229 |param |- |-- |---tutorial |= |%<tutorial> |&<on_tutorial> |'build tutorial doc. it is an example guide doc.'
230 |param |- |-- |---pdfdoc |= |%<pdfdoc> |&<on_pdfdoc> |'build pdf user manual doc.'
231 |param |- |-- |---htmldoc |= |%<htmldoc> |&<on_htmldoc> |'build html user manual doc.'
233 |blank '[build src-pkg setting]'
234 |param |- |-- |---setting |= |%<setting> |&<on_setting> |'set src pkg build paramters.'
235 |param |- |-- |---release |= |%<release> |&<on_release> |'build release version.'
236 |param |- |-- |---debug |= |%<debug> |&<on_debug> |'build debug version, and genpkg with dev lib, install pkg by devinstall.'
238 |blank
241 cmpl_other_desc_str="
243 |blank '[Version & Helper & Debug]'
244 |param |- |--version |---version |= |%<version> |&<on_version> |'output version info of this program.'
245 |param |-h |--help |---help |= |%<help> |&<on_help> |'this helper text doc.'
251 # application relative
253 SRCBAK_DIR=${SRCBAK_DIR='../'}
254 buildlib_loaded=0
256 load_shlib_on_build ()
258 # check if loaded, or not in root of srcpkg.
259 test ! -d "build" && test ! -d "tools/build-srcpkg" && test buildlib_loaded = 1 &&
260 echo "'build' & 'tools/build-srcpkg' are not exist in current dir." &&
261 echo "is it the root dir of srcpkg?" >&2 &&
262 exit -1
264 SRCPKG_DIR_FULL=$PWD
266 . tools/build-srcpkg/shlib/incfile.shlib
268 #loadimi ${SRCPKG_DIR_FULL}/tools/build-srcpkg/info/SrcPkgDirs.imi
269 loadimi tools/build-srcpkg/info/SrcPkgDirs.imi
271 inc tools/build-srcpkg/shlib/worklist.shlib
273 inc tools/build-srcpkg/shlib/cmdline.shlib
274 #inc build/shlib/pre_build.shlib
276 buildlib_loaded=1
279 ##############################
280 # section: private function
281 ##############################
285 ############################################################################
286 # arg proc func
287 ############################################################################
290 # fsyntax: on_envchk
291 # fdesc: on envchk sub-cmd.
293 on_envchk ()
295 # TBD:
297 echo xxxxxxxxxxxx
301 # fsyntax: on_depchk
302 # fdesc: on depchk sub-cmd.
304 on_depchk ()
306 # TBD:
308 echo xxxxxxxxxxxx
312 # fsyntax: on_buildep
313 # fdesc: on buildep sub-cmd.
315 on_buildep ()
317 # TBD:
322 # fsyntax: on_clean
323 # fdesc: on help sub-cmd.
325 on_clean ()
327 echo cleaning ...
329 OBJDIR="$(eval echo ${OBJDIR_FMT})"
331 rm $OBJ_LIST ${OBJDIR} $OUTDIR -rf
333 while read dst; do
334 test -z $dst && continue
335 eval echo rm "${OUTDIR}${DST_FMT}" -rf
336 eval rm ${OUTDIR}${DST_FMT} -rf
337 done <<< "$DST_LIST"
339 # dirbuild "$@"
343 # fsyntax: on_config
344 # fdesc: on help sub-cmd.
346 on_config ()
348 test -f Config.in || test -f Kconfig &&
349 build-config "$@" && return $?
350 return -1
354 # fsyntax: on_all
355 # fdesc: on all sub-cmd. build with defconf, default, example, test,
356 # doc, umdoc, genpkg.
358 on_all ()
360 on_defconf "$@"
361 test $? != 0 && return $?
363 pre_build
364 # g_pre_build
365 test $? != 0 && return $?
367 on_default "$@"
368 test $? != 0 && return $?
370 on_example "$@"
371 test $? != 0 && return $?
373 on_test "$@"
374 test $? != 0 && return $?
376 on_doc "$@"
377 test $? != 0 && return $?
379 on_umdoc "$@"
380 test $? != 0 && return $?
382 on_genpkg "$@"
383 test $? != 0 && return $?
387 # fsyntax: on_defconf
388 # fdesc: on defconf sub-cmd.
390 on_defconf ()
392 test -e build/default.config && cp build/default.config .config
393 return $?
397 # fsyntax: on_default
398 # fdesc: on help sub-cmd.
400 on_default ()
402 load_shlib_on_build
404 construct_init
406 # build src to executables
407 TASK_RUNNING "STEP_BUILD_DEST"
408 test $? != 0 && return $?
412 # fsyntax: on_example
413 # fdesc: on help sub-cmd.
415 on_example ()
417 load_shlib_on_build
419 construct_init
421 TASK_RUNNING "STEP_EXAMPLE"
422 return $?
426 # fsyntax: on_test
427 # fdesc: on test sub-cmd.
429 on_test ()
431 scripttest
432 return $?
436 # fsyntax: on_doc
437 # fdesc: on doc sub-cmd.
439 on_doc ()
441 load_shlib_on_build
443 construct_init
445 TASK_RUNNING "STEP_DOC"
446 return $?
450 # fsyntax: on_umdoc
451 # fdesc: on umdoc sub-cmd.
453 on_umdoc ()
455 load_shlib_on_build
457 construct_init
459 TASK_RUNNING "STEP_UMDOC"
460 return $?
464 # fsyntax: on_genpkg
465 # fdesc: on genpkg sub-cmd.
467 on_genpkg ()
469 load_shlib_on_build
471 construct_init
473 TASK_RUNNING "STEP_INSTPKG"
474 return $?
478 # fsyntax: on_install
479 # fdesc: on help sub-cmd.
481 on_install ()
483 # TBD:
488 # fsyntax: on_dest
489 # fdesc: on dest sub-cmd.
491 on_dest ()
493 # TBD:
498 # fsyntax: on_full
499 # fdesc: on help sub-cmd.
501 on_full ()
503 local i=
505 for i in $(cat ); do
506 echo "##################################################"
507 echo "##################################################"
508 echo "# build for arch '$i' ..."
510 ARCH=$i on_all "$@"
511 done
515 # fsyntax: on_setarch
516 # fdesc: on setarch sub-cmd.
518 on_setarch ()
520 echo "ARCH=$1" > build/output/arch.imi
524 # fsyntax: on_cross
525 # fdesc: on cross sub-cmd.
527 on_cross ()
529 test -f build/output/arch.imi &&
530 echo "[err] arch.imi does not existing. run 'cmpl setarch <arch>' first." >&2 &&
531 exit
533 loadimi build/output/arch.imi
535 on_all "$@"
539 # fsyntax: on_crosspkg
540 # fdesc: on crosspkg sub-cmd.
542 on_crosspkg ()
544 test -f build/output/arch.imi &&
545 echo "[err] arch.imi does not existing. run 'cmpl setarch <arch>' first." >&2 &&
546 exit
548 loadimi build/output/arch.imi
550 on_genpkg "$@"
554 # fsyntax: on_gendevpkg
555 # fdesc: on gendevpkg sub-cmd.
557 on_gendevpkg ()
559 # TBD:
561 echo xxxxxxxxxxxx "$@"
565 # fsyntax: on_devinstall
566 # fdesc: on devinstall sub-cmd.
568 on_devinstall ()
570 # TBD:
575 # fsyntax: on_mandoc
576 # fdesc: on mandoc sub-cmd.
578 on_mandoc ()
580 # TBD:
585 # fsyntax: on_infodoc
586 # fdesc: on help sub-cmd.
588 on_infodoc ()
590 # TBD:
595 # fsyntax: on_tutorial
596 # fdesc: on help sub-cmd.
598 on_tutorial ()
600 # TBD:
605 # fsyntax: on_pdfdoc
606 # fdesc: on help sub-cmd.
608 on_pdfdoc ()
610 # TBD:
615 # fsyntax: on_htmldoc
616 # fdesc: on help sub-cmd.
618 on_htmldoc ()
620 # TBD:
625 # fsyntax: on_setting
626 # fdesc: on help sub-cmd.
628 on_setting ()
630 # TBD:
635 # fsyntax: on_release
636 # fdesc: on help sub-cmd.
638 on_release ()
640 # TBD:
645 # fsyntax: on_debug
646 # fdesc: on help sub-cmd.
648 on_debug ()
650 # TBD:
655 # fsyntax: on_version
656 # fdesc: on version sub-cmd.
658 on_version ()
660 eval echo "$CMPL_PROG_VERSION"
662 exit
666 # fsyntax: on_help
667 # fdesc: on help sub-cmd.
669 on_help ()
671 test -z $term_width && term_width=80
673 helper="$(opt_helper)"
675 eval CMPL_PROG_SYNTAX="\"$CMPL_PROG_SYNTAX\""
676 eval echo -ne "\"$CMPL_USAGE_FMT\""
678 exit
683 ############################################################################
685 ############################################################################
688 # main function
690 main ()
692 expr match "$@" '--load' >/dev/null 2>&1
693 test "$?" = 0 && return
696 # default operation just compile src code.
698 test $# = 0 && set -- "$@" default
701 # args parsing
703 # prog_opt_dispatch "$@"
704 opt_desc_str_dispatch cmpl_desc_hdr_str
705 opt_desc_str_dispatch cmpl_desc_str
706 opt_desc_str_dispatch cmpl_other_desc_str
707 prog_opt_proc "$@"
709 # init_dbglogout 2 testing 20000
710 # set_output_prefix info ""
711 # set_auto_newline ""
714 # running action list function for options after init.
715 # the paramter for init will be effact, then execute the
716 # action function.
718 action_list_exec
720 exit $?
723 ##############################
724 # section: public function
725 ##############################
728 expr match "$@" '--debug' >/dev/null 2>&1
729 if test "$?" = 0; then
730 DBGOUTD_OUTPUT=1
732 # does not need to shift args if the --debug option is not the
733 # first option. it will be ignored in process.
734 # shift
737 expr match "$@" '--loadshlib' >/dev/null 2>&1
738 if test "$?" != 0; then
739 main "$@"
740 else
741 shift
744 unset DBGOUTD_OUTPUT
746 ##############################
747 # section: file tail
748 ##############################
754 if false; then
756 # copy code from term.shlib in shlib, do not use whole lib.
758 readonly CNORMAL="\033[0m" # restore to normal mode
759 readonly FCGREEN="\033[32m" # font green
760 readonly FCMAGENTA="\033[35m" # font magenta
761 readonly FCRED="\033[31m" # font red
762 readonly FCCYAN="\033[36m" # font cyan
763 readonly CREV="\033[7m" # reverse
765 FCCOLOR=$FCGREEN
766 IFCCOLOR=$FCMAGENTA
767 EFCCOLOR=$FCRED
768 INFOCOLOR=$FCGREEN