expat: Fix linking with system Expat via its CMake package

The Expat package intentionally provides the lowercase imported target
name `expat::expat` [1].  CMake performs `find_package(EXPAT)` w/o
specifying a finding mode (CONFIG or MODULE).  However, if a user
provides `CMAKE_FIND_PACKAGE_PREFER_CONFIG=TRUE`, the build fails due to
an unknown `EXPAT::EXPAT` target.

[1] https://github.com/libexpat/libexpat/tree/R_2_7_4/expat#using-libexpat-in-your-cmake-based-project

Issue: #27646
This commit is contained in:
Alex Turbov
2026-02-25 11:42:12 -05:00
committed by Brad King
parent 538c1b5d50
commit 06460b7764
+4
View File
@@ -181,6 +181,10 @@ if(CMAKE_USE_SYSTEM_EXPAT)
endif()
set(CMAKE_EXPAT_INCLUDES ${EXPAT_INCLUDE_DIRS})
set(CMAKE_EXPAT_LIBRARIES ${EXPAT_LIBRARIES})
# Expat's CMake package provides a lower-case target name.
if(NOT TARGET EXPAT::EXPAT AND TARGET expat::expat)
add_library(EXPAT::EXPAT ALIAS expat::expat)
endif()
else()
set(CMAKE_EXPAT_INCLUDES)
set(CMAKE_EXPAT_LIBRARIES cmexpat)