From deaf362fa14ca192bd74b839ce3f2d3aa91a845d Mon Sep 17 00:00:00 2001 From: TimPushkin Date: Wed, 8 Feb 2023 18:41:22 +0300 Subject: [PATCH] Set CMAKE_* cache vars only as a top-level project --- CMakeLists.txt | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05962163a..36f656398 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,18 +12,25 @@ set(LIB_PREFIX g2o_) set(g2o_C_FLAGS) set(g2o_CXX_FLAGS) -# default built type -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING - "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." - FORCE) -endif(NOT CMAKE_BUILD_TYPE) - -# postfix, based on type -set(CMAKE_DEBUG_POSTFIX "_d" CACHE STRING "postfix applied to debug build of libraries") -set(CMAKE_RELEASE_POSTFIX "" CACHE STRING "postfix applied to release build of libraries") -set(CMAKE_RELWITHDEBINFO_POSTFIX "_rd" CACHE STRING "postfix applied to release-with-debug-information libraries") -set(CMAKE_MINSIZEREL_POSTFIX "_s" CACHE STRING "postfix applied to minimum-size-build libraries") +# manually check for top-level project if CMake is older than 3.21 +if(CMAKE_VERSION VERSION_LESS 3.21) + string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}" PROJECT_IS_TOP_LEVEL) +endif() + +if(PROJECT_IS_TOP_LEVEL) + # default built type + if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release CACHE STRING + "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." + FORCE) + endif() + + # postfix, based on type + set(CMAKE_DEBUG_POSTFIX "_d" CACHE STRING "postfix applied to debug build of libraries") + set(CMAKE_RELEASE_POSTFIX "" CACHE STRING "postfix applied to release build of libraries") + set(CMAKE_RELWITHDEBINFO_POSTFIX "_rd" CACHE STRING "postfix applied to release-with-debug-information libraries") + set(CMAKE_MINSIZEREL_POSTFIX "_s" CACHE STRING "postfix applied to minimum-size-build libraries") +endif() # work out the postfix; required where we use OUTPUT_NAME if(CMAKE_BUILD_TYPE MATCHES Release)