From c136dac61e42c1d5736607ef9f0da113d806f400 Mon Sep 17 00:00:00 2001 From: Sebastian Hahta <joseha@utu.fi> Date: Mon, 9 Nov 2020 18:39:02 +0200 Subject: [PATCH] Python tests (SDK) --- SDK/CPP/CMakeLists.txt | 8 +++- SDK/CPP/public/CMakeLists.txt | 40 ++++++++++++++----- SDK/CPP/public/include/voltu/system.hpp | 2 +- SDK/CPP/public/python/CMakeLists.txt | 40 +++++++++---------- SDK/CPP/public/python/tests/CMakeLists.txt | 12 ------ SDK/CPP/tests/CMakeLists.txt | 15 +++++++ SDK/CPP/{public/python => }/tests/__init__.py | 0 .../{public/python => }/tests/test_load.py | 3 +- 8 files changed, 72 insertions(+), 48 deletions(-) delete mode 100644 SDK/CPP/public/python/tests/CMakeLists.txt create mode 100644 SDK/CPP/tests/CMakeLists.txt rename SDK/CPP/{public/python => }/tests/__init__.py (100%) rename SDK/CPP/{public/python => }/tests/test_load.py (77%) diff --git a/SDK/CPP/CMakeLists.txt b/SDK/CPP/CMakeLists.txt index f66f714b8..472ac9eb1 100644 --- a/SDK/CPP/CMakeLists.txt +++ b/SDK/CPP/CMakeLists.txt @@ -35,11 +35,17 @@ target_include_directories(voltu target_link_libraries(voltu ftlcommon ftldata ftlctrl ftlrgbd ftlstreams ftlrender Threads::Threads ${OpenCV_LIBS} openvr ftlnet nanogui ${NANOGUI_EXTRA_LIBS} ceres nvidia-ml) +set(SDK_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/sdk") + ExternalProject_Add( voltu_sdk SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/public" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/sdk" + BINARY_DIR ${SDK_BINARY_DIR} INSTALL_COMMAND "" BUILD_ALWAYS true CMAKE_ARGS -DOpenCV_DIR=${OpenCV_DIR} ) + +if (BUILD_TESTS) + add_subdirectory(tests) +endif() diff --git a/SDK/CPP/public/CMakeLists.txt b/SDK/CPP/public/CMakeLists.txt index e31bbdf7f..51c276271 100644 --- a/SDK/CPP/public/CMakeLists.txt +++ b/SDK/CPP/public/CMakeLists.txt @@ -5,18 +5,13 @@ project (voltu_sdk VERSION 0.0.1) include(GNUInstallDirs) option(WITH_OPENCV "Build with OpenCV wrapper" ON) -option(WITH_PYTHON "Build Python module" OFF) -option(WITH_TESTS "Enable unit tests" ON) +option(WITH_PYTHON "Build Python module" ON) -find_package( Eigen3 REQUIRED NO_MODULE ) -find_package( Threads REQUIRED ) - -if (WITH_TESTS) - enable_testing() -endif() +find_package(Eigen3 REQUIRED NO_MODULE) +find_package(Threads REQUIRED) if (WITH_OPENCV) - find_package( OpenCV REQUIRED ) + find_package(OpenCV REQUIRED) endif() if(WIN32) @@ -72,7 +67,30 @@ add_executable(voltu_fusion_evaluator ) target_link_libraries(voltu_fusion_evaluator voltu_sdk) +find_package (Python COMPONENTS Development.Module Interpreter) + +function(find_python_module module) + string(TOUPPER ${module} module_upper) + if(NOT PY_${module_upper}) + execute_process(COMMAND "${Python_EXECUTABLE}" "-c" + "import ${module}; print(${module}.__file__.rstrip('__init__.py'))" + RESULT_VARIABLE _${module}_status + OUTPUT_VARIABLE _${module}_location + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT _${module}_status) + set(PY_${module_upper} ${_${module}_location} CACHE STRING + "Location of Python module ${module}") + endif(NOT _${module}_status) + endif(NOT PY_${module_upper}) + find_package_handle_standard_args(PY_${module} DEFAULT_MSG PY_${module_upper}) +endfunction(find_python_module) + if (WITH_PYTHON) - add_subdirectory(ext/pybind11) - add_subdirectory(python) + find_python_module(ply) + if (Python_FOUND AND PY_PLY) + add_subdirectory(ext/pybind11) + add_subdirectory(python) + else() + message(WARNING "Python dependencies not found, Python module is not built") + endif() endif() diff --git a/SDK/CPP/public/include/voltu/system.hpp b/SDK/CPP/public/include/voltu/system.hpp index 0ac85870c..8fb9c744c 100644 --- a/SDK/CPP/public/include/voltu/system.hpp +++ b/SDK/CPP/public/include/voltu/system.hpp @@ -20,7 +20,7 @@ namespace voltu /** * @brief Voltu semantic versioning information. */ -struct Version +PY_NO_SHARED_PTR struct Version { int major; ///< API Incompatible change int minor; ///< Possible binary incompatible, extensions diff --git a/SDK/CPP/public/python/CMakeLists.txt b/SDK/CPP/public/python/CMakeLists.txt index 27a2a8bdf..b00857e37 100644 --- a/SDK/CPP/public/python/CMakeLists.txt +++ b/SDK/CPP/public/python/CMakeLists.txt @@ -1,29 +1,29 @@ set(SDK_AUTO_HEADERS - voltu/types/channel.hpp - voltu/types/frame.hpp - voltu/types/image.hpp - voltu/types/intrinsics.hpp - voltu/observer.hpp - voltu/feed.hpp - voltu/initialise.hpp - voltu/room.hpp - voltu/source.hpp - voltu/system.hpp + voltu/types/channel.hpp + voltu/types/frame.hpp + voltu/types/image.hpp + voltu/types/intrinsics.hpp + voltu/observer.hpp + voltu/feed.hpp + voltu/initialise.hpp + voltu/room.hpp + voltu/source.hpp + voltu/system.hpp ) add_custom_command( - OUTPUT automatic_bindings.cpp - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen.py - automatic_bindings.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/../include - ${SDK_AUTO_HEADERS} + OUTPUT automatic_bindings.cpp + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen.py + automatic_bindings.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${SDK_AUTO_HEADERS} - DEPENDS voltu_sdk gen.py + DEPENDS voltu_sdk gen.py ) pybind11_add_module(voltu_sdk_py MODULE - automatic_bindings.cpp - module.cpp + automatic_bindings.cpp + module.cpp ) target_include_directories(voltu_sdk_py PUBLIC include) @@ -31,7 +31,3 @@ target_include_directories(voltu_sdk_py PRIVATE .) target_link_libraries(voltu_sdk_py PUBLIC voltu_sdk) set_target_properties(voltu_sdk_py PROPERTIES OUTPUT_NAME voltu) - -if (WITH_TESTS) - add_subdirectory(tests) -endif() diff --git a/SDK/CPP/public/python/tests/CMakeLists.txt b/SDK/CPP/public/python/tests/CMakeLists.txt deleted file mode 100644 index dfdc1b01b..000000000 --- a/SDK/CPP/public/python/tests/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -find_package(Python3 COMPONENTS Interpreter) - -function(add_python_test TEST_NAME TEST_SCRIPT) - add_test(NAME ${TEST_NAME} - COMMAND Python3::Interpreter -B -m unittest ${TEST_SCRIPT} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) - # binary module directory to PYTHONPATH - set_tests_properties(${TEST_NAME} PROPERTIES - ENVIRONMENT PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}/..) -endfunction() - -add_python_test(TestLoad test_load.py) diff --git a/SDK/CPP/tests/CMakeLists.txt b/SDK/CPP/tests/CMakeLists.txt new file mode 100644 index 000000000..489f6e338 --- /dev/null +++ b/SDK/CPP/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +find_package(Python3 COMPONENTS Interpreter) + +function(add_python_test TEST_NAME TEST_SCRIPT) + add_test(NAME ${TEST_NAME} + COMMAND Python3::Interpreter -B -m unittest ${TEST_SCRIPT} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + + # binary module directory to PYTHONPATH, additional variables have to be + # separated by semicolon + set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT + "PYTHONPATH=${SDK_BINARY_DIR}/python;LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/..") + set_property(TEST ${TEST_NAME} APPEND PROPERTY DEPENDS voltu_sdk) +endfunction() + +add_python_test(Py_TestLoad test_load.py) diff --git a/SDK/CPP/public/python/tests/__init__.py b/SDK/CPP/tests/__init__.py similarity index 100% rename from SDK/CPP/public/python/tests/__init__.py rename to SDK/CPP/tests/__init__.py diff --git a/SDK/CPP/public/python/tests/test_load.py b/SDK/CPP/tests/test_load.py similarity index 77% rename from SDK/CPP/public/python/tests/test_load.py rename to SDK/CPP/tests/test_load.py index 5118876f9..df4292cc6 100644 --- a/SDK/CPP/public/python/tests/test_load.py +++ b/SDK/CPP/tests/test_load.py @@ -7,7 +7,8 @@ class LoadLibrary(unittest.TestCase): import voltu def test_import_twice(self): - # verify that System instance is created just once + # verify that System instance is created just once, even if module + # imported multiple times import voltu import voltu self.assertIsNotNone(voltu.System) -- GitLab