[llvm] [runtimes][CMake] Move Fortran support code from flang-rt (PR #171610)

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 02:07:34 PST 2026


================
@@ -0,0 +1,233 @@
+# Check whether the build environment supports building Fortran modules
+# flang-rt and openmp are the only runtimes that contain Fortran modules.
+#
+# Sets:
+#  * RUNTIMES_FLANG_MODULES_ENABLED Whether .mod files can be created
+#  * CMAKE_Fortran_*                CMake Fortran toolchain info for older versions of CMake
+
+
+# Check whether the Fortran compiler already has access to builtin modules. Sets
+# HAVE_FORTRAN_INTRINSIC_MODS when returning.
+#
+# This must be wrapped in a function because
+# cmake_push_check_state/cmake_pop_check_state is insufficient to isolate
+# a compiler introspection environment, see
+# https://gitlab.kitware.com/cmake/cmake/-/issues/27419
+function (check_fortran_builtins_available)
+  if (CMAKE_Fortran_COMPILER_FORCED AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
+    # CMake's check_fortran_source_compiles/try_compile does not take a
+    # user-defined CMAKE_Fortran_PREPROCESS_SOURCE into account. Instead of
+    # test-compiling, ask Flang directly for the builtin module files.
+    # CMAKE_Fortran_PREPROCESS_SOURCE is defined for CMake < 3.24 because it
+    # does not natively recognize Flang (see below). Once we bump the required
+    # CMake version, and because setting CMAKE_Fortran_PREPROCESS_SOURCE has
+    # been deprecated by CMake, this workaround can be removed.
+    if (NOT DEFINED FORTRAN_HAS_ISO_C_BINDING_MOD)
+      message(STATUS "Performing Test ISO_C_BINDING_PATH")
+      execute_process(
+        COMMAND ${CMAKE_Fortran_COMPILER} ${CMAKE_Fortran_FLAGS} "-print-file-name=iso_c_binding.mod"
+        OUTPUT_VARIABLE ISO_C_BINDING_PATH
+        OUTPUT_STRIP_TRAILING_WHITESPACE
+        ERROR_QUIET
+      )
+      set(FORTRAN_HAS_ISO_C_BINDING_MOD "")
+      if (EXISTS "${ISO_C_BINDING_PATH}")
+        message(STATUS "Performing Test ISO_C_BINDING_PATH -- Success")
+        set(FORTRAN_HAS_ISO_C_BINDING_MOD TRUE CACHE INTERNAL "Existence result of ${CMAKE_Fortran_COMPILER} -print-file-name=iso_c_binding.mod")
+      else ()
+        message(STATUS "Performing Test ISO_C_BINDING_PATH -- Failed")
+        set(FORTRAN_HAS_ISO_C_BINDING_MOD FALSE CACHE INTERNAL "Existence result of ${CMAKE_Fortran_COMPILER} -print-file-name=iso_c_binding.mod")
+      endif ()
+    endif ()
+  else ()
+    cmake_push_check_state(RESET)
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
----------------
petrhosek wrote:

Have you checked if `CMAKE_TRY_COMPILE_TARGET_TYPE` is saved and restored by `cmake_push_check_state` and `cmake_pop_check_state`? Looking at https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakePushCheckState.cmake, it doesn't appear to be the case in which case we have to save and restore it manually.

https://github.com/llvm/llvm-project/pull/171610


More information about the llvm-commits mailing list