[CMake] [CPack] replace files in source tree when packaging sources

Thomas Richard Thomas.Richard at imgtec.com
Fri Aug 23 06:41:06 EDT 2013


Hi all,

I thought it would be interesting to share what I found about CPack.

I am trying to use CPack to package my source files. Some of our headers are generated at compilation time from scripts. I am trying to take the generated headers (from the binary defs folder) instead of the scripts in my source defs folder using cpack source package functionality. The client folder contains some different CMake files to use when the headers are present instead of generated, this folder should be used instead of the source defs folder too.
 
To do so one has to use a variable that is not in the documentation (or that I did not find) that I found by chance: CPACK_SOURCE_INSTALLED_DIRECTORIES. This variable expects a pair of folder names. But for it to work one needs to include the top-level folder too.
The other variable to use is CPACK_SOURCE_IGNORE_FILE to ignore the source folder (this variable is documented).

Top level CMakeLists:

set(CPACK_SOURCE_IGNORE_FILE "~$" "${CMAKE_CURRENT_SOURCE_DIR}/defs/")
set(CPACK_SOURCE_INSTALLED_DIRECTORIES 
   "${CMAKE_CURRENT_SOURCE_DIR}" "."
   "${CMAKE_CURRENT_BINARY_DIR}/defs/generated/" "defs/include/"
   "${CMAKE_CURRENT_SOURCE_DIR}/defs/client/" "defs/"
)
 include(CPack)

# add subfolders and things

# as the headers are generated at compile time we need an extra rule to insure they are generated before packing
add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
add_dependencies(dist defs) # dist depends on the generation of the headers

So now I can run "make dist" and it will use my binary files instead of the source files when creating the package.

I hope this will help other people that may have struggle searching on the web on how to replace a file when packaging sources with CPack.

Thomas RICHARD




More information about the CMake mailing list