Salome HOME
update copyright notes (2007)
[tools/install.git] / config_files / 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-2007 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 (optionally)"
25     echo "            make install commands"
26     echo ""
27     echo "Usage:"
28     echo "            build.csh [ <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 "-d          Delete build directories before calling configure, to enforce full"
42     echo "            rebuild (and reinstall if -i or -p option is used) of module."
43     echo "            Use this option carefully."
44     echo ""
45     echo "-o          Build sources in optimized mode. Default is off that means"
46     echo "            debug mode."
47     echo "            Use this option together with -c or -b."
48     echo "            This option can require -d option to be set (to ensure that"
49     echo "            previously created build directories are removed to enforce"
50     echo "            rebuild in optimized mode)."
51     echo ""
52     echo "-t          Performs make dev_docs step to build TUI documentation for those"
53     echo "            modules which support this. Default is off."
54     echo ""
55     echo "-i          Performs make install step. Default is off that means only make"
56     echo "            step."
57     echo ""
58     echo "-p <prefix> Define the directory where to install modules after compilation."
59     echo "            By default the directory where compilation is performed is used."
60     echo ""
61     echo "-v <level>  Verbose level (0-2, default 2): print information on build status:"
62     echo "            0: only 'make' errors"
63     echo "            1: 0 + compiler and other errors (build_configure, configure)"
64     echo "            2: 1 + compiler and other warnings "
65     echo ""
66     echo "-h          Prints this help information."
67     echo ""
68     echo "Note:       If no keys are given script just performs make step."
69     echo ""
70     echo "Example:"
71     echo "            ./build.csh -o -i -b KERNEL MED GEOM"
72     echo ""
73     echo "            This will make KERNEL, GEOM and MED modules: build_configure,"
74     echo "            configure, make and install procedures will be performed for all"
75     echo "            specified modules. The modules will be built in the optimized mode."
76     echo ""
77     exit 1
78 }
79
80 is_build_configure=0
81 is_configure=0
82 is_delete=0
83 verbose_level=2
84 params=""
85 b_params=""
86 modules="KERNEL GUI GEOM MED SMESH VISU SUPERV NETGENPLUGIN GHS3DPLUGIN COMPONENT PYCALCULATOR CALCULATOR HELLO PYHELLO LIGHT"
87 optim=""
88 is_install=0
89 is_tui=0
90 prefix=""
91
92 #########################################################
93 # parse parameters
94 #########################################################
95 while getopts ":hbcdoitv:p:" option ; do
96     case $option in
97         h ) usage ;;
98         b ) is_build_configure=1 ; is_configure=1 ;;
99         c ) is_configure=1 ;;
100         d ) is_delete=1 ;;
101         o ) optim="--enable-production=yes --disable-debug" ;;
102         i ) is_install=1 ;;
103         t ) is_tui=1 ;;
104         v ) verbose_level=$OPTARG ;;
105         p ) is_install=1 ; prefix=$OPTARG ;;
106         ? ) usage ;;
107     esac
108 done
109 # shift to have the good number of other args
110 shift $((OPTIND - 1))
111
112 for arg in $@ ; do
113     known=0
114     for m in $modules ; do
115         if [ "$m" == "$arg" ] ; then known=1 ; break ; fi
116     done
117     if [ $known -eq 1 ] ; then
118         params="$params $arg"
119     else
120         b_params="$b_params $arg"
121         echo
122         echo '!!! Warning !!! Unknown module: '$arg'. Skipping.'
123     fi
124 done
125
126 #########################################################
127 # sort modules in proper order according to the dependancies
128 #########################################################
129 if [ "${params}" != "" ] ; then
130     xparams=""
131     for module in ${modules} ; do
132         found=0
133         for p in $params ; do
134             if [ "$p" == "$module" ] ; then found=1 ; break ; fi
135         done
136         if [ $found -eq 1 ] ; then xparams="$xparams $module" ; fi
137     done
138     modules="$xparams"
139 elif [ "${b_params}" != "" ] ; then
140     echo
141     echo "Nothing to be built. Exiting."
142     echo
143     exit 1
144 fi
145
146 echo
147 echo ">>> The following SALOME packages will be built:"
148 echo $modules
149
150 #########################################################
151 # set environment
152 #########################################################
153 env_script=`dirname $0`/env_build.sh
154 if [ ! -e $env_script ] ; then
155     env_script=`dirname $0`/env_products.sh
156 fi
157 if [ -e $env_script ] ; then
158     echo
159     echo ">>> Setting environment"
160     source $env_script
161 else
162     echo
163     echo '!!! Warning !!! Environment is not set.'
164     echo 'Neither env_build.sh nor env_products.sh file is found.'
165     echo 'Proceed with the current environment.'
166 fi
167
168 BUILD_DIR=${PWD}
169 LOG_DIR=${BUILD_DIR}/LOGS
170
171 #########################################################
172 # define installation prefix
173 #########################################################
174 if [ "$prefix" == "" ] ; then
175     prefix=${BUILD_DIR}/INSTALL
176     if [ $is_install -eq 1 ] ; then
177         echo
178         echo '!!! Warning !!! Installation directory is not set.'
179         echo "All the modules will be installed to the $prefix"
180     elif [ "`echo ${modules} | grep KERNEL`" != "" ] ; then
181         echo
182         echo '!!! Warning !!! KERNEL module requires install step to be performed.'
183         echo "For this module -i option will be forced."
184         echo "The module(s) will be installed to the $prefix"
185     fi
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         add_keys=""
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 ] ; 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 ] ; then
247                 echo '!!! Warning !!! Cannot find build_configure script in '${module_src}'.'
248             else
249                 #########################################################
250                 # call build_configure
251                 #########################################################
252                 cd ${module_src}
253                 ./build_configure >& ${LOG_DIR}/build_configure_${module}.log
254                 #########################################################
255                 # echo possible errors
256                 #########################################################
257                 if [ $verbose_level -gt 0 ] ; then
258                     cat ${LOG_DIR}/build_configure_${module}.log | grep -E "(failed|: error:)" > /dev/stderr
259                 fi
260             fi
261         fi
262         #########################################################
263         # deleting build directory if -d flag is given
264         #########################################################
265         if [ -e ${module_build} ] && [ $is_delete -eq 1 ] ; then
266             echo "... Removing ${module_build}"
267             rm -rf ${module_build}
268         fi
269         #########################################################
270         # creating build directory if it does not exist
271         #########################################################
272         if [ ! -e ${module_build} ] ; then
273             mkdir -p ${module_build}
274         fi
275         cd ${module_build}
276         #########################################################
277         # check if top Makefile exists in build directory, i.e. 
278         # is it necessary to run configure script
279         #########################################################
280         mkfile_exist=0
281         if [ -e ${module_build}/Makefile ] ; then
282             mkfile_exist=1
283         fi
284         #########################################################
285         # check if configure options are changed from the 
286         # previous launch
287         #########################################################
288         opts_changed=0
289 ##      if [ -f ${module_build}/config.log ] ; then
290 ##          old_prefix=`grep -e '^prefix=' ${module_build}/config.log | sed -e "s%^prefix='\(.*\)'%\1%"`
291 ##          if [ "$old_prefix" != "$prefix" ] ; then
292 ##              opts_changed=1
293 ##          fi
294 ##      fi
295         #########################################################
296         # define installation directory (by using module's version
297         # number); default is a build directory
298         #########################################################
299         vx=""
300         cfg_file=configure.ac
301         if [ ! -e ${module_src}/${cfg_file} ] ; then
302             cfg_file=configure.in.base
303         fi
304         if [ -e ${module_src}/${cfg_file} ] ; then
305             vx=`grep -e "^VERSION=" ${module_src}/${cfg_file} | awk -F= '{ if (NF>1) print $NF; }' | tr -d '[:space:]'`
306         fi
307         px=${BUILD_DIR}/${module}
308         if [ "$prefix" != "" ] ; then
309             px=$prefix/${module}
310         fi
311         if [ "$vx" != "" ] ; then
312             px="$px"_"$vx"
313         fi
314         #########################################################
315         # perform configure if -c flag is given or if 
316         # Makefile does not exist (first compilation?)
317         #########################################################
318         if [ $is_configure -eq 1 ] || [ $mkfile_exist -eq 0 ] || [ $opts_changed -eq 1 ] ; then
319             echo "... Performing configure"
320             #########################################################
321             # check if configure script exists
322             #########################################################
323             if [ ! -e ${module_src}/configure ] ; then
324                 echo
325                 echo '!!! Warning !!! Can not find configure script in '${module_src}'.'
326                 echo
327             else
328                 if [  "${module}" == "NETGENPLUGIN" ] && [ "$NETGENHOME" != "" ] ; then
329                     add_keys="--with-netgen=${NETGENHOME}"
330                 fi
331                 ${module_src}/configure --prefix=$px ${optim} ${add_keys} >& ${LOG_DIR}/configure_${module}.log
332                 #########################################################
333                 # echo possible errors
334                 #########################################################
335                 if [ $verbose_level -gt 0 ] ; then
336                     cat ${LOG_DIR}/configure_${module}.log | grep ": error:" > /dev/stderr
337                 fi
338                 if [ $verbose_level -gt 1 ] ; then
339                     cat ${LOG_DIR}/configure_${module}.log | grep ": WARNING:" > /dev/stderr
340                 fi
341             fi
342         fi
343         #########################################################
344         # perform make
345         #########################################################
346         echo "... Performing make"
347         #########################################################
348         # first clear dependencies
349         #########################################################
350         find . -name ".dep*" -type f -exec rm -f {} \; >& /dev/null
351         make >& ${LOG_DIR}/make_${module}.log
352         sts=$?
353         #########################################################
354         # if make step is successful set the ${module}_ROOT_DIR
355         # environment variable
356         #########################################################
357         if [ $sts -eq 0 ] ; then
358             export ${module}_ROOT_DIR=${module_build}
359         fi
360         #########################################################
361         # print make errors
362         #########################################################
363         cat ${LOG_DIR}/make_${module}.log | grep '[*][*][*]' > /dev/stderr
364         #########################################################
365         # print compiler errors and warnings
366         # Note: here awk is used to concatenate together several lines of single
367         #       error or warning message produced by stupid gnu compiler
368         # Actually, instead of that we could use 'fmessage length=n' option of g++...
369         #########################################################
370         if [ $verbose_level -gt 0 ] ; then
371             ###cat ${LOG_DIR}/make_${module}.log | grep ": error"
372             cat ${LOG_DIR}/make_${module}.log | awk 'substr($0,0,1)!=" " {print ""} {printf $0} END {print ""}' | grep "${module_src}/[A-Za-z_0-9./]*:" | sed s@"^${module_src}/src/"@@ > /dev/stderr
373         fi
374         if [ $verbose_level -gt 1 ] ; then
375             cat ${LOG_DIR}/make_${module}.log | awk 'substr($0,0,1)!=" " {print ""} {printf $0} END {print ""}' | grep ": warning" | sed s@"^${module_src}/src/"@@ > /dev/stderr
376         fi
377         #########################################################
378         # perform make dev_docs step if -t flag is given
379         #########################################################
380         if [ $is_tui -eq 1 ] && [ $sts -eq 0 ] && [ -d ${module_build}/doc ] ; then
381             cd ${module_build}/doc
382             dd=`cat Makefile | grep -E "dev_docs[[:space:]]*:"`
383             if [ "$dd" != "" ] ; then
384                 echo "... Performing make dev_docs"
385                 make dev_docs >& ${LOG_DIR}/make_dev_docs_${module}.log
386                 cat ${LOG_DIR}/make_dev_docs_${module}.log | grep '[*][*][*]' > /dev/stderr
387             fi
388         fi
389         cd ${module_build}
390         #########################################################
391         # perform make if -i or -p flags are given
392         #########################################################
393         if [ $is_install -eq 1 ] && [ $sts -eq 0 ] || [ "${module}" == "KERNEL" ] ; then
394             #########################################################
395             # deleting build directory if -d flag is given
396             #########################################################
397             if [ -e $px ] && [ $is_delete -eq 1 ] ; then
398                 echo "... Removing $px"
399                 rm -rf $px
400             fi
401             echo "... Performing make install"
402             make install >& ${LOG_DIR}/make_install_${module}.log
403             sts=$?
404             #########################################################
405             # if make install step is successful set the 
406             # ${module}_ROOT_DIR environment variable
407             #########################################################
408             if [ $sts -eq 0 ] ; then
409                 export ${module}_ROOT_DIR=$px
410             fi
411         fi
412         echo ">>> Finished ${module} module"
413     else
414         echo
415         echo '!!! Error !!! Cannot find module sources. '${module}'_SRC_DIR environment variable is not set.'
416         echo
417     fi
418 done
419 #########################################################
420 # finalize
421 #########################################################
422 cd ${BUILD_DIR}
423
424 echo
425 echo "==========================================================================="
426 echo "SALOME build finished at `date`"
427 echo "==========================================================================="
428 echo