[CMake] Retrieving target's sources and libraries

Brad King brad.king at kitware.com
Fri Dec 14 10:19:51 EST 2007


Rodolfo Lima wrote:
> Hi, I'd like to mention bug #6137 in bug tracker, where I've written a
> patch to cmake (both 2.4 and 2.5-cvs) where I add a target property
> (SOURCES) to get the sources assigned to a target.

Thanks, we're looking at the patch.  We typically have constructed the
set of source files for a target in a variable so they can be used later:

SET(mylib_SOURCES
  mylib1.c
  mylib2.c
  )
ADD_LIBRARY(mylib ${mylib_SOURCES})
SET_SOURCE_FILES_PROPERTIES(${mylib_SOURCES} PROPERTIES ...)

> As I say there, I'm
> using it to properly implement header precompilation because I need to
> add the precompiled header as a dependency of each target's source file.

There is help for you in CVS CMake.  We've added some primitives that
make implementing PCH possible, though there is not yet an interface
making it a first-class feature.

You've already discovered the OBJECT_DEPENDS property.  This is useful
to make sure the PCH is generated before sources using it are compiled.

There is also an OBJECT_OUTPUTS property (CVS CMake).  This is useful to
get dependencies right when the PCH is created by one of the source
files as in the Visual Studio approach to PCH.  Look in
Tests/PrecompiledHeader/CMakeLists.txt in CVS CMake to see its use.

Look at the INCLUDE_DIRECTORIES directory property (see
GET_DIRECTORY_PROPERTY command).  This will help you create a custom
command for GCC-style PCH and get the include path like any other object
file.

Finally, look at the IMPLICIT_DEPENDS option of ADD_CUSTOM_COMMAND (CVS
CMake).  It will allow you to create a custom command for creating a
GCC-style PCH and get implicit dependency scanning like any other object
file.

-Brad


More information about the CMake mailing list