Salome HOME
Merge from V7_3_BR branch 18/12/2013
[modules/med.git] / adm_local_without_kernel / cmake_files / FindSalomePython.cmake
1 # Copyright (C) 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: Adrien Bruneton
20 #
21
22 # Python libraries and interpreter detection for SALOME
23 #
24 #  !! Please read the generic detection procedure in SalomeMacros.cmake !!
25 #
26 # The interpreter is found first, and if OK, the corresponding libraries are searched.
27 # We ensure the version of the libraries matches the one of the interpreter.
28 #
29 # We also look for an installation of NumPy, and if found the following variables are set
30 #   NUMPY_INCLUDE_DIR  - NumPy header location
31 #   NUMPY_DEFINITIONS  - compiler flag
32 # and are automatically appended to PYTHON_INCLUDE_DIRS (and PYTHON_DEFINITIONS resp.)    
33 #
34
35 # 1. Load environment or any previously detected Python
36 IF(DEFINED ENV{PYTHON_ROOT_DIR})
37   FILE(TO_CMAKE_PATH "$ENV{PYTHON_ROOT_DIR}" _PYTHON_ROOT_DIR_ENV)
38   SET(_dflt_value "${_PYTHON_ROOT_DIR_ENV}")
39 ELSE()
40   # will be blank if no Python was previously loaded
41   SET(_dflt_value "${PYTHON_ROOT_DIR_EXP}")
42 ENDIF()
43
44 #   Make cache entry 
45 SET(PYTHON_ROOT_DIR "${_dflt_value}" CACHE PATH "Path to Python directory (interpreter and libs)")
46
47 # 2. Find package - config mode first (i.e. looking for XYZ-config.cmake)
48 IF(WIN32)
49  IF(CMAKE_BUILD_TYPE STREQUAL Debug)
50   SET(PythonInterp_FIND_VERSION _d)
51   SET(PYTHON_DEFINITIONS "-DHAVE_DEBUG_PYTHON")
52  ENDIF(CMAKE_BUILD_TYPE STREQUAL Debug)
53 ENDIF(WIN32)
54 IF(EXISTS "${PYTHON_ROOT_DIR}")
55   # Hope to find direclty a CMake config file there
56   SET(_CONF_DIR "${PYTHON_ROOT_DIR}/share/cmake") 
57
58   # Try find_package in config mode with a hard-coded guess. This
59   # has the priority.
60   FIND_PACKAGE(Python CONFIG QUIET PATHS "${_CONF_DIR}")
61   MARK_AS_ADVANCED(Python_DIR)
62     
63   IF (NOT PYTHON_FOUND)  
64     LIST(APPEND CMAKE_PREFIX_PATH "${PYTHON_ROOT_DIR}")
65   ELSE()
66     MESSAGE(STATUS "Found Python in CONFIG mode!")
67   ENDIF()
68 ENDIF()
69
70 # Otherwise try the standard way (module mode, with the standard CMake Find*** macro):
71 SALOME_FIND_PACKAGE(SalomePython PythonInterp MODULE)
72 SET(_found1 ${PYTHONINTERP_FOUND})
73
74 IF (PYTHONINTERP_FOUND)
75   # Now ensure we find the Python libraries matching the interpreter:
76   # This uses the variable PYTHON_EXECUTABLE
77
78   GET_FILENAME_COMPONENT(_python_bin "${PYTHON_EXECUTABLE}" NAME )
79   SET(PYTHONBIN "${_python_bin}" CACHE STRING "Name of Python interpreter")
80
81   GET_FILENAME_COMPONENT(_python_dir "${PYTHON_EXECUTABLE}" PATH)
82   GET_FILENAME_COMPONENT(CMAKE_INCLUDE_PATH "${_python_dir}/../include/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" ABSOLUTE)
83   GET_FILENAME_COMPONENT(CMAKE_LIBRARY_PATH "${_python_dir}/../lib" ABSOLUTE)
84   # For a Windows install, this might look more like this:
85   IF(WIN32)
86     LIST(APPEND CMAKE_LIBRARY_PATH "${_python_dir}/libs" ABSOLUTE)
87     LIST(APPEND CMAKE_INCLUDE_PATH "${_python_dir}/include" ABSOLUTE)
88   ENDIF()
89   # Override the EXACT and VERSION settings of the SalomePython module
90   # to force the next call to SALOME_FIND_PACKAGE() to find the exact matching
91   # version:
92   SET(_old_EXACT ${SalomePython_FIND_VERSION_EXACT})
93   SET(_old_VERSION "${SalomePython_FIND_VERSION}")
94   SET(SalomePython_FIND_VERSION_EXACT TRUE)
95   SET(SalomePython_FIND_VERSION "${PYTHON_VERSION_STRING}")
96   # Prepare call to FIND_PACKAGE(PythonLibs) and ensure priority is given to 
97   # the location found for the interpreter:
98   GET_FILENAME_COMPONENT(_tmp "${_python_dir}" PATH)
99 #  SET(PYTHON_LIBRARY ${_tmp}/lib)
100 #  SET(PYTHON_INCLUDE_DIR ${_tmp}/include)
101   SALOME_FIND_PACKAGE(SalomePython PythonLibs MODULE)
102   # Restore variables:
103   SET(SalomePython_FIND_VERSION_EXACT ${_old_EXACT})
104   SET(SalomePython_FIND_VERSION "${_old_VERSION}")
105 ENDIF()
106
107 # Set the FOUND flag for SalomePython and Python:
108 SET(SALOMEPYTHON_FOUND FALSE)
109 IF (_found1 AND PYTHONLIBS_FOUND)
110   SET(SALOMEPYTHON_FOUND TRUE)
111   SET(Python_FOUND TRUE)
112 ELSE()
113 SET(SALOMEPYTHON_FOUND FALSE)
114   SET(Python_FOUND FALSE)
115 ENDIF()
116
117 IF (SALOMEPYTHON_FOUND)
118   MESSAGE(STATUS "Python interpreter and Python libraries found:")
119   MESSAGE(STATUS "Python libraries: ${PYTHON_LIBRARY}")
120   MESSAGE(STATUS "Python include dir: ${PYTHON_INCLUDE_DIR}")
121
122   # 3. Set the root dir which was finally retained 
123   # For Python this is the grand-parent of the
124   # include directory:
125   GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${PYTHON_INCLUDE_DIR}" PATH)
126   IF(NOT WIN32)
127     GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH)
128   ENDIF()
129
130   # 4. Warn if CMake found something not located under ENV(XYZ_ROOT_DIR)
131   IF(DEFINED ENV{PYTHON_ROOT_DIR})
132     SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${_PYTHON_ROOT_DIR_ENV}")
133     IF(NOT _res)
134       MESSAGE(WARNING "Python was found, but not a the path given by the "
135 "environment PYTHON_ROOT_DIR! Is the variable correctly set?")
136     ELSE()
137       MESSAGE(STATUS "Python found directory matches what was specified in the PYTHON_ROOT_DIR, all good!")    
138     ENDIF()
139   ENDIF()
140
141   # 5. Conflict detection
142   # 5.1  From another prerequisite using Python
143   IF(PYTHON_ROOT_DIR_EXP)
144       SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${PYTHON_ROOT_DIR_EXP}") 
145       IF(NOT _res)
146          MESSAGE(WARNING "Warning: Python: detected version conflicts with a previously found Python!"
147                           "The two paths are " ${_tmp_ROOT_DIR} " vs " ${PYTHON_ROOT_DIR_EXP})
148       ELSE()
149           MESSAGE(STATUS "Python directory matches what was previously exposed by another prereq, all good!")
150       ENDIF()        
151   ENDIF()
152
153   ##
154   ## 6. Save the final detected installation
155   ##
156   SET(PYTHON_ROOT_DIR "${_tmp_ROOT_DIR}")
157   SET(PYTHON_PYTHONPATH "${_tmp_ROOT_DIR}/lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
158
159   ## 7. Specifics
160   ##
161   
162   # NumPy detection 
163   EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy ; import sys ; sys.stdout.write(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIR ERROR_QUIET )
164   IF(NUMPY_INCLUDE_DIR)
165     SET(NUMPY_FOUND TRUE)
166   ENDIF(NUMPY_INCLUDE_DIR)
167   IF(NUMPY_FOUND)
168     SET(PYTHON_INCLUDE_DIRS ${NUMPY_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
169     SET(PYTHON_DEFINITIONS "${PYTHON_DEFINITIONS} -DWITH_NUMPY")
170     MESSAGE(STATUS "NumPy found : ${NUMPY_INCLUDE_DIR}")
171   ELSE(NUMPY_FOUND)
172     MESSAGE(STATUS "NumPy not found.")
173   ENDIF(NUMPY_FOUND)
174   # SciPy detection
175   EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import scipy ; import sys ; sys.stdout.write(scipy.version.version)" OUTPUT_VARIABLE SCIPY_VERSION ERROR_QUIET )
176   IF(SCIPY_VERSION)
177     SET(SCIPY_FOUND TRUE)
178   ENDIF(SCIPY_VERSION)
179   IF(SCIPY_FOUND)
180     MESSAGE(STATUS "Scipy found : Version ${SCIPY_VERSION}")
181   ENDIF(SCIPY_FOUND)
182   ## None here    
183 ELSE()
184   MESSAGE(STATUS "Python was only partially (or not at all) found .")
185 ENDIF()
186
187 IF(SALOMEPYTHON_FOUND) 
188   SALOME_ACCUMULATE_HEADERS(PYTHON_INCLUDE_DIR)
189   SALOME_ACCUMULATE_ENVIRONMENT(PATH ${PYTHON_EXECUTABLE})
190   SALOME_ACCUMULATE_ENVIRONMENT(LD_LIBRARY_PATH ${PYTHON_LIBRARIES})
191   SALOME_ACCUMULATE_ENVIRONMENT(PYTHONPATH ${PYTHON_PYTHONPATH})
192 ENDIF()