From 2c9b1d5b58d42f204a1cd7b49c84f23c0f3b89a9 Mon Sep 17 00:00:00 2001 From: dev Date: Mon, 20 May 2024 17:11:08 -0400 Subject: [PATCH] Update carma-clock to compile into python module using boost python --- .devcontainer/devcontainer.json | 2 +- CMakeLists.txt | 16 ++++++++++++++-- Config.cmake.in | 4 ++++ include/carma_clock.h | 6 ++++++ install_dependencies.sh | 1 + python_wrapper_test.py | 6 ++++++ src/python_wrapper.cpp | 16 ++++++++++++++++ 7 files changed, 48 insertions(+), 3 deletions(-) create mode 100755 python_wrapper_test.py create mode 100644 src/python_wrapper.cpp diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index bd64a37..d55574b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,7 @@ // README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile { "name": "CARMA build image", - "image" : "ghcr.io/usdot-fhwa-stol/carma-builds-x64:latest", + "image" : "ghcr.io/usdot-fhwa-stol/carma-builds-x64:jammy", // "image" : "carma_cmake_builder_x64", // Features to add to the dev container. More info: https://containers.dev/features. diff --git a/CMakeLists.txt b/CMakeLists.txt index 416e773..795a296 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,23 +6,35 @@ set(CMAKE_MODULE_PATH "$ENV{CARMA_OPT_DIR}/cmake") set(CMAKE_CXX_STANDARD 17) -project(carma-clock +project(carma_clock DESCRIPTION "CARMA time library" HOMEPAGE_URL https://github.com/usdot-fhwa-stol/carma-time-lib VERSION 0.0.1 LANGUAGES CXX ) -find_package(Threads) +include(cmake/dependencies.cmake) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") add_library(${PROJECT_NAME} SHARED) +target_include_directories(${PROJECT_NAME} + PUBLIC + $ +) +target_link_libraries( ${PROJECT_NAME} PUBLIC + Boost::python + Python3::Python +) + set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION 1 ) include(CommonSource) include(Testing) +# Test for Python inclusion +FILE(COPY python_wrapper_test.py DESTINATION .) +ADD_TEST(NAME python_wrapper_test COMMAND ${PYTHON_EXECUTABLE} python_wrapper_test.py) include(CodeCoverage) include(InstallingGeneral) include(InstallingConfigs) diff --git a/Config.cmake.in b/Config.cmake.in index 8c9ad12..2896c58 100644 --- a/Config.cmake.in +++ b/Config.cmake.in @@ -2,4 +2,8 @@ include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") +find_dependency(Boost COMPONENTS python REQUIRED ) +find_dependency(Threads) +find_dependency(Python3 COMPONENTS Interpreter Development) + check_required_components(@PROJECT_NAME@) diff --git a/include/carma_clock.h b/include/carma_clock.h index 85f8443..2adc0c8 100644 --- a/include/carma_clock.h +++ b/include/carma_clock.h @@ -21,6 +21,7 @@ #include #include + namespace fwha_stol::lib::time { /** Alias for a timestamp in seconds. */ @@ -48,6 +49,11 @@ class CarmaClock { * @brief Destructor */ ~CarmaClock() = default; + // Public move constructor and move assignment operator + CarmaClock(const CarmaClock&) = delete; // Deleted copy constructor + CarmaClock& operator=(const CarmaClock&) = delete; // Deleted copy assignment operator + CarmaClock(CarmaClock&&) = delete; // Deleted move constructor + CarmaClock& operator=(CarmaClock&&) = delete; // Deleted move assignment operator public: /** diff --git a/install_dependencies.sh b/install_dependencies.sh index 973ea90..d411f39 100755 --- a/install_dependencies.sh +++ b/install_dependencies.sh @@ -1,5 +1,6 @@ #!/bin/bash set -ex +apt install libboost-python1.74-dev # TODO diff --git a/python_wrapper_test.py b/python_wrapper_test.py new file mode 100755 index 0000000..ecc614d --- /dev/null +++ b/python_wrapper_test.py @@ -0,0 +1,6 @@ +#!/usr/bin/python3 + +import libcarma_clockd +print("Creating Clock Object") + +# clock = libcarma_clockd.CarmaClock() diff --git a/src/python_wrapper.cpp b/src/python_wrapper.cpp new file mode 100644 index 0000000..96a7f8c --- /dev/null +++ b/src/python_wrapper.cpp @@ -0,0 +1,16 @@ +#include +#include +#include "carma_clock.h" + +BOOST_PYTHON_MODULE(libcarma_clockd) { + using namespace boost::python; + class_("CarmaClock", init()) + .def("nowInSeconds", &fwha_stol::lib::time::CarmaClock::nowInSeconds) + .def("nowInMilliseconds", &fwha_stol::lib::time::CarmaClock::nowInMilliseconds) + .def("update", &fwha_stol::lib::time::CarmaClock::update,args("current_time")) + .def("is_simulation_mode", &fwha_stol::lib::time::CarmaClock::is_simulation_mode) + .def("wait_for_initialization",&fwha_stol::lib::time::CarmaClock::wait_for_initialization) + .def("sleep_until",&fwha_stol::lib::time::CarmaClock::sleep_until,args("future_time")) + .def("sleep_for",&fwha_stol::lib::time::CarmaClock::sleep_for,args("time_to_sleep")); + init_module_libcarma_clockd(); +} \ No newline at end of file