Skip to content

Instantly share code, notes, and snippets.

@nsmith-
Last active August 12, 2024 15:12
Show Gist options
  • Save nsmith-/6f40a9614fe461ee274dd708b7e59691 to your computer and use it in GitHub Desktop.
Save nsmith-/6f40a9614fe461ee274dd708b7e59691 to your computer and use it in GitHub Desktop.

Building G4beamline on apple silicon

This worked for me on an M3 Mac with Sonoma (14.5). I decided to install everything in a folder rather than $HOME.

mkdir g4beamline
cd g4beamline/
export INSTALL_PREFIX=$PWD/install

The G4beamline source is downloaded from https://muonsinc.com/G4beamlineDownload.php

mv ~/Downloads/G4beamline-3.08-source.tgz .
tar -zxf ../G4beamline-3.08-source.tgz 

Most of the instructions follow G4beamline-3.08-source/doc/BUILD.txt. Note: root requires a full xcode install, which is a bit annoying

brew install cmake fftw gsl root qt@5
curl -Ol https://gitlab.cern.ch/geant4/geant4/-/archive/v11.0.2/geant4-v11.0.2.tar.gz
tar zxf geant4-v11.0.2.tar.gz 
cd geant4-v11.0.2
mkdir build 
cd build/
# I added `-DGEANT4_INSTALL_DATA=ON` to download all the Geant4 data
PATH="$(brew --prefix qt@5)/bin:$PATH" cmake -DGEANT4_INSTALL_DATA=ON -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DGEANT4_USE_QT=ON -DBUILD_SHARED_LIBS=OFF -DBUILD_STATIC_LIBS=ON ..
cmake --build . --config Release --target install -j4

There's a bug in $INSTALL_PREFIX/lib/Geant4-11.0.2/Geant4PackageCache.cmake that I had to fix manually:

19c19
< geant4_set_and_check_package_variable(EXPAT_LIBRARY ""  "")
---
> geant4_set_and_check_package_variable(EXPAT_LIBRARY "" PATH "")
cd $INSTALL_PREFIX
mkdir G4beamline-3.08
cd G4beamline-3.08
export GEANT4_DIR=$INSTALL_PREFIX
export ROOTSYS=$(brew --prefix root)
export GSL_DIR=$(brew --prefix gsl)
export FFTW_DIR=$(brew --prefix fftw)
PATH="$(brew --prefix qt@5)/bin:$PATH" cmake ../G4beamline-3.08-source
cmake --build . --config Release --target install -j4

I had some trouble with errors like <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header which seem to stem from build toolset choice. Google suggested that I switch the toolset back to CommandLineTools with sudo xcode-select --switch /Library/Developer/CommandLineTools. To see which c++ compiler is used you can add --verbose to the cmake command.

The following seems to be the steps to build the MacOS .app:

mkdir lib
../G4beamline-3.08-source/scripts/distrib-darwin 3.08
echo $INSTALL_PREFIX/share/Geant4-11.0.2/data/ > G4beamline-3.08.app/Contents/.data

Then you can start the app with open G4beamline-3.08.app or through finder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment