[libcxx] Make it drastically simpler to link libc++.
Summary:
Currently on most platforms you have to manually link the c++ abi library used with libc++ whenever you use libc++. So your typical libc++ command like invocation might look like:
```
clang++ -stdlib=libc++ foo.cpp -lc++abi
```
Having to manually link `libc++abi.so` makes it harder for libc++ to be used generically. This patch fixes that by generating a linker script for `libc++.so` that correctly links the ABI library. On linux the linker script for libc++abi would look like:
```
# libc++.so
INPUT(libc++.so.1 -lc++abi)
```
With the linker script you can now use libc++ using only `-stdlib=libc++`. This is the technique that is used on FreeBSD in ordered to link cxxrt and I think it's the best approach to make our users lives simpler.
The CMake option used to enable this is `LIBCXX_ENABLE_ABI_LINKER_SCRIPT`. In future I would like to enable this by default on all platforms except for Darwin.
Reviewers: mclow.lists, danalbert, rsmith, jroelofs, EricWF
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D12508
llvm-svn: 250319
diff --git a/libcxx/lib/CMakeLists.txt b/libcxx/lib/CMakeLists.txt
index 1a1c55b..a3b50a7 100644
--- a/libcxx/lib/CMakeLists.txt
+++ b/libcxx/lib/CMakeLists.txt
@@ -133,11 +133,40 @@
SOVERSION "${LIBCXX_ABI_VERSION}"
)
+# Generate a linker script inplace of a libc++.so symlink. Rerun this command
+# after cxx builds.
+if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
+ # Get the name of the ABI library and handle the case where CXXABI_LIBNAME
+ # is a target name and not a library. Ex cxxabi_shared.
+ set(SCRIPT_ABI_LIBNAME "${CXXABI_LIBNAME}")
+ if (SCRIPT_ABI_LIBNAME STREQUAL "cxxabi_shared")
+ set("${SCRIPT_ABI_LIBNAME}" "c++abi")
+ endif()
+ # Generate a linker script inplace of a libc++.so symlink. Rerun this command
+ # after cxx builds.
+ add_custom_command(TARGET cxx POST_BUILD
+ COMMAND
+ ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/gen_link_script/gen_link_script.py
+ ARGS
+ "$<TARGET_LINKER_FILE:cxx>"
+ "${SCRIPT_ABI_LIBNAME}"
+ WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
+ )
+endif()
+
+
if (LIBCXX_INSTALL_LIBRARY)
install(TARGETS cxx
LIBRARY DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT libcxx
ARCHIVE DESTINATION lib${LIBCXX_LIBDIR_SUFFIX} COMPONENT libcxx
)
+ # NOTE: This install command must go after the cxx install command otherwise
+ # it will not be executed after the library symlinks are installed.
+ if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
+ install(FILES "$<TARGET_LINKER_FILE:cxx>"
+ DESTINATION lib${LIBCXX_LIBDIR_SUFFIX}
+ COMPONENT libcxx)
+ endif()
endif()
if (NOT CMAKE_CONFIGURATION_TYPES AND (LIBCXX_INSTALL_LIBRARY OR