|
| 1 | +# pcre2-config.cmake |
| 2 | +# ---------------- |
| 3 | +# |
| 4 | +# Finds the PCRE2 library, specify the starting search path in PCRE2_ROOT. |
| 5 | +# |
| 6 | +# Static vs. shared |
| 7 | +# ----------------- |
| 8 | +# To make use of the static library instead of the shared one, one needs |
| 9 | +# to set the variable PCRE2_USE_STATIC_LIBS to ON before calling find_package. |
| 10 | +# Example: |
| 11 | +# set(PCRE2_USE_STATIC_LIBS ON) |
| 12 | +# find_package(PCRE2 CONFIG COMPONENTS 8BIT) |
| 13 | +# |
| 14 | +# This will define the following variables: |
| 15 | +# |
| 16 | +# PCRE2_FOUND - True if the system has the PCRE2 library. |
| 17 | +# PCRE2_VERSION - The version of the PCRE2 library which was found. |
| 18 | +# |
| 19 | +# and the following imported targets: |
| 20 | +# |
| 21 | +# PCRE2::8BIT - The 8 bit PCRE2 library. |
| 22 | +# PCRE2::16BIT - The 16 bit PCRE2 library. |
| 23 | +# PCRE2::32BIT - The 32 bit PCRE2 library. |
| 24 | +# PCRE2::POSIX - The POSIX PCRE2 library. |
| 25 | + |
| 26 | +set(PCRE2_NON_STANDARD_LIB_PREFIX ) |
| 27 | +set(PCRE2_NON_STANDARD_LIB_SUFFIX ) |
| 28 | +set(PCRE2_8BIT_NAME pcre2-8) |
| 29 | +set(PCRE2_16BIT_NAME pcre2-16) |
| 30 | +set(PCRE2_32BIT_NAME pcre2-32) |
| 31 | +set(PCRE2_POSIX_NAME pcre2-posix) |
| 32 | +find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h DOC "PCRE2 include directory") |
| 33 | +if(PCRE2_USE_STATIC_LIBS) |
| 34 | + if(MSVC) |
| 35 | + set(PCRE2_8BIT_NAME pcre2-8-static) |
| 36 | + set(PCRE2_16BIT_NAME pcre2-16-static) |
| 37 | + set(PCRE2_32BIT_NAME pcre2-32-static) |
| 38 | + set(PCRE2_POSIX_NAME pcre2-posix-static) |
| 39 | + endif() |
| 40 | + |
| 41 | + set(PCRE2_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX}) |
| 42 | + set(PCRE2_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 43 | +else() |
| 44 | + set(PCRE2_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX}) |
| 45 | + if(MINGW AND PCRE2_NON_STANDARD_LIB_PREFIX) |
| 46 | + set(PCRE2_PREFIX "") |
| 47 | + endif() |
| 48 | + |
| 49 | + set(PCRE2_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| 50 | + if(MINGW AND PCRE2_NON_STANDARD_LIB_SUFFIX) |
| 51 | + set(PCRE2_SUFFIX "-0.dll") |
| 52 | + elseif(MSVC) |
| 53 | + set(PCRE2_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) |
| 54 | + endif() |
| 55 | +endif() |
| 56 | +find_library( |
| 57 | + PCRE2_8BIT_LIBRARY |
| 58 | + NAMES ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} |
| 59 | + DOC "8 bit PCRE2 library" |
| 60 | +) |
| 61 | +find_library( |
| 62 | + PCRE2_16BIT_LIBRARY |
| 63 | + NAMES ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}d${PCRE2_SUFFIX} |
| 64 | + DOC "16 bit PCRE2 library" |
| 65 | +) |
| 66 | +find_library( |
| 67 | + PCRE2_32BIT_LIBRARY |
| 68 | + NAMES ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}d${PCRE2_SUFFIX} |
| 69 | + DOC "32 bit PCRE2 library" |
| 70 | +) |
| 71 | +find_library( |
| 72 | + PCRE2_POSIX_LIBRARY |
| 73 | + NAMES ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}d${PCRE2_SUFFIX} |
| 74 | + DOC "8 bit POSIX PCRE2 library" |
| 75 | +) |
| 76 | + |
| 77 | +if(WIN32) |
| 78 | + find_file( |
| 79 | + PCRE2_8BIT_DLL |
| 80 | + NAMES ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX} |
| 81 | + PATH_SUFFIXES bin |
| 82 | + DOC "8 bit PCRE2 DLL" |
| 83 | + ) |
| 84 | + find_file( |
| 85 | + PCRE2_16BIT_DLL |
| 86 | + NAMES ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX} |
| 87 | + PATH_SUFFIXES bin |
| 88 | + DOC "16 bit PCRE2 DLL" |
| 89 | + ) |
| 90 | + find_file( |
| 91 | + PCRE2_32BIT_DLL |
| 92 | + NAMES ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX} |
| 93 | + PATH_SUFFIXES bin |
| 94 | + DOC "32 bit PCRE2 DLL" |
| 95 | + ) |
| 96 | + find_file( |
| 97 | + PCRE2_POSIX_DLL |
| 98 | + NAMES ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX} |
| 99 | + PATH_SUFFIXES bin |
| 100 | + DOC "8 bit POSIX PCRE2 DLL" |
| 101 | + ) |
| 102 | +endif() |
| 103 | + |
| 104 | +unset(PCRE2_NON_STANDARD_LIB_PREFIX) |
| 105 | +unset(PCRE2_NON_STANDARD_LIB_SUFFIX) |
| 106 | +unset(PCRE2_8BIT_NAME) |
| 107 | +unset(PCRE2_16BIT_NAME) |
| 108 | +unset(PCRE2_32BIT_NAME) |
| 109 | +unset(PCRE2_POSIX_NAME) |
| 110 | + |
| 111 | +# Set version |
| 112 | +if(PCRE2_INCLUDE_DIR) |
| 113 | + set(PCRE2_VERSION "10.45.0") |
| 114 | +endif() |
| 115 | + |
| 116 | +# Which components have been found. |
| 117 | +if(PCRE2_8BIT_LIBRARY AND (NOT WIN32 OR PCRE2_8BIT_DLL)) |
| 118 | + set(PCRE2_8BIT_FOUND TRUE) |
| 119 | +endif() |
| 120 | +if(PCRE2_16BIT_LIBRARY AND (NOT WIN32 OR PCRE2_16BIT_DLL)) |
| 121 | + set(PCRE2_16BIT_FOUND TRUE) |
| 122 | +endif() |
| 123 | +if(PCRE2_32BIT_LIBRARY AND (NOT WIN32 OR PCRE2_32BIT_DLL)) |
| 124 | + set(PCRE2_32BIT_FOUND TRUE) |
| 125 | +endif() |
| 126 | +if(PCRE2_POSIX_LIBRARY AND (NOT WIN32 OR PCRE2_POSIX_DLL)) |
| 127 | + set(PCRE2_POSIX_FOUND TRUE) |
| 128 | +endif() |
| 129 | + |
| 130 | +# Check if at least one component has been specified. |
| 131 | +list(LENGTH PCRE2_FIND_COMPONENTS PCRE2_NCOMPONENTS) |
| 132 | +if(PCRE2_NCOMPONENTS LESS 1) |
| 133 | + message(FATAL_ERROR "No components have been specified. This is not allowed. Please, specify at least one component.") |
| 134 | +endif() |
| 135 | +unset(PCRE2_NCOMPONENTS) |
| 136 | + |
| 137 | +# When POSIX component has been specified make sure that also 8BIT component is specified. |
| 138 | +set(PCRE2_8BIT_COMPONENT FALSE) |
| 139 | +set(PCRE2_POSIX_COMPONENT FALSE) |
| 140 | +foreach(component ${PCRE2_FIND_COMPONENTS}) |
| 141 | + if(component STREQUAL "8BIT") |
| 142 | + set(PCRE2_8BIT_COMPONENT TRUE) |
| 143 | + elseif(component STREQUAL "POSIX") |
| 144 | + set(PCRE2_POSIX_COMPONENT TRUE) |
| 145 | + endif() |
| 146 | +endforeach() |
| 147 | + |
| 148 | +if(PCRE2_POSIX_COMPONENT AND NOT PCRE2_8BIT_COMPONENT) |
| 149 | + message( |
| 150 | + FATAL_ERROR |
| 151 | + "The component POSIX is specified while the 8BIT one is not. This is not allowed. Please, also specify the 8BIT component." |
| 152 | + ) |
| 153 | +endif() |
| 154 | +unset(PCRE2_8BIT_COMPONENT) |
| 155 | +unset(PCRE2_POSIX_COMPONENT) |
| 156 | + |
| 157 | +include(FindPackageHandleStandardArgs) |
| 158 | +set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}") |
| 159 | +find_package_handle_standard_args( |
| 160 | + PCRE2 |
| 161 | + FOUND_VAR PCRE2_FOUND |
| 162 | + REQUIRED_VARS PCRE2_INCLUDE_DIR |
| 163 | + HANDLE_COMPONENTS |
| 164 | + VERSION_VAR PCRE2_VERSION |
| 165 | + CONFIG_MODE |
| 166 | +) |
| 167 | + |
| 168 | +set(PCRE2_LIBRARIES) |
| 169 | +if(PCRE2_FOUND) |
| 170 | + foreach(component ${PCRE2_FIND_COMPONENTS}) |
| 171 | + add_library(PCRE2::${component} SHARED IMPORTED) |
| 172 | + if(WIN32) |
| 173 | + set_target_properties( |
| 174 | + PCRE2::${component} |
| 175 | + PROPERTIES |
| 176 | + IMPORTED_LOCATION "${PCRE2_${component}_DLL}" |
| 177 | + IMPORTED_IMPLIB "${PCRE2_${component}_LIBRARY}" |
| 178 | + INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}" |
| 179 | + ) |
| 180 | + else() |
| 181 | + set_target_properties( |
| 182 | + PCRE2::${component} |
| 183 | + PROPERTIES |
| 184 | + IMPORTED_LOCATION "${PCRE2_${component}_LIBRARY}" |
| 185 | + IMPORTED_IMPLIB "${PCRE2_${component}_LIBRARY}" |
| 186 | + INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}" |
| 187 | + ) |
| 188 | + endif() |
| 189 | + |
| 190 | + if(component STREQUAL "POSIX") |
| 191 | + set_target_properties( |
| 192 | + PCRE2::${component} |
| 193 | + PROPERTIES INTERFACE_LINK_LIBRARIES "PCRE2::8BIT" LINK_LIBRARIES "PCRE2::8BIT" |
| 194 | + ) |
| 195 | + endif() |
| 196 | + |
| 197 | + set(PCRE2_LIBRARIES ${PCRE2_LIBRARIES} ${PCRE2_${component}_LIBRARY}) |
| 198 | + mark_as_advanced(PCRE2_${component}_LIBRARY) |
| 199 | + endforeach() |
| 200 | +endif() |
| 201 | + |
| 202 | +mark_as_advanced(PCRE2_INCLUDE_DIR) |
0 commit comments