Salome HOME
Fix a bug: compilation failed on Mandrake 10.1 with automake version 1.9
[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         fi
263     else
264         grep " $1" ${ENVIRON_FILE}.old > /dev/null
265         if [ $? -eq 0 ]
266         then
267            # if variable is already set, we replace old setting by new one
268            cat ${ENVIRON_FILE}.old | awk -v var_root_dir=$1 -v path=$2 -v src_root_dir=$3 -v src_dir=$4 '
269                $1=="setenv" && $2 == var_root_dir {print "#"$0; $0="setenv "var_root_dir " " path}
270                $1=="setenv" && $2 == src_root_dir {print "#"$0; $0="setenv "src_root_dir " " src_dir}
271                { print $0}'  > ${ENVIRON_FILE}
272         else
273            echo -e "##\n#------ ${SOURCE_NAME}-Src ------\nsetenv $3 $4" >> ${ENVIRON_FILE}
274            echo -e "##\n#------ ${SOURCE_NAME}-Bin ------\nsetenv $1 $2" >> ${ENVIRON_FILE}
275         fi
276     fi
277 }
278
279 ###########  Programme Principal #############
280
281 # parse arguments
282
283 LISTE_SRC=""
284 OPTIONS_CONFIGURE=""
285 LISTE_MODULE=""
286 BATCH_MODE=0
287 COMPILE_ONLY=0
288
289 if [ $# -eq 0 ]
290 then
291     echo
292     echo "Pas d'arguments!"
293     usage
294 fi
295
296 for arg in $*
297 do
298     case $arg in
299
300       --disable-debug  )   OPTIONS_CONFIGURE="${OPTIONS_CONFIGURE} --disable-debug"   ;;
301
302       --batch)             BATCH_MODE=1   ;;
303       
304       --environ=*)         set_environ ${arg#--environ=}   ;;
305
306       --install=*)         INSTALL_ROOT_DIR=${arg#--install=}   ;;
307       
308       --sources=*)         LISTE_SRC="${LISTE_SRC} ${arg#--sources=}"   ;;
309       
310       --local | -l )       SRC_ROOT_DIR=$PWD ; INSTALL_ROOT_DIR=$PWD ;;
311
312       --compile-only | -c ) COMPILE_ONLY=1 ;;
313
314       *_SRC  )             LISTE_SRC="${LISTE_SRC} ${arg}" ;;
315
316       *)                   LISTE_MODULE="${LISTE_MODULE} ${arg}" ;;
317
318     esac
319 done
320
321 LISTE_SRC=`echo ${LISTE_SRC} | sed "s/,/ /g"`  # replaces ',' by blanks
322
323 if [ ! $INSTALL_ROOT_DIR ]
324 then
325     echo Local installation
326     INSTALL_ROOT_DIR=$PWD
327 fi
328
329 clear
330 echo
331 echo "                  ------------------------------------"
332 echo "                  | Utilitaire de Compilation Salome |"
333 echo "                  ------------------------------------"
334 echo
335 echo
336 echo "Liste des modules à parcourir : $LISTE_MODULE"
337 echo "Liste repertoire à compiler   : $LISTE_SRC"
338 echo "Options configure             : $OPTIONS_CONFIGURE"
339 echo "Environment File              : $ENVIRON_FILE"
340 echo
341 CR=/tmp/${USER}_SA_build_CR
342 echo -e "\n" > $CR
343
344 if [ $ENVIRON_FILE ]  && [ -f $ENVIRON_FILE ]  # if an existing environment file was specified
345 then
346     # get shell extension
347     case ${ENVIRON_FILE##*\.} in 
348        bash) SHELL_EXT=sh;;
349        ksh)  SHELL_EXT=sh;;
350        csh)  SHELL_EXT=csh;;
351        sh)   SHELL_EXT=sh;;
352        *)    SHELL_EXT=sh;echo WARNING : shell extension not recognized! Assume sh ;;
353     esac
354     if [ "${SHELL_EXT}" == "sh" ]
355     then
356     #   if shell syntax is sh, source environment with ENV_FOR_LAUNCH set to 0
357         cat ${ENVIRON_FILE} | sed "s/ENV_FOR_LAUNCH=1/ENV_FOR_LAUNCH=0/g" > ${ENVIRON_FILE}.tmp
358         source ${ENVIRON_FILE}.tmp
359         rm ${ENVIRON_FILE}.tmp
360         echo source Environment File with ENV_FOR_LAUNCH=0
361     else
362         echo "WARNING : environment file has csh extension => cannot source it and assume environment already set."
363         echo -e "          (if compilation fails, please set environment before, of choose an sh syntax file)\n"
364     fi
365 else
366     echo Warning : Environment File Not Found!
367 fi
368
369 LOCAL_DIR=`pwd`
370 if [ "$LISTE_MODULE" == "" ]
371 then
372     # no module specified -> lookup for sources localy
373     echo lookup for sources localy
374     SOURCE_DIRS=
375     for src in $LISTE_SRC
376     do
377         if [ -d ${LOCAL_DIR}/${src} ]
378         then
379            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
380         elif [ -d ${SRC_ROOT_DIR}/${src} ]
381         then
382            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
383         fi
384     done
385     build_source_dir
386 else
387     for module in $LISTE_MODULE
388     do
389        echo "----------------- Compilation dans $module ----------------"
390        echo
391        echo
392        get_source_dir
393        build_source_dir
394        echo
395     done
396 fi
397
398 echo -e "\n----------------- Résumé ----------------"
399 cat ${CR}