From d0ada518e50322a1d2cfda9ce1225922bf1a16ed Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 18 Sep 2018 11:43:31 +0200
Subject: [PATCH] [CMake] Added ParseCMakeArgs.cmake

Stores user-given CMake variables from the recent CMake run into cmake-args file. Install cmake-args file and CMakeCache.txt.
---
 CMakeLists.txt                     |  1 +
 scripts/cmake/ParseCMakeArgs.cmake | 40 ++++++++++++++++++++++++++++++
 scripts/cmake/packaging/Pack.cmake |  3 +++
 3 files changed, 44 insertions(+)
 create mode 100644 scripts/cmake/ParseCMakeArgs.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6a889ee6fd5..16dda92348c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,7 @@ endif()
 project( OGS-6 )
 
 include(scripts/cmake/CMakeSetup.cmake)
+include(ParseCMakeArgs)
 
 ###########################
 ### Preliminary Options ###
diff --git a/scripts/cmake/ParseCMakeArgs.cmake b/scripts/cmake/ParseCMakeArgs.cmake
new file mode 100644
index 00000000000..ed56906582e
--- /dev/null
+++ b/scripts/cmake/ParseCMakeArgs.cmake
@@ -0,0 +1,40 @@
+# Implementation from https://stackoverflow.com/questions/10205986
+#
+# Captures not-yet cached CMake variables.
+# On first CMake run via cmake-cli this works as expected.
+# Once the variables are cached this will not work anymore (and is
+# therefore skipped).
+#
+# When running CMake -D.. passed args can be retrieved with by the
+# CACHE_VARIABLES CMake property. On sub-sequent CMake runs it is
+# no longer possible to differentiate between variables already cached
+# by option()-calls and variables passed to the CMake call with -D..
+#
+# A (cached) map data structure would solve this.
+# Tried the following map implementations without luck:
+# - https://github.com/toeb/cmakepp (not cached)
+# - https://github.com/j3lamp/mcl (did not work at all)
+
+if(EXISTS ${PROJECT_BINARY_DIR}/CMakeCache.txt)
+    set(CMAKE_ARGS "Information not available.")
+    set(CMAKE_ARGS_ESCAPED ${CMAKE_ARGS})
+    file(REMOVE ${PROJECT_BINARY_DIR}/cmake-args)
+    return()
+endif()
+
+get_cmake_property(CACHE_VARS CACHE_VARIABLES)
+foreach(CACHE_VAR ${CACHE_VARS})
+  get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
+  if(CACHE_VAR_HELPSTRING STREQUAL "No help, variable specified on the command line.")
+    get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
+    if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
+      set(CACHE_VAR_TYPE)
+    else()
+      set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
+    endif()
+    set(CMAKE_ARGS "${CMAKE_ARGS} -D${CACHE_VAR}${CACHE_VAR_TYPE}=\"${${CACHE_VAR}}\"")
+  endif()
+endforeach()
+
+string(REPLACE "\"" "\\\"" CMAKE_ARGS_ESCAPED ${CMAKE_ARGS})
+file(WRITE ${PROJECT_BINARY_DIR}/cmake-args "${CMAKE_ARGS}\n")
diff --git a/scripts/cmake/packaging/Pack.cmake b/scripts/cmake/packaging/Pack.cmake
index 3ef648bf2d4..0ef317ec54a 100644
--- a/scripts/cmake/packaging/Pack.cmake
+++ b/scripts/cmake/packaging/Pack.cmake
@@ -126,3 +126,6 @@ endif()
 
 configure_file(Documentation/README.txt.in ${PROJECT_BINARY_DIR}/README.txt)
 install(FILES ${PROJECT_BINARY_DIR}/README.txt DESTINATION .)
+
+install(FILES ${PROJECT_BINARY_DIR}/CMakeCache.txt DESTINATION ${CMAKE_INSTALL_INFODIR})
+install(FILES ${PROJECT_BINARY_DIR}/cmake-args DESTINATION ${CMAKE_INSTALL_INFODIR})
-- 
GitLab