From 00650506922437271d4ed1a08b7365694108bd21 Mon Sep 17 00:00:00 2001
From: Andrej Rode <mail@andrejro.de>
Date: Sat, 6 Jun 2020 14:56:09 +0200
Subject: [PATCH] cmake: use TIMESTAMP function for build date and generate
 constants once

Previously a a custom python command was used to generate the build date
but this can be done with native CMake functions for all supported CMake
versions.

Additionally it's compatible with reproducible builds since CMake 3.8 as
it honors SOURCE_DATE_EPOCH set in the environment varibales.

Thanks to Sebastian Kosloswki for pointing out the right way in CMake
---
 CMakeLists.txt                      | 19 ++++++++++++++++++-
 gnuradio-runtime/lib/CMakeLists.txt | 21 ---------------------
 2 files changed, 18 insertions(+), 22 deletions(-)

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -578,7 +578,24 @@
   ${CMAKE_CURRENT_BINARY_DIR}/gnuradio-runtime/include/gnuradio/config.h
 )
 
-#Re-generate the constants file, now that we actually know which components will be enabled.
+########################################################################
+# Handle the generated constants
+########################################################################
+
+# Use TIMESTAMP to be compatible with reproducible builds
+# and put in in the cache so configure_file sees it
+string(TIMESTAMP BUILD_DATE "%a, %d %b %Y %H:%M:%S")
+set(BUILD_DATE ${BUILD_DATE} CACHE INTERNAL "Build date")
+message(STATUS "Loading build date ${BUILD_DATE} into constants...")
+message(STATUS "Loading version ${VERSION} into constants...")
+
+#double escape for windows backslash path separators
+string(REPLACE "\\" "\\\\" prefix "${prefix}")
+string(REPLACE "\\" "\\\\" SYSCONFDIR "${SYSCONFDIR}")
+string(REPLACE "\\" "\\\\" GR_PREFSDIR "${GR_PREFSDIR}")
+
+
+#Generate the constants file, now that we actually know which components will be enabled.
 configure_file(
     ${CMAKE_CURRENT_SOURCE_DIR}/gnuradio-runtime/lib/constants.cc.in
     ${CMAKE_CURRENT_BINARY_DIR}/gnuradio-runtime/lib/constants.cc
--- a/gnuradio-runtime/lib/CMakeLists.txt
+++ b/gnuradio-runtime/lib/CMakeLists.txt
@@ -20,21 +20,6 @@
 include(GrMiscUtils)
 gr_check_hdr_n_def(sys/resource.h HAVE_SYS_RESOURCE_H)
 
-########################################################################
-# Handle the generated constants
-########################################################################
-execute_process(COMMAND ${PYTHON_EXECUTABLE} -c
-    "import time;print(time.strftime('%a, %d %b %Y %H:%M:%S', time.gmtime()))"
-    OUTPUT_VARIABLE BUILD_DATE OUTPUT_STRIP_TRAILING_WHITESPACE
-)
-message(STATUS "Loading build date ${BUILD_DATE} into constants...")
-message(STATUS "Loading version ${VERSION} into constants...")
-
-#double escape for windows backslash path separators
-string(REPLACE "\\" "\\\\" prefix "${prefix}")
-string(REPLACE "\\" "\\\\" SYSCONFDIR "${SYSCONFDIR}")
-string(REPLACE "\\" "\\\\" GR_PREFSDIR "${GR_PREFSDIR}")
-
 
 #########################################################################
 # Include subdirs rather to populate to the sources lists.
@@ -44,12 +29,6 @@
 ########################################################################
 # Setup library
 ########################################################################
-configure_file(
-    ${CMAKE_CURRENT_SOURCE_DIR}/constants.cc.in
-    ${CMAKE_CURRENT_BINARY_DIR}/constants.cc
-    ESCAPE_QUOTES
-    @ONLY)
-
 add_library(gnuradio-runtime
   ${CMAKE_CURRENT_BINARY_DIR}/constants.cc
   basic_block.cc
