Skip to content

Commit e338c45

Browse files
committed
[lto][cmake] When building with lto on macOS, specify to the linker the path where it should put the generated LTO object file.
This is important so that the object file references in the debug info are preserved for possible use by dsymutil. rdar://28062659
1 parent 9b2c647 commit e338c45

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,14 @@ function(_add_variant_swift_compile_flags
250250
endfunction()
251251

252252
function(_add_variant_link_flags)
253-
set(oneValueArgs SDK ARCH BUILD_TYPE ENABLE_ASSERTIONS ANALYZE_CODE_COVERAGE DEPLOYMENT_VERSION_IOS RESULT_VAR_NAME ENABLE_LTO)
253+
set(oneValueArgs SDK ARCH BUILD_TYPE ENABLE_ASSERTIONS ANALYZE_CODE_COVERAGE
254+
DEPLOYMENT_VERSION_IOS RESULT_VAR_NAME ENABLE_LTO LTO_OBJECT_NAME)
254255
cmake_parse_arguments(LFLAGS
255256
""
256257
"${oneValueArgs}"
257258
""
258259
${ARGN})
259-
260+
260261
if("${LFLAGS_SDK}" STREQUAL "")
261262
message(FATAL_ERROR "Should specify an SDK")
262263
endif()
@@ -294,6 +295,19 @@ function(_add_variant_link_flags)
294295
"${SWIFT_ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so")
295296
else()
296297
list(APPEND result "-lobjc")
298+
299+
# If lto is enabled, we need to add the object path flag so that the LTO code
300+
# generator leaves the intermediate object file in a place where it will not
301+
# be touched. The reason why this must be done is that on OS X, debug info is
302+
# left in object files. So if the object file is removed when we go to
303+
# generate a dsym, the debug info is gone.
304+
if (LFLAGS_ENABLE_LTO)
305+
precondition(LFLAGS_LTO_OBJECT_NAME
306+
MESSAGE "Should specify a unique name for the lto object")
307+
set(lto_object_dir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
308+
set(lto_object ${lto_object_dir}/${LFLAGS_LTO_OBJECT_NAME}-lto.o)
309+
list(APPEND result "-Wl,-object_path_lto,${lto_object}")
310+
endif()
297311
endif()
298312

299313
if(NOT "${SWIFT_${LFLAGS_SDK}_ICU_UC}" STREQUAL "")
@@ -932,6 +946,7 @@ function(_add_swift_library_single target name)
932946
ENABLE_ASSERTIONS "${enable_assertions}"
933947
ANALYZE_CODE_COVERAGE "${analyze_code_coverage}"
934948
ENABLE_LTO "${lto_type}"
949+
LTO_OBJECT_NAME "${target}-${SWIFTLIB_SINGLE_SDK}-${SWIFTLIB_SINGLE_ARCHITECTURE}"
935950
DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}"
936951
RESULT_VAR_NAME link_flags
937952
)
@@ -1641,6 +1656,7 @@ function(_add_swift_executable_single name)
16411656
BUILD_TYPE "${CMAKE_BUILD_TYPE}"
16421657
ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}"
16431658
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
1659+
LTO_OBJECT_NAME "${name}-${SWIFTEXE_SINGLE_SDK}-${SWIFTEXE_SINGLE_ARCHITECTURE}"
16441660
ANALYZE_CODE_COVERAGE "${SWIFT_ANALYZE_CODE_COVERAGE}"
16451661
RESULT_VAR_NAME link_flags)
16461662

tools/SourceKit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function(add_sourcekit_default_compiler_flags target)
8383
BUILD_TYPE "${build_type}"
8484
ENABLE_ASSERTIONS "${enable_assertions}"
8585
ENABLE_LTO "${SWIFT_TOOLS_ENABLE_LTO}"
86+
LTO_OBJECT_NAME "${target}-${sdk}-${arch}"
8687
ANALYZE_CODE_COVERAGE "${analyze_code_coverage}"
8788
RESULT_VAR_NAME link_flags)
8889

0 commit comments

Comments
 (0)