Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying files and including them in a CPack archive

Tags:

cmake

cpack

So, there are numerous ways to copy files (and directories) at CMake runtime (file(COPY ...), configure_file(...) and add_custom_command() all work*), but I haven't yet found out how to make a file or directory copied from the source to build directory appear in an archive generated by CPack. I though that this SO answer would fix it, as it actually links the copying to a target which will then have an install linked to it:

install(TARGET mytarget DESTINATION bin)

whereas I did recognise that the file() and configure_file() commands don't have an obvious way to be added to a target. But, this didn't work. So, given a simple CMakeLists.txt, such as the one below, how do I make all of the files (including the exmaple directory) appear in the archive?!

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
enable_language(FORTRAN)
add_executable(mytarget ${PROJECT_SOURCE_DIR}/myprog.for)
install(TARGETS mytarget DESTINATION bin)

add_custom_command(TARGET mytarget PREBUILD
                COMMAND ${CMAKE_COMMAND} -E copy_directory
                  ${PROJECT_SOURCE_DIR}/examplefiles ${PROJECT_BINARY_DIR}/examplefiles)

set(CPACK_GENERATOR "TGZ")
include(CPack)

* I haven't yet found out which one of these 3 methods is actually (most?) correct - so any advice on this too will be hugely appreciated

like image 666
ChrisW Avatar asked Jul 05 '13 19:07

ChrisW


People also ask

When would you use a CPack?

CPack is a tool included with CMake, it can be used to create installers and packages for projects. CPack can create two basic types of packages, source and binary. CPack works in much the same way as CMake does for building software.

How does a CPack work?

CPack works to create a duplicate of the source tree for the project and tar or zip file, and you can transfer the file to another machine and store them in the correct directory and have your project up and running.

What is CPack generator?

Introduction. The CPack module generates the configuration files CPackConfig. cmake and CPackSourceConfig. cmake . They are intended for use in a subsequent run of the cpack program where they steer the generation of installers or/and source packages.


1 Answers

As explained in the documentation of the CPack module, the binary installers created by CPack contain everything installed via CMake's INSTALL command. Thus the executable mytarget in your example will be included in the CPack archive, because you use the install command to copy it to the bin folder.

To also have CPack include the example folder in the generated archive, you can use the DIRECTORY variant of the install command in the following way:

install(DIRECTORY "${PROJECT_SOURCE_DIR}/examplefiles/" DESTINATION "example")

The file(COPY ...) and configure_file(...) do not have an effect on what is installed by CPack. Both command are usually used to copy files from the source tree to the binary tree upon configuring the CMake project.

Using add_custom_command with ${CMAKE_COMMAND} -E copy_directory ... will postpone the actual copying of files to the build time of the project. It will however not trigger the inclusion of the copied files in the CPack archive, either.

like image 77
sakra Avatar answered Sep 18 '22 07:09

sakra