Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[tools/hxx2salome.git] / scripts / SA_build
1 #!/bin/bash
2 #
3 # Utility for building Salome Modules
4 #
5 # default directory containing sources. Look-up is performed first localy, then in this directory if present.
6 SRC_ROOT_DIR=
7 # default directory where you want to put build & install directories. alternative for -i option
8 INSTALL_ROOT_DIR=
9 # default building environment file. alternative to --environ= option
10 ENVIRON_FILE=
11 # Utilitaire de compilation Salome
12 #
13
14 usage()
15 {
16     echo
17     echo Name : SA_build  -  build Salome modules
18     echo
19     echo "Description : Small utility that automate Salome components building."
20     echo "               -> call build_configure in source directories"
21     echo "               -> create build and install directories"
22     echo "               -> call configure, make, make install from build directories"
23     echo "               -> update environment file (only sh syntax is supported)"
24     echo
25     echo Usage :
26     echo
27     echo "  SA_build   [modulename(s)]  sourcename(s)  [ options ]"
28     echo
29     echo "     sourcename(s) are component's source directories and should end up by convention with _SRC"
30     echo "     modulename(s) are module directories that contain source directories"
31     echo
32     echo "    -> A look-up is done to find component's source directories - then building is done"
33     echo "        - modulenames are searched localy, then in SRC_ROOT_DIR"
34     echo "        - sourcename(s) are searched in modulename(s), or localy if no module name is specified"
35     echo "        - if no sourcename is entered, search for modulename_SRC"
36     echo
37     echo "Options :"
38     echo
39     echo "  --disable-debug : compilation without debug traces"
40     echo "  --batch         : compilation is done in batch mode - no prompting"
41     echo "  --environ=path  : set environment file to path (should be absolute)"
42     echo "                    this file will be updated with new environment variables. "
43     echo "                    it will also be sources if it has sh syntax"
44     echo "  --install=path  : set install directory where module building and install is done (should be absolute)"
45     echo "  --compile-only -c  : no building and configure - just compile by calling make && make install"
46     echo "  --local -l      : look for source directories localy - build and install localy"
47     echo "  --sources=src1,src2,...  : for specifying source directories that don't end up with _SRC"
48     echo 
49     echo
50     echo "Examples :"
51     echo
52     echo "  SA_build  KERNEL"
53     echo "    -> compilation of KERNEL/KERNEL_SRC"
54     echo "       build is done in KERNEL/KERNEL_BUILD"
55     echo "       install is done in KERNEL/KERNEL_INSTALL"
56     echo "       environment should be set up correctly before (option --environ not used)"
57     echo
58     echo "  SA_build  MED_SRC --install=/export/home/SALOME"
59     echo "    -> compilation of MED_SRC  (no module name was specified)"
60     echo "       build is done in /export/home/SALOME/MED_BUILD"
61     echo "       install is done in /export/home/SALOME/MED_INSTALL"
62     echo 
63     echo "  SA_build  --batch --disable-debug --environ=my_env_products.sh V_3_0_2 KERNEL_SRC GUI_SRC GEOM_SRC MED_SRC MESH_SRC VISU_SRC SUPERV_SRC"
64     echo "    -> compilation without prompting (--batch option) of KERNEL_SRC, GUI_SRC, GEOM_SRC, MED_SRC, MESH_SRC, VISU_SRC and SUPERV_SRC"
65     echo "       all sources are searched in V_3_0_2 directory "
66     echo "       environment is setup using my_env_products.sh file"
67     echo "       configure is done with --disable-debug option"
68     echo 
69     exit 1
70 }
71
72 ask_confirmation()
73 {
74        echo
75        echo $1
76        echo
77        read answer
78        case $answer in
79            y* | Y* | o* | O* )   ;;
80            *) exit 1
81        esac
82 }
83
84 check_dir()
85 {
86    if [ -d $1 ]
87    then
88       rm -rf $1/*
89       echo "remove $1 content"
90    else
91       mkdir -p $1
92       echo "create $1"
93    fi
94 }
95
96 set_environ()
97 {
98    if [ -f $1 ]
99    then
100        ENVIRON_FILE=$1
101    else
102        echo -e "\n\nError : environment file $1 NOT FOUND\n"
103        exit
104    fi
105 }
106
107 compile()
108 {
109     clear
110     echo "----------------- Compile ---------------------"
111     make 
112     if [ $? -eq 0 ]
113     then
114         echo -e "\n     [OK] Compile : ${module}/${SOURCE_NAME}_SRC" >> $CR
115         echo Compilation succeeded : we make install
116         echo
117         echo "----------------- Install ---------------------"
118         make install
119         if [ $? -eq 0 ]
120         then
121             echo -e "     [OK] Install : ${module}/${SOURCE_NAME}_SRC" >> $CR
122         else
123             echo -e "     [KO] Install : ${module}/${SOURCE_NAME}_SRC" >> $CR
124         fi    
125     else
126         echo Compilation failed
127         echo -e "\n     [KO] Compile : ${module}/${SOURCE_NAME}_SRC" >> $CR
128     fi
129 }
130
131 get_source_dir()
132 {
133     COMPILE_DIR=
134     SOURCE_DIRS=
135     if [ -d ${LOCAL_DIR}/${module} ]
136     then
137        COMPILE_DIR=${LOCAL_DIR}/${module}
138     elif [ -d ${SRC_ROOT_DIR}/${module} ]
139     then
140            COMPILE_DIR=${SRC_ROOT_DIR}/${module}
141     else
142        echo Sorry : ${module} not found!
143        usage
144     fi
145     
146     if [ -d ${COMPILE_DIR}/${module}_SRC ]
147     then
148         SOURCE_DIRS=${COMPILE_DIR}/${module}_SRC
149     fi
150
151     for src in $LISTE_SRC
152     do
153         if [ -d ${COMPILE_DIR}/${src} ]
154         then
155            SOURCE_DIRS="$SOURCE_DIRS ${COMPILE_DIR}/${src}"
156         fi
157     done
158
159 }
160
161 build_source_dir()
162 {
163    isrc=0 # compteur
164    for SOURCE_DIR in ${SOURCE_DIRS}
165    do
166        (( isrc += 1 ))
167        echo -e "\n${isrc})  Compilation dans $SOURCE_DIR \n"
168        src=`basename $SOURCE_DIR`
169        SOURCE_NAME=${src%_SRC}
170        echo " -> Repertoire Source  = ${SOURCE_DIR}"
171        echo " -> Component name     = ${SOURCE_NAME}"
172        echo " -> Repertoire Build   = ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD"
173        echo " -> Repertoire Install = ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL"
174        echo
175        echo
176        echo
177        if [ $COMPILE_ONLY -eq 1 ]
178        then
179            if [ $BATCH_MODE -eq 0 ]
180            then
181                ask_confirmation "Compile ?"
182            fi
183            cd ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
184            if [ $? -eq 0 ]
185            then
186                compile
187            else
188                echo "ERROR : build directory ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD  not found"
189                echo "        with -c option, build directory should already exist"
190                exit
191            fi    
192        else
193
194            if [ $BATCH_MODE -eq 0 ]
195            then
196                ask_confirmation "build configure and configure ?"
197            fi
198         #
199            cd ${SOURCE_DIR}
200            echo
201            echo "----------------- Build configure ---------------------"
202            ./build_configure
203            echo
204            echo "----------------- Configure ---------------------"
205            echo
206            # crée ou vide les répertoires
207            check_dir ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
208            check_dir ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL
209            cd ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
210            ${SOURCE_DIR}/configure ${OPTIONS_CONFIGURE} --prefix=${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL | tee ${TMP_FILE}
211            echo
212            echo "retour configure : $?"
213            echo
214            if [ $BATCH_MODE -eq 0 ]
215            then
216                ask_confirmation "Compile ?"
217            fi
218            export `echo ${SOURCE_NAME}`_ROOT_DIR=${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL
219            compile
220            cd $LOCAL_DIR
221            if [ $ENVIRON_FILE ]  &&  [ -f $ENVIRON_FILE ]
222            then
223                #  Mise à jour du script d'environnement
224                if [ $BATCH_MODE -eq 1 ]
225                then
226                    update_environ ${SOURCE_NAME}_ROOT_DIR ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL ${SOURCE_NAME}_SRC_DIR ${SOURCE_DIR}
227                else
228                    echo "Voulez vous mettre mettre à jour le script d'environnement  ${ENVIRON_FILE} ?"
229                    echo " (ajouter la variable ${SOURCE_NAME}_ROOT_DIR - une copie de l'ancien script est faite dans un .old)"
230                    echo
231                    read answer
232                    case $answer in
233                      y* | Y* | o* | O* )  update_environ ${SOURCE_NAME}_ROOT_DIR ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL ${SOURCE_NAME}_SRC_DIR ${SOURCE_DIR} ;;
234                      *) exit 1
235                    esac
236                fi
237            fi
238        fi
239    done    
240 }
241
242
243 # update environment scrip : add or replace ROOT_DIR and SRC_DIR variable for component
244 update_environ()
245 {
246     cp ${ENVIRON_FILE} ${ENVIRON_FILE}.old
247     if [ "${SHELL_EXT}" == "sh" ]
248     then
249         grep " $1" ${ENVIRON_FILE}.old > /dev/null
250         if [ $? -eq 0 ]
251         then
252            # if variable is already set, we replace old setting by new one
253            cat ${ENVIRON_FILE}.old | awk -v var_root_dir=$1 -v path=$2 -v src_root_dir=$3 -v src_dir=$4 '
254                $1=="export" && $2 ~ "^"var_root_dir"=" {print "#"$0; $0="export "var_root_dir "=" path}
255                $1=="export" && $2 ~ "^"src_root_dir"=" {print "#"$0; $0="export "src_root_dir "=" src_dir}
256                $1 ~ "^"var_root_dir"=" {print "#"$0; $0=var_root_dir "=" path}
257                $1 ~ "^"src_root_dir"=" {print "#"$0; $0=src_root_dir "=" src_dir}
258                { print $0}'  > ${ENVIRON_FILE}
259         else
260            echo -e "##\n#------ ${SOURCE_NAME}-Src ------\nexport $3=$4" >> ${ENVIRON_FILE}
261            echo -e "##\n#------ ${SOURCE_NAME}-Bin ------\nexport $1=$2" >> ${ENVIRON_FILE}
262            if [ -d $2/lib ] && [ ! -d $2/lib/salome ]
263            then
264                # for salome modules, do nothing
265                echo -e "export PYTHONPATH=\${${1}}/bin:\${${1}}/lib:\${PYTHONPATH}" >> ${ENVIRON_FILE}
266            fi
267         fi
268     else
269         grep " $1" ${ENVIRON_FILE}.old > /dev/null
270         if [ $? -eq 0 ]
271         then
272            # if variable is already set, we replace old setting by new one
273            cat ${ENVIRON_FILE}.old | awk -v var_root_dir=$1 -v path=$2 -v src_root_dir=$3 -v src_dir=$4 '
274                $1=="setenv" && $2 == var_root_dir {print "#"$0; $0="setenv "var_root_dir " " path}
275                $1=="setenv" && $2 == src_root_dir {print "#"$0; $0="setenv "src_root_dir " " src_dir}
276                { print $0}'  > ${ENVIRON_FILE}
277         else
278            echo -e "##\n#------ ${SOURCE_NAME}-Src ------\nsetenv $3 $4" >> ${ENVIRON_FILE}
279            echo -e "##\n#------ ${SOURCE_NAME}-Bin ------\nsetenv $1 $2" >> ${ENVIRON_FILE}
280            if [ -d $2/lib ] && [ ! -d $2/lib/salome ]
281            then
282                echo -e "setenv PYTHONPATH \${${1}}/bin:\${${1}}/lib:\${PYTHONPATH}" >> ${ENVIRON_FILE}
283            fi
284
285         fi
286     fi
287 }
288
289 ###########  Programme Principal #############
290
291 # parse arguments
292
293 LISTE_SRC=""
294 OPTIONS_CONFIGURE=""
295 LISTE_MODULE=""
296 BATCH_MODE=0
297 COMPILE_ONLY=0
298
299 if [ $# -eq 0 ]
300 then
301     echo
302     echo "Pas d'arguments!"
303     usage
304 fi
305
306 for arg in $*
307 do
308     case $arg in
309
310       --disable-debug  )   OPTIONS_CONFIGURE="${OPTIONS_CONFIGURE} --disable-debug"   ;;
311
312       --batch)             BATCH_MODE=1   ;;
313       
314       --environ=*)         set_environ ${arg#--environ=}   ;;
315
316       --install=*)         INSTALL_ROOT_DIR=${arg#--install=}   ;;
317       
318       --sources=*)         LISTE_SRC="${LISTE_SRC} ${arg#--sources=}"   ;;
319       
320       --local | -l )       SRC_ROOT_DIR=$PWD ; INSTALL_ROOT_DIR=$PWD ;;
321
322       --compile-only | -c ) COMPILE_ONLY=1 ;;
323
324       *_SRC  )             LISTE_SRC="${LISTE_SRC} ${arg}" ;;
325
326       *)                   LISTE_MODULE="${LISTE_MODULE} ${arg}" ;;
327
328     esac
329 done
330
331 LISTE_SRC=`echo ${LISTE_SRC} | sed "s/,/ /g"`  # replaces ',' by blanks
332
333 if [ ! $INSTALL_ROOT_DIR ]
334 then
335     echo Local installation
336     INSTALL_ROOT_DIR=$PWD
337 fi
338
339 clear
340 echo
341 echo "                  ------------------------------------"
342 echo "                  | Utilitaire de Compilation Salome |"
343 echo "                  ------------------------------------"
344 echo
345 echo
346 echo "Liste des modules à parcourir : $LISTE_MODULE"
347 echo "Liste repertoire à compiler   : $LISTE_SRC"
348 echo "Options configure             : $OPTIONS_CONFIGURE"
349 echo "Environment File              : $ENVIRON_FILE"
350 echo
351 CR=/tmp/${USER}_SA_build_CR
352 echo -e "\n" > $CR
353
354 if [ $ENVIRON_FILE ]  && [ -f $ENVIRON_FILE ]  # if an existing environment file was specified
355 then
356     # get shell extension
357     case ${ENVIRON_FILE##*\.} in 
358        bash) SHELL_EXT=sh;;
359        ksh)  SHELL_EXT=sh;;
360        csh)  SHELL_EXT=csh;;
361        sh)   SHELL_EXT=sh;;
362        *)    SHELL_EXT=sh;echo WARNING : shell extension not recognized! Assume sh ;;
363     esac
364     if [ "${SHELL_EXT}" == "sh" ]
365     then
366     #   if shell syntax is sh, source environment with ENV_FOR_LAUNCH set to 0
367         cat ${ENVIRON_FILE} | sed "s/ENV_FOR_LAUNCH=1/ENV_FOR_LAUNCH=0/g" > ${ENVIRON_FILE}.tmp
368         source ${ENVIRON_FILE}.tmp
369         rm ${ENVIRON_FILE}.tmp
370         echo source Environment File with ENV_FOR_LAUNCH=0
371     else
372         echo "WARNING : environment file has csh extension => cannot source it and assume environment already set."
373         echo -e "          (if compilation fails, please set environment before, of choose an sh syntax file)\n"
374     fi
375 else
376     echo Warning : Environment File Not Found!
377 fi
378
379 LOCAL_DIR=`pwd`
380 if [ "$LISTE_MODULE" == "" ]
381 then
382     # no module specified -> lookup for sources localy
383     echo lookup for sources localy
384     SOURCE_DIRS=
385     for src in $LISTE_SRC
386     do
387         if [ -d ${LOCAL_DIR}/${src} ]
388         then
389            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
390         elif [ -d ${SRC_ROOT_DIR}/${src} ]
391         then
392            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
393         fi
394     done
395     build_source_dir
396 else
397     for module in $LISTE_MODULE
398     do
399        echo "----------------- Compilation dans $module ----------------"
400        echo
401        echo
402        get_source_dir
403        build_source_dir
404        echo
405     done
406 fi
407
408 echo -e "\n----------------- Résumé ----------------"
409 cat ${CR}