From 89a9ca86c0ee585e80e1d980f9ffd11f0959e962 Mon Sep 17 00:00:00 2001
From: Julie Brossard <julie.brossard@c-s.fr>
Date: Fri, 21 Aug 2020 14:44:41 +0200
Subject: [PATCH] Revert "[REFAC] Switched tag and always options"

This reverts commit 1870787d3f9c2489032ee3c885119b5b55934c45.
---
 CMake/GetVersionFromGitTag.cmake      | 106 ++++++++++++++++++++++++++
 CMakeLists.txt                        |   6 ++
 Documentation/Cookbook/CMakeLists.txt |   2 +-
 Packaging/CMakeLists.txt              |   8 +-
 Packaging/README.md                   |  22 +++++-
 Utilities/Doxygen/CMakeLists.txt      |   2 +-
 6 files changed, 142 insertions(+), 4 deletions(-)
 create mode 100644 CMake/GetVersionFromGitTag.cmake

diff --git a/CMake/GetVersionFromGitTag.cmake b/CMake/GetVersionFromGitTag.cmake
new file mode 100644
index 0000000000..331635c2bd
--- /dev/null
+++ b/CMake/GetVersionFromGitTag.cmake
@@ -0,0 +1,106 @@
+#
+# This cmake module sets the project version and partial version
+# variables by analysing the git tag and commit history. It expects git
+# tags defined with semantic versioning 2.0.0 (http://semver.org/).
+#
+# The module expects the PROJECT_NAME variable to be set, and recognizes
+# the GIT_FOUND, GIT_EXECUTABLE and VERSION_UPDATE_FROM_GIT variables.
+# If Git is found and VERSION_UPDATE_FROM_GIT is set to boolean TRUE,
+# the project version will be updated using information fetched from the
+# most recent git tag and commit. Otherwise, the module will try to read
+# a VERSION file containing the full and partial versions. The module
+# will update this file each time the project version is updated.
+#
+# Once done, this module will define the following variables:
+#
+# ${PROJECT_NAME}_VERSION_STRING - Version string without metadata
+# such as "v2.0.0" or "v1.2.41-beta.1". This should correspond to the
+# most recent git tag.
+# ${PROJECT_NAME}_VERSION_STRING_FULL - Version string with metadata
+# such as "v2.0.0+3.a23fbc" or "v1.3.1-alpha.2+4.9c4fd1"
+# ${PROJECT_NAME}_VERSION - Same as ${PROJECT_NAME}_VERSION_STRING,
+# without the preceding 'v', e.g. "2.0.0" or "1.2.41-beta.1"
+# ${PROJECT_NAME}_VERSION_MAJOR - Major version integer (e.g. 2 in v2.3.1-RC.2+21.ef12c8)
+# ${PROJECT_NAME}_VERSION_MINOR - Minor version integer (e.g. 3 in v2.3.1-RC.2+21.ef12c8)
+# ${PROJECT_NAME}_VERSION_PATCH - Patch version integer (e.g. 1 in v2.3.1-RC.2+21.ef12c8)
+# ${PROJECT_NAME}_VERSION_TWEAK - Tweak version string (e.g. "RC.2" in v2.3.1-RC.2+21.ef12c8)
+# ${PROJECT_NAME}_VERSION_AHEAD - How many commits ahead of last tag (e.g. 21 in v2.3.1-RC.2+21.ef12c8)
+# ${PROJECT_NAME}_VERSION_GIT_SHA - The git sha1 of the most recent commit (e.g. the "ef12c8" in v2.3.1-RC.2+21.ef12c8)
+#
+# This module is public domain, use it as it fits you best.
+#
+# Author: Nuno Fachada
+
+
+function(get_package_name root_repo_dir project_version_string)
+  
+  if(EXISTS "${root_repo_dir}/.git")  
+    find_package(Git)
+    if(NOT GIT_FOUND)
+      message(ERROR "git not found. Make sure git can be found in your PATH")
+      return()
+    endif()
+    
+    message(STATUS "PROJECT_NAME: ${PROJECT_NAME}")
+    message(STATUS "VERSION MINOR: ${${PROJECT_NAME}_VERSION_MAJOR}")
+    message(STATUS "VERSION MAJOR: ${${PROJECT_NAME}_VERSION_MINOR}")
+    message(STATUS "VERSION PATCH: ${${PROJECT_NAME}_VERSION_PATCH}")
+
+
+    if(DEFINED ENV{CI_COMMIT_REF_NAME})
+      set(branch_name "$ENV{CI_COMMIT_REF_NAME}")
+    else()
+      execute_process(COMMAND ${GIT_EXECUTABLE} symbolic-ref -q HEAD
+        WORKING_DIRECTORY ${root_repo_dir}
+        OUTPUT_VARIABLE git_symbolic_ref_output
+        OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
+
+      set(branch_name)
+      if(git_symbolic_ref_output)
+        get_filename_component(branch_name ${git_symbolic_ref_output} NAME)
+      endif()
+    endif()
+
+    message(STATUS "branch_name: ${branch_name}")
+
+    if("${branch_name}" MATCHES "^release-[0-9]+\\.[0-9]+\$")
+
+      set(${project_version_string} "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}" PARENT_SCOPE)
+
+    else()
+      if(DEFINED ENV{CI_COMMIT_SHORT_SHA})
+        set(${project_version_string} "${branch_name}-$ENV{CI_COMMIT_SHORT_SHA}" PARENT_SCOPE)
+      else()
+        execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
+          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+          OUTPUT_VARIABLE ${PROJECT_NAME}_COMMIT_SHA_STRING
+          OUTPUT_STRIP_TRAILING_WHITESPACE)
+        set(${project_version_string} "${branch_name}-${${PROJECT_NAME}_COMMIT_SHA_STRING}" PARENT_SCOPE)
+      endif()
+
+    endif()
+
+  else()
+
+    # Standalone source directory, get version from RELEASE_NOTE file
+    file(STRINGS ${root_repo_dir}/RELEASE_NOTES.txt RN_FIRSTLINE LIMIT_COUNT 1)
+    message(STATUS "${RN_FIRSTLINE}")
+    string(REPLACE " " ";" RN_FIRSTLINE_LIST ${RN_FIRSTLINE})
+    list(GET RN_FIRSTLINE_LIST 1 PROJECT_VERSION_STRING)
+    message(STATUS ${PROJECT_VERSION_STRING})
+
+    # Get partial versions into a list
+    string(REGEX MATCHALL "-.*$|[0-9]+" PARTIAL_VERSION_LIST
+      ${PROJECT_VERSION_STRING})
+
+    list(GET PARTIAL_VERSION_LIST 0 _VERSION_MAJOR)
+    list(GET PARTIAL_VERSION_LIST 1 _VERSION_MINOR)
+    list(GET PARTIAL_VERSION_LIST 2 _VERSION_PATCH)
+    
+    message(STATUS "M: ${_VERSION_MAJOR}, m: ${_VERSION_MINOR}, p: ${_VERSION_PATCH}")
+
+	  set(${project_version_string} "${PROJECT_VERSION_STRING}" PARENT_SCOPE)
+    
+  endif()
+
+endfunction()
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 570bee7cff..c8df73beca 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -89,6 +89,7 @@ mark_as_advanced(OTB_APPLICATIONS_NAME_LIST)
 set(OTB_CMAKE_DIR ${OTB_SOURCE_DIR}/CMake)
 set(CMAKE_MODULE_PATH ${OTB_CMAKE_DIR} ${CMAKE_MODULE_PATH})
 include(SourceStatus)
+include(GetVersionFromGitTag)
 include(PreventInSourceBuilds)
 include(PreventInBuildInstalls)
 include(OTBModuleMacros)
@@ -136,6 +137,11 @@ set(OTB_VERSION_MINOR "1")
 set(OTB_VERSION_PATCH "0")
 set(OTB_VERSION_STRING "${OTB_VERSION_MAJOR}.${OTB_VERSION_MINOR}.${OTB_VERSION_PATCH}")
 
+get_package_name(${OTB_SOURCE_DIR} OTB_VERSION_STRING2)
+
+message(STATUS "## ${OTB_VERSION_STRING2} ##")
+
+
 # Monteverdi version number (follows OTB)
 set( Monteverdi_VERSION_MAJOR ${OTB_VERSION_MAJOR} )
 set( Monteverdi_VERSION_MINOR ${OTB_VERSION_MINOR} )
diff --git a/Documentation/Cookbook/CMakeLists.txt b/Documentation/Cookbook/CMakeLists.txt
index edfb6a76fd..ae504c7993 100644
--- a/Documentation/Cookbook/CMakeLists.txt
+++ b/Documentation/Cookbook/CMakeLists.txt
@@ -151,7 +151,7 @@ add_custom_target(CookBookArchive
   ALL
   COMMAND ${TAR_COMMAND}
   --transform "s/^html/CookBook-${OTB_VERSION_MAJOR}.${OTB_VERSION_MINOR}/"
-  -czf ${CMAKE_BINARY_DIR}/CookBook-${OTB_VERSION_MAJOR}.${OTB_VERSION_MINOR}-html.tar.gz html
+  -czf ${CMAKE_BINARY_DIR}/CookBook-${OTB_VERSION_STRING2}-html.tar.gz html
   WORKING_DIRECTORY ${RST_BUILD_DIR}
   DEPENDS CookBookHTML
   COMMENT "Creating archive for html CookBook")
diff --git a/Packaging/CMakeLists.txt b/Packaging/CMakeLists.txt
index cd18967e06..4a92a0d123 100644
--- a/Packaging/CMakeLists.txt
+++ b/Packaging/CMakeLists.txt
@@ -106,9 +106,15 @@ if(OTB_TARGET_SYSTEM_ARCH_IS_X64)
 set(PACKAGE_ARCH "64")
 endif()
 
+
+include(${PACKAGE_OTB_SRC_DIR}/CMake/GetVersionFromGitTag.cmake)
+get_package_name(${PACKAGE_OTB_SRC_DIR} OTB_VERSION_STRING2)
+message(STATUS "OTB_VERSION_STRING2 : ${OTB_VERSION_STRING2}")
+
+
 # This directory is important.
 # We stage/keep files that goes into final package in this directory
-set(PKG_STAGE_DIR ${PACKAGE_NAME}-${PKG_OTB_VERSION_STRING}-${PACKAGE_PLATFORM_NAME}${PACKAGE_ARCH}${NAME_SUFFIX})
+set(PKG_STAGE_DIR ${PACKAGE_NAME}-${OTB_VERSION_STRING2}-${PACKAGE_PLATFORM_NAME}${PACKAGE_ARCH}${NAME_SUFFIX})
 
 set(PATCHELF_PROGRAM "${CMAKE_BINARY_DIR}/PATCHELF/src/patchelf/src/patchelf")
 include(External_patchelf)
diff --git a/Packaging/README.md b/Packaging/README.md
index df83d58b21..64c292a963 100644
--- a/Packaging/README.md
+++ b/Packaging/README.md
@@ -1 +1,21 @@
-OTB recipes to build standalone binary packages for Windows/Linux/Mac OS X
+OTB recipes to build standalone binary packages for Windows/Linux/Mac OS X.
+
+
+
+##### Create a binary package
+
+After compiling you can generate a standalone binary package for OTB-Applications using this command in the Packaging folder:
+
+```
+cmake -DSUPERBUILD_BINARY_DIR=<PathToSuperbuildBinaryDir> \
+      -DSUPERBUILD_INSTALL_DIR=<PathToSuperbuildInstallDir> \
+      -DCMAKE_INSTALL_PREFIX=`pwd`/../../install
+
+make 
+make install
+
+```
+
+This will create the package OTB-*.run inside the *pwd`/../../install* folder.
+
+
diff --git a/Utilities/Doxygen/CMakeLists.txt b/Utilities/Doxygen/CMakeLists.txt
index 4cb046702a..79bfc9e3f1 100644
--- a/Utilities/Doxygen/CMakeLists.txt
+++ b/Utilities/Doxygen/CMakeLists.txt
@@ -176,7 +176,7 @@ if (BUILD_DOCUMENTATION)
   
   add_custom_target(Documentation
     COMMAND ${CMAKE_COMMAND} -E tar cjf
-      ${OTB_BINARY_DIR}/Documentation/Doxygen/OTB-Doxygen-${OTB_VERSION_MAJOR}.${OTB_VERSION_MINOR}.tar.bz2
+      ${OTB_BINARY_DIR}/Documentation/Doxygen/OTB-Doxygen-${OTB_VERSION_STRING2}.tar.bz2
       ${OTB_BINARY_DIR}/Documentation/Doxygen/html
     DEPENDS Documentation-doxygen
     WORKING_DIRECTORY ${OTB_BINARY_DIR}/Documentation/Doxygen
-- 
GitLab