4.2.  Building and Installing Geant4 with CMake

CMake processes simple input scripts to generate output buildscripts for a native build tool such as makefiles on UNIX. The build process for Geant4 using CMake is thus to run CMake first to generate the native buildscripts and then to run the native tool using these buildscripts as input to build and install.

You should have obtained all the prerequisite software described above, and should have unpacked the Geant4 sources to a location of your choice, for instance

            /path/to/geant4.9.4
        

We call this directory the source tree. The CMake build enforces an out of source build so that the buildscripts generated by CMake do not mix with the actual Geant4 sources. This is particularly important for developers using a working copy from the geant4 repository, and also means you can build multiple configurations of Geant4 against one single source tree.

You therefore need to build Geant4 in a separate directory which we call the build tree. This directory can be created wherever you like, though we strongly recommend it is outside the source tree. In addition, if you wish to use an IDE such as Eclipse, there may be limitations on the location of the build tree relative to the source tree. For these reasons, we recommmend creating your build tree directory alongside the source tree, e.g. using our location from above

            /path/to/geant4.9.4
            /path/to/geant4.9.4-build
        

You are of course free to experiment here, but the above is the recommended way. You may have as many build trees as you like.

4.2.1.  Running CMake

CMake generates buildscripts (e.g. Makefiles, Xcode projects) for the build tool (e.g. make, Xcode) of your choice. The first step after setting up the source and build trees is therefore to run CMake in the build tree to generate the needed scripts. We shall assume in the following that we are on *NIX, where CMake defaults to generating Makefiles, and we shall use the CMake command line interface. CMake has other interfaces such as the Curses based ccmake, and if you wish to use these, you should consult the documentation supplied with CMake.

If you wish to generate, e.g. Xcode projects, consult the CMake documentation for the -G option which allows you to select another tool. The GUI interface to CMake available on some platforms also has a switch to choose the build tool.

Our first step is to move into the build tree directory and then run CMake, pointing it to the source tree, and setting the CMAKE_INSTALL_PREFIX variable to set where geant4 will be installed. Using our example source and build trees from above, and assuming you want to install Geant4 under /install/location (NB $ denotes the prompt, not a literal '$')

            $ cd /path/to/geant4.9.4-build
            $ cmake -DCMAKE_INSTALL_PREFIX=/install/location ../geant4.9.4
        

The cmake command is the basic command line interface to CMake. All being well, you will see some platform dependent output (not complete)

            $ cmake -DCMAKE_INSTALL_PREFIX=/install/location ../geant4.9.4
            -- The C compiler identification is GNU
            -- The CXX compiler identification is GNU
            -- Check for working C compiler: /usr/bin/gcc
            -- Check for working C compiler: /usr/bin/gcc -- works
            -- Detecting C compiler ABI info
            -- Detecting C compiler ABI info - done
            -- setting default compiler flags for CXX
            -- Check for working CXX compiler: /usr/bin/c++
            -- Check for working CXX compiler: /usr/bin/c++ -- works
            -- Detecting CXX compiler ABI info
            -- Detecting CXX compiler ABI info - done
        

CMake will then proceed to check for needed external packages. Assuming these are all installed correctly, then it should continue, giving the output (on Linux with GNU compiler)

            -- Found CLHEP version: CLHEP 2.1.0.1
            -- Found CLHEP: TRUE
            -- Geant4 backwards compatible system name: Linux
            -- Geant4 backwards compatible compiler name: g++
            -- Geant4 backwards compatible variable G4SYSTEM : Linux-g++
            -- Geant4 backwards compatible variable G4INSTALL: /install/location/share/geant4-9.4.0
            -- Geant4 backwards compatible variable G4INCLUDE: /install/location/include/geant4
            -- Geant4 backwards compatible variable G4LIB    : /install/location/lib/geant4-9.4.0
            -- The following Geant4 features are enabled:

            -- Configuring done
            -- Generating done
            -- Build files have been written to: /path/to/geant4.9.4-build
        

Depending on the speed of your computer and/or filesystem, there may be pauses at the Configuring and Generating stages as CMake actually writes out files.

The detection of CLHEP relies on having the clhep-config script in the PATH, and if this is not the case on your system, you may see an error

            CMake Error at /usr/share/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:70 (MESSAGE):
              Failed to find CLHEP (missing: CLHEP_VERSION_OK CLHEP_LIBRARIES
              CLHEP_INCLUDE_DIRS)
            Call Stack (most recent call first):
              cmake/Modules/FindCLHEP.cmake:118 (find_package_handle_standard_args)
              CMakeLists.txt:114 (find_package)


            -- Configuring incomplete, errors occurred!
        

If you see this error, you should either add the location of clhep-config to your PATH and re-run cmake as above, or re-run cmake and pass it the full path to clhep-config as

            $ cmake -DCLHEP_CONFIG_EXECUTABLE=/full/path/to/clhep-config ../geant4.9.4
        

You should then see a successfull configuration as above.

If you now list the contents of your build directory, it will contain something like

            $  ls
            CMakeCache.txt  cmake_install.cmake  CPackSourceConfig.cmake  outputs
            CMakeFiles      CPackConfig.cmake    Makefile                 source
        

Here a Makefile has been created, and if you choose to use one of the other generators, different files will be visible.

4.2.2.  Enabling Optional Components

The proceedure for running CMake described in the preceeding section configures a default build of Geant4

  • Global dynamic libraries.

  • No Geant4 modules requiring external library support.

Extra features can be enabled by passing extra -D command line arguments to CMake, as was done to tweak the install prefix above through CMAKE_INSTALL_PREFIX. Available options to enable extra features and tweak paths are listed below.

  • BUILD_SHARED_LIBS: Set to ON to build dynamic Geant4 libraries (ON by default).

  • BUILD_STATIC_LIBS: Set to ON to build archive Geant4 libraries (OFF by default).

  • CMAKE_INSTALL_PREFIX: Install path prefix (/usr/local by default on UNIX).

  • GEANT4_USE_GDML: Set to ON to build Geant4 with GDML support (OFF by default, requires Xerces-C to be installed).

  • GEANT4_USE_GEANT3TOGEANT4: Set to ON to build Geant4 with 3-to-4 conversion support (OFF by default).

  • GEANT4_USE_QT: Set to ON to build Geant4 with Qt support (OFF by default, requires Qt4 with QtCore, QtGui and QtOpenGL, and OpenGL to be installed).

Note that if you enable a USE option, extra system checks will be triggered to check for the needed headers and libraries. If you encounter errors at this point, the error message should give you details on the cause.

Further variables are available for accurately locating needed programs and libraries, tuning the install and selecting advanced options.

  • CLHEP_CONFIG_EXECUTABLE: Full path to clhep-config executable (only needed for fine tuning or if clhep-config is not in your PATH).

  • GEANT4_USE_NETWORKDAWN: Set to ON to build Geant4 DAWN driver with Client/Server support(OFF by default, only available on *NIX systems).

  • GEANT4_USE_NETWORKVRML: Set to ON to build Geant4 VRML driver with Client/Server support(OFF by default, only available on *NIX systems).

  • GEANT4_BUILD_GRANULAR_LIBS: Set to ON to build Geant4 granular libraries (OFF by default, if set to ON global libraries will not be built).

It is strongly recommend to build global libraries. Granular libraries are only intended as a tool for Geant4 developers to cleanly debug and test their code.

4.2.3.  Building Geant4 from CMake Generated Buildscripts

Once CMake has configured the build, we simply need to run the appropriate build tool with the generated build scripts. In our example we have generated Makefiles, so in our build directory, we simply type

            $ make
        

to build. All being well, you should see the output

            $ make
            Scanning dependencies of target G4global
            [  1%] Building CXX object source/global/CMakeFiles/G4global.dir/HEPNumerics/src/G4AnalyticalPolSolver.cc.o
            [  1%] Building CXX object source/global/CMakeFiles/G4global.dir/HEPNumerics/src/G4ChebyshevApproximation.cc.o
...
        

By default, CMake Makefiles produce a summary output of what's being built. If you wish to see more detail, you can run make with

            $ make VERBOSE=1
        

which will output a very detailed report of everything being done.

CMake Makefiles also support parallel builds, so you can also do

            $ make -jN
        

where N should be selected based on the number of cores your machine has available. If there are errors in the build, make will immediately exit except in parallel mode where it will exit at the next branch point.

You can also build parts of Geant4 selectively.

            $ make help
        

will print a list of all targets that can be built.

If the build is successful, you can run

            $ make install
        

to install Geant4 under the location specified by CMAKE_INSTALL_PREFIX from earlier. As noted, this defaults to /usr/local on *NIX, so you will need to change it as described if you don't have write permission there. Note that you can also do a staged install using DESTDIR as

            $ make install DESTDIR=/path/to/stage
        

If you are not using Makefiles, you will need to consult the documentation of your buildtool. However, CMake generally works by the book of these tools and so using the generated buildscripts should be straightforward.