Merge topic 'cpack-innosetup-linux'
[kiteware-cmake.git] / Modules / FindXercesC.cmake
blobee6a4015c57b3825338ace6892bcd41b3ee5dc97
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
5 FindXercesC
6 -----------
8 .. versionadded:: 3.1
10 Find the Apache Xerces-C++ validating XML parser headers and libraries.
12 Imported targets
13 ^^^^^^^^^^^^^^^^
15 .. versionadded:: 3.5
17 This module defines the following :prop_tgt:`IMPORTED` targets:
19 ``XercesC::XercesC``
20   The Xerces-C++ ``xerces-c`` library, if found.
22 Result variables
23 ^^^^^^^^^^^^^^^^
25 This module will set the following variables in your project:
27 ``XercesC_FOUND``
28   true if the Xerces headers and libraries were found
29 ``XercesC_VERSION``
30   Xerces release version
31 ``XercesC_INCLUDE_DIRS``
32   the directory containing the Xerces headers
33 ``XercesC_LIBRARIES``
34   Xerces libraries to be linked
36 Cache variables
37 ^^^^^^^^^^^^^^^
39 The following cache variables may also be set:
41 ``XercesC_INCLUDE_DIR``
42   the directory containing the Xerces headers
43 ``XercesC_LIBRARY``
44   the Xerces library
46 .. versionadded:: 3.4
47   Debug and Release variants are found separately.
48 #]=======================================================================]
50 cmake_policy(PUSH)
51 cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
53 # Written by Roger Leigh <rleigh@codelibre.net>
55 function(_XercesC_GET_VERSION  version_hdr)
56     file(STRINGS ${version_hdr} _contents REGEX "^[ \t]*#define XERCES_VERSION_.*")
57     if(_contents)
58         string(REGEX REPLACE ".*#define XERCES_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" XercesC_MAJOR "${_contents}")
59         string(REGEX REPLACE ".*#define XERCES_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" XercesC_MINOR "${_contents}")
60         string(REGEX REPLACE ".*#define XERCES_VERSION_REVISION[ \t]+([0-9]+).*" "\\1" XercesC_PATCH "${_contents}")
62         if(NOT XercesC_MAJOR MATCHES "^[0-9]+$")
63             message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_MAJOR!")
64         endif()
65         if(NOT XercesC_MINOR MATCHES "^[0-9]+$")
66             message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_MINOR!")
67         endif()
68         if(NOT XercesC_PATCH MATCHES "^[0-9]+$")
69             message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_REVISION!")
70         endif()
72         set(XercesC_VERSION "${XercesC_MAJOR}.${XercesC_MINOR}.${XercesC_PATCH}" PARENT_SCOPE)
73         set(XercesC_VERSION_MAJOR "${XercesC_MAJOR}" PARENT_SCOPE)
74         set(XercesC_VERSION_MINOR "${XercesC_MINOR}" PARENT_SCOPE)
75         set(XercesC_VERSION_PATCH "${XercesC_PATCH}" PARENT_SCOPE)
76     else()
77         message(FATAL_ERROR "Include file ${version_hdr} does not exist or does not contain expected version information")
78     endif()
79 endfunction()
81 # Find include directory
82 find_path(XercesC_INCLUDE_DIR
83           NAMES "xercesc/util/PlatformUtils.hpp"
84           DOC "Xerces-C++ include directory")
85 mark_as_advanced(XercesC_INCLUDE_DIR)
87 if(XercesC_INCLUDE_DIR AND EXISTS "${XercesC_INCLUDE_DIR}/xercesc/util/XercesVersion.hpp")
88   _XercesC_GET_VERSION("${XercesC_INCLUDE_DIR}/xercesc/util/XercesVersion.hpp")
89 endif()
91 if(NOT XercesC_LIBRARY)
92   # Find all XercesC libraries
93   find_library(XercesC_LIBRARY_RELEASE
94                NAMES "xerces-c"
95                      "xerces-c_${XercesC_VERSION_MAJOR}"
96                      "xerces-c-${XercesC_VERSION_MAJOR}.${XercesC_VERSION_MINOR}"
97                NAMES_PER_DIR
98                DOC "Xerces-C++ libraries (release)")
99   find_library(XercesC_LIBRARY_DEBUG
100                NAMES "xerces-cd"
101                      "xerces-c_${XercesC_VERSION_MAJOR}D"
102                      "xerces-c_${XercesC_VERSION_MAJOR}_${XercesC_VERSION_MINOR}D"
103                NAMES_PER_DIR
104                DOC "Xerces-C++ libraries (debug)")
105   include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
106   select_library_configurations(XercesC)
107   mark_as_advanced(XercesC_LIBRARY_RELEASE XercesC_LIBRARY_DEBUG)
108 endif()
110 unset(XercesC_VERSION_MAJOR)
111 unset(XercesC_VERSION_MINOR)
112 unset(XercesC_VERSION_PATCH)
114 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
115 FIND_PACKAGE_HANDLE_STANDARD_ARGS(XercesC
116                                   FOUND_VAR XercesC_FOUND
117                                   REQUIRED_VARS XercesC_LIBRARY
118                                                 XercesC_INCLUDE_DIR
119                                                 XercesC_VERSION
120                                   VERSION_VAR XercesC_VERSION
121                                   FAIL_MESSAGE "Failed to find XercesC")
123 if(XercesC_FOUND)
124   set(XercesC_INCLUDE_DIRS "${XercesC_INCLUDE_DIR}")
125   set(XercesC_LIBRARIES "${XercesC_LIBRARY}")
127   # For header-only libraries
128   if(NOT TARGET XercesC::XercesC)
129     add_library(XercesC::XercesC UNKNOWN IMPORTED)
130     if(XercesC_INCLUDE_DIRS)
131       set_target_properties(XercesC::XercesC PROPERTIES
132         INTERFACE_INCLUDE_DIRECTORIES "${XercesC_INCLUDE_DIRS}")
133     endif()
134     if(EXISTS "${XercesC_LIBRARY}")
135       set_target_properties(XercesC::XercesC PROPERTIES
136         IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
137         IMPORTED_LOCATION "${XercesC_LIBRARY}")
138     endif()
139     if(EXISTS "${XercesC_LIBRARY_RELEASE}")
140       set_property(TARGET XercesC::XercesC APPEND PROPERTY
141         IMPORTED_CONFIGURATIONS RELEASE)
142       set_target_properties(XercesC::XercesC PROPERTIES
143         IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
144         IMPORTED_LOCATION_RELEASE "${XercesC_LIBRARY_RELEASE}")
145     endif()
146     if(EXISTS "${XercesC_LIBRARY_DEBUG}")
147       set_property(TARGET XercesC::XercesC APPEND PROPERTY
148         IMPORTED_CONFIGURATIONS DEBUG)
149       set_target_properties(XercesC::XercesC PROPERTIES
150         IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
151         IMPORTED_LOCATION_DEBUG "${XercesC_LIBRARY_DEBUG}")
152     endif()
153   endif()
154 endif()
156 cmake_policy(POP)