Enable Tint as part of a larger CMake project

- Add googletest and spirv-tools local builds only if their CMake targets
  are not already configured.
- Add local spirv-headers as an include directory only if the associated
  CMake directory does not already point to a directory.

Change-Id: I53ebb0d8fdf7703e8219000b78c75c30859c91a4
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24020
Reviewed-by: dan sinclair <dsinclair@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e248444..088ca4f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -81,16 +81,18 @@
   set(COMPILER_IS_LIKE_GNU TRUE)
 endif()
 
-find_package(Doxygen)
-if(DOXYGEN_FOUND)
-  add_custom_target(tint-docs ALL
-      COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
-      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-      COMMENT "Generating API documentation"
-      VERBATIM)
-else()
-  message("Doxygen not found. Skipping documentation")
-endif()
+if(${TINT_BUILD_DOCS})
+  find_package(Doxygen)
+  if(DOXYGEN_FOUND)
+    add_custom_target(tint-docs ALL
+        COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
+        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+        COMMENT "Generating API documentation"
+        VERBATIM)
+  else()
+    message("Doxygen not found. Skipping documentation")
+  endif(DOXYGEN_FOUND)
+endif(TINT_BUILD_DOCS)
 
 if(MSVC)
   # We don't want to have to copy the C Runtime DLL everywhere the executable
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index 5928c2d..2cc8083 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -12,10 +12,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest EXCLUDE_FROM_ALL)
+if (NOT TARGET gmock)
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/googletest EXCLUDE_FROM_ALL)
+endif()
 
 if(${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
-  set(SPIRV-Headers_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers CACHE STRING "")
-  set(SPIRV_SKIP_TESTS ON CACHE BOOL ON)
-  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-tools)
+  if (NOT IS_DIRECTORY "${SPIRV-Headers_SOURCE_DIR}")
+    set(SPIRV-Headers_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers CACHE STRING "")
+  endif()
+
+  if (NOT TARGET SPIRV-Tools)
+    set(SPIRV_SKIP_TESTS ON CACHE BOOL ON)
+    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-tools)
+  endif()
 endif()