From c797630cb3b1e12cefbc9978d7144b83d46ed4df Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 31 Mar 2019 10:14:26 +0300
Subject: [PATCH] Refactor cmake configure files

---
 .gitignore                         |  3 ++-
 CMakeLists.txt                     | 32 ++++++++++++++++++++++--------
 cmake/Findglog.cmake               |  2 +-
 cmake/git_version.cmake            |  5 +++--
 common/cpp/include/ftl/config.h.in | 24 ++++++++++++++++++++++
 common/cpp/src/config.cpp.in       |  9 +++++++++
 cv-node/CMakeLists.txt             |  5 +----
 cv-node/include/ftl/config.h.in    | 18 -----------------
 cv-node/src/calibrate.cpp          | 12 +++++------
 cv-node/src/main.cpp               |  8 +++++++-
 10 files changed, 77 insertions(+), 41 deletions(-)
 create mode 100644 common/cpp/include/ftl/config.h.in
 create mode 100644 common/cpp/src/config.cpp.in
 delete mode 100644 cv-node/include/ftl/config.h.in

diff --git a/.gitignore b/.gitignore
index a95be5d02..1a54ff158 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 */build
 */node_modules
-*/include/ftl/config.h
+**/include/ftl/config.h
+**/src/config.cpp
 **/*.blend1
 fabric/
 fabric-js/
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a7a01fb96..f30fecaf9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,8 @@ find_package( OpenCV REQUIRED )
 find_package( Threads REQUIRED )
 find_package( LibSGM )
 
+include_directories(${PROJECT_SOURCE_DIR}/common/cpp/include)
+
 check_language(CUDA)
 if (CUDA_FOUND)
 enable_language(CUDA)
@@ -31,10 +33,10 @@ include(git_version)
 
 if (WIN32)
 	file(TO_CMAKE_PATH "$ENV{USERPROFILE}" ENV_USERPROFILE)
-	set(FTL_CONFIG_ROOT "\"${ENV_USERPROFILE}/AppData/ftl\"")
-	message(STATUS "Config root is ${FTL_CONFIG_ROOT}")
-	set(FTL_CACHE_ROOT "\"$ENV{USERPROFILE}/AppData/ftl\"")
-	set(FTL_DATA_ROOT "\"$ENV{USERPROFILE}/AppData/ftl\"")
+	set(FTL_LOCAL_CONFIG_ROOT "\"${ENV_USERPROFILE}/AppData/ftl\"")
+	message(STATUS "Config root is ${FTL_LOCAL_CONFIG_ROOT}")
+	set(FTL_LOCAL_CACHE_ROOT "\"$ENV{USERPROFILE}/AppData/ftl\"")
+	set(FTL_LOCAL_DATA_ROOT "\"$ENV{USERPROFILE}/AppData/ftl\"")
 	add_definitions(-DWIN32)
 
 	set(CMAKE_CXX_FLAGS "/std:c++17")
@@ -44,10 +46,16 @@ endif (WIN32)
 
 if (UNIX)
 	add_definitions(-DUNIX)
-	set(FTL_CONFIG_ROOT "\"$ENV{HOME}/.config/ftl\"")
-	message(STATUS "Config root is ${FTL_CONFIG_ROOT}")
-	set(FTL_CACHE_ROOT "\"$ENV{HOME}/.cache/ftl\"")
-	set(FTL_DATA_ROOT "\"$ENV{HOME}/.local/share/ftl\"")
+	
+	set(FTL_LOCAL_CONFIG_ROOT "\"$ENV{HOME}/.config/ftl\"")
+	message(STATUS "Config root is ${FTL_LOCAL_CONFIG_ROOT}")
+	set(FTL_LOCAL_CACHE_ROOT "\"$ENV{HOME}/.cache/ftl\"")
+	set(FTL_LOCAL_DATA_ROOT "\"$ENV{HOME}/.local/share/ftl\"")
+	
+	set(FTL_GLOBAL_CONFIG_ROOT "\"${CMAKE_INSTALL_PREFIX}/share/ftl\"")
+	message(STATUS "Global config root is ${FTL_GLOBAL_CONFIG_ROOT}")
+	set(FTL_GLOBAL_CACHE_ROOT "\"${CMAKE_INSTALL_PREFIX}/share/ftl\"")
+	set(FTL_GLOBAL_DATA_ROOT "\"${CMAKE_INSTALL_PREFIX}/share/ftl\"")
 
 	set(CMAKE_CXX_FLAGS "-std=c++17")
 	set(CMAKE_CXX_FLAGS_DEBUG "-D_DEBUG -pg -Wall")
@@ -59,3 +67,11 @@ SET(CMAKE_USE_RELATIVE_PATHS ON)
 add_subdirectory(net/cpp)
 add_subdirectory(cv-node)
 
+configure_file(${CMAKE_SOURCE_DIR}/common/cpp/include/ftl/config.h.in
+               ${CMAKE_SOURCE_DIR}/common/cpp/include/ftl/config.h
+)
+
+configure_file(${CMAKE_SOURCE_DIR}/common/cpp/src/config.cpp.in
+               ${CMAKE_SOURCE_DIR}/common/cpp/src/config.cpp
+)
+
diff --git a/cmake/Findglog.cmake b/cmake/Findglog.cmake
index e4e0eea8c..dc0bf0bd3 100644
--- a/cmake/Findglog.cmake
+++ b/cmake/Findglog.cmake
@@ -33,5 +33,5 @@ if(GLOG_FOUND)
 	include_directories(${GLOG_INCLUDE_DIRS})
     set(GLOG_FOUND TRUE CACHE BOOL "" FORCE)
     set(GLOG_LIBRARIES ${GLOG_LIBRARY})
-    message(STATUS "Found glog)")
+    message(STATUS "Found glog")
 endif()
diff --git a/cmake/git_version.cmake b/cmake/git_version.cmake
index 14ce6dfc1..f44754522 100644
--- a/cmake/git_version.cmake
+++ b/cmake/git_version.cmake
@@ -4,7 +4,7 @@ if (Git_FOUND)
 	#CHECK_REQUIRED_VARIABLE(GIT_EXECUTABLE)
 
 	execute_process(COMMAND
-        "${GIT_EXECUTABLE}" describe --contains HEAD
+        "${GIT_EXECUTABLE}" describe --tags
         WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
         RESULT_VARIABLE GIT_RESULT
         OUTPUT_VARIABLE VERSION
@@ -14,7 +14,8 @@ if (Git_FOUND)
 	string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" ftl_VERSION_MAJOR "${VERSION}")
 	string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" ftl_VERSION_MINOR "${VERSION}")
 	string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" ftl_VERSION_PATCH "${VERSION}")
-	string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+(.*)" "\\1" ftl_VERSION_SHA1 "${VERSION}")
+	string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+-([0-9]+).*" "\\1" ftl_VERSION_COMMITS "${VERSION}")
+	string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+-[0-9]+-(.*)" "\\1" ftl_VERSION_SHA1 "${VERSION}")
 	set(FTL_VERSION "\"${ftl_VERSION_MAJOR}.${ftl_VERSION_MINOR}.${ftl_VERSION_PATCH}\"")
 
 else()
diff --git a/common/cpp/include/ftl/config.h.in b/common/cpp/include/ftl/config.h.in
new file mode 100644
index 000000000..d3e216603
--- /dev/null
+++ b/common/cpp/include/ftl/config.h.in
@@ -0,0 +1,24 @@
+#ifndef __FTL_CONFIG_H__
+#define __FTL_CONFIG_H__
+
+#cmakedefine HAVE_VIZ
+#cmakedefine HAVE_OPENCVCUDA
+#cmakedefine HAVE_URIPARSESINGLE
+
+extern const char *FTL_VERSION_LONG;
+extern const char *FTL_VERSION;
+extern const int FTL_VERSION_MAJOR;
+extern const int FTL_VERSION_MINOR;
+extern const int FTL_VERSION_PATCH;
+extern const int FTL_VERSION_COMMITS;
+extern const char *FTL_VERSION_SHA1;
+
+#define FTL_LOCAL_CONFIG_ROOT @FTL_LOCAL_CONFIG_ROOT@
+#define FTL_LOCAL_CACHE_ROOT @FTL_LOCAL_CACHE_ROOT@
+#define FTL_LOCAL_DATA_ROOT @FTL_LOCAL_DATA_ROOT@
+
+#define FTL_GLOBAL_CONFIG_ROOT @FTL_GLOBAL_CONFIG_ROOT@
+#define FTL_GLOBAL_CACHE_ROOT @FTL_GLOBAL_CACHE_ROOT@
+#define FTL_GLOBAL_DATA_ROOT @FTL_GLOBAL_DATA_ROOT@
+
+#endif // __FTL_CONFIG_H__
diff --git a/common/cpp/src/config.cpp.in b/common/cpp/src/config.cpp.in
new file mode 100644
index 000000000..353e7ccba
--- /dev/null
+++ b/common/cpp/src/config.cpp.in
@@ -0,0 +1,9 @@
+
+const char *FTL_VERSION_LONG = "@VERSION@";
+const char *FTL_VERSION = @FTL_VERSION@;
+const int FTL_VERSION_MAJOR = @ftl_VERSION_MAJOR@;
+const int FTL_VERSION_MINOR = @ftl_VERSION_MINOR@;
+const int FTL_VERSION_PATCH = @ftl_VERSION_PATCH@;
+const int FTL_VERSION_COMMITS = @ftl_VERSION_COMMITS@;
+const char *FTL_VERSION_SHA1 = "@ftl_VERSION_SHA1@";
+
diff --git a/cv-node/CMakeLists.txt b/cv-node/CMakeLists.txt
index 0f7c6d64f..5f5457c82 100644
--- a/cv-node/CMakeLists.txt
+++ b/cv-node/CMakeLists.txt
@@ -7,11 +7,8 @@ set(CMAKE_REQUIRED_INCLUDES ${OpenCV_INCLUDE_DIRS})
 check_include_file_cxx("opencv2/viz.hpp" HAVE_VIZ)
 check_include_file_cxx("opencv2/cudastereo.hpp" HAVE_OPENCVCUDA)
 
-configure_file(${CMAKE_SOURCE_DIR}/cv-node/include/ftl/config.h.in
-               ${CMAKE_SOURCE_DIR}/cv-node/include/ftl/config.h
-)
-
 set(CVNODESRC
+	../common/cpp/src/config.cpp
 	src/main.cpp
 	src/calibrate.cpp
 	src/local.cpp
diff --git a/cv-node/include/ftl/config.h.in b/cv-node/include/ftl/config.h.in
deleted file mode 100644
index bd68be9df..000000000
--- a/cv-node/include/ftl/config.h.in
+++ /dev/null
@@ -1,18 +0,0 @@
-#ifndef __FTL_CONFIG_H__
-#define __FTL_CONFIG_H__
-
-#cmakedefine HAVE_VIZ
-#cmakedefine HAVE_OPENCVCUDA
-
-#define FTL_VERSION_LONG "@VERSION@"
-#define FTL_VERSION @FTL_VERSION@
-#define FTL_VERSION_MAJOR @ftl_VERSION_MAJOR@
-#define FTL_VERSION_MINOR @ftl_VERSION_MINOR@
-#define FTL_VERSION_PATCH @ftl_VERSION_PATCH@
-#define FTL_VERSION_SHA1 "@ftl_VERSION_SHA1@"
-
-#define FTL_CONFIG_ROOT @FTL_CONFIG_ROOT@
-#define FTL_CACHE_ROOT @FTL_CACHE_ROOT@
-#define FTL_DATA_ROOT @FTL_DATA_ROOT@
-
-#endif // __FTL_CONFIG_H__
diff --git a/cv-node/src/calibrate.cpp b/cv-node/src/calibrate.cpp
index cb66dad51..22b5d59b3 100644
--- a/cv-node/src/calibrate.cpp
+++ b/cv-node/src/calibrate.cpp
@@ -206,7 +206,7 @@ bool Calibrate::_loadCalibration() {
     Rect roi1, roi2;
     
     // reading intrinsic parameters
-    FileStorage fs(FTL_CONFIG_ROOT "/intrinsics.yml", FileStorage::READ);
+    FileStorage fs(FTL_LOCAL_CONFIG_ROOT "/intrinsics.yml", FileStorage::READ);
     if(!fs.isOpened())
     {
         LOG(WARNING) << "Calibration file not found";
@@ -222,7 +222,7 @@ bool Calibrate::_loadCalibration() {
     M1 *= scale;
     M2 *= scale;
 
-    fs.open(FTL_CONFIG_ROOT "/extrinsics.yml", FileStorage::READ);
+    fs.open(FTL_LOCAL_CONFIG_ROOT "/extrinsics.yml", FileStorage::READ);
     if(!fs.isOpened())
     {
         LOG(WARNING) << "Calibration file not found";
@@ -289,7 +289,7 @@ bool Calibrate::recalibrate() {
 		LOG(INFO) << "... done with RMS error=" << rms;
 		
 		// save intrinsic parameters
-		FileStorage fs(FTL_CONFIG_ROOT "/intrinsics.yml", FileStorage::WRITE);
+		FileStorage fs(FTL_LOCAL_CONFIG_ROOT "/intrinsics.yml", FileStorage::WRITE);
 		if( fs.isOpened() )
 		{
 		    fs << "M1" << cameraMatrix[0] << "D1" << distCoeffs[0] <<
@@ -307,7 +307,7 @@ bool Calibrate::recalibrate() {
 		              imageSize[0], R, T, R1, R2, P1, P2, Q_,
 		              CALIB_ZERO_DISPARITY, 1, imageSize[0], &validRoi[0], &validRoi[1]);
 
-		fs.open(FTL_CONFIG_ROOT "/extrinsics.yml", FileStorage::WRITE);
+		fs.open(FTL_LOCAL_CONFIG_ROOT "/extrinsics.yml", FileStorage::WRITE);
 		if( fs.isOpened() )
 		{
 		    fs << "R" << R << "T" << T << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q_;
@@ -376,13 +376,13 @@ bool Calibrate::_recalibrate(vector<vector<Point2f>> *imagePoints,
         LOG(INFO) << "Grabbing calibration image...";
         
         if (view[0].empty() || (local_->isStereo() && view[1].empty()) || imagePoints[0].size() >= (size_t)settings_.nrFrames) {
-        	settings_.outputFileName = FTL_CONFIG_ROOT "/cam0.xml";
+        	settings_.outputFileName = FTL_LOCAL_CONFIG_ROOT "/cam0.xml";
         	bool r = runCalibration(settings_, imageSize[0],
         					cameraMatrix[0], distCoeffs[0], imagePoints[0],
         					grid_width, release_object);
         					
         	if (local_->isStereo()) {
-		    	settings_.outputFileName = FTL_CONFIG_ROOT "/cam1.xml";
+		    	settings_.outputFileName = FTL_LOCAL_CONFIG_ROOT "/cam1.xml";
 		    	r &= runCalibration(settings_, imageSize[1],
 		    					cameraMatrix[1], distCoeffs[1], imagePoints[1],
 		    					grid_width, release_object);
diff --git a/cv-node/src/main.cpp b/cv-node/src/main.cpp
index 124801498..0eb2dc6f2 100644
--- a/cv-node/src/main.cpp
+++ b/cv-node/src/main.cpp
@@ -36,7 +36,7 @@ static json config;
  */
 static bool findConfiguration(const string &file) {
 	// TODO Check other locations
-	ifstream i((file != "") ? file : FTL_CONFIG_ROOT "/config.json");
+	ifstream i((file != "") ? file : FTL_LOCAL_CONFIG_ROOT "/config.json");
 	if (!i.is_open()) return false;
 	i >> config;
 	return true;
@@ -74,6 +74,12 @@ static void process_options(const map<string,string> &opts) {
 	for (auto opt : opts) {
 		if (opt.first == "config") continue;
 		
+		if (opt.first == "version") {
+			std::cout << "FTL Vision Node - v" << FTL_VERSION << std::endl;
+			std::cout << FTL_VERSION_LONG << std::endl;
+			exit(0);
+		}
+		
 		try {
 			auto ptr = json::json_pointer("/"+opt.first);
 			// TODO Allow strings without quotes
-- 
GitLab