[CMake] Obtaining a subversion revision number at build time ?

Tristan Carel tristan.carel at gmail.com
Mon Dec 3 15:08:55 EST 2007


On Dec 3, 2007 6:21 PM, Josef Karthauser <joe.karthauser at geomerics.com> wrote:
> Is there a supported method for getting a subversion revision number at
> build time?
>
> I see that I can do it at build tree generation time by using:
>
>        FIND_PACKAGE(Subversion)
>        IF(Subversion_FOUND)
>            Subversion_WC_INFO(${CMAKE_HOME_DIRECTORY} Project)
>            MESSAGE("Current revision is ${Project_WC_REVISION}")
>        ENDIF(Subversion_FOUND)
>
> However if I want to do this in a target it appears that I need to do it
> from a foo.cmake file and call that from the target like so:
>
>         ADD_CUSTOM_TARGET(FOO ALL
>                 COMMAND ${CMAKE_COMMAND} ARGS -P
> ${PROJECT_SOURCE_DIR}/subversion.cmake
>         )
>
> with the file subversion.cmake taking the form:
>
>        FIND_PACKAGE(Subversion)
>        IF(Subversion_FOUND)
>            Subversion_WC_INFO(${CMAKE_HOME_DIRECTORY} Project)
>            MESSAGE("Current revision is ${Project_WC_REVISION}")
>        ENDIF(Subversion_FOUND)
>
> as above.
>
> But if I do this I get the error:
>
>        CMake Error: Error in cmake code at
> C:/Devel/SVN/trunk/Bin/CMake/share/cmake-2.4/Modules/FindSubversion.cmak
> e:57:
>        Command MARK_AS_ADVANCED not scriptable
>
> Is this a limitation that I can work around in some fashion?

Hi Josef,

You are right, this is a limitation. Actually I didn't think this
could be used in that way.

Here are 2 solutions to perform what you need:

1. Use CMake 2.5 as you don't have the error you describe above. It
would be too simple, so you also have to patch  the module
FindSubversion.cmake (I have to fix it on the CVS):

--- CMake-CVS/CMake/Modules/FindSubversion.cmake        2006-10-30
21:30:59.000000000 +0100
+++ /usr/local/share/cmake-2.5/Modules/FindSubversion.cmake
2007-12-03 20:47:00.000000000 +0100
@@ -62,7 +62,6 @@

   MACRO(Subversion_WC_INFO dir prefix)
     EXECUTE_PROCESS(COMMAND ${Subversion_SVN_EXECUTABLE} --version
-      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
       OUTPUT_VARIABLE Subversion_VERSION_SVN
       OUTPUT_STRIP_TRAILING_WHITESPACE)


2. I understand this is quite painful, so here is a piece of code
which do what you need without using FindSubversion module.

# ----------------------------------------------------------------------
FIND_PROGRAM(SVN_EXECUTABLE svn
  DOC "subversion command line client")

MACRO(Subversion_GET_REVISION dir variable)
  EXECUTE_PROCESS(COMMAND ${SVN_EXECUTABLE} info ${dir}
    OUTPUT_VARIABLE ${variable}
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  STRING(REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
    "\\2" ${variable} "${${variable}}")
ENDMACRO(Subversion_GET_REVISION)

Subversion_GET_REVISION(/goinfre/sources/attic ProjectRevision)
MESSAGE(STATUS "Revision is ${ProjectRevision}")
# ----------------------------------------------------------------------

HTH
-- 
Tristan Carel
Music with dinner is an insult both to the cook and the violinist.
http://www.tristancarel.com


More information about the CMake mailing list