Skip to content

Commit 4c4fc46

Browse files
authored
[Flang-RT] Build libflang_rt.so (#121782)
Under non-Windows platforms, also create a dynamic library version of the runtime. Build of either version of the library can be switched on using FLANG_RT_ENABLE_STATIC=ON respectively FLANG_RT_ENABLE_SHARED=ON. Default is to build only the static library, consistent with previous behaviour. This is because the way the flang driver invokes the linker, most linkers choose the dynamic library by default, if available. Building the dynamic library therefore causes flang-built executables to depend on `libflang_rt.so`, unless explicitly told otherwise.
1 parent 43d308d commit 4c4fc46

File tree

9 files changed

+303
-138
lines changed

9 files changed

+303
-138
lines changed

flang-rt/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ endif ()
115115
extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}")
116116
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR)
117117
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH)
118+
# FIXME: For the libflang_rt.so, the toolchain resource lib dir is not a good
119+
# destination because it is not a ld.so default search path.
120+
# The machine where the executable is eventually executed may not be the
121+
# machine where the Flang compiler and its resource dir is installed, so
122+
# setting RPath by the driver is not an solution. It should belong into
123+
# /usr/lib/<triple>/libflang_rt.so, like e.g. libgcc_s.so.
124+
# But the linker as invoked by the Flang driver also requires
125+
# libflang_rt.so to be found when linking and the resource lib dir is
126+
# the only reliable location.
118127
cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR)
119128
cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
120129

@@ -129,6 +138,27 @@ cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH)
129138
option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}")
130139

131140

141+
option(FLANG_RT_ENABLE_STATIC "Build Flang-RT as a static library." ON)
142+
if (WIN32)
143+
# Windows DLL currently not implemented.
144+
set(FLANG_RT_ENABLE_SHARED OFF)
145+
else ()
146+
# TODO: Enable by default to increase test coverage, and which version of the
147+
# library should be the user's choice anyway.
148+
# Currently, the Flang driver adds `-L"libdir" -lflang_rt` as linker
149+
# argument, which leaves the choice which library to use to the linker.
150+
# Since most linkers prefer the shared library, this would constitute a
151+
# breaking change unless the driver is changed.
152+
option(FLANG_RT_ENABLE_SHARED "Build Flang-RT as a shared library." OFF)
153+
endif ()
154+
if (NOT FLANG_RT_ENABLE_STATIC AND NOT FLANG_RT_ENABLE_SHARED)
155+
message(FATAL_ERROR "
156+
Must build at least one type of library
157+
(FLANG_RT_ENABLE_STATIC=ON, FLANG_RT_ENABLE_SHARED=ON, or both)
158+
")
159+
endif ()
160+
161+
132162
set(FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT "" CACHE STRING "Compile Flang-RT with GPU support (CUDA or OpenMP)")
133163
set_property(CACHE FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT PROPERTY STRINGS
134164
""

0 commit comments

Comments
 (0)