Use ament to vendor a header only library

asked 2023-03-07 11:43:55 -0500

orensbruli gravatar image

I'm trying to create a ros package vendoring the cppitertools library. What I want to achieve is to be able to use in the downstream find_package(cppitertools_vendor) and then be able to use the ${cppitertools_vendor_INCLUDES_DIR} with the target_include_directories.

I have been looking for some headers only vendored package in ros with no luck.

Can someone point me to an example or help me with this?

This is the current status of my CMakeLists.txt (after many tries):

cmake_minimum_required(VERSION 3.10)

project(cppitertools_vendor)

find_package(ament_cmake REQUIRED)

macro(build_cppitertools)

  set(cmake_commands)
  set(cmake_configure_args -Wno-dev)

  if(WIN32)
    if(DEFINED CMAKE_GENERATOR)
      list(APPEND cmake_configure_args -G ${CMAKE_GENERATOR})
    endif()
    if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "^(x86_|x86-|AMD|amd|x)64$")
      list(APPEND cmake_configure_args -A x64)
    endif()
  endif()

  if(DEFINED CMAKE_BUILD_TYPE)
    if(WIN32)
      build_command(_build_command CONFIGURATION ${CMAKE_BUILD_TYPE})
      list(APPEND cmake_commands "BUILD_COMMAND ${_build_command}")
    else()
      list(APPEND cmake_configure_args -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
    endif()
  endif()

  if(DEFINED CMAKE_TOOLCHAIN_FILE)
    list(APPEND cmake_configure_args "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
    if(ANDROID)
      if(DEFINED ANDROID_ABI)
        list(APPEND cmake_configure_args "-DANDROID_ABI=${ANDROID_ABI}")
      endif()
      if(DEFINED ANDROID_CPP_FEATURES)
        list(APPEND cmake_configure_args "-DANDROID_CPP_FEATURES=${ANDROID_CPP_FEATURES}")
      endif()
      if(DEFINED ANDROID_FUNCTION_LEVEL_LINKING)
        list(APPEND cmake_configure_args "-DANDROID_FUNCTION_LEVEL_LINKING=${ANDROID_FUNCTION_LEVEL_LINKING}")
      endif()
      if(DEFINED ANDROID_NATIVE_API_LEVEL)
        list(APPEND cmake_configure_args "-DANDROID_NATIVE_API_LEVEL=${ANDROID_NATIVE_API_LEVEL}")
      endif()
      if(DEFINED ANDROID_NDK)
        list(APPEND cmake_configure_args "-DANDROID_NDK=${ANDROID_NDK}")
      endif()
      if(DEFINED ANDROID_STL)
        list(APPEND cmake_configure_args "-DANDROID_STL=${ANDROID_STL}")
      endif()
      if(DEFINED ANDROID_TOOLCHAIN_NAME)
        list(APPEND cmake_configure_args "-DANDROID_TOOLCHAIN_NAME=${ANDROID_TOOLCHAIN_NAME}")
      endif()
    endif()
  else()
    list(APPEND cmake_configure_args "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
  endif()
  set(cppitertools_external_project_dir ${CMAKE_CURRENT_BINARY_DIR}/cppitertools_external_project)


  include(ExternalProject)
  set(cppitertools_version "master")
  externalproject_add(${PROJECT_NAME}-ext
    GIT_REPOSITORY https://github.com/ryanhaining/cppitertools.git
    GIT_TAG ${cppitertools_version}
#    GIT_CONFIG advice.detachedHead=false
    # Suppress git update due to https://gitlab.kitware.com/cmake/cmake/-/issues/16419
    UPDATE_COMMAND ""
#    TIMEOUT 1000
    ${cmake_commands}
    CMAKE_ARGS
      -DCMAKE_INSTALL_PREFIX=${cppitertools_external_project_dir}/install/
      -DBUILD_SHARED_LIBS:BOOL=ON
      ${cmake_configure_args}
  )


  # Specify include dir


  # Library
  add_library(${PROJECT_NAME} INTERFACE)
  add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}-ext)
  target_include_directories(${PROJECT_NAME}
          INTERFACE
          $<BUILD_INTERFACE:${${PROJECT_NAME}_INCLUDE_DIRS}>
          $<INSTALL_INTERFACE:include>
          )


  ament_export_include_directories(${cppitertools_external_project_dir}/install/include)

  # The external project will install to the build folder, but we'll install that on make install.
  install(
          DIRECTORY
          ${cppitertools_external_project_dir}/install/
          DESTINATION
          ${CMAKE_INSTALL_PREFIX}
          USE_SOURCE_PERMISSIONS
  )
#  install(
#          DIRECTORY ${${PROJECT_NAME}_INCLUDE_DIRS}
#          DESTINATION include
#  )
#  install(
#          TARGETS ${PROJECT_NAME}
#          EXPORT ${PROJECT_NAME}-ext
#          LIBRARY DESTINATION lib
#          ARCHIVE DESTINATION lib
#          RUNTIME DESTINATION bin
#          INCLUDES DESTINATION include
#  )


#  MARK_AS_ADVANCED(${PROJECT_NAME_UPPER}_DIR ${PROJECT_NAME_UPPER}_INCLUDE_DIRS ${PROJECT_NAME_UPPER}_LIBRARIES)

endmacro()

build_cppitertools()

# this ensures that the package has an environment hook setting the PATH
ament_package()
edit retag flag offensive close merge delete