1include(ExternalProject)
2include(CompilerRTUtils)
3include(HandleCompilerRT)
4
5function(set_target_output_directories target output_dir)
6  # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators
7  # append a per-configuration subdirectory to the specified directory.
8  # To avoid the appended folder, the configuration specific variable must be
9  # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}':
10  # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ...
11  if(CMAKE_CONFIGURATION_TYPES)
12    foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
13      string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
14      set_target_properties("${target}" PROPERTIES
15          "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
16          "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
17          "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir})
18    endforeach()
19  else()
20    set_target_properties("${target}" PROPERTIES
21        ARCHIVE_OUTPUT_DIRECTORY ${output_dir}
22        LIBRARY_OUTPUT_DIRECTORY ${output_dir}
23        RUNTIME_OUTPUT_DIRECTORY ${output_dir})
24  endif()
25endfunction()
26
27# Tries to add an "object library" target for a given list of OSs and/or
28# architectures with name "<name>.<arch>" for non-Darwin platforms if
29# architecture can be targeted, and "<name>.<os>" for Darwin platforms.
30# add_compiler_rt_object_libraries(<name>
31#                                  OS <os names>
32#                                  ARCHS <architectures>
33#                                  SOURCES <source files>
34#                                  CFLAGS <compile flags>
35#                                  DEFS <compile definitions>
36#                                  DEPS <dependencies>
37#                                  ADDITIONAL_HEADERS <header files>)
38function(add_compiler_rt_object_libraries name)
39  cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS;ADDITIONAL_HEADERS"
40    ${ARGN})
41  set(libnames)
42  if(APPLE)
43    foreach(os ${LIB_OS})
44      set(libname "${name}.${os}")
45      set(libnames ${libnames} ${libname})
46      set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
47      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
48    endforeach()
49  else()
50    foreach(arch ${LIB_ARCHS})
51      set(libname "${name}.${arch}")
52      set(libnames ${libnames} ${libname})
53      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
54      if(NOT CAN_TARGET_${arch})
55        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
56        return()
57      endif()
58    endforeach()
59  endif()
60
61  # Add headers to LIB_SOURCES for IDEs
62  compiler_rt_process_sources(LIB_SOURCES
63    ${LIB_SOURCES}
64    ADDITIONAL_HEADERS
65      ${LIB_ADDITIONAL_HEADERS}
66  )
67
68  foreach(libname ${libnames})
69    add_library(${libname} OBJECT ${LIB_SOURCES})
70    if(LIB_DEPS)
71      add_dependencies(${libname} ${LIB_DEPS})
72    endif()
73
74    # Strip out -msse3 if this isn't macOS.
75    set(target_flags ${LIB_CFLAGS})
76    if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*")
77      list(REMOVE_ITEM target_flags "-msse3")
78    endif()
79
80    set_target_compile_flags(${libname}
81      ${extra_cflags_${libname}} ${target_flags})
82    set_property(TARGET ${libname} APPEND PROPERTY
83      COMPILE_DEFINITIONS ${LIB_DEFS})
84    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
85    if(APPLE)
86      set_target_properties(${libname} PROPERTIES
87        OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
88    endif()
89  endforeach()
90endfunction()
91
92# Takes a list of object library targets, and a suffix and appends the proper
93# TARGET_OBJECTS string to the output variable.
94# format_object_libs(<output> <suffix> ...)
95macro(format_object_libs output suffix)
96  foreach(lib ${ARGN})
97    list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
98  endforeach()
99endmacro()
100
101function(add_compiler_rt_component name)
102  add_custom_target(${name})
103  set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
104  if(COMMAND runtime_register_component)
105    runtime_register_component(${name})
106  endif()
107  add_dependencies(compiler-rt ${name})
108endfunction()
109
110function(add_asm_sources output)
111  set(${output} ${ARGN} PARENT_SCOPE)
112  # CMake doesn't pass the correct architecture for Apple prior to CMake 3.19. https://gitlab.kitware.com/cmake/cmake/-/issues/20771
113  # MinGW didn't work correctly with assembly prior to CMake 3.17. https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4287 and https://reviews.llvm.org/rGb780df052dd2b246a760d00e00f7de9ebdab9d09
114  # Workaround these two issues by compiling as C.
115  # Same workaround used in libunwind. Also update there if changed here.
116  if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
117    set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C)
118  endif()
119endfunction()
120
121macro(set_output_name output name arch)
122  if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
123    set(${output} ${name})
124  else()
125    if(ANDROID AND ${arch} STREQUAL "i386")
126      set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
127    else()
128      set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
129    endif()
130  endif()
131endmacro()
132
133# Adds static or shared runtime for a list of architectures and operating
134# systems and puts it in the proper directory in the build and install trees.
135# add_compiler_rt_runtime(<name>
136#                         {OBJECT|STATIC|SHARED}
137#                         ARCHS <architectures>
138#                         OS <os list>
139#                         SOURCES <source files>
140#                         CFLAGS <compile flags>
141#                         LINK_FLAGS <linker flags>
142#                         DEFS <compile definitions>
143#                         DEPS <dependencies>
144#                         LINK_LIBS <linked libraries> (only for shared library)
145#                         OBJECT_LIBS <object libraries to use as sources>
146#                         PARENT_TARGET <convenience parent target>
147#                         ADDITIONAL_HEADERS <header files>)
148function(add_compiler_rt_runtime name type)
149  if(NOT type MATCHES "^(OBJECT|STATIC|SHARED)$")
150    message(FATAL_ERROR "type argument must be OBJECT, STATIC or SHARED")
151    return()
152  endif()
153  cmake_parse_arguments(LIB
154    ""
155    "PARENT_TARGET"
156    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS"
157    ${ARGN})
158  set(libnames)
159  # Until we support this some other way, build compiler-rt runtime without LTO
160  # to allow non-LTO projects to link with it.
161  if(COMPILER_RT_HAS_FNO_LTO_FLAG)
162    set(NO_LTO_FLAGS "-fno-lto")
163  else()
164    set(NO_LTO_FLAGS "")
165  endif()
166
167  # By default do not instrument or use profdata for compiler-rt.
168  set(NO_PGO_FLAGS "")
169  if(NOT COMPILER_RT_ENABLE_PGO)
170    if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
171      list(APPEND NO_PGO_FLAGS "-fno-profile-instr-use")
172    endif()
173    if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
174      list(APPEND NO_PGO_FLAGS "-fno-profile-generate")
175    elseif(LLVM_BUILD_INSTRUMENTED AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
176      list(APPEND NO_PGO_FLAGS "-fno-profile-instr-generate")
177    endif()
178  endif()
179
180  list(LENGTH LIB_SOURCES LIB_SOURCES_LENGTH)
181  if (${LIB_SOURCES_LENGTH} GREATER 0)
182    # Add headers to LIB_SOURCES for IDEs. It doesn't make sense to
183    # do this for a runtime library that only consists of OBJECT
184    # libraries, so only add the headers when source files are present.
185    compiler_rt_process_sources(LIB_SOURCES
186      ${LIB_SOURCES}
187      ADDITIONAL_HEADERS
188        ${LIB_ADDITIONAL_HEADERS}
189    )
190  endif()
191
192  if(APPLE)
193    foreach(os ${LIB_OS})
194      # Strip out -msse3 if this isn't macOS.
195      list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
196      if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
197        list(REMOVE_ITEM LIB_CFLAGS "-msse3")
198      endif()
199      if(type STREQUAL "STATIC")
200        set(libname "${name}_${os}")
201      else()
202        set(libname "${name}_${os}_dynamic")
203        set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
204      endif()
205      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
206      if(LIB_ARCHS_${libname})
207        list(APPEND libnames ${libname})
208        set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
209        set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
210        set(sources_${libname} ${LIB_SOURCES})
211        format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
212        get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir_${libname})
213        get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_${libname})
214      endif()
215    endforeach()
216  else()
217    foreach(arch ${LIB_ARCHS})
218      if(NOT CAN_TARGET_${arch})
219        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
220        return()
221      endif()
222      if(type STREQUAL "OBJECT")
223        set(libname "${name}-${arch}")
224        set_output_name(output_name_${libname} ${name}${COMPILER_RT_OS_SUFFIX} ${arch})
225      elseif(type STREQUAL "STATIC")
226        set(libname "${name}-${arch}")
227        set_output_name(output_name_${libname} ${name} ${arch})
228      else()
229        set(libname "${name}-dynamic-${arch}")
230        set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
231        set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
232        if(WIN32)
233          set_output_name(output_name_${libname} ${name}_dynamic ${arch})
234        else()
235          set_output_name(output_name_${libname} ${name} ${arch})
236        endif()
237      endif()
238      if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT" AND
239         NOT name STREQUAL "clang_rt.builtins")
240        get_compiler_rt_target(${arch} target)
241        find_compiler_rt_library(builtins ${target} builtins_${libname})
242        if(builtins_${libname} STREQUAL "NOTFOUND")
243          message(FATAL_ERROR "Cannot find builtins library for the target architecture")
244        endif()
245      endif()
246      set(sources_${libname} ${LIB_SOURCES})
247      format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
248      set(libnames ${libnames} ${libname})
249      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
250      get_compiler_rt_output_dir(${arch} output_dir_${libname})
251      get_compiler_rt_install_dir(${arch} install_dir_${libname})
252    endforeach()
253  endif()
254
255  if(NOT libnames)
256    return()
257  endif()
258
259  if(LIB_PARENT_TARGET)
260    # If the parent targets aren't created we should create them
261    if(NOT TARGET ${LIB_PARENT_TARGET})
262      add_custom_target(${LIB_PARENT_TARGET})
263      set_target_properties(${LIB_PARENT_TARGET} PROPERTIES
264                            FOLDER "Compiler-RT Misc")
265    endif()
266  endif()
267
268  foreach(libname ${libnames})
269    # If you are using a multi-configuration generator we don't generate
270    # per-library install rules, so we fall back to the parent target COMPONENT
271    if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
272      set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
273    else()
274      set(COMPONENT_OPTION COMPONENT ${libname})
275    endif()
276
277    if(type STREQUAL "OBJECT")
278      if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
279        list(APPEND extra_cflags_${libname} "--target=${CMAKE_C_COMPILER_TARGET}")
280      endif()
281      if(CMAKE_SYSROOT)
282        list(APPEND extra_cflags_${libname} "--sysroot=${CMAKE_SYSROOT}")
283      endif()
284      string(REPLACE ";" " " extra_cflags_${libname} "${extra_cflags_${libname}}")
285      string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
286             ${CMAKE_C_COMPILE_OBJECT})
287      set(compile_command_${libname} "${CMAKE_C_COMPILE_OBJECT}")
288
289      set(output_file_${libname} ${output_name_${libname}}${CMAKE_C_OUTPUT_EXTENSION})
290      foreach(substitution ${substitutions})
291        if(substitution STREQUAL "<CMAKE_C_COMPILER>")
292          string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
293                 compile_command_${libname} ${compile_command_${libname}})
294        elseif(substitution STREQUAL "<OBJECT>")
295          string(REPLACE "<OBJECT>" "${output_dir_${libname}}/${output_file_${libname}}"
296                 compile_command_${libname} ${compile_command_${libname}})
297        elseif(substitution STREQUAL "<SOURCE>")
298          string(REPLACE "<SOURCE>" "${sources_${libname}}"
299                 compile_command_${libname} ${compile_command_${libname}})
300        elseif(substitution STREQUAL "<FLAGS>")
301          string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_cflags_${libname}}"
302                 compile_command_${libname} ${compile_command_${libname}})
303        else()
304          string(REPLACE "${substitution}" "" compile_command_${libname}
305                 ${compile_command_${libname}})
306        endif()
307      endforeach()
308      separate_arguments(compile_command_${libname})
309      add_custom_command(
310          OUTPUT ${output_dir_${libname}}/${output_file_${libname}}
311          COMMAND ${compile_command_${libname}}
312          DEPENDS ${sources_${libname}}
313          COMMENT "Building C object ${output_file_${libname}}")
314      add_custom_target(${libname} DEPENDS ${output_dir_${libname}}/${output_file_${libname}})
315      install(FILES ${output_dir_${libname}}/${output_file_${libname}}
316        DESTINATION ${install_dir_${libname}}
317        ${COMPONENT_OPTION})
318    else()
319      add_library(${libname} ${type} ${sources_${libname}})
320      set_target_compile_flags(${libname} ${extra_cflags_${libname}})
321      set_target_link_flags(${libname} ${extra_link_flags_${libname}})
322      set_property(TARGET ${libname} APPEND PROPERTY
323                   COMPILE_DEFINITIONS ${LIB_DEFS})
324      set_target_output_directories(${libname} ${output_dir_${libname}})
325      install(TARGETS ${libname}
326        ARCHIVE DESTINATION ${install_dir_${libname}}
327                ${COMPONENT_OPTION}
328        LIBRARY DESTINATION ${install_dir_${libname}}
329                ${COMPONENT_OPTION}
330        RUNTIME DESTINATION ${install_dir_${libname}}
331                ${COMPONENT_OPTION})
332    endif()
333    if(LIB_DEPS)
334      add_dependencies(${libname} ${LIB_DEPS})
335    endif()
336    set_target_properties(${libname} PROPERTIES
337        OUTPUT_NAME ${output_name_${libname}})
338    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
339    if(LIB_LINK_LIBS)
340      target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS})
341    endif()
342    if(builtins_${libname})
343      target_link_libraries(${libname} PRIVATE ${builtins_${libname}})
344    endif()
345    if(${type} STREQUAL "SHARED")
346      if(COMMAND llvm_setup_rpath)
347        llvm_setup_rpath(${libname})
348      endif()
349      if(WIN32 AND NOT CYGWIN AND NOT MINGW)
350        set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
351        set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
352      endif()
353      if(APPLE)
354        # Ad-hoc sign the dylibs
355        add_custom_command(TARGET ${libname}
356          POST_BUILD
357          COMMAND codesign --sign - $<TARGET_FILE:${libname}>
358          WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
359        )
360      endif()
361    endif()
362
363    set(parent_target_arg)
364    if(LIB_PARENT_TARGET)
365      set(parent_target_arg PARENT_TARGET ${LIB_PARENT_TARGET})
366    endif()
367    add_compiler_rt_install_targets(${libname} ${parent_target_arg})
368
369    if(APPLE)
370      set_target_properties(${libname} PROPERTIES
371      OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
372    endif()
373
374    if(type STREQUAL "SHARED")
375      rt_externalize_debuginfo(${libname})
376    endif()
377  endforeach()
378  if(LIB_PARENT_TARGET)
379    add_dependencies(${LIB_PARENT_TARGET} ${libnames})
380  endif()
381endfunction()
382
383# Compile and register compiler-rt tests.
384# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
385#                           <test architecture>
386#                           KIND <custom prefix>
387#                           SUBDIR <subdirectory for testing binary>
388#                           SOURCES <sources to compile>
389#                           RUNTIME <tests runtime to link in>
390#                           CFLAGS <compile-time flags>
391#                           COMPILE_DEPS <compile-time dependencies>
392#                           DEPS <dependencies>
393#                           LINK_FLAGS <flags to use during linking>
394# )
395function(generate_compiler_rt_tests test_objects test_suite testname arch)
396  cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
397    "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
398
399  foreach(source ${TEST_SOURCES})
400    sanitizer_test_compile(
401      "${test_objects}" "${source}" "${arch}"
402      KIND ${TEST_KIND}
403      COMPILE_DEPS ${TEST_COMPILE_DEPS}
404      DEPS ${TEST_DEPS}
405      CFLAGS ${TEST_CFLAGS}
406      )
407  endforeach()
408
409  set(TEST_DEPS ${${test_objects}})
410
411  if(NOT "${TEST_RUNTIME}" STREQUAL "")
412    list(APPEND TEST_DEPS ${TEST_RUNTIME})
413    list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
414  endif()
415
416  add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
417    SUBDIR ${TEST_SUBDIR}
418    OBJECTS ${${test_objects}}
419    DEPS ${TEST_DEPS}
420    LINK_FLAGS ${TEST_LINK_FLAGS}
421    )
422  set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
423endfunction()
424
425# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
426# using specified link flags. Make executable a part of provided
427# test_suite.
428# add_compiler_rt_test(<test_suite> <test_name> <arch>
429#                      SUBDIR <subdirectory for binary>
430#                      OBJECTS <object files>
431#                      DEPS <deps (e.g. runtime libs)>
432#                      LINK_FLAGS <link flags>)
433function(add_compiler_rt_test test_suite test_name arch)
434  cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
435  set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
436  if(TEST_SUBDIR)
437    set(output_dir "${output_dir}/${TEST_SUBDIR}")
438  endif()
439  set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
440  file(MAKE_DIRECTORY "${output_dir}")
441  set(output_bin "${output_dir}/${test_name}")
442  if(MSVC)
443    set(output_bin "${output_bin}.exe")
444  endif()
445
446  # Use host compiler in a standalone build, and just-built Clang otherwise.
447  if(NOT COMPILER_RT_STANDALONE_BUILD)
448    list(APPEND TEST_DEPS clang)
449  endif()
450
451  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
452  list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
453
454  # If we're not on MSVC, include the linker flags from CMAKE but override them
455  # with the provided link flags. This ensures that flags which are required to
456  # link programs at all are included, but the changes needed for the test
457  # trump. With MSVC we can't do that because CMake is set up to run link.exe
458  # when linking, not the compiler. Here, we hack it to use the compiler
459  # because we want to use -fsanitize flags.
460
461  # Only add CMAKE_EXE_LINKER_FLAGS when in a standalone bulid.
462  # Or else CMAKE_EXE_LINKER_FLAGS contains flags for build compiler of Clang/llvm.
463  # This might not be the same as what the COMPILER_RT_TEST_COMPILER supports.
464  # eg: the build compiler use lld linker and we build clang with default ld linker
465  # then to be tested clang will complain about lld options like --color-diagnostics.
466  if(NOT MSVC AND COMPILER_RT_STANDALONE_BUILD)
467    set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
468    separate_arguments(TEST_LINK_FLAGS)
469  endif()
470  if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_HAS_LLD AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
471    # CMAKE_EXE_LINKER_FLAGS may contain -fuse=lld
472    # FIXME: -DLLVM_ENABLE_LLD=ON and -DLLVM_ENABLE_PROJECTS without lld case.
473    list(APPEND TEST_DEPS lld)
474  endif()
475  add_custom_command(
476    OUTPUT "${output_bin}"
477    COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
478            ${TEST_LINK_FLAGS}
479    DEPENDS ${TEST_DEPS}
480    )
481  add_custom_target(T${test_name} DEPENDS "${output_bin}")
482  set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
483
484  # Make the test suite depend on the binary.
485  add_dependencies(${test_suite} T${test_name})
486endfunction()
487
488macro(add_compiler_rt_resource_file target_name file_name component)
489  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
490  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
491  add_custom_command(OUTPUT ${dst_file}
492    DEPENDS ${src_file}
493    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
494    COMMENT "Copying ${file_name}...")
495  add_custom_target(${target_name} DEPENDS ${dst_file})
496  # Install in Clang resource directory.
497  install(FILES ${file_name}
498    DESTINATION ${COMPILER_RT_INSTALL_PATH}/share
499    COMPONENT ${component})
500  add_dependencies(${component} ${target_name})
501
502  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
503endmacro()
504
505macro(add_compiler_rt_script name)
506  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
507  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
508  add_custom_command(OUTPUT ${dst}
509    DEPENDS ${src}
510    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
511    COMMENT "Copying ${name}...")
512  add_custom_target(${name} DEPENDS ${dst})
513  install(FILES ${dst}
514    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
515    DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin)
516endmacro(add_compiler_rt_script src name)
517
518# Builds custom version of libc++ and installs it in <prefix>.
519# Can be used to build sanitized versions of libc++ for running unit tests.
520# add_custom_libcxx(<name> <prefix>
521#                   DEPS <list of build deps>
522#                   CFLAGS <list of compile flags>
523#                   USE_TOOLCHAIN)
524macro(add_custom_libcxx name prefix)
525  if(NOT COMPILER_RT_LIBCXX_PATH)
526    message(FATAL_ERROR "libcxx not found!")
527  endif()
528  if(NOT COMPILER_RT_LIBCXXABI_PATH)
529    message(FATAL_ERROR "libcxxabi not found!")
530  endif()
531
532  cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
533
534  if(LIBCXX_USE_TOOLCHAIN)
535    set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
536                      -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
537    if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
538      set(toolchain_deps $<TARGET_FILE:clang>)
539      set(force_deps DEPENDS $<TARGET_FILE:clang>)
540    endif()
541  else()
542    set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
543                      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
544  endif()
545
546  set(STAMP_DIR ${prefix}-stamps/)
547  set(BINARY_DIR ${prefix}-bins/)
548
549  add_custom_target(${name}-clear
550    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
551    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
552    COMMENT "Clobbering ${name} build and stamp directories"
553    USES_TERMINAL
554    )
555  set_target_properties(${name}-clear PROPERTIES FOLDER "Compiler-RT Misc")
556
557  add_custom_command(
558    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
559    DEPENDS ${LIBCXX_DEPS} ${toolchain_deps}
560    COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
561    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
562    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
563    COMMENT "Clobbering bootstrap build and stamp directories"
564    )
565
566  add_custom_target(${name}-clobber
567    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
568  set_target_properties(${name}-clobber PROPERTIES FOLDER "Compiler-RT Misc")
569
570  set(PASSTHROUGH_VARIABLES
571    CMAKE_C_COMPILER_TARGET
572    CMAKE_CXX_COMPILER_TARGET
573    CMAKE_SHARED_LINKER_FLAGS
574    CMAKE_MODULE_LINKER_FLAGS
575    CMAKE_EXE_LINKER_FLAGS
576    CMAKE_INSTALL_PREFIX
577    CMAKE_MAKE_PROGRAM
578    CMAKE_LINKER
579    CMAKE_AR
580    CMAKE_RANLIB
581    CMAKE_NM
582    CMAKE_OBJCOPY
583    CMAKE_OBJDUMP
584    CMAKE_STRIP
585    CMAKE_SYSROOT
586    LIBCXX_HAS_MUSL_LIBC
587    PYTHON_EXECUTABLE
588    Python3_EXECUTABLE
589    Python2_EXECUTABLE
590    CMAKE_SYSTEM_NAME)
591  foreach(variable ${PASSTHROUGH_VARIABLES})
592    get_property(is_value_set CACHE ${variable} PROPERTY VALUE SET)
593    if(${is_value_set})
594      get_property(value CACHE ${variable} PROPERTY VALUE)
595      list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${value})
596    endif()
597  endforeach()
598
599  string(REPLACE ";" " " LIBCXX_C_FLAGS "${LIBCXX_CFLAGS}")
600  get_property(C_FLAGS CACHE CMAKE_C_FLAGS PROPERTY VALUE)
601  set(LIBCXX_C_FLAGS "${LIBCXX_C_FLAGS} ${C_FLAGS}")
602
603  string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CFLAGS}")
604  get_property(CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
605  set(LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS} ${CXX_FLAGS}")
606
607  ExternalProject_Add(${name}
608    DEPENDS ${name}-clobber ${LIBCXX_DEPS}
609    PREFIX ${prefix}
610    SOURCE_DIR ${COMPILER_RT_SOURCE_DIR}/cmake/Modules/CustomLibcxx
611    STAMP_DIR ${STAMP_DIR}
612    BINARY_DIR ${BINARY_DIR}
613    CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES}
614               ${compiler_args}
615               -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS}
616               -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS}
617               -DCMAKE_BUILD_TYPE=Release
618               -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
619               -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
620               -DLLVM_BINARY_DIR=${prefix}
621               -DLLVM_LIBRARY_OUTPUT_INTDIR=${prefix}/lib
622               -DCOMPILER_RT_LIBCXX_PATH=${COMPILER_RT_LIBCXX_PATH}
623               -DCOMPILER_RT_LIBCXXABI_PATH=${COMPILER_RT_LIBCXXABI_PATH}
624               ${LIBCXX_CMAKE_ARGS}
625    INSTALL_COMMAND ""
626    STEP_TARGETS configure build
627    BUILD_ALWAYS 1
628    USES_TERMINAL_CONFIGURE 1
629    USES_TERMINAL_BUILD 1
630    USES_TERMINAL_INSTALL 1
631    EXCLUDE_FROM_ALL TRUE
632    BUILD_BYPRODUCTS "${prefix}/lib/libc++.a" "${prefix}/lib/libc++abi.a"
633    )
634
635  if (CMAKE_GENERATOR MATCHES "Make")
636    set(run_clean "$(MAKE)" "-C" "${BINARY_DIR}" "clean")
637  else()
638    set(run_clean ${CMAKE_COMMAND} --build ${BINARY_DIR} --target clean
639                                   --config "$<CONFIG>")
640  endif()
641
642  ExternalProject_Add_Step(${name} clean
643    COMMAND ${run_clean}
644    COMMENT "Cleaning ${name}..."
645    DEPENDEES configure
646    ${force_deps}
647    WORKING_DIRECTORY ${BINARY_DIR}
648    EXCLUDE_FROM_MAIN 1
649    USES_TERMINAL 1
650    )
651  ExternalProject_Add_StepTargets(${name} clean)
652
653  if(LIBCXX_USE_TOOLCHAIN)
654    add_dependencies(${name}-clean ${name}-clobber)
655    set_target_properties(${name}-clean PROPERTIES
656      SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
657  endif()
658endmacro()
659
660function(rt_externalize_debuginfo name)
661  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
662    return()
663  endif()
664
665  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
666    set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
667  endif()
668
669  if(APPLE)
670    if(CMAKE_CXX_FLAGS MATCHES "-flto"
671      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
672
673      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
674      set_property(TARGET ${name} APPEND_STRING PROPERTY
675        LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
676    endif()
677    add_custom_command(TARGET ${name} POST_BUILD
678      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
679      ${strip_command})
680  else()
681    message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
682  endif()
683endfunction()
684
685
686# Configure lit configuration files, including compiler-rt specific variables.
687function(configure_compiler_rt_lit_site_cfg input output)
688  set_llvm_build_mode()
689
690  get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
691
692  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
693  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
694
695  configure_lit_site_cfg(${input} ${output})
696endfunction()
697