d1ea64b
# - Find python libraries
d1ea64b
# This module finds the libraries corresponding to the Python interpreter
d1ea64b
# FindPythonInterp provides.
d1ea64b
# This code sets the following variables:
d1ea64b
#
d1ea64b
#  PYTHONLIBS_FOUND           - have the Python libs been found
d1ea64b
#  PYTHON_PREFIX              - path to the Python installation
d1ea64b
#  PYTHON_LIBRARIES           - path to the python library
d1ea64b
#  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
d1ea64b
#  PYTHON_MODULE_EXTENSION    - lib extension, e.g. '.so' or '.pyd'
d1ea64b
#  PYTHON_MODULE_PREFIX       - lib name prefix: usually an empty string
d1ea64b
#  PYTHON_SITE_PACKAGES       - path to installation site-packages
d1ea64b
#  PYTHON_IS_DEBUG            - whether the Python interpreter is a debug build
d1ea64b
#
d1ea64b
# Thanks to talljimbo for the patch adding the 'LDVERSION' config
d1ea64b
# variable usage.
d1ea64b
d1ea64b
#=============================================================================
d1ea64b
# Copyright 2001-2009 Kitware, Inc.
d1ea64b
# Copyright 2012 Continuum Analytics, Inc.
d1ea64b
#
d1ea64b
# All rights reserved.
d1ea64b
#
d1ea64b
# Redistribution and use in source and binary forms, with or without
d1ea64b
# modification, are permitted provided that the following conditions
d1ea64b
# are met:
d1ea64b
#
d1ea64b
# * Redistributions of source code must retain the above copyright
d1ea64b
# notice, this list of conditions and the following disclaimer.
d1ea64b
#
d1ea64b
# * Redistributions in binary form must reproduce the above copyright
d1ea64b
# notice, this list of conditions and the following disclaimer in the
d1ea64b
# documentation and/or other materials provided with the distribution.
d1ea64b
#
d1ea64b
# * Neither the names of Kitware, Inc., the Insight Software Consortium,
d1ea64b
# nor the names of their contributors may be used to endorse or promote
d1ea64b
# products derived from this software without specific prior written
d1ea64b
# permission.
d1ea64b
#
d1ea64b
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
d1ea64b
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
d1ea64b
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
d1ea64b
# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
d1ea64b
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d1ea64b
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
d1ea64b
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
d1ea64b
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
d1ea64b
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d1ea64b
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
d1ea64b
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d1ea64b
#=============================================================================
d1ea64b
d1ea64b
# Checking for the extension makes sure that `LibsNew` was found and not just `Libs`.
d1ea64b
if(PYTHONLIBS_FOUND AND PYTHON_MODULE_EXTENSION)
d1ea64b
    return()
d1ea64b
endif()
d1ea64b
d1ea64b
# Use the Python interpreter to find the libs.
d1ea64b
if(PythonLibsNew_FIND_REQUIRED)
d1ea64b
    find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)
d1ea64b
else()
d1ea64b
    find_package(PythonInterp ${PythonLibsNew_FIND_VERSION})
d1ea64b
endif()
d1ea64b
d1ea64b
if(NOT PYTHONINTERP_FOUND)
d1ea64b
    set(PYTHONLIBS_FOUND FALSE)
d1ea64b
    set(PythonLibsNew_FOUND FALSE)
d1ea64b
    return()
d1ea64b
endif()
d1ea64b
d1ea64b
# According to http://stackoverflow.com/questions/646518/python-how-to-detect-debug-interpreter
d1ea64b
# testing whether sys has the gettotalrefcount function is a reliable, cross-platform
d1ea64b
# way to detect a CPython debug interpreter.
d1ea64b
#
d1ea64b
# The library suffix is from the config var LDVERSION sometimes, otherwise
d1ea64b
# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
d1ea64b
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
d1ea64b
    "from distutils import sysconfig as s;import sys;import struct;
d1ea64b
print('.'.join(str(v) for v in sys.version_info));
d1ea64b
print(sys.prefix);
d1ea64b
print(s.get_python_inc(plat_specific=True));
d1ea64b
print(s.get_python_lib(plat_specific=True));
d1ea64b
print(s.get_config_var('SO'));
d1ea64b
print(hasattr(sys, 'gettotalrefcount')+0);
d1ea64b
print(struct.calcsize('@P'));
d1ea64b
print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
d1ea64b
print(s.get_config_var('LIBDIR') or '');
d1ea64b
print(s.get_config_var('MULTIARCH') or '');
d1ea64b
"
d1ea64b
    RESULT_VARIABLE _PYTHON_SUCCESS
d1ea64b
    OUTPUT_VARIABLE _PYTHON_VALUES
d1ea64b
    ERROR_VARIABLE _PYTHON_ERROR_VALUE)
d1ea64b
d1ea64b
if(NOT _PYTHON_SUCCESS MATCHES 0)
d1ea64b
    if(PythonLibsNew_FIND_REQUIRED)
d1ea64b
        message(FATAL_ERROR
d1ea64b
            "Python config failure:\n${_PYTHON_ERROR_VALUE}")
d1ea64b
    endif()
d1ea64b
    set(PYTHONLIBS_FOUND FALSE)
d1ea64b
    set(PythonLibsNew_FOUND FALSE)
d1ea64b
    return()
d1ea64b
endif()
d1ea64b
d1ea64b
# Convert the process output into a list
d1ea64b
if(WIN32)
d1ea64b
    string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
d1ea64b
endif()
d1ea64b
string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
d1ea64b
string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
d1ea64b
list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
d1ea64b
list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
d1ea64b
list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
d1ea64b
list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
d1ea64b
list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
d1ea64b
list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
d1ea64b
list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
d1ea64b
list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
d1ea64b
list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
d1ea64b
list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
d1ea64b
d1ea64b
# Make sure the Python has the same pointer-size as the chosen compiler
d1ea64b
# Skip if CMAKE_SIZEOF_VOID_P is not defined
d1ea64b
if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
d1ea64b
    if(PythonLibsNew_FIND_REQUIRED)
d1ea64b
        math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
d1ea64b
        math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
d1ea64b
        message(FATAL_ERROR
d1ea64b
            "Python config failure: Python is ${_PYTHON_BITS}-bit, "
d1ea64b
            "chosen compiler is  ${_CMAKE_BITS}-bit")
d1ea64b
    endif()
d1ea64b
    set(PYTHONLIBS_FOUND FALSE)
d1ea64b
    set(PythonLibsNew_FOUND FALSE)
d1ea64b
    return()
d1ea64b
endif()
d1ea64b
d1ea64b
# The built-in FindPython didn't always give the version numbers
d1ea64b
string(REGEX REPLACE "\\." ";" _PYTHON_VERSION_LIST ${_PYTHON_VERSION_LIST})
d1ea64b
list(GET _PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
d1ea64b
list(GET _PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
d1ea64b
list(GET _PYTHON_VERSION_LIST 2 PYTHON_VERSION_PATCH)
d1ea64b
d1ea64b
# Make sure all directory separators are '/'
d1ea64b
string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX ${PYTHON_PREFIX})
d1ea64b
string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR ${PYTHON_INCLUDE_DIR})
d1ea64b
string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES ${PYTHON_SITE_PACKAGES})
d1ea64b
d1ea64b
if(CMAKE_HOST_WIN32 AND NOT (MSYS OR MINGW))
d1ea64b
    set(PYTHON_LIBRARY
d1ea64b
        "${PYTHON_PREFIX}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
d1ea64b
d1ea64b
    # when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
d1ea64b
    # original python installation. They may be found relative to PYTHON_INCLUDE_DIR.
d1ea64b
    if(NOT EXISTS "${PYTHON_LIBRARY}")
d1ea64b
        get_filename_component(_PYTHON_ROOT ${PYTHON_INCLUDE_DIR} DIRECTORY)
d1ea64b
        set(PYTHON_LIBRARY
d1ea64b
            "${_PYTHON_ROOT}/libs/Python${PYTHON_LIBRARY_SUFFIX}.lib")
d1ea64b
    endif()
d1ea64b
d1ea64b
    # raise an error if the python libs are still not found.
d1ea64b
    if(NOT EXISTS "${PYTHON_LIBRARY}")
d1ea64b
        message(FATAL_ERROR "Python libraries not found")
d1ea64b
    endif()
d1ea64b
d1ea64b
else()
d1ea64b
    if(PYTHON_MULTIARCH)
d1ea64b
        set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}/${PYTHON_MULTIARCH}" "${PYTHON_LIBDIR}")
d1ea64b
    else()
d1ea64b
        set(_PYTHON_LIBS_SEARCH "${PYTHON_LIBDIR}")
d1ea64b
    endif()
d1ea64b
    #message(STATUS "Searching for Python libs in ${_PYTHON_LIBS_SEARCH}")
d1ea64b
    # Probably this needs to be more involved. It would be nice if the config
d1ea64b
    # information the python interpreter itself gave us were more complete.
d1ea64b
    find_library(PYTHON_LIBRARY
d1ea64b
        NAMES "python${PYTHON_LIBRARY_SUFFIX}"
d1ea64b
        PATHS ${_PYTHON_LIBS_SEARCH}
d1ea64b
        NO_DEFAULT_PATH)
d1ea64b
d1ea64b
    # If all else fails, just set the name/version and let the linker figure out the path.
d1ea64b
    if(NOT PYTHON_LIBRARY)
d1ea64b
        set(PYTHON_LIBRARY python${PYTHON_LIBRARY_SUFFIX})
d1ea64b
    endif()
d1ea64b
endif()
d1ea64b
d1ea64b
MARK_AS_ADVANCED(
d1ea64b
  PYTHON_LIBRARY
d1ea64b
  PYTHON_INCLUDE_DIR
d1ea64b
)
d1ea64b
d1ea64b
# We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
d1ea64b
# cache entries because they are meant to specify the location of a single
d1ea64b
# library. We now set the variables listed by the documentation for this
d1ea64b
# module.
d1ea64b
SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
d1ea64b
SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
d1ea64b
SET(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
d1ea64b
d1ea64b
find_package_message(PYTHON
d1ea64b
    "Found PythonLibs: ${PYTHON_LIBRARY}"
d1ea64b
    "${PYTHON_EXECUTABLE}${PYTHON_VERSION}")
d1ea64b
d1ea64b
set(PYTHONLIBS_FOUND TRUE)
d1ea64b
set(PythonLibsNew_FOUND TRUE)