Salome HOME
Prepare SALOME 2.2.6 installation
[tools/install.git] / config_files / common.sh
1 #!/bin/sh
2
3 # This script installs a subset of products necessary for SALOME application
4 #
5 # Usage: install <target-directory> [all]
6 #        install <target-directory> <product> ... <product>
7 #
8 # Type 'install --help' to get more information
9 #
10 # Important note: 
11 #   <target-directory> must be an absolute path
12 #
13 # The script is the part of the SALOME PRO installation procedure.
14 #
15 # Copyright : 2003 CEA/DEN, EDF R&D
16 ####################################################################################
17
18 export DELIM="------"
19 export SHRC="salome.sh"
20 export CSHRC="salome.csh"
21 export DEBUG=1
22
23 #this function takes several parameters
24 #first parameter contains descriptor of output file
25 #next parameters is command for execution
26 #in case of bad result of execution ask user about furher steps
27 #(user can cancel installation procedure)
28 check_job()
29 {
30   out=$1
31   shift
32   echo -e "`pwd` -> $* >> $out" 
33   if [ $DEBUG == 0 ] ; then
34     $* | tee $out
35   else
36     if [ $out == "1" ] ; then
37       $*
38     else
39       $* >> $out 2>&1
40     fi 
41   fi
42   if [ "$?" -ne "0" ] ; then
43     if [ $out != "1" ] ; then
44       cat $out
45     fi
46     echo -n " " >&2
47     echo -e "NOT DONE !!! => $*"
48     echo -n "Would you like to continue to install this product: (Y,N) [Y] : "
49     read rep;
50     rep=`echo $rep  | tr "[A-Z]" "[a-z]"`
51     if test -z $rep || [ $rep = "y" ] ; then
52       return 1
53     fi
54     exit 1
55   fi
56   return 0
57 }
58
59 #invoke check_job function
60 #all output will be put into terminal
61 check_jb()
62 {
63   check_job 1 $*
64   return
65 }
66
67 # make directory
68 makedir()
69 {
70   rm -rf "$1" > /dev/null
71   mkdir -p "$1"
72 }
73
74 #create environment for sh and csh
75 make_env()
76 {
77 install_root=$1;  shift
78 install_work=$1; 
79 for i in $SHRC $CSHRC ; do
80     if [ -e ${install_work}/$i ]; then
81         rm ${install_work}/$i
82     fi
83     touch ${install_work}/$i
84 done
85
86 if [ $# -eq 3 ] ; then
87     product_name=$3;
88     product_env=${install_work}/env_${product_name}.sh
89     (test -e ${product_env} && rm ${product_env} )
90 fi
91 ### !!! writing salome.sh file !!!  -> define INSTALL_ROOT
92 cat >> ${install_work}/$SHRC <<EOF
93 #${DELIM} Setting products installation directory ${DELIM}
94 export INSTALL_ROOT=${install_root}
95
96 #${DELIM} Environment switch: 0 for SALOME building, 1 for SALOME launching ${DELIM}
97 export ENV_FOR_LAUNCH=1
98
99 EOF
100 ### !!! The next is for RedHat 9 only !!! 
101 if [ -f /etc/redhat-release ] ; then
102   grep -e "Red Hat Linux release 9" /etc/redhat-release > /dev/null
103   if [ $? -eq 0 ] ; then
104     cat >> ${install_work}/$SHRC <<EOF
105 #${DELIM} Force RH9 to use old implementation of threads ${DELIM}
106 export LD_ASSUME_KERNEL=2.4.18
107
108 EOF
109   fi
110 fi
111 ### !!! writing salome.csh file !!! -> set inital variables
112 pre_vars="PATH LD_LIBRARY_PATH LD_RUN_PATH PYTHONPATH TCLLIBPATH"
113 for i in $pre_vars ; do
114 cat >> ${install_work}/$CSHRC <<EOF
115 #${DELIM} setting initial ${i} ${DELIM}
116 if (! (\$?${i}) ) then
117   setenv $i
118 endif
119
120 EOF
121 done
122
123 ### !!! writing salome.(c)sh files !!! -> dump products environment files, changing 'export' by 'setenv(a|p)' where necessary
124 ### Note, that for performance reasons temporary environment files used during installation procedure itself
125 ### are created without using setenv(a|p) aliases.
126
127 # 1. first dump all environment files into the $SHRC file
128 product_sequence="$2"
129 for i in $product_sequence ; do
130     cat ${install_work}/env_${i}.sh >> ${install_work}/$SHRC 2>/dev/null
131 done
132
133 # 2. writing global functions to _salome.sh file
134 cat >> ${install_work}/_$SHRC <<EOF
135 ##########################################################################
136 # Functions exporta and exportp are used to append/prepend correspondingly 
137 # one directory or a set of directories separated by semicolon symbol (':')
138 # to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
139 # LD_RUN_PATH etc. 
140 # The main purpose is to replace default setenv command behavior:
141 # exporta, exportp also remove duplicated entries, shortening in that way
142 # the environment variables.
143 # If some directory being added is already included into the variable
144 # nothing is done for it.
145 # Note, that these functions work some slower that setenv command itself.
146 #
147 #### cleandup ###
148 # appends/prepends set of directories (second parameter) 
149 # to the another set of directories (first parameter) and
150 # removes duplicated entries;
151 # the third parameter defines the mode: 0 - append, 1 - prepend
152 cleandup() {
153 out_var=\`echo \$1 \$2 | awk -v dir=\$3 '{                   \\
154     na = split(\$2,a,":");                                \\
155     if ( \$1 == "<empty>") nb = split("",b,":");          \\
156     else nb = split(\$1,b,":");                           \\
157     s = "" ;                                             \\
158     for(i=1;i<=nb;i++) {                                 \\
159         if(!(b[i] in ccc)) {                             \\
160             if(length(s)!=0)                             \\
161                 s = sprintf("%s:",s);                    \\
162             s = sprintf("%s%s",s,b[i]);                  \\
163             ccc[b[i]];                                   \\
164         };                                               \\
165     };                                                   \\
166     s1 = "";                                             \\
167     for(i=1;i<=na;i++) {                                 \\
168         if(!(a[i] in ccc)) {                             \\
169             if(length(s1)!=0)                            \\
170                 s1 = sprintf("%s:",s1);                  \\
171             s1 = sprintf("%s%s",s1,a[i]);                \\
172             ccc[a[i]];                                   \\
173         };                                               \\
174     };                                                   \\
175     if(dir)                                              \\
176         s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\
177     else                                                 \\
178         s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\
179     printf("%s\n",s);                                    \\
180     }'\`
181 echo \$out_var
182 }
183 ### exporta ###
184 # appends directory or set of directories, separated by ':' (second parameter)
185 # to the variable (first parameter)
186 exporta () { 
187 xenv=\`printenv \$1\`
188 if [ -z "\$xenv" ]; then xenv="<empty>"; fi
189 out_var=\`cleandup \$xenv \$2 0\`
190 export \$1=\$out_var
191 }
192 ### exportp ###
193 # prepends directory or set of directories, separated by ':' (second parameter)
194 # to the variable (first parameter)
195 exportp () { 
196 xenv=\`printenv \$1\`
197 if [ -z "\$xenv" ]; then xenv="<empty>"; fi
198 out_var=\`cleandup \$xenv \$2 1\`
199 export \$1=\$out_var
200 }
201 ###########################################################################
202
203 EOF
204
205 # 3. writing global functions to _salome.csh file
206 cat >> ${install_work}/_$CSHRC <<EOF
207 ###########################################################################
208 # Aliases setenva and setenvp are used to append/prepend correspondingly 
209 # one directory or a set of directories separated by semicolon symbol (':')
210 # to the environment variables like PATH, LD_LIBRARY_PATH, PYTHONPATH,
211 # LD_RUN_PATH etc. 
212 # The main purpose is to replace default setenv command behavior:
213 # setenva, setenvp also remove duplicated entries, shortening in that way
214 # the environment variables.
215 # If some directory being added is already included into the variable
216 # nothing is done for it.
217 # Note, that these aliases work some slower that setenv command itself.
218 #
219 #### cleandup ###
220 # appends/prepends set of directories (second parameter) 
221 # to the another set of directories (first parameter) and
222 # removes duplicated entries;
223 # the third parameter defines the mode: 0 - append, 1 - prepend
224 alias cleandup "echo \!:1 \!:2 | awk -v dir=\!:3         \\\\
225 '"'{ na = split(\$2,a,":");                               \\\\
226      nb = split(\$1,b,":");                               \\\\
227      s = "" ;                                            \\\\
228      for(i=1;i<=nb;i++) {                                \\\\
229         if(!(b[i] in ccc)) {                             \\\\
230             if(length(s)!=0)                             \\\\
231                 s = sprintf("%s:",s);                    \\\\
232             s = sprintf("%s%s",s,b[i]);                  \\\\
233             ccc[b[i]];                                   \\\\
234         };                                               \\\\
235     };                                                   \\\\
236     s1 = "";                                             \\\\
237     for(i=1;i<=na;i++) {                                 \\\\
238         if(!(a[i] in ccc)) {                             \\\\
239             if(length(s1)!=0)                            \\\\
240                 s1 = sprintf("%s:",s1);                  \\\\
241             s1 = sprintf("%s%s",s1,a[i]);                \\\\
242             ccc[a[i]];                                   \\\\
243         };                                               \\\\
244     };                                                   \\\\
245     if(dir)                                              \\\\
246         s = length(s) == 0 ? s1 : sprintf("%s:%s",s1,s); \\\\
247     else                                                 \\\\
248         s = length(s1) == 0 ? s : sprintf("%s:%s",s,s1); \\\\
249     printf("%s\n",s);                                    \\\\
250     }'"'"
251 ### setenva ###
252 # appends directory or set of directories, separated by ':' (second parameter)
253 # to the variable (first parameter)
254 alias setenva 'set a=\!:1 ; set b=\!:2 ;  \\
255                set c=\`printenv \$a\` ;      \\
256                set b=\`cleandup \$c \$b 0\` ; \\
257                setenv \$a \$b ;             \\
258                unset a, b, c'
259
260 ### setenvp ###
261 # prepends directory or set of directories, separated by ':' (second parameter)
262 # to the variable (first parameter)
263 alias setenvp 'set a=\!:1 ; set b=\!:2 ;  \\
264                set c=\`printenv \$a\` ;      \\
265                set b=\`cleandup \$c \$b 1\` ; \\
266                setenv \$a \$b ;             \\
267                unset a, b, c'
268 ###########################################################################
269
270 EOF
271 cat ${install_work}/$CSHRC >> ${install_work}/_$CSHRC
272
273 # 4. put the contents of salome.sh to _salome.sh replacing export by export(a|p) aliases where necessary
274 sed -e 's%export\([[:blank:]]*\)PATH=\$[{]\?PATH[}]\?:\(.*\)%exporta\1PATH \2%g' -e 's%export\([[:blank:]]*\)PATH=\(.*\):\$[{]\?PATH[}]\?%exportp\1PATH \2%g' ${install_work}/$SHRC > ${INSTALL_WORK}/_tmp1 2>/dev/null
275 sed -e 's%export\([[:blank:]]*\)LD_LIBRARY_PATH=\$[{]\?LD_LIBRARY_PATH[}]\?:\(.*\)%exporta\1LD_LIBRARY_PATH \2%g' -e 's%export\([[:blank:]]*\)LD_LIBRARY_PATH=\(.*\):\$[{]\?LD_LIBRARY_PATH[}]\?%exportp\1LD_LIBRARY_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
276 sed -e 's%export\([[:blank:]]*\)PYTHONPATH=\$[{]\?PYTHONPATH[}]\?:\(.*\)%exporta\1PYTHONPATH \2%g' -e 's%export\([[:blank:]]*\)PYTHONPATH=\(.*\):\$[{]\?PYTHONPATH[}]\?%exportp\1PYTHONPATH \2%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
277 sed -e 's%export\([[:blank:]]*\)LD_RUN_PATH=\$[{]\?LD_RUN_PATH[}]\?:\(.*\)%exporta\1LD_RUN_PATH \2%g' -e 's%export\([[:blank:]]*\)LD_RUN_PATH=\(.*\):\$[{]\?LD_RUN_PATH[}]\?%exportp\1LD_RUN_PATH \2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2
278 cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/_$SHRC
279
280 # 5. Create a salome.csh file from salome.sh by replacing export by setenv, etc.
281 sed -e 's%export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${install_work}/$SHRC > ${INSTALL_WORK}/_tmp1 2>/dev/null
282 cat ${INSTALL_WORK}/_tmp1 >> ${install_work}/$CSHRC
283
284 # 6. Create a _salome.csh file from _salome.sh by replacing export by setenv, exporta by setenva, etc.
285 sed -e 's%export \([[:alnum:]_[:blank:]]*\)\=%setenv \1 %g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
286 sed -e 's%exporta \([[:alnum:]_[:blank:]]*\)%setenva \1%g'  ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
287 sed -e 's%exportp \([[:alnum:]_[:blank:]]*\)%setenvp \1%g'  ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
288 sed -e 's%if \[ -n "\${ENV_FOR_LAUNCH}" \] ; then%if ( ${?ENV_FOR_LAUNCH} ) then%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
289 sed -e 's%if \[ "\${ENV_FOR_LAUNCH}" == "1" \] ; then%if ( "\${ENV_FOR_LAUNCH}" == "1" ) then%g' ${INSTALL_WORK}/_tmp2 > ${INSTALL_WORK}/_tmp1 2>/dev/null
290 sed -e 's%^\([[:blank:]]*\)fi\([[:blank:]]*\)$%\1endif\2%g' ${INSTALL_WORK}/_tmp1 > ${INSTALL_WORK}/_tmp2 2>/dev/null
291 cat ${INSTALL_WORK}/_tmp2 >> ${install_work}/_$CSHRC 2>/dev/null
292
293 rm -f ${INSTALL_WORK}/_tmp1 ${INSTALL_WORK}/_tmp2
294
295 if [ $# -eq 4 ] ; then
296     product_dir=$4
297     if [ -e ${product_dir} ] ; then
298         cp -f ${install_work}/_$SHRC  ${product_dir}/$SHRC
299         cp -f ${install_work}/_$CSHRC ${product_dir}/$CSHRC
300     fi
301 fi
302 sed -e 's%setenv ENV_FOR_LAUNCH 1%setenv ENV_FOR_LAUNCH 0%' ${install_work}/_$CSHRC > ${install_root}/env_products.csh
303 sed -e 's%export ENV_FOR_LAUNCH=1%export ENV_FOR_LAUNCH=0%' ${install_work}/_$SHRC  > ${install_root}/env_products.sh
304 rm -f ${install_work}/_$SHRC
305 rm -f ${install_work}/_$CSHRC
306 ### !!! copying build.csh script
307 if [ -e ./build.csh ]; then
308     cp -f ./build.csh ${install_root}
309 fi
310 }
311
312 #try use already existing product
313 try_existing()
314 {
315 product_dir=$1;  install_root=$2;  install_work=$3;  product_sequence="$4";  product_type=$5
316 env_file=${product_dir}/env_${product_type}.sh
317 if [ -f ${env_file} ] ; then
318     cp -f ${env_file} ${install_work}
319     make_env ${install_root} ${install_work} "${product_sequence}"
320     source ${install_work}/$SHRC
321     check_version
322     if [ $? -eq 0 ] ; then
323         if [ -d ${product_dir} ] && [ -w ${product_dir} ] ; then
324             #makedir ${product_dir}
325             print_env
326             return 0    
327         fi
328     fi
329 fi
330 return 1;
331 }
332
333 #check existance of lib passed as first parameter
334 #return 0 if lib exists
335 #if you pass second parameter search will be done in it 
336 #otherwise search will be done in $LD_LIBRARY_PATH
337 #you should use : as dilimeter if you are going to pass second parameter
338 check_lib_version(){
339 if [ -n "$2" ]; then
340    whereIs=$2
341 else
342    whereIs=$LD_LIBRARY_PATH
343 fi
344 for L in `echo ${whereIs} | sed -e"s%:% %g"` ;  do
345     ret=`find $L -name $1 2>/dev/null`
346     if [ -n "$ret" ] ; then
347         #echo "The $1 exists on yours system in a $L folder"
348         return 0
349     fi
350 done
351 return 1
352 }
353
354 find_in_path(){
355 file=$1;  shift; 
356 path=$*
357 for i in `echo ${path} | sed -e"s%:% %g"` ; do 
358     ret=`find $i -name $file 2>/dev/null`
359     if [ -n "$ret" ] ; then
360         ret=`echo $ret | sed -e"s%/\$file$%%g"`
361         echo $ret
362         return 0
363     fi
364 done
365 return 1
366 }
367
368 sort_path(){
369 arg1=$1;  val1=$2;  arg2=$3;  val2=$4
370 tmp="^$val1$|^$val1:|:$val1$|:$val1:"
371 #echo $val2 | grep -E "$tmp" >/dev/null 2>&1
372 #if [ $? -eq 0 ] ; then 
373 #    echo "$arg2"
374 #    return
375 #fi
376 to_tail=1
377 exclude_list="usr lib bin sbin etc"
378 for i in ${exclude_list} ; do
379     tmp="^/$i/|^/$i\$"
380     echo ${val1} | grep -E "$tmp" >/dev/null 2>&1
381     if [ $? == 0 ] ; then to_tail=0; break; fi
382 done
383 if [ $to_tail -eq 0 ] ; then
384     echo $arg2:$arg1
385 else
386     echo $arg1:$arg2
387 fi
388 return $to_tail
389 }
390
391 where_tcl(){
392 if test -z "${TCLHOME}"; then
393     TCLHOME=/usr
394 fi
395 tclcfg=`find ${TCLHOME}/lib -name "tclConfig.sh" 2> /dev/null`
396 file=""
397 maxver=0
398 for f in $tclcfg; do
399     ver=`cat $f | grep -e "TCL_VERSION=.*" | sed -e "s%TCL_VERSION=[\'|\"]\(.*\)[\'|\"]%\1%g" | awk -F. '{x=0;for(i=1;i<=3;i++){x=x*100;if(i<=NF)x+=$i;}print x;}'`
400     if [ $maxver -lt $ver ]; then
401         maxver=$ver
402         file=$f
403     fi
404 done
405 if test -n "$file"; then
406     echo `dirname $file`
407     return 0
408 else
409     echo ""
410     return 1
411 fi
412 }
413
414 where_tk(){
415 if test -z "${TCLHOME}"; then
416     TCLHOME=/usr
417 fi
418 tclcfg=`find ${TCLHOME}/lib -name "tkConfig.sh" 2> /dev/null`
419 file=""
420 maxver=0
421 for f in $tclcfg; do
422     ver=`cat $f | grep -e "TK_VERSION=.*" | sed -e "s%TK_VERSION=[\'|\"]\(.*\)[\'|\"]%\1%g" | awk -F. '{x=0;for(i=1;i<=3;i++){x=x*100;if(i<=NF)x+=$i;}print x;}'`
423     if [ $maxver -lt $ver ]; then
424         maxver=$ver
425         file=$f
426     fi
427 done
428 if test -n "$file"; then
429     echo `dirname $file`
430     return 0
431 else
432     echo ""
433     return 1
434 fi
435 }