Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit ef2d239

Browse files
committed
[libc++] Move the linker script generation step to CMake
Summary: This allows the linker script generation to query CMake properties (specifically the dependencies of libc++.so) instead of having to carry these dependencies around manually in global variables. Notice the removal of the LIBCXX_INTERFACE_LIBRARIES global variable. Reviewers: phosek, EricWF Subscribers: mgorny, christof, jkorous, dexonsmith, libcxx-commits Tags: #libc Differential Revision: https://reviews.llvm.org/D68343 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@374116 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d265021 commit ef2d239

File tree

4 files changed

+54
-92
lines changed

4 files changed

+54
-92
lines changed

CMakeLists.txt

-6
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,6 @@ if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
380380
if (APPLE)
381381
message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets")
382382
endif()
383-
if (NOT PYTHONINTERP_FOUND)
384-
message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT requires python but it was not found.")
385-
endif()
386383
if (NOT LIBCXX_ENABLE_SHARED)
387384
message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.")
388385
endif()
@@ -447,12 +444,9 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
447444
# LIBCXX_COMPILE_FLAGS: Compile only flags.
448445
# LIBCXX_LINK_FLAGS: Linker only flags.
449446
# LIBCXX_LIBRARIES: libraries libc++ is linked to.
450-
# LIBCXX_INTERFACE_LIBRARIES: Libraries that must be linked when using libc++
451-
# These libraries are exposed in the linker script.
452447
set(LIBCXX_COMPILE_FLAGS "")
453448
set(LIBCXX_LINK_FLAGS "")
454449
set(LIBCXX_LIBRARIES "")
455-
set(LIBCXX_INTERFACE_LIBRARIES "")
456450

457451
# Include macros for adding and removing libc++ flags.
458452
include(HandleLibcxxFlags)
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This function defines a linker script in place of the symlink traditionally
2+
# created for shared libraries.
3+
#
4+
# More specifically, this function goes through the PUBLIC and INTERFACE
5+
# library dependencies of <target> and gathers them into a linker script,
6+
# such that those libraries are linked against when the shared library for
7+
# <target> is linked against.
8+
#
9+
# Arguments:
10+
# <target>: A target representing a shared library. A linker script will be
11+
# created in place of that target's TARGET_LINKER_FILE, which is
12+
# the symlink pointing to the actual shared library (usually
13+
# libFoo.so pointing to libFoo.so.1, which itself points to
14+
# libFoo.so.1.0).
15+
16+
function(define_linker_script target)
17+
if (NOT TARGET "${target}")
18+
message(FATAL_ERROR "The provided target '${target}' is not actually a target.")
19+
endif()
20+
21+
get_target_property(target_type "${target}" TYPE)
22+
if (NOT "${target_type}" STREQUAL "SHARED_LIBRARY")
23+
message(FATAL_ERROR "The provided target '${target}' is not a shared library (its type is '${target_type}').")
24+
endif()
25+
26+
set(symlink "$<TARGET_LINKER_FILE:${target}>")
27+
set(soname "$<TARGET_SONAME_FILE_NAME:${target}>")
28+
29+
get_target_property(interface_libs "${target}" INTERFACE_LINK_LIBRARIES)
30+
31+
set(link_libraries)
32+
if (interface_libs)
33+
foreach(lib IN LISTS interface_libs)
34+
if (TARGET "${lib}")
35+
list(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:${lib},OUTPUT_NAME>")
36+
else()
37+
list(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}${lib}")
38+
endif()
39+
endforeach()
40+
endif()
41+
list(JOIN link_libraries " " link_libraries)
42+
43+
set(linker_script "INPUT(${soname} ${link_libraries})")
44+
add_custom_command(TARGET "${target}" POST_BUILD
45+
COMMAND "${CMAKE_COMMAND}" -E remove "${symlink}"
46+
COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "${symlink}"
47+
COMMENT "Generating linker script: '${linker_script}' as file ${symlink}"
48+
VERBATIM
49+
)
50+
endfunction()

src/CMakeLists.txt

+4-28
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,10 @@ if (LIBCXX_ENABLE_SHARED)
177177
if (LIBCXXABI_USE_LLVM_UNWINDER)
178178
if (NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
179179
target_link_libraries(cxx_shared PUBLIC unwind_shared)
180-
list(APPEND LIBCXX_INTERFACE_LIBRARIES unwind_shared) # For the linker script
181180
elseif (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_static OR HAVE_LIBUNWIND))
182181
# libunwind is already included in libc++abi
183182
else()
184-
target_link_libraries(cxx_shared PRIVATE unwind)
185-
list(APPEND LIBCXX_INTERFACE_LIBRARIES unwind) # For the linker script
183+
target_link_libraries(cxx_shared PUBLIC unwind)
186184
endif()
187185
endif()
188186

@@ -195,7 +193,6 @@ if (LIBCXX_ENABLE_SHARED)
195193
endif()
196194
else()
197195
target_link_libraries(cxx_shared PUBLIC "${LIBCXX_CXX_SHARED_ABI_LIBRARY}")
198-
list(APPEND LIBCXX_INTERFACE_LIBRARIES "${LIBCXX_CXX_SHARED_ABI_LIBRARY}") # For the linker script
199196
endif()
200197

201198
# Maybe re-export symbols from libc++abi
@@ -222,31 +219,10 @@ if (LIBCXX_ENABLE_SHARED)
222219
endif()
223220
endif()
224221

225-
# Generate a linker script in place of a libc++.so symlink. Rerun this command
226-
# after cxx builds.
222+
# Generate a linker script in place of a libc++.so symlink.
227223
if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
228-
# Get the name of the ABI library and handle the case where CXXABI_LIBNAME
229-
# is a target name and not a library. Ex cxxabi_shared.
230-
set(LIBCXX_INTERFACE_LIBRARY_NAMES)
231-
foreach(lib ${LIBCXX_INTERFACE_LIBRARIES})
232-
# FIXME: Handle cxxabi_static and unwind_static.
233-
if (TARGET ${lib} OR
234-
(${lib} MATCHES "cxxabi(_static|_shared)?" AND HAVE_LIBCXXABI) OR
235-
(${lib} MATCHES "unwind(_static|_shared)?" AND HAVE_LIBUNWIND))
236-
list(APPEND LIBCXX_INTERFACE_LIBRARY_NAMES "$<TARGET_PROPERTY:${lib},OUTPUT_NAME>")
237-
else()
238-
list(APPEND LIBCXX_INTERFACE_LIBRARY_NAMES "${lib}")
239-
endif()
240-
endforeach()
241-
add_custom_command(TARGET cxx_shared POST_BUILD
242-
COMMAND
243-
${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/gen_link_script.py
244-
ARGS
245-
--input "$<TARGET_SONAME_FILE:cxx_shared>"
246-
--output "$<TARGET_LINKER_FILE:cxx_shared>"
247-
${LIBCXX_INTERFACE_LIBRARY_NAMES}
248-
WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
249-
)
224+
include(DefineLinkerScript)
225+
define_linker_script(cxx_shared)
250226
endif()
251227

252228
list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")

utils/gen_link_script.py

-58
This file was deleted.

0 commit comments

Comments
 (0)