]> SALOME platform Git repositories - tools/hxx2salome.git/blob - scripts/SA_build
Salome HOME
5c1328afac18802ae37204617b7d7651f06a617f
[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
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            { print $0}'  > ${ENVIRON_FILE}
238     else
239        echo -e "##\n#------ ${SOURCE_NAME}-Src ------\nexport $3=$4" >> ${ENVIRON_FILE}
240        echo -e "##\n#------ ${SOURCE_NAME}-Bin ------\nexport $1=$2" >> ${ENVIRON_FILE}
241     fi
242 }
243
244 ###########  Programme Principal #############
245
246 # parse arguments
247
248 LISTE_SRC=""
249 OPTIONS_CONFIGURE=""
250 LISTE_MODULE=""
251 BATCH_MODE=0
252 COMPILE_ONLY=0
253
254 if [ $# -eq 0 ]
255 then
256     echo
257     echo "Pas d'arguments!"
258     usage
259 fi
260
261 for arg in $*
262 do
263     case $arg in
264
265       --disable-debug  )   OPTIONS_CONFIGURE="${OPTIONS_CONFIGURE} --disable-debug"   ;;
266
267       --batch)             BATCH_MODE=1   ;;
268       
269       --environ=*)         set_environ ${arg#--environ=}   ;;
270
271       --install=*)         INSTALL_ROOT_DIR=${arg#--install=}   ;;
272       
273       --sources=*)         LISTE_SRC="${LISTE_SRC} ${arg#--sources=}"   ;;
274       
275       --local | -l )       SRC_ROOT_DIR=$PWD ; INSTALL_ROOT_DIR=$PWD ;;
276
277       --compile-only | -c ) COMPILE_ONLY=1 ;;
278
279       *_SRC  )             LISTE_SRC="${LISTE_SRC} ${arg}" ;;
280
281       *)                   LISTE_MODULE="${LISTE_MODULE} ${arg}" ;;
282
283     esac
284 done
285
286 LISTE_SRC=`echo ${LISTE_SRC} | sed "s/,/ /g"`  # replaces ',' by blanks
287
288 if [ ! $INSTALL_ROOT_DIR ]
289 then
290     echo Local installation
291     INSTALL_ROOT_DIR=$PWD
292 fi
293
294 clear
295 echo
296 echo "                  ------------------------------------"
297 echo "                  | Utilitaire de Compilation Salome |"
298 echo "                  ------------------------------------"
299 echo
300 echo
301 echo "Liste des modules à parcourir : $LISTE_MODULE"
302 echo "Liste repertoire à compiler   : $LISTE_SRC"
303 echo "Options configure             : $OPTIONS_CONFIGURE"
304 echo "Environment File              : $ENVIRON_FILE"
305 echo
306
307 if [ $ENVIRON_FILE ]  && [ -f $ENVIRON_FILE ]
308 then
309     # force option for compilation - then source environment
310     cat ${ENVIRON_FILE} | sed "s/ENV_FOR_LAUNCH=1/ENV_FOR_LAUNCH=0/g" > ${ENVIRON_FILE}.tmp
311     source ${ENVIRON_FILE}.tmp
312     rm ${ENVIRON_FILE}.tmp
313     echo source Environment File with ENV_FOR_LAUNCH=0
314 else
315     echo Warning : Environment File Not Found!
316 fi
317
318 LOCAL_DIR=`pwd`
319 if [ "$LISTE_MODULE" == "" ]
320 then
321     # no module specified -> lookup for sources localy
322     echo lookup for sources localy
323     SOURCE_DIRS=
324     for src in $LISTE_SRC
325     do
326         if [ -d ${LOCAL_DIR}/${src} ]
327         then
328            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
329         elif [ -d ${SRC_ROOT_DIR}/${src} ]
330         then
331            SOURCE_DIRS="$SOURCE_DIRS ${LOCAL_DIR}/${src}"
332         fi
333     done
334     build_source_dir
335 else
336     for module in $LISTE_MODULE
337     do
338        echo "----------------- Compilation dans $module ----------------"
339        echo
340        echo
341        get_source_dir
342        build_source_dir
343        echo
344     done
345 fi
346