From dbe64f88a03995549d3ce147fd969e1d8c0432ab Mon Sep 17 00:00:00 2001
From: Valerio Mariani <valerio.mariani@unibas.ch>
Date: Wed, 27 Jul 2011 19:01:37 +0200
Subject: [PATCH] Options and changes to achieve Debian compaitbility

1. Option to move libexec to lib/openstructure/libexec
2. Gosty moved to libexec
---
 .gitignore                     |  1 +
 CMakeLists.txt                 | 37 ++++++++++++++++----------
 cmake_support/OST.cmake        | 47 ++++++++++++++++++++++++++++++++--
 modules/config/CMakeLists.txt  |  5 ++++
 modules/config/config.hh.in    |  2 ++
 modules/gui/src/CMakeLists.txt |  2 +-
 modules/gui/src/gosty.cc       | 16 +++++++++++-
 scripts/CMakeLists.txt         |  4 +--
 scripts/dng.in                 |  4 +--
 scripts/ost.in                 |  2 +-
 scripts/ost_config.in          |  8 +++---
 11 files changed, 101 insertions(+), 27 deletions(-)

diff --git a/.gitignore b/.gitignore
index c4c1b9add..56410cfa5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@ CTestTestfile.cmake
 .*
 !.gitignore
 CMakeCache.txt
+version.hh
 config.hh
 *.pyc
 index.txt
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 68594813e..da42bdb5f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,8 +34,10 @@ option(ENABLE_SPNAV "whether 3DConnexion devices should be supported"
       OFF)
 option(STATIC_PROPERTY_WORKAROUND "workaround for static property bug with some boost/boost_python combinations" OFF)
 option(DEPLOYMENT "switch on deployment settings" OFF)
-option(COMPILE_TESTS "wheter unit tests should be compiled by default" OFF)
+option(COMPILE_TESTS "whether unit tests should be compiled by default" OFF)
 option(ENABLE_STATIC "whether static libraries should be compiled" OFF)
+option(DEBIAN_STYLE_LIBEXEC "whether 'libexec' should be put under 'lib/openstructure" OFF)
+
 if (CXX)
   set(CMAKE_CXX_COMPILER ${CXX})
 endif()
@@ -114,6 +116,12 @@ else()
   set(_PROFILE OFF)
 endif()
 
+if (DEBIAN_STYLE_LIBEXEC)
+  set(_DEBIAN_STYLE_LIBEXEC ON)
+else()
+  set(_DEBIAN_STYLE_LIBEXEC OFF)
+endif()
+
 add_definitions(-DEIGEN2_SUPPORT)
 
 if (COMPOUND_LIB)
@@ -222,16 +230,17 @@ set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES
 
 message(STATUS 
         "OpenStructure will be built with the following options:\n"
-        "   Install Prefix                 (-DPREFIX) : ${CMAKE_INSTALL_PREFIX}\n"
-        "   RPath in install            (-DUSE_RPATH) : ${_USE_RPATH}\n"
-        "   Graphical interface        (-DENABLE_GUI) : ${_UI}\n"
-        "   OpenGL support             (-DENABLE_GFX) : ${_OPENGL}\n"
-        "   Image Processing support   (-DENABLE_IMG) : ${_IMG}\n"
-        "   Shader support             (-DUSE_SHADER) : ${_SHADER}\n"
-        "   Numpy support               (-DUSE_NUMPY) : ${_NUMPY}\n"
-        "   Optimize                     (-DOPTIMIZE) : ${_OPT}\n"
-        "   Profiling support             (-DPROFILE) : ${_PROFILE}\n"
-        "   Double Precision (-DUSE_DOUBLE_PRECISION) : ${_DOUBLE_PREC}\n"
-        "   Compound Lib             (-DCOMPOUND_LIB) : ${_COMP_LIB}\n"
-        "   TMAlign and TMScore   (-DCOMPILE_TMTOOLS) : ${_TM_TOOLS}\n"
-        "   Static Libraries        (-DENABLE_STATIC) : ${ENABLE_STATIC}")
+        "   Install Prefix                       (-DPREFIX) : ${CMAKE_INSTALL_PREFIX}\n"
+        "   RPath in install                  (-DUSE_RPATH) : ${_USE_RPATH}\n"
+        "   Graphical interface              (-DENABLE_GUI) : ${_UI}\n"
+        "   OpenGL support                   (-DENABLE_GFX) : ${_OPENGL}\n"
+        "   Image Processing support         (-DENABLE_IMG) : ${_IMG}\n"
+        "   Shader support                   (-DUSE_SHADER) : ${_SHADER}\n"
+        "   Numpy support                     (-DUSE_NUMPY) : ${_NUMPY}\n"
+        "   Optimize                           (-DOPTIMIZE) : ${_OPT}\n"
+        "   Profiling support                   (-DPROFILE) : ${_PROFILE}\n"
+        "   Double Precision       (-DUSE_DOUBLE_PRECISION) : ${_DOUBLE_PREC}\n"
+        "   Compound Lib                   (-DCOMPOUND_LIB) : ${_COMP_LIB}\n"
+        "   TMAlign and TMScore         (-DCOMPILE_TMTOOLS) : ${_TM_TOOLS}\n"
+        "   Static Libraries              (-DENABLE_STATIC) : ${ENABLE_STATIC}\n"
+        "   Debian-style 'libexec' (-DDEBIAN_STYLE_LIBEXEC) : ${_DEBIAN_STYLE_LIBEXEC}")
\ No newline at end of file
diff --git a/cmake_support/OST.cmake b/cmake_support/OST.cmake
index 1eac2a349..539c68b9e 100644
--- a/cmake_support/OST.cmake
+++ b/cmake_support/OST.cmake
@@ -313,6 +313,40 @@ macro(executable)
 endmacro()
 
 
+#-------------------------------------------------------------------------------
+# Synopsis
+#   executable_libexec(NAME exe_name SOURCES source1 source2 LINK link1 link2)
+#
+# Description:
+#  Compile, link and stage a C++ executable into the libexec directory
+#-------------------------------------------------------------------------------
+macro(executable_libexec)
+  parse_argument_list(_ARG 
+                      "NAME;SOURCES;LINK;DEPENDS_ON" "NO_RPATH;STATIC" ${ARGN})
+  if (NOT _ARG_NAME)
+    message(FATAL_ERROR "invalid use of executable(): a name must be provided")
+  endif()   
+  add_executable(${_ARG_NAME} ${_ARG_SOURCES})
+  set_target_properties(${_ARG_NAME}
+                        PROPERTIES RUNTIME_OUTPUT_DIRECTORY
+                       "${LIBEXEC_STAGE_PATH}")  
+  if (APPLE AND NOT _ARG_NO_RPATH AND NOT _ARG_STATIC)
+    set_target_properties(${_ARG_NAME} PROPERTIES
+                          LINK_FLAGS "-Wl,-rpath,@loader_path/../lib")
+  endif()
+  if (_ARG_LINK)
+    target_link_libraries(${_ARG_NAME} ${_ARG_LINK})
+  endif()
+  if (ENABLE_STATIC AND _ARG_STATIC)
+    set(TARGET_SUFFIX _static)
+    target_link_libraries(${_ARG_NAME} ${STATIC_LIBRARIES})
+  endif()
+  foreach(_DEP ${_ARG_DEPENDS_ON})
+    target_link_libraries(${_ARG_NAME} ${_DEP}${TARGET_SUFFIX})
+  endforeach()
+  install(TARGETS ${_ARG_NAME} DESTINATION ${LIBEXEC_PATH})
+endmacro()
+
 #-------------------------------------------------------------------------------
 # Synopsis:
 #   substitute(IN_FILE in_file OUT_FILE out_file DICT a=b c=d)
@@ -586,7 +620,7 @@ macro(ost_unittest)
       endif()
       if (WIN32)
         target_link_libraries(${_test_name} ${BOOST_UNIT_TEST_LIBRARIES} "${_ARG_PREFIX}_${_ARG_MODULE}")  
-        add_custom_target("${_test_name}_run"
+        add_custom_target("${_test_name}_run":
                         COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${_test_name}.exe || echo
                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${CMAKE_BUILD_TYPE}/..
                         COMMENT "running checks for module ${_ARG_MODULE}"
@@ -725,7 +759,6 @@ macro(setup_stage)
   set(STAGE_DIR "${CMAKE_BINARY_DIR}/stage")
   set(EXECUTABLE_OUTPUT_PATH ${STAGE_DIR}/bin  )
   set(HEADER_STAGE_PATH ${STAGE_DIR}/include )
-  set(LIBEXEC_STAGE_PATH ${STAGE_DIR}/libexec/openstructure  )
   set(SHARED_DATA_PATH ${STAGE_DIR}/share/openstructure  )
 
   if (UNIX AND NOT APPLE)
@@ -739,6 +772,16 @@ macro(setup_stage)
     set(LIB_DIR lib  )
     set(LIB_STAGE_PATH "${STAGE_DIR}/lib"  )
   endif()
+  if (_DEBIAN_STYLE_LIBEXEC)
+    set(LIBEXEC_PATH ${LIB_DIR}/openstructure/libexec  )
+  else()
+    set(LIBEXEC_PATH libexec/openstructure  )
+  endif()
+  if (_DEBIAN_STYLE_LIBEXEC)
+    set(LIBEXEC_STAGE_PATH ${LIB_STAGE_PATH}/openstructure/libexec  )
+  else()
+    set(LIBEXEC_STAGE_PATH ${STAGE_DIR}/libexec/openstructure  )
+  endif()
 
   include_directories("${HEADER_STAGE_PATH}")
   link_directories(${LIB_STAGE_PATH})
diff --git a/modules/config/CMakeLists.txt b/modules/config/CMakeLists.txt
index 753a98190..617aa51c5 100644
--- a/modules/config/CMakeLists.txt
+++ b/modules/config/CMakeLists.txt
@@ -57,6 +57,11 @@ if (FFTW_USE_THREADS)
 else()
   set(fftw_use_threads 0)
 endif()
+if (_DEBIAN_STYLE_LIBEXEC)
+  set(debian_style_libexec 1)
+else()
+  set(debian_style_libexec 0)
+endif()
 
 set(config_hh_generator "CMake")
 set(CONFIG_HH_FILE "${CMAKE_CURRENT_SOURCE_DIR}/config.hh")
diff --git a/modules/config/config.hh.in b/modules/config/config.hh.in
index 88653c1e8..a9e33adca 100644
--- a/modules/config/config.hh.in
+++ b/modules/config/config.hh.in
@@ -32,4 +32,6 @@
 #define OST_SPNAV_ENABLED @spnav_enabled@
 #define OST_FFT_USE_THREADS @fftw_use_threads@
 #define OST_NUMPY_SUPPORT_ENABLED @numpy_support@
+#define OST_DEBIAN_STYLE_LIBEXEC @debian_style_libexec@
+
 #endif
diff --git a/modules/gui/src/CMakeLists.txt b/modules/gui/src/CMakeLists.txt
index 7c8f6d640..993e776f1 100644
--- a/modules/gui/src/CMakeLists.txt
+++ b/modules/gui/src/CMakeLists.txt
@@ -471,5 +471,5 @@ if (NOT WIN32)
   set(LINK LINK ${BOOST_PROGRAM_OPTIONS_LIBRARIES})
 endif()
 
-executable(NAME gosty SOURCES gosty.cc ${OST_GOSTY_MOC} ${OST_QT_RESOURCE} 
+executable_libexec(NAME gosty SOURCES gosty.cc ${OST_GOSTY_MOC} ${OST_QT_RESOURCE} 
            DEPENDS_ON ost_gui ${LINK})
diff --git a/modules/gui/src/gosty.cc b/modules/gui/src/gosty.cc
index cb1ed7384..b07d5f44e 100644
--- a/modules/gui/src/gosty.cc
+++ b/modules/gui/src/gosty.cc
@@ -82,13 +82,27 @@ String get_ost_root()
 {
   QDir dir(QApplication::applicationDirPath());
 
+#if OST_DEBIAN_STYLE_LIBEXEC  
   #ifdef _MSC_VER
     dir.cdUp();
     dir.cdUp();
+    dir.cdUp();
+    dir.cdUp();
   #else
     dir.cdUp();
+    dir.cdUp();
+    dir.cdUp();
   #endif
-
+#else
+  #ifdef _MSC_VER
+    dir.cdUp();
+    dir.cdUp();
+    dir.cdUp();
+  #else
+    dir.cdUp();
+    dir.cdUp();
+  #endif
+#endif
   return dir.path().toStdString();
 }
 
diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt
index 6a936d282..f71f15327 100644
--- a/scripts/CMakeLists.txt
+++ b/scripts/CMakeLists.txt
@@ -1,4 +1,4 @@
-set(SUBST_DICT BUILD_TYPE=${CMAKE_BUILD_TYPE} PYTHON_BINARY=${PYTHON_BINARY} LIBDIR=${LIB_DIR})
+set(SUBST_DICT BUILD_TYPE=${CMAKE_BUILD_TYPE} PYTHON_BINARY=${PYTHON_BINARY} LIBDIR=${LIB_DIR} LIBEXEC_PATH=${LIBEXEC_PATH})
 message("${BUILD_TYPE}")
 add_custom_target(ost_scripts ALL)
 
@@ -19,7 +19,7 @@ if(WIN32)
   script(NAME gipltng.bat INPUT gipltng.bat.in SUBSTITUTE ${SUBST_DICT})
 else()
   script(NAME ost_config INPUT ost_config.in SUBSTITUTE ${SUBST_DICT}
-         TARGET ost_scripts OUTPUT_DIR libexec/openstructure)
+         TARGET ost_scripts OUTPUT_DIR ${LIBEXEC_PATH})
   script(NAME ost INPUT ost.in SUBSTITUTE ${SUBST_DICT} 
          TARGET ost_scripts)
   if (ENABLE_GUI)
diff --git a/scripts/dng.in b/scripts/dng.in
index e78b6511f..b6f02fbf2 100755
--- a/scripts/dng.in
+++ b/scripts/dng.in
@@ -26,6 +26,6 @@ else
   SCRIPT_NAME="$0"
 fi
 BIN_DIR=`dirname "$SCRIPT_NAME"`
-source $BIN_DIR/../libexec/openstructure/ost_config
+source $BIN_DIR/../@LIBEXEC_PATH@/ost_config
 
-$DNG_BINDIR/gosty $DNG_INITDIR/init.py ost $opts
+$BIN_DIR/../@LIBEXEC_PATH@/gosty $DNG_INITDIR/init.py ost $opts
diff --git a/scripts/ost.in b/scripts/ost.in
index 3cf716f00..2891d37c2 100755
--- a/scripts/ost.in
+++ b/scripts/ost.in
@@ -29,7 +29,7 @@ else
 fi
 BIN_DIR=`dirname "$SCRIPT_NAME"`
 
-source $BIN_DIR/../libexec/openstructure/ost_config
+source $BIN_DIR/../@LIBEXEC_PATH@/ost_config
 
 $pyexec $interactive "$DNG_ROOT/@LIBDIR@/openstructure/init_cl.py" $opts
 RC=$?
diff --git a/scripts/ost_config.in b/scripts/ost_config.in
index bfd79f436..b7b8b7c3e 100644
--- a/scripts/ost_config.in
+++ b/scripts/ost_config.in
@@ -26,10 +26,10 @@ export DNG_BINDIR="$DNG_ROOT/bin"
 export DNG_LIBDIR="$DNG_ROOT/@LIBDIR@"
 export DNG_INITDIR="$DNG_LIBDIR/openstructure"
 
-#export PATH="$DNG_BINDIR:$PATH"
-#export DYLD_FRAMEWORK_PATH="$BIN_LIBDIR:${DYLD_FRAMEWORK_PATH}"
-#export DYLD_LIBRARY_PATH="$BIN_LIBDIR:${DYLD_LIBRARY_PATH}"
-#export LD_LIBRARY_PATH="$DNG_LIBDIR:$LD_LIBRARY_PATH"
+export PATH="$DNG_BINDIR:$PATH"
+export DYLD_FRAMEWORK_PATH="$BIN_LIBDIR:${DYLD_FRAMEWORK_PATH}"
+export DYLD_LIBRARY_PATH="$BIN_LIBDIR:${DYLD_LIBRARY_PATH}"
+export LD_LIBRARY_PATH="$DNG_LIBDIR:$LD_LIBRARY_PATH"
 # set QT_PLUGIN_PATH PYTHONHOME and PYTHONPATH for bundle (commented except in linux bundles)
 # set QT_PLUGIN_PATH and pythonpath for bundle (commented except in linux bundles)
 #export QT_PLUGIN_PATH="$BIN_DIR/plugins"
-- 
GitLab