From 5b116a4a67fbe930fdc01913537f1ed743e59a05 Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Sat, 28 Nov 2015 00:34:48 +0100 Subject: [PATCH] Add option to chose target python version to create module --- cmake/GtsamPythonWrap.cmake | 13 +++++++------ python/CMakeLists.txt | 29 ++++++++++++++++++++++++----- python/handwritten/CMakeLists.txt | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cmake/GtsamPythonWrap.cmake b/cmake/GtsamPythonWrap.cmake index c23ee783d..f7d468940 100644 --- a/cmake/GtsamPythonWrap.cmake +++ b/cmake/GtsamPythonWrap.cmake @@ -1,4 +1,5 @@ #Setup cache options +set(GTSAM_PYTHON_VERSION "2.7" CACHE STRING "Version of python used to build the wrapper") set(GTSAM_BUILD_PYTHON_FLAGS "" CACHE STRING "Extra flags for running Matlab PYTHON compilation") set(GTSAM_PYTHON_INSTALL_PATH "" CACHE PATH "Python toolbox destination, blank defaults to CMAKE_INSTALL_PREFIX/borg/python") if(NOT GTSAM_PYTHON_INSTALL_PATH) @@ -7,13 +8,13 @@ endif() #Author: Paul Furgale Modified by Andrew Melim function(wrap_python TARGET_NAME PYTHON_MODULE_DIRECTORY) - # Boost - find_package(Boost COMPONENTS python filesystem system REQUIRED) - include_directories(${Boost_INCLUDE_DIRS}) + # # Boost + # find_package(Boost COMPONENTS python filesystem system REQUIRED) + # include_directories(${Boost_INCLUDE_DIRS}) - # Find Python - FIND_PACKAGE(PythonLibs 2.7 REQUIRED) - INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS}) + # # Find Python + # FIND_PACKAGE(PythonLibs 2.7 REQUIRED) + # INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS}) IF(APPLE) # The apple framework headers don't include the numpy headers for some reason. diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 0fcb1fc28..4fe0d8cf9 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,12 +1,31 @@ -# Obtain Dependencies -# Boost Python -find_package(Boost COMPONENTS python REQUIRED) -include_directories(${Boost_INCLUDE_DIRS}) +# The code below allows to clear the PythonLibs cache if we change GTSAM_PYTHON_VERSION +# Inspired from the solution found here: http://blog.bethcodes.com/cmake-tips-tricks-drop-down-list +if((NOT DEFINED GTSAM_LAST_PYTHON_VERSION)) + set(GTSAM_LAST_PYTHON_VERSION ${GTSAM_PYTHON_VERSION} CACHE STRING "Python version used in the last build") + mark_as_advanced(FORCE GTSAM_LAST_PYTHON_VERSION) +endif() +if((NOT (${GTSAM_PYTHON_VERSION} MATCHES ${GTSAM_LAST_PYTHON_VERSION}))) + unset(PYTHON_INCLUDE_DIR CACHE) + unset(PYTHON_INCLUDE_DIR2 CACHE) + unset(PYTHON_LIBRARY CACHE) + unset(PYTHON_LIBRARY_DEBUG CACHE) + set(GTSAM_LAST_PYTHON_VERSION ${GTSAM_PYTHON_VERSION} CACHE STRING "Updating python version used in the last build" FORCE) +endif() + +# Be sure that python version can be found by FindPythonLibs.cmake +# See: http://stackoverflow.com/a/15660652/2220173 +set(Python_ADDITIONAL_VERSIONS ${GTSAM_PYTHON_VERSION}) # Find Python -find_package(PythonLibs 2.7 REQUIRED) +find_package(PythonLibs ${GTSAM_PYTHON_VERSION} REQUIRED) include_directories(${PYTHON_INCLUDE_DIRS}) +# Boost Python +string(REPLACE "." "" PYTHON_VERSION_NUMBER ${GTSAM_PYTHON_VERSION}) # Remove '.' from version +string(SUBSTRING ${PYTHON_VERSION_NUMBER} 0 2 PYTHON_VERSION_NUMBER) # Trim version number to 2 digits +find_package(Boost COMPONENTS python-py${PYTHON_VERSION_NUMBER} REQUIRED) +include_directories(${Boost_INCLUDE_DIRS}) + # Numpy_Eigen include_directories(${CMAKE_SOURCE_DIR}/gtsam/3rdparty/numpy_eigen/include/) diff --git a/python/handwritten/CMakeLists.txt b/python/handwritten/CMakeLists.txt index 1090ef9cf..893fbae71 100644 --- a/python/handwritten/CMakeLists.txt +++ b/python/handwritten/CMakeLists.txt @@ -18,7 +18,7 @@ set_target_properties(${moduleName}_python PROPERTIES OUTPUT_NAME ${moduleName}_python CLEAN_DIRECT_OUTPUT 1) -target_link_libraries(${moduleName}_python ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp +target_link_libraries(${moduleName}_python ${Boost_PYTHON-PY${PYTHON_VERSION_NUMBER}_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp # On OSX and Linux, the python library must end in the extension .so. Build this # filename here.