Skip to content

Commit

Permalink
py3k: Allow build Python wrapper on 2 and/or 3.
Browse files Browse the repository at this point in the history
Signed-off-by: Elliott Sales de Andrade <[email protected]>
  • Loading branch information
QuLogic committed Nov 16, 2015
1 parent 244a326 commit 3579e20
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
######################################################################################

cmake_minimum_required(VERSION 2.6)
set(PYTHON_EXECUTABLE "python2")

######################################################################################
# Project declaration and options
Expand Down Expand Up @@ -61,7 +60,9 @@ OPTION(BUILD_C_SYNC "Build c synchronous library" ON)
OPTION(BUILD_CPP "Build C++ Library (currently header only)" ON)
OPTION(BUILD_CV "Build OpenCV wrapper" OFF)
OPTION(BUILD_AS3_SERVER "Build the Actionscript 3 Server Example" OFF)
OPTION(BUILD_PYTHON "Build Python extension" OFF)
OPTION(BUILD_PYTHON "Build Python extensions" OFF)
OPTION(BUILD_PYTHON2 "Build Python 2 extension" OFF)
OPTION(BUILD_PYTHON3 "Build Python 3 extension" OFF)
OPTION(BUILD_OPENNI2_DRIVER "Build libfreenect driver for OpenNI2" OFF)
IF(PROJECT_OS_LINUX)
OPTION(BUILD_CPACK_DEB "Build an DEB using CPack" OFF)
Expand Down Expand Up @@ -157,6 +158,10 @@ IF(BUILD_AS3_SERVER)
ENDIF()

IF(BUILD_PYTHON)
set(BUILD_PYTHON2 ON)
set(BUILD_PYTHON3 ON)
ENDIF()
IF(BUILD_PYTHON2 OR BUILD_PYTHON3)
add_subdirectory (wrappers/python)
ENDIF()

Expand Down
65 changes: 49 additions & 16 deletions wrappers/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,71 @@
# Python extension builder
######################################################################################

include(FindPythonInterp)
include(FindPythonLibs)
macro(Python_BUILD_EXTENSION Python_BUILD_VERSION)

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)

# Figure out installation path
execute_process(COMMAND
${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(prefix='${CMAKE_INSTALL_PREFIX}'))"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
${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)

# Figure out numpy include path
execute_process(COMMAND
${PYTHON_EXECUTABLE} -c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE NUMPY_INCLUDE_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
${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)

# How to Cython the .pyx file
add_custom_command(OUTPUT freenect.c
COMMAND ${CYTHON_EXECUTABLE} -o freenect.c "${CMAKE_CURRENT_SOURCE_DIR}/freenect.pyx")
list(APPEND ADDITIONAL_MAKE_CLEAN_FILES freenect.c)
add_custom_command(OUTPUT freenect${Python_BUILD_VERSION}.c
COMMAND
${CYTHON_EXECUTABLE}
-${Python_BUILD_VERSION}
-o freenect${Python_BUILD_VERSION}.c
"${CMAKE_CURRENT_SOURCE_DIR}/freenect.pyx")
list(APPEND ADDITIONAL_MAKE_CLEAN_FILES freenect${Python_BUILD_VERSION}.c)

# Compile the extension
add_library(cython_freenect MODULE freenect.c)
set_target_properties(cython_freenect PROPERTIES
add_library(cython${Python_BUILD_VERSION}_freenect MODULE freenect${Python_BUILD_VERSION}.c)
set_target_properties(cython${Python_BUILD_VERSION}_freenect PROPERTIES
PREFIX ""
OUTPUT_NAME "freenect"
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(cython_freenect freenect_sync ${PYTHON_LIBRARIES})
include_directories(${PYTHON_INCLUDE_PATH} ../c_sync/ ${NUMPY_INCLUDE_PATH})
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})
target_include_directories(cython${Python_BUILD_VERSION}_freenect PRIVATE
${PYTHON${Python_BUILD_VERSION}_INCLUDE_PATH}
../c_sync/
${PYTHON${Python_BUILD_VERSION}_NUMPY_INCLUDE_PATH})

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

# TODO: decide on what to do with demo_ scripts and were to install
# them
endmacro(Python_BUILD_EXTENSION)

if (BUILD_PYTHON2)
Python_BUILD_EXTENSION(2)
endif(BUILD_PYTHON2)
if (BUILD_PYTHON3)
Python_BUILD_EXTENSION(3)
endif(BUILD_PYTHON3)

0 comments on commit 3579e20

Please sign in to comment.