FindPythonLibsNew.cmake revision 14299
11736SN/A# - Find python libraries 21736SN/A# This module finds the libraries corresponding to the Python interpreter 31736SN/A# FindPythonInterp provides. 41736SN/A# This code sets the following variables: 51736SN/A# 61736SN/A# PYTHONLIBS_FOUND - have the Python libs been found 71736SN/A# PYTHON_PREFIX - path to the Python installation 81736SN/A# PYTHON_LIBRARIES - path to the python library 91736SN/A# PYTHON_INCLUDE_DIRS - path to where Python.h is found 101736SN/A# PYTHON_MODULE_EXTENSION - lib extension, e.g. '.so' or '.pyd' 111736SN/A# PYTHON_MODULE_PREFIX - lib name prefix: usually an empty string 121736SN/A# PYTHON_SITE_PACKAGES - path to installation site-packages 131736SN/A# PYTHON_IS_DEBUG - whether the Python interpreter is a debug build 141736SN/A# 151736SN/A# Thanks to talljimbo for the patch adding the 'LDVERSION' config 161736SN/A# variable usage. 171736SN/A 181736SN/A#============================================================================= 191736SN/A# Copyright 2001-2009 Kitware, Inc. 201736SN/A# Copyright 2012 Continuum Analytics, Inc. 211736SN/A# 221736SN/A# All rights reserved. 231736SN/A# 241736SN/A# Redistribution and use in source and binary forms, with or without 251736SN/A# modification, are permitted provided that the following conditions 262665Ssaidi@eecs.umich.edu# are met: 272665Ssaidi@eecs.umich.edu# 281736SN/A# * Redistributions of source code must retain the above copyright 2913714Sandreas.sandberg@arm.com# notice, this list of conditions and the following disclaimer. 3013714Sandreas.sandberg@arm.com# 3113714Sandreas.sandberg@arm.com# * Redistributions in binary form must reproduce the above copyright 326654Snate@binkert.org# notice, this list of conditions and the following disclaimer in the 336654Snate@binkert.org# documentation and/or other materials provided with the distribution. 346654Snate@binkert.org# 352655Sstever@eecs.umich.edu# * Neither the names of Kitware, Inc., the Insight Software Consortium, 364762Snate@binkert.org# nor the names of their contributors may be used to endorse or promote 3711991Sandreas.sandberg@arm.com# products derived from this software without specific prior written 3811802Sandreas.sandberg@arm.com# permission. 398222Snate@binkert.org# 408222Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 4111802Sandreas.sandberg@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 4211802Sandreas.sandberg@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 434762Snate@binkert.org# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 4411802Sandreas.sandberg@arm.com# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4511802Sandreas.sandberg@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 464762Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 4711802Sandreas.sandberg@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 4813714Sandreas.sandberg@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 4913714Sandreas.sandberg@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 5013724Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 5113714Sandreas.sandberg@arm.com#============================================================================= 5213714Sandreas.sandberg@arm.com 5313714Sandreas.sandberg@arm.com# Checking for the extension makes sure that `LibsNew` was found and not just `Libs`. 5413724Sgabeblack@google.comif(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION) 5513724Sgabeblack@google.com return() 5613724Sgabeblack@google.comendif() 5713714Sandreas.sandberg@arm.com 585798Snate@binkert.org# Use the Python interpreter to find the libs. 5913714Sandreas.sandberg@arm.comif(PythonLibsNew_FIND_REQUIRED) 6013714Sandreas.sandberg@arm.com find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED) 6113714Sandreas.sandberg@arm.comelse() 629342SAndreas.Sandberg@arm.com find_package(PythonInterp ${PythonLibsNew_FIND_VERSION}) 63endif() 64 65if(NOT PYTHONINTERP_FOUND) 66 set(PYTHONLIBS_FOUND FALSE) 67 set(PythonLibsNew_FOUND FALSE) 68 return() 69endif() 70 71# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter 72# testing whether sys has the gettotalrefcount function is a reliable, cross-platform 73# way to detect a CPython debug interpreter. 74# 75# The library suffix is from the config var LDVERSION sometimes, otherwise 76# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows. 77execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" 78 "from distutils import sysconfig as s;import sys;import struct; 79print('.'.join(str(v) for v in sys.version_info)); 80print(sys.prefix); 81print(s.get_python_inc(plat_specific=True)); 82print(s.get_python_lib(plat_specific=True)); 83print(s.get_config_var('SO')); 84print(hasattr(sys, 'gettotalrefcount')+0); 85print(struct.calcsize('@P')); 86print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION')); 87print(s.get_config_var('LIBDIR') or ''); 88print(s.get_config_var('MULTIARCH') or ''); 89" 90 RESULT_VARIABLE _PYTHON_SUCCESS 91 OUTPUT_VARIABLE _PYTHON_VALUES 92 ERROR_VARIABLE _PYTHON_ERROR_VALUE) 93 94if(NOT _PYTHON_SUCCESS MATCHES 0) 95 if(PythonLibsNew_FIND_REQUIRED) 96 message(FATAL_ERROR 97 "Python config failure:\n${_PYTHON_ERROR_VALUE}") 98 endif() 99 set(PYTHONLIBS_FOUND FALSE) 100 set(PythonLibsNew_FOUND FALSE) 101 return() 102endif() 103 104# Convert the process output into a list 105if(WIN32) 106 string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES}) 107endif() 108string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES}) 109string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES}) 110list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST) 111list(GET _PYTHON_VALUES 1 PYTHON_PREFIX) 112list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR) 113list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES) 114list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION) 115list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG) 116list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P) 117list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX) 118list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR) 119list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH) 120 121# Make sure the Python has the same pointer-size as the chosen compiler 122# Skip if CMAKE_SIZEOF_VOID_P is not defined 123if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}")) 124 if(PythonLibsNew_FIND_REQUIRED) 125 math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8") 126 math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8") 127 message(FATAL_ERROR 128 "Python config failure: Python is ${_PYTHON_BITS}-bit, " 129 "chosen compiler is ${_CMAKE_BITS}-bit") 130 endif() 131 set(PYTHONLIBS_FOUND FALSE) 132 set(PythonLibsNew_FOUND FALSE) 133 return() 134endif() 135 136# The built-in FindPython didn't always give the version numbers 137string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST}) 138list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR) 139list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR) 140list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH) 141 142# Make sure all directory separators are '/' 143string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX}) 144string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR}) 145string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES}) 146 147if(CMAKE_HOST_WIN32 AND NOT (MSYS OR MINGW)) 148 set(PYTHON_LIBRARY 149 "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib") 150 151 # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the 152 # original python installation. They may be found relative to PYTHON_INCLUDE_DIR. 153 if(NOT EXISTS "${PYTHON_LIBRARY}") 154 get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY) 155 set(PYTHON_LIBRARY 156 "${_PYTHON_ROOT}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib") 157 endif() 158 159 # raise an error if the python libs are still not found. 160 if(NOT EXISTS "${PYTHON_LIBRARY}") 161 message(FATAL_ERROR "Python libraries not found") 162 endif() 163 164else() 165 if(PYTHON_MULTIARCH) 166 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}") 167 else() 168 set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}") 169 endif() 170 #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}") 171 # Probably this needs to be more involved. It would be nice if the config 172 # information the python interpreter itself gave us were more complete. 173 find_library(PYTHON_LIBRARY 174 NAMES "python${PYTHON_LIBRARY_SUFFIX}" 175 PATHS ${_PYTHON_LIBS_SEARCH} 176 NO_DEFAULT_PATH) 177 178 # If all else fails, just set the name/version and let the linker figure out the path. 179 if(NOT PYTHON_LIBRARY) 180 set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX}) 181 endif() 182endif() 183 184MARK_AS_ADVANCED( 185 PYTHON_LIBRARY 186 PYTHON_INCLUDE_DIR 187) 188 189# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the 190# cache entries because they are meant to specify the location of a single 191# library. We now set the variables listed by the documentation for this 192# module. 193SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}") 194SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}") 195SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}") 196 197find_package_message(PYTHON 198 "Found PythonLibs: ${PYTHON_LIBRARY}" 199 "${PYTHON_EXECUTABLE}${PYTHON_VERSION}") 200 201set(PYTHONLIBS_FOUND TRUE) 202set(PythonLibsNew_FOUND TRUE) 203