Building G4beamline -- old, around version 1.16 or so, perhaps earlier
Tom Roberts
original: 1-March-2007; Updated: Geant4.9.2.p01 26-MARCH-2009

**************************************************************************
***                                                                    ***
***  This is completely outdated, and is of historical interest only.  ***
***  Both Geant4 and G4beamline build systems have completely changed. ***
***                                                                    ***
**************************************************************************

G4beamline is set up to be built on three different OSs: Linux (g++), 
Mac OS X (g++), and Windows XP (with Cygwin and VC++).

This note describes How I chose to install and build the necessary libraries,
and then build G4beamline.

Where to get components:
	Coin		http://www.coin3d.org
	SoXt		http://www.coin3d.org
	SoWin		http://www.coin3d.org
	CLHEP		http://proj-clhep.web.cern.ch/proj-clhep/
	Geant4		http://geant4.cern.ch
	GSL		http://www.gnu.org/software/gsl/
	Root		http://root.cern.ch



==============
Linux  (Intel)
==============

Note that I build g4beamline on an old RedHat 7.3 image, so the executable will
run on just about any Linux newer than that. You can almost surely build on
your own Linux system if all you want is to run it there. You must have a g++
version 3.2.1 or later -- this is not native to RH 7.3, and I use the Fermilab
ups/upd system to setup gcc 3.2.1.

NOTE: my Linux virtual machine has 2 CPUs, so I put this into my environment:
	export MAKEFLAGS="-j 2"
with 1 CPU no value is needed. This lets make run 2 compilations in parallel
and significantly speeds things up.

### I build using RedHat 7.3, so the binaries will execute n any reasonably
### modern Linux. You, on the other hand, can build using the normal g++
### on your system. So omit the following 3 lines:
	# Run under RedHat 7.3, but using gcc 3.2.1
	setup_ups
	setup gcc v3_2_1
	# in $HOME
	mkdir Coin
	Download Coin 2.4.6 source and unpack it into Coin/Coin-2.4.6
	Download SoXt 1.2.2 source and unpack it into Coin/SoXt-1.2.2
	cd $HOME/Coin/Coin-2.4.6
	# BUG in Coin on such an old OS:
	# edit libtool, put "tagname=CXX" after line 1
	./configure --prefix=$HOME/Coin --disable-shared --enable-static \
		--disable-sound
	# you may need --with-motif=/usr/X11R6
	make install
	cd ../SoXt-1.2.2
	# work around PATH bug in SoXt's configure
	cp ../bin/coin-config .
	./configure --prefix=$HOME/Coin --with-coin=$HOME/Coin \
		--disable-shared --enable-static
	make install
	# Now $HOME/Coin contains the installed Coin and SoXt.

	# install the Gnu Scientific Library as usual (it gets put into
	# /usr so no config is needed):
	#  RedHat-derived systems:
	#	yum install gsl gsl-devel
	#  Debian derived systems will have a similar apt-get command.

	# in HOME
	mkdir clhep
	# download and unpack CLHEP 2.0.4.2 source into clhep/2.0.4.2
	cd $HOME/clhep/2.0.4.2
	mkdir build
	cd build
	../CLHEP/configure --prefix=$HOME/clhep --disable-shared
	make
	make install
	# now $HOME/clhep contains the installede CLHEP

	# in HOME
	# download geant4.9.2.p01 source, and unpack it into geant4.9.2.p01
	cd geant4.9.2.p01
	./Configure -build
		OS=Linux, Compiler=g++
		multiple machines: y
		source installed: $HOME/geant4.9.2.p01
		lib and source to be installed: $HOME/geant4.9.2.p01
		all headers in one dir: y                 (very important)
		default data dir: $HOME/geant4.9.2.p01/data   (+ defaults)
		CLHEP_BASE_DIR: $HOME/CLHEP               (+ defaults)
		build DLLs: n
		build 'global' libs: y                    (very important)
		build granular libs too: n                (very important)
		debug mode: n
		G4UI_NONE: n
		G4UI_BUILD_XAW_SESSION: n
		G4UI_BUILD_XM_SESSION: y
		G4UI_BUILD_QT_SESSION: n
		G4VIS_NONE: n
		G4VIS_BUILD_OPENGLX_DRIVER: y
		G4VIS_BUILD_OPENGLXM_DRIVER: y
		G4VIS_BUILD_DAWN_DRIVER: y
		G4VIS_BUILD_OIX_DRIVER: y
		OIVHOME: $HOME/Coin
		G4VIS_BUILD_RAYTRACERX_DRIVER: y
		G4VIS_BUILD_VRML_DRIVER: y
		G4UI_BUILD_OPENGLQT_DRIVER: n
		G4LIB_BUILD_G4TOG4: n
		G4LIB_BUILD_ZLIB: y
		G4ANALYSIS_USE: n
	./Configure -install
	./Configure

	# download and install Root version 5.22 into $HOME/root 
	#  FROM SOURCE. Be sure ROOTSYS is set properly.
	export ROOTSYS=$HOME/root
	cd $HOME/root
	./configure
	make
	sudo make install
	make static

	# download and unpack the G4beamline Linux distribution and source
	# into $HOME/g4beamline-1.16-Linux-g++

	# In HOME, create a file g4 containing the following:
		#       g4 - setup g4beamline environment
		#
		#       MUST be executed as ". g4"

		###
		### for RH7.3 environment
		###
		setup_ups
		setup gcc v3_2_1

		export G4INSTALL=$HOME/geant4.9.2.p01
		export OIVHOME=$HOME/Coin
		export ROOTSYS=$HOME/root
		export HISTO_DIR=$HOME/HistoScope/old_histo-v5-0-4-Linux-2-2

		# g4bl.setup must be run from its source directory
		'cd' $HOME/g4beamline-1.16-Linux-g++/source
		. ./g4bl.setup
		'cd' -

	# the idea is the g4 file will setup the build environment.
	. g4
	cd - # (goes to g4beamline-1.16-Linux-g++/source)
	
	# For a private build  you must remove the distribution libraries
	# from the g4bl lib directory, because they are ancient and won't
	# work with the version of gcc you are using.
	cd ../lib/Linux-g++
	mkdir unused
	mv *.so unused
	cd ../../source

	# make G4beamline
	make

	#put the bin into your path
	PATH=$HOME/g4beamline-1.16-Linux-g++/bin:$PATH
	g4bl -
	... should type the version and today's date, etc.




=========================
Windows XP (Intel) - VC++
=========================

NOTE: The Makefiles do not work for directories that have a space in their
name, so all external packages are installed into C:\. There is also the
problem that BOTH Cygwin and Microsoft tools must reference the files, so
you must NEVER use cygwin pathnames, you must use "mixed" pathnames like
C:/CLHEP (avoid \ because bash will munge them, always prepend C: or MS
tools will think it is an option). The default G4SYSTEM is WIN32-VC which
uses Microsoft VC++ as the compiler, but uses the Cygwin tools to control
the make.  Using g++ as the compiler (G4SYSTEM=WIN32-g++) works except for
Coin (which seemed to be hopeless). Note the VC++ code ran significantly
faster than g++ code (factor of 2 to 4, depending on CPU architecture).

It takes about 8 hours (plus mistakes) to build the program from scratch,
with high-speed internet access. Once all prerequisites are built, it takes
about 5 minutes to build g4beamline from its source.

Download and install the latest Cywgin version. Install the C++ development 
tools. Yes, you will need g++ (to generate Makefiles, not to compile code).
Also install your favorite editor. There are probably other packages
required.... All instructions below are typed into a Cygwin bash window.

Download and install MS VC++ 2008 express edition, and its SP1.
You only need the IDE if you are going to debug programs.
Add this line to /cygwin.bat to setup VC++ in Cygwin:
  call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvars32.bat"
Then close the Cygwin window and open a new one; make sure cl can be executed
(cl is the name of the VC++ compiler).

Do this to use 2 CPUs (if your system or VM has them):
	export MAKEFLAGS='-j 2'

NOTE: all binary libraries are for Windows, not Cygwin. The compiler will be
VC++, not g++ (but the latter is used to generate Makefiles).

NOTE: to build Coin you must have the VC++ link in your PATH before /bin/link.
Geant4, Root, and G4beamline need this too. You probably already have
the Windows system32 in your PATH after /usr/bin. What I did is:
	cd /usr/bin
	mv link.exe link-cygwin.exe

Download and install the Gnu Scientific Library (I have Gsl-1.8), binary.

Download Root 5.22 into C:/root
	export ROOTSYS=C:/root
	cd C:/root
	./configure
	make
	make static

Download Coin 2.4.6 source (.tar.gz). OLDER VERSIONS DO NOT WORK.
Download SoWin 1.4.0 source (.tar.gz).  http://www.coin3d.org
	cd C:/
	# C:/Coin will be install dir; source will be C:/Coin/Coin-2.4.6
	mkdir Coin
	cd Coin
	# bug in Cygwin's tar: cannot handle directory with spaces; mv can do so
	# (you can drag-and-drop a file onto a Cygwin window and paste its name)
	mv "---place you downloaded Coin-2.4.6.tar.gz---" .
	mv "---place you downloaded SoWin-1.4.0.tar.gz---" .
	tar -xzf Coin*.gz
	tar -xzf SoWin*.gz
	# optional cleanup: rm *.gz
	cd Coin-2.4.6
	# note shared libs DO NOT WORK (bug in Coin Makefiles)
	./configure --prefix=C:/Coin --disable-shared --enable-static \
		--disable-sound
	# Coin's configure will detect the presence of VC++ (cl) and use it.
	make
	# ignore cl command-line warnings
	unset MAKEFLAGS # cannot handle multiple CPUs
	make install
	export MAKEFLAGS='-j 2'
	cd ../SoWin-1.4.0
	# work around PATH bug in SoWin's configure
	cp ../bin/coin-config .
	./configure --prefix=C:/Coin --with-coin=C:/Coin --disable-shared \
		--enable-static
	make install

Download CLHEP source. Get the latest version. VERSIONS OLDER THAN 2.0.4.2
WILL NOT WORK (incompatible with Geant4.9.2).
http://proj-clhep.web.cern.ch/proj-clhep/
	cd C:/
	mkdir CLHEP
	cd CLHEP
	mv "---place you downloaded clhep-2.0.4.2.tgz---" .
	tar -xzf clhep-2.0.4.2.tgz
	# optional cleanup: rm *.tgz
	cd 2.0.4.2
	mkdir build
	cd build
	# CLHEP's configure will find cl and use it
	../CLHEP/configure --prefix=C:/CLHEP --disable-shared
	make
	make install

Download Geant4.9.2.p01 source files (GNU or Linux tar format). VERSIONS 
OLDER THAN 9.2 WILL NOT WORK (incompatible with G4beamline code).
	# geant4's --prefix and "Configure -install" are broken, so put source
	# and install directory right into C:/
	cd C:/
	mv "---place you downloaded geant4.9.2.p01.gtar.gz---" .
	tar -xzf geant4.9.2.p01.gtar.gz
	# optional cleanup: rm geant4.9.2.p01.gtar.gz
	cd geant4.9.2.p01
	*** NOTE: YOU MUST EDIT THE FOLLOWING FILE:
	Edit G4OpeninventorWin.cc to comment out lines 54-55
	   (omit calls to RemoveDispatcher(...) and AddDispatcher(...))

	export CPPFLAGS="-DCOIN_NOT_DLL -DSOWIN_NOT_DLL"
	./Configure -build
		OS=WIN32, Compiler=VC
		multiple machines: y
		source installed: C:/geant4.9.2.p01
		lib and source to be installed: C:/geant4.9.2.p01
		all headers in one dir: y                 (very important)
		default data dir: C:/geant4.9.2.p01/data      (+ defaults)
		CLHEP_BASE_DIR: C:/CLHEP                  (+ defaults)
		build DLLs: n
		build 'global' libs: n
		debug mode: n
		G4UI_NONE: n
		G4UI_BUILD_WIN32_SESSION: y
		G4UI_BUILD_QT_SESSION: n
		G4VIS_NONE: n
		G4VIS_BUILD_OPENGLWIN32_DRIVER: y
		G4VIS_BUILD_OIWIN32_DRIVER: y
		G4VIS_BUILD_OPENGLQT_DRIVER: n
		OIVHOME: C:/Coin
		G4LIB_BUILD_GDML: n
		G4LIB_BUILD_G4TOG4: n
		G4LIB_BUILD_ZLIB: n
		G4ANALYSIS_USE: n
	./Configure -install
	./Configure

Now build G4beamline itself.
	cd "-- place G4beamline source is installed --"
	cd source
	. g4bl.setup
	G4INSTALL=C:/geant4.9.2.p01
	make

Note you will get ~82 warnings of the form "cl : Command line warning D9024 :
  unrecognized source file type '...name.o'" -- ignore them.

Now test G4beamline.
	cd ../test
	./all

Now test the gui and OpenInventor.
	cd ../examples
	g4blgui example1.in
	--- select viewer=OIWin32, 9 ev/run, 9 runs, and push Run
	--- in OI window select File/escape to display next 9 events
	--- use Browse button to try the other examples



	DEBUG MODE

	export CPPFLAGS=/Zi CXXFLAGS=/Zi LDFLAGS=/Zi
	export CPPFLAGS="$CPPFLAGS -DCOIN_NOT_DLL -DSOWIN_NOT_DLL"

	# ignore "/Zi: No such file or directory" warnings

	cp -a C:/CLHEP C:/CLHEP-DEBUG
	remove bin,lib,doc,
	cd 2.0.4.2
	rm -fr build/*; cd build
	../CLHEP/configure --prefix=C:/CLHEP-DEBUG --disable-shared
	make
	make install

	cp -a C:/geant4.9.2.p01 C:/geant4.9.2.p01.debug
	cd $_
	rm -fr tmp lib
	./Configure -build -- use above CLHEP, set G4UI_NONE and G4VIS_NONE,
	debug mode yes, global static libs only, no UIs, no vizualization
	./Configure -install
	./Configure

	cd 
	. g4-debug # edited to point to the above directories
	cd T:/g4bl/tmp
	rm -fr WIN32-VC     # a fast "make clean"
	cd ../source
	make

	Remarkably, the VC++ GUI debugger can "Attach to Process", and it
	works for g4beamline, including source(!). So there is no need to
	use the VC++ GUI to build a project, just build as above, run g4bl,
	and do Debug/AttachToProcess selecting g4beamline.exe. You may want
	to slow its startup down so you can attach to it, but it seems that 
	the dialog box that pops up asking if you want to send a report to
	Microsoft gives enough time if there is an exception. Amazingly,
	breakpoints are preserved across runs, as long as you don't rebuild.




================
MAC OS X (Intel)
================

The necessary libraries are: Coin, SoXt, Root, CLHEP, GSL, and Geant4.
The first two are not needed if OpenInventor visualization is not used.
The Gnu Scientific Library (/usr/lib/libgsl.a) is on my system, but I have
lost track of how it got there.

NOTE: my Mac Pro has 4 CPUs, so I put this into my environment:
	export MAKEFLAGS="-j 4"
with 1 CPU no value is needed. This lets make run 4 compilations in parallel
and significantly speeds things up.

Note: you must install lesstif using Fink commander; it goes into /sw .

Note: g4bl runs only under X-windows (i.e. use the X11 application).

Note: on Leopard it found Motif in /usr/local/lib/libXm.dylib, but it was PPC 
only. I have lesstif installed into /sw, so be sure to use that one.

NOTE: You must compile Root to get its static libraries. Root requires 
up-to-date tools: Xcode 3.1.2 and gfortran (from http://www.macresearch.org/gfortran-leopard).
So it's best to update these tools before starting (though Coin built with
the Xcode 3.0 seems to work)..



Coin and SoXt
-------------
The basic idea is to download and build them in $HOME/coin. The Geant4
Makefile-s do not support frameworks, so I built as a static library.
Geant4 only supports the X-windows binding (SoXt).
	# use fink to get motif into /sw
	cd $HOME
	mkdir coin
	cd coin
	# download and unpack the source into Coin-3.1.0 and SoXt-1.2.2 
	# under $HOME/coin -- http://coin3d.org
	cd Coin-3.1.0
        configure --enable-static --without-framework \
                --enable-darwin-x11 --prefix=$HOME/coin
	make
	make install
	cd ../SoXt-1.2.2
        configure --enable-static --without-framework \
                --enable-darwin-x11 --prefix=$HOME/coin \
		--with-motif=/sw --with-coin=$HOME/coin
	make
	make install
The result is:
    coin/bin:
    coin-config     soxt-config

    coin/include:
    Inventor        SoWinEnterScope.h       SoWinLeaveScope.h

    coin/lib:
    libCoin.60.1.0.dylib*    libCoin.la*           libSoXt.dylib@
    libCoin.60.dylib@        libSoXt.2.0.0.dylib*  libSoXt.la*
    libCoin.a                libSoXt.2.dylib@      pkgconfig/
    libCoin.dylib@           libSoXt.a

(plus man and share directories)

These environmant variables are used to build Geant4 and G4beamline: 
    export OIVHOME=$HOME/coin
    export OIVFLAGS=-I$OIVHOME/include
    export OIVLIBS="$OIVHOME/lib/libSoXt.a $OIVHOME/lib/libCoin.a"
Export these BEFORE doing the Geant4 "Configure -build".


Root
----
Simply download version 5.22 SOURCE and install it into $HOME/root. 
http:root/cern.ch

Then make sure that you set the ROOTSYS environment variable:
   (bash,ksh,sh)	export ROOTSYS=$HOME/root
   (tcsh,csh)		setenv ROOTSYS $HOME/root
you should probably put those into .bash_profile, .profile, or whatever 
shell initialization file is appropriate for your shell. Then re-login or
otherwise activate it before proceeding.  

	cd $HOME/root
	./configure macosx  # the default macosx64 does not seem to work
	unset MAKEFLAGS     # root build is not multi-CPU
	make
	make static
The result is $HOME/root/lib/libRoot.a (plus the root executable and its
shared objects and a few other .a files).

NOTE: G4beamline is now built with the STATIC root library on Mac OS X.

As I often want to keep multiple versions of Root around, I set ROOTSYS to
$HOME/root in $HOME/.bash_profile, and do this:
	cd $HOME
	# BEFORE unpacking Root
	# if root isn't a symbolic link, make it be one
	rm root
	# now unpack the Root tarball (it unpacks into a directory named root)
	cd root
	# build root as above
	cd $HOME
	mv root root_5.22
	ln -s root_5.22 root
Now by just changing the symbolic link I can change versions.


CLHEP
-----
The basic idea is to download and build CLHEP in $HOME/clhep. The Geant4
Makefile-s do not support frameworks.
	cd $HOME
	mkdir clhep
	cd clhep
	# download and unpack CLHEP into 2.0.4.2 under $HOME/clhep
	# http://proj-clhep.web.cern.ch/proj-clhep/
	cd 2.0.4.2
	mkdir build  # this is how they say to do it
	cd build
	../CLHEP/configure --prefix=$HOME/clhep 
	export MAKEFLAGS='-j 4'   # clhep build can be multi-CPU
	make
	make install

The result is:
	./bin:
	Cast-config*			RandomObjects-config*
	Evaluator-config*		RefCount-config*
	Exceptions-config*		Units-config*
	GenericFunctions-config*	Vector-config*
	Geometry-config*		clhep-config*
	Matrix-config*			clheplib*
	Random-config*
	
	./include:
	CLHEP/
	
	./lib:
	libCLHEP-2.0.4.2.a
	libCLHEP-2.0.4.2.dylib*
	libCLHEP-Cast-2.0.4.2.a
	libCLHEP-Cast-2.0.4.2.dylib*
	libCLHEP-Evaluator-2.0.4.2.a
	libCLHEP-Evaluator-2.0.4.2.dylib*
	libCLHEP-Exceptions-2.0.4.2.a
	libCLHEP-Exceptions-2.0.4.2.dylib*
	libCLHEP-GenericFunctions-2.0.4.2.a
	libCLHEP-GenericFunctions-2.0.4.2.dylib*
	libCLHEP-Geometry-2.0.4.2.a
	libCLHEP-Geometry-2.0.4.2.dylib*
	libCLHEP-Matrix-2.0.4.2.a
	libCLHEP-Matrix-2.0.4.2.dylib*
	libCLHEP-Random-2.0.4.2.a
	libCLHEP-Random-2.0.4.2.dylib*
	libCLHEP-RandomObjects-2.0.4.2.a
	libCLHEP-RandomObjects-2.0.4.2.dylib*
	libCLHEP-RefCount-2.0.4.2.a
	libCLHEP-RefCount-2.0.4.2.dylib*
	libCLHEP-Vector-2.0.4.2.a
	libCLHEP-Vector-2.0.4.2.dylib*
	libCLHEP.a@
	libCLHEP.dylib@



Geant4
------
Geant4 does not like having its install directory different from its source dir.
So the basic idea is to download and build Geant4 into $HOME/geant4.9.2.p01, and
then symbolically link that to $HOME/geant4..
	cd $HOME
	# download and unpack the geant4 source into geant4.9.2.p01
	# http://geant4.web.cern.ch/geant4/
	cd geant4.9.2.p01
	# now build Geant4
	export MAKEFLAGS='-j 4'	# Geant4 granular build can be multi-CPU
				# (global library build cannot be multi-CPU)
	./Configure -build
  OS) Darwin
  Compiler) g++
  run on multiple machines? y
  Geant4 source $HOME/geant4.9.2.p01
  path where lib and source files should be installed $HOME/geant4
  copy all headers into one directory? y
  data directory $HOME/geant4.9.2.p01/data
  directory where CLHEP is installed $HOME/clhep
  build shared (.so) libs? yes
  build static libraries also? yes
  global compound libraries? no
  compile in DEBUG mode? no
  set G4UI_NONE? no
  Use XAW? no
  Use XM? no
  Use QT? no
  set G4VIS_NONE? no
  Use OPENGLX? yes
  Use OPENGLXM? yes
  Path where Xm is found? /sw
  Use DAWN? yes
  Use OIX? yes
  Path for OpenInventor $HOME/coin
  Use RAYTRACERX? yes
  Use VRML? yes
  Use OPENGLQT? no
  Build GDML? no
  Build G3TOG4? no
  Build ZLIB? yes
  Use G4ANALYSIS? no
  ... Press [Enter] to start...

... took ~10 minutes to build on a 4 CPU Mac Pro.

IMPORTANT: There's more to do:

	./Configure -install
	./Configure

Now before building with geant4, you must do:
      (bash,ksh,sh)	. $HOME/geant4/env.sh
      (csh,tcsh)	source $HOME/geant4/env.csh
but this is included in the g4bl.setup script.


At this point I exit the shell and start a new one.


G4beamline
----------
NOTE: for private builds you must move the distribution .dylib-s out of
your path, and use the ones appropriate for your system. Since the g4bl script
and g4blgui both put $G4BL_DIR/lib/Darwin-g++ into LD_DYLIB_PATH, you must 
do something like this:
	cd g4beamline-install-dir
	cd lib/Darwin-g++
	mkdir unused
	mv *.dylib unused

The G4beamline source is in $HOME/g4bl . The Makefile is arranged so you
should do ". g4bl.setup" in g4bl/source before doing the make. It will prompt
for unknown directories, so I create a file $HOME/g4 that sets it all up:
    #	g4 - setup g4beamline build environment
    #
    #	MUST be executed as ". g4"
    export G4INSTALL=$HOME/geant4.9.2.p01
    export ROOTSYS=$HOME/root
    export HISTO_DIR=""
    export OIVHOME=$HOME/coin
    export OIVFLAGS=-I$OIVHOME/include
    export OIVLIBS="$OIVHOME/lib/libSoXt.a $OIVHOME/lib/libCoin.a"
    'cd' g4bl/source
    . g4bl.setup
    'cd' -
(For consistency, g4bl.setup uses the configuration of CLHEP used to build
Geant4, so this file does not need to set it up.)

So the way to build g4beamline (once Coin, SoXt, CLHEP, Root, GSL, and Geant4
are all installed, and g4 is setup to reference them):
	cd $HOME
	. g4
	cd -
	make

The GNUmakefile has no install target, and one must install it manually.
"make distrib" will make the Mac OS distribution.