Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix osx and win32 builds #83

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions bondcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ find_package(rclcpp_lifecycle REQUIRED)
find_package(smclib REQUIRED)
find_package(Threads REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(UUID REQUIRED uuid)
# we have to find the absolute path to uuid as target_link_directories is not available before cmake 3.13
find_library(uuid_ABS_PATH ${UUID_LIBRARIES} PATHS ${UUID_LIBRARY_DIRS})
if(UNIX AND NOT APPLE)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens on macOS? If we are never finding the uuid library, how does it build/link against it?

Copy link

@traversaro traversaro Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On macOS libuuid header and symbols are provided by the OS SDK/libc, so there is no need to provide additonal header directories or library to link, see gazebosim/gz-cmake#128 and gazebosim/gz-cmake#127 for a similar change on Ignition libraries or https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/uuid_generate.3.html for the macOS docs.

find_package(PkgConfig REQUIRED)
pkg_check_modules(UUID REQUIRED uuid)
# we have to find the absolute path to uuid as target_link_directories is not available before cmake 3.13
find_library(uuid_ABS_PATH ${UUID_LIBRARIES} PATHS ${UUID_LIBRARY_DIRS})
elseif(WIN32)
set(uuid_ABS_PATH Rpcrt4.lib)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really make it work on Windows? Where does Rpcrt4.lib come from?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endif()

add_library(${PROJECT_NAME}
SHARED
Expand All @@ -29,6 +33,10 @@ target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
${UUID_INCLUDE_DIRS})
if(WIN32)
target_compile_definitions(${PROJECT_NAME}
PRIVATE "BONDCPP_BUILDING_LIBRARY" "NOMINMAX")
endif()
ament_target_dependencies(${PROJECT_NAME}
bond
rclcpp
Expand Down
36 changes: 36 additions & 0 deletions bondcpp/include/bondcpp/bond.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "bond/msg/status.hpp"

#include "bondcpp/BondSM_sm.hpp"
#include "bondcpp/visibility_control.hpp"

#include <rclcpp/rclcpp.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>
Expand Down Expand Up @@ -71,6 +72,7 @@ class Bond
* \param on_broken callback that will be called when the bond is broken.
* \param on_formed callback that will be called when the bond is formed.
*/
BONDCPP_PUBLIC
Bond(
const std::string & topic, const std::string & id,
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
Expand All @@ -90,6 +92,7 @@ class Bond
* \param on_broken callback that will be called when the bond is broken.
* \param on_formed callback that will be called when the bond is formed.
*/
BONDCPP_PUBLIC
Bond(
const std::string & topic, const std::string & id,
rclcpp_lifecycle::LifecycleNode::SharedPtr nh,
Expand All @@ -105,6 +108,7 @@ class Bond
* \param on_broken callback that will be called when the bond is broken.
* \param on_formed callback that will be called when the bond is formed.
*/
BONDCPP_PUBLIC
Bond(
const std::string & topic, const std::string & id,
rclcpp::Node::SharedPtr nh,
Expand All @@ -113,45 +117,70 @@ class Bond

/** \brief Destructs the object, breaking the bond if it is still formed.
*/
BONDCPP_PUBLIC
~Bond();

BONDCPP_PUBLIC
void setupConnections();

BONDCPP_PUBLIC
double getConnectTimeout() const {return connect_timeout_.seconds();}
BONDCPP_PUBLIC
void setConnectTimeout(double dur);
BONDCPP_PUBLIC
void connectTimerReset();
BONDCPP_PUBLIC
void connectTimerCancel();

BONDCPP_PUBLIC
double getDisconnectTimeout() const {return disconnect_timeout_.seconds();}
BONDCPP_PUBLIC
void setDisconnectTimeout(double dur);
BONDCPP_PUBLIC
void disconnectTimerReset();
BONDCPP_PUBLIC
void disconnectTimerCancel();

BONDCPP_PUBLIC
double getHeartbeatTimeout() const {return heartbeat_timeout_.seconds();}
BONDCPP_PUBLIC
void setHeartbeatTimeout(double dur);
BONDCPP_PUBLIC
void heartbeatTimerReset();
BONDCPP_PUBLIC
void heartbeatTimerCancel();

BONDCPP_PUBLIC
double getHeartbeatPeriod() const {return heartbeat_period_.seconds();}
BONDCPP_PUBLIC
void setHeartbeatPeriod(double dur);
BONDCPP_PUBLIC
void publishingTimerReset();
BONDCPP_PUBLIC
void publishingTimerCancel();

BONDCPP_PUBLIC
double getDeadPublishPeriod() const {return dead_publish_period_.seconds();}
BONDCPP_PUBLIC
void setDeadPublishPeriod(double dur);
BONDCPP_PUBLIC
void deadpublishingTimerReset();
BONDCPP_PUBLIC
void deadpublishingTimerCancel();

/** \brief Starts the bond and connects to the sister process.
*/
BONDCPP_PUBLIC
void start();

/** \brief Sets the formed callback.
*/
BONDCPP_PUBLIC
void setFormedCallback(EventCallback on_formed);

/** \brief Sets the broken callback
*/
BONDCPP_PUBLIC
void setBrokenCallback(EventCallback on_broken);

/** \brief Blocks until the bond is formed for at most 'duration'.
Expand All @@ -160,6 +189,7 @@ class Bond
* \param timeout Maximum duration to wait. If -1 then this call will not timeout.
* \return true iff the bond has been formed.
*/
BONDCPP_PUBLIC
bool waitUntilFormed(rclcpp::Duration timeout = rclcpp::Duration(std::chrono::seconds(-1)));

/** \brief Blocks until the bond is broken for at most 'duration'.
Expand All @@ -168,18 +198,24 @@ class Bond
* \param timeout Maximum duration to wait. If -1 then this call will not timeout.
* \return true iff the bond has been broken, even if it has never been formed.
*/
BONDCPP_PUBLIC
bool waitUntilBroken(rclcpp::Duration timeout = rclcpp::Duration(std::chrono::seconds(-1)));

/** \brief Indicates if the bond is broken.
*/
BONDCPP_PUBLIC
bool isBroken();

/** \brief Breaks the bond, notifying the other process.
*/
BONDCPP_PUBLIC
void breakBond();

BONDCPP_PUBLIC
std::string getTopic() const {return topic_;}
BONDCPP_PUBLIC
std::string getId() const {return id_;}
BONDCPP_PUBLIC
std::string getInstanceId() const {return instance_id_;}

private:
Expand Down
87 changes: 87 additions & 0 deletions bondcpp/include/bondcpp/visibility_control.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2022, Open Source Robotics Foundation, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef BONDCPP__VISIBILITY_CONTROL_HPP_
#define BONDCPP__VISIBILITY_CONTROL_HPP_

/*! \file visibility_control.hpp
* \brief Macros for controlling visibilty of exported iterfaces.
*
* This logic was borrowed (then namespaced) from the examples on the gcc wiki:
* https://gcc.gnu.org/wiki/Visibility
*/
/**
* \def BONDCPP_EXPORT
* \brief Exposes the function with its decorated name in the compiled library object.
*/
/**
* \def BONDCPP_IMPORT
* \brief On Windows declares a function will be imported from a dll, otherwise it is empty
*/
/**
* \def BONDCPP_PUBLIC
* \brief Declares symbols and functions will be visible for export.
*/
/**
* \def BONDCPP_PUBLIC_TYPE
* \brief On Windows, this is a replica of BONDCPP_PUBLIC, otherwise it is empty.
*/
/**
* \def BONDCPP_LOCAL
* \brief Declares symbols cannot be exported from the dll.
*/

#if defined _WIN32 || defined __CYGWIN__
#ifdef __GNUC__
#define BONDCPP_EXPORT __attribute__ ((dllexport))
#define BONDCPP_IMPORT __attribute__ ((dllimport))
#else
#define BONDCPP_EXPORT __declspec(dllexport)
#define BONDCPP_IMPORT __declspec(dllimport)
#endif
#ifdef BONDCPP_BUILDING_LIBRARY
#define BONDCPP_PUBLIC BONDCPP_EXPORT
#else
#define BONDCPP_PUBLIC BONDCPP_IMPORT
#endif
#define BONDCPP_PUBLIC_TYPE BONDCPP_PUBLIC
#define BONDCPP_LOCAL
#else
#define BONDCPP_EXPORT __attribute__ ((visibility("default")))
#define BONDCPP_IMPORT
#if __GNUC__ >= 4
#define BONDCPP_PUBLIC __attribute__ ((visibility("default")))
#define BONDCPP_LOCAL __attribute__ ((visibility("hidden")))
#else
#define BONDCPP_PUBLIC
#define BONDCPP_LOCAL
#endif
#define BONDCPP_PUBLIC_TYPE
#endif

#endif // BONDCPP__VISIBILITY_CONTROL_HPP_
4 changes: 2 additions & 2 deletions bondcpp/src/bond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,8 @@ void Bond::publishStatus(bool active)
msg.id = id_;
msg.instance_id = instance_id_;
msg.active = active;
msg.heartbeat_timeout = heartbeat_timeout_.seconds();
msg.heartbeat_period = heartbeat_period_.seconds();
msg.heartbeat_timeout = static_cast<float>(heartbeat_timeout_.seconds());
msg.heartbeat_period = static_cast<float>(heartbeat_period_.seconds());
pub_->publish(msg);
}

Expand Down
10 changes: 7 additions & 3 deletions smclib/include/smclib/statemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ inline char * copyString(const char * s)
{
char * retval = NULL;
if (s != NULL) {
retval = new char[MAX_NAME_LEN + 1];
retval[MAX_NAME_LEN] = '\0';
(void) std::strncpy(retval, s, MAX_NAME_LEN);
size_t copy_len = strlen(s);
if (copy_len > MAX_NAME_LEN) {
copy_len = MAX_NAME_LEN;
}
retval = new char[copy_len + 1];
memcpy(retval, s, copy_len);
retval[copy_len] = '\0';
}

return retval;
Expand Down
7 changes: 6 additions & 1 deletion test_bond/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
add_definitions(-DNOMINMAX)

find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
Expand All @@ -30,10 +31,14 @@ if(BUILD_TESTING)
find_package(rosidl_cmake REQUIRED)
find_package(Threads REQUIRED)

if(UNIX AND NOT APPLE)
find_package(PkgConfig REQUIRED)
pkg_search_module(UUID REQUIRED uuid)
pkg_check_modules(UUID REQUIRED uuid)
# we have to find the absolute path to uuid as target_link_directories is not available before cmake 3.13
find_library(uuid_ABS_PATH ${UUID_LIBRARIES} PATHS ${UUID_LIBRARY_DIRS})
elseif(WIN32)
set(uuid_ABS_PATH Rpcrt4.lib)
endif()

ament_lint_auto_find_test_dependencies()

Expand Down