mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 21:25:13 +00:00
Include a more robust FindPythonLibs module for CMake
This commit is contained in:
parent
bd986fe57e
commit
928fff649f
@ -25,18 +25,9 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
|||||||
endif()
|
endif()
|
||||||
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
|
||||||
|
|
||||||
|
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
|
||||||
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
|
set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
|
||||||
if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
|
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
|
||||||
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
|
|
||||||
if (NOT PYTHONLIBS_FOUND)
|
|
||||||
find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
|
|
||||||
endif()
|
|
||||||
else()
|
|
||||||
find_package(PythonLibs REQUIRED)
|
|
||||||
endif()
|
|
||||||
# The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next line to work
|
|
||||||
string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}")
|
|
||||||
find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
|
|
||||||
|
|
||||||
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
|
||||||
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
|
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
|
||||||
@ -86,14 +77,13 @@ endif()
|
|||||||
|
|
||||||
|
|
||||||
# Check if Eigen is available
|
# Check if Eigen is available
|
||||||
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools")
|
|
||||||
find_package(Eigen3 QUIET)
|
find_package(Eigen3 QUIET)
|
||||||
|
|
||||||
# Include path for pybind11 header files
|
# Include path for pybind11 header files
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
# Include path for Python header files
|
# Include path for Python header files
|
||||||
include_directories(${PYTHON_INCLUDE_DIR})
|
include_directories(${PYTHON_INCLUDE_DIRS})
|
||||||
|
|
||||||
set(PYBIND11_HEADERS
|
set(PYBIND11_HEADERS
|
||||||
include/pybind11/attr.h
|
include/pybind11/attr.h
|
||||||
@ -184,7 +174,7 @@ if (WIN32)
|
|||||||
set_target_properties(example PROPERTIES SUFFIX ".pyd")
|
set_target_properties(example PROPERTIES SUFFIX ".pyd")
|
||||||
|
|
||||||
# Link against the Python shared library
|
# Link against the Python shared library
|
||||||
target_link_libraries(example ${PYTHON_LIBRARY})
|
target_link_libraries(example ${PYTHON_LIBRARIES})
|
||||||
elseif (UNIX)
|
elseif (UNIX)
|
||||||
# It's quite common to have multiple copies of the same Python version
|
# It's quite common to have multiple copies of the same Python version
|
||||||
# installed on one's system. E.g.: one copy from the OS and another copy
|
# installed on one's system. E.g.: one copy from the OS and another copy
|
||||||
|
187
tools/FindPythonLibsNew.cmake
Normal file
187
tools/FindPythonLibsNew.cmake
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
# - Find python libraries
|
||||||
|
# This module finds the libraries corresponding to the Python interpeter
|
||||||
|
# FindPythonInterp provides.
|
||||||
|
# This code sets the following variables:
|
||||||
|
#
|
||||||
|
# PYTHONLIBS_FOUND - have the Python libs been found
|
||||||
|
# PYTHON_PREFIX - path to the Python installation
|
||||||
|
# PYTHON_LIBRARIES - path to the python library
|
||||||
|
# PYTHON_INCLUDE_DIRS - path to where Python.h is found
|
||||||
|
# PYTHON_SITE_PACKAGES - path to installation site-packages
|
||||||
|
# PYTHON_IS_DEBUG - whether the Python interpreter is a debug build
|
||||||
|
#
|
||||||
|
# PYTHON_INCLUDE_PATH - path to where Python.h is found (deprecated)
|
||||||
|
#
|
||||||
|
# A function PYTHON_ADD_MODULE(<name> src1 src2 ... srcN) is defined
|
||||||
|
# to build modules for python.
|
||||||
|
#
|
||||||
|
# Thanks to talljimbo for the patch adding the 'LDVERSION' config
|
||||||
|
# variable usage.
|
||||||
|
|
||||||
|
#=============================================================================
|
||||||
|
# Copyright 2001-2009 Kitware, Inc.
|
||||||
|
# Copyright 2012 Continuum Analytics, Inc.
|
||||||
|
#
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions
|
||||||
|
# are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright
|
||||||
|
# notice, this list of conditions and the following disclaimer.
|
||||||
|
#
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright
|
||||||
|
# notice, this list of conditions and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
#
|
||||||
|
# * Neither the names of Kitware, Inc., the Insight Software Consortium,
|
||||||
|
# nor the names of their contributors may be used to endorse or promote
|
||||||
|
# products derived from this software without specific prior written
|
||||||
|
# permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
#=============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
# Use the Python interpreter to find the libs.
|
||||||
|
if(PythonLibsNew_FIND_REQUIRED)
|
||||||
|
find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
|
||||||
|
else()
|
||||||
|
find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT PYTHONINTERP_FOUND)
|
||||||
|
set(PYTHONLIBS_FOUND FALSE)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
|
||||||
|
# testing whether sys has the gettotalrefcount function is a reliable, cross-platform
|
||||||
|
# way to detect a CPython debug interpreter.
|
||||||
|
#
|
||||||
|
# The library suffix is from the config var LDVERSION sometimes, otherwise
|
||||||
|
# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
|
||||||
|
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
|
||||||
|
"from distutils import sysconfig as s;import sys;import struct;
|
||||||
|
print('.'.join(str(v) for v in sys.version_info));
|
||||||
|
print(sys.prefix);
|
||||||
|
print(s.get_python_inc(plat_specific=True));
|
||||||
|
print(s.get_python_lib(plat_specific=True));
|
||||||
|
print(s.get_config_var('SO'));
|
||||||
|
print(hasattr(sys, 'gettotalrefcount')+0);
|
||||||
|
print(struct.calcsize('@P'));
|
||||||
|
print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
|
||||||
|
"
|
||||||
|
RESULT_VARIABLE _PYTHON_SUCCESS
|
||||||
|
OUTPUT_VARIABLE _PYTHON_VALUES
|
||||||
|
ERROR_VARIABLE _PYTHON_ERROR_VALUE
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
|
|
||||||
|
if(NOT _PYTHON_SUCCESS MATCHES 0)
|
||||||
|
if(PythonLibsNew_FIND_REQUIRED)
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"Python config failure:\n${_PYTHON_ERROR_VALUE}")
|
||||||
|
endif()
|
||||||
|
set(PYTHONLIBS_FOUND FALSE)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Convert the process output into a list
|
||||||
|
string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
|
||||||
|
string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
|
||||||
|
list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
|
||||||
|
list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
|
||||||
|
list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
|
||||||
|
list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
|
||||||
|
list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
|
||||||
|
list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
|
||||||
|
list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
|
||||||
|
list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
|
||||||
|
|
||||||
|
# Make sure the Python has the same pointer-size as the chosen compiler
|
||||||
|
# Skip if CMAKE_SIZEOF_VOID_P is not defined
|
||||||
|
if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
|
||||||
|
if(PythonLibsNew_FIND_REQUIRED)
|
||||||
|
math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
|
||||||
|
math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"Python config failure: Python is ${_PYTHON_BITS}-bit, "
|
||||||
|
"chosen compiler is ${_CMAKE_BITS}-bit")
|
||||||
|
endif()
|
||||||
|
set(PYTHONLIBS_FOUND FALSE)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# The built-in FindPython didn't always give the version numbers
|
||||||
|
string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
|
||||||
|
list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
|
||||||
|
list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
|
||||||
|
list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
|
||||||
|
|
||||||
|
# Make sure all directory separators are '/'
|
||||||
|
string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX})
|
||||||
|
string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
|
||||||
|
string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
|
||||||
|
|
||||||
|
# TODO: All the nuances of CPython debug builds have not been dealt with/tested.
|
||||||
|
if(PYTHON_IS_DEBUG)
|
||||||
|
set(PYTHON_MODULE_EXTENSION "_d${PYTHON_MODULE_EXTENSION}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(CMAKE_HOST_WIN32)
|
||||||
|
set(PYTHON_LIBRARY
|
||||||
|
"${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
|
||||||
|
elseif(APPLE)
|
||||||
|
set(PYTHON_LIBRARY
|
||||||
|
"${PYTHON_PREFIX}/lib/libpython${PYTHON_LIBRARY_SUFFIX}.dylib")
|
||||||
|
else()
|
||||||
|
if(${PYTHON_SIZEOF_VOID_P} MATCHES 8)
|
||||||
|
set(_PYTHON_LIBS_SEARCH "${PYTHON_PREFIX}/lib64" "${PYTHON_PREFIX}/lib")
|
||||||
|
else()
|
||||||
|
set(_PYTHON_LIBS_SEARCH "${PYTHON_PREFIX}/lib")
|
||||||
|
endif()
|
||||||
|
#message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
|
||||||
|
# Probably this needs to be more involved. It would be nice if the config
|
||||||
|
# information the python interpreter itself gave us were more complete.
|
||||||
|
find_library(PYTHON_LIBRARY
|
||||||
|
NAMES "python${PYTHON_LIBRARY_SUFFIX}"
|
||||||
|
PATHS ${_PYTHON_LIBS_SEARCH}
|
||||||
|
NO_DEFAULT_PATH)
|
||||||
|
|
||||||
|
# If all else fails, just set the name/version and let the linker figure out the path.
|
||||||
|
if(NOT PYTHON_LIBRARY)
|
||||||
|
set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# For backward compatibility, set PYTHON_INCLUDE_PATH, but make it internal.
|
||||||
|
SET(PYTHON_INCLUDE_PATH "${PYTHON_INCLUDE_DIR}" CACHE INTERNAL
|
||||||
|
"Path to where Python.h is found (deprecated)")
|
||||||
|
|
||||||
|
MARK_AS_ADVANCED(
|
||||||
|
PYTHON_LIBRARY
|
||||||
|
PYTHON_INCLUDE_DIR
|
||||||
|
)
|
||||||
|
|
||||||
|
# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
|
||||||
|
# cache entries because they are meant to specify the location of a single
|
||||||
|
# library. We now set the variables listed by the documentation for this
|
||||||
|
# module.
|
||||||
|
SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
|
||||||
|
SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
|
||||||
|
SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
|
||||||
|
|
||||||
|
find_package_message(PYTHON
|
||||||
|
"Found PythonLibs: ${PYTHON_LIBRARY}"
|
||||||
|
"${PYTHON_EXECUTABLE}${PYTHON_VERSION}")
|
Loading…
Reference in New Issue
Block a user