How to build flang on Windows on Arm

 

In order to build flang on a Windows on Arm machine, you will first need to setup your machine for LLVM development as described here: How to setup Windows 10 on Arm for llvm development

You will have to add flang, clang and mlir to the list of enabled projects.

You will also need to tell clang where to find the builtins, otherwise the linker will complain that it cannot find symbols such as __udivdi3 or others. Adding the path to the builtin library as described in the https://clang.llvm.org/docs/UsersManual.html#finding-clang-runtime-libraries doesn’t seem to be working at the moment (bug report pending). Luckily, we can work around this issue by adding the builtin library directly on every link command (-DCMAKE_*_LINKER_FLAGS=path/to/clang_rt.builtins-aarch64.lib; if you’re lucky you’ll only need CMAKE_EXE_LINKER_FLAGS, but depending on the compiler version you’re using and your other build flags, you might also need to set CMAKE_SHARED_LINKER_FLAGS or CMAKE_STATIC_LINKER_FLAGS).

You should end up with a script that looks something like this:

REM You need to modify the paths below: set build_dir=path\to\where\you\want\the\build (must alredy exist) set clang_root=path\to\where\clang\is\installed set clang_version=x.0.y (should match what’s in %clang_root%) REM Some helper variables. REM Setting CMAKE_CL_SHOWINCLUDES_PREFIX to work around PR27226. set cmake_flags=^   -DCMAKE_BUILD_TYPE=Release ^   -DLLVM_ENABLE_ASSERTIONS=ON ^   -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON ^   -DLLVM_BUILD_LLVM_C_DYLIB=ON ^   -DCMAKE_INSTALL_UCRT_LIBRARIES=ON ^   -DCMAKE_CL_SHOWINCLUDES_PREFIX="Note: including file: " ^   -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-unknown-windows-msvc ^   -DLLVM_HOST_TRIPLE=aarch64-windows-msvc ^   -DLLVM_TARGET_ARCH=AArch64 ^   -DCLANG_DEFAULT_LINKER=lld  cd %build_dir% set clang_path=%clang_root%\bin\clang-cl.exe set builtins_path=%clang_root%\lib\clang\%clang_version%\lib\windows set builtins_lib=clang_rt.builtins-aarch64.lib set CC=%clang_path% set CXX=%clang_path% REM We enable clang because it is needed by the flang driver. cmake -GNinja %cmake_flags% ^   -DLLVM_ENABLE_PROJECTS="clang;flang;mlir" ^   -DLLVM_TARGETS_TO_BUILD="AArch64" ^   -DCMAKE_C_FLAGS="-fms-compatibility-version=19.14" ^   -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.14" ^   -DCMAKE_EXE_LINKER_FLAGS="%builtins_path%/%builtins_lib%" ^   ..\llvm-project\llvm || exit /b ninja all || ninja all || ninja all || exit /b

Depending on which revision of flang you’re building, you should now have one or both of f18.exe or flang-new.exe in %build_dir%/bin.

At the time of writing, clang version 13.0.0 is known to work for building flang without code generation support.

However, if you want to build a flang that can generate actual binaries, you need to use custom development branches such as this one. The latter is based on the community’s fir-dev branch, which is currently in the process of being upstreamed, plus some minor changes to get things working on Windows on Arm. You can use the steps described above to build even this version of flang, and the resulting executable should be able to compile a simple ‘hello world’ (to link it with the builtins, pass -Xlinker /path/to/clang_rt.builtins-aarch64.lib to flang, just as you did earlier to compile flang itself).

We are naturally working hard towards getting upstream flang to work on Windows on Arm in the cleanest way possible. Current progress is tracked in Jira.