Salome HOME
935a10ca301a29bbd2c8151b293e0ab6c6a302a4
[tools/simanio.git] / scripts / build.sh
1 #!/bin/bash -noprofile
2
3 ####################################################################################
4 #  File      : build.sh
5 #  Created   : Thu Jan 27 09:50:55 2005
6 #  Author    : Vadim SANDLER, Open CASCADE SAS (vadim.sandler@opencascade.com)
7 #  Project   : SALOME
8 #  Module    : Installation Wizard
9 #  Copyright : 2002-2012 CEA
10 #
11 #  This script is the part of the SALOME installation procedure.
12 #
13 #  This script can be used to build and install SALOME modules from sources.
14 #  Try build.sh -h for more details about usage.
15 #
16 ####################################################################################
17
18 ###############################################################
19 # Prints usage information and exits
20 ###############################################################
21 usage(){
22     echo ""
23     echo "Description:"
24     echo "            Builds given SALOME modules by performing make and "
25     echo "            make install commands"
26     echo ""
27     echo "Usage:"
28     echo "            build.sh [ <option> ] ... [ <module> [ <module> ... ] ]"
29     echo ""
30     echo "<module>    Modules to build, separated by space(s)."
31     echo "            If no modules are given - all SALOME modules are assumed."
32     echo "            Note, that modules given in command lines are automatically"
33     echo "            sorted in such order to support correct modules dependencies."
34     echo ""
35     echo "Options:"
36     echo "-b          Perform build_configure command for all given modules."
37     echo "            This option forces configure (-c) key to be set. Default is off."
38     echo ""
39     echo "-c          Perform configure command for all given modules. Default is off."
40     echo ""
41     echo "-w          Add --without-gui key to build_configure and configure commands"
42     echo "            to build SALOME module sources without GUI."
43     echo "            By default sources are built with GUI."
44     echo ""
45     echo "-d          Delete build directories before calling configure, to force full"
46     echo "            rebuild and reinstall of the module."
47     echo "            Use this option carefully."
48     echo ""
49     echo "-o          Build sources in optimized mode. Default is off that means"
50     echo "            debug mode."
51     echo "            Use this option together with -c or -b."
52     echo "            This option can require -d option to be set (to ensure that"
53     echo "            previously created build directories are removed to enforce"
54     echo "            rebuild in optimized mode)."
55     echo ""
56     echo "-t          Performs make dev_docs step to build TUI documentation for those"
57     echo "            modules which support this. Default is off."
58     echo ""
59     echo "-p <prefix> Define the directory where to install modules after compilation."
60     echo "            By default the directory where compilation is performed is used."
61     echo ""
62     echo "-v <level>  Verbose level (0-2, default 2): print information on build status:"
63     echo "            0: only 'make' errors"
64     echo "            1: 0 + compiler and other errors (build_configure, configure)"
65     echo "            2: 1 + compiler and other warnings "
66     echo ""
67     echo "-h          Prints this help information."
68     echo ""
69     echo "Note:       If no keys are given script just performs make step."
70     echo ""
71     echo "Example:"
72     echo "            ./build.csh -o -p /home/user/salome -b KERNEL MED GEOM"
73     echo ""
74     echo "            This will make KERNEL, GEOM and MED modules: build_configure,"
75     echo "            configure, make and install procedures will be performed for all"
76     echo "            specified modules. The modules will be built in the optimized mode."
77     echo ""
78     exit 1
79 }
80
81 is_build_configure=0
82 is_configure=0
83 inst_with_gui=1
84 is_delete=0
85 verbose_level=2
86 params=""
87 modules="KERNEL GUI GEOM MED SMESH VISU PARAVIS YACS NETGENPLUGIN GHS3DPLUGIN BLSURFPLUGIN HexoticPLUGIN GHS3DPRLPLUGIN COMPONENT PYCALCULATOR CALCULATOR HELLO PYHELLO LIGHT PYLIGHT SIERPINSKY RANDOMIZER HEXABLOCK HEXABLOCKPLUGIN ATOMIC ATOMGEN ATOMSOLV HOMARD JOBMANAGER"
88 #modules="KERNEL GUI GEOM MED SMESH VISU PARAVIS YACS NETGENPLUGIN GHS3DPLUGIN BLSURFPLUGIN HexoticPLUGIN GHS3DPRLPLUGIN PYHELLO"
89 modules="KERNEL GUI GEOM MED SMESH PYHELLO"
90 optim=""
91 is_install=1
92 is_tui=0
93 prefix=""
94 def_install_dir="/INSTALL"
95
96 #########################################################
97 # parse parameters
98 #########################################################
99 while getopts ":hbcwdotv:p:" option ; do
100     case $option in
101         h ) usage ;;
102         b ) is_build_configure=1 ; is_configure=1 ;;
103         c ) is_configure=1 ;;
104         w ) inst_with_gui=0 ;;
105         d ) is_delete=1 ;;
106         o ) optim="--enable-production=yes --disable-debug" ;;
107         t ) is_tui=1 ;;
108         v ) verbose_level=$OPTARG ;;
109         p ) is_install=1 ; prefix=$OPTARG ;;
110         ? ) usage ;;
111     esac
112 done
113 # shift to have the good number of other args
114 shift $((OPTIND - 1))
115
116 b_params=""
117
118 for arg in $@ ; do
119     known=0
120     for m in $modules ; do
121         if [ "$m" == "$arg" ] ; then known=1 ; break ; fi
122     done
123     if [ $known -eq 0 ] ; then
124         echo
125         echo '!!! Warning !!! Unknown module: '$arg'.'
126         b_params="$b_params $arg"
127     else
128         params="$params $arg"
129     fi
130 done
131
132 #########################################################
133 # sort modules in proper order according to the dependancies
134 #########################################################
135 if [ "${params}" != "" ] ; then
136     xparams=""
137     for module in ${modules} ; do
138         found=0
139         for p in $params ; do
140             if [ "$p" == "$module" ] ; then found=1 ; break ; fi
141         done
142         if [ $found -eq 1 ] ; then 
143             xparams="$xparams $module"
144         fi
145     done
146     modules="$xparams $b_params"
147 elif [ "${b_params}" != "" ] ; then
148     modules="$b_params"
149 fi
150
151 echo
152 echo ">>> The following modules will be built:"
153 echo $modules
154 echo
155
156 #########################################################
157 # set environment
158 #########################################################
159 env_script=`dirname $0`/env_build.sh
160 if [ ! -e $env_script ] ; then
161     env_script=`dirname $0`/env_products.sh
162 fi
163 if [ ! -e $env_script ] ; then
164     env_script=`dirname $0`/env.sh
165 fi
166 if [ -e $env_script ] ; then
167     echo
168     echo ">>> Setting environment"
169     source $env_script
170 else
171     echo
172     echo '!!! Warning !!! Environment is not set.'
173     echo 'No environment file (env_build.sh, env_products.sh, env.sh) is found.'
174     echo 'Proceed with the current environment.'
175 fi
176
177 BUILD_DIR=${PWD}
178 LOG_DIR=${BUILD_DIR}/LOGS
179
180 #########################################################
181 # define installation prefix
182 #########################################################
183 if [ "$prefix" == "" ] ; then
184     echo
185     echo '!!! Warning !!! Installation directory is not set.'
186     echo "All the modules will be installed to the ${BUILD_DIR}${def_install_dir}"
187 else
188     if [ "`echo $prefix | grep -E '^/'`" == "" ] ; then
189         prefix=${BUILD_DIR}/$prefix
190         echo
191         echo '!!! Warning !!! Relative prefix is used.'
192         echo "All the modules will be installed to the $prefix."
193     fi
194 fi
195
196 #########################################################
197 # create log directory
198 #########################################################
199 if [ ! -e ${LOG_DIR} ] ; then
200     mkdir -p ${LOG_DIR}
201 fi
202
203 echo
204 echo "==========================================================================="
205 echo "Starting SALOME build at `date`"
206 echo "==========================================================================="
207 echo
208
209 #########################################################
210 # loop for all given modules
211 #########################################################
212 for module in ${modules}; do
213     echo ">>> Processing ${module} module"
214
215     module_src=`printenv ${module}_SRC_DIR`
216     module_build=${BUILD_DIR}/${module}_BUILD
217     
218     if [ "${module_src}" != "" ] ; then
219         cd ${BUILD_DIR}
220         #########################################################
221         # check if sources directory exists
222         #########################################################
223         if [ ! -e ${module_src} ] ; then
224             echo
225             echo '!!! Error !!! Cannot find module sources directory:'
226             echo "${module_src} does not exist."
227             echo
228             continue
229         fi
230         #########################################################
231         # check if configure script exists
232         #########################################################
233         cfg_exist=0
234         if [ -e ${module_src}/configure ] || [ -e ${module_src}/CMakeLists.txt ] ; then
235             cfg_exist=1
236         fi
237         #########################################################
238         # perform build_configure if -b flag is given or if 
239         # configure script does not exist (first compilation?)
240         #########################################################
241         if [ $is_build_configure -eq 1 ] || [ $cfg_exist -eq 0 ] ; then
242             echo "... Performing build_configure"
243             #########################################################
244             # check if build_configure script exists
245             #########################################################
246             if [ ! -e ${module_src}/build_configure ] && [ "${module}" != "PARAVIS" ] ; then
247                 echo '!!! Warning !!! Cannot find build_configure script in '${module_src}'.'
248             else
249                 if [ "${module}" == "PARAVIS" ]; then
250                     echo '!!! Warning !!! build_configure step is not executed for '${module}'.'                    
251                 else
252                     #########################################################
253                     # call build_configure
254                     #########################################################
255                     mybuildconf_keys="${BUILDCONF_OPTS}"
256                     if [ $inst_with_gui -eq 0 ] ; then
257                         mybuildconf_keys="${mybuildconf_keys} --without-gui"
258                     fi
259                     cd ${module_src}
260                     ./build_configure ${mybuildconf_keys} >& ${LOG_DIR}/build_configure_${module}.log
261                     #########################################################
262                     # echo possible errors
263                     #########################################################
264                     if [ $verbose_level -gt 0 ] ; then
265                         cat ${LOG_DIR}/build_configure_${module}.log | grep -E "(failed|: error:)" >&2
266                     fi
267                 fi
268             fi
269         fi
270         #########################################################
271         # deleting build directory if -d flag is given
272         #########################################################
273         if [ -e ${module_build} ] && [ $is_delete -eq 1 ] ; then
274             echo "... Removing ${module_build}"
275             chmod -R +w ${module_build} && rm -rf ${module_build} >& /dev/null
276             if [ "$?" != "0" ] ; then
277                 echo
278                 echo '!!! Warning !!! Cannot remove build directory ${module_build}. Permission denied.'
279                 echo
280             fi
281         fi
282         #########################################################
283         # creating build directory if it does not exist
284         #########################################################
285         if [ ! -e ${module_build} ] ; then
286             mkdir -p ${module_build} >& /dev/null
287             if [ "$?" != "0" ] ; then
288                 echo
289                 echo '!!! Error !!! Cannot create build directory ${module_build}. Permission denied.'
290                 echo
291                 continue
292             fi
293         fi
294         cd ${module_build}
295         #########################################################
296         # check if top Makefile exists in build directory, i.e. 
297         # is it necessary to run configure script
298         #########################################################
299         mkfile_exist=0
300         if [ -e ${module_build}/Makefile ] ; then
301             mkfile_exist=1
302         fi
303         #########################################################
304         # check if configure options are changed from the 
305         # previous launch
306         #########################################################
307         opts_changed=0
308         #########################################################
309         # define installation directory (by using module's version
310         # number); default is a build directory
311         #########################################################
312         vx=""
313         if [ -e ${module_src}/configure.ac ] ; then
314             vx=`grep -e "^AC_INIT" ${module_src}/configure.ac | sed -e "s%.*\[\([[:digit:].]*\)\].*%\1%g"`
315         elif [ -e ${module_src}/configure.in.base ] ; then
316             vx=`grep -e "^VERSION=" ${module_src}/configure.in.base | awk -F= '{ if (NF>1) print $NF; }' | tr -d '[:space:]'`
317             if [ "$vx" == "" ] ; then
318                 vx=`grep -e "^AC_INIT" ${module_src}/configure.in.base | sed -e "s%.*\[\([[:digit:].]*\)\].*%\1%g"`
319             fi
320         elif [ -e ${module_src}/CMakeLists.txt ] ; then
321             ver_maj=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MAJOR | sed -e "s%[A-Z_() ]%%g"`
322             ver_min=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MINOR | sed -e "s%[A-Z_() ]%%g"`
323             ver_maintenance=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MAINTENANCE | sed -e "s%[A-Z_() ]%%g"`
324             vx="${ver_maj}.${ver_min}.${ver_maintenance}"
325         fi
326         if [ "$vx" != "" ] ; then
327             vx="_$vx"
328         fi
329         if [ "$prefix" == "" ] ; then
330             px=${BUILD_DIR}${def_install_dir}/${module}${vx}
331         else
332             px=$prefix/${module}${vx}
333         fi
334         #########################################################
335         # perform configure if -c flag is given or if 
336         # Makefile does not exist (first compilation?)
337         #########################################################
338         if [ $is_configure -eq 1 ] || [ $mkfile_exist -eq 0 ] || [ $opts_changed -eq 1 ] ; then
339             echo "... Performing configure"
340             #########################################################
341             # check if configure script exists
342             #########################################################
343             if [ ! -e ${module_src}/configure ] && [ "${module}" != "PARAVIS" ] ; then
344                 echo
345                 echo '!!! Warning !!! Can not find configure script in '${module_src}'.'
346                 echo
347             else
348                 myconf_keys="${CONF_OPTS}"
349                 if [ $inst_with_gui -eq 0 ] ; then
350                     myconf_keys="${myconf_keys} --without-gui"
351                 fi
352                 if [ "${module}" == "MED" ] && [ "${METISDIR}" != "" ] && [ "${SCOTCHDIR}" != "" ] ; then
353                     myconf_keys="${myconf_keys} --enable-splitter=yes --with-metis=${METISDIR} --with-scotch=${SCOTCHDIR}"
354                 fi
355                 if [ "${module}" == "NETGENPLUGIN" ] && [ "$NETGENHOME" != "" ] ; then
356                     myconf_keys="${myconf_keys} --with-netgen=${NETGENHOME}"
357                 fi
358
359                 if [ "${module}" == "PARAVIS" ] ; then
360                     cd ${module_build}; cmake ${module_src} -DCMAKE_INSTALL_PREFIX=$px >& ${LOG_DIR}/configure_${module}.log
361                 else
362                     ${module_src}/configure --prefix=$px ${optim} ${myconf_keys} >& ${LOG_DIR}/configure_${module}.log
363                 fi
364
365                 #########################################################
366                 # echo possible errors
367                 #########################################################
368                 if [ $verbose_level -gt 0 ] ; then
369                     cat ${LOG_DIR}/configure_${module}.log | grep ": error:" >&2
370                 fi
371                 if [ $verbose_level -gt 1 ] ; then
372                     cat ${LOG_DIR}/configure_${module}.log | grep ": WARNING:" >&2
373                 fi
374             fi
375         fi
376         #########################################################
377         # perform make
378         #########################################################
379         echo "... Performing make"
380         #########################################################
381         # first clear dependencies
382         #########################################################
383         find . -name ".dep*" -type f -exec rm -f {} \; >& /dev/null
384         #########################################################
385         # then make
386         #########################################################
387         if [ "${module}" == "PARAVIS" ] ; then
388             VTK_AUTOLOAD_PATH_BACKUP=${VTK_AUTOLOAD_PATH}
389             test "${VTK_AUTOLOAD_PATH}" != "" && unset VTK_AUTOLOAD_PATH
390         fi
391         make >& ${LOG_DIR}/make_${module}.log
392         sts=$?
393         if [ "${module}" == "PARAVIS" ] ; then
394             test "${VTK_AUTOLOAD_PATH_BACKUP}" != "" && export VTK_AUTOLOAD_PATH=${VTK_AUTOLOAD_PATH_BACKUP}
395         fi
396         #########################################################
397         # if make step is successful set the ${module}_ROOT_DIR
398         # environment variable
399         #########################################################
400         #if [ $sts -eq 0 ] ; then
401         #    export ${module}_ROOT_DIR=${module_build}
402         #fi
403         #########################################################
404         # print make errors
405         #########################################################
406         cat ${LOG_DIR}/make_${module}.log | grep -Ei '[*][*][*].*error' >&2
407         #########################################################
408         # print compiler errors and warnings
409         # Note: here awk is used to concatenate together several lines of single
410         #       error or warning message produced by stupid gnu compiler
411         # Actually, instead of that we could use 'fmessage length=n' option of g++...
412         #########################################################
413         if [ $verbose_level -gt 0 ] ; then
414             ###cat ${LOG_DIR}/make_${module}.log | grep ": error"
415             cat ${LOG_DIR}/make_${module}.log | awk 'substr($0,0,1)!=" " {print ""} {print $0} END {print ""}' | grep "${module_src}/[A-Za-z_0-9./]*:" | sed s@"^${module_src}/src/"@@ >&2
416         fi
417         if [ $verbose_level -gt 1 ] ; then
418             cat ${LOG_DIR}/make_${module}.log | awk 'substr($0,0,1)!=" " {print ""} {print $0} END {print ""}' | grep -E ": warning|* Warning" | sed s@"^${module_src}/src/"@@ >&2
419         fi
420         #########################################################
421         # perform make dev_docs step if -t flag is given
422         #########################################################
423         if [ $is_tui -eq 1 ] && [ $sts -eq 0 ] && [ -d ${module_build}/doc ] ; then
424             cd ${module_build}/doc
425             dd=`cat Makefile | grep -E "dev_docs[[:space:]]*:"`
426             if [ "$dd" != "" ] ; then
427                 echo "... Performing make dev_docs"
428                 make dev_docs >& ${LOG_DIR}/make_dev_docs_${module}.log
429                 cat ${LOG_DIR}/make_dev_docs_${module}.log | grep '[*][*][*]' >&2
430             fi
431         fi
432         cd ${module_build}
433         #########################################################
434         # perform make if -i or -p flags are given
435         #########################################################
436         if [ $is_install -eq 1 ] && [ $sts -eq 0 ] || [ "${module}" == "KERNEL" ] ; then
437             #########################################################
438             # deleting build directory if -d flag is given
439             #########################################################
440             if [ -e $px ] && [ $is_delete -eq 1 ] ; then
441                 echo "... Removing $px"
442                 rm -rf $px >& /dev/null
443                 if [ "$?" != "0" ] ; then
444                     echo
445                     echo '!!! Warning !!! Cannot remove install directory ${px}. Permission denied.'
446                     echo
447                 fi
448             fi
449             echo "... Performing make install"
450             make install >& ${LOG_DIR}/make_install_${module}.log
451             sts=$?
452             #########################################################
453             # if make install step is successful set the 
454             # ${module}_ROOT_DIR environment variable
455             #########################################################
456             if [ $sts -eq 0 ] ; then
457                 export ${module}_ROOT_DIR=$px
458             fi
459         fi
460         echo ">>> Finished ${module} module"
461     else
462         echo
463         echo '!!! Error !!! Cannot find module sources. '${module}'_SRC_DIR environment variable is not set.'
464         echo
465     fi
466 done
467 #########################################################
468 # finalize
469 #########################################################
470 cd ${BUILD_DIR}
471
472 echo
473 echo "==========================================================================="
474 echo "SALOME build finished at `date`"
475 echo "==========================================================================="
476 echo