[flang-commits] [flang] [flang][runtime] Build ISO_FORTRAN_ENV to export kind arrays as linkable symbols (PR #95388)

Michael Kruse via flang-commits flang-commits at lists.llvm.org
Tue Jul 16 07:03:14 PDT 2024


================
@@ -58,18 +76,41 @@ if (NOT CMAKE_CROSSCOMPILING)
       endif()
     endif()
 
+
+    # Some modules have an implementation part that needs to be added to the
+    # FortranRuntime library.
+    set(compile_with "-fsyntax-only")
+    set(object_output "")
+    set(include_in_link FALSE)
+    if(${filename} IN_LIST MODULES_WITH_IMPLEMENTATION)
+      set(object_output "${CMAKE_CURRENT_BINARY_DIR}/${filename}${CMAKE_CXX_OUTPUT_EXTENSION}")
+      set(compile_with -c -o ${object_output})
+      set(include_in_link TRUE)
+    endif()
+
     set(base ${FLANG_INTRINSIC_MODULES_DIR}/${filename})
     # TODO: We may need to flag this with conditional, in case Flang is built w/o OpenMP support
-    add_custom_command(OUTPUT ${base}.mod
+    add_custom_command(OUTPUT ${base}.mod ${object_output}
       COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
-      COMMAND flang-new ${opts} -cpp -fsyntax-only -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
+      COMMAND flang-new ${opts} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
         ${FLANG_SOURCE_DIR}/module/${filename}.f90
       DEPENDS flang-new ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${FLANG_SOURCE_DIR}/module/__fortran_builtins.f90 ${depends}
     )
     list(APPEND MODULE_FILES ${base}.mod)
     install(FILES ${base}.mod DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/flang")
+
+    # If a module has been compiled into an object file, add the file to
+    # the link line for the FortranRuntime library.
+    if(include_in_link)
+      set(module_objects ${module_} ${object_output})
+    endif()
   endforeach()
 
+  # Set a CACHE variable that is visible to the CMakeLists.txt in runtime/, so that
+  # the compiled Fortran modules can be added to the link line of the FortranRuntime
+  # library.
+  set(FORTRAN_MODULE_OBJECTS ${module_objects} CACHE INTERNAL "")
----------------
Meinersbur wrote:

```suggestion
  set(FORTRAN_MODULE_OBJECTS ${module_objects} CACHE INTERNAL "" FORCE)
```
Without the [`FORCE`](https://cmake.org/cmake/help/latest/command/set.html#set-cache-entry) flag, the cached value will only be overwritten if it was not previously listed in `CMakeCache.txt`. So if a patch ever adds another module_object, all previous CMake build directories must be deleted.

A better alternative is [`set_property(GLOBAL PROPERTY ...)`](https://cmake.org/cmake/help/latest/command/set_property.html). `set(... CACHE)` does not use any caching and works as long as `set_property` and `get_property` are evaluated in the expected order (this file before `runtime/CMakeLists.txt`).


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


More information about the flang-commits mailing list