[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
This commit is contained in:
Petr Hosek 2019-06-02 01:14:31 +00:00
parent fe699c32a2
commit 737de4d363
3 changed files with 29 additions and 2 deletions

View File

@ -375,12 +375,16 @@ if (LIBCXX_ENABLE_STATIC)
set(MERGE_ARCHIVES_ABI_TARGET set(MERGE_ARCHIVES_ABI_TARGET
"${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}") "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif() endif()
if (APPLE)
set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
endif()
add_custom_command(TARGET cxx_static POST_BUILD add_custom_command(TARGET cxx_static POST_BUILD
COMMAND COMMAND
${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py ${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py
ARGS ARGS
-o $<TARGET_LINKER_FILE:cxx_static> -o $<TARGET_LINKER_FILE:cxx_static>
--ar "${CMAKE_AR}" --ar "${CMAKE_AR}"
${MERGE_ARCHIVES_LIBTOOL}
"$<TARGET_LINKER_FILE:cxx_static>" "$<TARGET_LINKER_FILE:cxx_static>"
"${MERGE_ARCHIVES_ABI_TARGET}" "${MERGE_ARCHIVES_ABI_TARGET}"
"${MERGE_ARCHIVES_SEARCH_PATHS}" "${MERGE_ARCHIVES_SEARCH_PATHS}"

View File

@ -97,6 +97,12 @@ def main():
'--ar', dest='ar_exe', required=False, '--ar', dest='ar_exe', required=False,
help='The ar executable to use, finds \'ar\' in the path if not given', help='The ar executable to use, finds \'ar\' in the path if not given',
type=str, action='store') 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( parser.add_argument(
'archives', metavar='archives', nargs='+', 'archives', metavar='archives', nargs='+',
help='The archives to merge') help='The archives to merge')
@ -109,6 +115,13 @@ def main():
if not ar_exe: if not ar_exe:
print_and_exit("failed to find 'ar' executable") 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: if len(args.archives) < 2:
print_and_exit('fewer than 2 inputs provided') print_and_exit('fewer than 2 inputs provided')
archives = [find_and_diagnose_missing(ar, args.search_paths) archives = [find_and_diagnose_missing(ar, args.search_paths)
@ -127,6 +140,11 @@ def main():
out = execute_command_verbose([ar_exe, 't', arc]) out = execute_command_verbose([ar_exe, 't', arc])
files.extend(out.splitlines()) files.extend(out.splitlines())
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, execute_command_verbose([ar_exe, 'rcs', args.output] + files,
cwd=temp_directory_root, verbose=args.verbose) cwd=temp_directory_root, verbose=args.verbose)

View File

@ -226,6 +226,10 @@ if (LIBCXXABI_ENABLE_STATIC)
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static") list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
endif() endif()
if (APPLE)
set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
endif()
# Merge the the libc++abi.a and libunwind.a into one. # Merge the the libc++abi.a and libunwind.a into one.
if(LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY) if(LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
add_custom_command(TARGET cxxabi_static POST_BUILD add_custom_command(TARGET cxxabi_static POST_BUILD
@ -233,6 +237,7 @@ if (LIBCXXABI_ENABLE_STATIC)
ARGS ARGS
-o "$<TARGET_LINKER_FILE:cxxabi_static>" -o "$<TARGET_LINKER_FILE:cxxabi_static>"
--ar "${CMAKE_AR}" --ar "${CMAKE_AR}"
${MERGE_ARCHIVES_LIBTOOL}
"$<TARGET_LINKER_FILE:cxxabi_static>" "$<TARGET_LINKER_FILE:cxxabi_static>"
"$<TARGET_LINKER_FILE:unwind_static>" "$<TARGET_LINKER_FILE:unwind_static>"
WORKING_DIRECTORY ${LIBCXXABI_BUILD_DIR} WORKING_DIRECTORY ${LIBCXXABI_BUILD_DIR}