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