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