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      # 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)
376        # Ad-hoc sign the dylibs
377        add_custom_command(TARGET ${libname}
378          POST_BUILD
379          COMMAND codesign --sign - $<TARGET_FILE:${libname}>
380          WORKING_DIRECTORY ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
381        )
382      endif()
383    endif()
384
385    set(parent_target_arg)
386    if(LIB_PARENT_TARGET)
387      set(parent_target_arg PARENT_TARGET ${LIB_PARENT_TARGET})
388    endif()
389    add_compiler_rt_install_targets(${libname} ${parent_target_arg})
390
391    if(APPLE)
392      set_target_properties(${libname} PROPERTIES
393      OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
394    endif()
395
396    if(type STREQUAL "SHARED")
397      rt_externalize_debuginfo(${libname})
398    endif()
399  endforeach()
400  if(LIB_PARENT_TARGET)
401    add_dependencies(${LIB_PARENT_TARGET} ${libnames})
402  endif()
403endfunction()
404
405# Compile and register compiler-rt tests.
406# generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
407#                           <test architecture>
408#                           KIND <custom prefix>
409#                           SUBDIR <subdirectory for testing binary>
410#                           SOURCES <sources to compile>
411#                           RUNTIME <tests runtime to link in>
412#                           CFLAGS <compile-time flags>
413#                           COMPILE_DEPS <compile-time dependencies>
414#                           DEPS <dependencies>
415#                           LINK_FLAGS <flags to use during linking>
416# )
417function(generate_compiler_rt_tests test_objects test_suite testname arch)
418  cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
419    "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
420
421  foreach(source ${TEST_SOURCES})
422    sanitizer_test_compile(
423      "${test_objects}" "${source}" "${arch}"
424      KIND ${TEST_KIND}
425      COMPILE_DEPS ${TEST_COMPILE_DEPS}
426      DEPS ${TEST_DEPS}
427      CFLAGS ${TEST_CFLAGS}
428      )
429  endforeach()
430
431  set(TEST_DEPS ${${test_objects}})
432
433  if(NOT "${TEST_RUNTIME}" STREQUAL "")
434    list(APPEND TEST_DEPS ${TEST_RUNTIME})
435    list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
436  endif()
437
438  add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
439    SUBDIR ${TEST_SUBDIR}
440    OBJECTS ${${test_objects}}
441    DEPS ${TEST_DEPS}
442    LINK_FLAGS ${TEST_LINK_FLAGS}
443    )
444  set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
445endfunction()
446
447# Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
448# using specified link flags. Make executable a part of provided
449# test_suite.
450# add_compiler_rt_test(<test_suite> <test_name> <arch>
451#                      SUBDIR <subdirectory for binary>
452#                      OBJECTS <object files>
453#                      DEPS <deps (e.g. runtime libs)>
454#                      LINK_FLAGS <link flags>)
455function(add_compiler_rt_test test_suite test_name arch)
456  cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
457  set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
458  if(TEST_SUBDIR)
459    set(output_dir "${output_dir}/${TEST_SUBDIR}")
460  endif()
461  set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
462  file(MAKE_DIRECTORY "${output_dir}")
463  set(output_bin "${output_dir}/${test_name}")
464  if(MSVC)
465    set(output_bin "${output_bin}.exe")
466  endif()
467
468  # Use host compiler in a standalone build, and just-built Clang otherwise.
469  if(NOT COMPILER_RT_STANDALONE_BUILD)
470    list(APPEND TEST_DEPS clang)
471  endif()
472
473  get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
474  list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
475
476  # If we're not on MSVC, include the linker flags from CMAKE but override them
477  # with the provided link flags. This ensures that flags which are required to
478  # link programs at all are included, but the changes needed for the test
479  # trump. With MSVC we can't do that because CMake is set up to run link.exe
480  # when linking, not the compiler. Here, we hack it to use the compiler
481  # because we want to use -fsanitize flags.
482
483  # Only add CMAKE_EXE_LINKER_FLAGS when in a standalone bulid.
484  # Or else CMAKE_EXE_LINKER_FLAGS contains flags for build compiler of Clang/llvm.
485  # This might not be the same as what the COMPILER_RT_TEST_COMPILER supports.
486  # eg: the build compiler use lld linker and we build clang with default ld linker
487  # then to be tested clang will complain about lld options like --color-diagnostics.
488  if(NOT MSVC AND COMPILER_RT_STANDALONE_BUILD)
489    set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
490    separate_arguments(TEST_LINK_FLAGS)
491  endif()
492  if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_HAS_LLD AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
493    # CMAKE_EXE_LINKER_FLAGS may contain -fuse=lld
494    # FIXME: -DLLVM_ENABLE_LLD=ON and -DLLVM_ENABLE_PROJECTS without lld case.
495    list(APPEND TEST_DEPS lld)
496  endif()
497  add_custom_command(
498    OUTPUT "${output_bin}"
499    COMMAND ${COMPILER_RT_TEST_CXX_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
500            ${TEST_LINK_FLAGS}
501    DEPENDS ${TEST_DEPS}
502    )
503  add_custom_target(T${test_name} DEPENDS "${output_bin}")
504  set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
505
506  # Make the test suite depend on the binary.
507  add_dependencies(${test_suite} T${test_name})
508endfunction()
509
510macro(add_compiler_rt_resource_file target_name file_name component)
511  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
512  set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
513  add_custom_command(OUTPUT ${dst_file}
514    DEPENDS ${src_file}
515    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
516    COMMENT "Copying ${file_name}...")
517  add_custom_target(${target_name} DEPENDS ${dst_file})
518  # Install in Clang resource directory.
519  install(FILES ${file_name}
520    DESTINATION ${COMPILER_RT_INSTALL_DATA_DIR}
521    COMPONENT ${component})
522  add_dependencies(${component} ${target_name})
523
524  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
525endmacro()
526
527macro(add_compiler_rt_script name)
528  set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
529  set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
530  add_custom_command(OUTPUT ${dst}
531    DEPENDS ${src}
532    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
533    COMMENT "Copying ${name}...")
534  add_custom_target(${name} DEPENDS ${dst})
535  install(FILES ${dst}
536    PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
537    DESTINATION ${COMPILER_RT_INSTALL_BINARY_DIR})
538endmacro(add_compiler_rt_script src name)
539
540# Builds custom version of libc++ and installs it in <prefix>.
541# Can be used to build sanitized versions of libc++ for running unit tests.
542# add_custom_libcxx(<name> <prefix>
543#                   DEPS <list of build deps>
544#                   CFLAGS <list of compile flags>
545#                   USE_TOOLCHAIN)
546macro(add_custom_libcxx name prefix)
547  if(NOT COMPILER_RT_LIBCXX_PATH)
548    message(FATAL_ERROR "libcxx not found!")
549  endif()
550  if(NOT COMPILER_RT_LIBCXXABI_PATH)
551    message(FATAL_ERROR "libcxxabi not found!")
552  endif()
553
554  cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
555
556  if(LIBCXX_USE_TOOLCHAIN)
557    set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
558                      -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
559    if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
560      set(toolchain_deps $<TARGET_FILE:clang>)
561      set(force_deps DEPENDS $<TARGET_FILE:clang>)
562    endif()
563  else()
564    set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
565                      -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
566  endif()
567
568  set(STAMP_DIR ${prefix}-stamps/)
569  set(BINARY_DIR ${prefix}-bins/)
570
571  add_custom_target(${name}-clear
572    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
573    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
574    COMMENT "Clobbering ${name} build and stamp directories"
575    USES_TERMINAL
576    )
577  set_target_properties(${name}-clear PROPERTIES FOLDER "Compiler-RT Misc")
578
579  add_custom_command(
580    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
581    DEPENDS ${LIBCXX_DEPS} ${toolchain_deps}
582    COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
583    COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
584    COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
585    COMMENT "Clobbering bootstrap build and stamp directories"
586    )
587
588  add_custom_target(${name}-clobber
589    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
590  set_target_properties(${name}-clobber PROPERTIES FOLDER "Compiler-RT Misc")
591
592  set(PASSTHROUGH_VARIABLES
593    CMAKE_C_COMPILER_TARGET
594    CMAKE_CXX_COMPILER_TARGET
595    CMAKE_SHARED_LINKER_FLAGS
596    CMAKE_MODULE_LINKER_FLAGS
597    CMAKE_EXE_LINKER_FLAGS
598    CMAKE_INSTALL_PREFIX
599    CMAKE_MAKE_PROGRAM
600    CMAKE_LINKER
601    CMAKE_AR
602    CMAKE_RANLIB
603    CMAKE_NM
604    CMAKE_OBJCOPY
605    CMAKE_OBJDUMP
606    CMAKE_STRIP
607    CMAKE_READELF
608    CMAKE_SYSROOT
609    LIBCXX_HAS_MUSL_LIBC
610    PYTHON_EXECUTABLE
611    Python3_EXECUTABLE
612    Python2_EXECUTABLE
613    CMAKE_SYSTEM_NAME)
614  foreach(variable ${PASSTHROUGH_VARIABLES})
615    get_property(is_value_set CACHE ${variable} PROPERTY VALUE SET)
616    if(${is_value_set})
617      get_property(value CACHE ${variable} PROPERTY VALUE)
618      list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${value})
619    endif()
620  endforeach()
621
622  string(REPLACE ";" " " LIBCXX_C_FLAGS "${LIBCXX_CFLAGS}")
623  get_property(C_FLAGS CACHE CMAKE_C_FLAGS PROPERTY VALUE)
624  set(LIBCXX_C_FLAGS "${LIBCXX_C_FLAGS} ${C_FLAGS}")
625
626  string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CFLAGS}")
627  get_property(CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
628  set(LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS} ${CXX_FLAGS}")
629
630  ExternalProject_Add(${name}
631    DEPENDS ${name}-clobber ${LIBCXX_DEPS}
632    PREFIX ${prefix}
633    SOURCE_DIR ${COMPILER_RT_SOURCE_DIR}/cmake/Modules/CustomLibcxx
634    STAMP_DIR ${STAMP_DIR}
635    BINARY_DIR ${BINARY_DIR}
636    CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES}
637               ${compiler_args}
638               -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS}
639               -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS}
640               -DCMAKE_BUILD_TYPE=Release
641               -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
642               -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
643               -DLLVM_BINARY_DIR=${prefix}
644               -DLLVM_LIBRARY_OUTPUT_INTDIR=${prefix}/lib
645               -DCOMPILER_RT_LIBCXX_PATH=${COMPILER_RT_LIBCXX_PATH}
646               -DCOMPILER_RT_LIBCXXABI_PATH=${COMPILER_RT_LIBCXXABI_PATH}
647               ${LIBCXX_CMAKE_ARGS}
648    INSTALL_COMMAND ""
649    STEP_TARGETS configure build
650    BUILD_ALWAYS 1
651    USES_TERMINAL_CONFIGURE 1
652    USES_TERMINAL_BUILD 1
653    USES_TERMINAL_INSTALL 1
654    EXCLUDE_FROM_ALL TRUE
655    BUILD_BYPRODUCTS "${prefix}/lib/libc++.a" "${prefix}/lib/libc++abi.a"
656    )
657
658  if (CMAKE_GENERATOR MATCHES "Make")
659    set(run_clean "$(MAKE)" "-C" "${BINARY_DIR}" "clean")
660  else()
661    set(run_clean ${CMAKE_COMMAND} --build ${BINARY_DIR} --target clean
662                                   --config "$<CONFIG>")
663  endif()
664
665  ExternalProject_Add_Step(${name} clean
666    COMMAND ${run_clean}
667    COMMENT "Cleaning ${name}..."
668    DEPENDEES configure
669    ${force_deps}
670    WORKING_DIRECTORY ${BINARY_DIR}
671    EXCLUDE_FROM_MAIN 1
672    USES_TERMINAL 1
673    )
674  ExternalProject_Add_StepTargets(${name} clean)
675
676  if(LIBCXX_USE_TOOLCHAIN)
677    add_dependencies(${name}-clean ${name}-clobber)
678    set_target_properties(${name}-clean PROPERTIES
679      SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
680  endif()
681endmacro()
682
683function(rt_externalize_debuginfo name)
684  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
685    return()
686  endif()
687
688  if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
689    set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
690  endif()
691
692  if(APPLE)
693    if(CMAKE_CXX_FLAGS MATCHES "-flto"
694      OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
695
696      set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
697      set_property(TARGET ${name} APPEND_STRING PROPERTY
698        LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
699    endif()
700    add_custom_command(TARGET ${name} POST_BUILD
701      COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
702      ${strip_command})
703  else()
704    message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
705  endif()
706endfunction()
707
708
709# Configure lit configuration files, including compiler-rt specific variables.
710function(configure_compiler_rt_lit_site_cfg input output)
711  set_llvm_build_mode()
712
713  get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
714
715  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
716  string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
717
718  configure_lit_site_cfg(${input} ${output})
719endfunction()
720