RobotLocomotion / drake-external-examples

Examples of how to use Drake in your own project.
https://drake.mit.edu/
MIT No Attribution
100 stars 49 forks source link

ament cmake transitive dependency issue #292

Open weithegreat opened 2 months ago

weithegreat commented 2 months ago

Following the drake_ament_cmake_installed, the package I have base_lib depends on drake builds without problem, however another package that depends on base_lib throws error saying it cannot find drake::drake

CMake Error at /code/src/install/base_lib/share/base_lib/cmake/export_base_libExport.cmake:61 (set_target_properties):
  The link interface of target "base_lib::base_lib" contains:

    drake::drake

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

Call Stack (most recent call first):
  /code/src/install/base_lib/share/base_lib/cmake/ament_cmake_export_targets-extras.cmake:9 (include)
  /code/src/install/base_lib/share/base_lib/cmake/base_libConfig.cmake:41 (include)
  CMakeLists.txt:19 (find_package)

My CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(base_lib)
find_package(rclcpp_components REQUIRED)

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 20)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
# list(APPEND CMAKE_PREFIX_PATH /opt/drake)
find_package(drake CONFIG REQUIRED PATHS /opt/drake)
# find_package(drake CONFIG REQUIRED)

add_library(${PROJECT_NAME} SHARED
  src/param_utils.cc
  src/yaml_read_include.cc
  src/logger.cc
)

target_link_libraries(${PROJECT_NAME} drake::drake)

target_include_directories(${PROJECT_NAME} PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_HEADER_DIR "${CMAKE_CURRENT_BINARY_DIR}")

install(TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

# Export old-style CMake variables
# ament_export_libraries(${PROJECT_NAME})

# Export modern CMake targets

install(DIRECTORY include/ DESTINATION include/)

if(BUILD_TESTING)
  find_package(ament_cmake_gtest CONFIG REQUIRED)
  find_package(ament_lint_auto CONFIG REQUIRED)

  ament_add_gtest(${PROJECT_NAME}_test test/test_yaml.cc)
  target_link_libraries(${PROJECT_NAME}_test 
    ${PROJECT_NAME}
    rclcpp_components::component  
  )

  ament_lint_auto_find_test_dependencies()
endif()

ament_export_include_directories(include/${PROJECT_NAME})
ament_export_targets(export_${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME})

# ament_export_include_directories(include)

ament_package()

Any help would be greatly appreciated. Drake is installed properly to /opt/drake and I can build standalone packages that depends on drake

jwnimmer-tri commented 2 months ago

I noticed that this was later asked and answered here: https://stackoverflow.com/a/78630550/13032709

If the code at drake_ament_cmake could be improved to help out the next person who reaches this problem, we would definitely accept a pull request with the improvements.