[CMake] Invoke a target on a separate project in a generic way?

Bill Hoffman bill.hoffman at kitware.com
Thu Dec 4 08:50:47 EST 2008


Hugo Heden wrote:
> Good day all,
> 
> I am looking for a way within a CMakeLists.txt make sure that:
> 
> -- at *build* time
> 
> -- cmake is invoked on a *separate* project, and then also the
> "all"-target is invoked
> 
> How do I do that?
> 
> The following is a first rough sketch (that is not good enough, I believe):
> 
> ADD_CUSTOM_TARGET(
>   build_separate_project
>   WORKING_DIRECTORY separate_project/build
>   COMMAND ${CMAKE_COMMAND} ../src
>   COMMAND ${CMAKE_BUILD_TOOL} all
> )
> 
> -- The above only works for a ${CMAKE_BUILD_TOOL} that happens to
> accept a commandline that contains a target and nothing else (like
> GNU-make does).
> 
> So is there a more *generic* way to, at build time, invoke cmake and
> then the "all"-target on a separate project?
> 
> (The following is a bit of background: We have a project called "FOO"
> that generates source code that eventually also needs to be compiled.
> The generated source files end up in a *directory* that is known
> CMake-time, but the *filenames* are not known until *build*-time. Thus
> there is no way with in project "FOO" to create a target to compile
> the generated source-code. (Doing FILE(GLOB...) within project FOO
> will not work, because the globbing will take place at cmake-time, at
> which time there are no files there). Therefore, together with the
> generated source files we generate a CMakeLists.txt that defines a
> *separate* project called "BAR" that contains a target to compile the
> generated source code. Hence the question above -- how do I invoke the
> all target in the BAR project in a way that works for all build-tools,
> not only GNU-make?)
> 

You should look at ctest --build-and-test.  It can do what you want, and 
invoke all build systems supported by CMake.  It is used in the testing 
of CMake.  See CMake/Tests/CMakeLists.txt, and ctest --help for information.


-Bill


More information about the CMake mailing list