Salome HOME
Improve CPPUNIT detection to avoid -ldl problem on link.
[modules/kernel.git] / salome_adm / 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
30 # 1. Load environment or any previously detected Python
31 IF(DEFINED ENV{PYTHON_ROOT_DIR})
32   FILE(TO_CMAKE_PATH "$ENV{PYTHON_ROOT_DIR}" _PYTHON_ROOT_DIR_ENV)
33   SET(_dflt_value "${_PYTHON_ROOT_DIR_ENV}")
34 ELSE()
35   # will be blank if no Python was previously loaded
36   SET(_dflt_value "${PYTHON_ROOT_DIR_EXP}")
37 ENDIF()
38
39 #   Make cache entry 
40 SET(PYTHON_ROOT_DIR "${_dflt_value}" CACHE PATH "Path to Python directory (interpreter and libs)")
41
42 # 2. Find package - config mode first (i.e. looking for XYZ-config.cmake)
43 IF(EXISTS "${PYTHON_ROOT_DIR}")
44   # Hope to find direclty a CMake config file there
45   SET(_CONF_DIR "${PYTHON_ROOT_DIR}/share/cmake") 
46
47   # Try find_package in config mode with a hard-coded guess. This
48   # has the priority.
49   FIND_PACKAGE(Python CONFIG QUIET PATHS "${_CONF_DIR}")
50   MARK_AS_ADVANCED(Python_DIR)
51     
52   IF (NOT PYTHON_FOUND)  
53     LIST(APPEND CMAKE_PREFIX_PATH "${PYTHON_ROOT_DIR}")
54   ELSE()
55     MESSAGE(STATUS "Found Python in CONFIG mode!")
56   ENDIF()
57 ENDIF()
58
59 # Otherwise try the standard way (module mode, with the standard CMake Find*** macro):
60 SALOME_FIND_PACKAGE(SalomePython PythonInterp MODULE)
61 SET(_found1 ${PYTHONINTERP_FOUND})
62
63 IF (PYTHONINTERP_FOUND)
64   # Now ensure we find the Python libraries matching the interpreter:
65   # This uses the variable PYTHON_EXECUTABLE
66   GET_FILENAME_COMPONENT(_python_dir "${PYTHON_EXECUTABLE}" PATH)
67   GET_FILENAME_COMPONENT(CMAKE_INCLUDE_PATH "${_python_dir}/../include/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" ABSOLUTE)
68   GET_FILENAME_COMPONENT(CMAKE_LIBRARY_PATH "${_python_dir}/../lib" ABSOLUTE)
69   # For a Windows install, this might look more like this:
70   IF(WIN32)
71     LIST(APPEND CMAKE_LIBRARY_PATH "${_python_dir}/libs" ABSOLUTE)
72     LIST(APPEND CMAKE_INCLUDE_PATH "${_python_dir}/include" ABSOLUTE)
73   ENDIF()
74   # Override the EXACT and VERSION settings of the SalomePython module
75   # to force the next call to SALOME_FIND_PACKAGE() to find the exact matching
76   # version:
77   SET(_old_EXACT ${SalomePython_FIND_VERSION_EXACT})
78   SET(_old_VERSION "${SalomePython_FIND_VERSION}")
79   SET(SalomePython_FIND_VERSION_EXACT TRUE)
80   SET(SalomePython_FIND_VERSION "${PYTHON_VERSION_STRING}")
81   # Prepare call to FIND_PACKAGE(PythonLibs) and ensure priority is given to 
82   # the location found for the interpreter:
83   GET_FILENAME_COMPONENT(_tmp "${_python_dir}" PATH)
84 #  SET(PYTHON_LIBRARY ${_tmp}/lib)
85 #  SET(PYTHON_INCLUDE_DIR ${_tmp}/include)
86   SALOME_FIND_PACKAGE(SalomePython PythonLibs MODULE)
87   # Restore variables:
88   SET(SalomePython_FIND_VERSION_EXACT ${_old_EXACT})
89   SET(SalomePython_FIND_VERSION "${_old_VERSION}")
90 ENDIF()
91
92 # Set the FOUND flag for SalomePython:
93 SET(SALOMEPYTHON_FOUND FALSE)
94 IF (_found1 AND PYTHONLIBS_FOUND)
95   SET(SALOMEPYTHON_FOUND TRUE)
96 ENDIF()
97
98 IF (SALOMEPYTHON_FOUND)
99   MESSAGE(STATUS "Python interpreter and Python libraries found:")
100   MESSAGE(STATUS "Python libraries: ${PYTHON_LIBRARY}")
101   MESSAGE(STATUS "Python include dir: ${PYTHON_INCLUDE_DIR}")
102
103   # 3. Set the root dir which was finally retained 
104   # For Python this is the grand-parent of the
105   # include directory:
106   GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${PYTHON_INCLUDE_DIR}" PATH)
107   IF(NOT WIN32)
108     GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH)
109   ENDIF()
110
111   # 4. Warn if CMake found something not located under ENV(XYZ_ROOT_DIR)
112   IF(DEFINED ENV{PYTHON_ROOT_DIR})
113     SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${_PYTHON_ROOT_DIR_ENV}")
114     IF(NOT _res)
115       MESSAGE(WARNING "Python was found, but not a the path given by the "
116 "environment PYTHON_ROOT_DIR! Is the variable correctly set?")
117     ELSE()
118       MESSAGE(STATUS "Python found directory matches what was specified in the PYTHON_ROOT_DIR, all good!")    
119     ENDIF()
120   ENDIF()
121
122   # 5. Conflict detection
123   # 5.1  From another prerequisite using Python
124   IF(PYTHON_ROOT_DIR_EXP)
125       SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${PYTHON_ROOT_DIR_EXP}") 
126       IF(NOT _res)
127          MESSAGE(WARNING "Warning: Python: detected version conflicts with a previously found Python!"
128                           "The two paths are " ${_tmp_ROOT_DIR} " vs " ${PYTHON_ROOT_DIR_EXP})
129       ELSE()
130           MESSAGE(STATUS "Python directory matches what was previously exposed by another prereq, all good!")
131       ENDIF()        
132   ENDIF()
133
134   # 6. Specifics
135   ## None here    
136 ELSE()
137   MESSAGE(STATUS "Python was only partially (or not at all) found .")  
138 ENDIF()
139
140