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