Does CMake have the concept of ifdef endif

I am working on a sub-project that will be shared with another CMake project most of the time. I wonder if CMake has something like

ifndef cmake_minimum_required
cmake_minimum_required(VERSION 2.8.11)
endif

      

basically if cmake_minimum_required

already defined somewhere, in my subproject I want to use what has been defined, otherwise I want to define cmake_minimum_required

in my subproject.

+3


source to share


1 answer


You can use CMake policy to indicate that t21> has already been called. CMP0000

When called cmake_minimum_required

, it is CMP0000

automatically set to NEW

, so you can do:

cmake_policy(GET CMP0000 MinimumVersionIsSet)
if(NOT MinimumVersionIsSet STREQUAL "NEW")
  cmake_minimum_required(VERSION 2.8.11)
endif()

      

The only way I can see this is if the higher level CMakeLists.txt contains something like:



cmake_minimum_required(VERSION 2.6)
cmake_policy(SET CMP0000 OLD)

      

before calling add_subdirectory

for your project. This would set the policy NEW

and then immediately set it to OLD

. However, I believe this is a very unlikely scenario, as the policy will be set OLD

to allow the call cmake_minimum_required

.

Having said all that, I think your approach might be a little risky. Let's say a top-level project sets the minimum required version to 2.6 and you want features that are only available in 2.8. Since your call is cmake_minimum_required

skipped, users running CMake 2.6 will receive more cryptic failure messages (about unknown CMake commands) rather than the more succinct "CMake 2.8.11 or higher". You are using version 2.6.0 ".

+2


source







All Articles