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    elseif("${arch}" MATCHES "^arm")
128      if(COMPILER_RT_DEFAULT_TARGET_ONLY)
129        set(triple "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")
130      else()
131        set(triple "${LLVM_TARGET_TRIPLE}")
132      endif()
133      # Except for baremetal, when using arch-suffixed runtime library names,
134      # clang only looks for libraries named "arm" or "armhf", see
135      # getArchNameForCompilerRTLib in clang. Therefore, try to inspect both
136      # the arch name and the triple if it seems like we're building an armhf
137      # target.
138      if (COMPILER_RT_BAREMETAL_BUILD)
139        set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
140      elseif ("${arch}" MATCHES "hf$" OR "${triple}" MATCHES "hf$")
141        set(${output} "${name}-armhf${COMPILER_RT_OS_SUFFIX}")
142      else()
143        set(${output} "${name}-arm${COMPILER_RT_OS_SUFFIX}")
144      endif()
145    else()
146      set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
147    endif()
148  endif()
149endmacro()
150
151# Adds static or shared runtime for a list of architectures and operating
152# systems and puts it in the proper directory in the build and install trees.
153# add_compiler_rt_runtime(<name>
154#                         {OBJECT|STATIC|SHARED|MODULE}
155#                         ARCHS <architectures>
156#                         OS <os list>
157#                         SOURCES <source files>
158#                         CFLAGS <compile flags>
159#                         LINK_FLAGS <linker flags>
160#                         DEFS <compile definitions>
161#                         DEPS <dependencies>
162#                         LINK_LIBS <linked libraries> (only for shared library)
163#                         OBJECT_LIBS <object libraries to use as sources>
164#                         PARENT_TARGET <convenience parent target>
165#                         ADDITIONAL_HEADERS <header files>)
166function(add_compiler_rt_runtime name type)
167  if(NOT type MATCHES "^(OBJECT|STATIC|SHARED|MODULE)$")
168    message(FATAL_ERROR
169            "type argument must be OBJECT, STATIC, SHARED or MODULE")
170    return()
171  endif()
172  cmake_parse_arguments(LIB
173    ""
174    "PARENT_TARGET"
175    "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS"
176    ${ARGN})
177  set(libnames)
178  # Until we support this some other way, build compiler-rt runtime without LTO
179  # to allow non-LTO projects to link with it.
180  if(COMPILER_RT_HAS_FNO_LTO_FLAG)
181    set(NO_LTO_FLAGS "-fno-lto")
182  else()
183    set(NO_LTO_FLAGS "")
184  endif()
185
186  # By default do not instrument or use profdata for compiler-rt.
187  set(NO_PGO_FLAGS "")
188  if(NOT COMPILER_RT_ENABLE_PGO)
189    if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
190      list(APPEND NO_PGO_FLAGS "-fno-profile-instr-use")
191    endif()
192    if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
193      list(APPEND NO_PGO_FLAGS "-fno-profile-generate")
194    elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
195      list(APPEND NO_PGO_FLAGS "-fno-profile-instr-generate")
196      if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
197        list(APPEND NO_PGO_FLAGS "-fno-coverage-mapping")
198      endif()
199    endif()
200  endif()
201
202  list(LENGTH LIB_SOURCES LIB_SOURCES_LENGTH)
203  if (${LIB_SOURCES_LENGTH} GREATER 0)
204    # Add headers to LIB_SOURCES for IDEs. It doesn't make sense to
205    # do this for a runtime library that only consists of OBJECT
206    # libraries, so only add the headers when source files are present.
207    compiler_rt_process_sources(LIB_SOURCES
208      ${LIB_SOURCES}
209      ADDITIONAL_HEADERS
210        ${LIB_ADDITIONAL_HEADERS}
211    )
212  endif()
213
214  if(APPLE)
215    foreach(os ${LIB_OS})
216      # Strip out -msse3 if this isn't macOS.
217      list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
218      if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
219        list(REMOVE_ITEM LIB_CFLAGS "-msse3")
220      endif()
221      if(type STREQUAL "STATIC")
222        set(libname "${name}_${os}")
223      else()
224        set(libname "${name}_${os}_dynamic")
225        set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
226      endif()
227      list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
228      if(LIB_ARCHS_${libname})
229        list(APPEND libnames ${libname})
230        set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
231        set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
232        set(sources_${libname} ${LIB_SOURCES})
233        format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
234        get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir_${libname})
235        get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_${libname})
236      endif()
237    endforeach()
238  else()
239    foreach(arch ${LIB_ARCHS})
240      if(NOT CAN_TARGET_${arch})
241        message(FATAL_ERROR "Architecture ${arch} can't be targeted")
242        return()
243      endif()
244      if(type STREQUAL "OBJECT")
245        set(libname "${name}-${arch}")
246        set_output_name(output_name_${libname} ${name}${COMPILER_RT_OS_SUFFIX} ${arch})
247      elseif(type STREQUAL "STATIC")
248        set(libname "${name}-${arch}")
249        set_output_name(output_name_${libname} ${name} ${arch})
250      else()
251        set(libname "${name}-dynamic-${arch}")
252        set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
253        set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
254        if(WIN32)
255          set_output_name(output_name_${libname} ${name}_dynamic ${arch})
256        else()
257          set_output_name(output_name_${libname} ${name} ${arch})
258        endif()
259      endif()
260      if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT" AND
261         NOT name STREQUAL "clang_rt.builtins")
262        get_compiler_rt_target(${arch} target)
263        find_compiler_rt_library(builtins builtins_${libname} TARGET ${target})
264        if(builtins_${libname} STREQUAL "NOTFOUND")
265          message(FATAL_ERROR "Cannot find builtins library for the target architecture")
266        endif()
267      endif()
268      set(sources_${libname} ${LIB_SOURCES})
269      format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
270      set(libnames ${libnames} ${libname})
271      set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
272      get_compiler_rt_output_dir(${arch} output_dir_${libname})
273      get_compiler_rt_install_dir(${arch} install_dir_${libname})
274    endforeach()
275  endif()
276
277  if(NOT libnames)
278    return()
279  endif()
280
281  if(LIB_PARENT_TARGET)
282    # If the parent targets aren't created we should create them
283    if(NOT TARGET ${LIB_PARENT_TARGET})
284      add_custom_target(${LIB_PARENT_TARGET})
285      set_target_properties(${LIB_PARENT_TARGET} PROPERTIES
286                            FOLDER "Compiler-RT Misc")
287    endif()
288  endif()
289
290  foreach(libname ${libnames})
291    # If you are using a multi-configuration generator we don't generate
292    # per-library install rules, so we fall back to the parent target COMPONENT
293    if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
294      set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
295    else()
296      set(COMPONENT_OPTION COMPONENT ${libname})
297    endif()
298
299    if(type STREQUAL "OBJECT")
300      if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
301        list(APPEND extra_cflags_${libname} "--target=${CMAKE_C_COMPILER_TARGET}")
302      endif()
303      if(CMAKE_SYSROOT)
304        list(APPEND extra_cflags_${libname} "--sysroot=${CMAKE_SYSROOT}")
305      endif()
306      string(REPLACE ";" " " extra_cflags_${libname} "${extra_cflags_${libname}}")
307      string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
308             ${CMAKE_C_COMPILE_OBJECT})
309      set(compile_command_${libname} "${CMAKE_C_COMPILE_OBJECT}")
310
311      set(output_file_${libname} ${output_name_${libname}}${CMAKE_C_OUTPUT_EXTENSION})
312      foreach(substitution ${substitutions})
313        if(substitution STREQUAL "<CMAKE_C_COMPILER>")
314          string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
315                 compile_command_${libname} ${compile_command_${libname}})
316        elseif(substitution STREQUAL "<OBJECT>")
317          string(REPLACE "<OBJECT>" "${output_dir_${libname}}/${output_file_${libname}}"
318                 compile_command_${libname} ${compile_command_${libname}})
319        elseif(substitution STREQUAL "<SOURCE>")
320          string(REPLACE "<SOURCE>" "${sources_${libname}}"
321                 compile_command_${libname} ${compile_command_${libname}})
322        elseif(substitution STREQUAL "<FLAGS>")
323          string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_cflags_${libname}}"
324                 compile_command_${libname} ${compile_command_${libname}})
325        else()
326          string(REPLACE "${substitution}" "" compile_command_${libname}
327                 ${compile_command_${libname}})
328        endif()
329      endforeach()
330      separate_arguments(compile_command_${libname})
331      add_custom_command(
332          OUTPUT ${output_dir_${libname}}/${output_file_${libname}}
333          COMMAND ${compile_command_${libname}}
334          DEPENDS ${sources_${libname}}
335          COMMENT "Building C object ${output_file_${libname}}")
336      add_custom_target(${libname} DEPENDS ${output_dir_${libname}}/${output_file_${libname}})
337      install(FILES ${output_dir_${libname}}/${output_file_${libname}}
338        DESTINATION ${install_dir_${libname}}
339        ${COMPONENT_OPTION})
340    else()
341      add_library(${libname} ${type} ${sources_${libname}})
342      set_target_compile_flags(${libname} ${extra_cflags_${libname}})
343      set_target_link_flags(${libname} ${extra_link_flags_${libname}})
344      set_property(TARGET ${libname} APPEND PROPERTY
345                   COMPILE_DEFINITIONS ${LIB_DEFS})
346      set_target_output_directories(${libname} ${output_dir_${libname}})
347      install(TARGETS ${libname}
348        ARCHIVE DESTINATION ${install_dir_${libname}}
349                ${COMPONENT_OPTION}
350        LIBRARY DESTINATION ${install_dir_${libname}}
351                ${COMPONENT_OPTION}
352        RUNTIME DESTINATION ${install_dir_${libname}}
353                ${COMPONENT_OPTION})
354    endif()
355    if(LIB_DEPS)
356      add_dependencies(${libname} ${LIB_DEPS})
357    endif()
358    set_target_properties(${libname} PROPERTIES
359        OUTPUT_NAME ${output_name_${libname}})
360    set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
361    if(LIB_LINK_LIBS)
362      target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS})
363    endif()
364    if(builtins_${libname})
365      target_link_libraries(${libname} PRIVATE ${builtins_${libname}})
366    endif()
367    if(${type} STREQUAL "SHARED")
368      if(COMMAND llvm_setup_rpath)
369        llvm_setup_rpath(${libname})
370      endif()
371      if(WIN32 AND NOT CYGWIN AND NOT MINGW)
372        set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
373        set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
374      endif()
375      if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
376        # Ad-hoc sign the dylibs when using Xcode versions older than 12.
377        # Xcode 12 shipped with ld64-609.
378        # FIXME: Remove whole conditional block once everything uses Xcode 12+.
379        set(LD_V_OUTPUT)
380        execute_process(
381          COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
382          RESULT_VARIABLE HAD_ERROR
383          OUTPUT_VARIABLE LD_V_OUTPUT
384        )
385        if (HAD_ERROR)
386          message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
387        endif()
388        set(NEED_EXPLICIT_ADHOC_CODESIGN 1)
389        if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
390          string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
391          if (HOST_LINK_VERSION VERSION_GREATER_EQUAL 609)
392            set(NEED_EXPLICIT_ADHOC_CODESIGN 0)
393          endif()
394        endif()
395        if (NEED_EXPLICIT_ADHOC_CODESIGN)
396          add_custom_command(TARGET ${libname}
397            POST_BUILD
398            COMMAND codesign --sign - $<TARGET_FILE:${libname}>
399            WORKING_DIRECTORY ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
400          )
401        endif()
402      endif()
403    endif()
404
405    set(parent_target_arg)
406    if(LIB_PARENT_TARGET)
407      set(parent_target_arg PARENT_TARGET ${LIB_PARENT_TARGET})
408    endif()
409    add_compiler_rt_install_targets(${libname} ${parent_target_arg})
410
411    if(APPLE)
412      set_target_properties(${libname} PROPERTIES
413      OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
414    endif()
415
416    if(type STREQUAL "SHARED")
417      rt_externalize_debuginfo(${libname})
418    endif()
419  endforeach()
420  if(LIB_PARENT_TARGET)
421    add_dependencies(${LIB_PARENT_TARGET} ${libnames})
422  endif()
423endfunction()
424
425# Compile and register compiler-rt tests.
426# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
427#                           <test architecture>
428#                           KIND <custom prefix>
429#                           SUBDIR <subdirectory for testing binary>
430#                           SOURCES <sources to compile>
431#                           RUNTIME <tests runtime to link in>
432#                           CFLAGS <compile-time flags>
433#                           COMPILE_DEPS <compile-time dependencies>
434#                           DEPS <dependencies>
435#                           LINK_FLAGS <flags to use during linking>
436# )
437function(generate_compiler_rt_tests test_objects test_suite testname arch)
438  cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
439    "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
440
441  foreach(source ${TEST_SOURCES})
442    sanitizer_test_compile(
443      "${test_objects}" "${source}" "${arch}"
444      KIND ${TEST_KIND}
445      COMPILE_DEPS ${TEST_COMPILE_DEPS}
446      DEPS ${TEST_DEPS}
447      CFLAGS ${TEST_CFLAGS}
448      )
449  endforeach()
450
451  set(TEST_DEPS ${${test_objects}})
452
453  if(NOT "${TEST_RUNTIME}" STREQUAL "")
454    list(APPEND TEST_DEPS ${TEST_RUNTIME})
455    list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
456  endif()
457
458  add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
459    SUBDIR ${TEST_SUBDIR}
460    OBJECTS ${${test_objects}}
461    DEPS ${TEST_DEPS}
462    LINK_FLAGS ${TEST_LINK_FLAGS}
463    )
464  set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
465endfunction()
466
467# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
468# using specified link flags. Make executable a part of provided
469# test_suite.
470# add_compiler_rt_test(<test_suite> <test_name> <arch>
471#                      SUBDIR <subdirectory for binary>
472#                      OBJECTS <object files>
473#                      DEPS <deps (e.g. runtime libs)>
474#                      LINK_FLAGS <link flags>)
475function(add_compiler_rt_test test_suite test_name arch)
476  cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
477  set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
478  if(TEST_SUBDIR)
479    set(output_dir "${output_dir}/${TEST_SUBDIR}")
480  endif()
481  set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
482  file(MAKE_DIRECTORY "${output_dir}")
483  set(output_bin "${output_dir}/${test_name}")
484  if(MSVC)
485    set(output_bin "${output_bin}.exe")
486  endif()
487
488  # Use host compiler in a standalone build, and just-built Clang otherwise.
489  if(NOT COMPILER_RT_STANDALONE_BUILD)
490    list(APPEND TEST_DEPS clang)
491  endif()
492
493  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
494  list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
495
496  # If we're not on MSVC, include the linker flags from CMAKE but override them
497  # with the provided link flags. This ensures that flags which are required to
498  # link programs at all are included, but the changes needed for the test
499  # trump. With MSVC we can't do that because CMake is set up to run link.exe
500  # when linking, not the compiler. Here, we hack it to use the compiler
501  # because we want to use -fsanitize flags.
502
503  # Only add CMAKE_EXE_LINKER_FLAGS when in a standalone bulid.
504  # Or else CMAKE_EXE_LINKER_FLAGS contains flags for build compiler of Clang/llvm.
505  # This might not be the same as what the COMPILER_RT_TEST_COMPILER supports.
506  # eg: the build compiler use lld linker and we build clang with default ld linker
507  # then to be tested clang will complain about lld options like --color-diagnostics.
508  if(NOT MSVC AND COMPILER_RT_STANDALONE_BUILD)
509    set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
510    separate_arguments(TEST_LINK_FLAGS)
511  endif()
512  if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_HAS_LLD AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
513    # CMAKE_EXE_LINKER_FLAGS may contain -fuse=lld
514    # FIXME: -DLLVM_ENABLE_LLD=ON and -DLLVM_ENABLE_PROJECTS without lld case.
515    list(APPEND TEST_DEPS lld)
516  endif()
517  add_custom_command(
518    OUTPUT "${output_bin}"
519    COMMAND ${COMPILER_RT_TEST_CXX_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
520            ${TEST_LINK_FLAGS}
521    DEPENDS ${TEST_DEPS}
522    )
523  add_custom_target(T${test_name} DEPENDS "${output_bin}")
524  set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
525
526  # Make the test suite depend on the binary.
527  add_dependencies(${test_suite} T${test_name})
528endfunction()
529
530macro(add_compiler_rt_resource_file target_name file_name component)
531  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
532  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
533  add_custom_command(OUTPUT ${dst_file}
534    DEPENDS ${src_file}
535    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
536    COMMENT "Copying ${file_name}...")
537  add_custom_target(${target_name} DEPENDS ${dst_file})
538  # Install in Clang resource directory.
539  install(FILES ${file_name}
540    DESTINATION ${COMPILER_RT_INSTALL_DATA_DIR}
541    COMPONENT ${component})
542  add_dependencies(${component} ${target_name})
543
544  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
545endmacro()
546
547macro(add_compiler_rt_script name)
548  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
549  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
550  add_custom_command(OUTPUT ${dst}
551    DEPENDS ${src}
552    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
553    COMMENT "Copying ${name}...")
554  add_custom_target(${name} DEPENDS ${dst})
555  install(FILES ${dst}
556    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
557    DESTINATION ${COMPILER_RT_INSTALL_BINARY_DIR})
558endmacro(add_compiler_rt_script src name)
559
560# Builds custom version of libc++ and installs it in <prefix>.
561# Can be used to build sanitized versions of libc++ for running unit tests.
562# add_custom_libcxx(<name> <prefix>
563#                   DEPS <list of build deps>
564#                   CFLAGS <list of compile flags>
565#                   USE_TOOLCHAIN)
566macro(add_custom_libcxx name prefix)
567  if(NOT COMPILER_RT_LIBCXX_PATH)
568    message(FATAL_ERROR "libcxx not found!")
569  endif()
570  if(NOT COMPILER_RT_LIBCXXABI_PATH)
571    message(FATAL_ERROR "libcxxabi not found!")
572  endif()
573
574  cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
575
576  if(LIBCXX_USE_TOOLCHAIN)
577    set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
578                      -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
579    if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
580      set(toolchain_deps $<TARGET_FILE:clang>)
581      set(force_deps DEPENDS $<TARGET_FILE:clang>)
582    endif()
583  else()
584    set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
585                      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
586  endif()
587
588  add_custom_target(${name}-clear
589    COMMAND ${CMAKE_COMMAND} -E remove_directory ${prefix}
590    COMMENT "Clobbering ${name} build directories"
591    USES_TERMINAL
592    )
593  set_target_properties(${name}-clear PROPERTIES FOLDER "Compiler-RT Misc")
594
595  add_custom_command(
596    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
597    DEPENDS ${LIBCXX_DEPS} ${toolchain_deps}
598    COMMAND ${CMAKE_COMMAND} -E touch ${prefix}/CMakeCache.txt
599    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
600    COMMENT "Clobbering bootstrap build directories"
601    )
602
603  add_custom_target(${name}-clobber
604    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
605  set_target_properties(${name}-clobber PROPERTIES FOLDER "Compiler-RT Misc")
606
607  set(PASSTHROUGH_VARIABLES
608    CMAKE_C_COMPILER_TARGET
609    CMAKE_CXX_COMPILER_TARGET
610    CMAKE_SHARED_LINKER_FLAGS
611    CMAKE_MODULE_LINKER_FLAGS
612    CMAKE_EXE_LINKER_FLAGS
613    CMAKE_INSTALL_PREFIX
614    CMAKE_MAKE_PROGRAM
615    CMAKE_LINKER
616    CMAKE_AR
617    CMAKE_RANLIB
618    CMAKE_NM
619    CMAKE_OBJCOPY
620    CMAKE_OBJDUMP
621    CMAKE_STRIP
622    CMAKE_READELF
623    CMAKE_SYSROOT
624    LIBCXX_HAS_MUSL_LIBC
625    LIBCXX_HAS_PTHREAD_LIB
626    LIBCXX_HAS_RT_LIB
627    LIBCXXABI_HAS_PTHREAD_LIB
628    PYTHON_EXECUTABLE
629    Python3_EXECUTABLE
630    Python2_EXECUTABLE
631    CMAKE_SYSTEM_NAME)
632  foreach(variable ${PASSTHROUGH_VARIABLES})
633    get_property(is_value_set CACHE ${variable} PROPERTY VALUE SET)
634    if(${is_value_set})
635      get_property(value CACHE ${variable} PROPERTY VALUE)
636      list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${value})
637    endif()
638  endforeach()
639
640  string(REPLACE ";" " " LIBCXX_C_FLAGS "${LIBCXX_CFLAGS}")
641  get_property(C_FLAGS CACHE CMAKE_C_FLAGS PROPERTY VALUE)
642  set(LIBCXX_C_FLAGS "${LIBCXX_C_FLAGS} ${C_FLAGS}")
643
644  string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CFLAGS}")
645  get_property(CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
646  set(LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS} ${CXX_FLAGS}")
647
648  ExternalProject_Add(${name}
649    DEPENDS ${name}-clobber ${LIBCXX_DEPS}
650    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/${name}
651    SOURCE_DIR ${LLVM_MAIN_SRC_DIR}/../runtimes
652    BINARY_DIR ${prefix}
653    CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES}
654               ${compiler_args}
655               -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS}
656               -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS}
657               -DCMAKE_BUILD_TYPE=Release
658               -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
659               -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
660               -DLLVM_ENABLE_RUNTIMES=libcxx|libcxxabi
661               -DLIBCXXABI_ENABLE_SHARED=OFF
662               -DLIBCXXABI_HERMETIC_STATIC_LIBRARY=ON
663               -DLIBCXXABI_INCLUDE_TESTS=OFF
664               -DLIBCXX_CXX_ABI=libcxxabi
665               -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF
666               -DLIBCXX_ENABLE_SHARED=OFF
667               -DLIBCXX_HERMETIC_STATIC_LIBRARY=ON
668               -DLIBCXX_INCLUDE_BENCHMARKS=OFF
669               -DLIBCXX_INCLUDE_TESTS=OFF
670               -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
671               ${LIBCXX_CMAKE_ARGS}
672    INSTALL_COMMAND ""
673    STEP_TARGETS configure build
674    BUILD_ALWAYS 1
675    USES_TERMINAL_CONFIGURE 1
676    USES_TERMINAL_BUILD 1
677    USES_TERMINAL_INSTALL 1
678    LIST_SEPARATOR |
679    EXCLUDE_FROM_ALL TRUE
680    BUILD_BYPRODUCTS "${prefix}/lib/libc++.a" "${prefix}/lib/libc++abi.a"
681    )
682
683  if (CMAKE_GENERATOR MATCHES "Make")
684    set(run_clean "$(MAKE)" "-C" "${prefix}" "clean")
685  else()
686    set(run_clean ${CMAKE_COMMAND} --build ${prefix} --target clean
687                                   --config "$<CONFIG>")
688  endif()
689
690  ExternalProject_Add_Step(${name} clean
691    COMMAND ${run_clean}
692    COMMENT "Cleaning ${name}..."
693    DEPENDEES configure
694    ${force_deps}
695    WORKING_DIRECTORY ${prefix}
696    EXCLUDE_FROM_MAIN 1
697    USES_TERMINAL 1
698    )
699  ExternalProject_Add_StepTargets(${name} clean)
700
701  if(LIBCXX_USE_TOOLCHAIN)
702    add_dependencies(${name}-clean ${name}-clobber)
703    set_target_properties(${name}-clean PROPERTIES
704      SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
705  endif()
706endmacro()
707
708function(rt_externalize_debuginfo name)
709  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
710    return()
711  endif()
712
713  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
714    set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
715  endif()
716
717  if(APPLE)
718    if(CMAKE_CXX_FLAGS MATCHES "-flto"
719      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
720
721      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
722      set_property(TARGET ${name} APPEND_STRING PROPERTY
723        LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
724    endif()
725    add_custom_command(TARGET ${name} POST_BUILD
726      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
727      ${strip_command})
728  else()
729    message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
730  endif()
731endfunction()
732
733
734# Configure lit configuration files, including compiler-rt specific variables.
735function(configure_compiler_rt_lit_site_cfg input output)
736  set_llvm_build_mode()
737
738  get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
739
740  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
741  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
742
743  configure_lit_site_cfg(${input} ${output})
744endfunction()
745