1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindEXPAT
6---------
7
8Find the native Expat headers and library.
9Expat is a stream-oriented XML parser library written in C.
10
11Imported Targets
12^^^^^^^^^^^^^^^^
13
14.. versionadded:: 3.10
15
16This module defines the following :prop_tgt:`IMPORTED` targets:
17
18``EXPAT::EXPAT``
19  The Expat ``expat`` library, if found.
20
21Result Variables
22^^^^^^^^^^^^^^^^
23
24This module will set the following variables in your project:
25
26``EXPAT_INCLUDE_DIRS``
27  where to find expat.h, etc.
28``EXPAT_LIBRARIES``
29  the libraries to link against to use Expat.
30``EXPAT_FOUND``
31  true if the Expat headers and libraries were found.
32
33#]=======================================================================]
34
35find_package(PkgConfig QUIET)
36
37pkg_check_modules(PC_EXPAT QUIET expat)
38
39# Look for the header file.
40find_path(EXPAT_INCLUDE_DIR NAMES expat.h HINTS ${PC_EXPAT_INCLUDE_DIRS})
41
42# Look for the library.
43find_library(EXPAT_LIBRARY NAMES expat libexpat NAMES_PER_DIR HINTS ${PC_EXPAT_LIBRARY_DIRS})
44
45if (EXPAT_INCLUDE_DIR AND EXISTS "${EXPAT_INCLUDE_DIR}/expat.h")
46    file(STRINGS "${EXPAT_INCLUDE_DIR}/expat.h" expat_version_str
47         REGEX "^#[\t ]*define[\t ]+XML_(MAJOR|MINOR|MICRO)_VERSION[\t ]+[0-9]+$")
48
49    unset(EXPAT_VERSION_STRING)
50    foreach(VPART MAJOR MINOR MICRO)
51        foreach(VLINE ${expat_version_str})
52            if(VLINE MATCHES "^#[\t ]*define[\t ]+XML_${VPART}_VERSION[\t ]+([0-9]+)$")
53                set(EXPAT_VERSION_PART "${CMAKE_MATCH_1}")
54                if(EXPAT_VERSION_STRING)
55                    string(APPEND EXPAT_VERSION_STRING ".${EXPAT_VERSION_PART}")
56                else()
57                    set(EXPAT_VERSION_STRING "${EXPAT_VERSION_PART}")
58                endif()
59            endif()
60        endforeach()
61    endforeach()
62endif ()
63
64include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
65FIND_PACKAGE_HANDLE_STANDARD_ARGS(EXPAT
66                                  REQUIRED_VARS EXPAT_LIBRARY EXPAT_INCLUDE_DIR
67                                  VERSION_VAR EXPAT_VERSION_STRING)
68
69# Copy the results to the output variables and target.
70if(EXPAT_FOUND)
71  set(EXPAT_LIBRARIES ${EXPAT_LIBRARY})
72  set(EXPAT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIR})
73
74  if(NOT TARGET EXPAT::EXPAT)
75    add_library(EXPAT::EXPAT UNKNOWN IMPORTED)
76    set_target_properties(EXPAT::EXPAT PROPERTIES
77      IMPORTED_LINK_INTERFACE_LANGUAGES "C"
78      IMPORTED_LOCATION "${EXPAT_LIBRARY}"
79      INTERFACE_INCLUDE_DIRECTORIES "${EXPAT_INCLUDE_DIRS}")
80  endif()
81endif()
82
83mark_as_advanced(EXPAT_INCLUDE_DIR EXPAT_LIBRARY)
84