[CMake] Problem with generated source and header files

Eric Noulard eric.noulard at gmail.com
Tue Jun 30 08:33:24 EDT 2009


2009/6/30 Jörg Förstner <Joerg.Foerstner at ubidyne.com>:
> Hi Tyler,
>
>> I don't know if linking multiple shared libraries together
>> like that is
>> going to work. You might need to compile libxyz.generated as a static
>> lib.
>
> You're right. Either I'll have to use a static library for libxyz.generated or I manage to add the generated objects to libxyz (the non-generated one) directly, in one step.
>
>> > The following attempt does not work, because the
>> subdirectory "generated" is still empty, when FILE is called:
>>
>> Right, because file() is run at CMake time but you need to work with
>> files generated at make time.
>>
>> >   FILE( GLOB SRCS ${CMAKE_CURRENT_SOURCE_DIR}/generated/*.cpp )
>> >   ADD_LIBRARY( xyz.generated SHARED ${SRCS} )
>> >   ADD_DEPENDENCIES( xyz.generated generate_files )
>>
>> I can't think of a good way to do this (it seems to me you'd have to
>> generate the list at make time and then somehow interpolate the list
>> into the already-written Makefile -- sounds ugly).
>
> How about generating a CMakeLists.txt file at maketime, containing the file list and somehow calling make or even cmake again?

Did you try this:

1) Call your generator at CMake time using ADD_CUSTOM_COMMAND
    (using OUTPUT and DEPENDS argument in order to avoid systematic generation)
    Your generator should generate a "generated.cmake" file containing
     whatever CMake code is needed, may be something like:

     SET(GENERATED_SRC
             generated/....
             generated/....
            )
     ADD_LIBRARY( xyz.generated SHARED ${GENERATED_SRCS} )

    (I  let you figure out the SHARED vs STATIC issue)


2)  Then optionnally include the generated cmake script using

     INCLUDE(generated.cmake OPTIONAL RESULT_VARIABLE GENOK)

     IF(NOT GENOK)
          MESSAGE(STATUS "Please rerun CMake again")
     ENDIF(NOT GENOK)


Now you should be able to

a) run cmake (the first time you run it "Please rerun CMake again"
will be printed out.
b) run make which should (to be verified) rerun CMake automatically because
    generated.cmake has changed (or appeared)

if b) does not trigger the second CMake run then you'll have to run it manually.

Each time your
-- 
Erk
Membre de l'April - « promouvoir et défendre le logiciel libre » -
http://www.april.org


More information about the CMake mailing list