Salome HOME
New CMake procedure.
[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(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
115       SET(PREFIX "${SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY}/")
116     ENDIF(SALOME_INSTALL_SCRIPTS_WORKING_DIRECTORY)
117     INSTALL(FILES ${PREFIX}${file} DESTINATION ${path} PERMISSIONS ${PERMS})
118     GET_FILENAME_COMPONENT(ext ${file} EXT)
119     IF(ext STREQUAL .py)
120       INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${path}/${file}\")")
121       INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${path}/${file}')\")")
122       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
123       INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
124     ENDIF(ext STREQUAL .py)
125   ENDFOREACH(file ${file_list})
126 ENDMACRO(SALOME_INSTALL_SCRIPTS)
127
128 #----------------------------------------------------------------------------
129 # SALOME_INSTALL_SCRIPTS is a macro useful for installing executable scripts.
130 # ARGUMENTS:
131 # PYFILE2COMPINST: IN : list of python files to be installed.
132 # PYFILELOC: IN : full pathname for installing.
133 # Permissions of installed files: OWNER_WRITE, OWNER_READ, GROUP_READ, and WORLD_READ
134 #----------------------------------------------------------------------------
135 MACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
136   INSTALL(CODE "SET(PYTHON_FILE ${f})")
137   FOREACH(input ${PYFILE2COMPINST})
138     GET_FILENAME_COMPONENT(inputname ${input} NAME)
139     INSTALL(FILES ${input} DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYFILELOC})
140     INSTALL(CODE "MESSAGE(STATUS \"py compiling ${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}\")")
141     INSTALL(CODE "SET(CMD \"import py_compile ; py_compile.compile('${CMAKE_INSTALL_PREFIX}/${PYFILELOC}/${inputname}')\")")
142     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c \"\${CMD}\")")
143     INSTALL(CODE "EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -O -c \"\${CMD}\")")
144   ENDFOREACH(input ${PYFILE2COMPINST})
145 ENDMACRO(INSTALL_AND_COMPILE_PYTHON_FILE PYFILE2COMPINST PYFILELOC)
146
147 #----------------------------------------------------------------------------
148 # SALOME_CONFIGURE_FILE is a macro useful for copying a file to another location 
149 # and modify its contents.
150 #
151 # USAGE: SALOME_CONFIGURE_FILE(in_file out_file [INSTALL dir])
152 #
153 # ARGUMENTS:
154 # in_file: IN : input file with full paths.
155 # out_file: IN : output file with full paths.
156 # If INSTALL is specified, then 'out_file' will be installed to the 'dir' directory.
157 #----------------------------------------------------------------------------
158 MACRO(SALOME_CONFIGURE_FILE IN_FILE OUT_FILE)
159   MESSAGE(STATUS "Creation of ${OUT_FILE}")
160   CONFIGURE_FILE(${IN_FILE} ${OUT_FILE} @ONLY)
161   PARSE_ARGUMENTS(SALOME_CONFIGURE_FILE "INSTALL" "" ${ARGN})
162   IF(SALOME_CONFIGURE_FILE_INSTALL)
163     INSTALL(FILES ${OUT_FILE} DESTINATION ${SALOME_CONFIGURE_FILE_INSTALL})
164   ENDIF(SALOME_CONFIGURE_FILE_INSTALL)
165 ENDMACRO(SALOME_CONFIGURE_FILE)
166
167
168 #######################################################################################
169 # Useful macros for SALOME own package detection system
170 #
171
172 ###
173 # SALOME_CHECK_EQUAL_PATHS(result path1 path2)
174 #  Check if two paths are identical, resolving links. If the paths do not exist a simple
175 #  text comparison is performed.
176 #  result is a boolean.
177 ###
178 MACRO(SALOME_CHECK_EQUAL_PATHS varRes path1 path2)  
179   SET("${varRes}" OFF)
180   IF(EXISTS "${path1}")
181     GET_FILENAME_COMPONENT(_tmp1 "${path1}" REALPATH)
182   ELSE()
183     SET(_tmp1 "${path1}")
184   ENDIF() 
185
186   IF(EXISTS "${path2}")
187     GET_FILENAME_COMPONENT(_tmp2 "${path2}" REALPATH)
188   ELSE()
189     SET(_tmp2 "${path2}")
190   ENDIF() 
191
192   IF("${_tmp1}" STREQUAL "${_tmp2}")
193     SET("${varRes}" ON)
194   ENDIF()
195 #  MESSAGE(${${varRes}})
196 ENDMACRO()
197
198
199 ####
200 # SALOME_FIND_PACKAGE(englobingPackageName standardPackageName modus)
201 # Encapsulate the call to the standard FIND_PACKAGE(standardPackageName) passing all the options
202 # given when calling the command FIND_PACKAGE(customPackageName)
203 # Modus is either MODULE or CONFIG (cf standard FIND_PACKAGE() documentation).
204 # This macro is to be called from within the FindCustomPackage.cmake file.
205 ####
206 MACRO(SALOME_FIND_PACKAGE englobPkg stdPkg mode)
207   # Only bother if the package was not already found:
208   # Some old packages use the lower case version - standard should be to always use
209   # upper case:
210   STRING(TOUPPER ${stdPkg} stdPkgUC)
211   IF(NOT (${stdPkg}_FOUND OR ${stdPkgUC}_FOUND))
212     IF(${englobPkg}_FIND_QUIETLY)
213       SET(_tmp_quiet "QUIET")
214     ELSE()
215       SET(_tmp_quiet)
216     ENDIF()  
217     IF(${englobPkg}_FIND_REQUIRED)
218       SET(_tmp_req "REQUIRED")
219     ELSE()
220       SET(_tmp_req)
221     ENDIF()  
222     IF(${englobPkg}_FIND_VERSION_EXACT)
223       SET(_tmp_exact "EXACT")
224     ELSE()
225       SET(_tmp_exact)
226     ENDIF()
227     IF(${englobPkg}_FIND_COMPONENTS)
228       STRING(REPLACE ";" " " _tmp_compo ${${englobPkg}_FIND_COMPONENTS})
229     ELSE()
230       SET(_tmp_compo)
231     ENDIF()
232
233     # Call the root FIND_PACKAGE():
234     IF(_tmp_compo)
235       FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} ${mode} ${_tmp_quiet} ${_tmp_req} COMPONENTS ${_tmp_compo})
236     ELSE()
237       FIND_PACKAGE(${stdPkg} ${${englobPkg}_FIND_VERSION} ${_tmp_exact} ${mode} ${_tmp_quiet} ${_tmp_req})
238     ENDIF()
239   ENDIF()
240 ENDMACRO()
241
242
243 ####################################################################"
244 # SALOME_FIND_PACKAGE_DETECT_CONFLICTS(pkg referenceVariable upCount)
245 #    pkg              : name of the system package to be detected
246 #    referenceVariable: variable containing a path that can be browsed up to 
247 # retrieve the package root directory (xxx_ROOT_DIR)
248 #    upCount          : number of times we have to go up from the path <referenceVariable>
249 # to obtain the package root directory.  
250 #   
251 # For example:  SALOME_FIND_PACKAGE_DETECT_CONFLICTS(SWIG SWIG_EXECUTABLE 2) 
252 #
253 # Generic detection (and conflict check) procedure for package XYZ:
254 # 1. Load a potential env variable XYZ_ROOT_DIR as a default choice for the cache entry XYZ_ROOT_DIR
255 #    If empty, load a potential XYZ_ROOT_DIR_EXP as default value (path exposed by another package depending
256 # directly on XYZ)
257 # 2. Invoke FIND_PACKAGE() in this order:
258 #    * in CONFIG mode first (if possible): priority is given to a potential 
259 #    "XYZ-config.cmake" file
260 #    * then switch to the standard MODULE mode, appending on CMAKE_PREFIX_PATH 
261 # the above XYZ_ROOT_DIR variable
262 # 3. Extract the path actually found into a temp variable _XYZ_TMP_DIR
263 # 4. Warn if XYZ_ROOT_DIR is set and doesn't match what was found (e.g. when CMake found the system installation
264 #    instead of what is pointed to by XYZ_ROOT_DIR - happens when a typo in the content of XYZ_ROOT_DIR).
265 # 5. Conflict detection:
266 #    * check the temp variable against a potentially existing XYZ_ROOT_DIR_EXP
267 # 6. Finally expose what was *actually* found in XYZ_ROOT_DIR.  
268 # 7. Specific stuff: for example exposing a prerequisite of XYZ to the rest of the world for future 
269 # conflict detection. This is added after the call to the macro by the callee.
270 #
271 MACRO(SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS pkg referenceVariable upCount)
272   STRING(TOUPPER ${pkg} pkg_UC)
273
274   # 1. Load environment or any previously detected root dir for the package
275   IF(DEFINED ENV{${pkg_UC}_ROOT_DIR})
276     FILE(TO_CMAKE_PATH "$ENV{${pkg_UC}_ROOT_DIR}" _${pkg_UC}_ROOT_DIR_ENV)
277     SET(_dflt_value "${_${pkg_UC}_ROOT_DIR_ENV}")
278   ELSE()
279     # will be blank if no package was previously loaded
280     SET(_dflt_value "${${pkg_UC}_ROOT_DIR_EXP}")
281   ENDIF()
282
283   #   Make cache entry 
284   SET(${pkg_UC}_ROOT_DIR "${_dflt_value}" CACHE PATH "Path to ${pkg_UC} directory")
285
286   # 2. Find package - config mode first (i.e. looking for XYZ-config.cmake)
287   IF(EXISTS "${${pkg_UC}_ROOT_DIR}")
288     # Hope to find direclty a CMake config file there
289     SET(_CONF_DIR "${${pkg_UC}_ROOT_DIR}/share/cmake") 
290
291     # Try find_package in config mode with a hard-coded guess. This
292     # has the priority.
293     STRING(TOLOWER ${pkg} _pkg_lc)
294     FIND_PACKAGE(${pkg} NO_MODULE QUIET PATHS "${_CONF_DIR}" "${_CONF_DIR}/${pkg}" 
295               "${_CONF_DIR}/${_pkg_lc}")
296     MARK_AS_ADVANCED(${pkg}_DIR)
297     
298     IF (NOT (${pkg_UC}_FOUND OR ${pkg}_FOUND))  
299       LIST(APPEND CMAKE_PREFIX_PATH "${${pkg_UC}_ROOT_DIR}")
300     ELSE()
301       MESSAGE(STATUS "Found ${pkg} in CONFIG mode!")
302     ENDIF()
303   ENDIF()
304
305   # Otherwise try the standard way (module mode, with the standard CMake Find*** macro):
306   SALOME_FIND_PACKAGE("Salome${pkg}" ${pkg} MODULE)
307   #MESSAGE("dbg ${pkg_UC} / ${PTHREAD_FOUND} / ${${pkg_UC}_FOUND}")
308   # Set the "FOUND" variable for the SALOME wrapper:
309   IF(${pkg_UC}_FOUND OR ${pkg}_FOUND)
310     SET(SALOME${pkg_UC}_FOUND TRUE)
311   ELSE()
312     SET(SALOME${pkg_UC}_FOUND FALSE)
313   ENDIF()
314   
315   IF (${pkg_UC}_FOUND OR ${pkg}_FOUND)
316     # 3. Set the root dir which was finally retained by going up "upDir" times
317     # from the given reference path. The variable "referenceVariable" may be a list
318     # - we take its first element. 
319     #   Note the double de-reference of "referenceVariable":
320     LIST(LENGTH "${${referenceVariable}}" _tmp_len)
321     IF(_tmp_len)
322        LIST(GET "${${referenceVariable}}" 0 _tmp_ROOT_DIR)
323     ELSE()
324        SET(_tmp_ROOT_DIR "${${referenceVariable}}")
325     ENDIF()
326     IF(${upCount})
327       MATH(EXPR _rge "${upCount}-1") 
328       FOREACH(_unused RANGE ${_rge})        
329         GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH)
330       ENDFOREACH()
331     ENDIF()
332
333     # 4. Warn if CMake found something not located under ENV(XYZ_ROOT_DIR)
334     IF(DEFINED ENV{${pkg_UC}_ROOT_DIR})
335       SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${_${pkg_UC}_ROOT_DIR_ENV}")
336       IF(NOT _res)
337         MESSAGE(WARNING "${pkg} was found, but not at the path given by the "
338             "environment ${pkg_UC}_ROOT_DIR! Is the variable correctly set? "
339             "The two paths are: ${_tmp_ROOT_DIR} and: ${_${pkg_UC}_ROOT_DIR_ENV}")
340         
341       ELSE()
342         MESSAGE(STATUS "${pkg} found directory matches what was specified in the ${pkg_UC}_ROOT_DIR variable, all good!")    
343       ENDIF()
344     ELSE()
345         MESSAGE(STATUS "Environment variable ${pkg_UC}_ROOT_DIR is not defined. The system installation was found.")
346     ENDIF()
347
348     # 5. Conflict detection
349     # 5.1  From another prerequisite using the package
350     IF(${pkg_UC}_ROOT_DIR_EXP)
351         SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${${pkg_UC}_ROOT_DIR_EXP}") 
352         IF(NOT _res)
353            MESSAGE(WARNING "Warning: ${pkg}: detected version conflicts with a previously found ${pkg}!"
354                             "The two paths are " ${_tmp_ROOT_DIR} " vs " ${${pkg_UC}_ROOT_DIR_EXP})
355         ELSE()
356             MESSAGE(STATUS "${pkg} directory matches what was previously exposed by another prereq, all good!")
357         ENDIF()        
358     ENDIF()
359     
360     # 6. Save the found installation
361     #
362     SET(${pkg_UC}_ROOT_DIR "${_tmp_ROOT_DIR}")
363      
364   ELSE()
365     MESSAGE(STATUS "${pkg} was not found.")  
366   ENDIF()
367 ENDMACRO(SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS)
368