Skip to content

Commit eeb2733

Browse files
authored
[flang-rt] Use --as-needed for linking flang-rt libraries. (#130856)
This change makes sure that there is no unnecessary library dependencies like libc++/libstdc++.
1 parent fdb4b89 commit eeb2733

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

flang-rt/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG)
244244
# Check whether -fno-lto is supported.
245245
check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG)
246246

247+
# Check whether -Wl,--as-needed is supported.
248+
check_linker_flag(C "LINKER:--as-needed" LINKER_SUPPORTS_AS_NEEDED)
249+
if (LINKER_SUPPORTS_AS_NEEDED)
250+
set(LINKER_AS_NEEDED_OPT "LINKER:--as-needed")
251+
endif()
252+
247253
# Different platform may have different name for the POSIX thread library.
248254
# For example, libpthread.a on AIX. Search for it as it is needed when
249255
# building the shared flang_rt.runtime.so.

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ function (add_flangrt_library name)
145145
if (Threads_FOUND)
146146
target_link_libraries(${name_shared} PUBLIC Threads::Threads)
147147
endif ()
148+
149+
# Special dependencies handling for shared libraries only:
150+
#
151+
# flang-rt libraries must not depend on libc++/libstdc++,
152+
# so set the linker language to C to avoid the unnecessary
153+
# library dependence. Note that libc++/libstdc++ may still
154+
# come through CMAKE_CXX_IMPLICIT_LINK_LIBRARIES.
155+
set_target_properties(${name_shared} PROPERTIES LINKER_LANGUAGE C)
156+
# Use --as-needed to avoid unnecessary dependencies.
157+
if (LINKER_AS_NEEDED_OPT)
158+
target_link_options(${name_shared} BEFORE PRIVATE
159+
"${LINKER_AS_NEEDED_OPT}"
160+
)
161+
endif()
148162
endif ()
149163

150164
if (libtargets)

0 commit comments

Comments
 (0)