FindPythonLibsNew.cmake revision 14299
11689SN/A# - Find python libraries 22316SN/A# This module finds the libraries corresponding to the Python interpreter 31689SN/A# FindPythonInterp provides. 41689SN/A# This code sets the following variables: 51689SN/A# 61689SN/A# PYTHONLIBS_FOUND - have the Python libs been found 71689SN/A# PYTHON_PREFIX - path to the Python installation 81689SN/A# PYTHON_LIBRARIES - path to the python library 91689SN/A# PYTHON_INCLUDE_DIRS - path to where Python.h is found 101689SN/A# PYTHON_MODULE_EXTENSION - lib extension, e.g. '.so' or '.pyd' 111689SN/A# PYTHON_MODULE_PREFIX - lib name prefix: usually an empty string 121689SN/A# PYTHON_SITE_PACKAGES - path to installation site-packages 131689SN/A# PYTHON_IS_DEBUG - whether the Python interpreter is a debug build 141689SN/A# 151689SN/A# Thanks to talljimbo for the patch adding the 'LDVERSION' config 161689SN/A# variable usage. 171689SN/A 181689SN/A#============================================================================= 191689SN/A# Copyright 2001-2009 Kitware, Inc. 201689SN/A# Copyright 2012 Continuum Analytics, Inc. 211689SN/A# 221689SN/A# All rights reserved. 231689SN/A# 241689SN/A# Redistribution and use in source and binary forms, with or without 251689SN/A# modification, are permitted provided that the following conditions 261689SN/A# are met: 272665Ssaidi@eecs.umich.edu# 282665Ssaidi@eecs.umich.edu# * Redistributions of source code must retain the above copyright 291689SN/A# notice, this list of conditions and the following disclaimer. 301689SN/A# 312733Sktlim@umich.edu# * Redistributions in binary form must reproduce the above copyright 322733Sktlim@umich.edu# notice, this list of conditions and the following disclaimer in the 332733Sktlim@umich.edu# documentation and/or other materials provided with the distribution. 342292SN/A# 352329SN/A# * Neither the names of Kitware, Inc., the Insight Software Consortium, 362292SN/A# nor the names of their contributors may be used to endorse or promote 372292SN/A# products derived from this software without specific prior written 381060SN/A# permission. 392292SN/A# 401717SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 412292SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 422292SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 432790Sktlim@umich.edu# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 442790Sktlim@umich.edu# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 452790Sktlim@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 462790Sktlim@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 472292SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 481060SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 491061SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 502292SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 512292SN/A#============================================================================= 522292SN/A 531060SN/A# Checking for the extension makes sure that `LibsNew` was found and not just `Libs`. 542292SN/Aif(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION) 551060SN/A return() 561060SN/Aendif() 571061SN/A 581060SN/A# Use the Python interpreter to find the libs. 592292SN/Aif(PythonLibsNew_FIND_REQUIRED) 601062SN/A find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED) 612316SN/Aelse() 622316SN/A find_package(PythonInterp ${PythonLibsNew_FIND_VERSION}) 632292SN/Aendif() 642292SN/A 652292SN/Aif(NOT PYTHONINTERP_FOUND) 662292SN/A set(PYTHONLIBS_FOUND FALSE) 672292SN/A set(PythonLibsNew_FOUND FALSE) 682292SN/A return() 692292SN/Aendif() 702292SN/A 712292SN/A# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter 722292SN/A# testing whether sys has the gettotalrefcount function is a reliable, cross-platform 732292SN/A# way to detect a CPython debug interpreter. 742292SN/A# 752669Sktlim@umich.edu# The library suffix is from the config var LDVERSION sometimes, otherwise 762292SN/A# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows. 772292SN/Aexecute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" 782292SN/A "from distutils import sysconfig as s;import sys;import struct; 792292SN/Aprint('.'.join(str(v) for v in sys.version_info)); 802292SN/Aprint(sys.prefix); 812292SN/Aprint(s.get_python_inc(plat_specific=True)); 822307SN/Aprint(s.get_python_lib(plat_specific=True)); 832843Sktlim@umich.eduprint(s.get_config_var('SO')); 842316SN/Aprint(hasattr(sys, 'gettotalrefcount')+0); 852316SN/Aprint(struct.calcsize('@P')); 862316SN/Aprint(s.get_config_var('LDVERSION') or s.get_config_var('VERSION')); 872292SN/Aprint(s.get_config_var('LIBDIR') or ''); 882292SN/Aprint(s.get_config_var('MULTIARCH') or ''); 892292SN/A" 902292SN/A RESULT_VARIABLE _PYTHON_SUCCESS 912292SN/A OUTPUT_VARIABLE _PYTHON_VALUES 922292SN/A ERROR_VARIABLE _PYTHON_ERROR_VALUE) 932292SN/A 942292SN/Aif(NOT _PYTHON_SUCCESS MATCHES 0) 952292SN/A if(PythonLibsNew_FIND_REQUIRED) 962292SN/A message(FATAL_ERROR 972292SN/A "Python config failure:\n${_PYTHON_ERROR_VALUE}") 982292SN/A endif() 992292SN/A set(PYTHONLIBS_FOUND FALSE) 1002292SN/A set(PythonLibsNew_FOUND FALSE) 1012292SN/A return() 1022292SN/Aendif() 1032292SN/A 1042292SN/A# Convert the process output into a list 1052292SN/Aif(WIN32) 1062292SN/A string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES}) 1072292SN/Aendif() 1082292SN/Astring(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES}) 1092292SN/Astring(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES}) 1102292SN/Alist(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST) 1112292SN/Alist(GET _PYTHON_VALUES 1 PYTHON_PREFIX) 1122292SN/Alist(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR) 1132292SN/Alist(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES) 1142292SN/Alist(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION) 1152292SN/Alist(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG) 1162292SN/Alist(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P) 1172292SN/Alist(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX) 1182292SN/Alist(GET _PYTHON_VALUES 8 PYTHON_LIBDIR) 1192292SN/Alist(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH) 1202292SN/A 1212292SN/A# Make sure the Python has the same pointer-size as the chosen compiler 1222292SN/A# Skip if CMAKE_SIZEOF_VOID_P is not defined 1232680Sktlim@umich.eduif(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}")) 1242678Sktlim@umich.edu if(PythonLibsNew_FIND_REQUIRED) 1252292SN/A math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8") 1262292SN/A math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8") 1272292SN/A message(FATAL_ERROR 1282292SN/A "Python config failure: Python is ${_PYTHON_BITS}-bit, " 1292292SN/A "chosen compiler is ${_CMAKE_BITS}-bit") 1302292SN/A endif() 1312292SN/A set(PYTHONLIBS_FOUND FALSE) 1322292SN/A set(PythonLibsNew_FOUND FALSE) 1332292SN/A return() 1342292SN/Aendif() 1352292SN/A 1362292SN/A# The built-in FindPython didn't always give the version numbers 1372292SN/Astring(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST}) 1382292SN/Alist(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR) 1392292SN/Alist(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR) 1402292SN/Alist(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH) 1412132SN/A 1422301SN/A# Make sure all directory separators are '/' 1431062SN/Astring(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX}) 1441062SN/Astring(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR}) 1451062SN/Astring(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES}) 1461062SN/A 1471062SN/Aif(CMAKE_HOST_WIN32 AND NOT (MSYS OR MINGW)) 1481062SN/A set(PYTHON_LIBRARY 1491062SN/A "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib") 1501062SN/A 1511062SN/A # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the 1521062SN/A # original python installation. They may be found relative to PYTHON_INCLUDE_DIR. 1531062SN/A if(NOT EXISTS "${PYTHON_LIBRARY}") 1541062SN/A get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY) 1551062SN/A set(PYTHON_LIBRARY 1561062SN/A "${_PYTHON_ROOT}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib") 1571062SN/A endif() 1581062SN/A 1591062SN/A # raise an error if the python libs are still not found. 1601062SN/A if(NOT EXISTS "${PYTHON_LIBRARY}") 1611062SN/A message(FATAL_ERROR "Python libraries not found") 1621062SN/A endif() 1631062SN/A 1642292SN/Aelse() 1651062SN/A if(PYTHON_MULTIARCH) 1661062SN/A set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") 1671062SN/A else() 1681062SN/A set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") 1691062SN/A endif() 1702301SN/A #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}") 1712316SN/A # Probably this needs to be more involved. It would be nice if the config 1722301SN/A # information the python interpreter itself gave us were more complete. 1732301SN/A find_library(PYTHON_LIBRARY 1742301SN/A NAMES "python${PYTHON_LIBRARY_SUFFIX}" 1752301SN/A PATHS ${_PYTHON_LIBS_SEARCH} 1762301SN/A NO_DEFAULT_PATH) 1772301SN/A 1782316SN/A # If all else fails, just set the name/version and let the linker figure out the path. 1792301SN/A if(NOT PYTHON_LIBRARY) 1802301SN/A set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX}) 1812301SN/A endif() 1822301SN/Aendif() 1832301SN/A 1842301SN/AMARK_AS_ADVANCED( 1852316SN/A PYTHON_LIBRARY 1862301SN/A PYTHON_INCLUDE_DIR 1872301SN/A) 1882301SN/A 1892301SN/A# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the 1902301SN/A# cache entries because they are meant to specify the location of a single 1912301SN/A# library. We now set the variables listed by the documentation for this 1922316SN/A# module. 1932301SN/ASET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}") 1942301SN/ASET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}") 1952301SN/ASET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}") 1962301SN/A 1972301SN/Afind_package_message(PYTHON 1982301SN/A "Found PythonLibs: ${PYTHON_LIBRARY}" 1992316SN/A "${PYTHON_EXECUTABLE}${PYTHON_VERSION}") 2002301SN/A 2012301SN/Aset(PYTHONLIBS_FOUND TRUE) 2022301SN/Aset(PythonLibsNew_FOUND TRUE) 2032301SN/A