Friday, November 10, 2006

Installing Geant4, and a fink/Apple dylib name conflict

Geant4 is a particle physics simulation toolkit. The easiest way I found to install it under OS X was to first fetch the latest source code then follow steps similar to those described for installing the SLAC Geant4 tutorial material. The key to this is creating an appropriate "setup-geant4.sh" script that will set the values of several environment variables. My first attempt at this is given below:

#!/bin/sh

export G4HERE=`pwd`
export G4INSTALL=$G4HERE/geant4.8.1.p01

# Check we are where we think we are
if ! test -e $G4INSTALL/source/GNUmakefile
then
echo "G4INSTALL not set correctly. Please edit setup-geant4 script to fix problem"
return
fi

export G4SYSTEM=Darwin-g++
export CLHEP_BASE_DIR=/sw
if ! test -e $CLHEP_BASE_DIR/include/CLHEP/config/CLHEP.h
then
echo "CLHEP_BASE_DIR not set correctly. Please edit setup-geant4 script to fix problem"
return
fi

if [ -z "$PATH" ] ; then
export PATH=""
fi
export PATH=${PATH}:${G4INSTALL}/bin/${G4SYSTEM}

# If we run WIN32-VC* , it also means that it is cygwin
case "$G4SYSTEM" in
WIN32-VC*)
export CLHEP_BASE_DIR=`/bin/cygpath -m "$CLHEP_BASE_DIR"`
export G4INSTALL=`/bin/cygpath -m "$G4INSTALL"`
if [ -z "$LIB" ] ; then
export LIB=""
fi
export LIB="$CLHEP_BASE_DIR;$LIB"
if [ -z "$lib" ] ; then
export lib=$LIB
else
export lib="${CLHEP_BASE_DIR};$lib"
fi
export G4LIB_BUILD_ZLIB=1
;;
Darwin-g++*)
# MacOS X
if [ -z "$DYLD_LIBRARY_PATH" ] ; then
export DYLD_LIBRARY_PATH=""
fi
export DYLD_LIBRARY_PATH=${CLHEP_BASE_DIR}/lib:${DYLD_LIBRARY_PATH}
;;
*)
# default
if [ -z "$LD_LIBRARY_PATH" ] ; then
export LD_LIBRARY_PATH=""
fi
export LD_LIBRARY_PATH=${CLHEP_BASE_DIR}/lib:${LD_LIBRARY_PATH}
;;
esac

export G4LIB_USE_ZLIB=1

echo "G4INSTALL=$G4INSTALL"
echo "G4SYSTEM=$G4SYSTEM"
echo "CLHEP_BASE_DIR=$CLHEP_BASE_DIR"

I put this script into /usr/src, then fetched geant4.8.1.p01.gtar.gz and unpacked it into /usr/src/geant4.8.1.p01 . I then typed "cd /usr/src; . setup-geant4.sh" to set the appropriate environment variables. Then I followed the steps outlined in the SLAC documentation and typed "cd geant4.8.1.p01/source; make". After make completed, I tried one of the example programs, following the steps outlined for example A01 in the SLAC tutorial. The program compiled, but I found that it wouldn't produce graphics on an X display. To solve this, I needed to set two more environment variables:

export G4VIS_BUILD_OPENGLX_DRIVER=1
export G4VIS_USE_OPENGLX=1

I then went back to the geant4 source directory, did a "make clean" and another "make". Once this had completed, I re-made example A01 and found a different problem. Now I was getting a run-time error that said:

dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /sw/lib/libJPEG.dylib

Trace/BPT trap

It turns out that Apple ships a "libJPEG.dylib" with OS X which is completely different from the "libjpeg.dylib" of the same name (modulo case) that fink provides. As you can see above, the setup-geant4.sh script I copied from SLAC adds the directory /sw/lib (where the fink-installed libjpeg.dylib lives) to DYLD_LIBRARY_PATH. The Geant4 program was looking for the Apple-supplied library, but because DYLD_LIBRARY_PATH told it to look in /sw/lib first, it found the fink-supplied file of the "same" name instead. Removing /sw/lib from DYLD_LIBRARY_PATH fixed the problem, and the example ran as expected thereafter.

Note that there is a similar potential name conflict between libGIF.dylib / libgif.dylib. These problems have been noticed by others.

Finally, to set up the environment for compiling Geant4 programs in the future, I copied /usr/src/geant4.8.1.p01 into the master copy of /common/lib/geant4.8.1.p01 and made a symlink from there to /common/lib/geant4. I then arranged to have the following environment variables defined:

G4INSTALL=/common/lib/geant4
G4HERE=/common/lib
G4VIS_BUILD_OPENGLX_DRIVER=1
G4VIS_USE_OPENGLX=1
G4SYSTEM=Darwin-g++
G4WORKDIR=$HOME/geant4
G4EXE=$G4WORKDIR/bin/Darwin-g++
G4LIB_USE_ZLIB=1
CLHEP_BASE_DIR=/sw

No comments: