On Thu, Mar 20, 2008 at 3:27 AM, Vandenbroucke Sander <
[EMAIL PROTECTED]> wrote:

>
> I also struggled with getting a SVN revision into my source code.
> By the looks of this my solution can be improved :-)
>
> > CMakeLists.txt:
> > =============
> > CONFIGURE_FILE(dump_svnversion.sh.in dump_svnversion.sh)
> > ADD_CUSTOM_TARGET(OutputSvnVersion ALL
> >         ${CMAKE_CURRENT_BINARY_DIR}/dump_svnversion.sh)
> >
> > # allow including the current_svnversion.h file which in this example is
> generated in the binary directory
> > INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
> >
> SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/current_svnversion.h
> >    PROPERTIES GENERATED true)
> > ADD_EXECUTABLE(foo
> >    foo.cc
> >    ${CMAKE_CURRENT_BINARY_DIR}/current_svnversion.h)
> >
> > dump_svnversion.sh.in:
> > =================
> > #!/bin/sh
> > [EMAIL PROTECTED]@/svnversion_tmp
> >
> > echo "#ifndef @[EMAIL PROTECTED]" >  $FILE
> > echo "#define @[EMAIL PROTECTED]" >> $FILE
> > echo "#define @[EMAIL PROTECTED] \"`svnversion
> @[EMAIL PROTECTED]"" >> $FILE
> > echo "#endif" >> $FILE
> >
> > @CMAKE_COMMAND@ -E copy_if_different
> @CMAKE_CURRENT_BINARY_DIR@/svnversion_tmp
> @CMAKE_CURRENT_BINARY_DIR@/current_svnversion.h
>
> This is verry unix like solution. I used a Python script with the PySvn
> plug-in. For me it works on both XP and Linux.
>
> First I tried a CMake macro as in:
> ADD_CUSTOM_TARGET(OutputSvnVersion ALL
>          ${MY_SVN_MACRO})
>
> Which obviously doesn't work. It should be a nice feature to have, in my
> opinion. That way there is no/less need for using external scripts.


I think the feature you're asking for (i.e. invocation of a macro which
could do anything at build time, not configure time) would likely be slow as
molasses.  It would require CMake to be run to pull in all of your other
existing variables and macros and then to invoke your custom macro because
you would likely want your custom macro to be able to use variables and
other functions/macros defined throughout your project.  In theory it sounds
possible.  One of the CMake architects might be able to answer how feasible
it would be to support.  It would as slow as it takes to configure your
build though (which wouldn't be acceptable for the use case we're talking
about here).

Here's an alternative for you that might work better.

You can use "cmake -P" though to run an arbitrary CMake script though during
runtime (when you think about it this is a lot like a macro).  The major
issue is of course that none of the variables you typically have defined are
available when invoking your custom cmake script.  I came up with a way to
do the svnversion command via a "cmake -P" script but it has the minor
downside of generating the header file within the source tree.

I'm sure someone on this list could come up with a way to fix this but I
haven't thought of anything yet.  I tried using "cmake -D" to set needed
variables but for some reason that didn't seem to work for me while invoking
CONFIGURE_FILE().  In any case, gaining cross-platform compatibility at the
cost of a single generated header file in the source tree probably isn't too
big of a deal to most.

extract_svnversion.cmake
============
# Execute with "cmake -P" using ADD_CUSTOM_TARGET with a WORKING_DIRECTORY
# of ${CMAKE_CURRENT_SOURCE_DIR} or wherever you want svnversion ran from.
#
SET(CMAKE_BACKWARDS_COMPATIBILITY 2.4)

EXECUTE_PROCESS(COMMAND svnversion .
                OUTPUT_VARIABLE version)
STRING(REGEX REPLACE "\n" "" version_stripped ${version})
CONFIGURE_FILE(current_svnversion.h.in current_svnversion.h)

current_svnversion.h.in:
===================
#ifndef CURRENT_SVNVERSION_H
#define CURRENT_SVNVERSION_H

   #define CURRENT_SVNVERSION "@version_stripped@"

#endif


CMakeLists.txt
=============
ADD_CUSTOM_TARGET(OutputSvnVersion ALL
          COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_SOURCE_DIR}/extract_svnversion.cmake
          WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )

SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/current_svnversion.h
    PROPERTIES GENERATED true)
ADD_EXECUTABLE(foo
    foo.cc
    current_svnversion.h)

-- 
Philip Lowman
_______________________________________________
CMake mailing list
[email protected]
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to