@@ -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)
@@ -186,17 +200,6 @@ function (add_flangrt_library name)
186200 else ()
187201 set_target_properties (${tgtname} PROPERTIES FOLDER "Flang-RT/Libraries" )
188202 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 ()
200203 endforeach ()
201204
202205 # Define how to compile and link the library.
0 commit comments