[cmake] Resolve symlink when finding install prefix (#124743)

When determining the install prefix in LLVMConfig.cmake etc resolve
symlinks in CMAKE_CURRENT_LIST_FILE first. The motivation for this is to
support symlinks like `/usr/lib64/cmake/llvm` to
`/usr/lib64/llvm19/lib/cmake/llvm`. This only works correctly if the
paths are relative to the resolved symlink.

It's worth noting that this *mostly* already works out of the box,
because cmake automatically does the symlink resolution when the library
is found via CMAKE_PREFIX_PATH. It just doesn't happen when it's found
via the default prefix path.
This commit is contained in:
Nikita Popov 2025-03-18 14:48:40 +01:00 committed by GitHub
parent 22c6674f1d
commit 2fbfbf499e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,10 +39,10 @@ function(find_prefix_from_config out_var prefix_var path_to_leave)
# install prefix, and avoid hard-coding any absolute paths. # install prefix, and avoid hard-coding any absolute paths.
set(config_code set(config_code
"# Compute the installation prefix from this LLVMConfig.cmake file location." "# Compute the installation prefix from this LLVMConfig.cmake file location."
"get_filename_component(${prefix_var} \"\${CMAKE_CURRENT_LIST_FILE}\" PATH)") "get_filename_component(${prefix_var} \"\${CMAKE_CURRENT_LIST_FILE}\" REALPATH)")
# Construct the proper number of get_filename_component(... PATH) # Construct the proper number of get_filename_component(... PATH)
# calls to compute the installation prefix. # calls to compute the installation prefix.
string(REGEX REPLACE "/" ";" _count "${path_to_leave}") string(REGEX REPLACE "/" ";" _count "${path_to_leave}/plus_one")
foreach(p ${_count}) foreach(p ${_count})
list(APPEND config_code list(APPEND config_code
"get_filename_component(${prefix_var} \"\${${prefix_var}}\" PATH)") "get_filename_component(${prefix_var} \"\${${prefix_var}}\" PATH)")