[CMake] lexical comparison of cmake version numbers

Brandon J. Van Every bvanevery at gmail.com
Fri Sep 22 02:15:36 EDT 2006


I would like more direct access to the CMake version number, and the 
ability to make easy lexical comparisons between version numbers.

Typically, this is so I can test the current release version of CMake, 
and CMake CVS, using the same CMakeLists.txt.  I have workarounds for 
various bugs in CMake 2.4.3 in my code, and it is working, shipping 
code.  Recipients of the code have CMake 2.4.3, they do not generally 
have CMake CVS, nor is it reasonable to require them to have it.  As 
things get fixed in CVS, I'd like to IF(...) out the bug workarounds and 
write for the newer version of CMake.  In this manner I can get a head 
start on what works for the next release, and verify that the next 
release will actually fix the bugs.  Once the next release becomes 
official, I can kill the old code and bump up CMAKE_MINIMUM_REQUIRED, 
but not before.

Extracting a useful version number is currently somewhat involved and 
error prone.  In particular, it took me several iterations of head 
scratching to realize that the CMake 2.4.3 version banner has a \n 
newline in it.  I had been trying to do an exact string comparison, 
based on what I saw printed on the command line, and it didn't work.  
Below is what I ended up doing.  I don't think people should have to 
rediscover these steps, nor do as much boilerplate.

Another danger of banner-driven version numbers, as we've seen in 
Chicken Scheme's development, is that banners tend to change.  This in 
turn tends to break REGEX-based version number extraction, and the 
failures tend to be weird and spectacular.  Thus in Chicken a canonical 
"-release" option was implemented, that only gives a clean version 
number, i.e. 2.4.3 or such.

Lexical comparison of Chicken versions hasn't been implemented.  I have 
a lot of fudge in Chicken's CMakeLists.txt for that.  Chicken's version 
numbers aren't properly canonized, i.e. 2.237, 2.3, 2.35, 2.4, 2.46, 
2.432 are successive versions of Chicken.  So I do some juggling of 
"place values" to get everything to work.  That shouldn't be a problem 
with CMake.  Still, it is important to have a comparison operator that 
actually works.  Simple string comparisons won't work, i.e. 2.9.1 < 
2.10.0 version-wise, but not string-wise.


Cheers,
Brandon Van Every

SET(IS_CMAKE_243 false)
EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} --version
  RESULT_VARIABLE CANNOT_GET_VERSION
  OUTPUT_VARIABLE CMAKE_VERSION_BANNER)
IF(CANNOT_GET_VERSION)
  MESSAGE(FATAL_ERROR "Unable to ascertain CMake version.  Most 
bizarre.  Terminating the build.")
ELSE(CANNOT_GET_VERSION)
  # CMAKE_VERSION_BANNER should be "cmake version 2.4-patch 3\n"
  # for CMake 2.4.3.  Note the newline character.
  # I'm not entirely confident that the line termination will be
  # the same on all operating systems and shells.  For paranoia
  # we do a regex match rather than a simple string equality.
  # That way, we can ignore weird characters like \n
  STRING(REGEX MATCH "version 2.4-patch 3" VERSION_PATCH 
"${CMAKE_VERSION_BANNER}")
  MESSAGE("${VERSION_PATCH}")
  IF(VERSION_PATCH STREQUAL "version 2.4-patch 3")
    SET(IS_CMAKE_243 true)
  ENDIF(VERSION_PATCH STREQUAL "version 2.4-patch 3")
ENDIF(CANNOT_GET_VERSION)



More information about the CMake mailing list