Salome HOME
README update
[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 optim=""
90 is_install=1
91 is_tui=0
92 prefix=""
93 def_install_dir="/INSTALL"
94
95 #########################################################
96 # parse parameters
97 #########################################################
98 while getopts ":hbcwdotv:p:" option ; do
99     case $option in
100         h ) usage ;;
101         b ) is_build_configure=1 ; is_configure=1 ;;
102         c ) is_configure=1 ;;
103         w ) inst_with_gui=0 ;;
104         d ) is_delete=1 ;;
105         o ) optim="--enable-production=yes --disable-debug" ;;
106         t ) is_tui=1 ;;
107         v ) verbose_level=$OPTARG ;;
108         p ) is_install=1 ; prefix=$OPTARG ;;
109         ? ) usage ;;
110     esac
111 done
112 # shift to have the good number of other args
113 shift $((OPTIND - 1))
114
115 b_params=""
116
117 for arg in $@ ; do
118     known=0
119     for m in $modules ; do
120         if [ "$m" == "$arg" ] ; then known=1 ; break ; fi
121     done
122     if [ $known -eq 0 ] ; then
123         echo
124         echo '!!! Warning !!! Unknown module: '$arg'.'
125         b_params="$b_params $arg"
126     else
127         params="$params $arg"
128     fi
129 done
130
131 #########################################################
132 # sort modules in proper order according to the dependancies
133 #########################################################
134 if [ "${params}" != "" ] ; then
135     xparams=""
136     for module in ${modules} ; do
137         found=0
138         for p in $params ; do
139             if [ "$p" == "$module" ] ; then found=1 ; break ; fi
140         done
141         if [ $found -eq 1 ] ; then 
142             xparams="$xparams $module"
143         fi
144     done
145     modules="$xparams $b_params"
146 elif [ "${b_params}" != "" ] ; then
147     modules="$b_params"
148 fi
149
150 echo
151 echo ">>> The following modules will be built:"
152 echo $modules
153 echo
154
155 #########################################################
156 # set environment
157 #########################################################
158 env_script=`dirname $0`/env_build.sh
159 if [ ! -e $env_script ] ; then
160     env_script=`dirname $0`/env_products.sh
161 fi
162 if [ ! -e $env_script ] ; then
163     env_script=`dirname $0`/env.sh
164 fi
165 if [ -e $env_script ] ; then
166     echo
167     echo ">>> Setting environment"
168     source $env_script
169 else
170     echo
171     echo '!!! Warning !!! Environment is not set.'
172     echo 'No environment file (env_build.sh, env_products.sh, env.sh) is found.'
173     echo 'Proceed with the current environment.'
174 fi
175
176 BUILD_DIR=${PWD}
177 LOG_DIR=${BUILD_DIR}/LOGS
178
179 #########################################################
180 # define installation prefix
181 #########################################################
182 if [ "$prefix" == "" ] ; then
183     echo
184     echo '!!! Warning !!! Installation directory is not set.'
185     echo "All the modules will be installed to the ${BUILD_DIR}${def_install_dir}"
186 else
187     if [ "`echo $prefix | grep -E '^/'`" == "" ] ; then
188         prefix=${BUILD_DIR}/$prefix
189         echo
190         echo '!!! Warning !!! Relative prefix is used.'
191         echo "All the modules will be installed to the $prefix."
192     fi
193 fi
194
195 #########################################################
196 # create log directory
197 #########################################################
198 if [ ! -e ${LOG_DIR} ] ; then
199     mkdir -p ${LOG_DIR}
200 fi
201
202 echo
203 echo "==========================================================================="
204 echo "Starting SALOME build at `date`"
205 echo "==========================================================================="
206 echo
207
208 #########################################################
209 # loop for all given modules
210 #########################################################
211 for module in ${modules}; do
212     echo ">>> Processing ${module} module"
213
214     module_src=`printenv ${module}_SRC_DIR`
215     module_build=${BUILD_DIR}/${module}_BUILD
216     
217     if [ "${module_src}" != "" ] ; then
218         cd ${BUILD_DIR}
219         #########################################################
220         # check if sources directory exists
221         #########################################################
222         if [ ! -e ${module_src} ] ; then
223             echo
224             echo '!!! Error !!! Cannot find module sources directory:'
225             echo "${module_src} does not exist."
226             echo
227             continue
228         fi
229         #########################################################
230         # check if configure script exists
231         #########################################################
232         cfg_exist=0
233         if [ -e ${module_src}/configure ] || [ -e ${module_src}/CMakeLists.txt ] ; then
234             cfg_exist=1
235         fi
236         #########################################################
237         # perform build_configure if -b flag is given or if 
238         # configure script does not exist (first compilation?)
239         #########################################################
240         if [ $is_build_configure -eq 1 ] || [ $cfg_exist -eq 0 ] ; then
241             echo "... Performing build_configure"
242             #########################################################
243             # check if build_configure script exists
244             #########################################################
245             if [ ! -e ${module_src}/build_configure ] && [ "${module}" != "PARAVIS" ] ; then
246                 echo '!!! Warning !!! Cannot find build_configure script in '${module_src}'.'
247             else
248                 if [ "${module}" == "PARAVIS" ]; then
249                     echo '!!! Warning !!! build_configure step is not executed for '${module}'.'                    
250                 else
251                     #########################################################
252                     # call build_configure
253                     #########################################################
254                     mybuildconf_keys="${BUILDCONF_OPTS}"
255                     if [ $inst_with_gui -eq 0 ] ; then
256                         mybuildconf_keys="${mybuildconf_keys} --without-gui"
257                     fi
258                     cd ${module_src}
259                     ./build_configure ${mybuildconf_keys} >& ${LOG_DIR}/build_configure_${module}.log
260                     #########################################################
261                     # echo possible errors
262                     #########################################################
263                     if [ $verbose_level -gt 0 ] ; then
264                         cat ${LOG_DIR}/build_configure_${module}.log | grep -E "(failed|: error:)" >&2
265                     fi
266                 fi
267             fi
268         fi
269         #########################################################
270         # deleting build directory if -d flag is given
271         #########################################################
272         if [ -e ${module_build} ] && [ $is_delete -eq 1 ] ; then
273             echo "... Removing ${module_build}"
274             chmod -R +w ${module_build} && rm -rf ${module_build} >& /dev/null
275             if [ "$?" != "0" ] ; then
276                 echo
277                 echo '!!! Warning !!! Cannot remove build directory ${module_build}. Permission denied.'
278                 echo
279             fi
280         fi
281         #########################################################
282         # creating build directory if it does not exist
283         #########################################################
284         if [ ! -e ${module_build} ] ; then
285             mkdir -p ${module_build} >& /dev/null
286             if [ "$?" != "0" ] ; then
287                 echo
288                 echo '!!! Error !!! Cannot create build directory ${module_build}. Permission denied.'
289                 echo
290                 continue
291             fi
292         fi
293         cd ${module_build}
294         #########################################################
295         # check if top Makefile exists in build directory, i.e. 
296         # is it necessary to run configure script
297         #########################################################
298         mkfile_exist=0
299         if [ -e ${module_build}/Makefile ] ; then
300             mkfile_exist=1
301         fi
302         #########################################################
303         # check if configure options are changed from the 
304         # previous launch
305         #########################################################
306         opts_changed=0
307         #########################################################
308         # define installation directory (by using module's version
309         # number); default is a build directory
310         #########################################################
311         vx=""
312         if [ -e ${module_src}/configure.ac ] ; then
313             vx=`grep -e "^AC_INIT" ${module_src}/configure.ac | sed -e "s%.*\[\([[:digit:].]*\)\].*%\1%g"`
314         elif [ -e ${module_src}/configure.in.base ] ; then
315             vx=`grep -e "^VERSION=" ${module_src}/configure.in.base | awk -F= '{ if (NF>1) print $NF; }' | tr -d '[:space:]'`
316             if [ "$vx" == "" ] ; then
317                 vx=`grep -e "^AC_INIT" ${module_src}/configure.in.base | sed -e "s%.*\[\([[:digit:].]*\)\].*%\1%g"`
318             fi
319         elif [ -e ${module_src}/CMakeLists.txt ] ; then
320             ver_maj=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MAJOR | sed -e "s%[A-Z_() ]%%g"`
321             ver_min=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MINOR | sed -e "s%[A-Z_() ]%%g"`
322             ver_maintenance=`cat ${module_src}/CMakeLists.txt | grep SET\(VERSION_MAINTENANCE | sed -e "s%[A-Z_() ]%%g"`
323             vx="${ver_maj}.${ver_min}.${ver_maintenance}"
324         fi
325         if [ "$vx" != "" ] ; then
326             vx="_$vx"
327         fi
328         if [ "$prefix" == "" ] ; then
329             px=${BUILD_DIR}${def_install_dir}/${module}${vx}
330         else
331             px=$prefix/${module}${vx}
332         fi
333         #########################################################
334         # perform configure if -c flag is given or if 
335         # Makefile does not exist (first compilation?)
336         #########################################################
337         if [ $is_configure -eq 1 ] || [ $mkfile_exist -eq 0 ] || [ $opts_changed -eq 1 ] ; then
338             echo "... Performing configure"
339             #########################################################
340             # check if configure script exists
341             #########################################################
342             if [ ! -e ${module_src}/configure ] && [ "${module}" != "PARAVIS" ] ; then
343                 echo
344                 echo '!!! Warning !!! Can not find configure script in '${module_src}'.'
345                 echo
346             else
347                 myconf_keys="${CONF_OPTS}"
348                 if [ $inst_with_gui -eq 0 ] ; then
349                     myconf_keys="${myconf_keys} --without-gui"
350                 fi
351                 if [ "${module}" == "MED" ] && [ "${METISDIR}" != "" ] && [ "${SCOTCHDIR}" != "" ] ; then
352                     myconf_keys="${myconf_keys} --enable-splitter=yes --with-metis=${METISDIR} --with-scotch=${SCOTCHDIR}"
353                 fi
354                 if [ "${module}" == "NETGENPLUGIN" ] && [ "$NETGENHOME" != "" ] ; then
355                     myconf_keys="${myconf_keys} --with-netgen=${NETGENHOME}"
356                 fi
357
358                 if [ "${module}" == "PARAVIS" ] ; then
359                     cd ${module_build}; cmake ${module_src} -DCMAKE_INSTALL_PREFIX=$px >& ${LOG_DIR}/configure_${module}.log
360                 else
361                     ${module_src}/configure --prefix=$px ${optim} ${myconf_keys} >& ${LOG_DIR}/configure_${module}.log
362                 fi
363
364                 #########################################################
365                 # echo possible errors
366                 #########################################################
367                 if [ $verbose_level -gt 0 ] ; then
368                     cat ${LOG_DIR}/configure_${module}.log | grep ": error:" >&2
369                 fi
370                 if [ $verbose_level -gt 1 ] ; then
371                     cat ${LOG_DIR}/configure_${module}.log | grep ": WARNING:" >&2
372                 fi
373             fi
374         fi
375         #########################################################
376         # perform make
377         #########################################################
378         echo "... Performing make"
379         #########################################################
380         # first clear dependencies
381         #########################################################
382         find . -name ".dep*" -type f -exec rm -f {} \; >& /dev/null
383         #########################################################
384         # then make
385         #########################################################
386         if [ "${module}" == "PARAVIS" ] ; then
387             VTK_AUTOLOAD_PATH_BACKUP=${VTK_AUTOLOAD_PATH}
388             test "${VTK_AUTOLOAD_PATH}" != "" && unset VTK_AUTOLOAD_PATH
389         fi
390         make >& ${LOG_DIR}/make_${module}.log
391         sts=$?
392         if [ "${module}" == "PARAVIS" ] ; then
393             test "${VTK_AUTOLOAD_PATH_BACKUP}" != "" && export VTK_AUTOLOAD_PATH=${VTK_AUTOLOAD_PATH_BACKUP}
394         fi
395         #########################################################
396         # if make step is successful set the ${module}_ROOT_DIR
397         # environment variable
398         #########################################################
399         #if [ $sts -eq 0 ] ; then
400         #    export ${module}_ROOT_DIR=${module_build}
401         #fi
402         #########################################################
403         # print make errors
404         #########################################################
405         cat ${LOG_DIR}/make_${module}.log | grep -Ei '[*][*][*].*error' >&2
406         #########################################################
407         # print compiler errors and warnings
408         # Note: here awk is used to concatenate together several lines of single
409         #       error or warning message produced by stupid gnu compiler
410         # Actually, instead of that we could use 'fmessage length=n' option of g++...
411         #########################################################
412         if [ $verbose_level -gt 0 ] ; then
413             ###cat ${LOG_DIR}/make_${module}.log | grep ": error"
414             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
415         fi
416         if [ $verbose_level -gt 1 ] ; then
417             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
418         fi
419         #########################################################
420         # perform make dev_docs step if -t flag is given
421         #########################################################
422         if [ $is_tui -eq 1 ] && [ $sts -eq 0 ] && [ -d ${module_build}/doc ] ; then
423             cd ${module_build}/doc
424             dd=`cat Makefile | grep -E "dev_docs[[:space:]]*:"`
425             if [ "$dd" != "" ] ; then
426                 echo "... Performing make dev_docs"
427                 make dev_docs >& ${LOG_DIR}/make_dev_docs_${module}.log
428                 cat ${LOG_DIR}/make_dev_docs_${module}.log | grep '[*][*][*]' >&2
429             fi
430         fi
431         cd ${module_build}
432         #########################################################
433         # perform make if -i or -p flags are given
434         #########################################################
435         if [ $is_install -eq 1 ] && [ $sts -eq 0 ] || [ "${module}" == "KERNEL" ] ; then
436             #########################################################
437             # deleting build directory if -d flag is given
438             #########################################################
439             if [ -e $px ] && [ $is_delete -eq 1 ] ; then
440                 echo "... Removing $px"
441                 rm -rf $px >& /dev/null
442                 if [ "$?" != "0" ] ; then
443                     echo
444                     echo '!!! Warning !!! Cannot remove install directory ${px}. Permission denied.'
445                     echo
446                 fi
447             fi
448             echo "... Performing make install"
449             make install >& ${LOG_DIR}/make_install_${module}.log
450             sts=$?
451             #########################################################
452             # if make install step is successful set the 
453             # ${module}_ROOT_DIR environment variable
454             #########################################################
455             if [ $sts -eq 0 ] ; then
456                 export ${module}_ROOT_DIR=$px
457             fi
458         fi
459         echo ">>> Finished ${module} module"
460     else
461         echo
462         echo '!!! Error !!! Cannot find module sources. '${module}'_SRC_DIR environment variable is not set.'
463         echo
464     fi
465 done
466 #########################################################
467 # finalize
468 #########################################################
469 cd ${BUILD_DIR}
470
471 echo
472 echo "==========================================================================="
473 echo "SALOME build finished at `date`"
474 echo "==========================================================================="
475 echo