• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
26set(PCRE2_NON_STANDARD_LIB_PREFIX @NON_STANDARD_LIB_PREFIX@)
27set(PCRE2_NON_STANDARD_LIB_SUFFIX @NON_STANDARD_LIB_SUFFIX@)
28set(PCRE2_8BIT_NAME pcre2-8)
29set(PCRE2_16BIT_NAME pcre2-16)
30set(PCRE2_32BIT_NAME pcre2-32)
31set(PCRE2_POSIX_NAME pcre2-posix)
32find_path(PCRE2_INCLUDE_DIR NAMES pcre2.h DOC "PCRE2 include directory")
33if (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})
43else ()
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  endif ()
53endif ()
54find_library(PCRE2_8BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "8 bit PCRE2 library")
55find_library(PCRE2_16BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_16BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "16 bit PCRE2 library")
56find_library(PCRE2_32BIT_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_32BIT_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "32 bit PCRE2 library")
57find_library(PCRE2_POSIX_LIBRARY NAMES ${PCRE2_PREFIX}${PCRE2_POSIX_NAME}${PCRE2_SUFFIX} ${PCRE2_PREFIX}${PCRE2_8BIT_NAME}d${PCRE2_SUFFIX} DOC "8 bit POSIX PCRE2 library")
58unset(PCRE2_NON_STANDARD_LIB_PREFIX)
59unset(PCRE2_NON_STANDARD_LIB_SUFFIX)
60unset(PCRE2_8BIT_NAME)
61unset(PCRE2_16BIT_NAME)
62unset(PCRE2_32BIT_NAME)
63unset(PCRE2_POSIX_NAME)
64
65# Set version
66if (PCRE2_INCLUDE_DIR)
67  set(PCRE2_VERSION "@PCRE2_MAJOR@.@PCRE2_MINOR@.0")
68endif ()
69
70# Which components have been found.
71if (PCRE2_8BIT_LIBRARY)
72  set(PCRE2_8BIT_FOUND TRUE)
73endif ()
74if (PCRE2_16BIT_LIBRARY)
75  set(PCRE2_16BIT_FOUND TRUE)
76endif ()
77if (PCRE2_32BIT_LIBRARY)
78  set(PCRE2_32BIT_FOUND TRUE)
79endif ()
80if (PCRE2_POSIX_LIBRARY)
81  set(PCRE2_POSIX_FOUND TRUE)
82endif ()
83
84# Check if at least one component has been specified.
85list(LENGTH PCRE2_FIND_COMPONENTS PCRE2_NCOMPONENTS)
86if (PCRE2_NCOMPONENTS LESS 1)
87  message(FATAL_ERROR "No components have been specified. This is not allowed. Please, specify at least one component.")
88endif ()
89unset(PCRE2_NCOMPONENTS)
90
91# When POSIX component has been specified make sure that also 8BIT component is specified.
92set(PCRE2_8BIT_COMPONENT FALSE)
93set(PCRE2_POSIX_COMPONENT FALSE)
94foreach(component ${PCRE2_FIND_COMPONENTS})
95  if (component STREQUAL "8BIT")
96    set(PCRE2_8BIT_COMPONENT TRUE)
97  elseif (component STREQUAL "POSIX")
98    set(PCRE2_POSIX_COMPONENT TRUE)
99  endif ()
100endforeach()
101
102if (PCRE2_POSIX_COMPONENT AND NOT PCRE2_8BIT_COMPONENT)
103  message(FATAL_ERROR "The component POSIX is specified while the 8BIT one is not. This is not allowed. Please, also specify the 8BIT component.")
104endif()
105unset(PCRE2_8BIT_COMPONENT)
106unset(PCRE2_POSIX_COMPONENT)
107
108include(FindPackageHandleStandardArgs)
109set(${CMAKE_FIND_PACKAGE_NAME}_CONFIG "${CMAKE_CURRENT_LIST_FILE}")
110find_package_handle_standard_args(PCRE2
111  FOUND_VAR PCRE2_FOUND
112  REQUIRED_VARS PCRE2_INCLUDE_DIR
113  HANDLE_COMPONENTS
114  VERSION_VAR PCRE2_VERSION
115  CONFIG_MODE
116)
117
118set(PCRE2_LIBRARIES)
119if (PCRE2_FOUND)
120  foreach(component ${PCRE2_FIND_COMPONENTS})
121    if (PCRE2_USE_STATIC_LIBS)
122      add_library(PCRE2::${component} STATIC IMPORTED)
123      target_compile_definitions(PCRE2::${component} INTERFACE PCRE2_STATIC)
124    else ()
125      add_library(PCRE2::${component} SHARED IMPORTED)
126    endif ()
127    set_target_properties(PCRE2::${component} PROPERTIES
128      IMPORTED_LOCATION "${PCRE2_${component}_LIBRARY}"
129      INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}"
130    )
131    if (component STREQUAL "POSIX")
132      set_target_properties(PCRE2::${component} PROPERTIES
133        INTERFACE_LINK_LIBRARIES "PCRE2::8BIT"
134        LINK_LIBRARIES "PCRE2::8BIT"
135      )
136    endif ()
137
138    set(PCRE2_LIBRARIES ${PCRE2_LIBRARIES} ${PCRE2_${component}_LIBRARY})
139    mark_as_advanced(PCRE2_${component}_LIBRARY)
140  endforeach()
141endif ()
142
143mark_as_advanced(
144  PCRE2_INCLUDE_DIR
145)
146