[FileItem] Fix mimetype content lookup
[xbmc.git] / cmake / modules / FindPCRE2.cmake
blob1fc4f6c302d6346e52b2c3441b418a4264d0d089
1 #.rst:
2 # FindPCRE2
3 # --------
4 # Finds the PCRE2 library
6 # This will define the following imported target::
8 #   ${APP_NAME_LC}::PCRE2    - The PCRE2 library
10 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
12   macro(buildPCRE2)
13     set(PCRE2_VERSION ${${MODULE}_VER})
14     if(WIN32)
15       set(PCRE2_DEBUG_POSTFIX d)
16     endif()
18     set(patches "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/001-all-enable_docs_pc.patch"
19                 "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/002-all-cmake-config-installdir.patch")
21     generate_patchcommand("${patches}")
23     if(CORE_SYSTEM_NAME STREQUAL darwin_embedded OR WINDOWS_STORE)
24       set(EXTRA_ARGS -DPCRE2_SUPPORT_JIT=OFF)
25     else()
26       set(EXTRA_ARGS -DPCRE2_SUPPORT_JIT=ON)
27     endif()
29     set(CMAKE_ARGS -DBUILD_STATIC_LIBS=ON
30                    -DPCRE2_STATIC_PIC=ON
31                    -DPCRE2_BUILD_PCRE2_8=ON
32                    -DPCRE2_BUILD_PCRE2_16=OFF
33                    -DPCRE2_BUILD_PCRE2_32=OFF
34                    -DPCRE_NEWLINE=ANYCRLF
35                    -DPCRE2_SUPPORT_UNICODE=ON
36                    -DPCRE2_BUILD_PCRE2GREP=OFF
37                    -DPCRE2_BUILD_TESTS=OFF
38                    -DENABLE_DOCS=OFF
39                    ${EXTRA_ARGS})
41     set(${CMAKE_FIND_PACKAGE_NAME}_COMPILEDEFINITIONS PCRE2_STATIC)
43     BUILD_DEP_TARGET()
44   endmacro()
46   include(cmake/scripts/common/ModuleHelpers.cmake)
48   set(MODULE_LC pcre2)
50   SETUP_BUILD_VARS()
52   if(KODI_DEPENDSBUILD OR (WIN32 OR WINDOWS_STORE))
53     set(PCRE2_USE_STATIC_LIBS ON)
54   endif()
56   # Check for existing PCRE2. If version >= PCRE2-VERSION file version, dont build
57   find_package(PCRE2 CONFIG COMPONENTS 8BIT QUIET)
59   if((PCRE2_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_PCRE2) OR
60      ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_PCRE2))
61     buildPCRE2()
62   else()
63       # if PCRE2::8BIT target exists, it meets version requirements
64       # we only do a pkgconfig search when a suitable cmake config returns nothing
65       if(TARGET PCRE2::8BIT)
66         get_target_property(_PCRE2_CONFIGURATIONS PCRE2::8BIT IMPORTED_CONFIGURATIONS)
67         if(_PCRE2_CONFIGURATIONS)
68         foreach(_pcre2_config IN LISTS _PCRE2_CONFIGURATIONS)
69           # Just set to RELEASE var so select_library_configurations can continue to work its magic
70           string(TOUPPER ${_pcre2_config} _pcre2_config_UPPER)
71           if((NOT ${_pcre2_config_UPPER} STREQUAL "RELEASE") AND
72              (NOT ${_pcre2_config_UPPER} STREQUAL "DEBUG"))
73             get_target_property(PCRE2_LIBRARY_RELEASE PCRE2::8BIT IMPORTED_LOCATION_${_pcre2_config_UPPER})
74           else()
75             get_target_property(PCRE2_LIBRARY_${_pcre2_config_UPPER} PCRE2::8BIT IMPORTED_LOCATION_${_pcre2_config_UPPER})
76           endif()
77         endforeach()
78       else()
79         get_target_property(PCRE2_LIBRARY_RELEASE PCRE2::8BIT IMPORTED_LOCATION)
80       endif()
81       get_target_property(PCRE2_INCLUDE_DIR PCRE2::8BIT INTERFACE_INCLUDE_DIRECTORIES)
82     else()
83       # ToDo: use pkgconfig data imported and drop manual find_path/find_library
84       find_package(PkgConfig)
85       if(PKG_CONFIG_FOUND)
86         pkg_check_modules(PC_PCRE2 libpcre2-8 QUIET)
87       endif()
88   
89       find_path(PCRE2_INCLUDE_DIR pcre2.h
90                                   HINTS ${PC_PCRE2_INCLUDEDIR})
91       find_library(PCRE2_LIBRARY_RELEASE NAMES pcre2-8
92                                          HINTS ${PC_PCRE2_LIBDIR})
93       set(PCRE2_VERSION ${PC_PCRE2_VERSION})
94     endif()
95   endif()
97   include(SelectLibraryConfigurations)
98   select_library_configurations(PCRE2)
100   include(FindPackageHandleStandardArgs)
101   find_package_handle_standard_args(PCRE2
102                                     REQUIRED_VARS PCRE2_LIBRARY PCRE2_INCLUDE_DIR
103                                     VERSION_VAR PCRE2_VERSION)
105   if(PCRE2_FOUND)
106     if(TARGET PCRE2::8BIT AND NOT TARGET pcre2)
107       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS PCRE2::8BIT)
108     else()
109       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} UNKNOWN IMPORTED)
110       if(PCRE2_LIBRARY_RELEASE)
111         set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
112                                                                          IMPORTED_CONFIGURATIONS RELEASE
113                                                                          IMPORTED_LOCATION_RELEASE "${PCRE2_LIBRARY_RELEASE}")
114       endif()
115       if(PCRE2_LIBRARY_DEBUG)
116         set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
117                                                                          IMPORTED_LOCATION_DEBUG "${PCRE2_LIBRARY_DEBUG}")
118         set_property(TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} APPEND PROPERTY
119                                                                               IMPORTED_CONFIGURATIONS DEBUG)
120       endif()
121       set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
122                                                                        INTERFACE_INCLUDE_DIRECTORIES "${PCRE2_INCLUDE_DIR}")
124       # Add interface compile definitions. This will usually come from an INTERNAL build being required.
125       if(${CMAKE_FIND_PACKAGE_NAME}_COMPILEDEFINITIONS)
126         set_property(TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} APPEND PROPERTY
127                                                                               INTERFACE_COMPILE_DEFINITIONS ${${CMAKE_FIND_PACKAGE_NAME}_COMPILEDEFINITIONS})
128       endif()
129     endif()
130     if(TARGET pcre2)
131       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} pcre2)
132     endif()
134     # Add internal build target when a Multi Config Generator is used
135     # We cant add a dependency based off a generator expression for targeted build types,
136     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
137     # therefore if the find heuristics only find the library, we add the internal build
138     # target to the project to allow user to manually trigger for any build type they need
139     # in case only a specific build type is actually available (eg Release found, Debug Required)
140     # This is mainly targeted for windows who required different runtime libs for different
141     # types, and they arent compatible
142     if(_multiconfig_generator)
143       if(NOT TARGET pcre2)
144         buildPCRE2()
145         set_target_properties(pcre2 PROPERTIES EXCLUDE_FROM_ALL TRUE)
146       endif()
147       add_dependencies(build_internal_depends pcre2)
148     endif()
150   else()
151     if(PCRE2_FIND_REQUIRED)
152       message(FATAL_ERROR "PCRE2 not found. Possibly use -DENABLE_INTERNAL_PCRE2=ON to build PCRE2")
153     endif()
154   endif()
155 endif()