From 746fd0c3d9d75e54e3758dea6376a30b6cb74cf9 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 21 Aug 2019 22:23:08 +0200
Subject: [PATCH] [CMake] Made existence of local git repo optional.

This is used to minimize the Docker build context when building
an image with OGS inside from the local source tree.

Supply OGS_VERSION CMake variable.
---
 .dockerignore                      |  2 ++
 scripts/cmake/PreFind.cmake        | 15 ++++++++++++---
 scripts/cmake/ProjectSetup.cmake   |  4 ++++
 scripts/cmake/SubmoduleSetup.cmake |  4 ++++
 4 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 .dockerignore

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 00000000000..59070d1dcb6
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,2 @@
+.git
+Tests/Data
diff --git a/scripts/cmake/PreFind.cmake b/scripts/cmake/PreFind.cmake
index b4ae3a14c15..6c028cb857b 100644
--- a/scripts/cmake/PreFind.cmake
+++ b/scripts/cmake/PreFind.cmake
@@ -16,7 +16,16 @@ execute_process(COMMAND ${GIT_EXECUTABLE} status
     RESULT_VARIABLE IS_GIT_REPO
     OUTPUT_QUIET)
 if(IS_GIT_REPO GREATER 0)
-    message(FATAL_ERROR "Source code at ${PROJECT_SOURCE_DIR} is not a git "
-        "repository! Please use git to obtain the source code! See "
-        "https://www.opengeosys.org/docs/devguide/getting-started/get-the-source-code/")
+    set(IS_GIT_REPO FALSE CACHE INTERNAL "")
+    if(DEFINED OGS_VERSION)
+        message(WARNING "Using user-provided OGS_VERSION: ${OGS_VERSION}!")
+        message(WARNING "Submodule setup is skipped!")
+    else()
+        message(FATAL_ERROR "No git repository found at ${PROJECT_SOURCE_DIR}! "
+            "Please use git to obtain the source code! See "
+            "https://www.opengeosys.org/docs/devguide/getting-started/get-the-source-code/"
+            " OR manually set the OGS_VERSION variable.")
+    endif()
+else()
+set(IS_GIT_REPO TRUE CACHE INTERNAL "")
 endif()
diff --git a/scripts/cmake/ProjectSetup.cmake b/scripts/cmake/ProjectSetup.cmake
index 10225564e67..d0fe0f7f19b 100644
--- a/scripts/cmake/ProjectSetup.cmake
+++ b/scripts/cmake/ProjectSetup.cmake
@@ -28,6 +28,10 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 
 set(CMAKE_MACOSX_RPATH 1)
 
+if(NOT IS_GIT_REPO)
+    return()
+endif()
+
 # Get version info from Git, implementation based on
 # https://github.com/tomtom-international/cpp-dependencies
 execute_process(
diff --git a/scripts/cmake/SubmoduleSetup.cmake b/scripts/cmake/SubmoduleSetup.cmake
index be0d9d32974..14b87e8af07 100644
--- a/scripts/cmake/SubmoduleSetup.cmake
+++ b/scripts/cmake/SubmoduleSetup.cmake
@@ -1,3 +1,7 @@
+if(NOT IS_GIT_REPO)
+    return()
+endif()
+
 # This file initializes the required submodules
 set(OGS_ADDITIONAL_SUBMODULES_TO_CHECKOUT "" CACHE STRING
     "User given submodules which should be checked out by CMake.")
-- 
GitLab