Skip to content

Building Flang

Steve Scalpone edited this page Mar 19, 2018 · 26 revisions

Building Flang

We build Flang on Intel x86-64 and OpenPOWER hardware running either Ubuntu or Red Hat.

Prerequisites

Building LLVM requires fairly modern compiler toolchain and CMake, check Getting started with LLVM and Building LLVM with CMake for the full list.

http://llvm.org/docs/CMake.html

Dependencies

  • LLVM
  • openmp-llvm
  • modified clang

The latest supported LLVM version is 5.0. Flang also supports LLVM version 4.0.

To build version 4.0, substitute 40 for 50 in the build instructions below.

Building

Custom install location

The command-line examples provided below will install everything into the default system location.

To specify a custom install location, add -DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX> to each of the CMake commands given below.

When using a custom install location, you must make sure that the bin directory is on your PATH when building and running flang.

Flang is developed outside of the llvm source tree.

Step-by-step instructions

Get LLVM 5.0, build and install it according to instructions

cd where/you/want/to/build
git clone https://github.com/llvm-mirror/llvm.git
cd llvm
git checkout release_50
mkdir build && cd build
cmake ..
make
sudo make install

Get the modified clang for flang, build and install it (4.0)

cd where/you/want/to/build
git clone https://github.com/flang-compiler/clang.git
cd clang
git checkout flang_release_50
mkdir build && cd build
cmake ..
make
sudo make install

If you use CMAKE_INSTALL_PREFIX in Step 1 and <INSTALL_PREFIX>/bin is not in your path, you need to add -DLLVM_CONFIG=<INSTALL_PREFIX>/bin/llvm-config when invoking CMake, otherwise you will encounter an error related to LLVMConfig.cmake not being found.

Build and install openmp-llvm (5.0)

cd where/you/want/to/build
git clone https://github.com/llvm-mirror/openmp.git
cd openmp/runtime
git checkout release_50
mkdir build && cd build
cmake ..
make`
sudo make install

Build and install the flang components

cd where/you/want/to/build
git clone https://github.com/flang-compiler/flang.git
cd flang
mkdir build && cd build
cmake -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DCMAKE_Fortran_COMPILER=flang ..
make
sudo make install

If you use CMAKE_INSTALL_PREFIX in Step 1 and <INSTALL_PREFIX>/bin is not in your path, you need to add -DLLVM_CONFIG=<INSTALL_PREFIX>/bin/llvm-config and set the explicit paths for the compilers (i.e. -DCMAKE_CXX_COMPILER=<INSTALL_PREFIX>/bin/clang++ -DCMAKE_C_COMPILER=<INSTALL_PREFIX>/bin/clang -DCMAKE_Fortran_COMPILER=<INSTALL_PREFIX>/bin/flang) when invoking CMake, otherwise you will encounter errors.

(Alternative:) Build flang using Spack

Spack is a flexible package manager for HPC system and can be used to build flang and its dependencies.

Get Spack

git clone https://github.com/llnl/spack.git

On bash:

source spack/share/spack/setup-env.sh

or tcsh:

source spack/share/spack/setup-env.csh

Build Flang and its depencencies:

spack install flang

Watch out for the installation path, the flang wrapper script in there is ready to use.

Setup Flang as a compiler inside spack if you want to build spack package with flang:

spack compiler add path/to/flang/bin

Now you might want to edit your ~/.spack/*/compilers.yaml to combine flang with any c compiler of your choice.

Using Flang

To test your installation, create a simple "hello world" program, like the following:

program hello
  print *, 'hello world'
end

Next, compile the program in the following manner. We will assume the program is called hello.f90

% flang hello.f90

If the build succeeds, then you can execute the program in the following manner:

% ./a.out

Clone this wiki locally