3 Flang is a ground-up implementation of a Fortran front end written in modern
4 C++. It started off as the f18 project (https://github.com/flang-compiler/f18)
5 with an aim to replace the previous flang project
6 (https://github.com/flang-compiler/flang) and address its various deficiencies.
7 F18 was subsequently accepted into the LLVM project and rechristened as Flang.
11 Read more about flang in the [docs directory](docs).
12 Start with the [compiler overview](docs/Overview.md).
14 To better understand Fortran as a language
15 and the specific grammar accepted by flang,
16 read [Fortran For C Programmers](docs/FortranForCProgrammers.md)
18 flang's specifications of the [Fortran grammar](docs/f2018-grammar.md)
20 the [OpenMP grammar](docs/OpenMP-4.5-grammar.md).
22 Treatment of language extensions is covered
23 in [this document](docs/Extensions.md).
25 To understand the compilers handling of intrinsics,
26 see the [discussion of intrinsics](docs/Intrinsics.md).
28 To understand how a flang program communicates with libraries at runtime,
29 see the discussion of [runtime descriptors](docs/RuntimeDescriptor.md).
31 If you're interested in contributing to the compiler,
32 read the [style guide](docs/C++style.md)
34 also review [how flang uses modern C++ features](docs/C++17.md).
36 If you are interested in writing new documentation, follow
37 [LLVM's Markdown style guide](https://github.com/llvm/llvm-project/blob/main/llvm/docs/MarkdownQuickstartTemplate.md).
40 There are two ways to build flang. The first method is to build it at the same
41 time that you build all of the projects on which it depends. This is called
42 building in tree. The second method is to first do an in tree build to create
43 all of the projects on which flang depends, and then only build the flang code
44 itself. This is called building standalone. Building standalone has the
45 advantage of being smaller and faster. Once you create the base build and base
46 install areas, you can create multiple standalone builds using them.
48 Note that instructions for building LLVM can be found at
49 https://llvm.org/docs/GettingStarted.html.
51 ### Building flang in tree
52 Building flang in tree means building flang along with all of the projects on
53 which it depends. These projects include mlir, clang, flang, and compiler-rt.
54 Note that compiler-rt is only needed to access libraries that support 16 bit
55 floating point numbers. It's not needed to run the automated tests.
57 Here's a complete set of commands to clone all of the necessary source and do
60 First clone the source:
62 git clone https://github.com/llvm/llvm-project.git my-project
64 Once the clone is complete, execute the following commands:
76 -DCMAKE_BUILD_TYPE=Release \
77 -DFLANG_ENABLE_WERROR=On \
78 -DLLVM_ENABLE_ASSERTIONS=ON \
79 -DLLVM_TARGETS_TO_BUILD=host \
80 -DCMAKE_INSTALL_PREFIX=$INSTALLDIR
82 -DLLVM_ENABLE_PROJECTS="clang;mlir;flang" \
83 -DLLVM_ENABLE_RUNTIMES="compiler-rt"
88 To run the flang tests on this build, execute the command in the "build"
94 Note that these instructions specify flang as one of the projects to build in
95 the in tree build. This is not strictly necessary for subsequent standalone
96 builds, but doing so lets you run the flang tests to verify that the source
97 code is in good shape.
98 ### Building flang standalone
99 To do the standalone build, start by building flang in tree as described above.
100 This build is base build for subsequent standalone builds. Start each
101 standalone build the same way by cloning the source for llvm-project:
103 git clone https://github.com/llvm/llvm-project.git standalone
105 Once the clone is complete, execute the following commands:
108 base=<directory that contains the in tree build>
117 -DCMAKE_BUILD_TYPE=Release \
118 -DFLANG_ENABLE_WERROR=On \
119 -DLLVM_TARGETS_TO_BUILD=host \
120 -DLLVM_ENABLE_ASSERTIONS=On \
121 -DLLVM_BUILD_MAIN_SRC_DIR=$base/build/lib/cmake/llvm \
123 -DLLVM_DIR=$base/build/lib/cmake/llvm \
124 -DCLANG_DIR=$base/build/lib/cmake/clang \
125 -DMLIR_DIR=$base/build/lib/cmake/mlir \
131 To run the flang tests on this build, execute the command in the "flang/build"
137 ## Supported C++ compilers
139 Flang is written in C++17.
141 The code has been compiled and tested with
142 GCC versions from 7.2.0 to 9.3.0.
144 The code has been compiled and tested with
145 clang version 7.0, 8.0, 9.0 and 10.0
146 using either GNU's libstdc++ or LLVM's libc++.
148 The code has been compiled on
149 AArch64, x86\_64 and ppc64le servers
150 with CentOS7, Ubuntu18.04, Rhel, MacOs, Mojave, XCode and
151 Apple Clang version 10.0.1.
153 ### Building flang with GCC
156 cmake will search for g++ on your PATH.
157 The g++ version must be one of the supported versions
158 in order to build flang.
160 Or, cmake will use the variable CXX to find the C++ compiler. CXX should include
161 the full path to the compiler or a name that will be found on your PATH, e.g.
162 g++-8.3, assuming g++-8.3 is on your PATH.
169 CXX=/opt/gcc-8.3/bin/g++-8.3 cmake ...
172 ### Building flang with clang
174 To build flang with clang,
175 cmake needs to know how to find clang++
176 and the GCC library and tools that were used to build clang++.
178 CXX should include the full path to clang++
179 or clang++ should be found on your PATH.
184 ### Installation Directory
186 To specify a custom install location,
188 `-DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX>`
190 where `<INSTALL_PREFIX>`
191 is the path where flang should be installed.
195 To create a debug build,
197 `-DCMAKE_BUILD_TYPE=Debug`
198 to the cmake command.
199 Debug builds execute slowly.
201 To create a release build,
203 `-DCMAKE_BUILD_TYPE=Release`
204 to the cmake command.
205 Release builds execute quickly.
209 Flang supports 2 different categories of tests
210 1. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests)
211 2. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests)
213 ## For standalone builds
217 cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src
221 To run individual regression tests llvm-lit needs to know the lit
222 configuration for flang. The parameters in charge of this are:
223 flang_site_config and flang_config. And they can be set as shown below:
225 <path-to-llvm-lit>/llvm-lit \
226 --param flang_site_config=<path-to-flang-build>/test-lit/lit.site.cfg.py \
227 --param flang_config=<path-to-flang-build>/test-lit/lit.cfg.py \
228 <path-to-fortran-test>
234 If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`ON` by default), it is possible to generate unittests.
235 Note: Unit-tests will be skipped for LLVM install for an standalone build as it does not include googletest related headers and libraries.
237 There are various ways to run unit-tests.
241 1. ninja check-flang-unit
242 2. ninja check-all or ninja check-flang
243 3. <path-to-llvm-lit>/llvm-lit \
245 4. Invoking tests from <standalone flang build>/unittests/<respective unit test folder>
250 ## For in tree builds
251 If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`On` by default), it is possible to
254 To run all of the flang unit tests use the `check-flang-unit` target:
256 ninja check-flang-unit
258 To run all of the flang regression tests use the `check-flang` target:
263 # How to Generate Documentation
265 ## Generate FIR Documentation
266 If flang was built with `-DLINK_WITH_FIR=On` (`On` by default), it is possible to
267 generate FIR language documentation by running `ninja flang-doc`. This will
268 create `docs/Dialect/FIRLangRef.md` in flang build directory.
270 ## Generate Doxygen-based Documentation
271 To generate doxygen-style documentation from source code
272 - Pass `-DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON` to the cmake command.
275 cd ~/llvm-project/build
276 cmake -DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON ../llvm
280 It will generate html in
283 <build-dir>/tools/flang/docs/doxygen/html # for flang docs
285 ## Generate Sphinx-based Documentation
286 <!TODO: Add webpage once we have a website.
288 Flang documentation should preferably be written in `markdown(.md)` syntax (they can be in `reStructuredText(.rst)` format as well but markdown is recommended in first place), it
289 is mostly meant to be processed by the Sphinx documentation generation
290 system to create HTML pages which would be hosted on the webpage of flang and
291 updated periodically.
293 If you would like to generate and view the HTML locally:
294 - Install [Sphinx](http://sphinx-doc.org/), including the [sphinx-markdown-tables](https://pypi.org/project/sphinx-markdown-tables/) extension.
295 - Pass `-DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF` to the cmake command.
298 cd ~/llvm-project/build
299 cmake -DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF ../llvm
300 ninja docs-flang-html
303 It will generate html in
306 $BROWSER <build-dir>/tools/flang/docs/html/