[CMake] Build Library from Thrift generated files

Eric Noulard eric.noulard at gmail.com
Wed Sep 4 05:26:01 EDT 2013


2013/9/4 Pascal Bach <pasci.bach at gmail.com>:
> Hello everybody
>
> I'm trying to build a library from source files that are generated at build
> time. The setup is the following.
>
> Inputfiles (Service.thrift)   -> Thrift Compiler/Generator -> Several
> generated files (gen-cpp/Service.h, gen-cpp/Service.cpp, gen-cpp/Types.h,
> [possibly more] -> Build Library -> Library (thrift-service.so)

Unknown source file name is just painful.

Can't the thrift generator tell you the name of the files he will generate?
Something like:
thrift --generated-file-list Service.thrift

or may be you can teach him the name of the flie you want?

If neither of this apply then you'll have to discover the source file yourself
(and file a feature request to thrift compiler guys :-] ).


> The idea is to rebuild the library whenever one of the Service.thrift file
> changes.
> The problem is that I don't know the output of the generated files in
> advanced so I like to use all the files generated by the generator and pack
> them in a library.
>
> I tried to use add_custom_command() but as I don't know what's the output of
> the generator I didn't succeed.

May be you can use a 2 steps approach.
Your custom command should generate

1) the source file
2) a cmake file which contains the list of the generated sources files,
    the OUTPUT of the custom command will be that file.

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/ThriftGeneratedFiles.cmake
                                   COMMAND thrift ARGS -o
${CMAKE_CURRENT_SOURCE_DIR}/ThriftGenerated Service.thrift
                                   COMMAND ${CMAKE_COMMAND} ARGS
-DSRCDIR=${CMAKE_CURRENT_SOURCE_DIR}/ThriftGenerated -P
${CMAKE_CURRENT_SOURCE_DIR}/GetThriftGeneratedFiles.cmake
                                   DEPENDS Service.thrift)

# This will include the file if it exists
include( ${CMAKE_CURRENT_SOURCE_DIR}/ThriftGeneratedFiles.cmake OPTIONAL)

Now the script "GetThriftGeneratedFiles.cmake" should be a CMake scripts which
knows that "SRCDIR" contains all the generated files, from which it can build
ThriftGeneratedFiles.cmake.

In the end ThriftGeneratedFiles.cmake content may be something like:

set(THRIFTGEN_SRC
      gen-cpp/Service.h, gen-cpp/Service.cpp, gen-cpp/Types.h)

add_library(thrift-service
                ${THRIFTGEN_SRC})

You may have to initially build twice in order to bootstrap the
missing "ThriftGeneratedFiles.cmake".

I did not test this scheme but I guess it should work.


>
> Is there a way to handle this scenario?
>
> Regards
> Pascal
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



-- 
Erk
L'élection n'est pas la démocratie -- http://www.le-message.org


More information about the CMake mailing list