From d51c6f3313174b7bd6f685fd7ca4c40d9c6f49f8 Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Tue, 1 Dec 2015 11:47:28 +0100 Subject: [PATCH] Fix cmake to use default python and boost python versions --- cmake/GtsamPythonWrap.cmake | 2 +- python/CMakeLists.txt | 67 +++++++++++++++++++++++++------ python/handwritten/CMakeLists.txt | 2 +- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/cmake/GtsamPythonWrap.cmake b/cmake/GtsamPythonWrap.cmake index f7d468940..714e37488 100644 --- a/cmake/GtsamPythonWrap.cmake +++ b/cmake/GtsamPythonWrap.cmake @@ -1,5 +1,5 @@ #Setup cache options -set(GTSAM_PYTHON_VERSION "2.7" CACHE STRING "Version of python used to build the wrapper") +set(GTSAM_PYTHON_VERSION "Default" CACHE STRING "Target python version for GTSAM python module. Use 'Default' to chose the default version") 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) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 4fe0d8cf9..28b916ab2 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,10 +1,15 @@ +# Guard to avoid breaking this code in ccmake if by accident GTSAM_PYTHON_VERSION is set to an empty string +if(GTSAM_PYTHON_VERSION STREQUAL "") + set(GTSAM_PYTHON_VERSION "Default" CACHE STRING "Target python version for GTSAM python module. Use 'Default' to chose the default version" FORCE) +endif() + # 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)) +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}))) +if(NOT (${GTSAM_PYTHON_VERSION} MATCHES ${GTSAM_LAST_PYTHON_VERSION})) unset(PYTHON_INCLUDE_DIR CACHE) unset(PYTHON_INCLUDE_DIR2 CACHE) unset(PYTHON_LIBRARY CACHE) @@ -12,21 +17,57 @@ if((NOT (${GTSAM_PYTHON_VERSION} MATCHES ${GTSAM_LAST_PYTHON_VERSION}))) set(GTSAM_LAST_PYTHON_VERSION ${GTSAM_PYTHON_VERSION} CACHE STRING "Updating python version used in the last build" FORCE) endif() +# Compose strings used to specify the boost python version. They will be empty if we want to use the defaut +if(NOT GTSAM_PYTHON_VERSION STREQUAL "Default") + string(REPLACE "." "" BOOST_PYTHON_VERSION_STRING ${GTSAM_PYTHON_VERSION}) # Remove '.' from version + string(SUBSTRING ${BOOST_PYTHON_VERSION_STRING} 0 2 BOOST_PYTHON_VERSION_STRING) # Trim version number to 2 digits + set(BOOST_PYTHON_VERSION_STRING "-py${BOOST_PYTHON_VERSION_STRING}") # Add '-py' prefix + string(TOUPPER ${BOOST_PYTHON_VERSION_STRING} UPPER_BOOST_PYTHON_VERSION_STRING) # Get uppercase string +else() + set(BOOST_PYTHON_VERSION_STRING "") + set(UPPER_BOOST_PYTHON_VERSION_STRING "") +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 ${GTSAM_PYTHON_VERSION} REQUIRED) -include_directories(${PYTHON_INCLUDE_DIRS}) +# Find Python +if(GTSAM_PYTHON_VERSION STREQUAL "Default") + # If no version is specified when looking for PythonLibs it searches the default version. + # See: https://cmake.org/cmake/help/v3.1/module/FindPythonInterp.html + find_package(PythonLibs) +else() + find_package(PythonLibs ${GTSAM_PYTHON_VERSION}) +endif() -# 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}) +# Find Boost Python +find_package(Boost COMPONENTS python${BOOST_PYTHON_VERSION_STRING}) -# Numpy_Eigen -include_directories(${CMAKE_SOURCE_DIR}/gtsam/3rdparty/numpy_eigen/include/) +# Add handwritten directory if we found python and boost python +if(Boost_PYTHON${UPPER_BOOST_PYTHON_VERSION_STRING}_FOUND AND PYTHONLIBS_FOUND) + include_directories(${PYTHON_INCLUDE_DIRS}) + include_directories(${Boost_INCLUDE_DIRS}) + include_directories(${CMAKE_SOURCE_DIR}/gtsam/3rdparty/numpy_eigen/include/) + add_subdirectory(handwritten) +# Disable python module if we didn't find required lybraries +else() + set(GTSAM_BUILD_PYTHON OFF CACHE BOOL "Build Python wrapper statically (increases build time)" FORCE) +endif() -add_subdirectory(handwritten) \ No newline at end of file +# Print warnings (useful for ccmake) +if(NOT PYTHONLIBS_FOUND) + if(GTSAM_PYTHON_VERSION STREQUAL "Default") + message(WARNING "Default PythonLibs was not found -- Python module cannot be built. Option GTSAM_BUILD_PYTHON disabled.") + else() + message(WARNING "PythonLibs version ${GTSAM_PYTHON_VERSION} was not found -- Python module cannot be built. Option GTSAM_BUILD_PYTHON disabled.") + endif() +endif() + +if(NOT Boost_PYTHON${UPPER_BOOST_PYTHON_VERSION_STRING}_FOUND) + if(GTSAM_PYTHON_VERSION STREQUAL "Default") + message(WARNING "Default Boost python was not found -- Python module cannot be built. Option GTSAM_BUILD_PYTHON disabled.") + else() + message(WARNING "Boost Python for python ${GTSAM_PYTHON_VERSION} was not found -- Python module cannot be built. Option GTSAM_BUILD_PYTHON disabled.") + endif() +endif() \ No newline at end of file diff --git a/python/handwritten/CMakeLists.txt b/python/handwritten/CMakeLists.txt index 893fbae71..ffd970217 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-PY${PYTHON_VERSION_NUMBER}_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp +target_link_libraries(${moduleName}_python ${Boost_PYTHON${UPPER_BOOST_PYTHON_VERSION_STRING}_LIBRARY} ${PYTHON_LIBRARY} ${gtsamLib}) #temp # On OSX and Linux, the python library must end in the extension .so. Build this # filename here.