Skip to content

Commit

Permalink
Add support for building tests within an ament context (#200)
Browse files Browse the repository at this point in the history
* Refs #20328: Add package.xml and colcon.pkg compatible with ROS 2 CI

Signed-off-by: EduPonz <[email protected]>

* Refs #20328: Add macro to find gtest both in and outside ament context

Signed-off-by: EduPonz <[email protected]>

* Refs #20328: Use new macro to find gtest

Signed-off-by: EduPonz <[email protected]>

* Refs #20328: Apply suggestions

Signed-off-by: EduPonz <[email protected]>

* Refs #20328: Fix typo

Signed-off-by: EduPonz <[email protected]>

---------

Signed-off-by: EduPonz <[email protected]>
  • Loading branch information
EduPonz authored Feb 22, 2024
1 parent fe726a8 commit e1cef73
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
40 changes: 40 additions & 0 deletions cmake/common/find_or_add_gtest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

macro(find_or_add_gtest)
# Check if building on an ament context, i.e. ROS 2
# Thanks to https://github.com/facontidavide/PlotJuggler/blob/main/CMakeLists.txt#L66
find_package(ament_cmake QUIET)

# This is a ROS 2 build
if(ament_cmake_FOUND)
# Find all GTest vendor required packages
find_package(ament_cmake REQUIRED)
find_package(gtest_vendor REQUIRED)
find_package(ament_cmake_gtest REQUIRED)

# Find GTest
ament_find_gtest()

# Add aliases for GTest libraries so we can use them as targets independently of the context
add_library(GTest::gtest ALIAS gtest)
add_library(GTest::gtest_main ALIAS gtest_main)
target_link_libraries(gtest_main gtest)

# This is a non-ROS 2 build
else()
# Find GTest normally
find_package(GTest CONFIG REQUIRED)
endif()
endmacro()
13 changes: 8 additions & 5 deletions colcon.pkg
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "fastcdr",
"type": "cmake",
"dependencies" : ["googletest-distribution"]
}
name: fastcdr
type: cmake
dependencies:
# Needed for test compilation in ROS 2 CI
- ament_cmake_gtest
- ament_cmake
# Needed for test compilation in eProsima CI
- googletest-distribution
26 changes: 26 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>fastcdr</name>
<version>2.1.3</version>
<description>
*eProsima Fast CDR* is a C++ serialization library implementing the Common Data Representation (CDR) mechanism defined by the Object Management Group (OMG) consortium. CDR is the serialization mechanism used in DDS for the DDS Interoperability Wire Protocol (DDSI-RTPS).
</description>
<maintainer email="[email protected]">Miguel Company</maintainer>
<maintainer email="[email protected]">Eduardo Ponz</maintainer>
<license file="LICENSE">Apache 2.0</license>

<url type="website">https://www.eprosima.com/</url>
<url type="bugtracker">https://github.com/eProsima/Fast-CDR/issues</url>
<url type="repository">https://github.com/eProsima/Fast-CDR</url>

<buildtool_depend>cmake</buildtool_depend>
<doc_depend>doxygen</doc_depend>

<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake</test_depend>

<export>
<build_type>cmake</build_type>
</export>
</package>
10 changes: 9 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

find_package(GTest CONFIG REQUIRED)
# Require C++ 14 for testing (since ROS 2 requires it anyways)
set(FORCE_CXX "14")
check_stdcxx(${FORCE_CXX})

# Find GTest
include(${PROJECT_SOURCE_DIR}/cmake/common/find_or_add_gtest.cmake)
find_or_add_gtest()

# Include functions to find and add tests dinamically
include(${PROJECT_SOURCE_DIR}/cmake/testing/GoogleTest.cmake)

add_subdirectory(cdr)
Expand Down

0 comments on commit e1cef73

Please sign in to comment.