Skip to content

Commit 5ad2b3d

Browse files
Merge pull request #903 from TomRoyls/master
Improve CMake integration for use as subdirectory (fetch_content)
2 parents f64b402 + 6836dcb commit 5ad2b3d

23 files changed

Lines changed: 126 additions & 40 deletions

File tree

CMakeLists.txt

Lines changed: 56 additions & 34 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)
51-
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})
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)
44+
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,28 @@ 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

525-
# building unit test framework and our tests
526546
option(BUILD_UNITTESTS "build unit test framework and the tests" OFF)
547+
527548
if(BUILD_UNITTESTS)
528549
enable_testing()
529550
add_subdirectory(unit_test)
@@ -532,8 +553,9 @@ endif()
532553
# Include the subdirectories
533554
add_subdirectory(g2o)
534555

535-
# Benchmarks
556+
536557
option(G2O_BUILD_BENCHMARKS "build benchmarks" OFF)
558+
537559
if(G2O_BUILD_BENCHMARKS)
538560
find_package(benchmark)
539561
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/autodiff/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ install(TARGETS g2o_ceres_ad
88
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
99
)
1010

11+
# Create alias target for easier usage when included as subdirectory
12+
add_library(g2o::g2o_ceres_ad ALIAS g2o_ceres_ad)
13+
1114
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
1215
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/autodiff)
1316
install(FILES LICENSE DESTINATION ${INCLUDES_INSTALL_DIR}/autodiff)

g2o/core/CMakeLists.txt

Lines changed: 4 additions & 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

@@ -66,6 +66,9 @@ install(TARGETS core
6666
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
6767
)
6868

69+
# Create alias target for easier usage when included as subdirectory
70+
add_library(g2o::core ALIAS core)
71+
6972
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
7073

7174
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/core)

g2o/solvers/cholmod/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ install(TARGETS solver_cholmod
4343
ARCHIVE DESTINATION ${ARCHIVE_DESTINATION}
4444
PUBLIC_HEADER DESTINATION ${INCLUDES_DESTINATION}/g2o/solvers/cholmod
4545
)
46+
47+
# Create alias target for easier usage when included as subdirectory
48+
add_library(g2o::solver_cholmod ALIAS solver_cholmod)

g2o/solvers/csparse/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ install(TARGETS solver_csparse csparse_extension
5252
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
5353
)
5454

55+
# Create alias targets for easier usage when included as subdirectory
56+
add_library(g2o::solver_csparse ALIAS solver_csparse)
57+
add_library(g2o::csparse_extension ALIAS csparse_extension)
58+
5559
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
5660
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/csparse)

g2o/solvers/dense/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ install(TARGETS solver_dense
2121
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
2222
)
2323

24+
# Create alias target for easier usage when included as subdirectory
25+
add_library(g2o::solver_dense ALIAS solver_dense)
26+
2427
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
2528

2629
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/dense)

g2o/solvers/eigen/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ install(TARGETS solver_eigen
2525
INCLUDES DESTINATION ${INCLUDES_DESTINATION}
2626
)
2727

28+
# Create alias target for easier usage when included as subdirectory
29+
add_library(g2o::solver_eigen ALIAS solver_eigen)
30+
2831
file(GLOB headers "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
2932
install(FILES ${headers} DESTINATION ${INCLUDES_INSTALL_DIR}/solvers/eigen)

0 commit comments

Comments
 (0)