Skip to content

Commit 76c2d56

Browse files
committed
[flang-rt] Use --as-needed for linking flang-rt libraries.
This change makes sure that there is no unnecessary library dependencies like libc++/libstdc++.
1 parent c337e2d commit 76c2d56

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flang-rt/CMakeLists.txt

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

248+
# Check whether -Wl,--as-needed is supported.
249+
check_linker_flag(C "-Wl,--as-needed" LINKER_SUPPORTS_AS_NEEDED)
250+
if (LINKER_SUPPORTS_AS_NEEDED)
251+
set(LINKER_AS_NEEDED_OPT "-Wl,--as-needed")
252+
endif()
253+
248254
# Different platform may have different name for the POSIX thread library.
249255
# For example, libpthread.a on AIX. Search for it as it is needed when
250256
# building the shared flang_rt.runtime.so.
@@ -295,7 +301,6 @@ elseif (FLANG_RT_GCC_RESOURCE_DIR)
295301
endif ()
296302

297303

298-
299304
#####################
300305
# Build Preparation #
301306
#####################

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ function (add_flangrt_library name)
186186
else ()
187187
set_target_properties(${tgtname} PROPERTIES FOLDER "Flang-RT/Libraries")
188188
endif ()
189+
190+
# flang-rt libraries must not depend on libc++/libstdc++,
191+
# so set the linker language to C to avoid the unnecessary
192+
# library dependence. Note that libc++/libstdc++ may still
193+
# come through CMAKE_CXX_IMPLICIT_LINK_LIBRARIES.
194+
set_target_properties(${tgtname} PROPERTIES LINKER_LANGUAGE C)
195+
# Use --as-needed to avoid unnecessary dependencies.
196+
if (LINKER_AS_NEEDED_OPT)
197+
set_property(TARGET ${tgtname} APPEND_STRING PROPERTY
198+
LINK_FLAGS "${LINKER_AS_NEEDED_OPT}")
199+
endif()
189200
endforeach ()
190201

191202
# Define how to compile and link the library.

0 commit comments

Comments
 (0)