Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions flang-rt/lib/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,21 @@ if (TARGET FortranFloat128MathILib)
endif ()


# When a target depends on an object library, CMake seems to try to only build
# the object files that the target actual needs. Before having analyzed the module
# dependencies, CMake assumes there are no module-dependencies.
# To ensure that the module files are built, insert a custom target that is
# opaque to CMake so the library has to be built regardless.
# Dependees on module files must add a target-level dependency on the barrier,
# and file-level dependencies on source files via target_link_libraries
# on the OBJECT libray. The target-level dependency ensures that the module
# files are built when they do not exist yet, the file-level dependency ensures
# that dependees are rebuilt when the module file changes.
function (add_module_barrier barriername objlib)
add_custom_target(${barriername})
add_dependencies(${barriername} ${objlib})
endfunction ()

# Build module files if requested.
# The object files written by Flang for these are unused. In the future parts
# of flang-rt may itself be implemented in Fortran in which case these Fortran
Expand All @@ -269,6 +284,7 @@ if (FLANG_RT_FORTRAN_MODULES)
${module_sources}
)
flang_module_target(flang-rt-mod PUBLIC)
add_module_barrier(flang-rt-mod.barrier flang-rt-mod)

if (RUNTIMES_FORTRAN_MODULES)
set(_mod_install_dest "${RUNTIMES_INSTALL_RESOURCE_MOD_PATH}/..")
Expand Down
60 changes: 36 additions & 24 deletions runtimes/cmake/config-Fortran.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
# not needed or is insufficient, e.g. if intrinsic modules are missing and
# cannot be compiled on-the-fly.
#
# RUNTIMES_FORTRAN_BUILD_DEPS - If RUNTIMES_ENABLE_FORTRAN is true, this is a
# list of dependencies that must be built before any Fortran source can be
# compiled. Contains the build targets for intrinsic modules, if necessary.
# Otherweise, it is empty.
#
# RUNTIMES_FORTRAN_MODULES - Whether to build Flang modules and emit them
# into Flang's search path. This is a CMake CACHE option defined in
# config-Fortran.cmake and default to ON iff the Fortran compiler is detected
Expand All @@ -23,6 +18,17 @@
# RUNTIMES_INSTALL_RESOURCE_MOD_PATH - Where to install intrinsic module files
# in the install prefix. Relative to CMAKE_INSTALL_PREFIX. Only used when
# RUNTIMES_FORTRAN_MODULES is ON.
#
#
# The following targets are defined:
#
# fortran-compile-options - sets the compile options necessary to compile
# modules. Primarily sets up the intrinsic module search paths if necessary.
# Gets applied to flang-rt as well.
#
# fortran-compile-depends - link to this INTERFACE library to ensure Fortran
# sources can be compiled. Ensures that the builtin modules from
# flang-rt are built before.


# Check whether the Fortran compiler already has access to builtin modules. Sets
Expand Down Expand Up @@ -74,8 +80,11 @@ endfunction ()

set(RUNTIMES_ENABLE_FORTRAN OFF)


set(RUNTIMES_FORTRAN_BUILD_DEPS "")
# Targets compiling Fortran sources can depend on this target to ensure toolchain
# prerequisites (runtime library and intrinsic modules) are built before.
add_library(fortran-compile-options INTERFACE)
add_library(fortran-compile-depends INTERFACE)
target_link_libraries(fortran-compile-depends INTERFACE fortran-compile-options)

if (CMAKE_Fortran_COMPILER)
# Workarounds for older versions of CMake not recognizing FLang. Hence, we
Expand Down Expand Up @@ -148,11 +157,19 @@ if (CMAKE_Fortran_COMPILER)
enable_language(Fortran)
include(CheckFortranSourceCompiles)

if (CMAKE_Fortran_COMPILER_ID MATCHES "LLVM")
target_compile_options(fortran-compile-options INTERFACE
# Flang bug workaround: Reformatting of cooked token buffer causes
# identifier to be split between lines

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this bug is being tracked? Just curious.

@Meinersbur Meinersbur May 21, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created #139297 once as a special case of "flang cannot process its own preprocessor output". The output without -fno-reformat looks so broken that it might just not be intended to work. Breaking lines within identifiers is just the first issue I encountered. Or maybe & is supposed to concatenate them back together in fixed form mode? I do not know. In any case, #139297 was the more pressing issue and creating another issue that I don't even know enough about could have diverted from what matters.

"$<$<COMPILE_LANGUAGE:Fortran>:SHELL:-Xflang;SHELL:-fno-reformat>"
)
endif ()

if (CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang" AND "flang-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
# In a bootstrapping build (or any runtimes-build that includes flang-rt),
# the intrinsic modules are not built yet. Targets can depend on
# flang-rt-mod to ensure that flang-rt's modules are built first.
list(APPEND RUNTIMES_FORTRAN_BUILD_DEPS flang-rt-mod)
target_link_libraries(fortran-compile-depends INTERFACE flang-rt-mod flang-rt-mod.barrier)
set(RUNTIMES_ENABLE_FORTRAN ON)
message(STATUS "Fortran support enabled using just-built Flang-RT builtin modules")
else ()
Expand Down Expand Up @@ -201,6 +218,11 @@ if (RUNTIMES_FORTRAN_MODULES)
# The INSTALL'ed directory must exist, even if empty, or `ninja install` will
# fail with an error.
file(MAKE_DIRECTORY "${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}")

# Let all Fortran sources find the public module files
target_compile_options(fortran-compile-options INTERFACE
"$<$<COMPILE_LANGUAGE:Fortran>:-fintrinsic-modules-path=${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}>"
)
else ()
# If Flang modules are disabled (e.g. because the compiler is not Flang), avoid the risk of Flang accidentally picking them up.
extend_path(RUNTIMES_OUTPUT_RESOURCE_MOD_DIR "${CMAKE_CURRENT_BINARY_DIR}" "finclude-${CMAKE_Fortran_COMPILER_ID}")
Expand Down Expand Up @@ -233,10 +255,12 @@ function (flang_module_target tgtname)
${ARGN}
)

# Let all modules find the public module files
target_compile_options(${tgtname} PRIVATE
"$<$<COMPILE_LANGUAGE:Fortran>:-fintrinsic-modules-path=${RUNTIMES_OUTPUT_RESOURCE_MOD_DIR}>"
)
if (tgtname STREQUAL "flang-rt-mod")
# Avoid circular dependency on flang-rt itself
target_link_libraries(${tgtname} PRIVATE fortran-compile-options)
else ()
target_link_libraries(${tgtname} PRIVATE fortran-compile-depends)
endif ()

# Make CMake not ignore "use, intrinsic ::"-dependencies. Unfortunately,
# it is not universally handled by CMake s.t. we currently must not use
Expand All @@ -250,18 +274,6 @@ function (flang_module_target tgtname)
Fortran_BUILDING_INSTRINSIC_MODULES ON
)

if (RUNTIMES_FORTRAN_BUILD_DEPS AND NOT tgtname IN_LIST RUNTIMES_FORTRAN_BUILD_DEPS)
target_link_libraries(${tgtname} PRIVATE ${RUNTIMES_FORTRAN_BUILD_DEPS})
endif ()

if (CMAKE_Fortran_COMPILER_ID MATCHES "LLVM")
target_compile_options(${tgtname} PRIVATE
# Flang bug workaround: Reformating of cooked token buffer causes
# identifier to be split between lines
"$<$<COMPILE_LANGUAGE:Fortran>:SHELL:-Xflang;SHELL:-fno-reformat>"
)
endif ()

if (ARG_PUBLIC)
set_target_properties(${tgtname}
PROPERTIES
Expand Down
Loading