diff --git a/cmake/GtsamCythonWrap.cmake b/cmake/GtsamCythonWrap.cmake index 4894596f6..d785b4ef8 100644 --- a/cmake/GtsamCythonWrap.cmake +++ b/cmake/GtsamCythonWrap.cmake @@ -145,3 +145,30 @@ function(install_cython_scripts source_directory dest_directory patterns) endfunction() +# Helper function to install specific files and handle multiple build types where the scripts +# should be installed to all build type toolboxes +# +# Arguments: +# source_files: The source files to be installed. +# dest_directory: The destination directory to install to. +function(install_cython_files source_files dest_directory) + + if(GTSAM_BUILD_TYPE_POSTFIXES) + foreach(build_type ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER "${build_type}" build_type_upper) + if(${build_type_upper} STREQUAL "RELEASE") + set(build_type_tag "") # Don't create release mode tag on installed directory + else() + set(build_type_tag "${build_type}") + endif() + # Split up filename to strip trailing '/' in GTSAM_CYTHON_INSTALL_PATH if there is one + get_filename_component(location "${dest_directory}" PATH) + get_filename_component(name "${dest_directory}" NAME) + install(FILES "${source_files}" DESTINATION "${location}/${name}${build_type_tag}") + endforeach() + else() + install(FILES "${source_files}" DESTINATION "${dest_directory}") + endif() + +endfunction() + diff --git a/cython/CMakeLists.txt b/cython/CMakeLists.txt index f7c5598c0..209c85f54 100644 --- a/cython/CMakeLists.txt +++ b/cython/CMakeLists.txt @@ -2,3 +2,14 @@ include(GtsamCythonWrap) # install scripts and tests install_cython_scripts("${CMAKE_SOURCE_DIR}/cython/gtsam" "${GTSAM_CYTHON_INSTALL_PATH}" "*.py") + + +# generate __init__.py into build folder (configured with or without gtsam_unstable import line) +# This also makes the build/cython/gtsam folder a python package, so gtsam can be found while wrapping gtsam_unstable +if(GTSAM_BUILD_UNSTABLE) + set(GTSAM_UNSTABLE_IMPORT "from gtsam_unstable import *") +endif() +configure_file(${CMAKE_SOURCE_DIR}/cython/gtsam/__init__.py.in ${PROJECT_BINARY_DIR}/cython/gtsam/__init__.py) + +# Install the custom-generated __init__.py +install_cython_files("${PROJECT_BINARY_DIR}/cython/gtsam/__init__.py" "${GTSAM_CYTHON_INSTALL_PATH}/gtsam") diff --git a/cython/gtsam/__init__.py b/cython/gtsam/__init__.py deleted file mode 100644 index ee2f47b59..000000000 --- a/cython/gtsam/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from gtsam import * -from gtsam_unstable import * diff --git a/cython/gtsam/__init__.py.in b/cython/gtsam/__init__.py.in new file mode 100644 index 000000000..7d456023f --- /dev/null +++ b/cython/gtsam/__init__.py.in @@ -0,0 +1,2 @@ +from gtsam import * +${GTSAM_UNSTABLE_IMPORT} diff --git a/cython/gtsam_unstable/setup.py.in b/cython/gtsam_unstable/setup.py.in index fe6a02fbd..bd3202c8d 100644 --- a/cython/gtsam_unstable/setup.py.in +++ b/cython/gtsam_unstable/setup.py.in @@ -20,7 +20,8 @@ setup( "${Boost_INCLUDE_DIR}" ] + eigency.get_includes(include_eigen=False), libraries=['gtsam', 'gtsam_unstable'], - library_dirs=["${GTSAM_DIR}/../../"], + library_dirs = ["${CMAKE_BINARY_DIR}/gtsam", + "${CMAKE_BINARY_DIR}/gtsam_unstable"], language="c++", extra_compile_args="${CMAKE_CXX_FLAGS}".split(), extra_link_args="${CMAKE_SHARED_LINKER_FLAGS}".split())) diff --git a/gtsam_unstable/CMakeLists.txt b/gtsam_unstable/CMakeLists.txt index eb4937cf1..7e26bca04 100644 --- a/gtsam_unstable/CMakeLists.txt +++ b/gtsam_unstable/CMakeLists.txt @@ -131,9 +131,6 @@ if (GTSAM_INSTALL_CYTHON_TOOLBOX) "${GTSAM_CYTHON_INSTALL_PATH}/gtsam" # install path ) add_dependencies(gtsam_unstable_cython_wrapper gtsam_cython_wrapper) - # making the build/cython/gtsam folder a python package, so gtsam can be found while wrapping gtsam_unstable - add_custom_target(copy_gtsam_init COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/cython/gtsam/__init__.py" "${CMAKE_BINARY_DIR}/cython/gtsam") - add_dependencies(gtsam_unstable_cython_wrapper copy_gtsam_init) endif () # Build examples