1# Copyright 2017 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15find_package(re2 QUIET CONFIG)
16if(re2_FOUND)
17  message(STATUS "Found RE2 via CMake.")
18  return()
19endif()
20
21# As per https://github.com/grpc/grpc/issues/25434, idempotence is necessary
22# because CMake fails when another target with the same name already exists.
23if(TARGET re2::re2)
24  message(STATUS "Found RE2 via pkg-config already?")
25  return()
26endif()
27
28find_package(PkgConfig REQUIRED)
29# TODO(junyer): Use the IMPORTED_TARGET option whenever CMake 3.6 (or newer)
30# becomes the minimum required: that will take care of the add_library() and
31# set_property() calls; then we can simply alias PkgConfig::RE2 as re2::re2.
32# For now, we can only set INTERFACE_* properties that existed in CMake 3.5.
33pkg_check_modules(RE2 QUIET re2)
34if(RE2_FOUND)
35  set(re2_FOUND "${RE2_FOUND}")
36  add_library(re2::re2 INTERFACE IMPORTED)
37  if(RE2_INCLUDE_DIRS)
38    set_property(TARGET re2::re2 PROPERTY
39                 INTERFACE_INCLUDE_DIRECTORIES "${RE2_INCLUDE_DIRS}")
40  endif()
41  if(RE2_CFLAGS_OTHER)
42    # Filter out the -std flag, which is handled by CMAKE_CXX_STANDARD.
43    # TODO(junyer): Use the FILTER option whenever CMake 3.6 (or newer)
44    # becomes the minimum required: that will allow this to be concise.
45    foreach(flag IN LISTS RE2_CFLAGS_OTHER)
46      if("${flag}" MATCHES "^-std=")
47        list(REMOVE_ITEM RE2_CFLAGS_OTHER "${flag}")
48      endif()
49    endforeach()
50    set_property(TARGET re2::re2 PROPERTY
51                 INTERFACE_COMPILE_OPTIONS "${RE2_CFLAGS_OTHER}")
52  endif()
53  if(RE2_LDFLAGS)
54    set_property(TARGET re2::re2 PROPERTY
55                 INTERFACE_LINK_LIBRARIES "${RE2_LDFLAGS}")
56  endif()
57  message(STATUS "Found RE2 via pkg-config.")
58  return()
59endif()
60
61if(re2_FIND_REQUIRED)
62  message(FATAL_ERROR "Failed to find RE2.")
63elseif(NOT re2_FIND_QUIETLY)
64  message(WARNING "Failed to find RE2.")
65endif()
66