@@ -23,6 +23,40 @@ set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
2323set (FLANG_RT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR} " )
2424set (FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /../flang" )
2525
26+ # CMake 3.24 is the first version of CMake that directly recognizes Flang.
27+ # LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang.
28+ if (CMAKE_VERSION VERSION_LESS "3.24" )
29+ cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM)
30+ if (_Fortran_COMPILER_STEM STREQUAL "flang-new" OR _Fortran_COMPILER_STEM STREQUAL "flang" )
31+ include (CMakeForceCompiler)
32+ CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER} " "LLVMFlang" )
33+
34+ set (CMAKE_Fortran_COMPILER_ID "LLVMFlang" )
35+ set (CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR} .${LLVM_VERSION_MINOR} " )
36+
37+ set (CMAKE_Fortran_SUBMODULE_SEP "-" )
38+ set (CMAKE_Fortran_SUBMODULE_EXT ".mod" )
39+
40+ set (CMAKE_Fortran_PREPROCESS_SOURCE
41+ "<CMAKE_Fortran_COMPILER> -cpp <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>" )
42+
43+ set (CMAKE_Fortran_FORMAT_FIXED_FLAG "-ffixed-form" )
44+ set (CMAKE_Fortran_FORMAT_FREE_FLAG "-ffree-form" )
45+
46+ set (CMAKE_Fortran_MODDIR_FLAG "-module-dir" )
47+
48+ set (CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON "-cpp" )
49+ set (CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF "-nocpp" )
50+ set (CMAKE_Fortran_POSTPROCESS_FLAG "-ffixed-line-length-72" )
51+
52+ set (CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=" )
53+
54+ set (CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl," )
55+ set (CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP "," )
56+ endif ()
57+ endif ()
58+ enable_language (Fortran)
59+
2660
2761list (APPEND CMAKE_MODULE_PATH
2862 "${FLANG_RT_SOURCE_DIR} /cmake/modules"
@@ -31,24 +65,69 @@ list(APPEND CMAKE_MODULE_PATH
3165include (AddFlangRT)
3266include (GetToolchainDirs)
3367include (FlangCommon)
34- include (FlangRTIntrospection)
3568include (HandleCompilerRT)
3669include (ExtendPath)
37- include (CheckFortranSourceCompiles)
38- include (CMakePushCheckState)
3970
4071
4172############################
4273# Build Mode Introspection #
4374############################
4475
76+ # Determine whether we are in the runtimes/runtimes-bins directory of a
77+ # bootstrap build.
78+ set (LLVM_TREE_AVAILABLE OFF )
79+ if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION )
80+ set (LLVM_TREE_AVAILABLE ON )
81+ endif ()
82+
4583# Path to LLVM development tools (FileCheck, llvm-lit, not, ...)
4684set (LLVM_TOOLS_DIR "${LLVM_BINARY_DIR} /bin" )
4785
48- # Fortran compiler not optional for building Flang-RT
49- enable_language (Fortran)
50-
51- flang_module_fortran_enable()
86+ # Determine build and install paths.
87+ # The build path is absolute, but the install dir is relative, CMake's install
88+ # command has to apply CMAKE_INSTALL_PREFIX itself.
89+ get_toolchain_library_subdir(toolchain_lib_subdir)
90+ if (LLVM_TREE_AVAILABLE)
91+ # In a bootstrap build emit the libraries into a default search path in the
92+ # build directory of the just-built compiler. This allows using the
93+ # just-built compiler without specifying paths to runtime libraries.
94+ #
95+ # Despite Clang in the name, get_clang_resource_dir does not depend on Clang
96+ # being added to the build. Flang uses the same resource dir as clang.
97+ include (GetClangResourceDir)
98+ get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR} /.." )
99+ get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT)
100+
101+ extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR} " "${toolchain_lib_subdir} " )
102+ else ()
103+ # In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be
104+ # read-only and/or shared by multiple runtimes with different build
105+ # configurations (e.g. Debug/Release). Use the runtime's own lib dir like any
106+ # non-toolchain library.
107+ # For the install prefix, still use the resource dir assuming that Flang will
108+ # be installed there using the same prefix. This is to not have a difference
109+ # between bootstrap and standalone runtimes builds.
110+ set (FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR} " )
111+ set (FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT "lib${LLVM_LIBDIR_SUFFIX} /clang/${LLVM_VERSION_MAJOR} " )
112+
113+ extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR} " "lib${LLVM_LIBDIR_SUFFIX} " )
114+ endif ()
115+ set (FLANG_RT_INSTALL_RESOURCE_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH_DEFAULT} "
116+ CACHE PATH "Path to install runtime libraries to (default: clang resource dir)" )
117+ extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH} " "${toolchain_lib_subdir} " )
118+ cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
119+ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
120+ # FIXME: For the libflang_rt.so, the toolchain resource lib dir is not a good
121+ # destination because it is not a ld.so default search path.
122+ # The machine where the executable is eventually executed may not be the
123+ # machine where the Flang compiler and its resource dir is installed, so
124+ # setting RPath by the driver is not an solution. It should belong into
125+ # /usr/lib/<triple>/libflang_rt.so, like e.g. libgcc_s.so.
126+ # But the linker as invoked by the Flang driver also requires
127+ # libflang_rt.so to be found when linking and the resource lib dir is
128+ # the only reliable location.
129+ cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
130+ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
52131
53132
54133#################
@@ -155,10 +234,6 @@ check_cxx_source_compiles(
155234 "
156235 HAVE_DECL_STRERROR_S)
157236
158- # Look for support of REAL(16), if not already defined via command
159- # line via -DFORTRAN_SUPPORTS_REAL16=YES/NO
160- check_fortran_quadmath_support()
161-
162237# Search for clang_rt.builtins library. Need in addition to msvcrt.
163238if (WIN32 )
164239 find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY)
0 commit comments