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