[CMake] Building a Version Header

John Drescher drescherjm at gmail.com
Wed Jul 7 22:43:15 EDT 2010


On Wed, Jul 7, 2010 at 9:44 PM, Clark Gaebel <cg.wowus.cg at gmail.com> wrote:
> I would like to generate file that looks something like this:
>
>    // version.h
>    #define VERSION "v0.1-345-ga77ede8"
>
> where the version string is the result of running "git describe --tags
> --dirty". How can I auto-generate this file, include it in my project,
> and have it regenerate as a pre-build event?
>

You want to do that with configure_file

The following is an example that grabs the subversion rev and puts
that into a version header file.

In CMakeLists.txt for the LungAnalysis project

set (${PROJECT_NAME}_VERSION_MAJOR 0)
set (${PROJECT_NAME}_VERSION_MINOR 25)
set (${PROJECT_NAME}_VERSION_PATCH 3)

FIND_PACKAGE(Subversion)
IF(Subversion_FOUND)
#Use the FindSubversion.cmake module to get the svn rev and append
that to the patch version.
	    Subversion_WC_INFO(${PROJECT_SOURCE_DIR} Project)
	    MESSAGE("Current revision is ${Project_WC_REVISION}")
	    Subversion_WC_LOG(${PROJECT_SOURCE_DIR} Project)
	    MESSAGE("Last changed log is ${Project_LAST_CHANGED_LOG}")
	
	        set (${PROJECT_NAME}_VERSION_PATCH
${${PROJECT_NAME}_VERSION_PATCH}.${Project_WC_REVISION})
	
ENDIF(Subversion_FOUND)

configure_file (
	        "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.h.in"
	        "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.h"
)

LungAnalysisConfig.h.in:
//The configured settings for the project
#define LungAnalysis_VERSION_MAJOR "@LungAnalysis_VERSION_MAJOR@"
#define LungAnalysis_VERSION_MINOR "@LungAnalysis_VERSION_MINOR@"
#define LungAnalysis_VERSION_PATCH "@LungAnalysis_VERSION_PATCH@"


So then in the binary folder the LungAnalysisConfig.h will look like

#define LungAnalysis_VERSION_MAJOR "0"
#define LungAnalysis_VERSION_MINOR "25"
#define LungAnalysis_VERSION_PATCH "3.903"

for svn revision 903

John


More information about the CMake mailing list