1include(ExternalProject)
2include(CompilerRTUtils)
3
4function(set_target_output_directories target output_dir)
5  # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators
6  # append a per-configuration subdirectory to the specified directory.
7  # To avoid the appended folder, the configuration specific variable must be
8  # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}':
9  # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ...
10  if(CMAKE_CONFIGURATION_TYPES)
11    foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
12      string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
13      set_target_properties("${target}" PROPERTIES
14          "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
15          "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
16          "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir})
17    endforeach()
18  else()
19    set_target_properties("${target}" PROPERTIES
20        ARCHIVE_OUTPUT_DIRECTORY ${output_dir}
21        LIBRARY_OUTPUT_DIRECTORY ${output_dir}
22        RUNTIME_OUTPUT_DIRECTORY ${output_dir})
23  endif()
24endfunction()
25
26# Tries to add an "object library" target for a given list of OSs and/or
27# architectures with name "<name>.<arch>" for non-Darwin platforms if
28# architecture can be targeted, and "<name>.<os>" for Darwin platforms.
29# add_compiler_rt_object_libraries(<name>
30#                                  OS <os names>
31#                                  ARCHS <architectures>
32#                                  SOURCES <source files>
33#                                  CFLAGS <compile flags>
34#                                  DEFS <compile definitions>)
35function(add_compiler_rt_object_libraries name)
36  cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS" ${ARGN})
37  set(libnames)
38  if(APPLE)
39    foreach(os ${LIB_OS})
40      set(libname "${name}.${os}")
41      set(libnames ${libnames} ${libname})
42      set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
43      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
44    endforeach()
45  else()
46    foreach(arch ${LIB_ARCHS})
47      set(libname "${name}.${arch}")
48      set(libnames ${libnames} ${libname})
49      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
50      if(NOT CAN_TARGET_${arch})
51        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
52        return()
53      endif()
54    endforeach()
55  endif()
56
57  foreach(libname ${libnames})
58    add_library(${libname} OBJECT ${LIB_SOURCES})
59
60    # Strip out -msse3 if this isn't macOS.
61    set(target_flags ${LIB_CFLAGS})
62    if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*")
63      list(REMOVE_ITEM target_flags "-msse3")
64    endif()
65
66    set_target_compile_flags(${libname}
67      ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${target_flags})
68    set_property(TARGET ${libname} APPEND PROPERTY
69      COMPILE_DEFINITIONS ${LIB_DEFS})
70    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
71    if(APPLE)
72      set_target_properties(${libname} PROPERTIES
73        OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
74    endif()
75  endforeach()
76endfunction()
77
78# Takes a list of object library targets, and a suffix and appends the proper
79# TARGET_OBJECTS string to the output variable.
80# format_object_libs(<output> <suffix> ...)
81macro(format_object_libs output suffix)
82  foreach(lib ${ARGN})
83    list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
84  endforeach()
85endmacro()
86
87function(add_compiler_rt_component name)
88  add_custom_target(${name})
89  set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
90  if(COMMAND runtime_register_component)
91    runtime_register_component(${name})
92  endif()
93  add_dependencies(compiler-rt ${name})
94endfunction()
95
96macro(set_output_name output name arch)
97  if(ANDROID AND ${arch} STREQUAL "i386")
98    set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
99  else()
100    set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
101  endif()
102endmacro()
103
104# Adds static or shared runtime for a list of architectures and operating
105# systems and puts it in the proper directory in the build and install trees.
106# add_compiler_rt_runtime(<name>
107#                         {STATIC|SHARED}
108#                         ARCHS <architectures>
109#                         OS <os list>
110#                         SOURCES <source files>
111#                         CFLAGS <compile flags>
112#                         LINK_FLAGS <linker flags>
113#                         DEFS <compile definitions>
114#                         LINK_LIBS <linked libraries> (only for shared library)
115#                         OBJECT_LIBS <object libraries to use as sources>
116#                         PARENT_TARGET <convenience parent target>)
117function(add_compiler_rt_runtime name type)
118  if(NOT type MATCHES "^(STATIC|SHARED)$")
119    message(FATAL_ERROR "type argument must be STATIC or SHARED")
120    return()
121  endif()
122  cmake_parse_arguments(LIB
123    ""
124    "PARENT_TARGET"
125    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
126    ${ARGN})
127  set(libnames)
128  # Until we support this some other way, build compiler-rt runtime without LTO
129  # to allow non-LTO projects to link with it.
130  if(COMPILER_RT_HAS_FNO_LTO_FLAG)
131    set(NO_LTO_FLAGS "-fno-lto")
132  else()
133    set(NO_LTO_FLAGS "")
134  endif()
135
136  if(APPLE)
137    foreach(os ${LIB_OS})
138      # Strip out -msse3 if this isn't macOS.
139      list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
140      if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
141        list(REMOVE_ITEM LIB_CFLAGS "-msse3")
142      endif()
143      if(type STREQUAL "STATIC")
144        set(libname "${name}_${os}")
145      else()
146        set(libname "${name}_${os}_dynamic")
147        set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
148      endif()
149      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
150      if(LIB_ARCHS_${libname})
151        list(APPEND libnames ${libname})
152        set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
153        set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
154        set(sources_${libname} ${LIB_SOURCES})
155        format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
156      endif()
157    endforeach()
158  else()
159    foreach(arch ${LIB_ARCHS})
160      if(NOT CAN_TARGET_${arch})
161        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
162        return()
163      endif()
164      if(type STREQUAL "STATIC")
165        set(libname "${name}-${arch}")
166        set_output_name(output_name_${libname} ${name} ${arch})
167      else()
168        set(libname "${name}-dynamic-${arch}")
169        set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
170        set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
171        if(WIN32)
172          set_output_name(output_name_${libname} ${name}_dynamic ${arch})
173        else()
174          set_output_name(output_name_${libname} ${name} ${arch})
175        endif()
176      endif()
177      set(sources_${libname} ${LIB_SOURCES})
178      format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
179      set(libnames ${libnames} ${libname})
180      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
181    endforeach()
182  endif()
183
184  if(NOT libnames)
185    return()
186  endif()
187
188  if(LIB_PARENT_TARGET)
189    # If the parent targets aren't created we should create them
190    if(NOT TARGET ${LIB_PARENT_TARGET})
191      add_custom_target(${LIB_PARENT_TARGET})
192    endif()
193    if(NOT TARGET install-${LIB_PARENT_TARGET})
194      # The parent install target specifies the parent component to scrape up
195      # anything not installed by the individual install targets, and to handle
196      # installation when running the multi-configuration generators.
197      add_custom_target(install-${LIB_PARENT_TARGET}
198                        DEPENDS ${LIB_PARENT_TARGET}
199                        COMMAND "${CMAKE_COMMAND}"
200                                -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
201                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
202      set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
203                            FOLDER "Compiler-RT Misc")
204      add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
205    endif()
206  endif()
207
208  foreach(libname ${libnames})
209    # If you are using a multi-configuration generator we don't generate
210    # per-library install rules, so we fall back to the parent target COMPONENT
211    if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
212      set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
213    else()
214      set(COMPONENT_OPTION COMPONENT ${libname})
215    endif()
216
217    add_library(${libname} ${type} ${sources_${libname}})
218    set_target_compile_flags(${libname} ${extra_cflags_${libname}})
219    set_target_link_flags(${libname} ${extra_link_flags_${libname}})
220    set_property(TARGET ${libname} APPEND PROPERTY
221                COMPILE_DEFINITIONS ${LIB_DEFS})
222    set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
223    set_target_properties(${libname} PROPERTIES
224        OUTPUT_NAME ${output_name_${libname}})
225    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
226    if(LIB_LINK_LIBS)
227      target_link_libraries(${libname} ${LIB_LINK_LIBS})
228    endif()
229    if(${type} STREQUAL "SHARED")
230      if(WIN32 AND NOT CYGWIN AND NOT MINGW)
231        set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
232        set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
233      endif()
234      if(APPLE)
235        # Ad-hoc sign the dylibs
236        add_custom_command(TARGET ${libname}
237          POST_BUILD
238          COMMAND codesign --sign - $<TARGET_FILE:${libname}>
239          WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
240        )
241      endif()
242    endif()
243    install(TARGETS ${libname}
244      ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
245              ${COMPONENT_OPTION}
246      LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
247              ${COMPONENT_OPTION}
248      RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
249              ${COMPONENT_OPTION})
250
251    # We only want to generate per-library install targets if you aren't using
252    # an IDE because the extra targets get cluttered in IDEs.
253    if(NOT CMAKE_CONFIGURATION_TYPES)
254      add_custom_target(install-${libname}
255                        DEPENDS ${libname}
256                        COMMAND "${CMAKE_COMMAND}"
257                                -DCMAKE_INSTALL_COMPONENT=${libname}
258                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
259      # If you have a parent target specified, we bind the new install target
260      # to the parent install target.
261      if(LIB_PARENT_TARGET)
262        add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
263      endif()
264    endif()
265    if(APPLE)
266      set_target_properties(${libname} PROPERTIES
267      OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
268    endif()
269
270    if(type STREQUAL "SHARED")
271      rt_externalize_debuginfo(${libname})
272    endif()
273  endforeach()
274  if(LIB_PARENT_TARGET)
275    add_dependencies(${LIB_PARENT_TARGET} ${libnames})
276  endif()
277endfunction()
278
279# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
280# in compilation and linking of unittests.
281string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
282set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
283
284# Unittests support.
285set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
286set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
287set(COMPILER_RT_GTEST_CFLAGS
288  -DGTEST_NO_LLVM_RAW_OSTREAM=1
289  -DGTEST_HAS_RTTI=0
290  -I${COMPILER_RT_GTEST_PATH}/include
291  -I${COMPILER_RT_GTEST_PATH}
292)
293
294append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
295append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
296
297if(MSVC)
298  # clang doesn't support exceptions on Windows yet.
299  list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
300
301  # We should teach clang to understand "#pragma intrinsic", see PR19898.
302  list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
303
304  # Clang doesn't support SEH on Windows yet.
305  list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
306
307  # gtest use a lot of stuff marked as deprecated on Windows.
308  list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
309endif()
310
311# Compile and register compiler-rt tests.
312# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
313#                           <test architecture>
314#                           KIND <custom prefix>
315#                           SUBDIR <subdirectory for testing binary>
316#                           SOURCES <sources to compile>
317#                           RUNTIME <tests runtime to link in>
318#                           CFLAGS <compile-time flags>
319#                           COMPILE_DEPS <compile-time dependencies>
320#                           DEPS <dependencies>
321#                           LINK_FLAGS <flags to use during linking>
322# )
323function(generate_compiler_rt_tests test_objects test_suite testname arch)
324  cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
325    "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
326
327  foreach(source ${TEST_SOURCES})
328    sanitizer_test_compile(
329      "${test_objects}" "${source}" "${arch}"
330      KIND ${TEST_KIND}
331      COMPILE_DEPS ${TEST_COMPILE_DEPS}
332      DEPS ${TEST_DEPS}
333      CFLAGS ${TEST_CFLAGS}
334      )
335  endforeach()
336
337  set(TEST_DEPS ${${test_objects}})
338
339  if(NOT "${TEST_RUNTIME}" STREQUAL "")
340    list(APPEND TEST_DEPS ${TEST_RUNTIME})
341    list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
342  endif()
343
344  add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
345    SUBDIR ${TEST_SUBDIR}
346    OBJECTS ${${test_objects}}
347    DEPS ${TEST_DEPS}
348    LINK_FLAGS ${TEST_LINK_FLAGS}
349    )
350  set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
351endfunction()
352
353# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
354# using specified link flags. Make executable a part of provided
355# test_suite.
356# add_compiler_rt_test(<test_suite> <test_name> <arch>
357#                      SUBDIR <subdirectory for binary>
358#                      OBJECTS <object files>
359#                      DEPS <deps (e.g. runtime libs)>
360#                      LINK_FLAGS <link flags>)
361function(add_compiler_rt_test test_suite test_name arch)
362  cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
363  set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
364  if(TEST_SUBDIR)
365    set(output_dir "${output_dir}/${TEST_SUBDIR}")
366  endif()
367  set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
368  file(MAKE_DIRECTORY "${output_dir}")
369  set(output_bin "${output_dir}/${test_name}")
370  if(MSVC)
371    set(output_bin "${output_bin}.exe")
372  endif()
373
374  # Use host compiler in a standalone build, and just-built Clang otherwise.
375  if(NOT COMPILER_RT_STANDALONE_BUILD)
376    list(APPEND TEST_DEPS clang)
377  endif()
378
379  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
380  list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
381
382  # If we're not on MSVC, include the linker flags from CMAKE but override them
383  # with the provided link flags. This ensures that flags which are required to
384  # link programs at all are included, but the changes needed for the test
385  # trump. With MSVC we can't do that because CMake is set up to run link.exe
386  # when linking, not the compiler. Here, we hack it to use the compiler
387  # because we want to use -fsanitize flags.
388  if(NOT MSVC)
389    set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
390    separate_arguments(TEST_LINK_FLAGS)
391  endif()
392  add_custom_command(
393    OUTPUT "${output_bin}"
394    COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
395            ${TEST_LINK_FLAGS}
396    DEPENDS ${TEST_DEPS}
397    )
398  add_custom_target(T${test_name} DEPENDS "${output_bin}")
399  set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
400
401  # Make the test suite depend on the binary.
402  add_dependencies(${test_suite} T${test_name})
403endfunction()
404
405macro(add_compiler_rt_resource_file target_name file_name component)
406  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
407  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/${file_name}")
408  add_custom_command(OUTPUT ${dst_file}
409    DEPENDS ${src_file}
410    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
411    COMMENT "Copying ${file_name}...")
412  add_custom_target(${target_name} DEPENDS ${dst_file})
413  # Install in Clang resource directory.
414  install(FILES ${file_name}
415    DESTINATION ${COMPILER_RT_INSTALL_PATH}
416    COMPONENT ${component})
417  add_dependencies(${component} ${target_name})
418
419  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
420endmacro()
421
422macro(add_compiler_rt_script name)
423  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
424  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
425  add_custom_command(OUTPUT ${dst}
426    DEPENDS ${src}
427    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
428    COMMENT "Copying ${name}...")
429  add_custom_target(${name} DEPENDS ${dst})
430  install(FILES ${dst}
431    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
432    DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
433endmacro(add_compiler_rt_script src name)
434
435# Builds custom version of libc++ and installs it in <prefix>.
436# Can be used to build sanitized versions of libc++ for running unit tests.
437# add_custom_libcxx(<name> <prefix>
438#                   DEPS <list of build deps>
439#                   CFLAGS <list of compile flags>)
440macro(add_custom_libcxx name prefix)
441  if(NOT COMPILER_RT_HAS_LIBCXX_SOURCES)
442    message(FATAL_ERROR "libcxx not found!")
443  endif()
444
445  cmake_parse_arguments(LIBCXX "" "" "DEPS;CFLAGS" ${ARGN})
446  foreach(flag ${LIBCXX_CFLAGS})
447    set(flagstr "${flagstr} ${flag}")
448  endforeach()
449  set(LIBCXX_CFLAGS ${flagstr})
450
451  if(NOT COMPILER_RT_STANDALONE_BUILD)
452    list(APPEND LIBCXX_DEPS clang)
453  endif()
454
455  ExternalProject_Add(${name}
456    PREFIX ${prefix}
457    SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
458    CMAKE_ARGS -DCMAKE_MAKE_PROGRAM:STRING=${CMAKE_MAKE_PROGRAM}
459               -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
460               -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER}
461               -DCMAKE_C_FLAGS=${LIBCXX_CFLAGS}
462               -DCMAKE_CXX_FLAGS=${LIBCXX_CFLAGS}
463               -DCMAKE_BUILD_TYPE=Release
464               -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
465               -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
466               -DLIBCXX_STANDALONE_BUILD=On
467    LOG_BUILD 1
468    LOG_CONFIGURE 1
469    LOG_INSTALL 1
470    )
471  set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)
472
473  ExternalProject_Add_Step(${name} force-reconfigure
474    DEPENDERS configure
475    ALWAYS 1
476    )
477
478  ExternalProject_Add_Step(${name} clobber
479    COMMAND ${CMAKE_COMMAND} -E remove_directory <BINARY_DIR>
480    COMMAND ${CMAKE_COMMAND} -E make_directory <BINARY_DIR>
481    COMMENT "Clobberring ${name} build directory..."
482    DEPENDERS configure
483    DEPENDS ${LIBCXX_DEPS}
484    )
485endmacro()
486
487function(rt_externalize_debuginfo name)
488  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
489    return()
490  endif()
491
492  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
493    set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
494  endif()
495
496  if(APPLE)
497    if(CMAKE_CXX_FLAGS MATCHES "-flto"
498      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
499
500      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
501      set_property(TARGET ${name} APPEND_STRING PROPERTY
502        LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
503    endif()
504    add_custom_command(TARGET ${name} POST_BUILD
505      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
506      ${strip_command})
507  else()
508    message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
509  endif()
510endfunction()
511