Skip to content

Commit c257709

Browse files
authored
On Darwin, pass LIBLTO_PATH to 'ar' and 'lipo' commands from CMake (#41243)
1 parent a546cc1 commit c257709

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

cmake/modules/UnixCompileRules.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,20 @@ set(CMAKE_C_ARCHIVE_FINISH "")
1111
set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> crs <TARGET> <LINK_FLAGS> <OBJECTS>")
1212
set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> qs <TARGET> <LINK_FLAGS> <OBJECTS>")
1313
set(CMAKE_CXX_ARCHIVE_FINISH "")
14+
15+
# When archiving LTO-based .o files with ar/ranlib/libtool on Darwin, the tools
16+
# use libLTO.dylib to inspect the bitcode files. However, by default the
17+
# "host" libLTO.dylib is loaded, which might be too old and not understand our
18+
# just-built bitcode format. So let's instead ask ar/ranlib/libtool to use the
19+
# just-built libLTO.dylib from the toolchain that we're using to build.
20+
if(APPLE AND SWIFT_NATIVE_CLANG_TOOLS_PATH)
21+
set(liblto_path "${SWIFT_NATIVE_CLANG_TOOLS_PATH}/../lib/libLTO.dylib")
22+
23+
set(CMAKE_C_ARCHIVE_CREATE "${CMAKE_COMMAND} -E env LIBLTO_PATH=${liblto_path} <CMAKE_AR> crs <TARGET> <LINK_FLAGS> <OBJECTS>")
24+
set(CMAKE_C_ARCHIVE_APPEND "${CMAKE_COMMAND} -E env LIBLTO_PATH=${liblto_path} <CMAKE_AR> qs <TARGET> <LINK_FLAGS> <OBJECTS>")
25+
set(CMAKE_C_ARCHIVE_FINISH "")
26+
27+
set(CMAKE_CXX_ARCHIVE_CREATE "${CMAKE_COMMAND} -E env LIBLTO_PATH=${liblto_path} <CMAKE_AR> crs <TARGET> <LINK_FLAGS> <OBJECTS>")
28+
set(CMAKE_CXX_ARCHIVE_APPEND "${CMAKE_COMMAND} -E env LIBLTO_PATH=${liblto_path} <CMAKE_AR> qs <TARGET> <LINK_FLAGS> <OBJECTS>")
29+
set(CMAKE_CXX_ARCHIVE_FINISH "")
30+
endif()

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,20 @@ function(_add_swift_lipo_target)
546546
if(LIPO_CODESIGN)
547547
set(codesign_command COMMAND "codesign" "-f" "-s" "-" "${LIPO_OUTPUT}")
548548
endif()
549+
550+
set(lipo_lto_env)
551+
# When lipo-ing LTO-based libraries with lipo on Darwin, the tool uses
552+
# libLTO.dylib to inspect the bitcode files. However, by default the "host"
553+
# libLTO.dylib is loaded, which might be too old and not understand the
554+
# just-built bitcode format. Let's ask lipo to use the just-built
555+
# libLTO.dylib from the toolchain that we're using to build.
556+
if(APPLE AND SWIFT_NATIVE_CLANG_TOOLS_PATH)
557+
set(lipo_lto_env "LIBLTO_PATH=${SWIFT_NATIVE_CLANG_TOOLS_PATH}/../lib/libLTO.dylib")
558+
endif()
559+
549560
# Use lipo to create the final binary.
550561
add_custom_command_target(unused_var
551-
COMMAND "${SWIFT_LIPO}" "-create" "-output" "${LIPO_OUTPUT}" ${source_binaries}
562+
COMMAND "${CMAKE_COMMAND}" "-E" "env" ${lipo_lto_env} "${SWIFT_LIPO}" "-create" "-output" "${LIPO_OUTPUT}" ${source_binaries}
552563
${codesign_command}
553564
CUSTOM_TARGET_NAME "${LIPO_TARGET}"
554565
OUTPUT "${LIPO_OUTPUT}"

0 commit comments

Comments
 (0)