4 # salome2 environment file (.bash or .sh) - can also be specified with -e option
6 # if present, hxx2salome will try to compile new module, by sourcing ENVIRON_FILE file, and executing
7 # build_configure, configure, make & make install
8 # remark : hxx2salome has to be lanched with clean PATH, LD_LIBRARY_PATH & PYTHONPATH variables!
10 # options you want to pass to configure
16 echo -e "\n Usage :\n"
17 echo -e " Set HXX2SALOME_ROOT_DIR variable"
18 echo -e " ${HXX2SALOME_ROOT_DIR}/hxx2salome [options] "
19 echo -e " cppComponent_root_dir cppComponent.hxx"
20 echo -e " libcppComponent.so salomeComponentRoot\n"
21 echo -e " - cppComponent_root_dir : install directory (absolute path)"
22 echo -e " of the c++ component\n"
23 echo -e " - cppComponent.hxx : header of the component"
24 echo -e " - libcppComponent.so : library\n"
25 echo -e " (cppComponent.hxx and libcppComponent.so have to be found"
26 echo -e " in cppComponent_root_dir)\n"
27 echo -e " - salomeComponentRoot : directory of the source/build/install"
28 echo -e " of the Salome component\n"
29 echo -e "\n Options :"
31 echo -e " -e environment_script : to specify the name of a environment file that will be updated with new necessary commands"
32 echo -e " (this file is also used for sourcing environment before compilation if it has sh or bash syntax,"
33 echo -e " if the syntax is csh, it is not sourced and for compiling (-c option) environment must be set up before)"
34 echo -e " -s script_extension : to use if your environment file name doesn't have extension"
35 echo -e " -g : to create a gui part in your component building tree"
36 echo -e " -c : to compile after generation"
37 echo -e " (use this option only if you don't have dependencies in your header or libraries"
38 echo -e " if it is the case, you'll have to adapt your Makefile.in"
39 echo -e " -l : to launch salome "
46 echo "----------------------------------------------------------------------------"
50 echo " Automatic generation of a Salome2 component from a C++ component"
52 echo "----------------------------------------------------------------------------"
61 # check if $1 is a directory
62 echo -e "-> check arguments\n"
65 echo -e "Error : directory $1 does not exist!\n"
68 CPP_ROOT_DIR=${1%%/} # remove trailing slash if present
69 echo " C++ Component directory : ${CPP_ROOT_DIR}"
71 # look for include file $2 - check number of files found and extension
72 nb=`find ${CPP_ROOT_DIR} -name $2 | wc -l` # number of files found, should be equal to 1
76 echo -e "\n Error:\n Include file $2 not found in $1 directory!\n"
80 echo -e "\n Error:\n More than one file named $2 was found in $1!\n Include file should be unique!"
82 elif [ $extension != "hxx" -a $extension != "hh" -a $extension != "h" ]
84 echo -e "\n Error:\n Extension=$extension\n Include file $2 should have extension .hxx .hh or .h !\n"
87 hxx_file=`find ${CPP_ROOT_DIR} -name $2` # name of c++ header we will parse to generate salome module
89 echo " C++ Component header : ${hxx_file}"
90 hxx_dir=`dirname ${hxx_file}`
93 nb=`find ${CPP_ROOT_DIR} -name $3 | wc -l` # number of files found, should be equal to 1
96 echo -e "\n Error:\n Library file $3 not found in $1 directory!\n"
100 echo -e "\n Error:\n More than one file named $3 was found in $1!\n Library file should be unique!"
103 so_file=`find ${CPP_ROOT_DIR} -name $3` # absolute path of library containing c++ module
104 echo " C++ Component library : ${so_file}"
105 lib_dir=`dirname ${so_file}`
106 lib_file=${3%.so} # name without .so
107 lib_file=${lib_file#lib} # name of library without lib and .so (needed by makefile)
109 # installation directory
114 salomeComponentRoot=${4%%/} # remove trailing slash
115 echo " Salome Component directory : ${salomeComponentRoot}"
118 # retrieve python test file ending up with _test.py
119 get_python_test_file()
122 for file in `find . -name "*_test.py"`
124 cp $file ${tmp_dir}/${CLASS_NAME}_SRC/src/${CLASS_NAME}
125 python_test_file=${python_test_file}" "`basename $file`
127 echo -e "\nList of exported python file test : $python_test_file \n"
131 create_component_tree()
133 INSTALL_DIR=${salomeComponentRoot}/${CLASS_NAME}
135 export NEW_COMPONENT_SRC_DIR=${INSTALL_DIR}/${CLASS_NAME}_SRC
136 export NEW_COMPONENT_BUILD_DIR=${INSTALL_DIR}/${CLASS_NAME}_BUILD
137 export NEW_COMPONENT_ROOT_DIR=${INSTALL_DIR}/${CLASS_NAME}_INSTALL
139 \rm -rf ${NEW_COMPONENT_SRC_DIR}
140 mkdir -p ${NEW_COMPONENT_SRC_DIR}
146 makefile_lib="-L\${${CLASS_NAME}CPP_ROOT_DIR}${lib_dir#${CPP_ROOT_DIR}} -l${lib_file}"
147 makefile_include="-I\${${CLASS_NAME}CPP_ROOT_DIR}${hxx_dir#${CPP_ROOT_DIR}}"
149 echo -e "\nlinking option : $makefile_lib"
150 echo -e "include option : $makefile_include"
153 generate_module_source()
156 # go in temporary directory to work on code generation
160 # ------------------------- parse hxx file and generate code ---------------------------------------
162 echo -e "\n-> Extract public functions\n"
163 cat ${hxx_file} | awk -f ${gene_dir}/parse01.awk | awk -f ${gene_dir}/parse1.awk > ${CLASS_NAME}_public_functions
164 cat ${CLASS_NAME}_public_functions
165 if [ ! -s ${CLASS_NAME}_public_functions ]
167 echo -e "\nError:\n Sorry - No compatible function was found!\n Please check your header file\n"
171 echo -e "\n\n-> Parse public functions and generate Salome2 files\n\n compatibility function\n"
172 cat ${CLASS_NAME}_public_functions | awk -f ${gene_dir}/parse2.awk |\
173 awk -v class_name=${CLASS_NAME} -f ${gene_dir}/parse3.awk
176 echo -e "\n IDL file:"
177 cat parse_result > hxx2salome_journal
178 echo -e "\n----------------- IDL file ------------------\n" >> hxx2salome_journal
179 cat code_idl >> hxx2salome_journal
181 echo -e "\n----------------- hxx file ------------------\n" >> hxx2salome_journal
182 cat code_hxx >> hxx2salome_journal
183 echo -e "\n----------------- cxx file ------------------\n" >> hxx2salome_journal
184 cat code_cxx >> hxx2salome_journal
188 # ------------------- duplicates template module and insert generated code ------------------------------
190 echo -e "\n-> Duplicate template module" | tee hxx2salome_journal
191 tar xvfz ${gene_dir}/template_src.tgz >> hxx2salome_journal
192 mv HXX2SALOME_GENERIC_CLASS_NAME_SRC ${CLASS_NAME}_SRC
193 ${gene_dir}/renameSalomeModule -i HXX2SALOME_GENERIC_CLASS_NAME ${CLASS_NAME} ${CLASS_NAME}_SRC >> hxx2salome_journal
195 if [ $make_gui -eq 0 ]
197 echo -e "\n-> Delete GUI part from the tree" >> hxx2salome_journal
198 'rm' -rf ${CLASS_NAME}_SRC/src/${CLASS_NAME}GUI
199 sed "s/${CLASS_NAME}GUI//" < ${CLASS_NAME}_SRC/src/Makefile.in > /tmp/h2smkf.$$
200 mv /tmp/h2smkf.$$ ${CLASS_NAME}_SRC/src/Makefile.in
203 echo -e "\n-> Substitute generated code in idl file"
204 echo "// this idl file was generated by hxx2salome" > tmpfile
205 cat ${CLASS_NAME}_SRC/idl/${CLASS_NAME}_Gen.idl |awk '
206 $0 ~ "HXX2SALOME_IDL_CODE" {system("cat code_idl >> tmpfile")}
207 $0 != "HXX2SALOME_IDL_CODE" { print $0 >> "tmpfile" }'
208 mv tmpfile ${CLASS_NAME}_SRC/idl/${CLASS_NAME}_Gen.idl
210 echo -e "\n-> Substitute generated code in hxx file"
211 echo "// this hxx file was generated by hxx2salome" > tmpfile
212 cat ${CLASS_NAME}_SRC/src/${CLASS_NAME}/${CLASS_NAME}_i.hxx |awk '
213 $0 ~ "HXX2SALOME_HXX_CODE" {system("cat code_hxx >> tmpfile")}
214 $0 !~ "HXX2SALOME" { print $0 >> "tmpfile" }'
215 mv tmpfile ${CLASS_NAME}_SRC/src/${CLASS_NAME}/${CLASS_NAME}_i.hxx
217 echo -e "\n-> Substitute generated code in cxx file"
218 echo "// this cxx file was generated by hxx2salome" > tmpfile
219 cat ${CLASS_NAME}_SRC/src/${CLASS_NAME}/${CLASS_NAME}_i.cxx |awk -v cpp_include=$hxx '
220 $0 ~ "HXX2SALOME_CXX_CODE" {system("cat code_cxx >> tmpfile")}
221 $0 ~ "HXX2SALOME_CPP_INCLUDE" { printf "#include \"%s\"\n",cpp_include >> "tmpfile" }
222 $0 !~ "HXX2SALOME" { print $0 >> "tmpfile" }'
223 mv tmpfile ${CLASS_NAME}_SRC/src/${CLASS_NAME}/${CLASS_NAME}_i.cxx
225 # add flags in makefile
229 echo -e "\n-> Substitute flags in Makefile.in"
230 sed "s?HXX2SALOME_INCLUDE?${makefile_include}?g
231 s?HXX2SALOME_PYTHON_FILE?${python_test_file}?g
232 s?HXX2SALOME_LIB?${makefile_lib}?g
233 " ${CLASS_NAME}_SRC/src/${CLASS_NAME}/Makefile.in > tmpfile
234 mv tmpfile ${CLASS_NAME}_SRC/src/${CLASS_NAME}/Makefile.in
236 # generate component catalog
237 echo -e "\n-> Generate component catalog" | tee hxx2salome_journal
238 if [ -f ${KERNEL_ROOT_DIR}/bin/salome/runIDLparser ]
240 idlparser=${KERNEL_ROOT_DIR}/bin/salome/runIDLparser
242 idlparser=${gene_dir}/runIDLparser
244 cd ${CLASS_NAME}_SRC/resources
245 VER=`cat ${KERNEL_ROOT_DIR}/bin/salome/VERSION | awk ' { print $NF }'` # extract number of version
246 ${idlparser} -Wbcatalog=tmp.xml,icon=${CLASS_NAME}.png,version=${VER} -I${KERNEL_ROOT_DIR}/idl/salome -I${MED_ROOT_DIR}/idl/salome ../idl/${CLASS_NAME}_Gen.idl >& /dev/null | tee hxx2salome_journal
247 cat tmp.xml | sed 's/_Gen//g' > ${CLASS_NAME}Catalog.xml
254 echo "----------------- Configure -------------------"
256 echo "----------------- Compile ---------------------"
260 # compilation succeeded : we make install
262 echo "----------------- Install ---------------------"
271 if [ -z ${ENVIRON_FILE} ]
273 ENVIRON_FILE="${NEW_COMPONENT_SRC_DIR}/env_${CLASS_NAME}.${SHELL_EXT}"
276 echo -e "\nEnvironment file : ", $ENVIRON_FILE
277 if [ -e ${ENVIRON_FILE} ]
279 cp ${ENVIRON_FILE} ${ENVIRON_FILE}.old
281 touch ${ENVIRON_FILE}
283 if [ "${SHELL_EXT}" == "csh" ]
285 grep -q " ${CLASS_NAME}_SRC_DIR" ${ENVIRON_FILE}
289 echo -e "###\n#------ ${CLASS_NAME}-Src ------" >> ${ENVIRON_FILE}
290 echo -e "setenv ${CLASS_NAME}_BASE ${INSTALL_DIR}" >> ${ENVIRON_FILE}
291 echo -e "setenv ${CLASS_NAME}_SRC_DIR \${${CLASS_NAME}_BASE}/${CLASS_NAME}_SRC\n" >> ${ENVIRON_FILE}
294 grep -q " ${CLASS_NAME}_ROOT_DIR" ${ENVIRON_FILE}
298 echo -e "###\n#------ ${CLASS_NAME}-Bin ------" >> ${ENVIRON_FILE}
299 echo -e "setenv ${CLASS_NAME}_ROOT_DIR \${${CLASS_NAME}_BASE}/${CLASS_NAME}_INSTALL" >> ${ENVIRON_FILE}
300 echo -e "setenv ${CLASS_NAME}CPP_ROOT_DIR ${CPP_ROOT_DIR}" >> ${ENVIRON_FILE}
301 echo -e "setenv LD_LIBRARY_PATH \${${CLASS_NAME}CPP_ROOT_DIR}${lib_dir#${CPP_ROOT_DIR}}:\${LD_LIBRARY_PATH}" >> ${ENVIRON_FILE}
304 if [ "${SHELL_EXT}" == "sh" ]
306 grep -q " ${CLASS_NAME}_SRC_DIR=" ${ENVIRON_FILE}
310 echo -e "###\n#------ ${CLASS_NAME}-Src ------" >> ${ENVIRON_FILE}
311 echo -e "export ${CLASS_NAME}_BASE=${INSTALL_DIR}" >> ${ENVIRON_FILE}
312 echo -e "export ${CLASS_NAME}_SRC_DIR=\${${CLASS_NAME}_BASE}/${CLASS_NAME}_SRC\n" >> ${ENVIRON_FILE}
315 grep -q " ${CLASS_NAME}_ROOT_DIR=" ${ENVIRON_FILE}
319 echo -e "###\n#------ ${CLASS_NAME}-Bin ------" >> ${ENVIRON_FILE}
320 echo -e "export ${CLASS_NAME}_ROOT_DIR=\${${CLASS_NAME}_BASE}/${CLASS_NAME}_INSTALL" >> ${ENVIRON_FILE}
321 echo -e "export ${CLASS_NAME}CPP_ROOT_DIR=${CPP_ROOT_DIR}" >> ${ENVIRON_FILE}
322 echo -e "export LD_LIBRARY_PATH=\${${CLASS_NAME}CPP_ROOT_DIR}${lib_dir#${CPP_ROOT_DIR}}:\${LD_LIBRARY_PATH}" \
329 copy_component_source()
331 mv ${tmp_dir}/${CLASS_NAME}_SRC/* ${NEW_COMPONENT_SRC_DIR}
332 mkdir -p ${NEW_COMPONENT_BUILD_DIR}
333 mkdir -p ${NEW_COMPONENT_ROOT_DIR}
338 echo -e "\n\nModule was created in ${NEW_COMPONENT_SRC_DIR}"
339 echo -e "\nTo compile it, do the following : \n"
340 echo -e "\tsource ${ENVIRON_FILE}"
341 echo -e "\tcd \${${CLASS_NAME}_SRC_DIR}"
342 echo -e "\t./build_configure"
343 echo -e "\tmkdir -p \${${CLASS_NAME}_BUILD_DIR}"
344 echo -e "\tcd \${${CLASS_NAME}_BUILD_DIR}"
345 echo -e "\t\${${CLASS_NAME}_SRC_DIR}/configure ${CONFIGURE_OPTION} --prefix=\${${CLASS_NAME}_ROOT_DIR}"
347 echo -e "\tmake install"
348 echo -e "\nTo use it :\n"
349 echo -e "\tsource the Salome environment"
350 echo -e "\tsource the component environment file (${ENVIRON_FILE})"
351 echo -e "\trun Salome"
352 echo -e "\tadd ${CLASS_NAME} to the Salome modules list"
353 echo -e "\t\t(with the --modules option of the runSalome command or"
354 echo -e "\t\tby editing your own per-user configuration file ~/.SalomeApprc.3.x.x)"
355 echo -e "\nIf the header of your component includes other headers that are not in the same directories,"
356 echo -e "or if your library has dependencies you want to specify,"
357 echo -e "you'll have to modify the following files Makefile.in: "
358 echo -e "\t\${${CLASS_NAME}_SRC_DIR}/src/${CLASS_NAME}/Makefile.in"
359 echo -e "\t\${${CLASS_NAME}_SRC_DIR}/src/${CLASS_NAME}GUI/Makefile.in"
364 ${KERNEL_ROOT_DIR}/bin/salome/runSalome --gui --modules=GEOM,SMESH,VISU,SUPERV,MED,`echo ${CLASS_NAME}` --containers=cpp,python --killall
369 if [ ! -d ${NEW_COMPONENT_BUILD_DIR} ]
371 mkdir -p ${NEW_COMPONENT_BUILD_DIR}
373 if [ ! -d ${NEW_COMPONENT_ROOT_DIR} ]
375 mkdir -p ${NEW_COMPONENT_ROOT_DIR}
377 \rm -rf ${NEW_COMPONENT_ROOT_DIR}/* ${NEW_COMPONENT_BUILD_DIR}/*
379 # source ${ENVIRON_FILE}
381 echo -e "\n-> Build Configure"
382 cd ${NEW_COMPONENT_SRC_DIR}
384 cd ${NEW_COMPONENT_BUILD_DIR}
385 echo -e "\n-> Configure in ${NEW_COMPONENT_BUILD_DIR}"
386 echo -e "\n-> Install in ${NEW_COMPONENT_ROOT_DIR}"
387 ${NEW_COMPONENT_SRC_DIR}/configure ${CONFIGURE_OPTION} --prefix=${NEW_COMPONENT_ROOT_DIR}
390 if [[ $do_compile -eq 1 ]]
395 if [[ $do_launch -eq 1 ]]
402 # ------------------------------------------------------------------------
403 # --------------------------- MAIN PROGRAM -------------------------------
404 # ------------------------------------------------------------------------
407 NEW_COMPONENT_ROOT_DIR=
408 NEW_COMPONENT_SRC_DIR=
409 NEW_COMPONENT_BUILD_DIR=
415 welcome # print some welcome info
417 while getopts "cs:e:h:lg" Option
422 e) ENVIRON_FILE=$OPTARG;;
431 *) echo "Unimplemented option chosen : $Option."
437 shift $(($OPTIND - 1))
439 # check number of other arguments
443 echo -e "\nBad number of arguments\n\n"
448 check_arguments $1 $2 $3 $4
450 # if there is a sh compatible environment file, source it
451 if [[ -n ${ENVIRON_FILE} && -f ${ENVIRON_FILE} ]]
453 # analyse extension of environment file
454 case ${ENVIRON_FILE##*\.} in
462 # Environment policy :
463 # - an existing sh file was specified : we source environment file
464 # - else (no file or csh syntax) : we don't source environment file, and do compile
465 # only if KERNEL_ROOT_DIR and MED_ROOT_DIR are defined
466 if [ "${SHELL_EXT}" == "sh" ] && [ ${ENVIRON_FILE} ] && [ -f ${ENVIRON_FILE} ]
468 echo -e "\n Environment file with sh syntax specified => we source ${ENVIRON_FILE}"
469 source ${ENVIRON_FILE}
471 if [ ${KERNEL_ROOT_DIR} ] && [ -d ${KERNEL_ROOT_DIR} ] && [ ${MED_ROOT_DIR} ] && [ -d ${MED_ROOT_DIR} ]
473 # if KERNEL_ROOT_DIR and MED_ROOT_DIR are defined, we consider that environment is set
474 echo -e "\n Environment already set (KERNEL_ROOT_DIR and MED_ROOT_DIR are defined)"
476 if [ $do_compile -eq 1 ]
478 echo -e "\n Warning - Cannot compile : Environment shoud be set up before, or specify a environment file with sh syntax!\n"
484 # look up hxx2salome scripts
485 # - first search in directory ${HXX2SALOME_ROOT_DIR} (if the variable is defined)
486 # - then search in directory ${HXX2SALOME_ROOT_DIR}/bin
487 # - finally seach locally.
489 if [ ${HXX2SALOME_ROOT_DIR} ] && [ -d ${HXX2SALOME_ROOT_DIR} ]
491 echo " HXX2SALOME_ROOT_DIR variable is defined : ${HXX2SALOME_ROOT_DIR} => we look up hxx2salome scripts inside"
492 if [ -f ${HXX2SALOME_ROOT_DIR}/parse1.awk -a -f ${HXX2SALOME_ROOT_DIR}/parse2.awk ] # check if script are found in ${HXX2SALOME_ROOT_DIR}
494 gene_dir=${HXX2SALOME_ROOT_DIR}
495 elif [ -f ${HXX2SALOME_ROOT_DIR}/bin/parse1.awk -a -f ${HXX2SALOME_ROOT_DIR}/bin/parse2.awk ] # else check /bin directory
497 gene_dir=${HXX2SALOME_ROOT_DIR}/bin
499 echo -e "\nError : Variable HXX2SALOME_ROOT_DIR not correctly set"
503 echo "HXX2SALOME_ROOT_DIR directory not set => we look up hxx2salome scripts locally"
504 if [ -f parse1.awk -a -f parse2.awk ] # check if script are found locally
506 gene_dir=`pwd` # case where hxx2salome was launched from HXX2SALOME directory
508 echo -e "\nError : Variable HXX2SALOME_ROOT_DIR is not set, and hxx2salome didn't find his scripts locally"
512 echo " hxx2salome directory found : $gene_dir"
513 if [ ! -f ${gene_dir}/parse3.awk -o ! -f ${gene_dir}/template_src.tgz ] # final check
515 echo -e "\nError : scripts parse3.awk or template_src.tgz not present in hxx2salome directory : $gene_dir"
521 CLASS_NAME=`awk '$1 == "class" && $0 !~ /;/ {print $2}' ${hxx_file}|awk -F: '{print $1}'`
522 echo " Name of class :" $CLASS_NAME
525 echo -e "\nError:\n Sorry - No class definition was found!\n Please check your header file\n"
530 # create temporary working directory
532 tmp_dir="/tmp/${USER}/${CLASS_NAME}"
541 # --------------------- Generation of module source from template ------------------------------------------
544 generate_module_source
547 # ---------------------- Installation of new module sources ------------------------------------------------
549 create_component_tree
552 # ---------------------- Modification of Salome environment file -------------------------------------------
555 export `echo ${CLASS_NAME}`_ROOT_DIR=${NEW_COMPONENT_ROOT_DIR} # to avoid resource env for compiling and launching salome
556 export `echo ${CLASS_NAME}`CPP_ROOT_DIR=${CPP_ROOT_DIR} # idem
558 # ---------------------- Copy the generated source from temp dir -------------------------------------------
560 copy_component_source
563 # ---------------------- If requested, compilation of the Salome component ----------------------------------
564 if [ ${do_compile} -eq 1 ]
571 echo -e "\nGeneration done\n"