Skip to content

Commit

Permalink
Build examples using native CMake (#301)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <michael@openrobotics.org>
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
  • Loading branch information
mjcarroll and ahcorde authored May 19, 2023
1 parent 88e7dc5 commit d0fb580
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
64 changes: 64 additions & 0 deletions cmake/GzUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,70 @@ macro(_gz_add_library_or_component)
endmacro()


#################################################
# gz_build_examples(
# SOURCE_DIR <source_dir>
# BINARY_DIR <binary_dir>
#
# Build examples for a Gazebo project.
# Requires a CMakeLists.txt file to be in SOURCE_DIR that acts
# as a top level project.
#
# This generates two test targets
# * EXAMPLES_Configure_TEST - Equivalent of calling "cmake .." on
# the examples directory
#
# * EXAMPLES_Build_TEST - Equivalent of calling "make" on the
# examples directory
#
# These tests are run during "make test" or can be run specifically
# via "ctest -R EXAMPLES_ -V"
#
# Arguments are as follows:
#
# SOURCE_DIR: Required. Path to the examples folder.
# For example ${CMAKE_CURRENT_SOURCE_DIR}/examples
#
# BINARY_DIR: Required. Path to the output binary folder
# For example ${CMAKE_CURRENT_BINARY_DIR}/examples
#
macro(gz_build_examples)
#------------------------------------
# Define the expected arguments
set(options)
set(oneValueArgs SOURCE_DIR BINARY_DIR)

#------------------------------------
# Parse the arguments
_gz_cmake_parse_arguments(gz_build_examples "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

set(gz_build_examples_CMAKE_PREFIX_PATH $ENV{CMAKE_PREFIX_PATH})
string(REPLACE ":" ";" gz_build_examples_CMAKE_PREFIX_PATH ${gz_build_examples_CMAKE_PREFIX_PATH})
list(APPEND gz_build_examples_CMAKE_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})

add_test(
NAME EXAMPLES_Configure_TEST
COMMAND ${CMAKE_COMMAND} -G${CMAKE_GENERATOR}
--no-warn-unused-cli
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
"-DCMAKE_PREFIX_PATH=${gz_build_examples_CMAKE_PREFIX_PATH}"
-S ${gz_build_examples_SOURCE_DIR}
-B ${gz_build_examples_BINARY_DIR}
)

add_test(
NAME EXAMPLES_Build_TEST
COMMAND ${CMAKE_COMMAND} --build ${gz_build_examples_BINARY_DIR}
--config $<CONFIG>
)
set_tests_properties(EXAMPLES_Build_TEST
PROPERTIES DEPENDS "EXAMPLES_Configure_TEST")
endmacro()

#################################################
# _gz_cmake_parse_arguments(<prefix> <options> <oneValueArgs> <multiValueArgs> [ARGN])
#
Expand Down
7 changes: 6 additions & 1 deletion cmake/gz-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ endif()
# contain imported targets, so @PKG_NAME@_INCLUDE_DIRS is never needed.
set(@PKG_NAME@_CORE_LIBRARY @simple_import_name@)
set(@PKG_NAME@_LIBRARIES @gz_namespace@requested)
set_and_check(@PKG_NAME@_INCLUDE_DIRS "@PACKAGE_GZ_INCLUDE_INSTALL_DIR_FULL@")

if (NOT @PKG_NAME@_INCLUDE_DIRS_OVERRIDE)
set_and_check(@PKG_NAME@_INCLUDE_DIRS "@PACKAGE_GZ_INCLUDE_INSTALL_DIR_FULL@")
else()
set_and_check(@PKG_NAME@_INCLUDE_DIRS ${@PKG_NAME@_INCLUDE_DIRS_OVERRIDE})
endif()

# Backwards compatibility variables
set(@LEGACY_PROJECT_PREFIX@_LIBRARIES ${@PKG_NAME@_LIBRARIES})
Expand Down

0 comments on commit d0fb580

Please sign in to comment.