Salome HOME
715b9fd7b0a6540beaca2780128570284fde3949
[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 "  --install=path  : set install directory where module building is done (should be absolute)"
43     echo "  --compile-only -c  : no building - just compile by calling make && make install"
44     echo "  --local -l      : look for source directories localy - build and install localy"
45     echo "  --sources=src1,src2,...  : for specifying source directories that don't end up with _SRC"
46     echo 
47     echo
48     echo "Examples :"
49     echo
50     echo "  SA_build  KERNEL"
51     echo "    -> compilation of KERNEL/KERNEL_SRC"
52     echo "       build is done in KERNEL/KERNEL_BUILD"
53     echo "       install is done in KERNEL/KERNEL_INSTALL"
54     echo "       environment should be set up correctly before (option --environ not used)"
55     echo
56     echo "  SA_build  MED_SRC --install=/export/home/SALOME"
57     echo "    -> compilation of MED_SRC  (no module name was specified)"
58     echo "       build is done in /export/home/SALOME/MED_BUILD"
59     echo "       install is done in /export/home/SALOME/MED_INSTALL"
60     echo 
61     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"
62     echo "    -> compilation without prompting (--batch option) of KERNEL_SRC, GUI_SRC, GEOM_SRC, MED_SRC, MESH_SRC, VISU_SRC and SUPERV_SRC"
63     echo "       all sources are searched in V_3_0_2 directory "
64     echo "       environment is setup using my_env_products.sh file"
65     echo "       configure is done with --disable-debug option"
66     echo 
67     exit 1
68 }
69
70 ask_confirmation()
71 {
72        echo
73        echo $1
74        echo
75        read answer
76        case $answer in
77            y* | Y* | o* | O* )   ;;
78            *) exit 1
79        esac
80 }
81
82 check_dir()
83 {
84    if [ -d $1 ]
85    then
86       rm -rf $1/*
87       echo "remove $1 content"
88    else
89       mkdir -p $1
90       echo "create $1"
91    fi
92 }
93
94 set_environ()
95 {
96    if [ -f $1 ]
97    then
98        ENVIRON_FILE=$1
99    else
100        echo -e "\n\nError : environment file $1 NOT FOUND\n"
101        exit
102    fi
103 }
104
105 compile()
106 {
107     clear
108     echo "----------------- Compile ---------------------"
109     make 
110     if [ $? -eq 0 ]
111     then
112         # compilation succeeded : we make install
113         echo
114         echo "----------------- Install ---------------------"
115         make install
116     else
117         exit 1
118     fi
119 }
120
121 get_source_dir()
122 {
123     COMPILE_DIR=
124     SOURCE_DIRS=
125     if [ -d ${LOCAL_DIR}/${module} ]
126     then
127        COMPILE_DIR=${LOCAL_DIR}/${module}
128     elif [ -d ${SRC_ROOT_DIR}/${module} ]
129     then
130            COMPILE_DIR=${SRC_ROOT_DIR}/${module}
131     else
132        echo Sorry : ${module} not found!
133        usage
134     fi
135     
136     if [ -d ${COMPILE_DIR}/${module}_SRC ]
137     then
138         SOURCE_DIRS=${COMPILE_DIR}/${module}_SRC
139     fi
140
141     for src in $LISTE_SRC
142     do
143         if [ -d ${COMPILE_DIR}/${src} ]
144         then
145            SOURCE_DIRS="$SOURCE_DIRS ${COMPILE_DIR}/${src}"
146         fi
147     done
148
149 }
150
151 build_source_dir()
152 {
153    isrc=0 # compteur
154    for SOURCE_DIR in ${SOURCE_DIRS}
155    do
156        (( isrc += 1 ))
157        echo -e "\n${isrc})  Compilation dans $SOURCE_DIR \n"
158        src=`basename $SOURCE_DIR`
159        SOURCE_NAME=${src%_SRC}
160        echo " -> Repertoire Source  = ${SOURCE_DIR}"
161        echo " -> Component name     = ${SOURCE_NAME}"
162        echo " -> Repertoire Build   = ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD"
163        echo " -> Repertoire Install = ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL"
164        echo
165        echo
166        echo
167        if [ $COMPILE_ONLY -eq 1 ]
168        then
169            if [ $BATCH_MODE -eq 0 ]
170            then
171                ask_confirmation "Compile ?"
172            fi
173            cd ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
174            compile
175        else
176
177            if [ $BATCH_MODE -eq 0 ]
178            then
179                ask_confirmation "build configure and configure ?"
180            fi
181         #
182            cd ${SOURCE_DIR}
183            echo
184            echo "----------------- Build configure ---------------------"
185            ./build_configure
186            echo
187            echo "----------------- Configure ---------------------"
188            echo
189            # crée ou vide les répertoires
190            check_dir ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
191            check_dir ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL
192            cd ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_BUILD
193            ${SOURCE_DIR}/configure ${OPTIONS_CONFIGURE} --prefix=${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL | tee ${TMP_FILE}
194            echo
195            echo "retour configure : $?"
196            echo
197            if [ $BATCH_MODE -eq 0 ]
198            then
199                ask_confirmation "Compile ?"
200            fi
201            export `echo ${SOURCE_NAME}`_ROOT_DIR=${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL
202            compile
203            cd $LOCAL_DIR
204            if [ $ENVIRON_FILE ]  &&  [ -f $ENVIRON_FILE ]
205            then
206                #  Mise à jour du script d'environnement
207                if [ $BATCH_MODE -eq 1 ]
208                then
209                    update_environ ${SOURCE_NAME}_ROOT_DIR ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL ${SOURCE_NAME}_SRC_DIR ${SOURCE_DIR}
210                else
211                    echo "Voulez vous mettre mettre à jour le script d'environnement  ${ENVIRON_FILE} ?"
212                    echo " (ajouter la variable ${SOURCE_NAME}_ROOT_DIR - une copie de l'ancien script est faite dans un .old)"
213                    echo
214                    read answer
215                    case $answer in
216                      y* | Y* | o* | O* )  update_environ ${SOURCE_NAME}_ROOT_DIR ${INSTALL_ROOT_DIR}/${module}/${SOURCE_NAME}_INSTALL ${SOURCE_NAME}_SRC_DIR ${SOURCE_DIR} ;;
217                      *) exit 1
218                    esac
219                fi
220            fi
221        fi
222    done    
223 }
224
225
226 # update environment scrip : add or replace ROOT_DIR and SRC_DIR variable for component
227 update_environ()
228 {
229     cp ${ENVIRON_FILE} ${ENVIRON_FILE}.old
230     grep " $1" ${ENVIRON_FILE}.old > /dev/null
231     if [ $? -eq 0 ]
232     then
233        # if variable is already set, we replace old setting by new one
234        cat ${ENVIRON_FILE}.old | awk -v var_root_dir=$1 -v path=$2 -v src_root_dir=$3 -v src_dir=$4 '
235            $1=="export" && $2 ~ "^"var_root_dir"=" {print "#"$0; $0="export "var_root_dir "=" path}
236            $1=="export" && $2 ~ "^"src_root_dir"=" {print "#"$0; $0="export "src_root_dir "=" src_dir}
237            $1 ~ "^"var_root_dir"=" {print "#"$0; $0=var_root_dir "=" path}
238            $1 ~ "^"src_root_dir"=" {print "#"$0; $0=src_root_dir "=" src_dir}
239            { print $0}'  > ${ENVIRON_FILE}
240     else
241        echo -e "##\n#------ ${SOURCE_NAME}-Src ------\nexport $3=$4" >> ${ENVIRON_FILE}
242        echo -e "##\n#------ ${SOURCE_NAME}-Bin ------\nexport $1=$2" >> ${ENVIRON_FILE}
243     fi
244 }
245
246 ###########  Programme Principal #############
247
248 # parse arguments
249
250 LISTE_SRC=""
251 OPTIONS_CONFIGURE=""
252 LISTE_MODULE=""
253 BATCH_MODE=0
254 COMPILE_ONLY=0
255
256 if [ $# -eq 0 ]
257 then
258     echo
259     echo "Pas d'arguments!"
260     usage
261 fi
262
263 for arg in $*
264 do
265     case $arg in
266
267       --disable-debug  )   OPTIONS_CONFIGURE="${OPTIONS_CONFIGURE} --disable-debug"   ;;
268
269       --batch)             BATCH_MODE=1   ;;
270       
271       --environ=*)         set_environ ${arg#--environ=}   ;;
272
273       --install=*)         INSTALL_ROOT_DIR=${arg#--install=}   ;;
274       
275       --sources=*)         LISTE_SRC="${LISTE_SRC} ${arg#--sources=}"   ;;
276       
277       --local | -l )       SRC_ROOT_DIR=$PWD ; INSTALL_ROOT_DIR=$PWD ;;
278
279       --compile-only | -c ) COMPILE_ONLY=1 ;;
280
281       *_SRC  )             LISTE_SRC="${LISTE_SRC} ${arg}" ;;
282
283       *)                   LISTE_MODULE="${LISTE_MODULE} ${arg}" ;;
284
285     esac
286 done
287
288 LISTE_SRC=`echo ${LISTE_SRC} | sed "s/,/ /g"`  # replaces ',' by blanks
289
290 if [ ! $INSTALL_ROOT_DIR ]
291 then
292     echo Local installation
293     INSTALL_ROOT_DIR=$PWD
294 fi
295
296 clear
297 echo
298 echo "                  ------------------------------------"
299 echo "                  | Utilitaire de Compilation Salome |"
300 echo "                  ------------------------------------"
301 echo
302 echo
303 echo "Liste des modules à parcourir : $LISTE_MODULE"
304 echo "Liste repertoire à compiler   : $LISTE_SRC"
305 echo "Options configure             : $OPTIONS_CONFIGURE"
306 echo "Environment File              : $ENVIRON_FILE"
307 echo
308
309 if [ $ENVIRON_FILE ]  && [ -f $ENVIRON_FILE ]
310 then
311     # force option for compilation - then source environment
312     cat ${ENVIRON_FILE} | sed "s/ENV_FOR_LAUNCH=1/ENV_FOR_LAUNCH=0/g" > ${ENVIRON_FILE}.tmp
313     source ${ENVIRON_FILE}.tmp
314     rm ${ENVIRON_FILE}.tmp
315     echo source Environment File with ENV_FOR_LAUNCH=0
316 else
317     echo Warning : Environment File Not Found!
318 fi
319
320 LOCAL_DIR=`pwd`
321 if [ "$LISTE_MODULE" == "" ]
322 then
323     # no module specified -> lookup for sources localy
324     echo lookup for sources localy
325     SOURCE_DIRS=
326     for src in $LISTE_SRC
327     do
328         if [ -d ${LOCAL_DIR}/${src} ]
329         then
330            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
331         elif [ -d ${SRC_ROOT_DIR}/${src} ]
332         then
333            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
334         fi
335     done
336     build_source_dir
337 else
338     for module in $LISTE_MODULE
339     do
340        echo "----------------- Compilation dans $module ----------------"
341        echo
342        echo
343        get_source_dir
344        build_source_dir
345        echo
346     done
347 fi
348