# Conditionally install file depending on CPack generator

**URL:** https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482
**Category:** Usage
**Created:** [September 13, 2022, 10:30pm UTC](https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482 "2022-09-13T22:30:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mrjoel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrjoel/32/2770_2.png) [@mrjoel](https://discourse.cmake.org/u/mrjoel)
#### Post date: [September 13, 2022, 10:30pm UTC](https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482/1 "2022-09-13T22:30:18Z")

</div>

I am seeking to generate Linux packages (DEB;RPM) with integration into distribution specific locations via custom file installation. I would prefer to only include the integration file for certain CPack generator types, and the system integration files need to be installed outside of the install prefix to distribution defined locations.

Since several external libraries are vendored/bundled, our package uses `CPACK_PACKAGING_INSTALL_PREFIX=/opt/MyPackage`, and assume the required integration file destination as:

RPM: `/etc/FooBar/conf.d/mypackage.conf`  
DEB: `/etc/foobar/config.d/mypackage.conf`

The best alternative I have been able to come up with is to universally `install()` a `share/doc/examples/mypackage.conf` file and copy it into the desired location using a post-install and pre-uninstall script. That seems quite clumsy to maintain, and doesn’t result in the integration file being reported as part of package contents when listing package files.

Is there a better way to accomplish this?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 14, 2022, 8:58pm UTC](https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482/2 "2022-09-14T20:58:57Z")

</div>

Unfortunately, there’s nothing I know of that allows this. Note that it is harder than that because sometimes the layout is different based on the generator (e.g., macOS `.app` bundles versus a Unix-like installation) which can change your rpath needs as well).

---

<div class="post-metadata">

### Author: ![mrjoel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrjoel/32/2770_2.png) [@mrjoel](https://discourse.cmake.org/u/mrjoel)
#### Post date: [September 14, 2022, 9:34pm UTC](https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482/3 "2022-09-14T21:34:54Z")

</div>

Would a feature request for something like this be considered? As an initial design stab an initial design option may be an additional condition option on `install()` similar to `CONFIGURATIONS`, resulting in something like below.

```auto
configure_file("${PROJECT_SOURCE_DIR}/mypackage.conf.in" "${PROJECT_BINARY_DIR}/mypackage.conf)

# Use RPM specific integration location
install(FILES "${PROJECT_BINARY_DIR}/mypackage.conf"
        CONFIGURATION Release
        CPACK_DESTINATION "/etc/FooBar/conf.d"
        CPACK_GENERATOR RPM)

# Use DEB specific integration location
install(FILES "${PROJECT_BINARY_DIR}/mypackage.conf"
        CONFIGURATION Release
        CPACK_DESTINATION "/etc/foobar/config.d"
        CPACK_GENERATOR DEB)

# Include example for manual reference select other packages
install(FILES "${PROJECT_BINARY_DIR}/mypackage.conf"
        CONFIGURATION Release
        DESTINATION "usr/share/MyPackage/examples"
        CPACK_GENERATOR "ZIP;TGZ;TZST")

```

Note the different usage of `DESTINATION` vs. the hypothetical `CPACK_DESTINATION`. I presume something like that would be required since running the actual install phase during CPack execution shouldn’t try to write to the actual full path, but be independent from install prefix when installing to `_CPACK_PACKAGES/X/Y` scratch directories. It would seem to be needed to have some mechanism to specify an absolute path-in-resulting-package. That may become overly messy however, so perhaps there’s a cleaner approach. On the other hand, being a bit more verbose and tedious for such cases may help continue usage of prefix variables instead of specifying absolute locations?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 16, 2022, 1:04pm UTC](https://discourse.cmake.org/t/conditionally-install-file-depending-on-cpack-generator/6482/4 "2022-09-16T13:04:24Z")

</div>

Given how other things work with `install()`, something like this might work better:

```cmake
install(FILES "${PROJECT_BINARY_DIR}/mypackage.conf"
  DESTINATION "share/MyPackage/examples" # default
  CPACK # if any match, ignore other `DESTINATION` bits
    RPM_DESTINATION "/etc/FooBar/conf.d"
    DEB_DESTINATION "/etc/foobar/config.d")

```

This should also work with things like (just an example as `CMAKE_INSTALL_LIBDIR` handles this specific case anyways):

```cmake
install(TARGET somelibrary
  ARCHIVE
    DESTINATION "lib" # default
    CPACK # if any match, ignore other `DESTINATION` bits
      RPM_DESTINATION "lib64"
      DEB_DESTINATION "lib/${triple}")

```

But it sounds like it could be useful, so [a feature request](https://gitlab.kitware.com/cmake/cmake/-/issues) would be fine with me.
