September 29, 2009

CMake Recipe #4: Append version number to install path (revisited)

In my first tip, I provided a method of updating the install path with the version number.  After using this in one of my own projects for some time, I found that the short-coming of the particular recipe is that the CMAKE_INSTALL_PREFIX does not update when the VERSION variable is incremented.  This means that running "make install" will install the program into the old version's folder (bad bad bad).

 Here is my revised recipe for appending the version number to the install path:

IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
SET(ORIGINAL_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE STRING "Default prefix path" FORCE)
ENDIF()
MARK_AS_ADVANCED(ORIGINAL_INSTALL_PREFIX)

IF( NOT( VERSION VERSION_EQUAL PROJECT_VERSION) )
SET(CMAKE_INSTALL_PREFIX "${ORIGINAL_INSTALL_PREFIX}-${VERSION}" CACHE STRING "Install path" FORCE)
SET(PROJECT_VERSION ${VERSION})
ENDIF() 

 This ensures that the CMAKE_INSTALL_PREFIX is updated whenever the old version number and the new version number doesn't match.  This does have some side-effects, such as situations where the user is customizing the CMAKE_INSTALL_PREFIX to some custom location.  Such users would need to modify the ORIGINAL_INSTALL_PREFIX instead to ensure that the project is going to the desired place. 

Click Here!