FindXercesC.cmake 5.21 KB
Newer Older
1
#.rst:
2 3
# FindXercesC
# -----------
4 5 6
#
# Find the Apache Xerces-C++ validating XML parser headers and libraries.
#
7 8
# Imported targets
# ^^^^^^^^^^^^^^^^
9
#
10
# This module defines the following :prop_tgt:`IMPORTED` targets:
11
#
12 13
# ``XercesC::XercesC``
#   The Xerces-C++ ``xerces-c`` library, if found.
14
#
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# Result variables
# ^^^^^^^^^^^^^^^^
#
# This module will set the following variables in your project:
#
# ``XercesC_FOUND``
#   true if the Xerces headers and libraries were found
# ``XercesC_VERSION``
#   Xerces release version
# ``XercesC_INCLUDE_DIRS``
#   the directory containing the Xerces headers
# ``XercesC_LIBRARIES``
#   Xerces libraries to be linked
#
# Cache variables
# ^^^^^^^^^^^^^^^
#
# The following cache variables may also be set:
#
# ``XercesC_INCLUDE_DIR``
#   the directory containing the Xerces headers
# ``XercesC_LIBRARY``
#   the Xerces library
38 39 40 41

# Written by Roger Leigh <rleigh@codelibre.net>

#=============================================================================
42
# Copyright 2014-2015 University of Dundee
43 44 45 46 47 48 49 50 51 52 53
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)

54
function(_XercesC_GET_VERSION  version_hdr)
55 56
    file(STRINGS ${version_hdr} _contents REGEX "^[ \t]*#define XERCES_VERSION_.*")
    if(_contents)
57 58 59
        string(REGEX REPLACE ".*#define XERCES_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" XercesC_MAJOR "${_contents}")
        string(REGEX REPLACE ".*#define XERCES_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" XercesC_MINOR "${_contents}")
        string(REGEX REPLACE ".*#define XERCES_VERSION_REVISION[ \t]+([0-9]+).*" "\\1" XercesC_PATCH "${_contents}")
60

61
        if(NOT XercesC_MAJOR MATCHES "^[0-9]+$")
62 63
            message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_MAJOR!")
        endif()
64
        if(NOT XercesC_MINOR MATCHES "^[0-9]+$")
65 66
            message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_MINOR!")
        endif()
67
        if(NOT XercesC_PATCH MATCHES "^[0-9]+$")
68 69 70
            message(FATAL_ERROR "Version parsing failed for XERCES_VERSION_REVISION!")
        endif()

71
        set(XercesC_VERSION "${XercesC_MAJOR}.${XercesC_MINOR}.${XercesC_PATCH}" PARENT_SCOPE)
72 73 74 75 76 77
    else()
        message(FATAL_ERROR "Include file ${version_hdr} does not exist or does not contain expected version information")
    endif()
endfunction()

# Find include directory
78
find_path(XercesC_INCLUDE_DIR
79 80
          NAMES "xercesc/util/PlatformUtils.hpp"
          DOC "Xerces-C++ include directory")
81
mark_as_advanced(XercesC_INCLUDE_DIR)
82

83 84 85 86 87 88
if(NOT XercesC_LIBRARY)
  # Find all XercesC libraries
  find_library(XercesC_LIBRARY_RELEASE
               NAMES "xerces-c" "xerces-c_3"
               DOC "Xerces-C++ libraries (release)")
  find_library(XercesC_LIBRARY_DEBUG
89
               NAMES "xerces-cd" "xerces-c_3D" "xerces-c_3_1D"
90 91 92 93 94
               DOC "Xerces-C++ libraries (debug)")
  include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  select_library_configurations(XercesC)
  mark_as_advanced(XercesC_LIBRARY_RELEASE XercesC_LIBRARY_DEBUG)
endif()
95

96 97
if(XercesC_INCLUDE_DIR)
  _XercesC_GET_VERSION("${XercesC_INCLUDE_DIR}/xercesc/util/XercesVersion.hpp")
98 99
endif()

Rolf Eike Beer's avatar
Rolf Eike Beer committed
100
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
101 102 103 104 105 106 107
FIND_PACKAGE_HANDLE_STANDARD_ARGS(XercesC
                                  FOUND_VAR XercesC_FOUND
                                  REQUIRED_VARS XercesC_LIBRARY
                                                XercesC_INCLUDE_DIR
                                                XercesC_VERSION
                                  VERSION_VAR XercesC_VERSION
                                  FAIL_MESSAGE "Failed to find XercesC")
108

109 110 111
if(XercesC_FOUND)
  set(XercesC_INCLUDE_DIRS "${XercesC_INCLUDE_DIR}")
  set(XercesC_LIBRARIES "${XercesC_LIBRARY}")
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  # For header-only libraries
  if(NOT TARGET XercesC::XercesC)
    add_library(XercesC::XercesC UNKNOWN IMPORTED)
    if(XercesC_INCLUDE_DIRS)
      set_target_properties(XercesC::XercesC PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${XercesC_INCLUDE_DIRS}")
    endif()
    if(EXISTS "${XercesC_LIBRARY}")
      set_target_properties(XercesC::XercesC PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
        IMPORTED_LOCATION "${XercesC_LIBRARY}")
    endif()
    if(EXISTS "${XercesC_LIBRARY_DEBUG}")
      set_property(TARGET XercesC::XercesC APPEND PROPERTY
        IMPORTED_CONFIGURATIONS DEBUG)
      set_target_properties(XercesC::XercesC PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
        IMPORTED_LOCATION_DEBUG "${XercesC_LIBRARY_DEBUG}")
    endif()
    if(EXISTS "${XercesC_LIBRARY_RELEASE}")
      set_property(TARGET XercesC::XercesC APPEND PROPERTY
        IMPORTED_CONFIGURATIONS RELEASE)
      set_target_properties(XercesC::XercesC PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
        IMPORTED_LOCATION_RELEASE "${XercesC_LIBRARY_RELEASE}")
    endif()
  endif()
140
endif()