From c374a03bb567ca9ce9b3b18dcf95a6231a615a87 Mon Sep 17 00:00:00 2001 From: abn Date: Thu, 4 Feb 2016 11:51:48 +0100 Subject: [PATCH] CMake: simplifying Python detection mechanism. --- CMakeLists.txt | 3 +- salome_adm/cmake_files/CMakeLists.txt | 4 +- salome_adm/cmake_files/FindSalomePython.cmake | 200 ------------------ .../cmake_files/FindSalomePythonInterp.cmake | 77 +++++++ .../cmake_files/FindSalomePythonLibs.cmake | 49 +++++ 5 files changed, 131 insertions(+), 202 deletions(-) delete mode 100644 salome_adm/cmake_files/FindSalomePython.cmake create mode 100644 salome_adm/cmake_files/FindSalomePythonInterp.cmake create mode 100644 salome_adm/cmake_files/FindSalomePythonLibs.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 384d9882a..f4a16cc78 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,7 +79,8 @@ IF(SALOME_USE_LIBBATCH) SALOME_LOG_OPTIONAL_PACKAGE(LibBatch SALOME_USE_LIBBATCH) ENDIF() -FIND_PACKAGE(SalomePython REQUIRED) +FIND_PACKAGE(SalomePythonInterp REQUIRED) +FIND_PACKAGE(SalomePythonLibs REQUIRED) FIND_PACKAGE(SalomePThread REQUIRED) FIND_PACKAGE(SalomeSWIG REQUIRED) FIND_PACKAGE(SalomeLibXml2 REQUIRED) diff --git a/salome_adm/cmake_files/CMakeLists.txt b/salome_adm/cmake_files/CMakeLists.txt index ed3f36a3d..f29187fea 100755 --- a/salome_adm/cmake_files/CMakeLists.txt +++ b/salome_adm/cmake_files/CMakeLists.txt @@ -45,7 +45,9 @@ SET(DATA FindSalomeOmniORB.cmake FindSalomeOmniORBPy.cmake FindSalomePThread.cmake - FindSalomePython.cmake +# FindSalomePython.cmake + FindSalomePythonLibs.cmake + FindSalomePythonInterp.cmake FindSalomeSphinx.cmake FindSalomeSWIG.cmake FindSphinx.cmake diff --git a/salome_adm/cmake_files/FindSalomePython.cmake b/salome_adm/cmake_files/FindSalomePython.cmake deleted file mode 100644 index 6d4cc2950..000000000 --- a/salome_adm/cmake_files/FindSalomePython.cmake +++ /dev/null @@ -1,200 +0,0 @@ -# Copyright (C) 2013-2015 CEA/DEN, EDF R&D, OPEN CASCADE -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -# -# Author: Adrien Bruneton -# - -# Python libraries and interpreter detection for SALOME -# -# !! Please read the generic detection procedure in SalomeMacros.cmake !! -# -# The interpreter is found first, and if OK, the corresponding libraries are searched. -# We ensure the version of the libraries matches the one of the interpreter. -# -# We also look for an installation of NumPy, and if found the following variables are set -# NUMPY_INCLUDE_DIR - NumPy header location -# NUMPY_DEFINITIONS - compiler flag -# and are automatically appended to PYTHON_INCLUDE_DIRS (and PYTHON_DEFINITIONS resp.) -# - -# 1. Load environment or any previously detected Python -IF(DEFINED ENV{PYTHON_ROOT_DIR}) - FILE(TO_CMAKE_PATH "$ENV{PYTHON_ROOT_DIR}" _PYTHON_ROOT_DIR_ENV) - SET(_dflt_value "${_PYTHON_ROOT_DIR_ENV}") -ELSE() - # will be blank if no Python was previously loaded - SET(_dflt_value "${PYTHON_ROOT_DIR_EXP}") -ENDIF() - -# Make cache entry -SET(PYTHON_ROOT_DIR "${_dflt_value}" CACHE PATH "Path to Python directory (interpreter and libs)") - -# 2. Find package - config mode first (i.e. looking for XYZ-config.cmake) -IF(WIN32) - IF(CMAKE_BUILD_TYPE STREQUAL Debug) - SET(PythonInterp_FIND_VERSION _d) - SET(PYTHON_DEFINITIONS "-DHAVE_DEBUG_PYTHON") - ENDIF(CMAKE_BUILD_TYPE STREQUAL Debug) -ENDIF(WIN32) -IF(EXISTS "${PYTHON_ROOT_DIR}") - # Hope to find direclty a CMake config file there - SET(_CONF_DIR "${PYTHON_ROOT_DIR}/share/cmake") - - # Try find_package in config mode with a hard-coded guess. This - # has the priority. - FIND_PACKAGE(Python CONFIG QUIET PATHS "${_CONF_DIR}") - MARK_AS_ADVANCED(Python_DIR) - - IF (NOT PYTHON_FOUND) - LIST(APPEND CMAKE_PREFIX_PATH "${PYTHON_ROOT_DIR}") - ELSE() - MESSAGE(STATUS "Found Python in CONFIG mode!") - ENDIF() -ENDIF() - -# Otherwise try the standard way (module mode, with the standard CMake Find*** macro): -SALOME_FIND_PACKAGE(SalomePython PythonInterp MODULE) -SET(_found1 ${PYTHONINTERP_FOUND}) - -IF (PYTHONINTERP_FOUND) - # Now ensure we find the Python libraries matching the interpreter: - # This uses the variable PYTHON_EXECUTABLE - - GET_FILENAME_COMPONENT(_python_bin "${PYTHON_EXECUTABLE}" NAME ) - SET(PYTHONBIN "${_python_bin}" CACHE STRING "Name of Python interpreter") - - GET_FILENAME_COMPONENT(_python_dir "${PYTHON_EXECUTABLE}" PATH) - GET_FILENAME_COMPONENT(CMAKE_INCLUDE_PATH "${_python_dir}/../include/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}" ABSOLUTE) - GET_FILENAME_COMPONENT(CMAKE_LIBRARY_PATH "${_python_dir}/../lib" ABSOLUTE) - # For a Windows install, this might look more like this: - IF(WIN32) - LIST(APPEND CMAKE_LIBRARY_PATH "${_python_dir}/libs" ABSOLUTE) - LIST(APPEND CMAKE_INCLUDE_PATH "${_python_dir}/include" ABSOLUTE) - ENDIF() - # Override the EXACT and VERSION settings of the SalomePython module - # to force the next call to SALOME_FIND_PACKAGE() to find the exact matching - # version: - SET(_old_EXACT ${SalomePython_FIND_VERSION_EXACT}) - SET(_old_VERSION "${SalomePython_FIND_VERSION}") - SET(SalomePython_FIND_VERSION_EXACT TRUE) - SET(SalomePython_FIND_VERSION "${PYTHON_VERSION_STRING}") - # Prepare call to FIND_PACKAGE(PythonLibs) and ensure priority is given to - # the location found for the interpreter: - GET_FILENAME_COMPONENT(_tmp "${_python_dir}" PATH) -# SET(PYTHON_LIBRARY ${_tmp}/lib) -# SET(PYTHON_INCLUDE_DIR ${_tmp}/include) - SALOME_FIND_PACKAGE(SalomePython PythonLibs MODULE) - # Restore variables: - SET(SalomePython_FIND_VERSION_EXACT ${_old_EXACT}) - SET(SalomePython_FIND_VERSION "${_old_VERSION}") -ENDIF() - -# Set the FOUND flag for SalomePython and Python: -SET(SALOMEPYTHON_FOUND FALSE) -IF (_found1 AND PYTHONLIBS_FOUND) - - # 24.03.2015 ANA: Fix problem on Windows in Debug mode - # If you have Python, installed by Windows MSI Installer, - # PYTHON_LIBRARIES variable contains redundant release libraries... - IF(WIN32 AND CMAKE_BUILD_TYPE STREQUAL Debug) - SET (PYTHON_LIBRARIES ${PYTHON_DEBUG_LIBRARIES}) - ENDIF() - - SET(SALOMEPYTHON_FOUND TRUE) - SET(Python_FOUND TRUE) -ELSE() -SET(SALOMEPYTHON_FOUND FALSE) - SET(Python_FOUND FALSE) -ENDIF() - -IF (SALOMEPYTHON_FOUND) - MESSAGE(STATUS "Python interpreter and Python libraries found:") - MESSAGE(STATUS "Python libraries: ${PYTHON_LIBRARIES}") - MESSAGE(STATUS "Python include dir: ${PYTHON_INCLUDE_DIR}") - - # 3. Set the root dir which was finally retained - # For Python this is the grand-parent of the - # include directory: - GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${PYTHON_INCLUDE_DIR}" PATH) - IF(NOT WIN32) - GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH) - ENDIF() - - # 4. Warn if CMake found something not located under ENV(XYZ_ROOT_DIR) - IF(DEFINED ENV{PYTHON_ROOT_DIR}) - SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${_PYTHON_ROOT_DIR_ENV}") - IF(NOT _res) - MESSAGE(WARNING "Python was found, but not a the path given by the " -"environment PYTHON_ROOT_DIR! Is the variable correctly set?") - ELSE() - MESSAGE(STATUS "Python found directory matches what was specified in the PYTHON_ROOT_DIR, all good!") - ENDIF() - ENDIF() - - # 5. Conflict detection - # 5.1 From another prerequisite using Python - IF(PYTHON_ROOT_DIR_EXP) - SALOME_CHECK_EQUAL_PATHS(_res "${_tmp_ROOT_DIR}" "${PYTHON_ROOT_DIR_EXP}") - IF(NOT _res) - MESSAGE(WARNING "Warning: Python: detected version conflicts with a previously found Python!" - "The two paths are " ${_tmp_ROOT_DIR} " vs " ${PYTHON_ROOT_DIR_EXP}) - ELSE() - MESSAGE(STATUS "Python directory matches what was previously exposed by another prereq, all good!") - ENDIF() - ENDIF() - - ## - ## 6. Save the final detected installation - ## - SET(PYTHON_ROOT_DIR "${_tmp_ROOT_DIR}") - SET(PYTHON_PYTHONPATH "${_tmp_ROOT_DIR}/lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}") - - ## 7. Specifics - ## - - # NumPy detection - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy ; import sys ; sys.stdout.write(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIR ERROR_QUIET ) - IF(NUMPY_INCLUDE_DIR) - SET(NUMPY_FOUND TRUE) - ENDIF(NUMPY_INCLUDE_DIR) - IF(NUMPY_FOUND) - SET(PYTHON_INCLUDE_DIRS ${NUMPY_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS}) - SET(PYTHON_DEFINITIONS "${PYTHON_DEFINITIONS} -DWITH_NUMPY") - MESSAGE(STATUS "NumPy found : ${NUMPY_INCLUDE_DIR}") - ELSE(NUMPY_FOUND) - MESSAGE(STATUS "NumPy not found.") - ENDIF(NUMPY_FOUND) - # SciPy detection - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import scipy ; import sys ; sys.stdout.write(scipy.version.version)" OUTPUT_VARIABLE SCIPY_VERSION ERROR_QUIET ) - IF(SCIPY_VERSION) - SET(SCIPY_FOUND TRUE) - ENDIF(SCIPY_VERSION) - IF(SCIPY_FOUND) - MESSAGE(STATUS "Scipy found : Version ${SCIPY_VERSION}") - ENDIF(SCIPY_FOUND) - ## None here -ELSE() - MESSAGE(STATUS "Python was only partially (or not at all) found .") -ENDIF() - -IF(SALOMEPYTHON_FOUND) - SALOME_ACCUMULATE_HEADERS(PYTHON_INCLUDE_DIR) - SALOME_ACCUMULATE_ENVIRONMENT(PATH ${PYTHON_EXECUTABLE}) - SALOME_ACCUMULATE_ENVIRONMENT(LD_LIBRARY_PATH ${PYTHON_LIBRARIES}) - SALOME_ACCUMULATE_ENVIRONMENT(PYTHONPATH ${PYTHON_PYTHONPATH}) -ENDIF() diff --git a/salome_adm/cmake_files/FindSalomePythonInterp.cmake b/salome_adm/cmake_files/FindSalomePythonInterp.cmake new file mode 100644 index 000000000..9028f26ce --- /dev/null +++ b/salome_adm/cmake_files/FindSalomePythonInterp.cmake @@ -0,0 +1,77 @@ +# Copyright (C) 2013-2015 CEA/DEN, EDF R&D, OPEN CASCADE +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Adrien Bruneton +# + +# Python interpreter detection for SALOME +# +# !! Please read the generic detection procedure in SalomeMacros.cmake !! +# +# We also look for an installation of NumPy, and if found the following variables are set +# NUMPY_INCLUDE_DIR - NumPy header location +# NUMPY_DEFINITIONS - compiler flag +# and are automatically appended to PYTHON_INCLUDE_DIRS (and PYTHON_DEFINITIONS resp.) +# + +# Make sure the detection of both libs and interpreter (if both needed) occur in the correct order: +IF(SALOMEPYTHONLIBS_FOUND AND NOT SALOMEPYTHONINTERP_FOUND) + MESSAGE(FATAL_ERROR "Developer error -> Python interpreter should be detected/required before Python libs!") +ENDIF() + +# Use the PYTHON_ROOT_DIR if PYTHONINTERP_ROOT_DIR is not defined: +SET(PYTHON_ROOT_DIR "$ENV{PYTHON_ROOT_DIR}" CACHE PATH "Path to the Python installation (libs+interpreter)") +IF(EXISTS "${PYTHON_ROOT_DIR}" AND (NOT PYTHONINTERP_ROOT_DIR)) + # Extract sub-directory "paraview-x.xx": + MESSAGE(STATUS "Setting PYTHONINTERP_ROOT_DIR to: ${PYTHON_ROOT_DIR}") + SET(PYTHONINTERP_ROOT_DIR "${PYTHON_ROOT_DIR}" CACHE PATH "Path to PythonInterp directory") +ENDIF() +SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS(PythonInterp PYTHON_EXECUTABLE 1) + +IF(SALOMEPYTHONINTERP_FOUND) + SET(PYTHON_PYTHONPATH "${PYTHON_ROOT_DIR}/lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}") + + ## + ## Specifics -- NumPy/SciPy detection + ## + + # Numpy + EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy ; import sys ; sys.stdout.write(numpy.get_include())" OUTPUT_VARIABLE NUMPY_INCLUDE_DIR ERROR_QUIET ) + IF(NUMPY_INCLUDE_DIR) + SET(NUMPY_FOUND TRUE) + ENDIF(NUMPY_INCLUDE_DIR) + IF(NUMPY_FOUND) + SET(PYTHON_INCLUDE_DIRS ${NUMPY_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS}) + SET(PYTHON_DEFINITIONS "${PYTHON_DEFINITIONS} -DWITH_NUMPY") + MESSAGE(STATUS "NumPy found : ${NUMPY_INCLUDE_DIR}") + ELSE(NUMPY_FOUND) + MESSAGE(STATUS "NumPy not found.") + ENDIF(NUMPY_FOUND) + # SciPy detection + EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import scipy ; import sys ; sys.stdout.write(scipy.version.version)" OUTPUT_VARIABLE SCIPY_VERSION ERROR_QUIET ) + IF(SCIPY_VERSION) + SET(SCIPY_FOUND TRUE) + ENDIF(SCIPY_VERSION) + IF(SCIPY_FOUND) + MESSAGE(STATUS "Scipy found : Version ${SCIPY_VERSION}") + ENDIF(SCIPY_FOUND) + + SALOME_ACCUMULATE_ENVIRONMENT(PATH ${PYTHON_EXECUTABLE}) + SALOME_ACCUMULATE_ENVIRONMENT(PYTHONPATH ${PYTHON_PYTHONPATH}) +ENDIF() + diff --git a/salome_adm/cmake_files/FindSalomePythonLibs.cmake b/salome_adm/cmake_files/FindSalomePythonLibs.cmake new file mode 100644 index 000000000..427786ed9 --- /dev/null +++ b/salome_adm/cmake_files/FindSalomePythonLibs.cmake @@ -0,0 +1,49 @@ +# Copyright (C) 2013-2015 CEA/DEN, EDF R&D, OPEN CASCADE +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +# Author: Adrien Bruneton +# + +# Python libraries detection for SALOME +# +# !! Please read the generic detection procedure in SalomeMacros.cmake !! +# + +# Use the PYTHON_ROOT_DIR if PYTHONLIBS_ROOT_DIR is not defined: +SET(PYTHON_ROOT_DIR "$ENV{PYTHON_ROOT_DIR}" CACHE PATH "Path to the Python installation (libs+interpreter)") +IF(EXISTS "${PYTHON_ROOT_DIR}" AND (NOT PYTHONLIBS_ROOT_DIR)) + # Extract sub-directory "paraview-x.xx": + MESSAGE(STATUS "Setting PYTHONLIBS_ROOT_DIR to: ${PYTHON_ROOT_DIR}") + SET(PYTHONLIBS_ROOT_DIR "${PYTHON_ROOT_DIR}" CACHE PATH "Path to PythonLibs directory") +ENDIF() +SALOME_FIND_PACKAGE_AND_DETECT_CONFLICTS(PythonLibs PYTHON_INCLUDE_DIR 2) + +IF(SALOMEPYTHONLIBS_FOUND) + SALOME_ACCUMULATE_HEADERS(PYTHON_INCLUDE_DIR) + SALOME_ACCUMULATE_ENVIRONMENT(LD_LIBRARY_PATH ${PYTHON_LIBRARIES}) +ENDIF() + +## Specifics -- check matching version with Interpreter if already detected: +IF (SALOMEPYTHONLIBS_FOUND AND SALOMEPYTHONINTERP_FOUND) + # Now ensure versions are matching + IF("${PYTHONLIBS_VERSION_STRING}" STREQUAL "${PYTHON_VERSION_STRING}") + MESSAGE(STATUS "Python libs and interpreter versions are matching: ${PYTHONLIBS_VERSION_STRING}") + ELSE() + MESSAGE(FATAL_ERROR "Python libs and interpreter versions are NOT matching: ${PYTHONLIBS_VERSION_STRING} vs ${PYTHON_VERSION_STRING}") + ENDIF() +ENDIF() -- 2.39.2