CMake: Try to improve compatibility with the FindLibLZMA module.

The naming conflict with FindLibLZMA module gets worse.
Not avoiding it in the first place was stupid.

Normally find_package(LibLZMA) will use the module and
find_package(liblzma 5.2.5 REQUIRED CONFIG) will use the config
file even with a case insensitive file system. However, if
CMAKE_FIND_PACKAGE_PREFER_CONFIG is TRUE and the file system
is case insensitive, find_package(LibLZMA) will find our liblzma
config file instead of using FindLibLZMA module.

One big problem with this is that FindLibLZMA uses
LibLZMA::LibLZMA and we use liblzma::liblzma as the target
name. With target names CMake happens to be case sensitive.
To workaround this, this commit adds

    add_library(LibLZMA::LibLZMA ALIAS liblzma::liblzma)

to the config file. Then both spellings work.

To make the behavior consistent between case sensitive and
insensitive file systems, the config and related files are
renamed from liblzmaConfig.cmake to liblzma-config.cmake style.
With this style CMake looks for lowercase version of the package
name so find_package(LiBLzmA 5.2.5 REQUIRED CONFIG) will work
to find our config file.

There are other differences between our config file and
FindLibLZMA so it's still possible that things break for
reasons other than the spelling of the target name. Hopefully
those situations aren't too common.

When the config file is available, it should always give as good or
better results as FindLibLZMA so this commit doesn't affect the
recommendation to use find_package(liblzma 5.2.5 REQUIRED CONFIG)
which explicitly avoids FindLibLZMA.

Thanks to Markus Rickert.
This commit is contained in:
Lasse Collin 2021-01-30 18:36:04 +02:00
parent 9e2f9e2d08
commit 64d9814761
1 changed files with 13 additions and 8 deletions

View File

@ -434,22 +434,27 @@ set_target_properties(liblzma PROPERTIES
PREFIX "" PREFIX ""
) )
# Create liblzmaConfigVersion.cmake. # Create liblzma-config-version.cmake. We use this spelling instead of
# liblzmaConfig.cmake to make find_package work in case insensitive manner
# even with case sensitive file systems. This gives more consistent behavior
# between operating systems.
# #
# FIXME: SameMajorVersion is correct for stable releases but it is wrong # FIXME: SameMajorVersion is correct for stable releases but it is wrong
# for development releases where each release may have incompatible changes. # for development releases where each release may have incompatible changes.
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
write_basic_package_version_file( write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/liblzmaConfigVersion.cmake" "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
VERSION "${liblzma_VERSION}" VERSION "${liblzma_VERSION}"
COMPATIBILITY SameMajorVersion) COMPATIBILITY SameMajorVersion)
# Create liblzmaConfig.cmake. # Create liblzma-config.cmake.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzmaConfig.cmake" file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
"include(CMakeFindDependencyMacro) "include(CMakeFindDependencyMacro)
set(THREADS_PREFER_PTHREAD_FLAG TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_dependency(Threads) find_dependency(Threads)
include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzmaTargets.cmake\") include(\"\${CMAKE_CURRENT_LIST_DIR}/liblzma-targets.cmake\")
# Be compatible with the spelling used by the FindLibLZMA module:
add_library(LibLZMA::LibLZMA ALIAS liblzma::liblzma)
") ")
# Set CMAKE_INSTALL_LIBDIR and friends. # Set CMAKE_INSTALL_LIBDIR and friends.
@ -481,12 +486,12 @@ set(liblzma_INSTALL_CMAKEDIR
install(EXPORT liblzmaTargets install(EXPORT liblzmaTargets
NAMESPACE liblzma:: NAMESPACE liblzma::
FILE liblzmaTargets.cmake FILE liblzma-targets.cmake
DESTINATION "${liblzma_INSTALL_CMAKEDIR}" DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
COMPONENT liblzma_Development) COMPONENT liblzma_Development)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzmaConfig.cmake" install(FILES "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/liblzmaConfigVersion.cmake" "${CMAKE_CURRENT_BINARY_DIR}/liblzma-config-version.cmake"
DESTINATION "${liblzma_INSTALL_CMAKEDIR}" DESTINATION "${liblzma_INSTALL_CMAKEDIR}"
COMPONENT liblzma_Development) COMPONENT liblzma_Development)