From c09fb8324c85964ff873cb16ad7f56fbdb186e20 Mon Sep 17 00:00:00 2001
From: Rashad Kanavath <rashad.kanavath@c-s.fr>
Date: Mon, 11 Apr 2016 11:59:19 +0200
Subject: [PATCH] PKG: keep packaging macros in sync with otb tree

---
 CMake/PackageHelper.cmake | 186 ++++++++++++++++++++++++++++++++------
 1 file changed, 156 insertions(+), 30 deletions(-)

diff --git a/CMake/PackageHelper.cmake b/CMake/PackageHelper.cmake
index ebac8ce54d..2ff68936fb 100644
--- a/CMake/PackageHelper.cmake
+++ b/CMake/PackageHelper.cmake
@@ -109,6 +109,7 @@ function(install_common include_mvd)
       DEPENDENCIES_INSTALL_DIR
       OTB_APPLICATIONS_DIR
       PKG_STAGE_DIR
+      PACKAGE_SUPPORT_FILES_DIR
       CMAKE_INSTALL_PREFIX
       OTB_INSTALL_DIR
       )
@@ -118,8 +119,25 @@ function(install_common include_mvd)
     set(vars "${vars}  ${req}=[${${req}}]\n")
   endforeach(req)
 
-  #one for debugging..
-#  install(CODE "message(\"CMake/PackageHelper.cmake:install_common(${outdir})\n${vars}\n\")")
+  # one for debugging..
+  # install(CODE "message(\"CMake/PackageHelper.cmake:install_common(${outdir})\n${vars}\n\")")
+
+  ##################### install environment source ##########################
+  if(WIN32 OR CROSS_COMPILING)
+    set(ENV_SOURCE_FILES
+      "${PACKAGE_SUPPORT_FILES_DIR}/otbenv.cmd"
+      "${PACKAGE_SUPPORT_FILES_DIR}/otbenv.profile"
+      )
+
+  elseif(UNIX)
+    set(ENV_SOURCE_FILES "${PACKAGE_SUPPORT_FILES_DIR}/otbenv.profile")
+  endif()
+
+  foreach(ENV_SOURCE_FILE ${ENV_SOURCE_FILES})
+    if(EXISTS ${ENV_SOURCE_FILE})
+      install(FILES ${ENV_SOURCE_FILE} DESTINATION ${PKG_STAGE_DIR})
+    endif()
+  endforeach()
 
   ####################### install cli and gui scripts ###########################
   file(GLOB PKG_APP_SCRIPTS
@@ -198,14 +216,6 @@ function(install_monteverdi_files)
     set(vars "${vars}  ${req}=[${${req}}]\n")
   endforeach(req)
 
-  #message("CMake/PackageHelper.cmake:install_common(${stage_dir})\n${vars}\n")
-
-  ##################### install mingw otbenv.cmd ##########################
-  if(EXISTS ${Monteverdi_SOURCE_DIR}/Packaging/Windows/mingw/otbenv.cmd)
-    install(FILES ${Monteverdi_SOURCE_DIR}/Packaging/Windows/mingw/otbenv.cmd
-      DESTINATION ${PKG_STAGE_DIR})
-  endif()
-
   ####################### install mingw qt.conf ##########################
   if(EXISTS ${Monteverdi_SOURCE_DIR}/Packaging/Windows/mingw/qt.conf)
     install(FILES ${Monteverdi_SOURCE_DIR}/Packaging/Windows/mingw/qt.conf
@@ -233,13 +243,12 @@ function(install_monteverdi_files)
 
 endfunction()
 
-macro(clear_package_staging_directory)
-  message(STATUS "Clearing package staging directory: ${CMAKE_INSTALL_PREFIX}/${PKG_STAGE_DIR}")
+macro(empty_package_staging_directory)
+  message(STATUS "Empty package staging directory: ${CMAKE_INSTALL_PREFIX}/${PKG_STAGE_DIR}")
   execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_INSTALL_PREFIX}/${PKG_STAGE_DIR}")
 endmacro()
 
 function(configure_package)
-
   set(EXE_EXT "")
   set(LIB_EXT "*so")
   set(SCR_EXT ".sh")
@@ -249,10 +258,33 @@ function(configure_package)
     set(SCR_EXT ".bat")
   endif()
 
-  foreach(EXE_FILE
-      monteverdi
+  #This must exist in any OTB Installation minimal or full
+  set(PKG_BINARY_FILES "bin/otbApplicationLauncherCommandLine")
+  set(PKG_PEFILES "${OTB_INSTALL_DIR}/bin/otbApplicationLauncherCommandLine${EXE_EXT}")
+  if(NOT EXISTS "${OTB_INSTALL_DIR}/bin/otbApplicationLauncherCommandLine${EXE_EXT}")
+    message(FATAL_ERROR "${OTB_INSTALL_DIR}/bin/otbApplicationLauncherCommandLine${EXE_EXT} not found.")
+  endif()
+
+  foreach(EXE_FILE otbApplicationLauncherQt
+      iceViewer
+      otbTestDriver)
+    if(EXISTS "${OTB_INSTALL_DIR}/bin/${EXE_FILE}${EXE_EXT}")
+      #see the first comment about PKG_BINARY_FILES
+      set(PKG_BINARY_FILES "${PKG_BINARY_FILES} bin/${EXE_FILE}${EXE_EXT}")
+      list(APPEND PKG_PEFILES
+        "${OTB_INSTALL_DIR}/bin/${EXE_FILE}${EXE_EXT}")
+    endif()
+  endforeach()
+
+  foreach(EXE_FILE monteverdi
       mapla)
     if(EXISTS "${CMAKE_INSTALL_PREFIX}/bin/${EXE_FILE}${EXE_EXT}")
+      #PKG_BINARY_FILES might seem a bit redundant variable if you
+      #consider PKG_PEFILES which also has same content.
+      #But PKG_BINARY_FILES goes into pkgsetup.in for Linux standalone binaries
+      # and other one (PKG_PEFILES) is for dependency resolution in
+      # process_deps() function
+      set(PKG_BINARY_FILES "${PKG_BINARY_FILES} bin/${EXE_FILE}${EXE_EXT}")
       list(APPEND PKG_PEFILES
         "${CMAKE_INSTALL_PREFIX}/bin/${EXE_FILE}${EXE_EXT}")
     endif()
@@ -261,32 +293,30 @@ function(configure_package)
         install(FILES
           ${Monteverdi_SOURCE_DIR}/Packaging/${EXE_FILE}${SCR_EXT}
           DESTINATION
-          "${PKG_STAGE_DIR}")
-    endif()
-  endif()
-
-  endforeach()
-
-  foreach(EXE_FILE otbApplicationLauncherQt
-      iceViewer
-      otbTestDriver
-      otbApplicationLauncherCommandLine)
-    if(EXISTS "${OTB_INSTALL_DIR}/bin/${EXE_FILE}${EXE_EXT}")
-      list(APPEND PKG_PEFILES
-        "${OTB_INSTALL_DIR}/bin/${EXE_FILE}${EXE_EXT}")
+          "${PKG_STAGE_DIR}/bin")
+      endif()
     endif()
   endforeach()
 
   file(GLOB OTB_APPS_LIST ${OTB_APPLICATIONS_DIR}/otbapp_${LIB_EXT}) # /lib/otb
 
+  #see the first comment about PKG_BINARY_FILES
+  foreach(OTB_APP_SO ${OTB_APPS_LIST})
+    get_filename_component(OTB_APP_SO_NAME ${OTB_APP_SO} NAME)
+    set(PKG_BINARY_FILES "${PKG_BINARY_FILES} lib/otb/applications/${OTB_APP_SO_NAME}")
+  endforeach()
+
   set(include_mvd 0)
   if(DEFINED Monteverdi_SOURCE_DIR)
     set(include_mvd 1)
   endif()
 
+  list(APPEND PKG_PEFILES ${OTB_APPS_LIST})
+
   install_common(${include_mvd})
 
-  list(APPEND PKG_PEFILES ${OTB_APPS_LIST})
+  execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/temp_so_names_dir")
+  execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/temp_so_names_dir")
 
   set(alldlls)
   set(notfound_dlls)
@@ -301,4 +331,100 @@ function(configure_package)
     message(FATAL_ERROR "Following dlls were not found: ${notfound_dlls}. Please consider adding their paths to SEARCHDIRS when calling superbuild_package macro.")
   endif()
 
-endfunction()
\ No newline at end of file
+  file(GLOB temp_files "${CMAKE_BINARY_DIR}/temp_so_names_dir/*") # /lib/otb
+  foreach(temp_file ${temp_files})
+    get_filename_component(basename_of_temp_file ${temp_file} NAME)
+    set(PKG_BINARY_FILES "${PKG_BINARY_FILES} lib/${basename_of_temp_file}")
+  endforeach()
+
+  #remove this temporary directory
+  execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_BINARY_DIR}/temp_so_names_dir")
+
+  set(PKG_BINARY_FILES "${PKG_BINARY_FILES}" PARENT_SCOPE)
+
+endfunction()
+
+# The below function is modified from GetPrerequisities.cmake
+# which is distributed with CMake.
+function(is_file_a_symbolic_link file result_var1 result_var2)
+  #
+  # A file is not executable until proven otherwise:
+  #
+  set(${result_var1} 0 PARENT_SCOPE)
+  set(${result_var2} "" PARENT_SCOPE)
+
+  get_filename_component(file_full "${file}" ABSOLUTE)
+  string(TOLOWER "${file_full}" file_full_lower)
+
+  # If file name ends in .exe on Windows, *assume* executable:
+  #
+  if(WIN32 AND NOT UNIX)
+    if("${file_full_lower}" MATCHES "\\.lnk$")
+      set(${result_var1} 1 PARENT_SCOPE)
+      #Assuming the file is linked to a file with same name without .lnk extension
+      get_filename_component(name_we_lnk "${file_full_lower}" NAME_WE)
+      set(${result_var2} "${name_we_lnk}" PARENT_SCOPE)
+      return()
+    endif()
+
+    # A clause could be added here that uses output or return value of dumpbin
+    # to determine ${result_var}. In 99%+? practical cases, the exe name
+    # match will be sufficient...
+    #
+  endif()
+
+  # Use the information returned from the Unix shell command "file" to
+  # determine if ${file_full} should be considered an executable file...
+  #
+  # If the file command's output contains "executable" and does *not* contain
+  # "text" then it is likely an executable suitable for prerequisite analysis
+  # via the get_prerequisites macro.
+  #
+  if(UNIX)
+    if(NOT file_cmd)
+      find_program(file_cmd "file")
+      mark_as_advanced(file_cmd)
+    endif()
+
+    if(file_cmd)
+      execute_process(COMMAND "${file_cmd}" "${file_full}"
+        RESULT_VARIABLE file_rv
+        OUTPUT_VARIABLE file_ov
+        ERROR_VARIABLE file_ev
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+        )
+      if(NOT file_rv STREQUAL "0")
+        message(FATAL_ERROR "${file_cmd} failed: ${file_rv}\n${file_ev}")
+      endif()
+
+      # Replace the name of the file in the output with a placeholder token
+      # (the string " _file_full_ ") so that just in case the path name of
+      # the file contains the word "text" or "executable" we are not fooled
+      # into thinking "the wrong thing" because the file name matches the
+      # other 'file' command output we are looking for...
+      #
+      string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
+      string(TOLOWER "${file_ov}" file_ov_lower)
+
+      #message(FATAL_ERROR "file_ov='${file_ov}'")
+      if("${file_ov_lower}" MATCHES "symbolic link")
+        #message(STATUS "symbolic link!")
+        set(${result_var1} 1 PARENT_SCOPE)
+        #Now find where the symlink is linked to.
+        #Do a regex replace
+        string(REGEX REPLACE "_file_full_*.*symbolic.link.to.."
+          "" symlinked_to ${file_ov})
+        #Take out last character which is a single quote
+        string(REPLACE "'" "" symlinked_to "${symlinked_to}")
+        #strip for our own sanity
+        string(STRIP ${symlinked_to} symlinked_to)
+        set(${result_var2} "${symlinked_to}" PARENT_SCOPE)
+        #message(FATAL_ERROR "${file_full} is symlinked_to ${symlinked_to}")
+        return()
+      endif()
+
+    else()
+      message(STATUS "warning: No 'file' command, skipping execute_process...")
+    endif()
+  endif()
+endfunction()
-- 
GitLab