1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

FindLibXml2: ensure consistent paths, version and flag information

As pkg-config is used as hint to perform the actual lookup of libraries
and include paths we may not find the pkg-config installation.
Mainly this makes sure the pkg-config information are used only if the
actual installation reported to the user is the one from pkg-config.

Fixes: #20149
This commit is contained in:
Sylvain Joubert
2020-01-07 09:18:10 +01:00
parent e353d8bd9b
commit 4041cf5f61

View File

@@ -52,7 +52,6 @@ The following cache variables may also be set:
# in the find_path() and find_library() calls
find_package(PkgConfig QUIET)
PKG_CHECK_MODULES(PC_LIBXML QUIET libxml-2.0)
set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
find_path(LIBXML2_INCLUDE_DIR NAMES libxml/xpath.h
HINTS
@@ -78,9 +77,7 @@ find_program(LIBXML2_XMLLINT_EXECUTABLE xmllint)
# for backwards compat. with KDE 4.0.x:
set(XMLLINT_EXECUTABLE "${LIBXML2_XMLLINT_EXECUTABLE}")
if(PC_LIBXML_VERSION)
set(LIBXML2_VERSION_STRING ${PC_LIBXML_VERSION})
elseif(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
if(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h")
file(STRINGS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.h" libxml2_version_str
REGEX "^#define[\t ]+LIBXML_DOTTED_VERSION[\t ]+\".*\"")
@@ -89,9 +86,20 @@ elseif(LIBXML2_INCLUDE_DIR AND EXISTS "${LIBXML2_INCLUDE_DIR}/libxml/xmlversion.
unset(libxml2_version_str)
endif()
set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR} ${PC_LIBXML_INCLUDE_DIRS})
set(LIBXML2_INCLUDE_DIRS ${LIBXML2_INCLUDE_DIR})
set(LIBXML2_LIBRARIES ${LIBXML2_LIBRARY})
# Did we find the same installation as pkg-config?
# If so, use additional information from it.
unset(LIBXML2_DEFINITIONS)
foreach(libxml2_pc_lib_dir IN LISTS PC_LIBXML_LIBDIR PC_LIBXML_LIBRARY_DIRS)
if (LIBXML2_LIBRARY MATCHES "^${libxml2_pc_lib_dir}")
list(APPEND LIBXML2_INCLUDE_DIRS ${PC_LIBXML_INCLUDE_DIRS})
set(LIBXML2_DEFINITIONS ${PC_LIBXML_CFLAGS_OTHER})
break()
endif()
endforeach()
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2
REQUIRED_VARS LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR
@@ -102,6 +110,7 @@ mark_as_advanced(LIBXML2_INCLUDE_DIR LIBXML2_LIBRARY LIBXML2_XMLLINT_EXECUTABLE)
if(LibXml2_FOUND AND NOT TARGET LibXml2::LibXml2)
add_library(LibXml2::LibXml2 UNKNOWN IMPORTED)
set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LIBXML2_INCLUDE_DIRS}")
set_target_properties(LibXml2::LibXml2 PROPERTIES INTERFACE_COMPILE_OPTIONS "${LIBXML2_DEFINITIONS}")
set_property(TARGET LibXml2::LibXml2 APPEND PROPERTY IMPORTED_LOCATION "${LIBXML2_LIBRARY}")
endif()