Skip to content

Commit

Permalink
Statically link if one RMW at compile time
Browse files Browse the repository at this point in the history
Add a scary message if the user tries to change the implementation and it is statically linked

Signed-off-by: Dan Rose <dan@digilabs.io>
  • Loading branch information
rotu committed Sep 13, 2019
1 parent af5891c commit 421b940
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
10 changes: 9 additions & 1 deletion rmw_implementation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ message(STATUS "")
message(STATUS "Default rmw implementation: '${RMW_IMPLEMENTATION}'")
message(STATUS "")

get_available_rmw_implementations(available_rmw_implementations)
list(LENGTH available_rmw_implementations count_available_rmw_implementations)
if(count_available_rmw_implementations EQUAL 1)
set(rmw_implementation_baked_default ON)
else()
set(rmw_implementation_baked_default OFF)
endif()

option(RMW_IMPLEMENTATION_BAKED "\
Use only the RMW implementation specified at build time, \
ignoring runtime value of the 'RMW_IMPLEMENTATION' environment variable \
and the 'RMW_IMPLEMENTATION' CMake option"
OFF)
${rmw_implementation_baked_default})

if(RMW_IMPLEMENTATION_BAKED)
message(STATUS "Dynamic loading disabled; directly referencing '${RMW_IMPLEMENTATION}'")
Expand Down
31 changes: 20 additions & 11 deletions rmw_implementation/rmw_implementation-extras.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@

find_package(rmw_implementation_cmake REQUIRED)

if(RMW_IMPLEMENTATION_BAKED)
if(NOT "${RMW_IMPLEMENTATION}" STREQUAL "")
set(requested_rmw_implementation "${RMW_IMPLEMENTATION}")
set(requested_rmw_implementation_from "CMake")
elseif(NOT "$ENV{RMW_IMPLEMENTATION}" STREQUAL "")
set(requested_rmw_implementation "$ENV{RMW_IMPLEMENTATION}")
set(requested_rmw_implementation_from "environment variable 'RMW_IMPLEMENTATION'")
else()
set(requested_rmw_implementation "@RMW_IMPLEMENTATION@")
set(requested_rmw_implementation_from "the default when @PROJECT_NAME@ was built")
endif()

if(@RMW_IMPLEMENTATION_BAKED@)
message(STATUS "Using RMW implementation '@RMW_IMPLEMENTATION@'")
if(NOT requested_rmw_implementation STREQUAL @RMW_IMPLEMENTATION@)
message(ERROR
"The RMW implementation has been specified as "
"'${requested_rmw_implementation}' via ${requested_rmw_implementation_from}, "
"but rmw_implementation was built without dynamic linking enabled. We will use "
"'@RMW_IMPLEMENTATION@'".
)
endif()
find_package("@RMW_IMPLEMENTATION@" REQUIRED)

# TODO should never need definitions and include dirs?
list(APPEND rmw_implementation_DEFINITIONS ${@RMW_IMPLEMENTATION@_DEFINITIONS})
list(APPEND rmw_implementation_INCLUDE_DIRS ${@RMW_IMPLEMENTATION@_INCLUDE_DIRS})
list(APPEND rmw_implementation_LIBRARIES ${@RMW_IMPLEMENTATION@_LIBRARIES})
else()
if(NOT "${RMW_IMPLEMENTATION}" STREQUAL "")
set(requested_rmw_implementation "${RMW_IMPLEMENTATION}")
set(requested_rmw_implementation_from "CMake")
elseif(NOT "$ENV{RMW_IMPLEMENTATION}" STREQUAL "")
set(requested_rmw_implementation "$ENV{RMW_IMPLEMENTATION}")
set(requested_rmw_implementation_from "environment variable 'RMW_IMPLEMENTATION'")
else()
set(requested_rmw_implementation "@RMW_IMPLEMENTATION@")
set(requested_rmw_implementation_from "the default when @PROJECT_NAME@ was built")
endif()

get_available_rmw_implementations(available_rmw_implementations)

Expand Down

0 comments on commit 421b940

Please sign in to comment.