Skip to content

Commit 737de4d

Browse files
committedJun 2, 2019
[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
1 parent fe699c3 commit 737de4d

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed
 

‎libcxx/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,16 @@ if (LIBCXX_ENABLE_STATIC)
375375
set(MERGE_ARCHIVES_ABI_TARGET
376376
"${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
377377
endif()
378+
if (APPLE)
379+
set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
380+
endif()
378381
add_custom_command(TARGET cxx_static POST_BUILD
379382
COMMAND
380383
${PYTHON_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py
381384
ARGS
382385
-o $<TARGET_LINKER_FILE:cxx_static>
383386
--ar "${CMAKE_AR}"
387+
${MERGE_ARCHIVES_LIBTOOL}
384388
"$<TARGET_LINKER_FILE:cxx_static>"
385389
"${MERGE_ARCHIVES_ABI_TARGET}"
386390
"${MERGE_ARCHIVES_SEARCH_PATHS}"

‎libcxx/utils/merge_archives.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def main():
9797
'--ar', dest='ar_exe', required=False,
9898
help='The ar executable to use, finds \'ar\' in the path if not given',
9999
type=str, action='store')
100+
parser.add_argument(
101+
'--use-libtool', dest='use_libtool', action='store_true', default=False)
102+
parser.add_argument(
103+
'--libtool', dest='libtool_exe', required=False,
104+
help='The libtool executable to use, finds \'libtool\' in the path if not given',
105+
type=str, action='store')
100106
parser.add_argument(
101107
'archives', metavar='archives', nargs='+',
102108
help='The archives to merge')
@@ -109,6 +115,13 @@ def main():
109115
if not ar_exe:
110116
print_and_exit("failed to find 'ar' executable")
111117

118+
if args.use_libtool:
119+
libtool_exe = args.libtool_exe
120+
if not libtool_exe:
121+
libtool_exe = distutils.spawn.find_executable('libtool')
122+
if not libtool_exe:
123+
print_and_exit("failed to find 'libtool' executable")
124+
112125
if len(args.archives) < 2:
113126
print_and_exit('fewer than 2 inputs provided')
114127
archives = [find_and_diagnose_missing(ar, args.search_paths)
@@ -127,8 +140,13 @@ def main():
127140
out = execute_command_verbose([ar_exe, 't', arc])
128141
files.extend(out.splitlines())
129142

130-
execute_command_verbose([ar_exe, 'rcs', args.output] + files,
131-
cwd=temp_directory_root, verbose=args.verbose)
143+
if args.use_libtool:
144+
files = [f for f in files if not f.startswith('__.SYMDEF')]
145+
execute_command_verbose([libtool_exe, '-static', '-o', args.output] + files,
146+
cwd=temp_directory_root, verbose=args.verbose)
147+
else:
148+
execute_command_verbose([ar_exe, 'rcs', args.output] + files,
149+
cwd=temp_directory_root, verbose=args.verbose)
132150

133151

134152
if __name__ == '__main__':

‎libcxxabi/src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,18 @@ if (LIBCXXABI_ENABLE_STATIC)
226226
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
227227
endif()
228228

229+
if (APPLE)
230+
set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
231+
endif()
232+
229233
# Merge the the libc++abi.a and libunwind.a into one.
230234
if(LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
231235
add_custom_command(TARGET cxxabi_static POST_BUILD
232236
COMMAND ${PYTHON_EXECUTABLE} ${LIBCXXABI_LIBCXX_PATH}/utils/merge_archives.py
233237
ARGS
234238
-o "$<TARGET_LINKER_FILE:cxxabi_static>"
235239
--ar "${CMAKE_AR}"
240+
${MERGE_ARCHIVES_LIBTOOL}
236241
"$<TARGET_LINKER_FILE:cxxabi_static>"
237242
"$<TARGET_LINKER_FILE:unwind_static>"
238243
WORKING_DIRECTORY ${LIBCXXABI_BUILD_DIR}

0 commit comments

Comments
 (0)
Please sign in to comment.