[CMake] Version embedding - how to cause a command when make is run?

Omar Souka cmake at souka.com
Thu Oct 4 14:15:01 EDT 2007


On 10/4/07, Atwood, Robert C <r.atwood at imperial.ac.uk> wrote:
>
> Hi:
> I have applied Cmake to a pre-existing project that used to use plain
> make-files that I crafted with great amounts of hair-tearing, and so far
> cmake was relatively easy and I am now a convert :=> Maybe my hair will
> even grow back...
>
>
> However, there's a little thing I have not quite figured out. I have had
> a policy of trying to keep enough information embedded in the executable
> to ensure that the exact code used to build it may be obtained, using a
> script embedded in the Makefile to place some strings, provided by
> version control software etc., into the code.


I do this:

Create a custom target that creates a header file based running a script:

    add_custom_target(version_header_file
        COMMAND perl5 GenerateVersion.pl
        COMMENT "Generating version info"
        WORKING_DIRECTORY ${util_dir}
    )

The perl script does something like this:

open SVN, "$svn info |" || die;

while (<SVN>) {
    if (/Revision/) {
        s/Revision: //;
        $svn_ver = $_;
    }
}

and then modifies the header file if the version has changed (it writes the
header to a temp file and diffs the temp file with the real header, and
overwrites the header if there is a change).  Writing to a temp file and
comparing the files allows the script to run for every build, but it only
causes recompiles if the version has changed.  This approach works on both
Unix and Windows.  On Windows, we take the svn rev number and use it in the
windows version resource.  So the official windows version is something like
a.b.c.svn rev number

Regards,

Omar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/cmake/attachments/20071004/dfb36441/attachment.html


More information about the CMake mailing list