-
Notifications
You must be signed in to change notification settings - Fork 137
Building Flang for Windows
Flang requires Visual Studio 2019 (>=16.0.0) to build, it is not tested with former versions of Visual Studio. Basically clang-cl compiler is used but Visual Studio’s header files, libraries, and some tools are required. Visual Studio Community Edition should work if it's license is appropriate for you. You must install the “Desktop development with C++” component and the “MFC/ATL support” sub-components. This can be done from the command line by passing these arguments to the Visual Studio installer (see below for ARM64 instructions):
$ PATH_TO_INSTALLER.EXE ^
--add Microsoft.VisualStudio.Workload.NativeDesktop ^
--add Microsoft.VisualStudio.Component.VC.ATLMFC ^
--includeRecommended
If you want to build for ARM64 Win32 then some extra arguments are needed. The full set for that case is:
$ PATH_TO_INSTALLER.EXE ^
--add Microsoft.VisualStudio.Workload.NativeDesktop ^
--add Microsoft.VisualStudio.Component.VC.ATLMFC ^
--add Microsoft.VisualStudio.Component.VC.Tools.ARM64 ^
--add Microsoft.VisualStudio.Component.VC.MFC.ARM64 ^
--includeRecommended
You must also have Windows 10 SDK version 10.0.19041 or higher installed. You can do it easily via Visual Studio Installer.
- All of the build commands should be executed via "x64 Native Tools Command Prompt for VS 2019" on x64 and "x86_arm64 Cross Tools Command Prompt for VS 2019" on ARM64.
- Python 3 (>= 3.9)
- Git for Windows
- CMake (3.19.0 or newer)
- Ninja is preferred on Windows, minimum version requirement is 1.11, NMake is also available on Windows by default. You can use it as well. For Windows on ARM you must use NMake, since Ninja is not available for Windows on ARM.
- Flang highly relies on shell scripts, so it is necessary to add sh.exe to Windows path, for example use sh.exe from Git for Windows directory (generally it's located in: C:\Program Files\Git\bin)
- Other dependency is awk which you can download and install as part of GNUWin32 from: https://sourceforge.net/projects/getgnuwin32/files/
After downloading getgnuwin32:
cd getgnuwin32
install c:\gnuwin32
(make sure you add all of these to your PATH environment variable)
There is a build script called build_llvm_project.py which builds llvm with classic flang enabled. It is customizable to accomodate multiple build options. To see all available options just type --help
.
Here is an example of usage:
build_llvm_project.py -e "clang"
Checkout LLVM repository (release_12x or newer branch is advised):
git clone -b release_12x https://github.com/flang-compiler/classic-flang-llvm-project.git
Create build directory:
cd classic-flang-llvm-project && mkdir build && cd build
Generate build files using cmake with extra flags if necessary. These are some variables that we generally use:
-DCMAKE_INSTALL_PREFIX=</path/to/install/dir> # desired installation directory
-DCMAKE_C_COMPILER=<c-compiler> # C compiler to use for build (e.g.: "clang","clang-cl")
-DCMAKE_CXX_COMPILER=<c++-compiler> # C++ compiler to use for build (e.g.: "clang++","clang-cl")
-DCMAKE_TARGETS_TO_BUILD=<target> # Control which targets are enabled (e.g.: X86, AArch64)
-DLLVM_ENABLE_CLASSIC_FLANG=<ON/OFF> # Build support for classic Flang
-DLLVM_ENABLE_PROJECTS="project1;project2" # list of the LLVM subprojects (e.g.:clang, openmp, etc.)
-G<generator> # Generator to use for build (e.g.: Ninja, NMake)
Here is an example for generating build files:
cmake -DCMAKE_INSTALL_PREFIX=path/to/install/dir \
-DCMAKE_C_COMPILER="clang-cl" \
-DCMAKE_CXX_COMPILER="clang-cl" \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_ENABLE_CLASSIC_FLANG=ON \
-DLLVM_ENABLE_PROJECTS="clang" \
-GNinja \
..\llvm
Then build llvm and all enabled subprojects:
cmake --build <path/to/build/dir> --config <Release/Debug> --parallel <number-of-jobs>
Then finally install built projects to desired location:
cmake --build <path/to/build/dir> --config <Release/Debug> --target install
There is a build script build-flang.py which is a thin wrapper around CMake. The script builds libpgmath and Flang.
build-flang.py -d build -p path-to-installed-llvm-dir
The script detects the platform where it runs and picks up a toolchain file based on that. On Windows clang-cl is used to build Flang.
If you are curious about about the available options of build script just type --help
.
Checkout Flang repository:
git clone https://github.com/flang-compiler/flang.git
First you need to build a runtime library called libpgmath. To create build directory:
cd flang/runtime/libpgmath && mkdir build && cd build
Use cmake to generate build files. We can use some of the variables used with LLVM (see above):
cmake -DCMAKE_INSTALL_PREFIX=path/to/install/dir \
-DCMAKE_C_COMPILER="clang-cl" \
-DCMAKE_CXX_COMPILER="clang-cl" \
-GNinja \
..
Then build libpgmath:
cmake --build <path/to/build/dir> --config <Release/Debug> --parallel <number-of-jobs>
And install it to the previously defined directory:
cmake --build <path/to/build/dir> --config <Release/Debug> --target install
After building and installing libpgmath we can build Flang. First we need a build directory:
cd <path/to/flang> && mkdir build && cd build
Generate build files using cmake with extra flags if necessary. These are some variables that we generally use excluding the ones we also use with LLVM (see above):
-DCMAKE_Fortran_COMPILER=<install/dir/bin/flang-executable> # Fortran compiler to use
-DCMAKE_Fortran_COMPILER_ID=Flang # Compiler ID should be set
-DFLANG_INCLUDE_DOCS=ON # Generate build targets for the Flang docs
-DFLANG_LLVM_EXTENSIONS=ON # Use Fortran debug information
-DWITH_WERROR=<ON/OFF> # Treat warnings as errors
NOTE: -DWITH_WERROR option should be switched OFF on Windows until it is fixed for this platform. Here is an example for usage:
cmake -DCMAKE_INSTALL_PREFIX=path/to/install/dir \
-DCMAKE_C_COMPILER="clang-cl" \
-DCMAKE_CXX_COMPILER="clang-cl" \
-DCMAKE_Fortran_COMPILER="<path/to/install>/bin/flang.exe \
-DCMAKE_Fortran_COMPILER_ID=Flang \
-DFLANG_INCLUDE_DOCS=ON \
-DFLANG_LLVM_EXTENSIONS=ON \
-DWITH_WERROR=ON \
-GNinja \
..
Then we build Flang:
cmake --build <path/to/build/dir> --config <Release/Debug> --parallel <number-of-jobs>
And finally install it:
cmake --build <path/to/build/dir> --config <Release/Debug> --target install
To build LLVM from source you basically need to do the same steps as with x64 but make some modifications to your cmake command: First you need to specify your target to build as well as the target triple (LLVM tends to not recognize windows arm64 target triple as default):
-DCMAKE_TARGETS_TO_BUILD=AArch64
-DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-pc-windows-msvc
You also need to specify a different generator (you can build Ninja from source but it does not have a released executable for Windows ARM64 at the moment):
-GNmake Makefiles
Note: NMake does not support parallel jobs so it will ignore -j --jobs and --parallel flags in your CMake command.
After these modifications to your CMake command it should work on Windows ARM64 systems.
To build Flang we use practically the same commands as with x64 but use the modifications mentioned above.
Note: You can use build scripts for building on Windows ARM64 machines natively. Cross-compilation is still work in progress.