Findre2.cmake 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. find_package(re2 QUIET CONFIG)
  15. if(re2_FOUND)
  16. message(STATUS "Found RE2 via CMake.")
  17. return()
  18. endif()
  19. # As per https://github.com/grpc/grpc/issues/25434, idempotence is necessary
  20. # because CMake fails when another target with the same name already exists.
  21. if(TARGET re2::re2)
  22. message(STATUS "Found RE2 via pkg-config already?")
  23. return()
  24. endif()
  25. find_package(PkgConfig REQUIRED)
  26. # TODO(junyer): Use the IMPORTED_TARGET option whenever CMake 3.6 (or newer)
  27. # becomes the minimum required: that will take care of the add_library() and
  28. # set_property() calls; then we can simply alias PkgConfig::RE2 as re2::re2.
  29. # For now, we can only set INTERFACE_* properties that existed in CMake 3.5.
  30. pkg_check_modules(RE2 QUIET re2)
  31. if(RE2_FOUND)
  32. set(re2_FOUND "${RE2_FOUND}")
  33. add_library(re2::re2 INTERFACE IMPORTED)
  34. if(RE2_INCLUDE_DIRS)
  35. set_property(TARGET re2::re2 PROPERTY
  36. INTERFACE_INCLUDE_DIRECTORIES "${RE2_INCLUDE_DIRS}")
  37. endif()
  38. if(RE2_CFLAGS_OTHER)
  39. # Filter out the -std flag, which is handled by CMAKE_CXX_STANDARD.
  40. # TODO(junyer): Use the FILTER option whenever CMake 3.6 (or newer)
  41. # becomes the minimum required: that will allow this to be concise.
  42. foreach(flag IN LISTS RE2_CFLAGS_OTHER)
  43. if("${flag}" MATCHES "^-std=")
  44. list(REMOVE_ITEM RE2_CFLAGS_OTHER "${flag}")
  45. endif()
  46. endforeach()
  47. set_property(TARGET re2::re2 PROPERTY
  48. INTERFACE_COMPILE_OPTIONS "${RE2_CFLAGS_OTHER}")
  49. endif()
  50. if(RE2_LDFLAGS)
  51. set_property(TARGET re2::re2 PROPERTY
  52. INTERFACE_LINK_LIBRARIES "${RE2_LDFLAGS}")
  53. endif()
  54. message(STATUS "Found RE2 via pkg-config.")
  55. return()
  56. endif()
  57. if(re2_FIND_REQUIRED)
  58. message(FATAL_ERROR "Failed to find RE2.")
  59. elseif(NOT re2_FIND_QUIETLY)
  60. message(WARNING "Failed to find RE2.")
  61. endif()