From 97f6087de2eff80a3bc0c9bb11844f2e0c4f4614 Mon Sep 17 00:00:00 2001
From: Alejandro Acosta <alejandro.acosta@codeplay.com>
Date: Wed, 22 Jan 2025 11:45:13 +0000
Subject: [PATCH] Refactor CMake configuration for google benchmark (#190)

Refactor the CMake configuration for fetching Google Benchmark. It also checks if GBench is already populated before fetching.

Co-authored-by: Muhammad Tanvir <muhammad.tanvir@codeplay.com>

---------

Co-authored-by: Muhammad Tanvir <muhammad.tanvir@codeplay.com>
---
 CMakeLists.txt              |  1 +
 benchmarks/CMakeLists.txt   |  8 ------
 cmake/googlebenchmark.cmake | 49 +++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 8 deletions(-)
 create mode 100644 cmake/googlebenchmark.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2d38e50291..d4e55ef9f2 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1047,6 +1047,7 @@ if (CUTLASS_ENABLE_TESTS)
 endif()
 
 if (CUTLASS_ENABLE_BENCHMARKS)
+  include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/googlebenchmark.cmake)
   add_subdirectory(benchmarks)
 endif()
 
diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt
index 6067dd0643..5326c5dde7 100644
--- a/benchmarks/CMakeLists.txt
+++ b/benchmarks/CMakeLists.txt
@@ -27,14 +27,6 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 
-include(FetchContent)
-FetchContent_Declare(
-    googlebenchmark
-    GIT_REPOSITORY https://github.com/google/benchmark.git
-    GIT_TAG v1.9.0
-)
-FetchContent_MakeAvailable(googlebenchmark)
-
 add_custom_target(cutlass_benchmarks)
 
 function(cutlass_benchmark_add_executable NAME)
diff --git a/cmake/googlebenchmark.cmake b/cmake/googlebenchmark.cmake
new file mode 100644
index 0000000000..ac5f26146d
--- /dev/null
+++ b/cmake/googlebenchmark.cmake
@@ -0,0 +1,49 @@
+# Copyright (c) 2024 - 2025 Codeplay Software Ltd. All rights reserved.
+# SPDX-License-Identifier: BSD-3-Clause
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+include(FetchContent)
+
+set(GOOGLEBENCHMARK_DIR "" CACHE STRING "Location of local GoogleBenchmark repo to build against")
+
+if(GOOGLEBENCHMARK_DIR)
+  set(FETCHCONTENT_SOURCE_DIR_GOOGLEBENCHMARK ${GOOGLEBENCHMARK_DIR} CACHE STRING "GoogleBenchmark source directory override")
+endif()
+
+set(GBENCH_REPOSITORY "https://github.com/google/benchmark.git" CACHE STRING "GoogleBench repo to fetch")
+FetchContent_Declare(
+  googlebenchmark
+  GIT_REPOSITORY ${GBENCH_REPOSITORY}
+  GIT_TAG        v1.9.0
+  )
+
+FetchContent_GetProperties(googlebenchmark)
+
+if(NOT googlebenchmark_POPULATED)
+  FetchContent_Populate(googlebenchmark)
+  add_subdirectory(${googlebenchmark_SOURCE_DIR} ${googlebenchmark_BINARY_DIR} EXCLUDE_FROM_ALL)
+endif()