6

I have a cmake project which one of the install targets is a collection of files. This files change depending on the configuration (Release, Debug...). I would like to be able to install the files like so:

install(DIRECTORY $<TARGET_FILE_DIR:tgt>
    DESTINATION bin
    COMPONENT files)

But cmake does not support that. Generator variables do not apply to DIRECTORY. So I was wondering if there is a way to either save the directory somewhere. Either the cache or a file and then load it into cpack.

So I guess the question is how to pass a variable from cmake to cpack?

Mac
  • 3,397
  • 3
  • 33
  • 58
  • According to `install` [documentation](https://cmake.org/cmake/help/v3.4/command/install.html#installing-directories), only `DESTINATION` option can use *generator expressions*, `DIRECTORY` option **cannot use them**. – Tsyvarev Feb 26 '16 at 08:55
  • @Tsyvarev yeah I know that. Do you have any idea to fix my problem? – Mac Feb 26 '16 at 19:19

4 Answers4

7

This is a rather late answer, but I happened upon this question trying to solve a somewhat different problem that could also be summarized as: "How do I pass a variable to CPack?" In my case, I was making this call from a customized version of CPackDeb.cmake copied to my workspace:

find_program(OPKG_CMD NAMES opkg-build HINTS "${OPKG_HINT}")
#                                             ^^^^^^^^^^^^
#                                 This is what I wanted to pass to CPack 

I was setting OPKG_HINT in a file included from my top-level CMakeLists.txt, but it was not getting passed through to cpack; the above find_program() invocation was seeing an empty string for OPKG_HINT.

The solution turned out to be stupid simple: just prepend CPACK_ to the variable name!

If I do this in CMakeLists.txt:

set(CPACK_OPKG_HINT "${_sysroot_top}/aarch64-poky-linux/usr/bin")

then I can put this in my CPackDeb.cmake file and it works fine:

find_program(OPKG_CMD NAMES opkg-build HINTS "${CPACK_OPKG_HINT}")

Anyway, this wound up being a bit of an X-Y problem for the OP, but... if you really need to set a variable at CMake time in such a way that it's accessible to cpack, prefixing the variable name with CPACK_ seems to do the trick nicely...

evadeflow
  • 4,704
  • 38
  • 51
  • Thanks for that answer.. I was struggling alot. This works – AnotherDeveloper Sep 02 '20 at 13:21
  • 1
    Do you have a reference for this in CMAKE's/CPACK's documentation? Or might this be just an implementation detail (I'd rather not depend on those)? – ricab Oct 11 '21 at 15:13
  • 1
    @ricab, I couldn't find anything in the docs, so I asked about it on the CMake Discourse Forum: https://discourse.cmake.org/t/why-are-cpack-variables-documented-in-two-different-places/1525/4. Brad King of Kitware replied that "it should be stable behavior", so... I guess it's safe to depend on this. – evadeflow Oct 14 '21 at 14:14
2

The following setup work if you use a "single-configuration generators (such as make and Ninja)" and call CMake with

cmake -DCMAKE_BUILD_TYPE=Release <source_dir>

https://cmake.org/cmake/help/v3.0/variable/CMAKE_BUILD_TYPE.html

You can define the ${dir} variable in another way if you like.

IF (CMAKE_BUILD_TYPE STREQUAL "Release")
    SET(dir release_dir)
ELSE()
    SET(dir debug_dir)
ENDIF()

INSTALL(DIRECTORY ${dir} DESTINATION bin COMPONENT files)
Emil
  • 456
  • 4
  • 9
2

CMake 3.5 supports generator expressions for the DIRECTORY arguments. See installing directories.

sakra
  • 62,199
  • 16
  • 168
  • 151
2

Until now this seems to be the best answer (from someone on the cmake mail list)

install(DIRECTORY path/to/Debug/dir
  DESTINATION bin
  CONFIGURATIONS Debug
  COMPONENT files
)

install(DIRECTORY path/to/Release/dir
  DESTINATION bin
  CONFIGURATIONS Release
  COMPONENT files
)
Mac
  • 3,397
  • 3
  • 33
  • 58