Skip to content

Commit

Permalink
Rewrite Python library finding
Browse files Browse the repository at this point in the history
Should fix building with Python 3 on Mac OS X.
  • Loading branch information
nixprime committed Sep 16, 2016
1 parent 558c7a2 commit 7f96ff3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 161 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)

Expand Down
40 changes: 0 additions & 40 deletions cmake/FindLibPython.py

This file was deleted.

117 changes: 0 additions & 117 deletions cmake/FindPythonLibrary.cmake

This file was deleted.

46 changes: 46 additions & 0 deletions cmake/FindPythonLibsCustom.cmake
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions cmake/pyinfo.py
Original file line number Diff line number Diff line change
@@ -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"))

0 comments on commit 7f96ff3

Please sign in to comment.