From 06f74fc6c172a7451c7da26100b3827f848d8b7b Mon Sep 17 00:00:00 2001
From: Frank Gaede <frank.gaede@desy.de>
Date: Mon, 9 Sep 2013 15:47:01 +0000
Subject: [PATCH]  - improved creation and usage of environment scripts:     -
 ./bin/thisdd4hep.sh now initializes also ROOT       and optionally geant4    
 - cmake/thisdd4hep_package.sh.in is used in       examples to create
 thisILDExDet.sh and thisILDExSimu.sh

  - the thisdd4hep.sh script is now also used in the build process
    for generating the rootmap files
---
 CMakeLists.txt                    |  5 +++++
 ReadMe.txt                        | 36 +++++++++---------------------
 cmake/DD4hep.cmake                | 37 ++++++++++++++++++++++++-------
 cmake/FindROOT.cmake              |  3 +++
 cmake/thisdd4hep.sh               | 21 ++++++++++++++----
 cmake/thisdd4hep_package.sh.in    | 19 ++++++++++++++++
 examples/ILDExDet/CMakeLists.txt  |  9 ++++++++
 examples/ILDExSimu/CMakeLists.txt |  8 +++++++
 8 files changed, 100 insertions(+), 38 deletions(-)
 create mode 100644 cmake/thisdd4hep_package.sh.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d4b40e9d1..387d48db4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -62,6 +62,11 @@ add_subdirectory(DDCore)
 if(DD4HEP_WITH_GEANT4)
   add_subdirectory(DDG4)
   add_subdirectory(DDSense)
+
+  #--- create a geant4 variables for the thisdd4hep.sh script
+  find_package( Geant4 ) 
+  get_filename_component(Geant4_ROOT "${Geant4_INCLUDE_DIR}/../../" ABSOLUTE)
+
 endif()
 
 add_subdirectory(UtilityApps)
diff --git a/ReadMe.txt b/ReadMe.txt
index 5737a64fe..0857be8ae 100644
--- a/ReadMe.txt
+++ b/ReadMe.txt
@@ -68,6 +68,9 @@ To build and run the simulation examples Geant4 will be required.
 
   source  /data/ilcsoft/root/5.34.03/bin/thisroot.sh
 
+  ( minimum is: export ROOTSYS=/data/ilcsoft/root/5.34.03 )
+
+
 - configure and build:
 
   cd DD4hep
@@ -101,30 +104,15 @@ To build and run the simulation examples Geant4 will be required.
    -DDD4HEP_USE_PYROOT=ON
 
 
-
-
-
-
-- Setup the running environment
-
-. thisdd4hep.sh
+- Setup the environment for running 
+  
+ . ./bin/thisdd4hep.sh
      or
-source thisdd4hep.csh
+source ./bin/thisdd4hep.csh
 
-### now call make ###########
-make -j4
 
 
 
-- Run ILDEx display
-
-DDExamples/ILDExDisplay/ILDExDisplay file:../DD4hep/DDExamples/ILDExDet/compact/ILDEx.xml
-
-- Run CLICSiD display
-
-DDExamples/CLICSiD/CLICSiDtest file:../DD4hep/DDExamples/CLICSiD/compact/compact.xml
-
-
 3) Extending
 ------------
 
@@ -143,11 +131,8 @@ DDExamples/CLICSiD/CLICSiDtest file:../DD4hep/DDExamples/CLICSiD/compact/compact
 Before any of the (new) examples in the ./examples directory can be build,
 one needs to set the environment:
 
- source __path_where_ROOT_is_installed___/bin/thisroot.sh
  source __path_where_DD4hep_is_installed_/bin/thisdd4hep.sh
  
- [note: possible other scripts need to be called, e.g. when using geant4  - see above ]
-
 
 - then building the examples should be very straight forward. e.g:
    (check for additional instructions in example subdirectory)
@@ -157,8 +142,9 @@ one needs to set the environment:
   cmake ..
   make -j install
 
-- before running the example, again the (DY)LD_LIBRARY_PATH has to be updated:
-  export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$PWD/../lib
+- before running the example, initialize the environment:
+  . ./bin/thisILDExDet.sh 
+
 
 - then you can for example display the ILD detector (toy model):
 
@@ -172,8 +158,6 @@ one needs to set the environment:
 
 6) run SLIC simulation on lccd file created with DD4Hep:
 
-# create lcdd file from compact:
-./bin/geoConverter -compact2lcdd -input file:../DD4hep/DDExamples/ILDExDet/compact/ILDEx.xml -output ILD_toy.lcdd
 
 # make sure slic is in the path and then:
 slic -g ./ILD_toy.lcdd -o ILD_toy.slcio -x -m ../DD4hep/DDExamples/ILDExSimu/run1.mac
diff --git a/cmake/DD4hep.cmake b/cmake/DD4hep.cmake
index 46328b4d1..540a305dd 100644
--- a/cmake/DD4hep.cmake
+++ b/cmake/DD4hep.cmake
@@ -5,19 +5,40 @@
 #---------------------------------------------------------------------------------------------------
 function(dd4hep_generate_rootmap library)
   find_package(ROOT QUIET)
+  find_package(DD4hep QUIET)
+
   set(rootmapfile ${CMAKE_SHARED_MODULE_PREFIX}${library}.rootmap)
 
   set(libname ${CMAKE_SHARED_MODULE_PREFIX}${library}${CMAKE_SHARED_LIBRARY_SUFFIX})
+
+
+#---------------------------------------------------------------------------------------
+if( DD4hep_FOUND )  
+  # we are building an external tool and need to source ${DD4hep_ROOT}/bin/thisdd4hep.sh
+  
+  add_custom_command(OUTPUT ${rootmapfile}
+    COMMAND cd ${LIBRARY_OUTPUT_PATH} && 
+    . ${DD4hep_ROOT}/bin/thisdd4hep.sh &&
+    genmap ${ROOT_genmap_CMD} -i ${libname} -o ${rootmapfile}
+    DEPENDS ${library})
+  
+else()  # we are building DD4hep itself - only need thisroot.sh
+  
   add_custom_command(OUTPUT ${rootmapfile}
-                     COMMAND ${CMAKE_COMMAND} -Dlibname=${libname} -Drootmapfile=${rootmapfile}
-                             -Dgenmap_install_dir=${LIBRARY_OUTPUT_PATH}
-                             -P ${CMAKE_SOURCE_DIR}/cmake/MakeRootMap.cmake
-                     DEPENDS ${library})
-  add_custom_target(${library}Rootmap ALL DEPENDS ${rootmapfile})
-
-  install(FILES ${LIBRARY_OUTPUT_PATH}/${rootmapfile}
-    DESTINATION lib
+    COMMAND cd ${LIBRARY_OUTPUT_PATH} && 
+    . ${ROOT_ROOT}/bin/thisroot.sh &&
+    genmap ${ROOT_genmap_CMD} -i ${libname} -o ${rootmapfile}
+    DEPENDS ${library})
+endif()
+
+
+add_custom_target(${library}Rootmap ALL DEPENDS ${rootmapfile})
+
+install(FILES ${LIBRARY_OUTPUT_PATH}/${rootmapfile}
+  DESTINATION lib
   )
+#--------------------------------------------------------------------------------------
+
 
   # Notify the project level target
   #gaudi_merge_files_append(Rootmap ${library}Rootmap ${CMAKE_CURRENT_BINARY_DIR}/${library}.rootmap)
diff --git a/cmake/FindROOT.cmake b/cmake/FindROOT.cmake
index 4b361539d..818f9cf21 100644
--- a/cmake/FindROOT.cmake
+++ b/cmake/FindROOT.cmake
@@ -2,6 +2,7 @@
 # This module sets up ROOT information 
 # It defines:
 # ROOT_FOUND          If the ROOT is found
+# ROOT_ROOT           the root directory ($ROOTSYS)
 # ROOT_INCLUDE_DIR    PATH to the include directory
 # ROOT_LIBRARIES      Most common libraries
 # ROOT_LIBRARY_DIR    PATH to the library directory 
@@ -33,6 +34,8 @@ if(ROOT_CONFIG_EXECUTABLE)
 
   set(ROOT_LIBRARY_DIR ${ROOTSYS}/lib)
 
+  set(ROOT_ROOT ${ROOTSYS})
+
   # Make variables changeble to the advanced user
   mark_as_advanced(ROOT_CONFIG_EXECUTABLE)
 
diff --git a/cmake/thisdd4hep.sh b/cmake/thisdd4hep.sh
index 56628e5b9..f9cda1946 100644
--- a/cmake/thisdd4hep.sh
+++ b/cmake/thisdd4hep.sh
@@ -16,6 +16,19 @@ else
     THIS=$(dirname ${BASH_ARGV[0]})
 fi
 
+
+#----------- source the ROOT environment first
+source @ROOT_ROOT@/bin/thisroot.sh
+
+if [ @DD4hep_WITH_GEANT4@ ]
+then
+    export G4INSTALL=@Geant4_ROOT@/../../
+    export G4ENV_INIT=@Geant4_ROOT@/../../bin/geant4.sh
+#    export G4SYSTEM="Darwin-g++"
+    test -r ${G4ENV_INIT} && { cd $(dirname ${G4ENV_INIT}) ; . ./$(basename ${G4ENV_INIT}) ; cd $OLDPWD ; }
+fi
+
+
 #----PATH-----------------------------------------------------------------
 if [ -z "${PATH}" ]; then
   PATH=@CMAKE_INSTALL_PREFIX@/bin; export PATH       # Linux, ELF HP-UX
@@ -25,16 +38,16 @@ fi
 
 #----LD_LIBRARY_PATH-----------------------------------------------------------------
 if [ -z "${LD_LIBRARY_PATH}" ]; then
-  LD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@ROOT_LIBRARY_DIR@:@Geant4_DIR@/..; export LD_LIBRARY_PATH       # Linux, ELF HP-UX
+  LD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@Geant4_DIR@/..; export LD_LIBRARY_PATH       # Linux, ELF HP-UX
 else
-  LD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@ROOT_LIBRARY_DIR@:@Geant4_DIR@/..:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
+  LD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@Geant4_DIR@/..:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
 fi
 
 #----DYLD_LIBRARY_PATH---------------------------------------------------------------
 if [ -z "${DYLD_LIBRARY_PATH}" ]; then
-  DYLD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@ROOT_LIBRARY_DIR@; export DYLD_LIBRARY_PATH   # Mac OS X
+  DYLD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib; export DYLD_LIBRARY_PATH   # Mac OS X
 else
-  DYLD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:@ROOT_LIBRARY_DIR@:$DYLD_LIBRARY_PATH; export DYLD_LIBRARY_PATH
+  DYLD_LIBRARY_PATH=@CMAKE_INSTALL_PREFIX@/lib:@XERCESC_ROOT_DIR@/lib:$DYLD_LIBRARY_PATH; export DYLD_LIBRARY_PATH
 fi
 
 #----PYTHONPATH---------------------------------------------------------------
diff --git a/cmake/thisdd4hep_package.sh.in b/cmake/thisdd4hep_package.sh.in
new file mode 100644
index 000000000..69ffbf276
--- /dev/null
+++ b/cmake/thisdd4hep_package.sh.in
@@ -0,0 +1,19 @@
+#################################################################################
+#
+#  environment script for DD4hep examples - initializes DD4hep (and ROOT) as well
+# 
+#  @author F.Gaede, DESY, 2013
+#
+#################################################################################
+
+
+source @DD4hep_ROOT@/bin/thisDD4hep.sh
+
+export PATH=$PATH:@CMAKE_INSTALL_PREFIX@/bin
+
+if [ @APPLE@ ]
+then
+    export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:@CMAKE_INSTALL_PREFIX@/lib
+else
+    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:@CMAKE_INSTALL_PREFIX@/lib
+fi
\ No newline at end of file
diff --git a/examples/ILDExDet/CMakeLists.txt b/examples/ILDExDet/CMakeLists.txt
index ea17e196d..4ac7caa3b 100644
--- a/examples/ILDExDet/CMakeLists.txt
+++ b/examples/ILDExDet/CMakeLists.txt
@@ -69,6 +69,15 @@ target_link_libraries(${PackageName} ${DD4hep_LIBRARIES}  ${ROOT_LIBRARIES} ${RO
 
 dd4hep_generate_rootmap(${PackageName} )
 
+
+#---- configure run environment ---------------
+configure_file( ${DD4hep_ROOT}/cmake/thisdd4hep_package.sh.in  this${PackageName}.sh @ONLY)
+
+install(FILES ${CMAKE_BINARY_DIR}/this${PackageName}.sh
+  DESTINATION bin
+  )
+
+
 #--- install target-------------------------------------
 
 install(TARGETS ${PackageName}
diff --git a/examples/ILDExSimu/CMakeLists.txt b/examples/ILDExSimu/CMakeLists.txt
index 7e8f1cc99..f67bb30d4 100644
--- a/examples/ILDExSimu/CMakeLists.txt
+++ b/examples/ILDExSimu/CMakeLists.txt
@@ -93,6 +93,14 @@ target_link_libraries(${PackageName} ${DD4hep_LIBRARIES}
 # fg: not needed here
 #dd4hep_generate_rootmap(${PackageName} )
 
+#---- configure run environment ---------------
+configure_file( ${DD4hep_ROOT}/cmake/thisdd4hep_package.sh.in  this${PackageName}.sh @ONLY)
+
+install(FILES ${CMAKE_BINARY_DIR}/this${PackageName}.sh
+  DESTINATION bin
+  )
+
+
 #--- install target-------------------------------------
 
 install(TARGETS ${PackageName}
-- 
GitLab