[CMake] Build Library from Thrift generated files

Pascal Bach pasci.bach at gmail.com
Wed Sep 4 16:29:57 EDT 2013


Hello Eric

On Wed, Sep 4, 2013 at 11:26 AM, Eric Noulard <eric.noulard at gmail.com> wrote:
>
> 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?

Currently there is no way toget just a list of the generated output
file. One idea that I had was to just run the
process once to get a list of the generated output files and then
throw the files away. If I wrap this in a function
it would be similar to the generated-file-list. But it seems like a waste to me.

Because the output of the generator is dependent on the content of the
file it is not possible to specify an output file, only an output
directory.

>
> 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.
>

Your suggestion is interesting.
What I don't understand is what triggers the
ThriftGeneratedFiles.cmake to be recreated. Is it right that this file
gets recreated whenever a .thift file changes?
I will try to implement something like this tomorrow and see if can
get it to work.

Regards
Pascal


More information about the CMake mailing list