@@ -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 )
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
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,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
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
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+
527553if (BUILD_UNITTESTS)
528554 enable_testing ()
529555 add_subdirectory (unit_test )
@@ -532,8 +558,83 @@ endif()
532558# Include the subdirectories
533559add_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+
537638if (G2O_BUILD_BENCHMARKS)
538639 find_package (benchmark )
539640 if (${benchmark_FOUND} )
0 commit comments