[CMake] How to regenerate source file during make?

Michael Wild themiwi at gmail.com
Fri Jan 15 10:35:24 EST 2010


On 15. Jan, 2010, at 16:25 , Marcel Loose wrote:

> On Fri, 2010-01-15 at 10:07 -0500, David Cole wrote:
>> On Fri, Jan 15, 2010 at 9:44 AM, Marcel Loose <loose at astron.nl> wrote:
>>        Hi Jack,
>> 
>>        The difference with your and my case is that you have an input
>>        file
>>        (the .xsd file) and output files (the .h and .cpp files). In
>>        my case I
>>        generate the output file, e.g. myheader.h, from myheader.h.in,
>>        using
>>        configure_file(). The problem is that I cannot get this to
>>        work during
>>        'make', only during 'cmake'.
>> 
>>        Best regards,
>>        Marcel Loose.
>> 
>>        On Fri, 2010-01-15 at 15:04 +0100, Smith Jack (Ext. - UGIS -
>>        UniCredit
>> 
>>        Group) wrote:
>>> I do a similar thing with an XSD file from which I generate
>>        c++
>>        wrapper classes for accessing the corresponding XML
>>> 
>>> Note the DEPENDS line !
>>> 
>>> MACRO(XSD FILE NAMESPACE)
>>>    ADD_CUSTOM_COMMAND(
>>>        OUTPUT  ${CMAKE_CURRENT_SOURCE_DIR}/xml/${FILE}.cpp
>>>        OUTPUT  ${CMAKE_CURRENT_SOURCE_DIR}/xml/${FILE}.h
>>>        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/xml/${FILE}.xsd
>>>        COMMAND ${XAPI_CODESYNTH}/bin/xsd
>>>        ARGS cxx-tree --generate-serialization
>>        --hxx-suffix .h
>>        --cxx-suffix .cpp --fwd-suffix -fwd.hxx --type-naming java
>>        --function-naming java --namespace-map =${NAMESPACE}
>>        --generate-polymorphic --output-dir
>>        ${CMAKE_CURRENT_SOURCE_DIR}/xml
>>        ${CMAKE_CURRENT_SOURCE_DIR}/xml/${FILE}.xsd
>>>        COMMENT "generating Codesynthesis Wrappers")
>>> ENDMACRO(XSD)
>>> 
>>> 
>>> 
>>>> -----Original Message-----
>>>> From: cmake-bounces at cmake.org
>>>> [mailto:cmake-bounces at cmake.org] On Behalf Of Marcel Loose
>>>> Sent: Friday, January 15, 2010 2:55 PM
>>>> To: cmake at cmake.org
>>>> Subject: [CMake] How to regenerate source file during
>>        make?
>>>> 
>>>> Hi all,
>>>> 
>>>> I've been struggling with this for hours now and I can't
>>        seem
>>>> to get a grip on it.
>>>> 
>>>> I have a header file that must be (re)generated during
>>        'make'
>>>> when some external data have changed. I can successfully
>>>> generate this file during 'cmake' using configure_file(),
>>        but
>>>> I haven't been able to figure out how I can trigger the
>>>> configure_file() during make.
>>>> 
>>>> Should I define a target for this header file? Or can I
>>>> specify some kind of file dependency, such that when the
>>>> header file is missing our out-of-date, it will be
>>        (re)generated?
>>>> 
>>>> I guess it must be some clever combination of
>>>> add_custom_command() and add_custom_target().
>>>> 
>>>> Best regards,
>>>> Marcel Loose.
>> 
>> 
>> 
>> This should work already: if you change my_header.h.in then cmake
>> should re-run to re-configure your file at 'make' time.
>> 
>> 
>> If you have additional dependencies that should trigger a
>> re-configure, you should probably convert it to an add_custom_command
>> such that it happens at build time.
> 
> It's not the myheader.h.in that's changing; myheader.h should be
> regenerated because the value of one of the variables in myheader.h.in
> (that get expanded) change. 
> 
> To be more precise, I try to record the svn revision number (and some
> more svn info). I carefully read
> http://www.mail-archive.com/cmake@cmake.org/msg25922.html, because it
> closely matches my problem, but I couldn't figure out what
> update_version.cmake should do exactly. In my case, I would need the
> values of a number CMake variables (like CMAKE_CURRENT_BINARY_DIR), but
> of course, these values are not around anymore when running 'make'.
> Should all these variables be passed using -D?
> 
> Best regards,
> Marcel Loose.

You configure_file a CMake script at CMake time which you then use in an add_custom_command to run at make-time. So you put all the "static" data you need (such as CMAKE_BINARY_DIRECTORY, etc.) in the script file and then you have them available at make time:

some_script.cmake:
------------------
set(CMAKE_SOURCE_DIR "@CMAKE_SOURCE_DIR@")
set(CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@")
set(SVN_EXECUTABLE "@SVN_EXECUTABLE@")
# ... do what you need to to ...
-------------------


But I still don't see why you have to extract version-information at build time, because IMHO that shouldn't change between CMake-runs.

Michael



More information about the CMake mailing list