1 include(ExternalProject)
2 include(CompilerRTUtils)
3 include(HandleCompilerRT)
5 # CMP0114: ExternalProject step targets fully adopt their steps.
6 # New in CMake 3.19: https://cmake.org/cmake/help/latest/policy/CMP0114.html
8 cmake_policy(SET CMP0114 OLD)
11 function(set_target_output_directories target output_dir)
12 # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators
13 # append a per-configuration subdirectory to the specified directory.
14 # To avoid the appended folder, the configuration specific variable must be
15 # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}':
16 # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ...
17 if(CMAKE_CONFIGURATION_TYPES)
18 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
19 string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
20 set_target_properties("${target}" PROPERTIES
21 "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
22 "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}
23 "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir})
26 set_target_properties("${target}" PROPERTIES
27 ARCHIVE_OUTPUT_DIRECTORY ${output_dir}
28 LIBRARY_OUTPUT_DIRECTORY ${output_dir}
29 RUNTIME_OUTPUT_DIRECTORY ${output_dir})
33 # Tries to add an "object library" target for a given list of OSs and/or
34 # architectures with name "<name>.<arch>" for non-Darwin platforms if
35 # architecture can be targeted, and "<name>.<os>" for Darwin platforms.
36 # add_compiler_rt_object_libraries(<name>
38 # ARCHS <architectures>
39 # SOURCES <source files>
40 # CFLAGS <compile flags>
41 # DEFS <compile definitions>
43 # ADDITIONAL_HEADERS <header files>)
44 function(add_compiler_rt_object_libraries name)
45 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS;ADDITIONAL_HEADERS"
50 set(libname "${name}.${os}")
51 set(libnames ${libnames} ${libname})
52 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS})
53 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
56 foreach(arch ${LIB_ARCHS})
57 set(libname "${name}.${arch}")
58 set(libnames ${libnames} ${libname})
59 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS})
60 if(NOT CAN_TARGET_${arch})
61 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
67 # Add headers to LIB_SOURCES for IDEs
68 compiler_rt_process_sources(LIB_SOURCES
71 ${LIB_ADDITIONAL_HEADERS}
74 foreach(libname ${libnames})
75 add_library(${libname} OBJECT ${LIB_SOURCES})
77 add_dependencies(${libname} ${LIB_DEPS})
80 # Strip out -msse3 if this isn't macOS.
81 set(target_flags ${LIB_CFLAGS})
82 if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*")
83 list(REMOVE_ITEM target_flags "-msse3")
86 # Build the macOS sanitizers with Mac Catalyst support.
88 "${COMPILER_RT_ENABLE_MACCATALYST}" AND
89 "${libname}" MATCHES ".*\.osx.*")
90 foreach(arch ${LIB_ARCHS_${libname}})
91 list(APPEND target_flags
92 "SHELL:-target ${arch}-apple-macos${DARWIN_osx_MIN_VER} -darwin-target-variant ${arch}-apple-ios13.1-macabi")
96 set_target_compile_flags(${libname}
97 ${extra_cflags_${libname}} ${target_flags})
98 set_property(TARGET ${libname} APPEND PROPERTY
99 COMPILE_DEFINITIONS ${LIB_DEFS})
100 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries")
102 set_target_properties(${libname} PROPERTIES
103 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
108 # Takes a list of object library targets, and a suffix and appends the proper
109 # TARGET_OBJECTS string to the output variable.
110 # format_object_libs(<output> <suffix> ...)
111 macro(format_object_libs output suffix)
113 list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
117 function(add_compiler_rt_component name)
118 add_custom_target(${name})
119 set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc")
120 if(COMMAND runtime_register_component)
121 runtime_register_component(${name})
123 add_dependencies(compiler-rt ${name})
126 macro(set_output_name output name arch)
127 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
128 set(${output} ${name})
130 if(ANDROID AND ${arch} STREQUAL "i386")
131 set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}")
132 elseif("${arch}" MATCHES "^arm")
133 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
134 set(triple "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")
136 set(triple "${LLVM_TARGET_TRIPLE}")
138 # Except for baremetal, when using arch-suffixed runtime library names,
139 # clang only looks for libraries named "arm" or "armhf", see
140 # getArchNameForCompilerRTLib in clang. Therefore, try to inspect both
141 # the arch name and the triple if it seems like we're building an armhf
143 if (COMPILER_RT_BAREMETAL_BUILD)
144 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
145 elseif ("${arch}" MATCHES "hf$" OR "${triple}" MATCHES "hf$")
146 set(${output} "${name}-armhf${COMPILER_RT_OS_SUFFIX}")
148 set(${output} "${name}-arm${COMPILER_RT_OS_SUFFIX}")
151 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}")
156 # Adds static or shared runtime for a list of architectures and operating
157 # systems and puts it in the proper directory in the build and install trees.
158 # add_compiler_rt_runtime(<name>
159 # {OBJECT|STATIC|SHARED|MODULE}
160 # ARCHS <architectures>
162 # SOURCES <source files>
163 # CFLAGS <compile flags>
164 # LINK_FLAGS <linker flags>
165 # DEFS <compile definitions>
166 # DEPS <dependencies>
167 # LINK_LIBS <linked libraries> (only for shared library)
168 # OBJECT_LIBS <object libraries to use as sources>
169 # PARENT_TARGET <convenience parent target>
170 # ADDITIONAL_HEADERS <header files>)
171 function(add_compiler_rt_runtime name type)
172 if(NOT type MATCHES "^(OBJECT|STATIC|SHARED|MODULE)$")
174 "type argument must be OBJECT, STATIC, SHARED or MODULE")
177 cmake_parse_arguments(LIB
180 "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS"
183 # Until we support this some other way, build compiler-rt runtime without LTO
184 # to allow non-LTO projects to link with it.
185 if(COMPILER_RT_HAS_FNO_LTO_FLAG)
186 set(NO_LTO_FLAGS "-fno-lto")
191 # By default do not instrument or use profdata for compiler-rt.
193 if(NOT COMPILER_RT_ENABLE_PGO)
194 if(LLVM_PROFDATA_FILE AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
195 list(APPEND NO_PGO_FLAGS "-fno-profile-instr-use")
197 if(LLVM_BUILD_INSTRUMENTED MATCHES IR AND COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
198 list(APPEND NO_PGO_FLAGS "-fno-profile-generate")
199 elseif((LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE) AND COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
200 list(APPEND NO_PGO_FLAGS "-fno-profile-instr-generate")
201 if(LLVM_BUILD_INSTRUMENTED_COVERAGE AND COMPILER_RT_HAS_FNO_COVERAGE_MAPPING_FLAG)
202 list(APPEND NO_PGO_FLAGS "-fno-coverage-mapping")
207 list(LENGTH LIB_SOURCES LIB_SOURCES_LENGTH)
208 if (${LIB_SOURCES_LENGTH} GREATER 0)
209 # Add headers to LIB_SOURCES for IDEs. It doesn't make sense to
210 # do this for a runtime library that only consists of OBJECT
211 # libraries, so only add the headers when source files are present.
212 compiler_rt_process_sources(LIB_SOURCES
215 ${LIB_ADDITIONAL_HEADERS}
220 foreach(os ${LIB_OS})
221 # Strip out -msse3 if this isn't macOS.
222 list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS)
223 if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$")
224 list(REMOVE_ITEM LIB_CFLAGS "-msse3")
226 if(type STREQUAL "STATIC")
227 set(libname "${name}_${os}")
229 set(libname "${name}_${os}_dynamic")
230 set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS})
232 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
233 if(LIB_ARCHS_${libname})
234 list(APPEND libnames ${libname})
235 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
236 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
237 set(sources_${libname} ${LIB_SOURCES})
238 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
239 get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir_${libname})
240 get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_${libname})
243 # Build the macOS sanitizers with Mac Catalyst support.
244 if ("${COMPILER_RT_ENABLE_MACCATALYST}" AND
245 "${os}" MATCHES "^(osx)$")
246 foreach(arch ${LIB_ARCHS_${libname}})
247 list(APPEND extra_cflags_${libname}
248 "SHELL:-target ${arch}-apple-macos${DARWIN_osx_MIN_VER} -darwin-target-variant ${arch}-apple-ios13.1-macabi")
249 list(APPEND extra_link_flags_${libname}
250 "SHELL:-target ${arch}-apple-macos${DARWIN_osx_MIN_VER} -darwin-target-variant ${arch}-apple-ios13.1-macabi")
255 foreach(arch ${LIB_ARCHS})
256 if(NOT CAN_TARGET_${arch})
257 message(FATAL_ERROR "Architecture ${arch} can't be targeted")
260 if(type STREQUAL "OBJECT")
261 set(libname "${name}-${arch}")
262 set_output_name(output_name_${libname} ${name}${COMPILER_RT_OS_SUFFIX} ${arch})
263 elseif(type STREQUAL "STATIC")
264 set(libname "${name}-${arch}")
265 set_output_name(output_name_${libname} ${name} ${arch})
267 set(libname "${name}-dynamic-${arch}")
268 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
269 set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS})
271 set_output_name(output_name_${libname} ${name}_dynamic ${arch})
273 set_output_name(output_name_${libname} ${name} ${arch})
276 if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT" AND
277 NOT name STREQUAL "clang_rt.builtins")
278 get_compiler_rt_target(${arch} target)
279 find_compiler_rt_library(builtins builtins_${libname} TARGET ${target})
280 if(builtins_${libname} STREQUAL "NOTFOUND")
281 message(FATAL_ERROR "Cannot find builtins library for the target architecture")
284 set(sources_${libname} ${LIB_SOURCES})
285 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
286 set(libnames ${libnames} ${libname})
287 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${NO_PGO_FLAGS} ${LIB_CFLAGS})
288 get_compiler_rt_output_dir(${arch} output_dir_${libname})
289 get_compiler_rt_install_dir(${arch} install_dir_${libname})
297 if(LIB_PARENT_TARGET)
298 # If the parent targets aren't created we should create them
299 if(NOT TARGET ${LIB_PARENT_TARGET})
300 add_custom_target(${LIB_PARENT_TARGET})
301 set_target_properties(${LIB_PARENT_TARGET} PROPERTIES
302 FOLDER "Compiler-RT Misc")
306 foreach(libname ${libnames})
307 # If you are using a multi-configuration generator we don't generate
308 # per-library install rules, so we fall back to the parent target COMPONENT
309 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET)
310 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
312 set(COMPONENT_OPTION COMPONENT ${libname})
315 if(type STREQUAL "SHARED")
316 list(APPEND LIB_DEFS COMPILER_RT_SHARED_LIB)
319 if(type STREQUAL "OBJECT")
320 if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET)
321 list(APPEND extra_cflags_${libname} "--target=${CMAKE_C_COMPILER_TARGET}")
324 list(APPEND extra_cflags_${libname} "--sysroot=${CMAKE_SYSROOT}")
326 string(REPLACE ";" " " extra_cflags_${libname} "${extra_cflags_${libname}}")
327 string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions
328 ${CMAKE_C_COMPILE_OBJECT})
329 set(compile_command_${libname} "${CMAKE_C_COMPILE_OBJECT}")
331 set(output_file_${libname} ${output_name_${libname}}${CMAKE_C_OUTPUT_EXTENSION})
332 foreach(substitution ${substitutions})
333 if(substitution STREQUAL "<CMAKE_C_COMPILER>")
334 string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}"
335 compile_command_${libname} ${compile_command_${libname}})
336 elseif(substitution STREQUAL "<OBJECT>")
337 string(REPLACE "<OBJECT>" "${output_dir_${libname}}/${output_file_${libname}}"
338 compile_command_${libname} ${compile_command_${libname}})
339 elseif(substitution STREQUAL "<SOURCE>")
340 string(REPLACE "<SOURCE>" "${sources_${libname}}"
341 compile_command_${libname} ${compile_command_${libname}})
342 elseif(substitution STREQUAL "<FLAGS>")
343 string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_cflags_${libname}}"
344 compile_command_${libname} ${compile_command_${libname}})
346 string(REPLACE "${substitution}" "" compile_command_${libname}
347 ${compile_command_${libname}})
350 separate_arguments(compile_command_${libname})
352 OUTPUT ${output_dir_${libname}}/${output_file_${libname}}
353 COMMAND ${compile_command_${libname}}
354 DEPENDS ${sources_${libname}}
355 COMMENT "Building C object ${output_file_${libname}}")
356 add_custom_target(${libname} DEPENDS ${output_dir_${libname}}/${output_file_${libname}})
357 install(FILES ${output_dir_${libname}}/${output_file_${libname}}
358 DESTINATION ${install_dir_${libname}}
361 add_library(${libname} ${type} ${sources_${libname}})
362 set_target_compile_flags(${libname} ${extra_cflags_${libname}})
363 set_target_link_flags(${libname} ${extra_link_flags_${libname}})
364 set_property(TARGET ${libname} APPEND PROPERTY
365 COMPILE_DEFINITIONS ${LIB_DEFS})
366 set_target_output_directories(${libname} ${output_dir_${libname}})
367 install(TARGETS ${libname}
368 ARCHIVE DESTINATION ${install_dir_${libname}}
370 LIBRARY DESTINATION ${install_dir_${libname}}
372 RUNTIME DESTINATION ${install_dir_${libname}}
376 add_dependencies(${libname} ${LIB_DEPS})
378 set_target_properties(${libname} PROPERTIES
379 OUTPUT_NAME ${output_name_${libname}})
380 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime")
382 target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS})
384 if(builtins_${libname})
385 target_link_libraries(${libname} PRIVATE ${builtins_${libname}})
387 if(${type} STREQUAL "SHARED")
389 set_property(TARGET ${libname} PROPERTY BUILD_WITH_INSTALL_RPATH ON)
391 if(WIN32 AND NOT CYGWIN AND NOT MINGW)
392 set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "")
393 set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib")
395 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
396 # Ad-hoc sign the dylibs when using Xcode versions older than 12.
397 # Xcode 12 shipped with ld64-609.
398 # FIXME: Remove whole conditional block once everything uses Xcode 12+.
401 COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
402 RESULT_VARIABLE HAD_ERROR
403 OUTPUT_VARIABLE LD_V_OUTPUT
406 message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
408 set(NEED_EXPLICIT_ADHOC_CODESIGN 1)
409 if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
410 string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
411 if (HOST_LINK_VERSION VERSION_GREATER_EQUAL 609)
412 set(NEED_EXPLICIT_ADHOC_CODESIGN 0)
415 if (NEED_EXPLICIT_ADHOC_CODESIGN)
416 add_custom_command(TARGET ${libname}
418 COMMAND codesign --sign - $<TARGET_FILE:${libname}>
419 WORKING_DIRECTORY ${COMPILER_RT_OUTPUT_LIBRARY_DIR}
425 set(parent_target_arg)
426 if(LIB_PARENT_TARGET)
427 set(parent_target_arg PARENT_TARGET ${LIB_PARENT_TARGET})
429 add_compiler_rt_install_targets(${libname} ${parent_target_arg})
432 set_target_properties(${libname} PROPERTIES
433 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
436 if(type STREQUAL "SHARED")
437 rt_externalize_debuginfo(${libname})
440 if(LIB_PARENT_TARGET)
441 add_dependencies(${LIB_PARENT_TARGET} ${libnames})
445 # Compile and register compiler-rt tests.
446 # generate_compiler_rt_tests(<output object files> <test_suite> <test_name>
447 # <test architecture>
448 # KIND <custom prefix>
449 # SUBDIR <subdirectory for testing binary>
450 # SOURCES <sources to compile>
451 # RUNTIME <tests runtime to link in>
452 # CFLAGS <compile-time flags>
453 # COMPILE_DEPS <compile-time dependencies>
454 # DEPS <dependencies>
455 # LINK_FLAGS <flags to use during linking>
457 function(generate_compiler_rt_tests test_objects test_suite testname arch)
458 cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR"
459 "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN})
461 foreach(source ${TEST_SOURCES})
462 sanitizer_test_compile(
463 "${test_objects}" "${source}" "${arch}"
465 COMPILE_DEPS ${TEST_COMPILE_DEPS}
467 CFLAGS ${TEST_CFLAGS}
471 set(TEST_DEPS ${${test_objects}})
473 if(NOT "${TEST_RUNTIME}" STREQUAL "")
474 list(APPEND TEST_DEPS ${TEST_RUNTIME})
475 list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>)
478 add_compiler_rt_test(${test_suite} "${testname}" "${arch}"
479 SUBDIR ${TEST_SUBDIR}
480 OBJECTS ${${test_objects}}
482 LINK_FLAGS ${TEST_LINK_FLAGS}
484 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE)
487 # Link objects into a single executable with COMPILER_RT_TEST_COMPILER,
488 # using specified link flags. Make executable a part of provided
490 # add_compiler_rt_test(<test_suite> <test_name> <arch>
491 # SUBDIR <subdirectory for binary>
492 # OBJECTS <object files>
493 # DEPS <deps (e.g. runtime libs)>
494 # LINK_FLAGS <link flags>)
495 function(add_compiler_rt_test test_suite test_name arch)
496 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN})
497 set(output_dir ${CMAKE_CURRENT_BINARY_DIR})
499 set(output_dir "${output_dir}/${TEST_SUBDIR}")
501 set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}")
502 file(MAKE_DIRECTORY "${output_dir}")
503 set(output_bin "${output_dir}/${test_name}")
505 set(output_bin "${output_bin}.exe")
508 # Use host compiler in a standalone build, and just-built Clang otherwise.
509 if(NOT COMPILER_RT_STANDALONE_BUILD)
510 list(APPEND TEST_DEPS clang)
513 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS)
514 list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS})
516 # If we're not on MSVC, include the linker flags from CMAKE but override them
517 # with the provided link flags. This ensures that flags which are required to
518 # link programs at all are included, but the changes needed for the test
519 # trump. With MSVC we can't do that because CMake is set up to run link.exe
520 # when linking, not the compiler. Here, we hack it to use the compiler
521 # because we want to use -fsanitize flags.
523 # Only add CMAKE_EXE_LINKER_FLAGS when in a standalone bulid.
524 # Or else CMAKE_EXE_LINKER_FLAGS contains flags for build compiler of Clang/llvm.
525 # This might not be the same as what the COMPILER_RT_TEST_COMPILER supports.
526 # eg: the build compiler use lld linker and we build clang with default ld linker
527 # then to be tested clang will complain about lld options like --color-diagnostics.
528 if(NOT MSVC AND COMPILER_RT_STANDALONE_BUILD)
529 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
530 separate_arguments(TEST_LINK_FLAGS)
532 if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_HAS_LLD AND "lld" IN_LIST LLVM_ENABLE_PROJECTS)
533 # CMAKE_EXE_LINKER_FLAGS may contain -fuse=lld
534 # FIXME: -DLLVM_ENABLE_LLD=ON and -DLLVM_ENABLE_PROJECTS without lld case.
535 list(APPEND TEST_DEPS lld)
538 OUTPUT "${output_bin}"
539 COMMAND ${COMPILER_RT_TEST_CXX_COMPILER} ${TEST_OBJECTS} -o "${output_bin}"
543 add_custom_target(T${test_name} DEPENDS "${output_bin}")
544 set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests")
546 # Make the test suite depend on the binary.
547 add_dependencies(${test_suite} T${test_name})
550 macro(add_compiler_rt_resource_file target_name file_name component)
551 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
552 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}")
553 add_custom_command(OUTPUT ${dst_file}
555 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
556 COMMENT "Copying ${file_name}...")
557 add_custom_target(${target_name} DEPENDS ${dst_file})
558 # Install in Clang resource directory.
559 install(FILES ${file_name}
560 DESTINATION ${COMPILER_RT_INSTALL_DATA_DIR}
561 COMPONENT ${component})
562 add_dependencies(${component} ${target_name})
564 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
567 macro(add_compiler_rt_script name)
568 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name})
569 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name})
570 add_custom_command(OUTPUT ${dst}
572 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst}
573 COMMENT "Copying ${name}...")
574 add_custom_target(${name} DEPENDS ${dst})
576 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
577 DESTINATION ${COMPILER_RT_INSTALL_BINARY_DIR})
578 endmacro(add_compiler_rt_script src name)
580 # Builds custom version of libc++ and installs it in <prefix>.
581 # Can be used to build sanitized versions of libc++ for running unit tests.
582 # add_custom_libcxx(<name> <prefix>
583 # DEPS <list of build deps>
584 # CFLAGS <list of compile flags>
586 macro(add_custom_libcxx name prefix)
587 if(NOT COMPILER_RT_LIBCXX_PATH)
588 message(FATAL_ERROR "libcxx not found!")
590 if(NOT COMPILER_RT_LIBCXXABI_PATH)
591 message(FATAL_ERROR "libcxxabi not found!")
594 cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN})
596 if(LIBCXX_USE_TOOLCHAIN)
597 set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER}
598 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER})
599 if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
600 set(toolchain_deps $<TARGET_FILE:clang>)
601 set(force_deps DEPENDS $<TARGET_FILE:clang>)
604 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
605 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
608 add_custom_target(${name}-clear
609 COMMAND ${CMAKE_COMMAND} -E remove_directory ${prefix}
610 COMMENT "Clobbering ${name} build directories"
613 set_target_properties(${name}-clear PROPERTIES FOLDER "Compiler-RT Misc")
616 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
617 DEPENDS ${LIBCXX_DEPS} ${toolchain_deps}
618 COMMAND ${CMAKE_COMMAND} -E touch ${prefix}/CMakeCache.txt
619 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
620 COMMENT "Clobbering bootstrap build directories"
623 add_custom_target(${name}-clobber
624 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
625 set_target_properties(${name}-clobber PROPERTIES FOLDER "Compiler-RT Misc")
627 set(PASSTHROUGH_VARIABLES
628 CMAKE_C_COMPILER_TARGET
629 CMAKE_CXX_COMPILER_TARGET
630 CMAKE_SHARED_LINKER_FLAGS
631 CMAKE_MODULE_LINKER_FLAGS
632 CMAKE_EXE_LINKER_FLAGS
646 LIBCXX_HAS_PTHREAD_LIB
648 LIBCXX_USE_COMPILER_RT
649 LIBCXXABI_HAS_PTHREAD_LIB
654 foreach(variable ${PASSTHROUGH_VARIABLES})
655 get_property(is_value_set CACHE ${variable} PROPERTY VALUE SET)
657 get_property(value CACHE ${variable} PROPERTY VALUE)
658 list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${value})
662 string(REPLACE ";" " " LIBCXX_C_FLAGS "${LIBCXX_CFLAGS}")
663 get_property(C_FLAGS CACHE CMAKE_C_FLAGS PROPERTY VALUE)
664 set(LIBCXX_C_FLAGS "${LIBCXX_C_FLAGS} ${C_FLAGS}")
666 string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CFLAGS}")
667 get_property(CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
668 set(LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS} ${CXX_FLAGS}")
670 ExternalProject_Add(${name}
671 DEPENDS ${name}-clobber ${LIBCXX_DEPS}
672 PREFIX ${CMAKE_CURRENT_BINARY_DIR}/${name}
673 SOURCE_DIR ${LLVM_MAIN_SRC_DIR}/../runtimes
675 CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES}
677 -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS}
678 -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS}
679 -DCMAKE_BUILD_TYPE=Release
680 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY
681 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR}
682 -DLLVM_ENABLE_RUNTIMES=libcxx|libcxxabi
683 -DLIBCXXABI_ENABLE_SHARED=OFF
684 -DLIBCXXABI_HERMETIC_STATIC_LIBRARY=ON
685 -DLIBCXXABI_INCLUDE_TESTS=OFF
686 -DLIBCXX_CXX_ABI=libcxxabi
687 -DLIBCXX_ENABLE_SHARED=OFF
688 -DLIBCXX_HERMETIC_STATIC_LIBRARY=ON
689 -DLIBCXX_INCLUDE_BENCHMARKS=OFF
690 -DLIBCXX_INCLUDE_TESTS=OFF
691 -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
694 STEP_TARGETS configure build
696 USES_TERMINAL_CONFIGURE 1
697 USES_TERMINAL_BUILD 1
698 USES_TERMINAL_INSTALL 1
700 EXCLUDE_FROM_ALL TRUE
701 BUILD_BYPRODUCTS "${prefix}/lib/libc++.a" "${prefix}/lib/libc++abi.a"
704 if (CMAKE_GENERATOR MATCHES "Make")
705 set(run_clean "$(MAKE)" "-C" "${prefix}" "clean")
707 set(run_clean ${CMAKE_COMMAND} --build ${prefix} --target clean
708 --config "$<CONFIG>")
711 ExternalProject_Add_Step(${name} clean
713 COMMENT "Cleaning ${name}..."
716 WORKING_DIRECTORY ${prefix}
720 ExternalProject_Add_StepTargets(${name} clean)
722 if(LIBCXX_USE_TOOLCHAIN)
723 add_dependencies(${name}-clean ${name}-clobber)
724 set_target_properties(${name}-clean PROPERTIES
725 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
729 function(rt_externalize_debuginfo name)
730 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO)
734 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP)
735 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>)
739 if(CMAKE_CXX_FLAGS MATCHES "-flto"
740 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto")
742 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o)
743 set_property(TARGET ${name} APPEND_STRING PROPERTY
744 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}")
746 add_custom_command(TARGET ${name} POST_BUILD
747 COMMAND xcrun dsymutil $<TARGET_FILE:${name}>
750 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!")
755 # Configure lit configuration files, including compiler-rt specific variables.
756 function(configure_compiler_rt_lit_site_cfg input output)
757 set_llvm_build_mode()
759 get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
761 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
762 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir})
764 configure_lit_site_cfg(${input} ${output})