Skip to content

Commit

Permalink
build(wrappers/python): improve python detection
Browse files Browse the repository at this point in the history
Signed-off-by: Benn Snyder <benn.snyder@gmail.com>
  • Loading branch information
piedar committed Feb 16, 2020
1 parent 48a5c66 commit 29b4829
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Notice: If you have the newer Kinect v2 (XBox One), use [OpenKinect/libfreenect2
To build libfreenect, you'll need

- [libusb](http://libusb.info) >= 1.0.18 (Windows needs >= 1.0.22)
- [CMake](http://cmake.org) >= 3.1.0
- [CMake](http://cmake.org) >= 3.12.4
- [python](http://python.org) >= 2.7 or >= 3.3 (only if BUILD_PYTHON=ON or BUILD_PYTHON2=ON or BUILD_PYTHON3=ON or BUILD_REDIST_PACKAGE=OFF)

For the examples, you'll need
Expand Down
70 changes: 21 additions & 49 deletions wrappers/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,34 @@
# Python extension builder
######################################################################################

macro(Python_BUILD_EXTENSION Python_BUILD_VERSION)
cmake_minimum_required(VERSION 3.12.4)

# Unfortunately, we can't use the cache because it treats 2 and 3 the same, so
# we wouldn't be able to compile against both.
foreach(_python_var
PYTHONINTERP_FOUND PYTHON_EXECUTABLE PYTHON_VERSION_STRING
PYTHON_VERSION_MAJOR PYTHON_VERSION_MINOR PYTHON_VERSION_PATCH)
unset(${_python_var})
unset(${_python_var} CACHE)
endforeach(_python_var)
foreach(_python_var
PYTHONLIBS_FOUND PYTHONLIBS_VERSION_STRING PYTHON_DEBUG_LIBRARIES
PYTHON_INCLUDE_DIR PYTHON_INCLUDE_DIRS PYTHON_INCLUDE_PATH PYTHON_LIBRARIES
PYTHON_LIBRARY PYTHON_LIBRARY_DEBUG)
unset(${_python_var})
unset(${_python_var} CACHE)
endforeach(_python_var)
set(Python2_EXACTVERSION CACHE STRING "exact version of python2")
set(Python3_EXACTVERSION CACHE STRING "exact version of python3")

set(Python_ADDITIONAL_VERSIONS ${Python_BUILD_VERSION})
find_package(PythonInterp)
set(PYTHON${Python_BUILD_VERSION}INTERP_FOUND ${PYTHONINTERP_FOUND})
set(PYTHON${Python_BUILD_VERSION}_EXECUTABLE ${PYTHON_EXECUTABLE})
set(PYTHON${Python_BUILD_VERSION}_VERSION_STRING ${PYTHON_VERSION_STRING})
set(PYTHON${Python_BUILD_VERSION}_VERSION_MAJOR ${PYTHON_VERSION_MAJOR})
set(PYTHON${Python_BUILD_VERSION}_VERSION_MINOR ${PYTHON_VERSION_MINOR})
set(PYTHON${Python_BUILD_VERSION}_VERSION_PATCH ${PYTHON_VERSION_PATCH})
find_package(PythonLibs)
set(PYTHON${Python_BUILD_VERSION}LIBS_FOUND ${PYTHONLIBS_FOUND})
set(PYTHON${Python_BUILD_VERSION}LIBS_VERSION_STRING ${PYTHONLIBS_VERSION_STRING})
set(PYTHON${Python_BUILD_VERSION}_DEBUG_LIBRARIES ${PYTHON_DEBUG_LIBRARIES})
set(PYTHON${Python_BUILD_VERSION}_INCLUDE_PATH ${PYTHON_INCLUDE_PATH})
set(PYTHON${Python_BUILD_VERSION}_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
set(PYTHON${Python_BUILD_VERSION}_LIBRARIES ${PYTHON_LIBRARIES})
find_program(CYTHON_EXECUTABLE cython)
macro(Python_BUILD_EXTENSION Python_BUILD_VERSION)

if(NOT ${PYTHON${Python_BUILD_VERSION}_VERSION_MAJOR} EQUAL ${Python_BUILD_VERSION})
message(FATAL_ERROR "Unable to find Python ${Python_BUILD_VERSION} interpreter.")
endif()
if(NOT ${PYTHON${Python_BUILD_VERSION}_VERSION_STRING} EQUAL ${PYTHON${Python_BUILD_VERSION}LIBS_VERSION_STRING})
message(FATAL_ERROR
"Unable to find consistent Python ${Python_BUILD_VERSION} libraries. "
"Python interpreter is ${PYTHON${Python_BUILD_VERSION}_VERSION_STRING}, "
"but libraries are ${PYTHON${Python_BUILD_VERSION}LIBS_VERSION_STRING}.")
if(NOT ${Python${Python_BUILD_VERSION}_EXACTVERSION} STREQUAL "")
find_package(Python${Python_BUILD_VERSION} ${Python${Python_BUILD_VERSION}_EXACTVERSION} EXACT REQUIRED COMPONENTS Interpreter Development)
else()
find_package(Python${Python_BUILD_VERSION} REQUIRED COMPONENTS Interpreter Development)
endif()

find_program(CYTHON_EXECUTABLE cython)

# Figure out installation path
# CMake populates this var automatically but we need to specify prefix
execute_process(COMMAND
${PYTHON${Python_BUILD_VERSION}_EXECUTABLE}
-c "from distutils.sysconfig import get_python_lib; print(get_python_lib(prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE PYTHON${Python_BUILD_VERSION}_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
${Python${Python_BUILD_VERSION}_EXECUTABLE}
-c "from distutils.sysconfig import get_python_lib; print(get_python_lib(plat_specific=False, standard_lib=False, prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE Python${Python_BUILD_VERSION}_SITELIB OUTPUT_STRIP_TRAILING_WHITESPACE)

# Figure out numpy include path
# todo: CMake >= 3.14 populates this var automatically when using COMPONENTS NumPy
execute_process(COMMAND
${PYTHON${Python_BUILD_VERSION}_EXECUTABLE}
${Python${Python_BUILD_VERSION}_EXECUTABLE}
-c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE PYTHON${Python_BUILD_VERSION}_NUMPY_INCLUDE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
OUTPUT_VARIABLE Python${Python_BUILD_VERSION}_NumPy_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE)

# How to Cython the .pyx file
add_custom_command(OUTPUT freenect${Python_BUILD_VERSION}.c
Expand All @@ -76,15 +48,15 @@ set_target_properties(cython${Python_BUILD_VERSION}_freenect PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python${Python_BUILD_VERSION})
target_link_libraries(cython${Python_BUILD_VERSION}_freenect
freenect_sync
${PYTHON${Python_BUILD_VERSION}_LIBRARIES})
${Python${Python_BUILD_VERSION}_LIBRARIES})
target_include_directories(cython${Python_BUILD_VERSION}_freenect PRIVATE
${PYTHON${Python_BUILD_VERSION}_INCLUDE_PATH}
${Python${Python_BUILD_VERSION}_INCLUDE_DIRS}
../c_sync/
${PYTHON${Python_BUILD_VERSION}_NUMPY_INCLUDE_PATH})
${Python${Python_BUILD_VERSION}_NumPy_INCLUDE_DIRS})

# Install the extension
install(TARGETS cython${Python_BUILD_VERSION}_freenect
DESTINATION ${PYTHON${Python_BUILD_VERSION}_SITE_PACKAGES})
DESTINATION ${Python${Python_BUILD_VERSION}_SITELIB})

# TODO: decide on what to do with demo_ scripts and were to install
# them
Expand Down

0 comments on commit 29b4829

Please sign in to comment.