LLVM Clang from Source

commentnote:Are you sure!? Are you sure!? Are you sure!?

Building an LLVM (Clang) compiler is substantially more difficult than GCC

Because we need MPICH to ultimately wrap to a Fortran compiler, it will be necessary to build GCC first, and then LLVM Clang. Therefore both compilers must be built perfectly (you really need to pay attention to any warnings/errors you encounter) to achieve a proper LLVM Clang compiler stack.

Due to the general nature of the following document, the commands you see here are for reference and should not blindly be 'cut and pasted' into your terminal.

Prerequisites

  • A modern GCC compiler with Fortran language enabled

  • A sane, modern developers environment (toolchain). Examples how to do this for several popular operating systems:

    • Macintosh: Xcode (xcode-select --install)

    • Windows: Windows Subsystem for Linux (WSL)

      • In addition, a chosen Linux flavor below

    • Ubuntu: (apt install build-essential)

    • CentOS/Rocky: (dnf groupinstall 'Development Tools')

    • OpenSUSE: (zypper install --type pattern devel_basis)

Different operating systems leverage different means by which to obtain a developers environment. We assume the reader is intricately familiar with their platform of choice, and has established such an environment before proceeding.

LLVM Clang

Clone the LLVM-Project:

git clone https://github.com/llvm/llvm-project
cd llvm-project
git checkout llvmorg-16.0.6

Configure LLVM using the recommended arguments:

mkdir llvm-build
cd llvm-build
cmake ../llvm -G 'Unix Makefiles' \
-DLLVM_ENABLE_PROJECTS='clang;clang-tools-extra;compiler-rt;libcxx;libcxxabi;libunwind;openmp;lldb' \
-DCMAKE_INSTALL_PREFIX=/target/installation/path/llvm-16.0.6 \
-DCMAKE_INSTALL_RPATH:STRING=/target/installation/path/llvm-16.0.6/lib \
-DCMAKE_INSTALL_NAME_DIR:STRING=/target/installation/path/llvm-16.0.6/lib \
-DCMAKE_BUILD_WITH_INSTALL_RPATH=1 \
-DLLVM_TARGETS_TO_BUILD="X86" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_MACOSX_RPATH:BOOL=OFF \
-DCMAKE_CXX_LINK_FLAGS="-L/some/path/to/gcc-12.2.1/lib64 -Wl,-rpath,/some/path/to/gcc-12.2.1/lib64" \
-DGCC_INSTALL_PREFIX=/some/path/to/gcc-12.2.1 \
-DCMAKE_CXX_COMPILER=/some/path/to/gcc-12.2.1/bin/g++ \
-DCMAKE_C_COMPILER=/some/path/to/gcc-12.2.1/bin/gcc
commentnote:GCC Paths

The above configuration assumes you are using a custom version of GCC (note the several gcc-12.2.1 paths)

With configure complete (and error free), build and install LLVM Clang:


make -j 6
make install

MPICH

Check and see if you already have an MPI wrapper available on your machine. One simple way of doing so, is to perform a which on several MPI wrapper binaries:


which mpicc mpicxx mpif90

If the above command returns with paths to their respected binaries, you will want to work with your system package manager to remove or suppress it.

warningwarning

Having two completely different MPI wrappers being made available simultaneously is prone to failure.

Download MPICH 4.0.2

curl -L -O http://www.mpich.org/static/downloads/4.0.2/mpich-4.0.2.tar.gz
tar -xf mpich-4.0.2.tar.gz -C .

Create an out-of-tree build location and configure MPICH using the recommended arguments:

mkdir mpich-4.0.2/clang-build
cd mpich-4.0.2/clang-build

../configure --prefix=/target/installation/path/mpich-4.0.2 \
--enable-shared \
--enable-sharedlibs=clang \
--enable-fast=O2 \
--enable-debuginfo \
--enable-totalview \
--enable-two-level-namespace \
CC=clang \
CXX=clang++ \
FC=gfortran \
F77=gfortran \
F90='' \
CFLAGS='' \
CXXFLAGS='' \
FFLAGS='-fallow-argument-mismatch' \
FCFLAGS='-fallow-argument-mismatch' \
F90FLAGS='' \
F77FLAGS=''

With configure complete and error free, build and install MPICH:


make -j 6
make install

Follow the onscreen instructions on how to make use of your new MPI wrapper.

With the compiler stack ready, you can proceed to the next section: