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>
35#                                  DEPS <dependencies>)
36function(add_compiler_rt_object_libraries name)
37  cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS" ${ARGN})
38  set(libnames)
39  if(APPLE)
40    foreach(os ${LIB_OS})
41      set(libname "${name}.${os}")
42      set(libnames ${libnames} ${libname})
43      set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
44      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
45    endforeach()
46  else()
47    foreach(arch ${LIB_ARCHS})
48      set(libname "${name}.${arch}")
49      set(libnames ${libnames} ${libname})
50      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
51      if(NOT CAN_TARGET_${arch})
52        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
53        return()
54      endif()
55    endforeach()
56  endif()
57
58  foreach(libname ${libnames})
59    add_library(${libname} OBJECT ${LIB_SOURCES})
60    if(LIB_DEPS)
61      add_dependencies(${libname} ${LIB_DEPS})
62    endif()
63
64    # Strip out -msse3 if this isn't macOS.
65    set(target_flags ${LIB_CFLAGS})
66    if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*")
67      list(REMOVE_ITEM target_flags "-msse3")
68    endif()
69
70    set_target_compile_flags(${libname}
71      ${CMAKE_CXX_FLAGS} ${extra_cflags_${libname}} ${target_flags})
72    set_property(TARGET ${libname} APPEND PROPERTY
73      COMPILE_DEFINITIONS ${LIB_DEFS})
74    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
75    if(APPLE)
76      set_target_properties(${libname} PROPERTIES
77        OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
78    endif()
79  endforeach()
80endfunction()
81
82# Takes a list of object library targets, and a suffix and appends the proper
83# TARGET_OBJECTS string to the output variable.
84# format_object_libs(<output> <suffix> ...)
85macro(format_object_libs output suffix)
86  foreach(lib ${ARGN})
87    list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
88  endforeach()
89endmacro()
90
91function(add_compiler_rt_component name)
92  add_custom_target(${name})
93  set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
94  if(COMMAND runtime_register_component)
95    runtime_register_component(${name})
96  endif()
97  add_dependencies(compiler-rt ${name})
98endfunction()
99
100function(add_asm_sources output)
101  set(${output} ${ARGN} PARENT_SCOPE)
102  # Xcode will try to compile asm files as C ('clang -x c'), and that will fail.
103  if (${CMAKE_GENERATOR} STREQUAL "Xcode")
104    enable_language(ASM)
105  else()
106    # Pass ASM file directly to the C++ compiler.
107    set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C)
108  endif()
109endfunction()
110
111macro(set_output_name output name arch)
112  if(ANDROID AND ${arch} STREQUAL "i386")
113    set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
114  else()
115    set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
116  endif()
117endmacro()
118
119# Adds static or shared runtime for a list of architectures and operating
120# systems and puts it in the proper directory in the build and install trees.
121# add_compiler_rt_runtime(<name>
122#                         {STATIC|SHARED}
123#                         ARCHS <architectures>
124#                         OS <os list>
125#                         SOURCES <source files>
126#                         CFLAGS <compile flags>
127#                         LINK_FLAGS <linker flags>
128#                         DEFS <compile definitions>
129#                         LINK_LIBS <linked libraries> (only for shared library)
130#                         OBJECT_LIBS <object libraries to use as sources>
131#                         PARENT_TARGET <convenience parent target>)
132function(add_compiler_rt_runtime name type)
133  if(NOT type MATCHES "^(STATIC|SHARED)$")
134    message(FATAL_ERROR "type argument must be STATIC or SHARED")
135    return()
136  endif()
137  cmake_parse_arguments(LIB
138    ""
139    "PARENT_TARGET"
140    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
141    ${ARGN})
142  set(libnames)
143  # Until we support this some other way, build compiler-rt runtime without LTO
144  # to allow non-LTO projects to link with it.
145  if(COMPILER_RT_HAS_FNO_LTO_FLAG)
146    set(NO_LTO_FLAGS "-fno-lto")
147  else()
148    set(NO_LTO_FLAGS "")
149  endif()
150
151  if(APPLE)
152    foreach(os ${LIB_OS})
153      # Strip out -msse3 if this isn't macOS.
154      list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
155      if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
156        list(REMOVE_ITEM LIB_CFLAGS "-msse3")
157      endif()
158      if(type STREQUAL "STATIC")
159        set(libname "${name}_${os}")
160      else()
161        set(libname "${name}_${os}_dynamic")
162        set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
163      endif()
164      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
165      if(LIB_ARCHS_${libname})
166        list(APPEND libnames ${libname})
167        set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
168        set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
169        set(sources_${libname} ${LIB_SOURCES})
170        format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
171      endif()
172    endforeach()
173  else()
174    foreach(arch ${LIB_ARCHS})
175      if(NOT CAN_TARGET_${arch})
176        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
177        return()
178      endif()
179      if(type STREQUAL "STATIC")
180        set(libname "${name}-${arch}")
181        set_output_name(output_name_${libname} ${name} ${arch})
182      else()
183        set(libname "${name}-dynamic-${arch}")
184        set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
185        set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
186        if(WIN32)
187          set_output_name(output_name_${libname} ${name}_dynamic ${arch})
188        else()
189          set_output_name(output_name_${libname} ${name} ${arch})
190        endif()
191      endif()
192      set(sources_${libname} ${LIB_SOURCES})
193      format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
194      set(libnames ${libnames} ${libname})
195      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS})
196    endforeach()
197  endif()
198
199  if(NOT libnames)
200    return()
201  endif()
202
203  if(LIB_PARENT_TARGET)
204    # If the parent targets aren't created we should create them
205    if(NOT TARGET ${LIB_PARENT_TARGET})
206      add_custom_target(${LIB_PARENT_TARGET})
207    endif()
208    if(NOT TARGET install-${LIB_PARENT_TARGET})
209      # The parent install target specifies the parent component to scrape up
210      # anything not installed by the individual install targets, and to handle
211      # installation when running the multi-configuration generators.
212      add_custom_target(install-${LIB_PARENT_TARGET}
213                        DEPENDS ${LIB_PARENT_TARGET}
214                        COMMAND "${CMAKE_COMMAND}"
215                                -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
216                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
217      add_custom_target(install-${LIB_PARENT_TARGET}-stripped
218                        DEPENDS ${LIB_PARENT_TARGET}
219                        COMMAND "${CMAKE_COMMAND}"
220                                -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET}
221                                -DCMAKE_INSTALL_DO_STRIP=1
222                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
223      set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES
224                            FOLDER "Compiler-RT Misc")
225      set_target_properties(install-${LIB_PARENT_TARGET}-stripped PROPERTIES
226                            FOLDER "Compiler-RT Misc")
227      add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET})
228      add_dependencies(install-compiler-rt-stripped install-${LIB_PARENT_TARGET}-stripped)
229    endif()
230  endif()
231
232  foreach(libname ${libnames})
233    # If you are using a multi-configuration generator we don't generate
234    # per-library install rules, so we fall back to the parent target COMPONENT
235    if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
236      set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
237    else()
238      set(COMPONENT_OPTION COMPONENT ${libname})
239    endif()
240
241    add_library(${libname} ${type} ${sources_${libname}})
242    set_target_compile_flags(${libname} ${extra_cflags_${libname}})
243    set_target_link_flags(${libname} ${extra_link_flags_${libname}})
244    set_property(TARGET ${libname} APPEND PROPERTY
245                COMPILE_DEFINITIONS ${LIB_DEFS})
246    set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
247    set_target_properties(${libname} PROPERTIES
248        OUTPUT_NAME ${output_name_${libname}})
249    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
250    if(LIB_LINK_LIBS)
251      target_link_libraries(${libname} ${LIB_LINK_LIBS})
252    endif()
253    if(${type} STREQUAL "SHARED")
254      if(COMMAND llvm_setup_rpath)
255        llvm_setup_rpath(${libname})
256      endif()
257      if(WIN32 AND NOT CYGWIN AND NOT MINGW)
258        set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
259        set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
260      endif()
261      if(APPLE)
262        # Ad-hoc sign the dylibs
263        add_custom_command(TARGET ${libname}
264          POST_BUILD
265          COMMAND codesign --sign - $<TARGET_FILE:${libname}>
266          WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
267        )
268      endif()
269    endif()
270    install(TARGETS ${libname}
271      ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
272              ${COMPONENT_OPTION}
273      LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
274              ${COMPONENT_OPTION}
275      RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
276              ${COMPONENT_OPTION})
277
278    # We only want to generate per-library install targets if you aren't using
279    # an IDE because the extra targets get cluttered in IDEs.
280    if(NOT CMAKE_CONFIGURATION_TYPES)
281      add_custom_target(install-${libname}
282                        DEPENDS ${libname}
283                        COMMAND "${CMAKE_COMMAND}"
284                                -DCMAKE_INSTALL_COMPONENT=${libname}
285                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
286      add_custom_target(install-${libname}-stripped
287                        DEPENDS ${libname}
288                        COMMAND "${CMAKE_COMMAND}"
289                                -DCMAKE_INSTALL_COMPONENT=${libname}
290                                -DCMAKE_INSTALL_DO_STRIP=1
291                                -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
292      # If you have a parent target specified, we bind the new install target
293      # to the parent install target.
294      if(LIB_PARENT_TARGET)
295        add_dependencies(install-${LIB_PARENT_TARGET} install-${libname})
296        add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${libname}-stripped)
297      endif()
298    endif()
299    if(APPLE)
300      set_target_properties(${libname} PROPERTIES
301      OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
302    endif()
303
304    if(type STREQUAL "SHARED")
305      rt_externalize_debuginfo(${libname})
306    endif()
307  endforeach()
308  if(LIB_PARENT_TARGET)
309    add_dependencies(${LIB_PARENT_TARGET} ${libnames})
310  endif()
311endfunction()
312
313# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help
314# in compilation and linking of unittests.
315string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}")
316set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})
317
318# Unittests support.
319set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest)
320set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc)
321set(COMPILER_RT_GTEST_CFLAGS
322  -DGTEST_NO_LLVM_RAW_OSTREAM=1
323  -DGTEST_HAS_RTTI=0
324  -I${COMPILER_RT_GTEST_PATH}/include
325  -I${COMPILER_RT_GTEST_PATH}
326)
327
328append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS)
329append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS)
330
331if(MSVC)
332  # clang doesn't support exceptions on Windows yet.
333  list(APPEND COMPILER_RT_UNITTEST_CFLAGS -D_HAS_EXCEPTIONS=0)
334
335  # We should teach clang to understand "#pragma intrinsic", see PR19898.
336  list(APPEND COMPILER_RT_UNITTEST_CFLAGS -Wno-undefined-inline)
337
338  # Clang doesn't support SEH on Windows yet.
339  list(APPEND COMPILER_RT_GTEST_CFLAGS -DGTEST_HAS_SEH=0)
340
341  # gtest use a lot of stuff marked as deprecated on Windows.
342  list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations)
343endif()
344
345# Compile and register compiler-rt tests.
346# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
347#                           <test architecture>
348#                           KIND <custom prefix>
349#                           SUBDIR <subdirectory for testing binary>
350#                           SOURCES <sources to compile>
351#                           RUNTIME <tests runtime to link in>
352#                           CFLAGS <compile-time flags>
353#                           COMPILE_DEPS <compile-time dependencies>
354#                           DEPS <dependencies>
355#                           LINK_FLAGS <flags to use during linking>
356# )
357function(generate_compiler_rt_tests test_objects test_suite testname arch)
358  cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
359    "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
360
361  foreach(source ${TEST_SOURCES})
362    sanitizer_test_compile(
363      "${test_objects}" "${source}" "${arch}"
364      KIND ${TEST_KIND}
365      COMPILE_DEPS ${TEST_COMPILE_DEPS}
366      DEPS ${TEST_DEPS}
367      CFLAGS ${TEST_CFLAGS}
368      )
369  endforeach()
370
371  set(TEST_DEPS ${${test_objects}})
372
373  if(NOT "${TEST_RUNTIME}" STREQUAL "")
374    list(APPEND TEST_DEPS ${TEST_RUNTIME})
375    list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
376  endif()
377
378  add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
379    SUBDIR ${TEST_SUBDIR}
380    OBJECTS ${${test_objects}}
381    DEPS ${TEST_DEPS}
382    LINK_FLAGS ${TEST_LINK_FLAGS}
383    )
384  set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
385endfunction()
386
387# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
388# using specified link flags. Make executable a part of provided
389# test_suite.
390# add_compiler_rt_test(<test_suite> <test_name> <arch>
391#                      SUBDIR <subdirectory for binary>
392#                      OBJECTS <object files>
393#                      DEPS <deps (e.g. runtime libs)>
394#                      LINK_FLAGS <link flags>)
395function(add_compiler_rt_test test_suite test_name arch)
396  cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
397  set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
398  if(TEST_SUBDIR)
399    set(output_dir "${output_dir}/${TEST_SUBDIR}")
400  endif()
401  set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
402  file(MAKE_DIRECTORY "${output_dir}")
403  set(output_bin "${output_dir}/${test_name}")
404  if(MSVC)
405    set(output_bin "${output_bin}.exe")
406  endif()
407
408  # Use host compiler in a standalone build, and just-built Clang otherwise.
409  if(NOT COMPILER_RT_STANDALONE_BUILD)
410    list(APPEND TEST_DEPS clang)
411  endif()
412
413  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
414  list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
415
416  # If we're not on MSVC, include the linker flags from CMAKE but override them
417  # with the provided link flags. This ensures that flags which are required to
418  # link programs at all are included, but the changes needed for the test
419  # trump. With MSVC we can't do that because CMake is set up to run link.exe
420  # when linking, not the compiler. Here, we hack it to use the compiler
421  # because we want to use -fsanitize flags.
422  if(NOT MSVC)
423    set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
424    separate_arguments(TEST_LINK_FLAGS)
425  endif()
426  add_custom_command(
427    OUTPUT "${output_bin}"
428    COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
429            ${TEST_LINK_FLAGS}
430    DEPENDS ${TEST_DEPS}
431    )
432  add_custom_target(T${test_name} DEPENDS "${output_bin}")
433  set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
434
435  # Make the test suite depend on the binary.
436  add_dependencies(${test_suite} T${test_name})
437endfunction()
438
439macro(add_compiler_rt_resource_file target_name file_name component)
440  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
441  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
442  add_custom_command(OUTPUT ${dst_file}
443    DEPENDS ${src_file}
444    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
445    COMMENT "Copying ${file_name}...")
446  add_custom_target(${target_name} DEPENDS ${dst_file})
447  # Install in Clang resource directory.
448  install(FILES ${file_name}
449    DESTINATION ${COMPILER_RT_INSTALL_PATH}/share
450    COMPONENT ${component})
451  add_dependencies(${component} ${target_name})
452
453  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
454endmacro()
455
456macro(add_compiler_rt_script name)
457  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
458  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
459  add_custom_command(OUTPUT ${dst}
460    DEPENDS ${src}
461    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
462    COMMENT "Copying ${name}...")
463  add_custom_target(${name} DEPENDS ${dst})
464  install(FILES ${dst}
465    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
466    DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
467endmacro(add_compiler_rt_script src name)
468
469# Builds custom version of libc++ and installs it in <prefix>.
470# Can be used to build sanitized versions of libc++ for running unit tests.
471# add_custom_libcxx(<name> <prefix>
472#                   DEPS <list of build deps>
473#                   CFLAGS <list of compile flags>
474#                   USE_TOOLCHAIN)
475macro(add_custom_libcxx name prefix)
476  if(NOT COMPILER_RT_LIBCXX_PATH)
477    message(FATAL_ERROR "libcxx not found!")
478  endif()
479
480  cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
481
482  if(LIBCXX_USE_TOOLCHAIN)
483    set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
484                      -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
485    if(NOT COMPILER_RT_STANDALONE_BUILD)
486      set(toolchain_deps $<TARGET_FILE:clang>)
487      set(force_deps DEPENDS $<TARGET_FILE:clang>)
488    endif()
489  else()
490    set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
491                      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
492  endif()
493
494  set(STAMP_DIR ${prefix}-stamps/)
495  set(BINARY_DIR ${prefix}-bins/)
496
497  add_custom_target(${name}-clear
498    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
499    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
500    COMMENT "Clobbering ${name} build and stamp directories"
501    USES_TERMINAL
502    )
503
504  add_custom_command(
505    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
506    DEPENDS ${LIBCXX_DEPS} ${toolchain_deps}
507    COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
508    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
509    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
510    COMMENT "Clobbering bootstrap build and stamp directories"
511    )
512
513  add_custom_target(${name}-clobber
514    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
515
516  set(PASSTHROUGH_VARIABLES
517    CMAKE_C_COMPILER_TARGET
518    CMAKE_CXX_COMPILER_TARGET
519    CMAKE_INSTALL_PREFIX
520    CMAKE_MAKE_PROGRAM
521    CMAKE_LINKER
522    CMAKE_AR
523    CMAKE_RANLIB
524    CMAKE_NM
525    CMAKE_OBJCOPY
526    CMAKE_OBJDUMP
527    CMAKE_STRIP
528    CMAKE_SYSROOT
529    CMAKE_SYSTEM_NAME)
530  foreach(variable ${PASSTHROUGH_VARIABLES})
531    if(${variable})
532      list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${${variable}})
533    endif()
534  endforeach()
535
536  string(REPLACE ";" " " FLAGS_STRING "${LIBCXX_CFLAGS}")
537  set(LIBCXX_C_FLAGS "${FLAGS_STRING}")
538  set(LIBCXX_CXX_FLAGS "${FLAGS_STRING}")
539
540  ExternalProject_Add(${name}
541    DEPENDS ${name}-clobber ${LIBCXX_DEPS}
542    PREFIX ${prefix}
543    SOURCE_DIR ${COMPILER_RT_LIBCXX_PATH}
544    STAMP_DIR ${STAMP_DIR}
545    BINARY_DIR ${BINARY_DIR}
546    CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES}
547               ${compiler_args}
548               -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS}
549               -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS}
550               -DCMAKE_BUILD_TYPE=Release
551               -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
552               -DLLVM_BINARY_DIR=${prefix}
553               -DLLVM_LIBRARY_OUTPUT_INTDIR=${prefix}/lib
554               -DLIBCXX_STANDALONE_BUILD=ON
555               ${LIBCXX_CMAKE_ARGS}
556    INSTALL_COMMAND ""
557    STEP_TARGETS configure build
558    BUILD_ALWAYS 1
559    USES_TERMINAL_CONFIGURE 1
560    USES_TERMINAL_BUILD 1
561    USES_TERMINAL_INSTALL 1
562    EXCLUDE_FROM_ALL TRUE
563    )
564
565  if (CMAKE_GENERATOR MATCHES "Make")
566    set(run_clean "$(MAKE)" "-C" "${BINARY_DIR}" "clean")
567  else()
568    set(run_clean ${CMAKE_COMMAND} --build ${BINARY_DIR} --target clean
569                                   --config "$<CONFIGURATION>")
570  endif()
571
572  ExternalProject_Add_Step(${name} clean
573    COMMAND ${run_clean}
574    COMMENT "Cleaning ${name}..."
575    DEPENDEES configure
576    ${force_deps}
577    WORKING_DIRECTORY ${BINARY_DIR}
578    EXCLUDE_FROM_MAIN 1
579    USES_TERMINAL 1
580    )
581  ExternalProject_Add_StepTargets(${name} clean)
582
583  if(LIBCXX_USE_TOOLCHAIN)
584    add_dependencies(${name}-clean ${name}-clobber)
585    set_target_properties(${name}-clean PROPERTIES
586      SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
587  endif()
588endmacro()
589
590function(rt_externalize_debuginfo name)
591  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
592    return()
593  endif()
594
595  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
596    set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
597  endif()
598
599  if(APPLE)
600    if(CMAKE_CXX_FLAGS MATCHES "-flto"
601      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
602
603      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
604      set_property(TARGET ${name} APPEND_STRING PROPERTY
605        LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
606    endif()
607    add_custom_command(TARGET ${name} POST_BUILD
608      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
609      ${strip_command})
610  else()
611    message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
612  endif()
613endfunction()
614
615
616# Configure lit configuration files, including compiler-rt specific variables.
617function(configure_compiler_rt_lit_site_cfg input output)
618  set_llvm_build_mode()
619
620  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
621  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
622
623  configure_lit_site_cfg(${input} ${output})
624endfunction()
625