From 7f96ff39102869db132a7a4b047b5d2f56e224f8 Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Tue, 13 Sep 2016 00:09:10 -0700 Subject: [PATCH] Rewrite Python library finding Should fix building with Python 3 on Mac OS X. --- CMakeLists.txt | 8 +-- cmake/FindLibPython.py | 40 ----------- cmake/FindPythonLibrary.cmake | 117 ------------------------------- cmake/FindPythonLibsCustom.cmake | 46 ++++++++++++ cmake/pyinfo.py | 21 ++++++ 5 files changed, 71 insertions(+), 161 deletions(-) delete mode 100644 cmake/FindLibPython.py delete mode 100644 cmake/FindPythonLibrary.cmake create mode 100644 cmake/FindPythonLibsCustom.cmake create mode 100755 cmake/pyinfo.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 31c577e..a38e018 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8.3) +cmake_minimum_required(VERSION 2.8.8) project(cpsm) option(PY3 "Build for python3 instead of python2." OFF) @@ -28,12 +28,12 @@ endif() if(PY3) set(Python_ADDITIONAL_VERSIONS 3.5 3.4 3.3) find_package(PythonInterp 3 REQUIRED) - find_package(PythonLibs 3 REQUIRED) else() set(Python_ADDITIONAL_VERSIONS 2.7 2.6) - find_package(PythonLibrary REQUIRED) + find_package(PythonInterp REQUIRED) endif() -include_directories(${PYTHON_INCLUDE_PATH}) +find_package(PythonLibsCustom REQUIRED) +include_directories(${PYTHON_INCLUDE_DIRS}) find_package(Threads REQUIRED) diff --git a/cmake/FindLibPython.py b/cmake/FindLibPython.py deleted file mode 100644 index 1a9b146..0000000 --- a/cmake/FindLibPython.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (c) 2007, Simon Edwards -# 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 name of the Simon Edwards nor the -# names of its contributors may be used to endorse or promote products -# derived from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY Simon Edwards ''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 Simon Edwards 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. -# -# FindLibPython.py -# Copyright (c) 2007, Simon Edwards -# Redistribution and use is allowed according to the terms of the BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. - -import sys -import distutils.sysconfig - -print("exec_prefix:%s" % sys.exec_prefix) -print("short_version:%s" % sys.version[:3]) -print("long_version:%s" % sys.version.split()[0]) -print("py_inc_dir:%s" % distutils.sysconfig.get_python_inc()) -print("site_packages_dir:%s" % distutils.sysconfig.get_python_lib(plat_specific=1)) diff --git a/cmake/FindPythonLibrary.cmake b/cmake/FindPythonLibrary.cmake deleted file mode 100644 index 0c2a0d6..0000000 --- a/cmake/FindPythonLibrary.cmake +++ /dev/null @@ -1,117 +0,0 @@ -# Find Python -# ~~~~~~~~~~~ -# Find the Python interpreter and related Python directories. -# -# This file defines the following variables: -# -# PYTHON_EXECUTABLE - The path and filename of the Python interpreter. -# -# PYTHON_SHORT_VERSION - The version of the Python interpreter found, -# excluding the patch version number. (e.g. 2.5 and not 2.5.1)) -# -# PYTHON_LONG_VERSION - The version of the Python interpreter found as a human -# readable string. -# -# PYTHON_SITE_PACKAGES_DIR - Location of the Python site-packages directory. -# -# PYTHON_INCLUDE_PATH - Directory holding the python.h include file. -# -# PYTHON_LIBRARY, PYTHON_LIBRARIES- Location of the Python library. - -# Copyright (c) 2007, Simon Edwards -# Redistribution and use is allowed according to the terms of the BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. - - - -INCLUDE(CMakeFindFrameworks) - -if(EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}" AND EXISTS "${PYTHON_SITE_PACKAGES_DIR}") - # Already in cache, be silent - set(PYTHONLIBRARY_FOUND TRUE) -else(EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}" AND EXISTS "${PYTHON_SITE_PACKAGES_DIR}") - - set(_custom_python_fw FALSE) - if(APPLE AND PYTHON_CUSTOM_FRAMEWORK) - if("${PYTHON_CUSTOM_FRAMEWORK}" MATCHES "Python\\.framework") - STRING(REGEX REPLACE "(.*Python\\.framework).*$" "\\1" _python_fw "${PYTHON_CUSTOM_FRAMEWORK}") - set(PYTHON_EXECUTABLE "${_python_fw}/Versions/Current/bin/python") - set(PYTHON_INCLUDE_PATH "${_python_fw}/Versions/Current/Headers") - set(PYTHON_LIBRARY "${_python_fw}/Versions/Current/Python") - if(EXISTS "${PYTHON_EXECUTABLE}" AND EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}") - set(_custom_python_fw TRUE) - endif() - endif("${PYTHON_CUSTOM_FRAMEWORK}" MATCHES "Python\\.framework") - endif(APPLE AND PYTHON_CUSTOM_FRAMEWORK) - - FIND_PACKAGE(PythonInterp) - - if(PYTHONINTERP_FOUND) - - FIND_FILE(_find_lib_python_py FindLibPython.py PATHS ${CMAKE_MODULE_PATH}) - - EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} ${_find_lib_python_py} OUTPUT_VARIABLE python_config) - if(python_config) - STRING(REGEX REPLACE ".*exec_prefix:([^\n]+).*$" "\\1" PYTHON_PREFIX ${python_config}) - STRING(REGEX REPLACE ".*\nshort_version:([^\n]+).*$" "\\1" PYTHON_SHORT_VERSION ${python_config}) - STRING(REGEX REPLACE ".*\nlong_version:([^\n]+).*$" "\\1" PYTHON_LONG_VERSION ${python_config}) - STRING(REGEX REPLACE ".*\npy_inc_dir:([^\n]+).*$" "\\1" PYTHON_INCLUDE_PATH ${python_config}) - if(NOT PYTHON_SITE_PACKAGES_DIR) - if(NOT PYTHON_LIBS_WITH_KDE_LIBS) - STRING(REGEX REPLACE ".*\nsite_packages_dir:([^\n]+).*$" "\\1" PYTHON_SITE_PACKAGES_DIR ${python_config}) - else(NOT PYTHON_LIBS_WITH_KDE_LIBS) - set(PYTHON_SITE_PACKAGES_DIR ${KDE4_LIB_INSTALL_DIR}/python${PYTHON_SHORT_VERSION}/site-packages) - endif(NOT PYTHON_LIBS_WITH_KDE_LIBS) - endif(NOT PYTHON_SITE_PACKAGES_DIR) - STRING(REGEX REPLACE "([0-9]+).([0-9]+)" "\\1\\2" PYTHON_SHORT_VERSION_NO_DOT ${PYTHON_SHORT_VERSION}) - set(PYTHON_LIBRARY_NAMES python${PYTHON_SHORT_VERSION} python${PYTHON_SHORT_VERSION_NO_DOT}) - if(WIN32) - STRING(REPLACE "\\" "/" PYTHON_SITE_PACKAGES_DIR ${PYTHON_SITE_PACKAGES_DIR}) - endif(WIN32) - FIND_LIBRARY(PYTHON_LIBRARY NAMES ${PYTHON_LIBRARY_NAMES} HINTS ${PYTHON_INCLUDE_PATH}/../libs) - set(PYTHON_INCLUDE_PATH ${PYTHON_INCLUDE_PATH} CACHE FILEPATH "Directory holding the python.h include file" FORCE) - set(PYTHONLIBRARY_FOUND TRUE) - endif(python_config) - - # adapted from cmake's builtin FindPythonLibs - if(APPLE AND NOT _custom_python_fw) - CMAKE_FIND_FRAMEWORKS(Python) - set(PYTHON_FRAMEWORK_INCLUDES) - if(Python_FRAMEWORKS) - # If a framework has been selected for the include path, - # make sure "-framework" is used to link it. - if("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework") - set(PYTHON_LIBRARY "") - set(PYTHON_DEBUG_LIBRARY "") - endif("${PYTHON_INCLUDE_PATH}" MATCHES "Python\\.framework") - if(NOT PYTHON_LIBRARY) - if(APPLE) - set (PYTHON_LIBRARY "-F/usr/local/Frameworks -F${HOME}/Library/Frameworks -framework Python" CACHE FILEPATH "Python Framework" FORCE) - else() - set (PYTHON_LIBRARY "-framework Python" CACHE FILEPATH "Python Framework" FORCE) - endif() - endif(NOT PYTHON_LIBRARY) - set(PYTHONLIBRARY_FOUND TRUE) - endif(Python_FRAMEWORKS) - endif(APPLE AND NOT _custom_python_fw) - endif(PYTHONINTERP_FOUND) - - if(PYTHONLIBRARY_FOUND) - if(APPLE) - # keep reference to system or custom python site-packages - # useful during app-bundling operations - set(PYTHON_SITE_PACKAGES_SYS ${PYTHON_SITE_PACKAGES_DIR}) - endif(APPLE) - set(PYTHON_LIBRARIES ${PYTHON_LIBRARY}) - if(NOT PYTHONLIBRARY_FIND_QUIETLY) - message(STATUS "Found Python executable: ${PYTHON_EXECUTABLE}") - message(STATUS "Found Python version: ${PYTHON_LONG_VERSION}") - message(STATUS "Found Python library: ${PYTHON_LIBRARY}") - endif(NOT PYTHONLIBRARY_FIND_QUIETLY) - else(PYTHONLIBRARY_FOUND) - if(PYTHONLIBRARY_FIND_REQUIRED) - message(FATAL_ERROR "Could not find Python") - endif(PYTHONLIBRARY_FIND_REQUIRED) - endif(PYTHONLIBRARY_FOUND) - -endif (EXISTS "${PYTHON_INCLUDE_PATH}" AND EXISTS "${PYTHON_LIBRARY}" AND EXISTS "${PYTHON_SITE_PACKAGES_DIR}") diff --git a/cmake/FindPythonLibsCustom.cmake b/cmake/FindPythonLibsCustom.cmake new file mode 100644 index 0000000..860d382 --- /dev/null +++ b/cmake/FindPythonLibsCustom.cmake @@ -0,0 +1,46 @@ +# FindPythonLibsCustom +# -------------------- +# +# This module locates Python libraries. +# +# This code sets the non-deprecated subset of the variables set by CMake's +# built-in FindPythonLibs: +# +# PYTHONLIBS_FOUND - have the Python libs been found +# PYTHON_LIBRARIES - path to the Python library +# PYTHON_INCLUDE_DIRS - path to where Python.h is found +# PYTHONLIBS_VERSION_STRING - version of the Python libs found +# +# If calling both `find_package(PythonInterp)` and +# `find_package(PythonLibsCustom)`, call `find_package(PythonInterp)` first. +# +# Compatibility: Believed to be CMake >=2.8.8 (earlier versions of +# FindPythonLibs do not provide PYTHONLIBS_VERSION_STRING). + +include(FindPackageHandleStandardArgs) + +find_package(PythonInterp) +if(PYTHONINTERP_FOUND) + find_file(_Python_pyinfo_py pyinfo.py PATHS ${CMAKE_MODULE_PATH}) + if(_Python_pyinfo_py) + execute_process(COMMAND ${PYTHON_EXECUTABLE} ${_Python_pyinfo_py} OUTPUT_VARIABLE _Python_pyinfo) + unset(_Python_pyinfo_py) + if(_Python_pyinfo) + string(REGEX REPLACE "^.*\ninc_dir:([^\n]*)\n.*$" "\\1" PYTHON_INCLUDE_DIRS ${_Python_pyinfo}) + string(REGEX REPLACE "^.*\nlib_name:([^\n]*)\n.*$" "\\1" _Python_lib_name ${_Python_pyinfo}) + string(REGEX REPLACE "^.*\nlib_ver:([^\n]*)\n.*$" "\\1" PYTHONLIBS_VERSION_STRING ${_Python_pyinfo}) + string(REGEX REPLACE "^.*\nlib_dir:([^\n]*)\n.*$" "\\1" _Python_lib_dir ${_Python_pyinfo}) + unset(_Python_pyinfo) + find_library(PYTHON_LIBRARIES NAMES ${_Python_lib_name} HINTS ${_Python_lib_dir}) + unset(_Python_lib_name) + unset(_Python_lib_dir) + endif(_Python_pyinfo) + else(_Python_pyinfo_py) + message(SEND_ERROR "failed to find pyinfo.py") + endif(_Python_pyinfo_py) + FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS PYTHONLIBS_VERSION_STRING) +else(PYTHONINTERP_FOUND) + message(STATUS "Python interpreter not found, falling back to FindPythonLibs") + find_package(PythonLibs) +endif(PYTHONINTERP_FOUND) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibsCustom DEFAULT_MSG PYTHONLIBS_FOUND) diff --git a/cmake/pyinfo.py b/cmake/pyinfo.py new file mode 100755 index 0000000..9635c9f --- /dev/null +++ b/cmake/pyinfo.py @@ -0,0 +1,21 @@ +from __future__ import print_function + +from distutils import sysconfig + +if __name__ == "__main__": + print() + print("inc_dir:%s" % sysconfig.get_python_inc()) + # From testing, these interpretations of LIBRARY and LIBPL are valid for + # both Python 2.7 and Python 3.5 on both Ubuntu and Mac OS X (with Homebrew + # Python). + lib_name = sysconfig.get_config_var("LIBRARY") + if lib_name.startswith("lib"): + lib_name = lib_name[3:] + if lib_name.endswith(".a"): + lib_name = lib_name[:-2] + print("lib_name:%s" % lib_name) + lib_ver = lib_name + if lib_ver.startswith("python"): + lib_ver = lib_ver[6:] + print("lib_ver:%s" % lib_ver) + print("lib_dir:%s" % sysconfig.get_config_var("LIBPL"))