1 set(LLVM_LINK_COMPONENTS
7 # Define the list of Fortran module files that need to be compiled
8 # to produce an object file for inclusion into the FortranRuntime
10 set(MODULES_WITH_IMPLEMENTATION
11 "iso_fortran_env_impl"
14 # Define the list of Fortran module files for which it is
15 # sufficient to generate the module file via -fsyntax-only.
16 set(MODULES_WITHOUT_IMPLEMENTATION
18 "__fortran_ieee_exceptions"
32 set(MODULES ${MODULES_WITH_IMPLEMENTATION} ${MODULES_WITHOUT_IMPLEMENTATION})
34 # Check if 128-bit float computations can be done via long double.
35 check_cxx_source_compiles(
37 #if LDBL_MANT_DIG != 113
38 #error LDBL_MANT_DIG != 113
40 int main() { return 0; }
42 HAVE_LDBL_MANT_DIG_113)
44 # Figure out whether we can support REAL(KIND=16)
45 if (FLANG_RUNTIME_F128_MATH_LIB)
46 set(FLANG_SUPPORT_R16 "1")
47 elseif (HAVE_LDBL_MANT_DIG_113)
48 set(FLANG_SUPPORT_R16 "1")
50 set(FLANG_SUPPORT_R16 "0")
53 # Init variable to hold extra object files coming from the Fortran modules;
54 # these module files will be contributed from the CMakeLists in flang/tools/f18.
55 set(module_objects "")
57 # Create module files directly from the top-level module source directory.
58 # If CMAKE_CROSSCOMPILING, then the newly built flang executable was
59 # cross compiled, and thus can't be executed on the build system and thus
60 # can't be used for generating module files.
61 if (NOT CMAKE_CROSSCOMPILING)
62 foreach(filename ${MODULES})
65 if(${filename} STREQUAL "__fortran_builtins" OR
66 ${filename} STREQUAL "__ppc_types")
67 elseif(${filename} STREQUAL "__ppc_intrinsics" OR
68 ${filename} STREQUAL "mma")
69 set(depends ${FLANG_INTRINSIC_MODULES_DIR}/__ppc_types.mod)
70 elseif(${filename} STREQUAL "cudadevice")
72 set(depends ${FLANG_INTRINSIC_MODULES_DIR}/__cuda_builtins.mod)
74 set(depends ${FLANG_INTRINSIC_MODULES_DIR}/__fortran_builtins.mod)
75 if(${filename} STREQUAL "iso_fortran_env")
76 set(depends ${depends} ${FLANG_INTRINSIC_MODULES_DIR}/iso_fortran_env_impl.mod)
78 if(${filename} STREQUAL "ieee_arithmetic" OR
79 ${filename} STREQUAL "ieee_exceptions")
80 set(depends ${depends} ${FLANG_INTRINSIC_MODULES_DIR}/__fortran_ieee_exceptions.mod)
83 if(NOT ${filename} STREQUAL "__fortran_type_info" AND NOT ${filename} STREQUAL "__fortran_builtins")
84 set(depends ${depends} ${FLANG_INTRINSIC_MODULES_DIR}/__fortran_type_info.mod)
87 # The module contains PPC vector types that needs the PPC target.
88 if(${filename} STREQUAL "__ppc_intrinsics" OR
89 ${filename} STREQUAL "mma")
90 if (PowerPC IN_LIST LLVM_TARGETS_TO_BUILD)
91 set(opts "--target=ppc64le")
93 # Do not compile PPC module if the target is not available.
99 if (FLANG_SUPPORT_R16)
100 set(decls "-DFLANG_SUPPORT_R16")
103 # Some modules have an implementation part that needs to be added to the
104 # FortranRuntime library.
105 set(compile_with "-fsyntax-only")
106 set(object_output "")
107 set(include_in_link FALSE)
108 if(${filename} IN_LIST MODULES_WITH_IMPLEMENTATION)
109 set(object_output "${CMAKE_CURRENT_BINARY_DIR}/${filename}${CMAKE_CXX_OUTPUT_EXTENSION}")
110 set(compile_with -c -o ${object_output})
111 set(include_in_link TRUE)
114 set(base ${FLANG_INTRINSIC_MODULES_DIR}/${filename})
115 # TODO: We may need to flag this with conditional, in case Flang is built w/o OpenMP support
116 add_custom_command(OUTPUT ${base}.mod ${object_output}
117 COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
118 COMMAND flang ${opts} ${decls} -cpp ${compile_with} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
119 ${FLANG_SOURCE_DIR}/module/${filename}.f90
120 DEPENDS flang ${FLANG_SOURCE_DIR}/module/${filename}.f90 ${FLANG_SOURCE_DIR}/module/__fortran_builtins.f90 ${depends}
122 list(APPEND MODULE_FILES ${base}.mod)
123 install(FILES ${base}.mod DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/flang")
125 # If a module has been compiled into an object file, add the file to
126 # the link line for the FortranRuntime library.
128 list(APPEND module_objects ${object_output})
132 # Set a CACHE variable that is visible to the CMakeLists.txt in runtime/, so that
133 # the compiled Fortran modules can be added to the link line of the FortranRuntime
135 set(FORTRAN_MODULE_OBJECTS ${module_objects} CACHE INTERNAL "" FORCE)
137 # Special case for omp_lib.mod, because its source comes from openmp/runtime/src/include.
138 # It also produces two module files: omp_lib.mod and omp_lib_kinds.mod. Compile these
139 # files only if OpenMP support has been configured.
140 if (LLVM_TOOL_OPENMP_BUILD)
141 message(STATUS "OpenMP runtime support enabled via LLVM_ENABLED_PROJECTS, building omp_lib.mod")
142 set(base ${FLANG_INTRINSIC_MODULES_DIR}/omp_lib)
143 add_custom_command(OUTPUT ${base}.mod ${base}_kinds.mod
144 COMMAND ${CMAKE_COMMAND} -E make_directory ${FLANG_INTRINSIC_MODULES_DIR}
145 COMMAND flang -cpp -fsyntax-only ${opts} -module-dir ${FLANG_INTRINSIC_MODULES_DIR}
146 ${CMAKE_BINARY_DIR}/projects/openmp/runtime/src/omp_lib.F90
147 DEPENDS flang ${FLANG_INTRINSIC_MODULES_DIR}/iso_c_binding.mod ${CMAKE_BINARY_DIR}/projects/openmp/runtime/src/omp_lib.F90 ${depends}
149 add_custom_command(OUTPUT ${base}.f18.mod
151 COMMAND ${CMAKE_COMMAND} -E copy ${base}.mod ${base}.f18.mod)
152 add_custom_command(OUTPUT ${base}_kinds.f18.mod
154 COMMAND ${CMAKE_COMMAND} -E copy ${base}_kinds.mod ${base}_kinds.f18.mod)
155 list(APPEND MODULE_FILES ${base}.mod ${base}.f18.mod ${base}_kinds.mod ${base}_kinds.f18.mod)
156 install(FILES ${base}.mod ${base}.f18.mod ${base}_kinds.mod ${base}_kinds.f18.mod DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/flang")
158 message(STATUS "Not building omp_lib.mod, no OpenMP runtime in LLVM_ENABLED_PROJECTS")
162 add_custom_target(module_files ALL DEPENDS ${MODULE_FILES})
163 set_target_properties(module_files PROPERTIES FOLDER "Flang/Resources")
165 # TODO Move this to a more suitable location
166 # Copy the generated omp_lib.h header file, if OpenMP support has been configured.
167 if (LLVM_TOOL_OPENMP_BUILD)
168 message(STATUS "OpenMP runtime support enabled via LLVM_ENABLED_PROJECTS, building omp_lib.h")
169 file(COPY ${CMAKE_BINARY_DIR}/projects/openmp/runtime/src/omp_lib.h DESTINATION "${CMAKE_BINARY_DIR}/include/flang/OpenMP/" FILE_PERMISSIONS OWNER_READ OWNER_WRITE)
170 install(FILES ${CMAKE_BINARY_DIR}/include/flang/OpenMP/omp_lib.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/flang/OpenMP")
172 message(STATUS "Not copying omp_lib.h, no OpenMP runtime in LLVM_ENABLED_PROJECTS")