[CMake] dependency, custom command/target and parallel build.

Alexander Neundorf a.neundorf-work at gmx.net
Thu Apr 9 15:41:49 EDT 2009


Hi,

On Wednesday 08 April 2009, Dhadi S wrote:
> Hi,
> I'm new to cmake. Here is the version i'm using on a linux box.
>
> cmake version 2.6-patch 3
>
> In my main directory i've the fllowing in my cmakelists.txt file.
> assume my main directory to be  /home/srini
>
> SET (XDR_FILES_HOME /home/thirdparty/xdr )
> SET (XDR_FILES
>     ${XDR_FILES_HOME}/xdrPrt.h
>     ${XDR_FILES_HOME}/xdrPrtBuf.c
>     ${XDR_FILES_HOME}/xdrPrt.c
>  )
>
> add_subdirectory (lib0 )
> add_subdirectory (lib1)
>
> Notice that the xdr files are not in my project heirarchy.
> The source files in both lib0 and lib1 depend on xdrPrt.h file generated at
> run time.
>
> In   lib0/cmakelists.txt file i've
>
>  ADD_CUSTOM_COMMAND(
>    OUTPUT ${XDR_FILES}
>    COMMAND  my_xdr_gen.exe
>    WORKING_DIRECTORY ${XDR_FILES_HOME}
>    )
>
> ADD_CUSTOM_TARGET(lib0_xdr ALL DEPENDS ${XDR_FILES} )
> add_library (LIB0_LIB SHARED ${XDR_FILES} lib0_src.c )
>
> and in lib1/cmakelists.txt file,  i almost repeat the same.
> ADD_CUSTOM_COMMAND(
>    OUTPUT ${XDR_FILES}
>    COMMAND  my_xdr_gen.exe
>    WORKING_DIRECTORY ${XDR_FILES_HOME}
>    )
>
> ADD_CUSTOM_TARGET(lib1_xdr ALL DEPENDS ${XDR_FILES} )
> add_library (LIB1_LIB SHARED ${XDR_FILES} lib1_src.c )
>
>
> The
> above works for me (i can't seem to move the common code to main
> cmakelists.txt file) when i do gmake in the build dir. In
> my_xdr_gen.exe i added print statement to make sure it was called.
> However when i do  gmake -j4  i see 2 print statements from it.  As if
> both lib0 and lib1 called custom command at the same time.
>
> Here are my questions, since i'm newbie and i'm sure what i'm doing is not
> correct.
>
> How can i not duplicate code in lib0/cmakelists.txt and lib1/cmakelists.txt
> I want to call my custom command just once and i want the lib0 and lib1
> comilation to wait for the generation of my XDR_FILES. And i should be able
> to do parallel build also.

I think this should work if you use add_custom_target(), targets are global.
So something like:

add_custom_target(xdrFiles
                  COMMAND ... )

Then in the subdirs:

add_library(lib1 ...)
add_dependencies(lib1 xdrFiles)

Alex


More information about the CMake mailing list