From 737de4d363ede4b90dd5609af0494fb39af53865 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Sun, 2 Jun 2019 01:14:31 +0000 Subject: [PATCH] [libcxx] Use libtool when merging archives on Apple platforms ar doesn't produce the correct results when used for linking static archives on Apple platforms, so instead use libtool -static which is the official way to build static archives on those platforms. Differential Revision: https://reviews.llvm.org/D62770 llvm-svn: 362311 --- libcxx/src/CMakeLists.txt | 4 ++++ libcxx/utils/merge_archives.py | 22 ++++++++++++++++++++-- libcxxabi/src/CMakeLists.txt | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt index 4fe4db47d2ad..2a8ff2c2d89b 100644 --- a/libcxx/src/CMakeLists.txt +++ b/libcxx/src/CMakeLists.txt @@ -375,12 +375,16 @@ if (LIBCXX_ENABLE_STATIC) set(MERGE_ARCHIVES_ABI_TARGET "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}") endif() + if (APPLE) + set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}") + endif() add_custom_command(TARGET cxx_static POST_BUILD COMMAND ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py ARGS -o $ --ar "${CMAKE_AR}" + ${MERGE_ARCHIVES_LIBTOOL} "$" "${MERGE_ARCHIVES_ABI_TARGET}" "${MERGE_ARCHIVES_SEARCH_PATHS}" diff --git a/libcxx/utils/merge_archives.py b/libcxx/utils/merge_archives.py index 75d7a54e47d8..5c04bc915a4a 100755 --- a/libcxx/utils/merge_archives.py +++ b/libcxx/utils/merge_archives.py @@ -97,6 +97,12 @@ def main(): '--ar', dest='ar_exe', required=False, help='The ar executable to use, finds \'ar\' in the path if not given', type=str, action='store') + parser.add_argument( + '--use-libtool', dest='use_libtool', action='store_true', default=False) + parser.add_argument( + '--libtool', dest='libtool_exe', required=False, + help='The libtool executable to use, finds \'libtool\' in the path if not given', + type=str, action='store') parser.add_argument( 'archives', metavar='archives', nargs='+', help='The archives to merge') @@ -109,6 +115,13 @@ def main(): if not ar_exe: print_and_exit("failed to find 'ar' executable") + if args.use_libtool: + libtool_exe = args.libtool_exe + if not libtool_exe: + libtool_exe = distutils.spawn.find_executable('libtool') + if not libtool_exe: + print_and_exit("failed to find 'libtool' executable") + if len(args.archives) < 2: print_and_exit('fewer than 2 inputs provided') archives = [find_and_diagnose_missing(ar, args.search_paths) @@ -127,8 +140,13 @@ def main(): out = execute_command_verbose([ar_exe, 't', arc]) files.extend(out.splitlines()) - execute_command_verbose([ar_exe, 'rcs', args.output] + files, - cwd=temp_directory_root, verbose=args.verbose) + if args.use_libtool: + files = [f for f in files if not f.startswith('__.SYMDEF')] + execute_command_verbose([libtool_exe, '-static', '-o', args.output] + files, + cwd=temp_directory_root, verbose=args.verbose) + else: + execute_command_verbose([ar_exe, 'rcs', args.output] + files, + cwd=temp_directory_root, verbose=args.verbose) if __name__ == '__main__': diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt index e4e1fac34bbc..77f00987097f 100644 --- a/libcxxabi/src/CMakeLists.txt +++ b/libcxxabi/src/CMakeLists.txt @@ -226,6 +226,10 @@ if (LIBCXXABI_ENABLE_STATIC) list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static") endif() + if (APPLE) + set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}") + endif() + # Merge the the libc++abi.a and libunwind.a into one. if(LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY) add_custom_command(TARGET cxxabi_static POST_BUILD @@ -233,6 +237,7 @@ if (LIBCXXABI_ENABLE_STATIC) ARGS -o "$" --ar "${CMAKE_AR}" + ${MERGE_ARCHIVES_LIBTOOL} "$" "$" WORKING_DIRECTORY ${LIBCXXABI_BUILD_DIR} -- 2.34.1