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