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.
9 Please note that flang is not ready yet for production usage.
13 Read more about flang in the [docs directory](docs).
14 Start with the [compiler overview](docs/Overview.md).
16 To better understand Fortran as a language
17 and the specific grammar accepted by flang,
18 read [Fortran For C Programmers](docs/FortranForCProgrammers.md)
20 flang's specifications of the [Fortran grammar](docs/f2018-grammar.md)
22 the [OpenMP grammar](docs/OpenMP-4.5-grammar.md).
24 Treatment of language extensions is covered
25 in [this document](docs/Extensions.md).
27 To understand the compilers handling of intrinsics,
28 see the [discussion of intrinsics](docs/Intrinsics.md).
30 To understand how a flang program communicates with libraries at runtime,
31 see the discussion of [runtime descriptors](docs/RuntimeDescriptor.md).
33 If you're interested in contributing to the compiler,
34 read the [style guide](docs/C++style.md)
36 also review [how flang uses modern C++ features](docs/C++17.md).
38 If you are interested in writing new documentation, follow
39 [LLVM's Markdown style guide](https://github.com/llvm/llvm-project/blob/main/llvm/docs/MarkdownQuickstartTemplate.md).
42 There are two ways to build flang. The first method is to build it at the same
43 time that you build all of the projects on which it depends. This is called
44 building in tree. The second method is to first do an in tree build to create
45 all of the projects on which flang depends. Then, after creating this base
46 build, only build the flang code itself. This is called building standalone.
47 Building standalone has the advantage of being smaller and faster. Once you
48 create the base build and base install areas, you can create multiple
49 standalone builds using them.
51 Note that instructions for building LLVM can be found at
52 https://llvm.org/docs/GettingStarted.html.
54 All of the examples below use GCC as the C/C++ compilers and ninja as the build
57 ### Building flang in tree
58 Building flang in tree means building flang along with all of the projects on
59 which it depends. These projects include mlir, clang, flang, openmp, and
60 compiler-rt. Note that compiler-rt is only needed to access libraries that
61 support 16 bit floating point numbers. It's not needed to run the automated
62 tests. You can use several different C++ compilers for most of the build,
63 includig GNU and clang. But building compiler-rt requres using the clang
64 compiler built in the initial part of the build.
66 Here's a directory structure that works. Create a root directory for the
67 cloned and built files. Under that root directory, clone the source code
68 into a directory called llvm-project. The build will also
69 create subdirectories under the root directory called build (holds most of
70 the built files), install (holds the installed files, and compiler-rt (holds
71 the result of building compiler-rt).
73 Here's a complete set of commands to clone all of the necessary source and do
76 First, create the root directory and `cd` into it.
83 git clone https://github.com/llvm/llvm-project.git
85 Once the clone is complete, execute the following commands:
92 INSTALLDIR=$ROOTDIR/install
98 -DCMAKE_BUILD_TYPE=Release \
99 -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
100 -DCMAKE_CXX_STANDARD=17 \
101 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
102 -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
103 -DFLANG_ENABLE_WERROR=ON \
104 -DLLVM_ENABLE_ASSERTIONS=ON \
105 -DLLVM_TARGETS_TO_BUILD=host \
107 -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;openmp" \
108 -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
114 By default flang tests that do not specify an explicit `--target` flag use
115 LLVM's default target triple. For these tests, if there is a need to test on a
116 different triple by overriding the default, the following needs to be added to
117 the cmake command above:
118 `-DLLVM_TARGET_TRIPLE_ENV="<some string>" -DFLANG_TEST_TARGET_TRIPLE="<your triple>"`.
120 To run the flang tests on this build, execute the command in the "build"
126 To create the installed files:
130 echo "latest" > $INSTALLDIR/bin/versionrc
133 To build compiler-rt:
139 CC=$INSTALLDIR/bin/clang \
140 CXX=$INSTALLDIR/bin/clang++ \
143 ../llvm-project/compiler-rt \
144 -DCMAKE_BUILD_TYPE=Release \
145 -DCMAKE_INSTALL_PREFIX=$INSTALLDIR \
146 -DCMAKE_CXX_STANDARD=11 \
147 -DCMAKE_C_CFLAGS=-mlong-double-128 \
148 -DCMAKE_CXX_CFLAGS=-mlong-double-128 \
149 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
150 -DCOMPILER_RT_BUILD_ORC=OFF \
151 -DCOMPILER_RT_BUILD_XRAY=OFF \
152 -DCOMPILER_RT_BUILD_MEMPROF=OFF \
153 -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
154 -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
155 -DLLVM_CONFIG_PATH=$INSTALLDIR/bin/llvm-config
161 Note that these instructions specify flang as one of the projects to build in
162 the in tree build. This is not strictly necessary for subsequent standalone
163 builds, but doing so lets you run the flang tests to verify that the source
164 code is in good shape.
165 ### Building flang standalone
166 To do the standalone build, start by building flang in tree as described above.
167 This build is base build for subsequent standalone builds. Start each
168 standalone build the same way by cloning the source for llvm-project:
172 git clone https://github.com/llvm/llvm-project.git
174 Once the clone is complete, execute the following commands:
176 cd llvm-project/flang
183 -DCMAKE_BUILD_TYPE=Release \
184 -DCMAKE_CXX_STANDARD=17 \
185 -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
186 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
187 -DFLANG_ENABLE_WERROR=ON \
188 -DLLVM_TARGETS_TO_BUILD=host \
189 -DLLVM_ENABLE_ASSERTIONS=ON \
190 -DLLVM_BUILD_MAIN_SRC_DIR=$ROOTDIR/build/lib/cmake/llvm \
191 -DLLVM_EXTERNAL_LIT=$ROOTDIR/build/bin/llvm-lit \
193 -DLLVM_DIR=$ROOTDIR/build/lib/cmake/llvm \
194 -DCLANG_DIR=$ROOTDIR/build/lib/cmake/clang \
195 -DMLIR_DIR=$ROOTDIR/build/lib/cmake/mlir \
201 To run the flang tests on this build, execute the command in the "flang/build"
207 ## Supported C++ compilers
209 Flang is written in C++17.
211 The code has been compiled and tested with GCC versions from 7.2.0 to 9.3.0.
213 The code has been compiled and tested with clang version 7.0, 8.0, 9.0 and 10.0
214 using either GNU's libstdc++ or LLVM's libc++.
216 The code has been compiled on AArch64, x86_64 and ppc64le servers with CentOS7,
217 Ubuntu18.04, Rhel, MacOs, Mojave, XCode and Apple Clang version 10.0.1.
219 Note that flang is not supported on 32 bit CPUs.
221 ### Building flang with GCC
224 cmake will search for g++ on your PATH.
225 The g++ version must be one of the supported versions
226 in order to build flang.
228 Or, cmake will use the variable CXX to find the C++ compiler. CXX should include
229 the full path to the compiler or a name that will be found on your PATH, e.g.
230 g++-8.3, assuming g++-8.3 is on your PATH.
237 CXX=/opt/gcc-8.3/bin/g++-8.3 cmake ...
240 ### Building flang with clang
242 To build flang with clang,
243 cmake needs to know how to find clang++
244 and the GCC library and tools that were used to build clang++.
246 CXX should include the full path to clang++
247 or clang++ should be found on your PATH.
252 ### Installation Directory
254 To specify a custom install location,
256 `-DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX>`
258 where `<INSTALL_PREFIX>`
259 is the path where flang should be installed.
263 To create a debug build,
265 `-DCMAKE_BUILD_TYPE=Debug`
266 to the cmake command.
267 Debug builds execute slowly.
269 To create a release build,
271 `-DCMAKE_BUILD_TYPE=Release`
272 to the cmake command.
273 Release builds execute quickly.
277 Flang supports 2 different categories of tests
278 1. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests)
279 2. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests)
281 ## For standalone builds
285 cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src
289 To run individual regression tests llvm-lit needs to know the lit
290 configuration for flang. The parameters in charge of this are:
291 flang_site_config and flang_config. And they can be set as shown below:
293 <path-to-llvm-lit>/llvm-lit \
294 --param flang_site_config=<path-to-flang-build>/test-lit/lit.site.cfg.py \
295 --param flang_config=<path-to-flang-build>/test-lit/lit.cfg.py \
296 <path-to-fortran-test>
302 If flang was built with `-DFLANG_INCLUDE_TESTS=ON` (`ON` by default), it is possible to generate unittests.
303 Note: Unit-tests will be skipped for LLVM install for an standalone build as it does not include googletest related headers and libraries.
305 There are various ways to run unit-tests.
309 1. ninja check-flang-unit
310 2. ninja check-all or ninja check-flang
311 3. <path-to-llvm-lit>/llvm-lit \
313 4. Invoking tests from <standalone flang build>/unittests/<respective unit test folder>
318 ## For in tree builds
319 If flang was built with `-DFLANG_INCLUDE_TESTS=ON` (`ON` by default), it is possible to
322 To run all of the flang unit tests use the `check-flang-unit` target:
324 ninja check-flang-unit
326 To run all of the flang regression tests use the `check-flang` target:
331 # How to Generate Documentation
333 ## Generate FIR Documentation
334 If flang was built with `-DLINK_WITH_FIR=ON` (`ON` by default), it is possible to
335 generate FIR language documentation by running `ninja flang-doc`. This will
336 create `<build-dir>/tools/flang/docs/Dialect/FIRLangRef.md` in flang build directory.
338 ## Generate Doxygen-based Documentation
339 To generate doxygen-style documentation from source code
340 - Pass `-DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON` to the cmake command.
343 cd ~/llvm-project/build
344 cmake -DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON ../llvm
348 It will generate html in
351 <build-dir>/tools/flang/docs/doxygen/html # for flang docs
353 ## Generate Sphinx-based Documentation
354 <!TODO: Add webpage once we have a website.
356 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
357 is mostly meant to be processed by the Sphinx documentation generation
358 system to create HTML pages which would be hosted on the webpage of flang and
359 updated periodically.
361 If you would like to generate and view the HTML locally:
362 - Install [Sphinx](http://sphinx-doc.org/), including the [sphinx-markdown-tables](https://pypi.org/project/sphinx-markdown-tables/) extension.
363 - Pass `-DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF` to the cmake command.
366 cd ~/llvm-project/build
367 cmake -DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF ../llvm
368 ninja docs-flang-html
371 It will generate html in
374 $BROWSER <build-dir>/tools/flang/docs/html/