Skip to content

Commit 011006e

Browse files
committed
Improve CMake integration for use as subdirectory
- Add PROJECT_IS_TOP_LEVEL checks to conditionally enable options and installations - Create alias targets (g2o::core, g2o::types_*, g2o::solver_*) for better namespacing - Fix include paths to use g2o_BINARY_DIR instead of PROJECT_BINARY_DIR - Add G2O_INSTALL_CMAKE_CONFIG option to control config file installation - Set default build options to OFF when used as subdirectory - Improve config.cmake to handle missing targets export gracefully
1 parent 18b1894 commit 011006e

6 files changed

Lines changed: 143 additions & 39 deletions

File tree

CMakeLists.txt

Lines changed: 134 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.14)
33
set(CMAKE_LEGACY_CYGWIN_WIN32 0)
44
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment target")
55

6-
project(g2o)
6+
project(g2o LANGUAGES CXX C)
77

88
include(CPack)
99
include(GNUInstallDirs)
@@ -39,26 +39,35 @@ if (BUILD_SHARED_LIBS)
3939
set (G2O_LIB_TYPE SHARED)
4040
endif()
4141

42-
# On the Mac platform, configure the RPATH as per the INSTALL, to
43-
# avoid the problem of loading both the built and INSTALLed versions
44-
# of the shared targets
45-
if(APPLE)
46-
set(CMAKE_INSTALL_RPATH "")
47-
set(CMAKE_MACOSX_RPATH TRUE)
48-
# ignore deprecated GL
49-
add_definitions(-DGL_SILENCE_DEPRECATION)
50-
endif(APPLE)
42+
# Option to control installation of cmake config files when used as subdirectory
43+
option(G2O_INSTALL_CMAKE_CONFIG "Install CMake configuration files even when used as subdirectory" OFF)
5144

52-
# Set the output directory for the build executables and libraries
53-
set(g2o_RUNTIME_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the binaries")
54-
if(WIN32)
55-
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the libraries")
56-
else(WIN32)
57-
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/lib CACHE PATH "Target for the libraries")
58-
endif(WIN32)
59-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
60-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
61-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${g2o_RUNTIME_OUTPUT_DIRECTORY})
45+
# Only set output directories and options when this is the top-level project
46+
if(PROJECT_IS_TOP_LEVEL)
47+
# On the Mac platform, configure the RPATH as per the INSTALL, to
48+
# avoid the problem of loading both the built and INSTALLed versions
49+
# of the shared targets
50+
if(APPLE)
51+
set(CMAKE_INSTALL_RPATH "")
52+
set(CMAKE_MACOSX_RPATH TRUE)
53+
# ignore deprecated GL
54+
add_definitions(-DGL_SILENCE_DEPRECATION)
55+
endif(APPLE)
56+
endif()
57+
58+
# Only set output directories when this is the top-level project
59+
if(PROJECT_IS_TOP_LEVEL)
60+
# Set the output directory for the build executables and libraries
61+
set(g2o_RUNTIME_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the binaries")
62+
if(WIN32)
63+
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/bin CACHE PATH "Target for the libraries")
64+
else(WIN32)
65+
set(g2o_LIBRARY_OUTPUT_DIRECTORY ${g2o_BINARY_DIR}/lib CACHE PATH "Target for the libraries")
66+
endif(WIN32)
67+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
68+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${g2o_LIBRARY_OUTPUT_DIRECTORY})
69+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${g2o_RUNTIME_OUTPUT_DIRECTORY})
70+
endif()
6271

6372
# Set standard installation directories
6473
set(RUNTIME_DESTINATION ${CMAKE_INSTALL_BINDIR})
@@ -262,7 +271,14 @@ else()
262271
endif()
263272

264273
# shall we build the core apps using the library
265-
option(G2O_BUILD_APPS "Build g2o apps" ON)
274+
if(PROJECT_IS_TOP_LEVEL)
275+
option(G2O_BUILD_APPS "Build g2o apps" ON)
276+
option(G2O_BUILD_EXAMPLES "Build g2o examples" ON)
277+
else()
278+
option(G2O_BUILD_APPS "Build g2o apps" OFF)
279+
option(G2O_BUILD_EXAMPLES "Build g2o examples" OFF)
280+
endif()
281+
266282
if(G2O_BUILD_APPS)
267283
message(STATUS "Compiling g2o apps")
268284
endif(G2O_BUILD_APPS)
@@ -272,7 +288,6 @@ CMAKE_DEPENDENT_OPTION(G2O_BUILD_LINKED_APPS "Build apps linked with the librari
272288
"G2O_BUILD_APPS" OFF)
273289

274290
# shall we build the examples
275-
option(G2O_BUILD_EXAMPLES "Build g2o examples" ON)
276291
if(G2O_BUILD_EXAMPLES)
277292
message(STATUS "Compiling g2o examples")
278293
endif(G2O_BUILD_EXAMPLES)
@@ -508,22 +523,33 @@ WRITE_BASIC_PACKAGE_VERSION_FILE(
508523
"${G2O_VERSION_CONFIG}" VERSION ${G2O_VERSION} COMPATIBILITY SameMajorVersion
509524
)
510525

511-
configure_file(config.h.in "${PROJECT_BINARY_DIR}/g2o/config.h")
512-
install(FILES ${PROJECT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)
526+
configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/g2o/config.h")
527+
# Only install config.h when this is the top-level project or when explicitly requested
528+
if(PROJECT_IS_TOP_LEVEL OR G2O_INSTALL_CMAKE_CONFIG)
529+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/g2o/config.h DESTINATION ${INCLUDES_DESTINATION}/g2o)
530+
endif()
513531

514532
configure_file("${g2o_SOURCE_DIR}/cmake_modules/Config.cmake.in" "${G2O_PROJECT_CONFIG}" @ONLY)
515533

516-
install(
517-
FILES "${G2O_PROJECT_CONFIG}" "${G2O_VERSION_CONFIG}"
518-
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
534+
# Only install cmake config files when this is the top-level project or when explicitly requested
535+
if(PROJECT_IS_TOP_LEVEL OR G2O_INSTALL_CMAKE_CONFIG)
536+
install(
537+
FILES "${G2O_PROJECT_CONFIG}" "${G2O_VERSION_CONFIG}"
538+
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
519539

520-
install(
521-
EXPORT "${G2O_TARGETS_EXPORT_NAME}"
522-
NAMESPACE "${G2O_NAMESPACE}"
523-
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
540+
install(
541+
EXPORT "${G2O_TARGETS_EXPORT_NAME}"
542+
NAMESPACE "${G2O_NAMESPACE}"
543+
DESTINATION "${G2O_CONFIG_INSTALL_DIR}")
544+
endif()
524545

525546
# building unit test framework and our tests
526-
option(BUILD_UNITTESTS "build unit test framework and the tests" OFF)
547+
if(PROJECT_IS_TOP_LEVEL)
548+
option(BUILD_UNITTESTS "build unit test framework and the tests" OFF)
549+
else()
550+
option(BUILD_UNITTESTS "build unit test framework and the tests" OFF)
551+
endif()
552+
527553
if(BUILD_UNITTESTS)
528554
enable_testing()
529555
add_subdirectory(unit_test)
@@ -532,8 +558,83 @@ endif()
532558
# Include the subdirectories
533559
add_subdirectory(g2o)
534560

561+
# Create alias targets for easier usage when included as subdirectory
562+
# Core libraries
563+
if(TARGET core)
564+
add_library(g2o::core ALIAS core)
565+
endif()
566+
if(TARGET stuff)
567+
add_library(g2o::stuff ALIAS stuff)
568+
endif()
569+
if(TARGET g2o_ceres_ad)
570+
add_library(g2o::g2o_ceres_ad ALIAS g2o_ceres_ad)
571+
endif()
572+
if(TARGET opengl_helper)
573+
add_library(g2o::opengl_helper ALIAS opengl_helper)
574+
endif()
575+
576+
# Type libraries
577+
if(TARGET types_data)
578+
add_library(g2o::types_data ALIAS types_data)
579+
endif()
580+
if(TARGET types_icp)
581+
add_library(g2o::types_icp ALIAS types_icp)
582+
endif()
583+
if(TARGET types_sba)
584+
add_library(g2o::types_sba ALIAS types_sba)
585+
endif()
586+
if(TARGET types_sclam2d)
587+
add_library(g2o::types_sclam2d ALIAS types_sclam2d)
588+
endif()
589+
if(TARGET types_sim3)
590+
add_library(g2o::types_sim3 ALIAS types_sim3)
591+
endif()
592+
if(TARGET types_slam2d)
593+
add_library(g2o::types_slam2d ALIAS types_slam2d)
594+
endif()
595+
if(TARGET types_slam2d_addons)
596+
add_library(g2o::types_slam2d_addons ALIAS types_slam2d_addons)
597+
endif()
598+
if(TARGET types_slam3d)
599+
add_library(g2o::types_slam3d ALIAS types_slam3d)
600+
endif()
601+
if(TARGET types_slam3d_addons)
602+
add_library(g2o::types_slam3d_addons ALIAS types_slam3d_addons)
603+
endif()
604+
605+
# Solver libraries
606+
if(TARGET solver_cholmod)
607+
add_library(g2o::solver_cholmod ALIAS solver_cholmod)
608+
endif()
609+
if(TARGET csparse_extension)
610+
add_library(g2o::csparse_extension ALIAS csparse_extension)
611+
endif()
612+
if(TARGET solver_csparse)
613+
add_library(g2o::solver_csparse ALIAS solver_csparse)
614+
endif()
615+
if(TARGET solver_dense)
616+
add_library(g2o::solver_dense ALIAS solver_dense)
617+
endif()
618+
if(TARGET solver_eigen)
619+
add_library(g2o::solver_eigen ALIAS solver_eigen)
620+
endif()
621+
if(TARGET solver_pcg)
622+
add_library(g2o::solver_pcg ALIAS solver_pcg)
623+
endif()
624+
if(TARGET solver_slam2d_linear)
625+
add_library(g2o::solver_slam2d_linear ALIAS solver_slam2d_linear)
626+
endif()
627+
if(TARGET solver_structure_only)
628+
add_library(g2o::solver_structure_only ALIAS solver_structure_only)
629+
endif()
630+
535631
# Benchmarks
536-
option(G2O_BUILD_BENCHMARKS "build benchmarks" OFF)
632+
if(PROJECT_IS_TOP_LEVEL)
633+
option(G2O_BUILD_BENCHMARKS "build benchmarks" OFF)
634+
else()
635+
option(G2O_BUILD_BENCHMARKS "build benchmarks" OFF)
636+
endif()
637+
537638
if(G2O_BUILD_BENCHMARKS)
538639
find_package(benchmark)
539640
if(${benchmark_FOUND})

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_executable(benchmark_jacobian_timing jacobian_timing_tests.cpp)
22

33
target_include_directories(benchmark_jacobian_timing PUBLIC
4-
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
4+
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
55
)
66

77
target_link_libraries(benchmark_jacobian_timing benchmark::benchmark ${G2O_EIGEN3_EIGEN_TARGET})

cmake_modules/Config.cmake.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ if (@G2O_HAVE_LOGGING@)
1111
find_dependency(spdlog)
1212
endif()
1313

14-
include("${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
14+
# Only include the targets export file if it exists (for installed builds)
15+
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
16+
include("${CMAKE_CURRENT_LIST_DIR}/@G2O_TARGETS_EXPORT_NAME@.cmake")
17+
endif()

g2o/EXTERNAL/freeglut/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_library(freeglut_minimal ${G2O_LIB_TYPE}
66

77
target_link_libraries(freeglut_minimal PUBLIC ${G2O_OPENGL_TARGET})
88
target_include_directories(freeglut_minimal PUBLIC
9-
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
9+
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
1010
$<INSTALL_INTERFACE:include/g2o/freeglut_minimal>
1111
)
1212
target_compile_features(freeglut_minimal PUBLIC cxx_std_17)

g2o/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ g2o_core_api.h
4242
)
4343

4444
target_include_directories(core PUBLIC
45-
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
45+
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
4646
$<INSTALL_INTERFACE:include/g2o/core>
4747
)
4848

g2o/stuff/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ add_library(stuff ${G2O_LIB_TYPE}
1212
)
1313

1414
target_include_directories(stuff PUBLIC
15-
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
15+
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
1616
$<INSTALL_INTERFACE:include/g2o/stuff>
1717
)
1818

@@ -60,7 +60,7 @@ if(OPENGL_FOUND AND G2O_HAVE_OPENGL)
6060
)
6161

6262
target_include_directories(opengl_helper PUBLIC
63-
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${PROJECT_BINARY_DIR}>"
63+
"$<BUILD_INTERFACE:${g2o_SOURCE_DIR};${g2o_BINARY_DIR}>"
6464
$<INSTALL_INTERFACE:include/g2o/stuff>
6565
)
6666

0 commit comments

Comments
 (0)