Skip to content

CMakePackageConfigHelpers: resolve CMAKE_CURRENT_LIST_DIR symlinks

A cmake config file may be found through a symlink, for example when using the popular MacOS Homebrew package manager to install a set of inter-woven dependences.

Homebrew creates a virtual /usr/local hierarchy by symlinking into self-contained installations under the /usr/local/Cellar namespace.

When using exported targets from find_package() config files that use @PACKAGE_INIT@, the /usr/local/include paths are used, not the granular /usr/local/Cellar/{name}/{version}/include paths.

This is undesirable because it makes it impossible to override arbitrary subsets of the tree with sandboxed versions of specific packages. The problem is that dependent libraries will add /usr/local/include into the search path, thus eclipsing a sandboxed development install of another library which is also present in the link tree.

This can be improved by resolving the CMAKE_CURRENT_LIST_DIR to its real path when calculating the relocatable PACKAGE_PREFIX_DIR.

Following the directory link results in a package prefix which points into the Cellar when the share/cmake/${name}/ convention is followed. The ${name} component will be present in the aggregate tree as a symlink because it can be relied upon to be unique across packages.

Adjust @PACKAGE_INIT@ to resolve symlinks when ${CMAKE_CURRENT_LIST_DIR} is a symlink itself.

Edited by David Aguilar

Merge request reports