# Installing CMake package config files for MacOS frameworks

**URL:** https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849
**Category:** Code
**Created:** [March 24, 2020, 10:07am UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849 "2020-03-24T10:07:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [March 24, 2020, 10:07am UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/1 "2020-03-24T10:07:33Z")

</div>

I created a versioned MacOS framework with CMake and stumbled over the question where to install the package config files `sample-config.cmake`, `sample-config-version.cmake`, `sample-exports.cmake`, `sample-exports-release.cmake`.

The framework is versioned so the structure is roughly as follows:

```
<sample.framework>
   |-- Headers # link to Versions/Current/Headers
   |-- Resources # link to Versions/Current/Resources
   +-- Versions
       |-- Current # link to B
       +-- B
           |-- Headers
           +-- Resources
               |-- CMake
               +-- Info.plist

```

I use CMakes `CMakePackageConfigHelpers` macros to generate a package config file from a template `sample-config.cmake.in` with the content

```
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/sample-export.cmake")

```

CMake does not mention versioned frameworks and recommends to install the files into the directory `Resources/CMake`.

I did so by calling

```
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sample-config.cmake"
  DESTINATION sample.framework/Resources/CMake
  COMPONENT development
)

```

The generated file looks like this:

```
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was sample-config.cmake.in ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file)
  set(${_var} "${_file}")
  if(NOT EXISTS "${_file}")
    message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  endif()
endmacro()

macro(check_required_components _NAME)
  foreach(comp ${${_NAME}_FIND_COMPONENTS})
    if(NOT ${_NAME}_${comp}_FOUND)
      if(${_NAME}_FIND_REQUIRED_${comp})
        set(${_NAME}_FOUND FALSE)
      endif()
    endif()
  endforeach()
endmacro()

include("${CMAKE_CURRENT_LIST_DIR}/sample-export.cmake")

```

**Error:**

There’s a difference in the generated config file whether I install those files to `Resources/CMake` or to `Versions/B/Resources/CMake` although both point to the same directory.

When installing via the `Resources` link to `Resources/CMake` the call to set `PACKAGE_PREFIX_DIR` is generated as

```
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

```

but when installing via `Versions/B/Resources/CMake` it is generated as

```
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../../../" ABSOLUTE)

```

As a consequence of this the package cannot be found by `find_package(sample CONFIG)` when installing to `Versions/B/Resources/CMake`.

Did anybody was facing this error before?

Thanks for any input on 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: [March 24, 2020, 4:40pm UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/2 "2020-03-24T16:40:05Z")

</div>

It seems like also searching `Versions/B` for `Resources/CMake` is a reasonable thing to do to me. Thoughts @brad.king?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 24, 2020, 5:42pm UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/3 "2020-03-24T17:42:26Z")

</div>

The [find\_package search procedure](https://cmake.org/cmake/help/v3.17/command/find_package.html#search-procedure) _does_ search `<prefix>/<name>.framework/Versions/*/Resources/CMake/` inside frameworks. One can see the implementation [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.17.0/Source/cmFindPackageCommand.cxx#L2365-2376).

@vre what actually goes wrong?

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [March 24, 2020, 6:19pm UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/4 "2020-03-24T18:19:59Z")

</div>

My question is why the `PACKAGE_PREFIX_DIR` is set to different values when installing to `Resources/CMake` and to `Versions/B/Resources/CMake` although both point to the same physical directory? If I have some variables dependent on `PACKAGE_PREFIX_DIR` they will not resolve properly when I use the one or the other path.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 24, 2020, 6:26pm UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/5 "2020-03-24T18:26:54Z")

</div>

The logic generating the code to compute `PACKAGE_PREFIX_DIR` assumes that there are no symbolic links involved. I don’t know if anyone has actually used `CMakePackageConfigHelpers` for a package configuration file inside a framework before.

The presence of symbolic links inside the framework may be problematic. `MyPackage_DIR` can be set by users to be either

- `<prefix>/MyPackage.framework/Resources/CMake`, or
- `<prefix>/MyPackage.framework/Versions/Current/Resources/CMake`, or
- `<prefix>/MyPackage.framework/Versions/B/Resources/CMake`.

In all of those cases we need to be able to recover the `<prefix>`, but the number of components that needs to be removed is not the same for all cases.

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [March 24, 2020, 10:26pm UTC](https://discourse.cmake.org/t/installing-cmake-package-config-files-for-macos-frameworks/849/6 "2020-03-24T22:26:56Z")

</div>

Thank you for the enlightening explanations. Good to know that symbolic links are problematic when placing configure files. I can work around that.
