From 1ab8ee8d19eaeda083501e17bc4e044149bcac47 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 29 Jan 2020 19:51:49 +0100
Subject: [PATCH] [CMake] Added version requirements file in json-format.

The file is read by CMake (via new a json parser submodule) and
the web site generator and provides info on required clang and
gcc versions. This info will be expanded in future commits.

Goal is to have a single source of truth for versions infos and
maybe other requirements.

On CMake you access the info via `ogs.`-prefixed variables:

    ${ogs.minimum_version.gcc}

In Hugo you access the info via the new dataFile-shortcode:

    {{< dataFile "versions.minimum_version.gcc" >}}
---
 .gitmodules                                          |  3 +++
 CMakeLists.txt                                       |  1 +
 ThirdParty/json-cmake                                |  1 +
 scripts/cmake/CompilerSetup.cmake                    | 12 ++++++------
 scripts/cmake/SubmoduleSetup.cmake                   |  1 +
 scripts/cmake/Versions.cmake                         |  8 ++++++++
 .../devguide/getting-started/prerequisites.pandoc    |  4 ++--
 web/data/versions.json                               |  6 ++++++
 web/layouts/shortcodes/dataFile.html                 |  9 +++++++++
 9 files changed, 37 insertions(+), 8 deletions(-)
 create mode 160000 ThirdParty/json-cmake
 create mode 100644 scripts/cmake/Versions.cmake
 create mode 100644 web/data/versions.json
 create mode 100644 web/layouts/shortcodes/dataFile.html

diff --git a/.gitmodules b/.gitmodules
index a83e793ed13..f39e4287a28 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -46,3 +46,6 @@
 [submodule "ThirdParty/ogs6py"]
 	path = ThirdParty/ogs6py
 	url = https://github.com/joergbuchwald/ogs6py.git
+[submodule "ThirdParty/json-cmake"]
+	path = ThirdParty/json-cmake
+	url = https://github.com/ufz/json-cmake.git
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d15d2dd0cbf..43c3169781d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -61,6 +61,7 @@ endif()
 option(OGS_USE_CVODE "Use the Sundials CVODE module?" OFF)
 
 # ---- CMake includes ----
+include(Versions)
 include(PreFind)
 include(CheckTypeSizes)
 include(Functions)
diff --git a/ThirdParty/json-cmake b/ThirdParty/json-cmake
new file mode 160000
index 00000000000..9708cb091f6
--- /dev/null
+++ b/ThirdParty/json-cmake
@@ -0,0 +1 @@
+Subproject commit 9708cb091f6b89b94d71ae98f8b9e68ea04c47dd
diff --git a/scripts/cmake/CompilerSetup.cmake b/scripts/cmake/CompilerSetup.cmake
index 03bd4dafc37..c5af43ae39b 100644
--- a/scripts/cmake/CompilerSetup.cmake
+++ b/scripts/cmake/CompilerSetup.cmake
@@ -95,18 +95,18 @@ if(COMPILER_IS_GCC OR COMPILER_IS_CLANG OR COMPILER_IS_INTEL)
     endif()
 
     if(COMPILER_IS_GCC)
-        if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.3")
-            message(FATAL_ERROR "GCC minimum required version is 7.3! You are \
-                using ${CMAKE_CXX_COMPILER_VERSION}.")
+        if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ogs.minimum_version.gcc})
+            message(FATAL_ERROR "GCC minimum required version is ${OGS_GCC_MINIMUM_VERSION}! \
+                You are using ${CMAKE_CXX_COMPILER_VERSION}.")
         endif()
         add_compile_options(-fext-numeric-literals)
         include(GCCSanitizer)
     endif()
 
     if(COMPILER_IS_CLANG)
-        if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5")
-            message(FATAL_ERROR "Aborting: Clang 3.5 is required! Found version \
-                ${CMAKE_CXX_COMPILER_VERSION}")
+        if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ogs.minimum_version.clang})
+            message(FATAL_ERROR "Aborting: Clang ${OGS_CLANG_MINIMUM_VERSION} \
+                is required! Found version ${CMAKE_CXX_COMPILER_VERSION}")
         endif()
         include(ClangSanitizer)
     endif()
diff --git a/scripts/cmake/SubmoduleSetup.cmake b/scripts/cmake/SubmoduleSetup.cmake
index 14b87e8af07..d2bc2ac8f3c 100644
--- a/scripts/cmake/SubmoduleSetup.cmake
+++ b/scripts/cmake/SubmoduleSetup.cmake
@@ -15,6 +15,7 @@ set(REQUIRED_SUBMODULES
     ThirdParty/googletest
     ThirdParty/iphreeqc/src
     ThirdParty/jedbrown-cmake-modules
+    ThirdParty/json-cmake
     ThirdParty/tclap
     ThirdParty/tetgen
     ThirdParty/vtkdiff
diff --git a/scripts/cmake/Versions.cmake b/scripts/cmake/Versions.cmake
new file mode 100644
index 00000000000..7d9eed4199c
--- /dev/null
+++ b/scripts/cmake/Versions.cmake
@@ -0,0 +1,8 @@
+include(${PROJECT_SOURCE_DIR}/ThirdParty/json-cmake/JSONParser.cmake)
+file(READ ${PROJECT_SOURCE_DIR}/web/data/versions.json jsonFileString)
+sbeParseJson(ogs jsonFileString)
+# Provides variables, e.g. ogs.minimum_version.gcc
+# Output all variables with
+#   foreach(var ${ogs})
+#     message("${var} = ${${var}}")
+#   endforeach()
diff --git a/web/content/docs/devguide/getting-started/prerequisites.pandoc b/web/content/docs/devguide/getting-started/prerequisites.pandoc
index 477f64bb4c5..cdf58c4a417 100644
--- a/web/content/docs/devguide/getting-started/prerequisites.pandoc
+++ b/web/content/docs/devguide/getting-started/prerequisites.pandoc
@@ -31,11 +31,11 @@ As we use lots of features of the C++11-standard we support **Visual Studio 2015
 :::
 
 ::: {.linux}
-If you have a recent linux distribution you should also have a recent gcc. Please check that you have at least **gcc 6.2**:
+If you have a recent linux distribution you should also have a recent gcc. Please check that you have at least **gcc {{< dataFile "versions.minimum_version.gcc" >}}**:
 
 ```bash
 $ gcc --version
-gcc (GCC) 6.2.0
+gcc (GCC) {{< dataFile "versions.minimum_version.gcc" >}}.0
 ```
 :::
 
diff --git a/web/data/versions.json b/web/data/versions.json
new file mode 100644
index 00000000000..2f6053556ab
--- /dev/null
+++ b/web/data/versions.json
@@ -0,0 +1,6 @@
+{
+  "minimum_version": {
+    "gcc": "7.3",
+    "clang": "3.5"
+  }
+}
diff --git a/web/layouts/shortcodes/dataFile.html b/web/layouts/shortcodes/dataFile.html
new file mode 100644
index 00000000000..d47c61c71a2
--- /dev/null
+++ b/web/layouts/shortcodes/dataFile.html
@@ -0,0 +1,9 @@
+{{- with .Get 0 -}}
+  {{- $map := split . "." }}
+  {{- $url := index $.Site.Data $map -}}
+  {{- if $url -}}
+{{- $url -}}
+  {{- else -}}
+{{- errorf "No data for key %s" . -}}
+  {{- end -}}
+{{- end -}}
-- 
GitLab