Skip to content

Commit ad909bf

Browse files
committed
feat(paths): Improve detection of ROS package paths
Nix installs all ros packages in their own separate install prefix. This breaks some default assumption we had, in particular in paths fallback when package:// cannot be resolved. We use to assume that we could fallback to a path relative to mc_env_description, this is no longer possible. In addition we explicitely check that manually provided fallback paths actually exist. Instead we make mc_env_description, mc_int_obj_description and jvrc_description paths detected at compile-time available through mc_rtc/config.h. This further simplifies cmake/test logic as a bonus In addition, fix an issue in find_description_package that would cause duplicated share/jvrc1_description/share/jvrc1_description. It seems to be an odd edge-case of cmake's set(...) function with variable-of-variables
1 parent 286e726 commit ad909bf

File tree

7 files changed

+51
-50
lines changed

7 files changed

+51
-50
lines changed

CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
3939
project(${PROJECT_NAME} C CXX)
4040
set(MC_RTC_SOURCE_DIR "${PROJECT_SOURCE_DIR}")
4141

42-
set_default_cmake_build_type(RelWithDebInfo)
43-
4442
if(NOT DEFINED MC_RTC_INSTALL_PREFIX)
4543
set(MC_RTC_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
4644
endif()
@@ -51,6 +49,19 @@ else()
5149
set(MC_RTC_LOADER_DEBUG_SUFFIX "")
5250
endif()
5351

52+
set_default_cmake_build_type(RelWithDebInfo)
53+
54+
# Check for Nix build environment
55+
if(DEFINED ENV{NIX_BUILD_CORES}
56+
OR DEFINED ENV{NIX_STORE}
57+
OR DEFINED ENV{IN_NIX_SHELL}
58+
)
59+
set(MC_RTC_BUILD_IN_NIX TRUE)
60+
message(STATUS "Detected Nix build environment")
61+
else()
62+
set(MC_RTC_BUILD_IN_NIX FALSE)
63+
endif()
64+
5465
option(GENERATE_COVERAGE "Generate coverage data" FALSE)
5566
if("${CMAKE_CXX_FLAGS}" MATCHES "--coverage")
5667
set(GENERATE_COVERAGE
@@ -225,17 +236,18 @@ macro(find_description_package PACKAGE)
225236
"Your ${PACKAGE} does not define where to find the data, please update."
226237
)
227238
else()
228-
set(${PACKAGE_PATH_VAR} "${${PACKAGE}_SOURCE_PREFIX}")
239+
set("${PACKAGE_PATH_VAR}" "${${PACKAGE}_SOURCE_PREFIX}")
229240
endif()
230241
else()
231-
set(${PACKAGE_PATH_VAR} "${${PACKAGE}_INSTALL_PREFIX}")
242+
set("${PACKAGE_PATH_VAR}" "${${PACKAGE}_INSTALL_PREFIX}")
232243
endif()
233244
# Cleanup the path provided by CMake
234-
get_filename_component(${PACKAGE_PATH_VAR} "${${PACKAGE_PATH_VAR}}" REALPATH)
245+
get_filename_component("${PACKAGE_PATH_VAR}" "${${PACKAGE_PATH_VAR}}" REALPATH)
235246
endif()
236247
message("-- Found ${PACKAGE}: ${${PACKAGE_PATH_VAR}}")
237248
endmacro()
238249

250+
find_description_package(jvrc_description)
239251
find_description_package(mc_env_description)
240252
find_description_package(mc_int_obj_description)
241253

@@ -293,7 +305,7 @@ else()
293305
endif()
294306

295307
configure_file(
296-
${PROJECT_SOURCE_DIR}/include/mc_rtc/config.h.cmake.in
308+
${PROJECT_SOURCE_DIR}/include/mc_rtc/config.in.h
297309
${CMAKE_CURRENT_BINARY_DIR}/include/mc_rtc/config.h
298310
)
299311

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace mc_rtc
1010
{
1111

12+
constexpr auto JVRC_DESCRIPTION_PATH = "${JVRC_DESCRIPTION_PATH}";
1213
constexpr auto MC_ENV_DESCRIPTION_PATH = "${MC_ENV_DESCRIPTION_PATH}";
14+
constexpr auto MC_INT_OBJ_DESCRIPTION_PATH = "${MC_INT_OBJ_DESCRIPTION_PATH}";
1315
constexpr auto INSTALL_PREFIX = "${MC_RTC_INSTALL_PREFIX}/";
1416
constexpr auto MC_ROBOTS_INSTALL_PREFIX = "${MC_ROBOTS_RUNTIME_DESTINATION_PREFIX}/";
1517
constexpr auto MC_OBSERVERS_INSTALL_PREFIX = "${MC_OBSERVERS_RUNTIME_DESTINATION_PREFIX}/";
@@ -19,4 +21,5 @@ constexpr auto DATA_PATH = "${CMAKE_INSTALL_PREFIX}/share/mc_rtc/";
1921
constexpr auto CONF_PATH = "${CMAKE_INSTALL_PREFIX}/etc/mc_rtc.yaml";
2022
constexpr bool MC_RTC_SUPPORT_ROS = ${MC_RTC_HAS_ROS_SUPPORT};
2123
constexpr auto JSON_SCHEMA_PATH = "${MC_RTC_JSON_SCHEMA_PATH}/";
22-
}
24+
constexpr bool MC_RTC_BUILD_IN_NIX = "${MC_RTC_BUILD_IN_NIX}";
25+
} // namespace mc_rtc

src/mc_robots/CMakeLists.txt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,9 @@ if(EXISTS "${MC_ENV_DESCRIPTION_PATH}")
7777
)
7878
endif()
7979

80-
find_description_package(jvrc_description)
8180
add_robot(jvrc1)
8281
if(TARGET jvrc1)
83-
set_target_properties(
84-
jvrc1
85-
PROPERTIES
86-
COMPILE_FLAGS
87-
"-DMC_ROBOTS_EXPORTS -DJVRC_DESCRIPTION_PATH=\"${JVRC_DESCRIPTION_PATH}\""
88-
)
82+
set_target_properties(jvrc1 PROPERTIES COMPILE_FLAGS "-DMC_ROBOTS_EXPORTS")
8983
else()
90-
set_target_properties(
91-
mc_rbdyn
92-
PROPERTIES
93-
COMPILE_FLAGS
94-
"-DMC_ROBOTS_EXPORTS -DJVRC_DESCRIPTION_PATH=\"${JVRC_DESCRIPTION_PATH}\""
95-
)
84+
set_target_properties(mc_rbdyn PROPERTIES COMPILE_FLAGS "-DMC_ROBOTS_EXPORTS")
9685
endif()

src/mc_robots/jvrc1.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
* Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
33
*/
44

5-
#ifndef JVRC_DESCRIPTION_PATH
6-
# error "JVRC_DESCRIPTION_PATH must be defined to build this RobotModule"
7-
#endif
8-
9-
#define JVRC_VAL(x) #x
10-
#define JVRC_VAL_VAL(x) JVRC_VAL(x)
11-
125
#include "jvrc1.h"
136

147
#include <mc_rbdyn/Device.h>
158
#include <mc_rbdyn/RobotModuleMacros.h>
169

10+
#include <mc_rtc/config.h>
1711
#include <mc_rtc/logging.h>
1812

1913
#include <RBDyn/parsers/urdf.h>
@@ -33,8 +27,7 @@ mc_rbdyn::DevicePtr JVRC1DummySpeaker::clone() const
3327
return mc_rbdyn::DevicePtr(dummy);
3428
}
3529

36-
JVRC1RobotModule::JVRC1RobotModule(bool fixed, bool filter_mimics)
37-
: RobotModule(std::string(JVRC_VAL_VAL(JVRC_DESCRIPTION_PATH)), "jvrc1")
30+
JVRC1RobotModule::JVRC1RobotModule(bool fixed, bool filter_mimics) : RobotModule(mc_rtc::JVRC_DESCRIPTION_PATH, "jvrc1")
3831
{
3932
_canonicalParameters = {"JVRC1"};
4033

src/mc_rtc/path.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <mc_rtc/config.h>
2+
#include <mc_rtc/logging.h>
23
#include <mc_rtc/path.h>
34
#include <fmt/format.h>
45

@@ -69,15 +70,29 @@ fs::path convertURI(const std::string & uri, std::string_view default_dir)
6970
}
7071
# endif
7172
// Fallback for non-ROS builds or when ROS package is not found
72-
if(pkg == "jvrc_description") { pkg = (MC_ENV_DESCRIPTION_PATH / ".." / "jvrc_description").string(); }
73-
else if(pkg == "mc_env_description") { pkg = MC_ENV_DESCRIPTION_PATH.string(); }
74-
else if(pkg == "mc_int_obj_description")
75-
{
76-
pkg = (MC_ENV_DESCRIPTION_PATH / ".." / "mc_int_obj_description").string();
77-
}
73+
if(pkg == "jvrc_description") { pkg = mc_rtc::JVRC_DESCRIPTION_PATH; }
74+
else if(pkg == "mc_env_description") { pkg = mc_rtc::MC_ENV_DESCRIPTION_PATH; }
75+
else if(pkg == "mc_int_obj_description") { pkg = mc_rtc::MC_INT_OBJ_DESCRIPTION_PATH; }
7876
else
79-
{
80-
pkg = default_dir;
77+
{ // could not resolve path using ament index, check for default_dir
78+
if(default_dir.empty())
79+
{
80+
mc_rtc::log::error_and_throw("[mc_rtc::convertURI] Could not resolve path to ROS package path package '{}' in "
81+
"URI '{}', and no default_dir was provided",
82+
pkg, uri);
83+
return uri;
84+
}
85+
else if(!fs::exists(default_dir) || !fs::is_directory(default_dir))
86+
{
87+
mc_rtc::log::error_and_throw("[mc_rtc::convertURI] Could not resolve path to ROS package path package '{}' in "
88+
"URI '{}', and default_dir '{}' does not exist or is not a directory",
89+
pkg, uri, default_dir);
90+
return uri;
91+
}
92+
else
93+
{ // default_dir exists
94+
pkg = default_dir;
95+
}
8196
}
8297
#else
8398
pkg = "/assets/" + pkg;

tests/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ mc_rtc_test(testLogger mc_rbdyn)
102102
mc_rtc_test(testLogUtils mc_rbdyn)
103103
mc_rtc_test(testRobotModule mc_rbdyn)
104104
mc_rtc_test(testRobotModule_visual mc_rbdyn)
105-
find_description_package(jvrc_description)
106-
set_target_properties(
107-
testRobotModule PROPERTIES COMPILE_FLAGS
108-
"-DJVRC_DESCRIPTION_PATH=\"${JVRC_DESCRIPTION_PATH}\""
109-
)
110105
mc_rtc_test(testCanonicalRobot mc_rbdyn)
111106
mc_rtc_test(testSolverBackend mc_tasks)
112107

tests/testRobotModule.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55
#include "utils.h"
66

77
#include <mc_rbdyn/Robots.h>
8+
#include <mc_rtc/config.h>
89

910
#include <boost/filesystem.hpp>
1011
namespace bfs = boost::filesystem;
1112

1213
#include <boost/test/unit_test.hpp>
1314

14-
#ifndef JVRC_DESCRIPTION_PATH
15-
# error "JVRC_DESCRIPTION_PATH must be defined to build this RobotModule"
16-
#endif
17-
18-
#define JVRC_VAL(x) #x
19-
#define JVRC_VAL_VAL(x) JVRC_VAL(x)
20-
2115
static std::string JVRC1_DATA = fmt::format(R"(
2216
path: "{}"
2317
name: jvrc1
@@ -77,7 +71,7 @@ default_attitude: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8275]
7771
X_p_f:
7872
translation: [0.1, 0, 0]
7973
)",
80-
JVRC_VAL_VAL(JVRC_DESCRIPTION_PATH));
74+
mc_rtc::JVRC_DESCRIPTION_PATH);
8175

8276
BOOST_AUTO_TEST_CASE(TestRobotModule)
8377
{

0 commit comments

Comments
 (0)