Salome HOME
3c3bf2d46c29402b103d2032e30a72a1b857e551
[modules/kernel.git] / salome_adm / cmake_files / SalomeMacros.cmake
1 # Copyright (C) 2012-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 # Author: A.Geay, V. Sandler, A. Bruneton
20 #
21
22 #----------------------------------------------------------------------------
23 # LIST_CONTAINS is a macro useful for determining whether a list has a 
24 # particular entry
25 #----------------------------------------------------------------------------
26 MACRO(LIST_CONTAINS var value)
27   SET(${var})
28   FOREACH(value2 ${ARGN})
29     IF(${value} STREQUAL "${value2}")
30       SET(${var} TRUE)
31     ENDIF (${value} STREQUAL "${value2}")
32   ENDFOREACH (value2)
33 ENDMACRO(LIST_CONTAINS)
34
35 #----------------------------------------------------------------------------
36 # The PARSE_ARGUMENTS macro will take the arguments of another macro and
37 # define several variables.
38 #
39 # USAGE:  PARSE_ARGUMENTS(prefix arg_names options arg1 arg2...)
40 #
41 # ARGUMENTS:
42 #
43 # prefix: IN: a prefix to put on all variables it creates.
44 #
45 # arg_names: IN: a list of names.
46 # For each item in arg_names, PARSE_ARGUMENTS will create a 
47 # variable with that name, prefixed with prefix_. Each variable will be filled
48 # with the arguments that occur after the given arg_name is encountered
49 # up to the next arg_name or the end of the arguments. All options are
50 # removed from these lists. PARSE_ARGUMENTS also creates a
51 # prefix_DEFAULT_ARGS variable containing the list of all arguments up
52 # to the first arg_name encountered.
53 #
54 # options: IN: a list of options.
55 # For each item in options, PARSE_ARGUMENTS will create a
56 # variable with that name, prefixed with prefix_. So, for example, if prefix is
57 # MY_MACRO and options is OPTION1;OPTION2, then PARSE_ARGUMENTS will
58 # create the variables MY_MACRO_OPTION1 and MY_MACRO_OPTION2. These
59 # variables will be set to true if the option exists in the command line
60 # or false otherwise.
61 # arg_names and options lists should be quoted.
62 #
63 # The rest of PARSE_ARGUMENTS are arguments from another macro to be parsed.
64 #----------------------------------------------------------------------------
65 MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
66   SET(DEFAULT_ARGS)
67   FOREACH(arg_name ${arg_names})
68     SET(${prefix}_${arg_name})
69   ENDFOREACH(arg_name)
70   FOREACH(option ${option_names})
71     SET(${prefix}_${option} FALSE)
72   ENDFOREACH(option)
73   SET(current_arg_name DEFAULT_ARGS)
74   SET(current_arg_list)
75   FOREACH(arg ${ARGN})
76     LIST_CONTAINS(is_arg_name ${arg} ${arg_names})
77     IF (is_arg_name)
78       SET(${prefix}_${current_arg_name} ${current_arg_list})
79       SET(current_arg_name ${arg})
80       SET(current_arg_list)
81     ELSE (is_arg_name)
82       LIST_CONTAINS(is_option ${arg} ${option_names})
83       IF (is_option)
84       SET(${prefix}_${arg} TRUE)
85       ELSE (is_option)
86       SET(current_arg_list ${current_arg_list} ${arg})
87       ENDIF (is_option)
88     ENDIF (is_arg_name)
89   ENDFOREACH(arg)
90   SET(${prefix}_${current_arg_name} ${current_arg_list})
91 ENDMACRO(PARSE_ARGUMENTS)
92
93 #----------------------------------------------------------------------------
94 # SALOME_INSTALL_SCRIPTS is a macro useful for installing scripts.
95 #
96 # USAGE: SALOME_INSTALL_SCRIPTS(file_list path [WORKING_DIRECTORY dir] [DEF_PERMS])
97 #
98 # ARGUMENTS:
99 # file_list: IN : list of files to be installed. This list should be quoted.
100 # path: IN : full pathname for installing.
101
102 # By default files to be installed as executable scripts.
103 # If DEF_PERMS option is provided, than permissions for installed files are
104 # only OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ. 
105 #----------------------------------------------------------------------------
106 MACRO(SALOME_INSTALL_SCRIPTS file_list path)
107   PARSE_ARGUMENTS(SALOME_INSTALL_SCRIPTS "WORKING_DIRECTORY" "DEF_PERMS" ${ARGN})
108   SET(PERMS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
109   IF(NOT SALOME_INSTALL_SCRIPTS_DEF_PERMS)
110     SET(PERMS ${PERMS} OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)
111   ENDIF(NOT SALOME_INSTALL_SCRIPTS_DEF_PERMS)
112   FOREACH(file ${file_list})
113     SET(PREFIX "")
114     IF(IS_ABSOLUTE ${file})
115       GET_FILENAME_COMPONENT(file_name ${file} NAME)
116     ELSE()
117       SET(file_name ${file})
118       IF(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
119         SET(PREFIX "${SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY}/")
120       ENDIF(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
121     ENDIF(IS_ABSOLUTE ${file})
122     INSTALL(FILES ${PREFIX}${file} DESTINATION ${path} PERMISSIONS ${PERMS})
123     GET_FILENAME_COMPONENT(ext ${file} EXT)
124     IF(ext STREQUAL .py)
125       INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${path}/${file_name}\")")
126       INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${path}/${file_name}')\")")
127       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
128       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
129     ENDIF(ext STREQUAL .py)
130   ENDFOREACH(file ${file_list})
131 ENDMACRO(SALOME_INSTALL_SCRIPTS)
132
133 #----------------------------------------------------------------------------
134 # SALOME_INSTALL_SCRIPTS is a macro useful for installing executable scripts.
135 # ARGUMENTS:
136 # PYFILE2COMPINST: IN : list of python files to be installed.
137 # PYFILELOC: IN : full pathname for installing.
138 # Permissions of installed files: OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ
139 #----------------------------------------------------------------------------
140 MACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
141   INSTALL(CODE "SET(PYTHON_FILE ${f})")
142   FOREACH(input ${PYFILE2COMPINST})
143     GET_FILENAME_COMPONENT(inputname ${input} NAME)
144     INSTALL(FILES ${input} DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYFILELOC})
145     INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}\")")
146     INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}')\")")
147     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
148     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
149   ENDFOREACH(input ${PYFILE2COMPINST})
150 ENDMACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
151
152 #----------------------------------------------------------------------------
153 # SALOME_CONFIGURE_FILE is a macro useful for copying a file to another location 
154 # and modify its contents.
155 #
156 # USAGE: SALOME_CONFIGURE_FILE(in_file out_file [INSTALL dir])
157 #
158 # ARGUMENTS:
159 # in_file: IN : input file (if relative path is given, full file path is computed from current source dir).
160 # out_file: IN : output file (if relative path is given, full file path is computed from current build dir).
161 # If INSTALL is specified, then 'out_file' will be installed to the 'dir' directory.
162 #----------------------------------------------------------------------------
163 MACRO(SALOME_CONFIGURE_FILE IN_FILE OUT_FILE)
164   IF(IS_ABSOLUTE ${IN_FILE})
165     SET(_in_file ${IN_FILE})
166   ELSE()
167     SET(_in_file ${CMAKE_CURRENT_SOURCE_DIR}/${IN_FILE})
168   ENDIF()
169   IF(IS_ABSOLUTE  ${OUT_FILE})
170     SET(_out_file ${OUT_FILE})
171   ELSE()
172     SET(_out_file ${CMAKE_CURRENT_BINARY_DIR}/${OUT_FILE})
173   ENDIF()
174   MESSAGE(STATUS "Creation of ${_out_file}")
175   CONFIGURE_FILE(${_in_file} ${_out_file} @ONLY)
176   PARSE_ARGUMENTS(SALOME_CONFIGURE_FILE "INSTALL" "" ${ARGN})
177   IF(SALOME_CONFIGURE_FILE_INSTALL)
178     INSTALL(FILES ${_out_file} DESTINATION ${SALOME_CONFIGURE_FILE_INSTALL})
179   ENDIF(SALOME_CONFIGURE_FILE_INSTALL)
180 ENDMACRO(SALOME_CONFIGURE_FILE)
181
182
183 #######################################################################################
184 # Useful macros for SALOME own package detection system
185 #
186
187 ###
188 # SALOME_CHECK_EQUAL_PATHS(result path1 path2)
189 #  Check if two paths are identical, resolving links. If the paths do not exist a simple
190 #  text comparison is performed.
191 #  result is a boolean.
192 ###
193 MACRO(SALOME_CHECK_EQUAL_PATHS varRes path1 path2)  
194   SET("${varRes}" OFF)
195   IF(EXISTS "${path1}")
196     GET_FILENAME_COMPONENT(_tmp1 "${path1}" REALPATH)
197   ELSE()
198     SET(_tmp1 "${path1}")
199   ENDIF() 
200
201   IF(EXISTS "${path2}")
202     GET_FILENAME_COMPONENT(_tmp2 "${path2}" REALPATH)
203   ELSE()
204     SET(_tmp2 "${path2}")
205   ENDIF() 
206
207   IF("${_tmp1}" STREQUAL "${_tmp2}")
208     SET("${varRes}" ON)
209   ENDIF()
210 #  MESSAGE(${${varRes}})
211 ENDMACRO()
212
213 ####
214 # SALOME_LOG_OPTIONAL_PACKAGE(pkg flag)
215 #
216 # Register in global variables the detection status (found or not) of the optional package 'pkg' 
217 # and the configuration flag that should be turned off to avoid detection of the package.
218 # The global variables are read again by SALOME_PACKAGE_REPORT_AND_CHECK to produce 
219 # a summary report of the detection status and stops the process if necessary.
220 MACRO(SALOME_LOG_OPTIONAL_PACKAGE pkg flag)
221   # Was the package found
222   STRING(TOUPPER ${pkg} _pkg_UC)
223   IF(${pkg}_FOUND OR ${_pkg_UC}_FOUND)
224     SET(_isFound TRUE)
225   ELSE()
226     SET(_isFound FALSE)
227   ENDIF()
228
229   # Is the package already in the list? Then update its status:
230   LIST(FIND _SALOME_OPTIONAL_PACKAGES_names ${pkg} _result)
231   IF(NOT ${_result} EQUAL -1)
232     LIST(REMOVE_AT _SALOME_OPTIONAL_PACKAGES_found ${_result})
233     LIST(REMOVE_AT _SALOME_OPTIONAL_PACKAGES_flags ${_result})
234     LIST(INSERT    _SALOME_OPTIONAL_PACKAGES_found ${_result} ${_isFound})
235     LIST(INSERT    _SALOME_OPTIONAL_PACKAGES_flags ${_result} ${flag})
236   ELSE()
237     # Otherwise insert it
238     LIST(APPEND _SALOME_OPTIONAL_PACKAGES_names ${pkg})
239     LIST(APPEND _SALOME_OPTIONAL_PACKAGES_found ${_isFound})
240     LIST(APPEND _SALOME_OPTIONAL_PACKAGES_flags ${flag})
241   ENDIF() 
242   
243 ENDMACRO(SALOME_LOG_OPTIONAL_PACKAGE)
244
245 ####
246 # SALOME_JUSTIFY_STRING()
247 #
248 # Justifies the string specified as an argument to the given length
249 # adding required number of spaces to the end. Does noting if input
250 # string is longer as required length.
251 # Puts the result to the output variable.
252 #
253 # USAGE: SALOME_JUSTIFY_STRING(input length result)
254 #
255 # ARGUMENTS:
256 #   input  [in] input string
257 #   length [in] required length of resulting string
258 #   result [out] name of variable where the result string is put to
259 #
260 MACRO(SALOME_JUSTIFY_STRING input length result)
261   SET(${result} ${input})
262   STRING(LENGTH ${input} _input_length)
263   MATH(EXPR _nb_spaces "${length}-${_input_length}-1")
264   IF (_nb_spaces GREATER 0)
265     FOREACH(_idx RANGE ${_nb_spaces})  
266       SET(${result} "${${result}} ")
267     ENDFOREACH()
268   ENDIF()
269 ENDMACRO(SALOME_JUSTIFY_STRING)
270
271 ####
272 # SALOME_PACKAGE_REPORT_AND_CHECK()
273 #
274 # Print a quick summary of the detection of optional prerequisites.
275 # If a package was not found, the configuration is stopped. The summary also indicates 
276 # which flag should be turned off to skip the detection of the package. 
277 #
278 # If optional JUSTIFY argument is specified, names of packages
279 # are left-justified to the given length; default value is 10.
280 #
281 # USAGE: SALOME_PACKAGE_REPORT_AND_CHECK([JUSTIFY length])
282 #
283 MACRO(SALOME_PACKAGE_REPORT_AND_CHECK)
284   SET(_will_fail OFF)
285   PARSE_ARGUMENTS(SALOME_PACKAGE_REPORT "JUSTIFY" "" ${ARGN})
286   IF(SALOME_PACKAGE_REPORT_JUSTIFY)
287     SET(_length ${SALOME_PACKAGE_REPORT_JUSTIFY})
288   ELSE()
289     SET(_length 10)
290   ENDIF()
291   MESSAGE(STATUS "") 
292   MESSAGE(STATUS "  Optional packages - Detection report ")
293   MESSAGE(STATUS "  ==================================== ")
294   MESSAGE(STATUS "")
295   LIST(LENGTH _SALOME_OPTIONAL_PACKAGES_names _list_len)
296   # Another CMake stupidity - FOREACH(... RANGE r) generates r+1 numbers ...
297   MATH(EXPR _range "${_list_len}-1")
298   FOREACH(_idx RANGE ${_range})  
299     LIST(GET _SALOME_OPTIONAL_PACKAGES_names ${_idx} _pkg_name)
300     LIST(GET _SALOME_OPTIONAL_PACKAGES_found ${_idx} _pkg_found)
301     LIST(GET _SALOME_OPTIONAL_PACKAGES_flags ${_idx} _pkg_flag)
302     SALOME_JUSTIFY_STRING(${_pkg_name} ${_length} _pkg_name)
303     IF(_pkg_found)
304       SET(_found_msg "Found")
305       SET(_flag_msg "")
306     ELSE()
307       SET(_will_fail ON)
308       SET(_found_msg "NOT Found")
309       SET(_flag_msg " - ${_pkg_flag} can be switched OFF to skip this prerequisite.")
310     ENDIF()
311     
312     MESSAGE(STATUS "  * ${_pkg_name}  ->  ${_found_msg}${_flag_msg}")
313   ENDFOREACH()
314   MESSAGE(STATUS "")
315   MESSAGE(STATUS "")
316   
317   # Failure if some packages were missing:
318   IF(_will_fail)
319     MESSAGE(FATAL_ERROR "Some required prerequisites have NOT been found. Take a look at the report above to fix this.")
320   ENDIF()
321 ENDMACRO(SALOME_PACKAGE_REPORT_AND_CHECK)
322
323 ####
324 # SALOME_FIND_PACKAGE(englobingPackageName standardPackageName modus [onlyTryQuietly])
325 #
326 # example:  SALOME_FIND_PACKAGE(SalomeVTK VTK CONFIG)
327 #
328 # Encapsulate the call to the standard FIND_PACKAGE(standardPackageName) passing all the options
329 # given when calling the command FIND_PACKAGE(SalomeXYZ). Those options are stored implicitly in 
330 # CMake variables: xyz__FIND_QUIETLY, xyz_FIND_REQUIRED, etc ...
331
332 # If a list of components was specified when invoking the initial FIND_PACKAGE(SalomeXyz ...) this is 
333 # also handled properly.
334 #
335 # Modus is either MODULE or CONFIG (cf standard FIND_PACKAGE() documentation).
336 # The last argument is optional and if set to TRUE will force the search to be OPTIONAL and QUIET.
337 # If the package is looked for in CONFIG mode, the standard system paths are skipped. If you still want a 
338 # system installation to be found in this mode, you have to set the ROOT_DIR variable explicitly to /usr (for
339 # example). 
340 #  
341 # This macro is to be called from within the FindSalomeXXXX.cmake file.
342 #
343 ####
344 MACRO(SALOME_FIND_PACKAGE englobPkg stdPkg mode)
345   SET(_OPT_ARG ${ARGV3})
346   # Only bother if the package was not already found:
347   # Some old packages use the lower case version - standard should be to always use
348   # upper case:
349   STRING(TOUPPER ${stdPkg} stdPkgUC)
350   IF(NOT (${stdPkg}_FOUND OR ${stdPkgUC}_FOUND))
351     IF(${englobPkg}_FIND_QUIETLY OR _OPT_ARG)
352       SET(_tmp_quiet "QUIET")
353     ELSE()
354       SET(_tmp_quiet)
355     ENDIF()  
356     IF(${englobPkg}_FIND_REQUIRED AND NOT _OPT_ARG)
357       SET(_tmp_req "REQUIRED")
358     ELSE()
359       SET(_tmp_req)
360     ENDIF()  
361     IF(${englobPkg}_FIND_VERSION_EXACT)
362       SET(_tmp_exact "EXACT")
363     ELSE()
364       SET(_tmp_exact)
365     ENDIF()
366
367     # Call the CMake FIND_PACKAGE() command:    
368     STRING(TOLOWER ${stdPkg} _pkg_lc)
369     IF(("${mode}" STREQUAL "NO_MODULE") OR ("${mode}" STREQUAL "CONFIG"))
370       # Hope to find direclty a CMake config file, indicating the SALOME CMake file
371       # paths (the command already looks in places like "share/cmake", etc ... by default)
372       # Note the options NO_CMAKE_BUILDS_PATH, NO_CMAKE_PACKAGE_REGISTRY to avoid (under Windows)
373       # looking into a previous CMake build done via a GUI, or into the Win registry.
374       # NO_CMAKE_SYSTEM_PATH and NO_SYSTEM_ENVIRONMENT_PATH ensure any _system_ files like 'xyz-config.cmake' 
375       # don't get loaded (typically Boost). To force their loading, set the XYZ_ROOT_DIR variable to '/usr'. 
376       # See documentation of FIND_PACKAGE() for full details.
377       
378       # Do we need to call the signature using components?
379       IF(${englobPkg}_FIND_COMPONENTS)
380         FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} 
381               NO_MODULE ${_tmp_quiet} ${_tmp_req} COMPONENTS ${${englobPkg}_FIND_COMPONENTS}
382               PATH_SUFFIXES "salome_adm/cmake_files" "adm_local/cmake_files"
383               NO_CMAKE_BUILDS_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PATH
384                 NO_SYSTEM_ENVIRONMENT_PATH)
385       ELSE()
386         FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} 
387               NO_MODULE ${_tmp_quiet} ${_tmp_req}
388               PATH_SUFFIXES "salome_adm/cmake_files" "adm_local/cmake_files"
389               NO_CMAKE_BUILDS_PATH NO_CMAKE_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PACKAGE_REGISTRY NO_CMAKE_SYSTEM_PATH
390                  NO_SYSTEM_ENVIRONMENT_PATH)
391       ENDIF()
392       MARK_AS_ADVANCED(${stdPkg}_DIR)
393       
394     ELSEIF("${mode}" STREQUAL "MODULE")
395     
396       # Do we need to call the signature using components?
397       IF(${englobPkg}_FIND_COMPONENTS)
398         FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} 
399               MODULE ${_tmp_quiet} ${_tmp_req} COMPONENTS ${${englobPkg}_FIND_COMPONENTS})
400       ELSE()
401         FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} 
402               MODULE ${_tmp_quiet} ${_tmp_req})
403       ENDIF()
404       
405     ELSE()
406     
407       MESSAGE(FATAL_ERROR "Invalid mode argument in the call to the macro SALOME_FIND_PACKAGE. Should be CONFIG or MODULE.")
408       
409     ENDIF()
410     
411   ENDIF()
412 ENDMACRO()
413
414
415 ####################################################################
416 # SALOME_FIND_PACKAGE_DETECT_CONFLICTS(pkg referenceVariable upCount)
417 #    pkg              : name of the system package to be detected
418 #    referenceVariable: variable containing a path that can be browsed up to 
419 # retrieve the package root directory (xxx_ROOT_DIR)
420 #    upCount          : number of times we have to go up from the path <referenceVariable>
421 # to obtain the package root directory.
422 #   
423 # For example:  SALOME_FIND_PACKAGE_DETECT_CONFLICTS(SWIG SWIG_EXECUTABLE 2) 
424 #
425 # Generic detection (and conflict check) procedure for package XYZ:
426 # 1. Load a potential env variable XYZ_ROOT_DIR as a default choice for the cache entry XYZ_ROOT_DIR
427 #    If empty, load a potential XYZ_ROOT_DIR_EXP as default value (path exposed by another package depending
428 # directly on XYZ)
429 # 2. Invoke FIND_PACKAGE() in this order:
430 #    * in CONFIG mode first (if possible): priority is given to a potential 
431 #    "XYZ-config.cmake" file
432 #    * then switch to the standard MODULE mode, appending on CMAKE_PREFIX_PATH 
433 # the above XYZ_ROOT_DIR variable
434 # 3. Extract the path actually found into a temp variable _XYZ_TMP_DIR
435 # 4. Warn if XYZ_ROOT_DIR is set and doesn't match what was found (e.g. when CMake found the system installation
436 #    instead of what is pointed to by XYZ_ROOT_DIR - happens when a typo in the content of XYZ_ROOT_DIR).
437 # 5. Conflict detection:
438 #    * check the temp variable against a potentially existing XYZ_ROOT_DIR_EXP
439 # 6. Finally expose what was *actually* found in XYZ_ROOT_DIR.  
440 # 7. Specific stuff: for example exposing a prerequisite of XYZ to the rest of the world for future 
441 # conflict detection. This is added after the call to the macro by the callee.
442 #
443 MACRO(SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS pkg referenceVariable upCount)
444   ##
445   ## 0. Initialization
446   ##
447   
448   # Package name, upper case
449   STRING(TOUPPER ${pkg} pkg_UC)
450
451   ##
452   ## 1. Load environment or any previously detected root dir for the package
453   ##
454   IF(DEFINED ENV{${pkg_UC}_ROOT_DIR})
455     FILE(TO_CMAKE_PATH "$ENV{${pkg_UC}_ROOT_DIR}" _${pkg_UC}_ROOT_DIR_ENV)
456     SET(_dflt_value "${_${pkg_UC}_ROOT_DIR_ENV}")
457   ELSE()
458     # will be blank if no package was previously loaded:
459     SET(_dflt_value "${${pkg_UC}_ROOT_DIR_EXP}")
460   ENDIF()
461
462   # Detect if the variable has been set on the command line or elsewhere:
463   IF(DEFINED ${pkg_UC}_ROOT_DIR)
464      SET(_var_already_there TRUE)
465   ELSE()
466      SET(_var_already_there FALSE)
467   ENDIF()
468   #   Make cache entry 
469   SET(${pkg_UC}_ROOT_DIR "${_dflt_value}" CACHE PATH "Path to ${pkg_UC} directory")
470
471   ##
472   ## 2. Find package - try CONFIG mode first (i.e. looking for XYZ-config.cmake)
473   ##
474   
475   # Override the variable - don't append to it, as it would give precedence
476   # to what was stored there before!  
477   SET(CMAKE_PREFIX_PATH "${${pkg_UC}_ROOT_DIR}")
478     
479   # Try find_package in config mode. This has the priority, but is 
480   # performed QUIET and not REQUIRED:
481   SALOME_FIND_PACKAGE("Salome${pkg}" ${pkg} NO_MODULE TRUE)
482   
483   IF (${pkg_UC}_FOUND OR ${pkg}_FOUND)
484     MESSAGE(STATUS "Found ${pkg} in CONFIG mode!")
485   ENDIF()
486
487   # Otherwise try the standard way (module mode, with the standard CMake Find*** macro):
488   # We do it quietly to produce our own error message, except if we are in debug mode:
489   IF(SALOME_CMAKE_DEBUG)
490     SALOME_FIND_PACKAGE("Salome${pkg}" ${pkg} MODULE FALSE)
491   ELSE()
492     SALOME_FIND_PACKAGE("Salome${pkg}" ${pkg} MODULE TRUE)
493   ENDIF()
494   
495   # Set the "FOUND" variable for the SALOME wrapper:
496   IF(${pkg_UC}_FOUND OR ${pkg}_FOUND)
497     SET(SALOME${pkg_UC}_FOUND TRUE)
498   ELSE()
499     SET(SALOME${pkg_UC}_FOUND FALSE)
500     IF(NOT Salome${pkg}_FIND_QUIETLY)
501       IF(Salome${pkg}_FIND_REQUIRED)
502          MESSAGE(FATAL_ERROR "Package ${pkg} couldn't be found - did you set the corresponing root dir correctly? "
503          "It currently contains ${pkg_UC}_ROOT_DIR=${${pkg_UC}_ROOT_DIR}  "
504          "Append -DSALOME_CMAKE_DEBUG=ON on the command line if you want to see the original CMake error.")
505       ELSE()
506          MESSAGE(WARNING "Package ${pkg} couldn't be found - did you set the corresponing root dir correctly? "
507          "It currently contains ${pkg_UC}_ROOT_DIR=${${pkg_UC}_ROOT_DIR}  "
508          "Append -DSALOME_CMAKE_DEBUG=ON on the command line if you want to see the original CMake error.")
509       ENDIF()
510     ENDIF()
511   ENDIF()
512   
513   IF (${pkg_UC}_FOUND OR ${pkg}_FOUND)
514     ## 3. Set the root dir which was finally retained by going up "upDir" times
515     ## from the given reference path. The variable "referenceVariable" may be a list.
516     ## In this case we take its first element. 
517     
518     # First test if the variable exists, warn otherwise:
519     IF(NOT DEFINED ${referenceVariable})
520       MESSAGE(WARNING "${pkg}: the reference variable '${referenceVariable}' used when calling the macro "
521       "SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS() is not defined.")
522     ENDIF()
523     
524     LIST(LENGTH ${referenceVariable} _tmp_len)
525     IF(_tmp_len)
526        LIST(GET ${referenceVariable} 0 _tmp_ROOT_DIR)
527     ELSE()
528        #  Note the double de-reference of "referenceVariable":
529        SET(_tmp_ROOT_DIR "${${referenceVariable}}")
530     ENDIF()
531     IF(${upCount}) 
532       FOREACH(_unused RANGE 1 ${upCount})        
533         GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH)
534       ENDFOREACH()
535     ENDIF()
536
537     ##
538     ## 4. Warn if CMake found something not located under ENV(XYZ_ROOT_DIR)
539     ##
540     IF(DEFINED ENV{${pkg_UC}_ROOT_DIR})
541       SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${_${pkg_UC}_ROOT_DIR_ENV}")
542       IF(NOT _res)
543         MESSAGE(WARNING "${pkg} was found, but not at the path given by the "
544             "environment ${pkg_UC}_ROOT_DIR! Is the variable correctly set? "
545             "The two paths are: ${_tmp_ROOT_DIR} and: ${_${pkg_UC}_ROOT_DIR_ENV}")
546         
547       ELSE()
548         MESSAGE(STATUS "${pkg} found directory matches what was specified in the ${pkg_UC}_ROOT_DIR variable, all good!")    
549       ENDIF()
550     ELSE()
551         IF(NOT _var_already_there) 
552           MESSAGE(STATUS "Variable ${pkg_UC}_ROOT_DIR was not explicitly defined. "
553           "An installation was found anyway: ${_tmp_ROOT_DIR}")
554         ENDIF()
555     ENDIF()
556
557     ##
558     ## 5. Conflict detection
559     ##     From another prerequisite using the package:
560     ##
561     IF(${pkg_UC}_ROOT_DIR_EXP)
562         SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${${pkg_UC}_ROOT_DIR_EXP}") 
563         IF(NOT _res)
564            MESSAGE(WARNING "Warning: ${pkg}: detected version conflicts with a previously found ${pkg}!"
565                            " The two paths are " ${_tmp_ROOT_DIR} " vs " ${${pkg_UC}_ROOT_DIR_EXP})
566         ELSE()
567             MESSAGE(STATUS "${pkg} directory matches what was previously exposed by another prereq, all good!")
568         ENDIF()        
569     ENDIF()
570     
571     ##
572     ## 6. Save the detected installation
573     ##
574     SET(${pkg_UC}_ROOT_DIR "${_tmp_ROOT_DIR}")
575      
576   ELSE()
577     MESSAGE(STATUS "${pkg} was not found.")  
578   ENDIF()
579   
580   SET(Salome${pkg}_FOUND "${pkg}_FOUND")
581 ENDMACRO(SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS)
582
583
584 ####################################################################
585 # SALOME_ADD_MPI_TO_HDF5()
586
587 # Overload the HDF5 flags so that they also contain MPI references.
588 # This is to be used when HDF5 was compiled with MPI support;
589 MACRO(SALOME_ADD_MPI_TO_HDF5)  
590   SET(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIRS} ${MPI_INCLUDE_DIRS})
591   SET(HDF5_DEFINITIONS "${HDF5_DEFINITIONS} ${MPI_DEFINITIONS}")
592   SET(HDF5_LIBRARIES ${HDF5_LIBRARIES} ${MPI_LIBRARIES})
593 ENDMACRO(SALOME_ADD_MPI_TO_HDF5)
594
595 ####################################################################
596 # SALOME_XVERSION()
597
598 # Computes hexadecimal version of SALOME package
599 #
600 # USAGE: SALOME_XVERSION(package)
601 #
602 # ARGUMENTS:
603 #
604 # package: IN: SALOME package name
605 #
606 # The macro reads SALOME package version from PACKAGE_VERSION variable
607 # (note package name in uppercase as assumed for SALOME modules);
608 # hexadecimal version value in form 0xAABBCC (where AA, BB and CC are
609 # major, minor and maintenance components of package version in
610 # hexadecimal form) is put to the PACKAGE_XVERSION variable
611 MACRO(SALOME_XVERSION pkg)
612   STRING(TOUPPER ${pkg} _pkg_UC)
613   IF(${_pkg_UC}_VERSION)
614     EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; t=sys.argv[-1].split(\".\") ; t[:]=(int(elt) for elt in t) ; sys.stdout.write(\"0x%02x%02x%02x\"%tuple(t))" ${${_pkg_UC}_VERSION}
615                     OUTPUT_VARIABLE ${_pkg_UC}_XVERSION)
616   ENDIF()
617 ENDMACRO(SALOME_XVERSION)
618
619 #########################################################################
620 # SALOME_ACCUMULATE_HEADERS()
621
622 # This macro is called in the various FindSalomeXYZ.cmake modules to accumulate
623 # internally the list of include headers to be saved for future export. 
624 # The full set of include is saved in a variable called 
625 #      _${PROJECT_NAME}_EXTRA_HEADERS
626 #
627 MACRO(SALOME_ACCUMULATE_HEADERS lst)
628   FOREACH(l IN LISTS ${lst})
629     LIST(FIND _${PROJECT_NAME}_EXTRA_HEADERS "${l}" _res)
630     IF(_res EQUAL "-1")
631       IF(NOT "${l}" STREQUAL "/usr/include")
632         LIST(APPEND _${PROJECT_NAME}_EXTRA_HEADERS "${l}")
633       ENDIF()
634     ENDIF()
635   ENDFOREACH()
636 ENDMACRO(SALOME_ACCUMULATE_HEADERS)