@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.14)
33set (CMAKE_LEGACY_CYGWIN_WIN32 0)
44set (CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment target" )
55
6- project (g2o)
6+ project (g2o LANGUAGES CXX C )
77
88include (CPack )
99include (GNUInstallDirs )
@@ -39,26 +39,35 @@ if (BUILD_SHARED_LIBS)
3939 set (G2O_LIB_TYPE SHARED)
4040endif ()
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
6473set (RUNTIME_DESTINATION ${CMAKE_INSTALL_BINDIR} )
@@ -262,7 +271,14 @@ else()
262271endif ()
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+
266282if (G2O_BUILD_APPS)
267283 message (STATUS "Compiling g2o apps" )
268284endif (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 )
276291if (G2O_BUILD_EXAMPLES)
277292 message (STATUS "Compiling g2o examples" )
278293endif (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
514532configure_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
526546option (BUILD_UNITTESTS "build unit test framework and the tests" OFF )
547+
527548if (BUILD_UNITTESTS)
528549 enable_testing ()
529550 add_subdirectory (unit_test )
@@ -532,8 +553,9 @@ endif()
532553# Include the subdirectories
533554add_subdirectory (g2o )
534555
535- # Benchmarks
556+
536557option (G2O_BUILD_BENCHMARKS "build benchmarks" OFF )
558+
537559if (G2O_BUILD_BENCHMARKS)
538560 find_package (benchmark )
539561 if (${benchmark_FOUND} )
0 commit comments