[CMake] target to copy include files?

Michael Wild themiwi at gmail.com
Sun Dec 21 07:45:03 EST 2008


On 20. Dec, 2008, at 1:09, Brad Aisa wrote:

> I would like to create a target that will copy (or link) a big whack  
> of include files defined in my project to a local /include folder.  
> (They start life in a bunch of subfolders, and some of them are  
> generated programmatically during the build.) Note that we already  
> have an install-time process, what I want is to copy (or freshen, or  
> link) all these include files automatically, as the last step in the  
> build. I have the files in lists, ex. ta_HEADS css_HEADS etc. Thanks!

Do you really need to copy them, or would it be enough to have a  
"wrapper" which #include's the actual file using a relative path?  
Because that is what I do. Below macro takes a destination directory  
and a bunch of header files and then, in ${CMAKE_CURRENT_BINARY_DIR}/ 
include/${__DESTDIR} configures a wrapper-template which #include's  
the original file. Of course, you could replace the configure step by  
a EXECUTE_PROCESS command where you do "${CMAKE_COMMAND} -E  
copy_if_different ${_ho} ${_hl}".


macro( CREATE_INCLUDE_WRAPPERS __DESTDIR )
   # destination directory
   set( _d ${CMAKE_BINARY_DIR}/include/${__DESTDIR} )
   # loop over headers
   foreach( _h ${ARGN} )
     # basename
     get_filename_component( _hb ${_h} NAME )
     # full path of the include wrapper
     set( _hl ${_d}/${_hb} )
     # check whether the header has been specified with an absolute path
     if( IS_ABSOLUTE ${_h} )
       # if so, make it relative to the current source directory
       file( RELATIVE_PATH _hr ${CMAKE_CURRENT_SOURCE_DIR} ${_h} )
     else( IS_ABSOLUTE ${_h} )
       set( _hr ${_h} )
     endif( IS_ABSOLUTE ${_h} )
     # relative path of the header w.r.t the destination directory
     file( RELATIVE_PATH _ho ${_d} ${CMAKE_CURRENT_SOURCE_DIR}/${_hr} )
     # used to configure the wrapper template
     set( CONFIG_HEADER_FILE ${_ho} )
     # configure the wrapper
     configure_file( ${CMAKE_SOURCE_DIR}/CMake/Modules/ 
includeWrapperTemplate.H.in ${_hl} @ONLY )
   endforeach( _h )
endmacro( CREATE_INCLUDE_WRAPPERS )

The includeWrapperTemplate.H.in looks like this:


/ 
*----------------------------------------------------------------------------*\
     THIS FILE WAS AUTOMATICALLY GENERATED BY CMAKE.
     DO NOT EDIT! YOUR CHANGES WILL BE LOST!

     This is an include-wrapper which the CMake build system  
configures for
     all header files. The generated files are only used during the  
build
     process and do not get installed.
\*----------------------------------------------------------------------------*/

/*
  * No include guards, some headers might need to be included
  * multiple times (e.g. to #undef some helper macros).
  */

#include "@CONFIG_HEADER_FILE@"


The advantage of this is, that you don't have to worry about updating  
the wrappers, as CMake takes care of this. Only thing which doesn't  
work, is when you remove a header. Then the wrapper is left lying  
around.

I hope this helps


Michael


More information about the CMake mailing list