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

Require calling find_package on the underlying package #2

Merged
merged 1 commit into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion CMakeLists.txt
Expand Up @@ -66,4 +66,39 @@ if(NOT ${${LIB_NAME_FULL}_FOUND})
ament_environment_hooks("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.dsv.in")
endif()

ament_package(CONFIG_EXTRAS_POST "gz_math_vendor-extras.cmake.in")
# The goal is to support versionless package names once the user has found the
# vendor package. Example usage:
#
# find_package(gz_sim_vendor)
# find_package(gz-sim VERSION 8.2.0) # Note gz-sim not gz-sim8
#
# To accomplish this, we create a `{LIB_NAME}-config.cmake` file that does
# find_package on the underlying package and sets up CMake targets with the
# same name as the original targets sans the version number in the target.
#
# However, since the vendor package is built with `GLOBAL_HOOK`, we can't
# install the `-config.cmake` file we're creating here to where the versioned
# `-config.cmake` from the underlying package is located. If we did, users can
# `find_package` the versionless package without first finding the vendor
# package. Thus, we install the `-config.cmake` file to a nonstandard
# location: `opt/${PROJECT_NAME}/extra_cmake/` and provide a
# `{vendor_name}-extras.cmake` file that adds that path to `CMAKE_PREFIX_PATH`.
ament_package(
CONFIG_EXTRAS_POST "gz_math_vendor-extras.cmake.in"
)

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

configure_package_config_file(${LIB_NAME}-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${LIB_NAME})

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}-config-version.cmake
VERSION ${LIB_VER}
COMPATIBILITY SameMajorVersion)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${LIB_NAME}-config-version.cmake
DESTINATION "opt/${PROJECT_NAME}/extra_cmake/lib/cmake/${LIB_NAME}")
31 changes: 31 additions & 0 deletions gz-math-config.cmake.in
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)

find_package(@LIB_NAME@@LIB_VER_MAJOR@ ${@LIB_NAME@_FIND_VERSION} REQUIRED COMPONENTS ${@LIB_NAME@_FIND_COMPONENTS})

# Set up the core library and add it to the list of all components
add_library(@LIB_NAME_COMP_PREFIX@::core ALIAS @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@)
set(ALL_COMPONENTS @LIB_NAME_COMP_PREFIX@::core)

# Retrieve the list of components
get_target_property(components @LIB_NAME@@LIB_VER_MAJOR@::requested INTERFACE_LINK_LIBRARIES)

foreach(component ${components})
# Skip the core library
if(${component} STREQUAL @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@)
continue()
endif()

# Skip the -all target
if(${component} STREQUAL @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@-all)
continue()
endif()

# Change "gz-libN::gz-libN-component" to "component"
string(REGEX REPLACE "@LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@-" "" component_name ${component})
add_library(@LIB_NAME_COMP_PREFIX@::${component_name} ALIAS ${component})
list(APPEND ALL_COMPONENTS @LIB_NAME_COMP_PREFIX@::${component_name})
endforeach()

# Add a root gz::lib alias
add_library(@LIB_NAME_COMP_PREFIX@ INTERFACE IMPORTED)
target_link_libraries(@LIB_NAME_COMP_PREFIX@ INTERFACE @LIB_NAME_COMP_PREFIX@::core ${ALL_COMPONENTS})
30 changes: 1 addition & 29 deletions gz_math_vendor-extras.cmake.in
@@ -1,29 +1 @@
find_package(@LIB_NAME@@LIB_VER_MAJOR@ REQUIRED COMPONENTS all)

# Set up the core library and add it to the list of all components
add_library(@LIB_NAME_COMP_PREFIX@::core ALIAS @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@)
set(ALL_COMPONENTS @LIB_NAME_COMP_PREFIX@::core)

# Retrieve the list of components
get_target_property(components @LIB_NAME@@LIB_VER_MAJOR@::requested INTERFACE_LINK_LIBRARIES)

foreach(component ${components})
# Skip the core library
if(${component} STREQUAL @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@)
continue()
endif()

# Skip the -all target
if(${component} STREQUAL @LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@-all)
continue()
endif()

# Change "gz-libN::gz-libN-component" to "component"
string(REGEX REPLACE "@LIB_NAME@@LIB_VER_MAJOR@::@LIB_NAME@@LIB_VER_MAJOR@-" "" component_name ${component})
add_library(@LIB_NAME_COMP_PREFIX@::${component_name} ALIAS ${component})
list(APPEND ALL_COMPONENTS @LIB_NAME_COMP_PREFIX@::${component_name})
endforeach()

# Add a root gz::lib alias
add_library(@LIB_NAME_COMP_PREFIX@ INTERFACE IMPORTED)
target_link_libraries(@LIB_NAME_COMP_PREFIX@ INTERFACE @LIB_NAME_COMP_PREFIX@::core ${ALL_COMPONENTS})
list(PREPEND CMAKE_PREFIX_PATH "@CMAKE_INSTALL_PREFIX@/opt/@PROJECT_NAME@/extra_cmake")
15 changes: 13 additions & 2 deletions package.xml
Expand Up @@ -3,8 +3,19 @@
<!-- This file is automatically generated. Do not modify! -->
<package format="3">
<name>gz_math_vendor</name>
<version>7.4.0</version>
<description>Gazebo Math : Math classes and functions for robot applications</description>
<!--
Note on package versioning:
This version only represents the version of the vendor package, not the
version of the underlying/vendored package (See <description> for that).
To avoid any confusion with the version of the vendor package, the major
version here will always be 0.
-->
<version>0.0.1</version>
<description>
Vendor package for: gz-math7 7.4.0

Gazebo Math : Math classes and functions for robot applications
</description>
<maintainer email="addisuzt@intrinsic.ai">Addisu Z. Taddese</maintainer>
<license>Apache License 2.0</license>
<url type="website">https://github.com/gazebosim/gz-math</url>
Expand Down