improve of cmpl.
[bush.git] / tools / cmpl / shlib / fname.shlib
blob32368932b95e2bb52d0644b4c611fc240f50dedd
1 #!/bin/bash
2 ############################################################
3 # source: fname.shlib
4 # author: CottonCandyOwner(CottonCandyOwner@126.com)
5 # date: 2023-02-15
6 ############################################################
7 # note:
9 # file & dir name stored in envar for using. as an auxiliary
10 # method, arg param in func update to local envar for using.
11 # those var are store dir/file name for using.
13 # SRC_FILE[0], input src file name. in compile, it's xxx.c, in link, it's xxx.o
14 # DSTFILE/DST_FILE[0], output dst file name. lib/dll/exe
15 # OBJFILE/DST_FILE[1], output obj file name.
16 # DEPFILE/DST_FILE[2], output hdr dep file name.
17 # CGRAPHFILE/DST_FILE[3], output call graph file name.
19 # @ all dir defined without '/' surffix, but root dir.
20 # @ ext name with '.' prefix in envar.
21 # @ {} should be added for envar using.
22 # @ ${var} should be quoted with double quote.
24 # this copy of src can be used as a single file. it process
25 # with vars below.
26 # @ public defination.
27 # EXT_NAME[]
28 # @ param var, defined in .imi for config.
29 # DEST_NAME/DEST_TYPE/OUTDIR=
30 # @ *_EVL format define var, defined in this file.
31 # DSTDIR_EVL/OBJDIR_EVL/DSTFILE_FMT/OBJFILE_FMT/DEPFILE_FMT
32 # /CGRAPHFILE_FMT
33 # @ output var, generated in func in this file.
34 # DSTDIR/OBJDIR/DSTFILE/OBJFILE/DEPFILE/CGRAPHFILE/DST_FILE[]
36 ############################################################
41 # todo£º
42 # @
43 # @
46 ######################
47 # section: common comment info
48 ######################
51 ######################
52 # section: variable define
53 ######################
56 # file ext name.
57 # TBD: those info should be defined in platform/toolchain/*.imi file as general info.
60 if [[ -z $OS || $OS == linux ]]; then
61 BUILD_SYSTEM=linux
62 elif [[ $OS == win32 || $OS == cygwin || TOOLCHAIN == vc ]]; then
63 BUILD_SYSTEM=win32
64 else
65 BUILD_SYSTEM=unkown
66 echo OS=$OS
67 echo "unkown build system."
70 if [[ -z $OS || $OS == cygwin || TOOLCHAIN == gcc ]]; then
71 CMPLR=gcc
72 elif strstr $VENDOR =~ GNU; then
73 CMPLR=gcc
74 elif [[ $OS == win32 || TOOLCHAIN == vc ]]; then
75 CMPLR=vc
76 elif [[ TOOLCHAIN == iar || TOOLCHAIN == mdk ]]; then
77 CMPLR=$TOOLCHAIN
78 else
82 # DEST_TYPE=null
83 setenv EXT_NAME[null]=""
86 # operating system relative
88 if [[ $BUILD_SYSTEM == linux ]]; then
89 # linux
90 EXT_NAME_drv=".ko"
91 EXT_NAME_exe=""
92 EXT_NAME_dll=".so"
93 EXT_NAME_lib=".a"
94 EXT_NAME_exelist=""
95 EXE=
96 DLL=.so
97 LIB=.a
98 DRV=.ko
99 elif [[ $BUILD_SYSTEM == win32 ]]; then
100 # win32
101 EXE=.exe
102 DLL=.dll
103 LIB=.lib
104 DRV=.sys
105 EXT_NAME_drv=".sys"
106 EXT_NAME_exe=".exe"
107 EXT_NAME_dll=".dll"
108 EXT_NAME_lib=".lib"
109 EXT_NAME_exelist=".exe"
110 else
111 echo "invalid OS type($BUILD_SYSTEM) info."
112 exit -1
116 # arch/toolchain relative
118 if [[ $CMPLR == gcc ]]; then
119 EXT_NAME_objlist=".o"
120 EXT_NAME_asm=".S"
121 EXT_NAME_obj=".o"
123 EXT_NAME_sh=".sh"
124 EXT_NAME_c=".c"
125 EXT_NAME_cxx=".cc"
126 EXT_NAME_cpp=".cpp"
127 EXT_NAME_dephdr=".dephdr"
128 EXT_NAME_cgraph=".callgraph"
129 elif [[ $CMPLR == vc ]]; then
130 EXT_NAME_objlist=".obj"
131 EXT_NAME_asm=".asm"
132 EXT_NAME_obj=".obj"
134 EXT_NAME_sh=".sh"
135 EXT_NAME_c=".c"
136 EXT_NAME_cxx=".cc"
137 EXT_NAME_cpp=".cpp"
138 EXT_NAME_dephdr=".dephdr"
139 EXT_NAME_cgraph=".callgraph"
140 elif [[ $CMPLR == iar || $CMPLR == mdk ]]; then
141 EXT_NAME_objlist=".obj"
142 EXT_NAME_asm=".asm"
143 EXT_NAME_obj=".obj"
145 EXT_NAME_sh=".sh"
146 EXT_NAME_c=".c"
147 EXT_NAME_cxx=".cc"
148 EXT_NAME_cpp=".cpp"
149 EXT_NAME_dephdr=".dephdr"
150 EXT_NAME_cgraph=".callgraph"
151 else
152 echo "invalid toolchain($TOOLCHAIN) info."
153 exit -1
156 EXT_NAME_texi=".texi"
157 EXT_NAME_md=".md"
158 EXT_NAME_docbook=".docbook"
159 EXT_NAME_man1=".1"
160 EXT_NAME_man2=".2"
161 EXT_NAME_man3=".3"
162 EXT_NAME_man4=".4"
163 EXT_NAME_man5=".5"
164 EXT_NAME_man6=".6"
165 EXT_NAME_man7=".7"
166 EXT_NAME_info=".info"
167 EXT_NAME_dvi=".dvi"
168 EXT_NAME_ps=".ps"
169 EXT_NAME_txt=".txt"
170 EXT_NAME_html=".html"
171 EXT_NAME_chm=".chm"
172 EXT_NAME_pdf=".pdf"
174 SRC2DST_c="obj"
175 SRC2DST_cxx="obj"
176 SRC2DST_cpp="obj"
177 SRC2DST_asm="obj"
180 # setenv LANG_EXT_NAME[c]="c"
181 # setenv LANG_EXT_NAME[sh]="sh"
182 #if [[ ${COMPILER_TYPE,,} == gcc ]]; then
183 # setenv LANG_EXT_NAME[cxx]="cc"
184 # setenv LANG_EXT_NAME[cpp]="cpp"
185 # setenv LANG_EXT_NAME[c++]="cpp"
186 # setenv LANG_EXT_NAME[asm]="S"
187 #elif [[ ${SYSTEM_TYPE,,} == mac ]]; then
188 # setenv LANG_EXT_NAME[cxx]="cc"
189 # setenv LANG_EXT_NAME[cpp]="cpp"
190 # setenv LANG_EXT_NAME[c++]="cpp"
191 # setenv LANG_EXT_NAME[asm]="asm"
195 setenv EXT_NAME[drv]="${EXT_NAME_drv}"
196 setenv EXT_NAME[exe]="${EXT_NAME_exe}"
197 setenv EXT_NAME[dll]="${EXT_NAME_dll}"
198 setenv EXT_NAME[lib]="${EXT_NAME_lib}"
199 setenv EXT_NAME[exelist]="${EXT_NAME_exelist}"
202 setenv EXT_NAME[objlist]="${EXT_NAME_objlist}"
203 setenv EXT_NAME[asm]="${EXT_NAME_asm}"
204 setenv EXT_NAME[obj]="${EXT_NAME_obj}"
206 setenv EXT_NAME[sh]="${EXT_NAME_sh}"
207 setenv EXT_NAME[c]="${EXT_NAME_c}"
208 setenv EXT_NAME[cxx]="${EXT_NAME_cxx}"
209 setenv EXT_NAME[cpp]="${EXT_NAME_cpp}"
210 setenv EXT_NAME[dephdr]="${EXT_NAME_dephdr}"
211 setenv EXT_NAME[cgraph]="${EXT_NAME_cgraph}"
214 setenv EXT_NAME[texi]="${EXT_NAME_texi}"
215 setenv EXT_NAME[md]="${EXT_NAME_md}"
216 setenv EXT_NAME[docbook]="${EXT_NAME_docbook}"
217 setenv EXT_NAME[man1]="${EXT_NAME_man1}"
218 setenv EXT_NAME[man2]="${EXT_NAME_man2}"
219 setenv EXT_NAME[man3]="${EXT_NAME_man3}"
220 setenv EXT_NAME[man4]="${EXT_NAME_man4}"
221 setenv EXT_NAME[man5]="${EXT_NAME_man5}"
222 setenv EXT_NAME[man6]="${EXT_NAME_man6}"
223 setenv EXT_NAME[man7]="${EXT_NAME_man7}"
224 setenv EXT_NAME[info]="${EXT_NAME_info}"
225 setenv EXT_NAME[dvi]="${EXT_NAME_dvi}"
226 setenv EXT_NAME[ps]="${EXT_NAME_ps}"
227 setenv EXT_NAME[txt]="${EXT_NAME_txt}"
228 setenv EXT_NAME[html]="${EXT_NAME_html}"
229 setenv EXT_NAME[chm]="${EXT_NAME_chm}"
230 setenv EXT_NAME[pdf]="${EXT_NAME_pdf}"
235 ######################
236 # section: private function
237 ######################
241 ######################
242 # section: public function
243 ######################
245 # syntax: fname_dstdir <ext-type> <fname>
246 fext_set ()
248 echo -n "$2" | sed -E "s/(.*)\..*/\1/g"
249 getenv EXT_NAME[${1}]
252 # syntax: fname_dstdir
253 fname_dstdir ()
255 eval DSTDIR="$DSTDIR_EVL"
258 # syntax: fname_objdir
259 fname_objdir ()
261 eval OBJDIR="$OBJDIR_EVL"
264 # syntax: fname_src2obj <src-file>
265 fname_src2obj ()
268 # append SRC_FILE as a local variable,
269 # it will not change global content.
271 test -n "$1" && local SRC_FILE="$1"
272 test -z "${SRC_FILE}" && return -1
274 local tmp="${SRC_FILE}"
275 SRC_FILE="$(echo "${SRC_FILE}" | sed -E "s/,.*$//g")"
277 eval OBJFILE="$OBJFILE_FMT"
278 SRC_FILE="$tmp"
279 setenv DST_FILE[1]=$OBJFILE
282 # syntax: fname_src2dst <obj-file> <dst-type>
283 fname_src2dst ()
285 test -n "$1" && local SRC_FILE="$1"
286 test -z "${SRC_FILE}" && return -1
288 local tmp="${SRC_FILE}"
289 SRC_FILE="$(echo "${SRC_FILE}" | sed -E "s/,.*$//g")"
291 eval DSTFILE="$DSTFILE_FMT"
292 SRC_FILE="$tmp"
293 DST_FILE=$DSTFILE
296 # syntax: fname_src2dep <src-file>
297 fname_src2dep ()
299 test -n "$1" && local SRC_FILE="$1"
300 test -z "${SRC_FILE}" && return -1
302 local tmp="${SRC_FILE}"
303 SRC_FILE="$(echo "${SRC_FILE}" | sed -E "s/,.*$//g")"
305 eval DEPFILE="$DEPFILE_FMT"
306 SRC_FILE="$tmp"
307 setenv DST_FILE[2]=$DEPFILE
310 # fsyntax: fname_src2graph <src-file>
311 fname_src2graph ()
313 test -n "$1" && local SRC_FILE="$1"
314 test -z "${SRC_FILE}" && return -1
316 local tmp="${SRC_FILE}"
317 SRC_FILE="$(echo "${SRC_FILE}" | sed -E "s/,.*$//g")"
319 eval CGRAPHFILE="$CGRAPHFILE_FMT"
320 SRC_FILE="$tmp"
321 setenv DST_FILE[3]=$CGRAPHFILE
324 fname_func_testing ()
326 echo "========================"
327 echo "= fname_func_testing()"
328 echo "========================"
330 export OUTDIR=build
331 export DEST_TYPE=exe
332 export DEST_NAME=bush
333 SRC_FILE="src/lxrgmr/subst.c"
334 echo DEST_NAME=$DEST_NAME
335 echo DEST_TYPE=$DEST_TYPE
336 echo OUTDIR=$OUTDIR
337 echo "SRC_FILE=${SRC_FILE}"
339 fname_dstdir
340 echo DSTDIR="\"$DSTDIR\""
342 fname_objdir
343 echo OBJDIR="\"$OBJDIR\""
345 fname_src2obj
346 echo OBJFILE="\"$OBJFILE\""
348 fname_obj2dst src/runner/xxx.c
349 echo DSTFILE="\"$DSTFILE\""
351 fname_src2dep src/var/variable.c
352 echo DEPFILE="\"$DEPFILE\""
353 fname_src2graph
354 echo CGRAPHFILE="\"$CGRAPHFILE\""
357 ######################
358 # section: file tail
359 ######################