[llvm] [flang-rt][build] Add support for building flang-rt as a standalone project (PR #168321)

liao jun via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 16 22:26:54 PST 2025


https://github.com/zjlcd created https://github.com/llvm/llvm-project/pull/168321

standalone build llvm flang-rt  need some cmake environment, This patch improves the CMake environment required for standalone build. and a  typical installation script is described as follows:

build-flangrt.sh
```bash
#!/usr/bin/bash
work_dir=`pwd`
install_dir=$work_dir/install
ROOTDIR=$work_dir/llvm-project

cd $work_dir
cd llvm-project/flang-rt
rm -rf build
mkdir build
cd build
cmake -G "Unix Makefiles" \
      -S $ROOTDIR/runtimes \
      -DCMAKE_CXX_STANDARD=17 \
      -DLLVM_RUNTIMES_BUILD=yes \
      -DFLANG_RUNTIME_F128_MATH_LIB="libquadmath" \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_TARGET_TRIPLE="x86_64-unknown-linux-gnu" \
      -DLLVM_BINARY_DIR=$ROOTDIR/build \
      -DCMAKE_INSTALL_PREFIX=$install_dir \
      -DCMAKE_C_COMPILER=/usr/bin/gcc \
      -DCMAKE_CXX_COMPILER=/usr/bin/g++ \
      -DCMAKE_Fortran_COMPILER=$install_dir/bin/flang \
      -DCMAKE_Fortran_COMPILER_WORKS=yes \
      -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON \
      ..

make -j16
make install
```

>From 56161a013c013d75212a54d9d8823419d5080c6b Mon Sep 17 00:00:00 2001
From: liao jun <liaojun at ultrarisc.com>
Date: Mon, 17 Nov 2025 14:12:28 +0800
Subject: [PATCH] [flang-rt] Add support for building flang-rt as a standalone
 project

---
 flang-rt/.gitignore     | 20 ++++++++++++++++++++
 flang-rt/CMakeLists.txt | 13 ++++++++++---
 2 files changed, 30 insertions(+), 3 deletions(-)
 create mode 100644 flang-rt/.gitignore

diff --git a/flang-rt/.gitignore b/flang-rt/.gitignore
new file mode 100644
index 0000000000000..508e70c1e1eaf
--- /dev/null
+++ b/flang-rt/.gitignore
@@ -0,0 +1,20 @@
+Debug
+Release
+MinSizeRel
+build
+root
+tags
+TAGS
+.nfs*
+*.sw?
+*~
+*#
+CMakeCache.txt
+*/CMakeFiles/*
+*/*/CMakeFiles/*
+*/Makefile
+*/*/Makefile
+cmake_install.cmake
+formatted
+.DS_Store 
+.vs_code 
diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 50b8e834776fb..30d17715252b9 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -10,7 +10,7 @@
 # included using the LLVM_ENABLE_RUNTIMES mechanism.
 #
 #===------------------------------------------------------------------------===#
-
+cmake_minimum_required(VERSION 3.20.0)
 if (NOT LLVM_RUNTIMES_BUILD)
   message(FATAL_ERROR "Use this CMakeLists.txt from LLVM's runtimes build system.
       Example:
@@ -54,6 +54,8 @@ if (CMAKE_VERSION VERSION_LESS "3.24")
     set(CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl,")
     set(CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP ",")
   endif ()
+else ()
+  set(CMAKE_Fortran_COMPILER_FORCED ON)
 endif ()
 enable_language(Fortran)
 
@@ -61,6 +63,7 @@ enable_language(Fortran)
 list(APPEND CMAKE_MODULE_PATH
     "${FLANG_RT_SOURCE_DIR}/cmake/modules"
     "${FLANG_SOURCE_DIR}/cmake/modules"
+    "${FLANG_SOURCE_DIR}/../cmake/Modules"
   )
 include(AddFlangRT)
 include(GetToolchainDirs)
@@ -142,12 +145,14 @@ option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and
 # Provide an interface to link against the LLVM libc/libc++ projects directly.
 set(FLANG_RT_SUPPORTED_PROVIDERS system llvm)
 set(FLANG_RT_LIBC_PROVIDER "system" CACHE STRING "Specify C library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
-if (NOT "${FLANG_RT_LIBC_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
+list(FIND FLANG_RT_SUPPORTED_PROVIDERS "${FLANG_RT_LIBC_PROVIDER}" libc_index)
+if (libc_index EQUAL -1)
   message(FATAL_ERROR "Unsupported library: '${FLANG_RT_RUNTIME_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
 endif ()
 
 set(FLANG_RT_LIBCXX_PROVIDER "system" CACHE STRING "Specify C++ library to use. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
-if (NOT "${FLANG_RT_LIBCXX_PROVIDER}" IN_LIST FLANG_RT_SUPPORTED_PROVIDERS)
+list(FIND FLANG_RT_SUPPORTED_PROVIDERS "${FLANG_RT_LIBCXX_PROVIDER}" libcxx_index)
+if (libcxx_index EQUAL -1)
   message(FATAL_ERROR "Unsupported library: '${FLANG_RT_LIBCXX_PROVIDER}'. Supported values are ${FLANG_RT_SUPPORTED_PROVIDERS}.")
 endif ()
 
@@ -250,6 +255,8 @@ endif ()
 # Aternatively, we could use
 #   CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU"
 # but some older versions of CMake don't define it for GCC itself.
+include(CheckCXXCompilerFlag)
+include(CheckLinkerFlag)
 check_cxx_compiler_flag("-Wp,-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
 
 # Check whether -fno-lto is supported.



More information about the llvm-commits mailing list