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