From e2971c9c16092d699e26ffdd8a0d088875f16c18 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 9 Mar 2021 21:24:48 +0100
Subject: [PATCH] [CMake] Disable formatting targets when formatters not found.

---
 scripts/cmake/Dependencies.cmake | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/scripts/cmake/Dependencies.cmake b/scripts/cmake/Dependencies.cmake
index 2bf2e408f99..e92aa3f81e7 100644
--- a/scripts/cmake/Dependencies.cmake
+++ b/scripts/cmake/Dependencies.cmake
@@ -313,10 +313,19 @@ configure_file(${PROJECT_SOURCE_DIR}/scripts/cmake/test/CTestCustom.in.cmake
     ${PROJECT_BINARY_DIR}/CTestCustom.cmake @ONLY
 )
 
-CPMAddPackage(
-  NAME Format.cmake
-  VERSION 1.7.0
-  GITHUB_REPOSITORY TheLartians/Format.cmake
-  OPTIONS
-    "CMAKE_FORMAT_EXCLUDE scripts/cmake/CPM.cmake|.*/Tests.cmake|scripts/cmake/jedbrown/.*|scripts/cmake/conan/conan.cmake|scripts/cmake/vector-of-bool/.*"
-)
+find_program(CLANG_FORMAT_PROGRAM clang-format)
+find_program(CMAKE_FORMAT_PROGRAM cmake-format)
+
+if(CLANG_FORMAT_PROGRAM OR CMAKE_FORMAT_PROGRAM)
+    if(NOT CMAKE_FORMAT_PROGRAM)
+        set(skip_cmake "FORMAT_SKIP_CMAKE YES")
+    endif()
+    CPMAddPackage(
+      NAME Format.cmake
+      VERSION 1.7.0
+      GITHUB_REPOSITORY TheLartians/Format.cmake
+      OPTIONS
+        ${skip_cmake}
+        "CMAKE_FORMAT_EXCLUDE scripts/cmake/CPM.cmake|.*/Tests.cmake|scripts/cmake/jedbrown/.*|scripts/cmake/conan/conan.cmake|scripts/cmake/vector-of-bool/.*"
+    )
+endif()
-- 
GitLab