Merge pull request #903 from TomRoyls/master

Improve CMake integration for use as subdirectory (fetch_content)
This commit is contained in:
Rainer Kümmerle
2025-06-01 19:31:55 +02:00
committed by GitHub
23 changed files with 125 additions and 39 deletions

View File

@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.14)
set(CMAKE_LEGACY_CYGWIN_WIN32 0)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment target")
project(g2o)
project(g2o LANGUAGES CXX C)
include(CPack)
include(GNUInstallDirs)
@@ -39,26 +39,35 @@ if (BUILD_SHARED_LIBS)
set (G2O_LIB_TYPE SHARED)
endif()
# On the Mac platform, configure the RPATH as per the INSTALL, to
# avoid the problem of loading both the built and INSTALLed versions
# of the shared targets
if(APPLE)
set(CMAKE_INSTALL_RPATH "")
set(CMAKE_MACOSX_RPATH TRUE)
# ignore deprecated GL
add_definitions(-DGL_SILENCE_DEPRECATION)
endif(APPLE)
# Option to control installation of cmake config files when used as subdirectory
option(G2O_INSTALL_CMAKE_CONFIG "Install CMake configuration files even when used as subdirectory" OFF)
# Set the output directory for the build executables and libraries
set(g2o_RUNTIME_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the binaries")
if(WIN32)
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the libraries")
else(WIN32)
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/lib CACHE PATH "Target for the libraries")
endif(WIN32)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${g2o_RUNTIME_OUTPUT_DIRECTORY})
# Only set output directories and options when this is the top-level project
if(PROJECT_IS_TOP_LEVEL)
# On the Mac platform, configure the RPATH as per the INSTALL, to
# avoid the problem of loading both the built and INSTALLed versions
# of the shared targets
if(APPLE)
set(CMAKE_INSTALL_RPATH "")
set(CMAKE_MACOSX_RPATH TRUE)
# ignore deprecated GL
add_definitions(-DGL_SILENCE_DEPRECATION)
endif(APPLE)
endif()
# Only set output directories when this is the top-level project
if(PROJECT_IS_TOP_LEVEL)
# Set the output directory for the build executables and libraries
set(g2o_RUNTIME_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the binaries")
if(WIN32)
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the libraries")
else(WIN32)
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/lib CACHE PATH "Target for the libraries")
endif(WIN32)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${g2o_RUNTIME_OUTPUT_DIRECTORY})
endif()
# Set standard installation directories
set(RUNTIME_DESTINATION ${CMAKE_INSTALL_BINDIR})
@@ -262,7 +271,14 @@ else()
endif()
# shall we build the core apps using the library
option(G2O_BUILD_APPS "Build g2o apps" ON)
if(PROJECT_IS_TOP_LEVEL)
option(G2O_BUILD_APPS "Build g2o apps" ON)
option(G2O_BUILD_EXAMPLES "Build g2o examples" ON)
else()
option(G2O_BUILD_APPS "Build g2o apps" OFF)
option(G2O_BUILD_EXAMPLES "Build g2o examples" OFF)
endif()
if(G2O_BUILD_APPS)
message(STATUS "Compiling g2o apps")
endif(G2O_BUILD_APPS)
@@ -272,7 +288,6 @@ CMAKE_DEPENDENT_OPTION(G2O_BUILD_LINKED_APPS "Build apps linked with the librari
"G2O_BUILD_APPS" OFF)
# shall we build the examples
option(G2O_BUILD_EXAMPLES "Build g2o examples" ON)
if(G2O_BUILD_EXAMPLES)
message(STATUS "Compiling g2o examples")
endif(G2O_BUILD_EXAMPLES)
@@ -508,22 +523,28 @@ WRITE_BASIC_PACKAGE_VERSION_FILE(
"${G2O_VERSION_CONFIG}" VERSION ${G2O_VERSION} COMPATIBILITY SameMajorVersion
)
configure_file(config.h.in "${PROJECT_BINARY_DIR}/g2o/config.h")
install(FILES ${PROJECT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)
configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/g2o/config.h")
# Only install config.h when this is the top-level project or when explicitly requested
if(PROJECT_IS_TOP_LEVEL OR G2O_INSTALL_CMAKE_CONFIG)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)
endif()
configure_file("${g2o_SOURCE_DIR}/cmake_modules/Config.cmake.in" "${G2O_PROJECT_CONFIG}" @ONLY)
install(
FILES "${G2O_PROJECT_CONFIG}" "${G2O_VERSION_CONFIG}"
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
# Only install cmake config files when this is the top-level project or when explicitly requested
if(PROJECT_IS_TOP_LEVEL OR G2O_INSTALL_CMAKE_CONFIG)
install(
FILES "${G2O_PROJECT_CONFIG}" "${G2O_VERSION_CONFIG}"
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
install(
EXPORT "${G2O_TARGETS_EXPORT_NAME}"
NAMESPACE "${G2O_NAMESPACE}"
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
install(
EXPORT "${G2O_TARGETS_EXPORT_NAME}"
NAMESPACE "${G2O_NAMESPACE}"
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
endif()
# building unit test framework and our tests
option(BUILD_UNITTESTS "build unit test framework and the tests" OFF)
if(BUILD_UNITTESTS)
enable_testing()
add_subdirectory(unit_test)
@@ -532,8 +553,9 @@ endif()
# Include the subdirectories
add_subdirectory(g2o)
# Benchmarks
option(G2O_BUILD_BENCHMARKS "build benchmarks" OFF)
if(G2O_BUILD_BENCHMARKS)
find_package(benchmark)
if(${benchmark_FOUND})

View File

@@ -1,7 +1,7 @@
add_executable(benchmark_jacobian_timing jacobian_timing_tests.cpp)
target_include_directories(benchmark_jacobian_timing PUBLIC
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
)
target_link_libraries(benchmark_jacobian_timing benchmark::benchmark ${G2O_EIGEN3_EIGEN_TARGET})

View File

@@ -11,4 +11,7 @@ if (@G2O_HAVE_LOGGING@)
find_dependency(spdlog)
endif()
include("${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
# Only include the targets export file if it exists (for installed builds)
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
endif()

View File

@@ -6,7 +6,7 @@ add_library(freeglut_minimal ${G2O_LIB_TYPE}
target_link_libraries(freeglut_minimal PUBLIC ${G2O_OPENGL_TARGET})
target_include_directories(freeglut_minimal PUBLIC
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
$<INSTALL_INTERFACE:include/g2o/freeglut_minimal>
)
target_compile_features(freeglut_minimal PUBLIC cxx_std_17)

View File

@@ -8,6 +8,9 @@ install(TARGETS g2o_ceres_ad
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::g2o_ceres_ad ALIAS g2o_ceres_ad)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/autodiff)
install(FILES LICENSE DESTINATION ${INCLUDES_INSTALL_DIR}/autodiff)

View File

@@ -42,7 +42,7 @@ g2o_core_api.h
)
target_include_directories(core PUBLIC
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
$<INSTALL_INTERFACE:include/g2o/core>
)
@@ -66,6 +66,9 @@ install(TARGETS core
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::core ALIAS core)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/core)

View File

@@ -43,3 +43,6 @@ install(TARGETS solver_cholmod
ARCHIVE DESTINATION ${ARCHIVE_DESTINATION}
PUBLIC_HEADER DESTINATION ${INCLUDES_DESTINATION}/g2o/solvers/cholmod
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_cholmod ALIAS solver_cholmod)

View File

@@ -52,5 +52,9 @@ install(TARGETS solver_csparse csparse_extension
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias targets for easier usage when included as subdirectory
add_library(g2o::solver_csparse ALIAS solver_csparse)
add_library(g2o::csparse_extension ALIAS csparse_extension)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/csparse)

View File

@@ -21,6 +21,9 @@ install(TARGETS solver_dense
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_dense ALIAS solver_dense)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/dense)

View File

@@ -25,5 +25,8 @@ install(TARGETS solver_eigen
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_eigen ALIAS solver_eigen)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/eigen)

View File

@@ -27,6 +27,9 @@ install(TARGETS solver_pcg
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_pcg ALIAS solver_pcg)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/pcg)

View File

@@ -23,6 +23,9 @@ install(TARGETS solver_slam2d_linear
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_slam2d_linear ALIAS solver_slam2d_linear)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/slam2d_linear)

View File

@@ -22,6 +22,9 @@ install(TARGETS solver_structure_only
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::solver_structure_only ALIAS solver_structure_only)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/structure_only)

View File

@@ -12,7 +12,7 @@ add_library(stuff ${G2O_LIB_TYPE}
)
target_include_directories(stuff PUBLIC
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
$<INSTALL_INTERFACE:include/g2o/stuff>
)
@@ -42,6 +42,9 @@ install(TARGETS stuff
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::stuff ALIAS stuff)
# build our OpenGL helper library
if(OPENGL_FOUND AND G2O_HAVE_OPENGL)
add_library(opengl_helper ${G2O_LIB_TYPE}
@@ -60,7 +63,7 @@ if(OPENGL_FOUND AND G2O_HAVE_OPENGL)
)
target_include_directories(opengl_helper PUBLIC
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
$<INSTALL_INTERFACE:include/g2o/stuff>
)
@@ -70,6 +73,9 @@ if(OPENGL_FOUND AND G2O_HAVE_OPENGL)
set_target_properties(opengl_helper PROPERTIES
VERSION ${G2O_LIB_VERSION}
SOVERSION ${G2O_LIB_SOVERSION})
# Create alias target for easier usage when included as subdirectory
add_library(g2o::opengl_helper ALIAS opengl_helper)
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND NOT ANDROID)

View File

@@ -37,5 +37,8 @@ install(TARGETS types_data
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_data ALIAS types_data)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/data)

View File

@@ -25,6 +25,9 @@ install(TARGETS types_icp
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_icp ALIAS types_icp)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/icp)

View File

@@ -52,6 +52,9 @@ install(TARGETS types_sba
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_sba ALIAS types_sba)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/sba)

View File

@@ -31,5 +31,8 @@ install(TARGETS types_sclam2d
ARCHIVE DESTINATION ${ARCHIVE_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_sclam2d ALIAS types_sclam2d)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/sclam2d)

View File

@@ -25,6 +25,9 @@ install(TARGETS types_sim3
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_sim3 ALIAS types_sim3)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/sim3)

View File

@@ -42,5 +42,8 @@ install(TARGETS types_slam2d
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_slam2d ALIAS types_slam2d)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/slam2d)

View File

@@ -37,5 +37,8 @@ install(TARGETS types_slam2d_addons
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_slam2d_addons ALIAS types_slam2d_addons)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/slam2d_addons)

View File

@@ -65,5 +65,8 @@ install(TARGETS types_slam3d
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_slam3d ALIAS types_slam3d)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/slam3d)

View File

@@ -42,5 +42,8 @@ install(TARGETS types_slam3d_addons
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
)
# Create alias target for easier usage when included as subdirectory
add_library(g2o::types_slam3d_addons ALIAS types_slam3d_addons)
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/types/slam3d_addons)