blob: 20a078193a2907388709a113115e2f19d37cd33e [file] [log] [blame] [view]
David Truby413467f2020-04-09 21:12:54 +01001# Flang
Steve Scalpone282358f2020-03-27 09:23:32 -07002
David Truby413467f2020-04-09 21:12:54 +01003Flang is a ground-up implementation of a Fortran front end written in modern
4C++. It started off as the f18 project (https://github.com/flang-compiler/f18)
5with an aim to replace the previous flang project
6(https://github.com/flang-compiler/flang) and address its various deficiencies.
7F18 was subsequently accepted into the LLVM project and rechristened as Flang.
Stephane Chauveau63141a02018-05-03 14:54:53 +02008
Steve Scalponec90ce542019-03-19 23:47:18 -07009## Getting Started
Stephane Chauveau63141a02018-05-03 14:54:53 +020010
sameeran joshieaff2002020-08-18 15:05:51 +053011Read more about flang in the [docs directory](docs).
12Start with the [compiler overview](docs/Overview.md).
psteinfeld1b50ccd2019-02-25 09:39:27 -080013
Steve Scalponec90ce542019-03-19 23:47:18 -070014To better understand Fortran as a language
David Truby413467f2020-04-09 21:12:54 +010015and the specific grammar accepted by flang,
sameeran joshieaff2002020-08-18 15:05:51 +053016read [Fortran For C Programmers](docs/FortranForCProgrammers.md)
Steve Scalponec90ce542019-03-19 23:47:18 -070017and
brett kooncef09587c2020-10-10 20:10:53 +000018flang's specifications of the [Fortran grammar](docs/f2018-grammar.md)
Steve Scalponec90ce542019-03-19 23:47:18 -070019and
brett kooncef09587c2020-10-10 20:10:53 +000020the [OpenMP grammar](docs/OpenMP-4.5-grammar.md).
psteinfeld1b50ccd2019-02-25 09:39:27 -080021
Steve Scalponec90ce542019-03-19 23:47:18 -070022Treatment of language extensions is covered
sameeran joshieaff2002020-08-18 15:05:51 +053023in [this document](docs/Extensions.md).
Steve Scalponec90ce542019-03-19 23:47:18 -070024
25To understand the compilers handling of intrinsics,
sameeran joshieaff2002020-08-18 15:05:51 +053026see the [discussion of intrinsics](docs/Intrinsics.md).
Steve Scalponec90ce542019-03-19 23:47:18 -070027
David Truby413467f2020-04-09 21:12:54 +010028To understand how a flang program communicates with libraries at runtime,
sameeran joshieaff2002020-08-18 15:05:51 +053029see the discussion of [runtime descriptors](docs/RuntimeDescriptor.md).
Steve Scalponec90ce542019-03-19 23:47:18 -070030
31If you're interested in contributing to the compiler,
sameeran joshieaff2002020-08-18 15:05:51 +053032read the [style guide](docs/C++style.md)
Steve Scalponec90ce542019-03-19 23:47:18 -070033and
sameeran joshieaff2002020-08-18 15:05:51 +053034also review [how flang uses modern C++ features](docs/C++17.md).
Stephane Chauveau63141a02018-05-03 14:54:53 +020035
Sameeran joshibc9cdfa2020-08-24 19:51:45 +053036If you are interested in writing new documentation, follow
37[markdown style guide from LLVM](https://github.com/llvm/llvm-project/blob/master/llvm/docs/MarkdownQuickstartTemplate.md).
38
David Truby413467f2020-04-09 21:12:54 +010039## Supported C++ compilers
Stephane Chauveau63141a02018-05-03 14:54:53 +020040
David Truby413467f2020-04-09 21:12:54 +010041Flang is written in C++17.
Stephane Chauveau63141a02018-05-03 14:54:53 +020042
Steve Scalpone2a696002018-05-18 13:02:58 -070043The code has been compiled and tested with
David Truby413467f2020-04-09 21:12:54 +010044GCC versions from 7.2.0 to 9.3.0.
Stephane Chauveau63141a02018-05-03 14:54:53 +020045
Steve Scalponec90ce542019-03-19 23:47:18 -070046The code has been compiled and tested with
David Truby413467f2020-04-09 21:12:54 +010047clang version 7.0, 8.0, 9.0 and 10.0
Steve Scalponec90ce542019-03-19 23:47:18 -070048using either GNU's libstdc++ or LLVM's libc++.
49
David Truby413467f2020-04-09 21:12:54 +010050The code has been compiled on
51AArch64, x86\_64 and ppc64le servers
52with CentOS7, Ubuntu18.04, Rhel, MacOs, Mojave, XCode and
53Apple Clang version 10.0.1.
54
55The code does not compile with Windows and a compiler that does not have
56support for C++17.
57
58## Building Flang out of tree
59These instructions are for building Flang separately from LLVM; if you are
60building Flang alongside LLVM then follow the standard LLVM build instructions
61and add flang to `LLVM_ENABLE_PROJECTS` instead, as detailed there.
62
Steve Scalponec90ce542019-03-19 23:47:18 -070063### LLVM dependency
64
Steve Scalponec90ce542019-03-19 23:47:18 -070065The instructions to build LLVM can be found at
David Truby413467f2020-04-09 21:12:54 +010066https://llvm.org/docs/GettingStarted.html. If you are building flang as part
67of LLVM, follow those instructions and add flang to `LLVM_ENABLE_PROJECTS`.
Steve Scalponec90ce542019-03-19 23:47:18 -070068
David Truby413467f2020-04-09 21:12:54 +010069We highly recommend using the same compiler to compile both llvm and flang.
Steve Scalponec90ce542019-03-19 23:47:18 -070070
David Truby413467f2020-04-09 21:12:54 +010071The flang CMakeList.txt file uses
Steve Scalpone282358f2020-03-27 09:23:32 -070072the variable `LLVM_DIR` to find the installed LLVM components
73and
74the variable `MLIR_DIR` to find the installed MLIR components.
Steve Scalponec90ce542019-03-19 23:47:18 -070075
David Truby413467f2020-04-09 21:12:54 +010076To get the correct LLVM and MLIR libraries included in your flang build,
Steve Scalpone282358f2020-03-27 09:23:32 -070077define LLVM_DIR and MLIR_DIR on the cmake command line.
Steve Scalponec90ce542019-03-19 23:47:18 -070078```
Steve Scalpone282358f2020-03-27 09:23:32 -070079LLVM=<LLVM_BUILD_DIR>/lib/cmake/llvm \
80MLIR=<LLVM_BUILD_DIR>/lib/cmake/mlir \
81cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ...
Steve Scalponec90ce542019-03-19 23:47:18 -070082```
Steve Scalponea705a4a2020-03-05 07:12:06 -080083where `LLVM_BUILD_DIR` is
84the top-level directory where LLVM was built.
CarolineConcatto0d9dd492020-01-14 16:20:49 +000085
David Truby413467f2020-04-09 21:12:54 +010086### Building flang with GCC
peter klausler1e036b22018-05-07 14:39:36 -070087
Steve Scalpone2a696002018-05-18 13:02:58 -070088By default,
89cmake will search for g++ on your PATH.
Steve Scalponec90ce542019-03-19 23:47:18 -070090The g++ version must be one of the supported versions
David Truby413467f2020-04-09 21:12:54 +010091in order to build flang.
Stephane Chauveau63141a02018-05-03 14:54:53 +020092
David Truby413467f2020-04-09 21:12:54 +010093Or, cmake will use the variable CXX to find the C++ compiler. CXX should include
94the full path to the compiler or a name that will be found on your PATH, e.g.
95g++-8.3, assuming g++-8.3 is on your PATH.
96
Steve Scalpone2a696002018-05-18 13:02:58 -070097```
Steve Scalpone282358f2020-03-27 09:23:32 -070098export CXX=g++-8.3
Steve Scalpone2a696002018-05-18 13:02:58 -070099```
Steve Scalponec90ce542019-03-19 23:47:18 -0700100or
101```
Steve Scalpone282358f2020-03-27 09:23:32 -0700102CXX=/opt/gcc-8.3/bin/g++-8.3 cmake ...
Steve Scalponec90ce542019-03-19 23:47:18 -0700103```
Steve Scalponec90ce542019-03-19 23:47:18 -0700104
David Truby413467f2020-04-09 21:12:54 +0100105### Building flang with clang
Steve Scalponec90ce542019-03-19 23:47:18 -0700106
David Truby413467f2020-04-09 21:12:54 +0100107To build flang with clang,
Steve Scalponec90ce542019-03-19 23:47:18 -0700108cmake needs to know how to find clang++
109and the GCC library and tools that were used to build clang++.
110
Steve Scalponec90ce542019-03-19 23:47:18 -0700111CXX should include the full path to clang++
112or clang++ should be found on your PATH.
113```
114export CXX=clang++
115```
Steve Scalpone2a696002018-05-18 13:02:58 -0700116
117### Installation Directory
118
119To specify a custom install location,
120add
121`-DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX>`
122to the cmake command
123where `<INSTALL_PREFIX>`
David Truby413467f2020-04-09 21:12:54 +0100124is the path where flang should be installed.
Steve Scalpone2a696002018-05-18 13:02:58 -0700125
126### Build Types
127
128To create a debug build,
129add
130`-DCMAKE_BUILD_TYPE=Debug`
131to the cmake command.
132Debug builds execute slowly.
133
134To create a release build,
135add
136`-DCMAKE_BUILD_TYPE=Release`
137to the cmake command.
138Release builds execute quickly.
139
David Truby413467f2020-04-09 21:12:54 +0100140### Build Flang out of tree
Steve Scalpone2a696002018-05-18 13:02:58 -0700141```
David Truby413467f2020-04-09 21:12:54 +0100142cd ~/flang/build
143cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src
Steve Scalpone2a696002018-05-18 13:02:58 -0700144make
145```
Caroline Concatto257b2972020-09-11 10:17:31 +0100146
147### Build The New Flang Driver
148The new Flang driver, `flang-new`, is currently under active development and
149should be considered as an experimental feature. For this reason it is disabled
150by default. This will change once the new driver replaces the _throwaway_
151driver, `flang`.
152
Andrzej Warzynski27da2872020-09-24 09:34:43 +0100153In order to build the new driver, add `-DFLANG_BUILD_NEW_DRIVER=ON` to your
Caroline Concatto257b2972020-09-11 10:17:31 +0100154CMake invocation line. Additionally, when building out-of-tree, use `CLANG_DIR`
155(similarly to `LLVM_DIR` and `MLIR_DIR`) to find the installed Clang
156components.
157
158**Note:** `CLANG_DIR` is only required when building the new Flang driver,
159which currently depends on Clang.
160
sameeran joshi93f602b2020-06-02 22:45:44 +0530161# How to Run Tests
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000162
sameeran joshi93f602b2020-06-02 22:45:44 +0530163Flang supports 2 different categories of tests
1641. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests)
1652. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests)
166
167## For out of tree builds
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000168To run all tests:
169```
David Truby413467f2020-04-09 21:12:54 +0100170cd ~/flang/build
171cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src
Tim Keith69055262020-03-16 10:46:17 -0700172make test check-all
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000173```
174
175To run individual regression tests llvm-lit needs to know the lit
David Truby413467f2020-04-09 21:12:54 +0100176configuration for flang. The parameters in charge of this are:
Richard Barton7e5dab52020-09-07 16:33:55 +0100177flang_site_config and flang_config. And they can be set as shown below:
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000178```
179<path-to-llvm-lit>/llvm-lit \
David Truby413467f2020-04-09 21:12:54 +0100180 --param flang_site_config=<path-to-flang-build>/test-lit/lit.site.cfg.py \
181 --param flang_config=<path-to-flang-build>/test-lit/lit.cfg.py \
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000182 <path-to-fortran-test>
sameeran joshi93f602b2020-06-02 22:45:44 +0530183
184```
185
186Unit tests:
187
188If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`ON` by default), it is possible to generate unittests.
189Note: Unit-tests will be skipped for LLVM install for an out-of-tree build as it does not include googletest related headers and libraries.
190
191There are various ways to run unit-tests.
192
193```
194
1951. make check-flang-unit
1962. make check-all or make check-flang
1973. <path-to-llvm-lit>/llvm-lit \
198 test/Unit
1994. Invoking tests from <out-of-tree flang build>/unittests/<respective unit test folder>
200
201```
202
203
204## For in tree builds
205If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`On` by default), it is possible to
206generate unittests.
207
208To run all of the flang unit tests use the `check-flang-unit` target:
209```
210make check-flang-unit
211```
212To run all of the flang regression tests use the `check-flang` target:
213```
214make check-flang
CarolineConcatto0d9dd492020-01-14 16:20:49 +0000215```
jeanPerier57f536a2020-03-11 21:47:22 -0700216
sameeran joshi807fe052020-04-27 14:52:36 +0530217# How to Generate Documentation
jeanPerier57f536a2020-03-11 21:47:22 -0700218
sameeran joshi807fe052020-04-27 14:52:36 +0530219## Generate FIR Documentation
220If flang was built with `-DLINK_WITH_FIR=On` (`On` by default), it is possible to
jeanPerier57f536a2020-03-11 21:47:22 -0700221generate FIR language documentation by running `make flang-doc`. This will
sameeran joshi807fe052020-04-27 14:52:36 +0530222create `docs/Dialect/FIRLangRef.md` in flang build directory.
223
224## Generate Doxygen-based Documentation
225To generate doxygen-style documentation from source code
226- Pass `-DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON` to the cmake command.
227
228```
229cd ~/llvm-project/build
230cmake -DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON ../llvm
231make doxygen-flang
Richard Barton7e5dab52020-09-07 16:33:55 +0100232```
sameeran joshi807fe052020-04-27 14:52:36 +0530233
234It will generate html in
235
Richard Barton7e5dab52020-09-07 16:33:55 +0100236```
sameeran joshi807fe052020-04-27 14:52:36 +0530237 <build-dir>/tools/flang/docs/doxygen/html # for flang docs
238```
Sameeran joshibc9cdfa2020-08-24 19:51:45 +0530239## Generate Sphinx-based Documentation
240<!TODO: Add webpage once we have a website.
241!>
242Flang 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
243is mostly meant to be processed by the Sphinx documentation generation
244system to create HTML pages which would be hosted on the webpage of flang and
245updated periodically.
246
Richard Barton7e5dab52020-09-07 16:33:55 +0100247If you would like to generate and view the HTML locally:
248- Install [Sphinx](http://sphinx-doc.org/), including the [sphinx-markdown-tables](https://pypi.org/project/sphinx-markdown-tables/) extension.
Sameeran joshibc9cdfa2020-08-24 19:51:45 +0530249- Pass `-DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF` to the cmake command.
250
251```
252cd ~/llvm-project/build
253cmake -DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF ../llvm
254make docs-flang-html
Richard Barton7e5dab52020-09-07 16:33:55 +0100255```
Sameeran joshibc9cdfa2020-08-24 19:51:45 +0530256
257It will generate html in
258
Richard Barton7e5dab52020-09-07 16:33:55 +0100259```
Sameeran joshibc9cdfa2020-08-24 19:51:45 +0530260 $BROWSER <build-dir>/tools/flang/docs/html/
261```