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

Build examples using native CMake #301

Merged
merged 18 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
153 changes: 153 additions & 0 deletions cmake/GzUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,159 @@ macro(gz_build_tests)

endmacro()

#################################################
# gz_build_examples(
# SOURCE_DIR <source_dir>
# BINARY_DIR <binary_dir>
# [COMPONENTS <project_component_dependencies>]
# [DEPENDENCIES <project_dependencies>]
#
# Requires a CMakeLists.txt file to be in SOURCE_DIR that acts
# Build examples for a Gazebo project.
# 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
#
# COMPONENTS:
# Optional. List of components built by this project that are used in the examples.
# For example "cli" for gz-utils
#
# DEPENDENCIES:
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me see if I understand this right: in DEPENDENCIES you can specify gz- libraries that are not being used by the build system of the project but are required to make the examples to build/run? Is this correct? If so, bellow there is a comment:

we are passing in the resolved dependency directories via -Ddep_DIR=${dep_DIR}

How does this work if a dependency is not in the current buildsystem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have a slight rework to this I can push shortly that drops this gymnastics a bit.

Note that this is only necessary for gazebo packages, as they may be built in the same workspace, so this special forwarding happens.

Currently, examples aren't allowed to depend on Gazebo packages things that the containing package doesn't. E.g. gz-gui examples cannot have a dependency on gz-gazebo, as this will introduce circular dependencies.

If an example depends on a non-Gazebo package, then typical find_package infrastructure should work.

# Optional. Additional Gazebo library dependencies for this example
# This is in addition to things that the project library already depends on.
# For example: gz-utils2 gz-common5

macro(gz_build_examples)
#------------------------------------
# Define the expected arguments
set(options)
set(oneValueArgs SOURCE_DIR BINARY_DIR)
set(multiValueArgs COMPONENTS DEPENDENCIES)

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

set(BUILD_EXAMPLES_CMAKE "")
set(BUILD_EXAMPLES_INCLUDES "")

get_target_property(PROJECT_LIBRARY_DEPS ${PROJECT_LIBRARY_TARGET_NAME} INTERFACE_LINK_LIBRARIES)

# Below here we are generating a series of arguments to the cmake command that is to follow.
# Since we may be building in a colcon workspace or otherwise isolated, we are passing
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
# in the resolved dependency directories via -Ddep_DIR=${dep_DIR}

# Always depends on on gz-cmake
list(APPEND BUILD_EXAMPLES_CMAKE "-Dgz-cmake${GZ_CMAKE_VER}_DIR=${gz-cmake${GZ_CMAKE_VER}_DIR}")

# Iterate through the project library dependencies and add them to the cmake args
foreach(PROJECT_LIBRARY_DEP ${PROJECT_LIBRARY_DEPS})
get_target_property(DEP_NAME ${PROJECT_LIBRARY_DEP} NAME)

string(REPLACE "::" ";" DEP_NAME ${DEP_NAME})
list(GET DEP_NAME 0 DEP_PACKAGE)
list(GET DEP_NAME 0 DEP_COMPONENT)
list(APPEND BUILD_EXAMPLES_CMAKE "-D${DEP_PACKAGE}_DIR=${${DEP_PACKAGE}_DIR}")

# For each dependency, also include the components that were requested
# at the find_package call to reduce verbosity in COMPONENTS arg
if (TARGET ${DEP}::requested)
get_target_property(REQUESTED_COMPONENTS ${DEP}::requested INTERFACE_LINK_LIBRARIES)

foreach(LINKLIB ${REQUESTED_COMPONENTS})
string(REGEX REPLACE "^${DEP}::" "" GZ_COMPONENT ${LINKLIB})

if (GZ_COMPONENT STREQUAL DEP)
continue()
endif()

list(APPEND BUILD_EXAMPLES_CMAKE "-D${GZ_COMPONENT}_DIR=${${GZ_COMPONENT}_DIR}")
endforeach()
endif()
endforeach()

# Iterate through additional dependencies, that is things that the project library
# doesn't depend on, but are needed for the examples.
# While this opens up the possibility of using any gazebo packages as part of the
# examples, care should be exercised to not introduce circular depdendencies.
foreach(DEP ${gz_build_examples_DEPENDENCIES})
list(APPEND BUILD_EXAMPLES_CMAKE "-D${DEP}_DIR=${${DEP}_DIR}")

# For each dependency, also include the components that were requested
# at the find_package call to reduce verbosity in COMPONENTS arg
if (TARGET ${DEP}::requested)
get_target_property(REQUESTED_COMPONENTS ${DEP}::requested INTERFACE_LINK_LIBRARIES)

foreach(LINKLIB ${REQUESTED_COMPONENTS})
string(REGEX REPLACE "^${DEP}::" "" GZ_COMPONENT ${LINKLIB})

if (GZ_COMPONENT STREQUAL DEP)
continue()
endif()

list(APPEND BUILD_EXAMPLES_CMAKE "-D${GZ_COMPONENT}_DIR=${${GZ_COMPONENT}_DIR}")
endforeach()
endif()
endforeach()

# Add root project CMake directory
list(APPEND BUILD_EXAMPLES_CMAKE "-D${PROJECT_NAME}_DIR=${PROJECT_BINARY_DIR}/cmake")

# Add project component CMake directories
foreach (COMPONENT ${gz_build_examples_COMPONENTS})
list(APPEND BUILD_EXAMPLES_CMAKE "-D${PROJECT_NAME}-${COMPONENT}_DIR=${PROJECT_BINARY_DIR}/cmake")
endforeach()

# Get core library includes from target include directories
get_target_property(core_INCLUDES ${PROJECT_LIBRARY_TARGET_NAME} INTERFACE_INCLUDE_DIRECTORIES)
list(APPEND BUILD_EXAMPLES_INCLUDES ${core_INCLUDES} )

# Get component library includes
foreach (COMPONENT ${gz_build_examples_COMPONENTS})
get_target_property(component_INCLUDES ${PROJECT_LIBRARY_TARGET_NAME}-${COMPONENT} INTERFACE_INCLUDE_DIRECTORIES)

message(STATUS "${COMPONENT}_INCLUDES = ${component_INCLUDES}")
list(APPEND BUILD_EXAMPLES_INCLUDES ${component_INCLUDES} )
endforeach()

# Join everything with semicolon to be passed on command line
set(INCLUDES "$<JOIN:${BUILD_EXAMPLES_INCLUDES},;>")

add_test(
NAME EXAMPLES_Configure_TEST
COMMAND ${CMAKE_COMMAND} -G${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-S ${gz_build_examples_SOURCE_DIR}
-B ${gz_build_examples_BINARY_DIR}
${BUILD_EXAMPLES_CMAKE}
"-D${PROJECT_LIBRARY_TARGET_NAME}_INCLUDE_DIRS_OVERRIDE=${INCLUDES}"
)

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