Call `find_package()` conditionally.

If a top-level project has called `add_directory(abseil-cpp)` already (possibly
indirectly), let that take precedence over any copy of Abseil that might have
been installed on the system. And likewise for ICU, GoogleTest and Benchmark.

Fixes #427.

Change-Id: I1902f6fc04df49d029357734116e08ee4a565836
Reviewed-on: https://code-review.googlesource.com/c/re2/+/61350
Reviewed-by: Paul Wankadia <junyer@google.com>
Reviewed-by: Alex Chernyakhovsky <achernya@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac15ab7..5312252 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,11 +72,18 @@
     absl_synchronization
     )
 
-find_package(absl REQUIRED)
+# If a top-level project has called add_directory(abseil-cpp) already (possibly
+# indirectly), let that take precedence over any copy of Abseil that might have
+# been installed on the system. And likewise for ICU, GoogleTest and Benchmark.
+if(NOT TARGET absl::base)
+  find_package(absl REQUIRED)
+endif()
 list(APPEND REQUIRES ${ABSL_DEPS})
 
 if(RE2_USE_ICU)
-  find_package(ICU REQUIRED COMPONENTS uc)
+  if(NOT TARGET ICU::uc)
+    find_package(ICU REQUIRED COMPONENTS uc)
+  endif()
   add_definitions(-DRE2_USE_ICU)
   list(APPEND REQUIRES icu-uc)
 endif()
@@ -159,8 +166,12 @@
 endif()
 
 if(RE2_BUILD_TESTING)
-  find_package(GTest REQUIRED)
-  find_package(benchmark REQUIRED)
+  if(NOT TARGET GTest::gtest)
+    find_package(GTest REQUIRED)
+  endif()
+  if(NOT TARGET benchmark::benchmark)
+    find_package(benchmark REQUIRED)
+  endif()
 
   set(TESTING_SOURCES
       re2/testing/backtrack.cc