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