1include(ExternalProject) 2include(CompilerRTUtils) 3 4function(set_target_output_directories target output_dir) 5 # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators 6 # append a per-configuration subdirectory to the specified directory. 7 # To avoid the appended folder, the configuration specific variable must be 8 # set 'RUNTIME_OUTPUT_DIRECTORY_${CONF}': 9 # RUNTIME_OUTPUT_DIRECTORY_DEBUG, RUNTIME_OUTPUT_DIRECTORY_RELEASE, ... 10 if(CMAKE_CONFIGURATION_TYPES) 11 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES}) 12 string(TOUPPER "${build_mode}" CONFIG_SUFFIX) 13 set_target_properties("${target}" PROPERTIES 14 "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir} 15 "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir} 16 "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${output_dir}) 17 endforeach() 18 else() 19 set_target_properties("${target}" PROPERTIES 20 ARCHIVE_OUTPUT_DIRECTORY ${output_dir} 21 LIBRARY_OUTPUT_DIRECTORY ${output_dir} 22 RUNTIME_OUTPUT_DIRECTORY ${output_dir}) 23 endif() 24endfunction() 25 26# Tries to add an "object library" target for a given list of OSs and/or 27# architectures with name "<name>.<arch>" for non-Darwin platforms if 28# architecture can be targeted, and "<name>.<os>" for Darwin platforms. 29# add_compiler_rt_object_libraries(<name> 30# OS <os names> 31# ARCHS <architectures> 32# SOURCES <source files> 33# CFLAGS <compile flags> 34# DEFS <compile definitions> 35# DEPS <dependencies> 36# ADDITIONAL_HEADERS <header files>) 37function(add_compiler_rt_object_libraries name) 38 cmake_parse_arguments(LIB "" "" "OS;ARCHS;SOURCES;CFLAGS;DEFS;DEPS;ADDITIONAL_HEADERS" 39 ${ARGN}) 40 set(libnames) 41 if(APPLE) 42 foreach(os ${LIB_OS}) 43 set(libname "${name}.${os}") 44 set(libnames ${libnames} ${libname}) 45 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS}) 46 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS) 47 endforeach() 48 else() 49 foreach(arch ${LIB_ARCHS}) 50 set(libname "${name}.${arch}") 51 set(libnames ${libnames} ${libname}) 52 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS}) 53 if(NOT CAN_TARGET_${arch}) 54 message(FATAL_ERROR "Architecture ${arch} can't be targeted") 55 return() 56 endif() 57 endforeach() 58 endif() 59 60 # Add headers to LIB_SOURCES for IDEs 61 compiler_rt_process_sources(LIB_SOURCES 62 ${LIB_SOURCES} 63 ADDITIONAL_HEADERS 64 ${LIB_ADDITIONAL_HEADERS} 65 ) 66 67 foreach(libname ${libnames}) 68 add_library(${libname} OBJECT ${LIB_SOURCES}) 69 if(LIB_DEPS) 70 add_dependencies(${libname} ${LIB_DEPS}) 71 endif() 72 73 # Strip out -msse3 if this isn't macOS. 74 set(target_flags ${LIB_CFLAGS}) 75 if(APPLE AND NOT "${libname}" MATCHES ".*\.osx.*") 76 list(REMOVE_ITEM target_flags "-msse3") 77 endif() 78 79 set_target_compile_flags(${libname} 80 ${extra_cflags_${libname}} ${target_flags}) 81 set_property(TARGET ${libname} APPEND PROPERTY 82 COMPILE_DEFINITIONS ${LIB_DEFS}) 83 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Libraries") 84 if(APPLE) 85 set_target_properties(${libname} PROPERTIES 86 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}") 87 endif() 88 endforeach() 89endfunction() 90 91# Takes a list of object library targets, and a suffix and appends the proper 92# TARGET_OBJECTS string to the output variable. 93# format_object_libs(<output> <suffix> ...) 94macro(format_object_libs output suffix) 95 foreach(lib ${ARGN}) 96 list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>) 97 endforeach() 98endmacro() 99 100function(add_compiler_rt_component name) 101 add_custom_target(${name}) 102 set_target_properties(${name} PROPERTIES FOLDER "Compiler-RT Misc") 103 if(COMMAND runtime_register_component) 104 runtime_register_component(${name}) 105 endif() 106 add_dependencies(compiler-rt ${name}) 107endfunction() 108 109function(add_asm_sources output) 110 set(${output} ${ARGN} PARENT_SCOPE) 111 # Xcode will try to compile asm files as C ('clang -x c'), and that will fail. 112 if (${CMAKE_GENERATOR} STREQUAL "Xcode") 113 enable_language(ASM) 114 else() 115 # Pass ASM file directly to the C++ compiler. 116 set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C) 117 endif() 118endfunction() 119 120macro(set_output_name output name arch) 121 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR) 122 set(${output} ${name}) 123 else() 124 if(ANDROID AND ${arch} STREQUAL "i386") 125 set(${output} "${name}-i686${COMPILER_RT_OS_SUFFIX}") 126 else() 127 set(${output} "${name}-${arch}${COMPILER_RT_OS_SUFFIX}") 128 endif() 129 endif() 130endmacro() 131 132# Adds static or shared runtime for a list of architectures and operating 133# systems and puts it in the proper directory in the build and install trees. 134# add_compiler_rt_runtime(<name> 135# {OBJECT|STATIC|SHARED} 136# ARCHS <architectures> 137# OS <os list> 138# SOURCES <source files> 139# CFLAGS <compile flags> 140# LINK_FLAGS <linker flags> 141# DEFS <compile definitions> 142# LINK_LIBS <linked libraries> (only for shared library) 143# OBJECT_LIBS <object libraries to use as sources> 144# PARENT_TARGET <convenience parent target> 145# ADDITIONAL_HEADERS <header files>) 146function(add_compiler_rt_runtime name type) 147 if(NOT type MATCHES "^(OBJECT|STATIC|SHARED)$") 148 message(FATAL_ERROR "type argument must be OBJECT, STATIC or SHARED") 149 return() 150 endif() 151 cmake_parse_arguments(LIB 152 "" 153 "PARENT_TARGET" 154 "OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS" 155 ${ARGN}) 156 set(libnames) 157 # Until we support this some other way, build compiler-rt runtime without LTO 158 # to allow non-LTO projects to link with it. 159 if(COMPILER_RT_HAS_FNO_LTO_FLAG) 160 set(NO_LTO_FLAGS "-fno-lto") 161 else() 162 set(NO_LTO_FLAGS "") 163 endif() 164 165 list(LENGTH LIB_SOURCES LIB_SOURCES_LENGTH) 166 if (${LIB_SOURCES_LENGTH} GREATER 0) 167 # Add headers to LIB_SOURCES for IDEs. It doesn't make sense to 168 # do this for a runtime library that only consists of OBJECT 169 # libraries, so only add the headers when source files are present. 170 compiler_rt_process_sources(LIB_SOURCES 171 ${LIB_SOURCES} 172 ADDITIONAL_HEADERS 173 ${LIB_ADDITIONAL_HEADERS} 174 ) 175 endif() 176 177 if(APPLE) 178 foreach(os ${LIB_OS}) 179 # Strip out -msse3 if this isn't macOS. 180 list(LENGTH LIB_CFLAGS HAS_EXTRA_CFLAGS) 181 if(HAS_EXTRA_CFLAGS AND NOT "${os}" MATCHES "^(osx)$") 182 list(REMOVE_ITEM LIB_CFLAGS "-msse3") 183 endif() 184 if(type STREQUAL "STATIC") 185 set(libname "${name}_${os}") 186 else() 187 set(libname "${name}_${os}_dynamic") 188 set(extra_link_flags_${libname} ${DARWIN_${os}_LINK_FLAGS} ${LIB_LINK_FLAGS}) 189 endif() 190 list_intersect(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS) 191 if(LIB_ARCHS_${libname}) 192 list(APPEND libnames ${libname}) 193 set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS}) 194 set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) 195 set(sources_${libname} ${LIB_SOURCES}) 196 format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS}) 197 get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir_${libname}) 198 get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_${libname}) 199 endif() 200 endforeach() 201 else() 202 foreach(arch ${LIB_ARCHS}) 203 if(NOT CAN_TARGET_${arch}) 204 message(FATAL_ERROR "Architecture ${arch} can't be targeted") 205 return() 206 endif() 207 if(type STREQUAL "OBJECT") 208 set(libname "${name}-${arch}") 209 set_output_name(output_name_${libname} ${name}${COMPILER_RT_OS_SUFFIX} ${arch}) 210 elseif(type STREQUAL "STATIC") 211 set(libname "${name}-${arch}") 212 set_output_name(output_name_${libname} ${name} ${arch}) 213 else() 214 set(libname "${name}-dynamic-${arch}") 215 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) 216 set(extra_link_flags_${libname} ${TARGET_${arch}_LINK_FLAGS} ${LIB_LINK_FLAGS}) 217 if(WIN32) 218 set_output_name(output_name_${libname} ${name}_dynamic ${arch}) 219 else() 220 set_output_name(output_name_${libname} ${name} ${arch}) 221 endif() 222 endif() 223 set(sources_${libname} ${LIB_SOURCES}) 224 format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS}) 225 set(libnames ${libnames} ${libname}) 226 set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${NO_LTO_FLAGS} ${LIB_CFLAGS}) 227 get_compiler_rt_output_dir(${arch} output_dir_${libname}) 228 get_compiler_rt_install_dir(${arch} install_dir_${libname}) 229 endforeach() 230 endif() 231 232 if(NOT libnames) 233 return() 234 endif() 235 236 if(LIB_PARENT_TARGET) 237 # If the parent targets aren't created we should create them 238 if(NOT TARGET ${LIB_PARENT_TARGET}) 239 add_custom_target(${LIB_PARENT_TARGET}) 240 set_target_properties(${LIB_PARENT_TARGET} PROPERTIES 241 FOLDER "Compiler-RT Misc") 242 endif() 243 if(NOT TARGET install-${LIB_PARENT_TARGET}) 244 # The parent install target specifies the parent component to scrape up 245 # anything not installed by the individual install targets, and to handle 246 # installation when running the multi-configuration generators. 247 add_custom_target(install-${LIB_PARENT_TARGET} 248 DEPENDS ${LIB_PARENT_TARGET} 249 COMMAND "${CMAKE_COMMAND}" 250 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET} 251 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") 252 add_custom_target(install-${LIB_PARENT_TARGET}-stripped 253 DEPENDS ${LIB_PARENT_TARGET} 254 COMMAND "${CMAKE_COMMAND}" 255 -DCMAKE_INSTALL_COMPONENT=${LIB_PARENT_TARGET} 256 -DCMAKE_INSTALL_DO_STRIP=1 257 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") 258 set_target_properties(install-${LIB_PARENT_TARGET} PROPERTIES 259 FOLDER "Compiler-RT Misc") 260 set_target_properties(install-${LIB_PARENT_TARGET}-stripped PROPERTIES 261 FOLDER "Compiler-RT Misc") 262 add_dependencies(install-compiler-rt install-${LIB_PARENT_TARGET}) 263 add_dependencies(install-compiler-rt-stripped install-${LIB_PARENT_TARGET}-stripped) 264 endif() 265 endif() 266 267 foreach(libname ${libnames}) 268 # If you are using a multi-configuration generator we don't generate 269 # per-library install rules, so we fall back to the parent target COMPONENT 270 if(CMAKE_CONFIGURATION_TYPES AND LIB_PARENT_TARGET) 271 set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET}) 272 else() 273 set(COMPONENT_OPTION COMPONENT ${libname}) 274 endif() 275 276 if(type STREQUAL "OBJECT") 277 if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_TARGET) 278 list(APPEND extra_cflags_${libname} "--target=${CMAKE_C_COMPILER_TARGET}") 279 endif() 280 if(CMAKE_SYSROOT) 281 list(APPEND extra_cflags_${libname} "--sysroot=${CMAKE_SYSROOT}") 282 endif() 283 string(REPLACE ";" " " extra_cflags_${libname} "${extra_cflags_${libname}}") 284 string(REGEX MATCHALL "<[A-Za-z0-9_]*>" substitutions 285 ${CMAKE_C_COMPILE_OBJECT}) 286 set(compile_command_${libname} "${CMAKE_C_COMPILE_OBJECT}") 287 set(output_file_${libname} ${output_name_${libname}}${CMAKE_C_OUTPUT_EXTENSION}) 288 foreach(substitution ${substitutions}) 289 if(substitution STREQUAL "<CMAKE_C_COMPILER>") 290 string(REPLACE "<CMAKE_C_COMPILER>" "${CMAKE_C_COMPILER}" 291 compile_command_${libname} ${compile_command_${libname}}) 292 elseif(substitution STREQUAL "<OBJECT>") 293 string(REPLACE "<OBJECT>" "${output_dir_${libname}}/${output_file_${libname}}" 294 compile_command_${libname} ${compile_command_${libname}}) 295 elseif(substitution STREQUAL "<SOURCE>") 296 string(REPLACE "<SOURCE>" "${sources_${libname}}" 297 compile_command_${libname} ${compile_command_${libname}}) 298 elseif(substitution STREQUAL "<FLAGS>") 299 string(REPLACE "<FLAGS>" "${CMAKE_C_FLAGS} ${extra_cflags_${libname}}" 300 compile_command_${libname} ${compile_command_${libname}}) 301 else() 302 string(REPLACE "${substitution}" "" compile_command_${libname} 303 ${compile_command_${libname}}) 304 endif() 305 endforeach() 306 separate_arguments(compile_command_${libname}) 307 add_custom_command( 308 OUTPUT ${output_dir_${libname}}/${output_file_${libname}} 309 COMMAND ${compile_command_${libname}} 310 DEPENDS ${sources_${libname}} 311 COMMENT "Building C object ${output_file_${libname}}") 312 add_custom_target(${libname} DEPENDS ${output_dir_${libname}}/${output_file_${libname}}) 313 install(FILES ${output_dir_${libname}}/${output_file_${libname}} 314 DESTINATION ${install_dir_${libname}} 315 ${COMPONENT_OPTION}) 316 else() 317 add_library(${libname} ${type} ${sources_${libname}}) 318 set_target_compile_flags(${libname} ${extra_cflags_${libname}}) 319 set_target_link_flags(${libname} ${extra_link_flags_${libname}}) 320 set_property(TARGET ${libname} APPEND PROPERTY 321 COMPILE_DEFINITIONS ${LIB_DEFS}) 322 set_target_output_directories(${libname} ${output_dir_${libname}}) 323 install(TARGETS ${libname} 324 ARCHIVE DESTINATION ${install_dir_${libname}} 325 ${COMPONENT_OPTION} 326 LIBRARY DESTINATION ${install_dir_${libname}} 327 ${COMPONENT_OPTION} 328 RUNTIME DESTINATION ${install_dir_${libname}} 329 ${COMPONENT_OPTION}) 330 endif() 331 set_target_properties(${libname} PROPERTIES 332 OUTPUT_NAME ${output_name_${libname}}) 333 set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime") 334 if(LIB_LINK_LIBS) 335 target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS}) 336 endif() 337 if(${type} STREQUAL "SHARED") 338 if(COMMAND llvm_setup_rpath) 339 llvm_setup_rpath(${libname}) 340 endif() 341 if(WIN32 AND NOT CYGWIN AND NOT MINGW) 342 set_target_properties(${libname} PROPERTIES IMPORT_PREFIX "") 343 set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib") 344 endif() 345 if(APPLE) 346 # Ad-hoc sign the dylibs 347 add_custom_command(TARGET ${libname} 348 POST_BUILD 349 COMMAND codesign --sign - $<TARGET_FILE:${libname}> 350 WORKING_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR} 351 ) 352 endif() 353 endif() 354 355 # We only want to generate per-library install targets if you aren't using 356 # an IDE because the extra targets get cluttered in IDEs. 357 if(NOT CMAKE_CONFIGURATION_TYPES) 358 add_custom_target(install-${libname} 359 DEPENDS ${libname} 360 COMMAND "${CMAKE_COMMAND}" 361 -DCMAKE_INSTALL_COMPONENT=${libname} 362 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") 363 add_custom_target(install-${libname}-stripped 364 DEPENDS ${libname} 365 COMMAND "${CMAKE_COMMAND}" 366 -DCMAKE_INSTALL_COMPONENT=${libname} 367 -DCMAKE_INSTALL_DO_STRIP=1 368 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake") 369 # If you have a parent target specified, we bind the new install target 370 # to the parent install target. 371 if(LIB_PARENT_TARGET) 372 add_dependencies(install-${LIB_PARENT_TARGET} install-${libname}) 373 add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${libname}-stripped) 374 endif() 375 endif() 376 if(APPLE) 377 set_target_properties(${libname} PROPERTIES 378 OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}") 379 endif() 380 381 if(type STREQUAL "SHARED") 382 rt_externalize_debuginfo(${libname}) 383 endif() 384 endforeach() 385 if(LIB_PARENT_TARGET) 386 add_dependencies(${LIB_PARENT_TARGET} ${libnames}) 387 endif() 388endfunction() 389 390# when cross compiling, COMPILER_RT_TEST_COMPILER_CFLAGS help 391# in compilation and linking of unittests. 392string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_CFLAGS}") 393set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS}) 394 395# Unittests support. 396set(COMPILER_RT_GTEST_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest) 397set(COMPILER_RT_GTEST_SOURCE ${COMPILER_RT_GTEST_PATH}/src/gtest-all.cc) 398set(COMPILER_RT_GTEST_CFLAGS 399 -DGTEST_NO_LLVM_RAW_OSTREAM=1 400 -DGTEST_HAS_RTTI=0 401 -I${COMPILER_RT_GTEST_PATH}/include 402 -I${COMPILER_RT_GTEST_PATH} 403) 404 405# Mocking support. 406set(COMPILER_RT_GMOCK_PATH ${LLVM_MAIN_SRC_DIR}/utils/unittest/googlemock) 407set(COMPILER_RT_GMOCK_SOURCE ${COMPILER_RT_GMOCK_PATH}/src/gmock-all.cc) 408set(COMPILER_RT_GMOCK_CFLAGS 409 -DGTEST_NO_LLVM_RAW_OSTREAM=1 410 -DGTEST_HAS_RTTI=0 411 -I${COMPILER_RT_GMOCK_PATH}/include 412 -I${COMPILER_RT_GMOCK_PATH} 413) 414 415append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 COMPILER_RT_UNITTEST_CFLAGS) 416append_list_if(COMPILER_RT_HAS_WCOVERED_SWITCH_DEFAULT_FLAG -Wno-covered-switch-default COMPILER_RT_UNITTEST_CFLAGS) 417 418if(MSVC) 419 # gtest use a lot of stuff marked as deprecated on Windows. 420 list(APPEND COMPILER_RT_GTEST_CFLAGS -Wno-deprecated-declarations) 421endif() 422 423# Compile and register compiler-rt tests. 424# generate_compiler_rt_tests(<output object files> <test_suite> <test_name> 425# <test architecture> 426# KIND <custom prefix> 427# SUBDIR <subdirectory for testing binary> 428# SOURCES <sources to compile> 429# RUNTIME <tests runtime to link in> 430# CFLAGS <compile-time flags> 431# COMPILE_DEPS <compile-time dependencies> 432# DEPS <dependencies> 433# LINK_FLAGS <flags to use during linking> 434# ) 435function(generate_compiler_rt_tests test_objects test_suite testname arch) 436 cmake_parse_arguments(TEST "" "KIND;RUNTIME;SUBDIR" 437 "SOURCES;COMPILE_DEPS;DEPS;CFLAGS;LINK_FLAGS" ${ARGN}) 438 439 foreach(source ${TEST_SOURCES}) 440 sanitizer_test_compile( 441 "${test_objects}" "${source}" "${arch}" 442 KIND ${TEST_KIND} 443 COMPILE_DEPS ${TEST_COMPILE_DEPS} 444 DEPS ${TEST_DEPS} 445 CFLAGS ${TEST_CFLAGS} 446 ) 447 endforeach() 448 449 set(TEST_DEPS ${${test_objects}}) 450 451 if(NOT "${TEST_RUNTIME}" STREQUAL "") 452 list(APPEND TEST_DEPS ${TEST_RUNTIME}) 453 list(APPEND "${test_objects}" $<TARGET_FILE:${TEST_RUNTIME}>) 454 endif() 455 456 add_compiler_rt_test(${test_suite} "${testname}" "${arch}" 457 SUBDIR ${TEST_SUBDIR} 458 OBJECTS ${${test_objects}} 459 DEPS ${TEST_DEPS} 460 LINK_FLAGS ${TEST_LINK_FLAGS} 461 ) 462 set("${test_objects}" "${${test_objects}}" PARENT_SCOPE) 463endfunction() 464 465# Link objects into a single executable with COMPILER_RT_TEST_COMPILER, 466# using specified link flags. Make executable a part of provided 467# test_suite. 468# add_compiler_rt_test(<test_suite> <test_name> <arch> 469# SUBDIR <subdirectory for binary> 470# OBJECTS <object files> 471# DEPS <deps (e.g. runtime libs)> 472# LINK_FLAGS <link flags>) 473function(add_compiler_rt_test test_suite test_name arch) 474 cmake_parse_arguments(TEST "" "SUBDIR" "OBJECTS;DEPS;LINK_FLAGS" "" ${ARGN}) 475 set(output_dir ${CMAKE_CURRENT_BINARY_DIR}) 476 if(TEST_SUBDIR) 477 set(output_dir "${output_dir}/${TEST_SUBDIR}") 478 endif() 479 set(output_dir "${output_dir}/${CMAKE_CFG_INTDIR}") 480 file(MAKE_DIRECTORY "${output_dir}") 481 set(output_bin "${output_dir}/${test_name}") 482 if(MSVC) 483 set(output_bin "${output_bin}.exe") 484 endif() 485 486 # Use host compiler in a standalone build, and just-built Clang otherwise. 487 if(NOT COMPILER_RT_STANDALONE_BUILD) 488 list(APPEND TEST_DEPS clang) 489 endif() 490 491 get_target_flags_for_arch(${arch} TARGET_LINK_FLAGS) 492 list(APPEND TEST_LINK_FLAGS ${TARGET_LINK_FLAGS}) 493 494 # If we're not on MSVC, include the linker flags from CMAKE but override them 495 # with the provided link flags. This ensures that flags which are required to 496 # link programs at all are included, but the changes needed for the test 497 # trump. With MSVC we can't do that because CMake is set up to run link.exe 498 # when linking, not the compiler. Here, we hack it to use the compiler 499 # because we want to use -fsanitize flags. 500 if(NOT MSVC) 501 set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}") 502 separate_arguments(TEST_LINK_FLAGS) 503 endif() 504 add_custom_command( 505 OUTPUT "${output_bin}" 506 COMMAND ${COMPILER_RT_TEST_COMPILER} ${TEST_OBJECTS} -o "${output_bin}" 507 ${TEST_LINK_FLAGS} 508 DEPENDS ${TEST_DEPS} 509 ) 510 add_custom_target(T${test_name} DEPENDS "${output_bin}") 511 set_target_properties(T${test_name} PROPERTIES FOLDER "Compiler-RT Tests") 512 513 # Make the test suite depend on the binary. 514 add_dependencies(${test_suite} T${test_name}) 515endfunction() 516 517macro(add_compiler_rt_resource_file target_name file_name component) 518 set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}") 519 set(dst_file "${COMPILER_RT_OUTPUT_DIR}/share/${file_name}") 520 add_custom_command(OUTPUT ${dst_file} 521 DEPENDS ${src_file} 522 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file} 523 COMMENT "Copying ${file_name}...") 524 add_custom_target(${target_name} DEPENDS ${dst_file}) 525 # Install in Clang resource directory. 526 install(FILES ${file_name} 527 DESTINATION ${COMPILER_RT_INSTALL_PATH}/share 528 COMPONENT ${component}) 529 add_dependencies(${component} ${target_name}) 530 531 set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc") 532endmacro() 533 534macro(add_compiler_rt_script name) 535 set(dst ${COMPILER_RT_EXEC_OUTPUT_DIR}/${name}) 536 set(src ${CMAKE_CURRENT_SOURCE_DIR}/${name}) 537 add_custom_command(OUTPUT ${dst} 538 DEPENDS ${src} 539 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src} ${dst} 540 COMMENT "Copying ${name}...") 541 add_custom_target(${name} DEPENDS ${dst}) 542 install(FILES ${dst} 543 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE 544 DESTINATION ${COMPILER_RT_INSTALL_PATH}/bin) 545endmacro(add_compiler_rt_script src name) 546 547# Builds custom version of libc++ and installs it in <prefix>. 548# Can be used to build sanitized versions of libc++ for running unit tests. 549# add_custom_libcxx(<name> <prefix> 550# DEPS <list of build deps> 551# CFLAGS <list of compile flags> 552# USE_TOOLCHAIN) 553macro(add_custom_libcxx name prefix) 554 if(NOT COMPILER_RT_LIBCXX_PATH) 555 message(FATAL_ERROR "libcxx not found!") 556 endif() 557 if(NOT COMPILER_RT_LIBCXXABI_PATH) 558 message(FATAL_ERROR "libcxxabi not found!") 559 endif() 560 561 cmake_parse_arguments(LIBCXX "USE_TOOLCHAIN" "" "DEPS;CFLAGS;CMAKE_ARGS" ${ARGN}) 562 563 if(LIBCXX_USE_TOOLCHAIN) 564 set(compiler_args -DCMAKE_C_COMPILER=${COMPILER_RT_TEST_COMPILER} 565 -DCMAKE_CXX_COMPILER=${COMPILER_RT_TEST_CXX_COMPILER}) 566 if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT RUNTIMES_BUILD) 567 set(toolchain_deps $<TARGET_FILE:clang>) 568 set(force_deps DEPENDS $<TARGET_FILE:clang>) 569 endif() 570 else() 571 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 572 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}) 573 endif() 574 575 set(STAMP_DIR ${prefix}-stamps/) 576 set(BINARY_DIR ${prefix}-bins/) 577 578 add_custom_target(${name}-clear 579 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR} 580 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR} 581 COMMENT "Clobbering ${name} build and stamp directories" 582 USES_TERMINAL 583 ) 584 set_target_properties(${name}-clear PROPERTIES FOLDER "Compiler-RT Misc") 585 586 add_custom_command( 587 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 588 DEPENDS ${LIBCXX_DEPS} ${toolchain_deps} 589 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt 590 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir 591 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 592 COMMENT "Clobbering bootstrap build and stamp directories" 593 ) 594 595 add_custom_target(${name}-clobber 596 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 597 set_target_properties(${name}-clobber PROPERTIES FOLDER "Compiler-RT Misc") 598 599 set(PASSTHROUGH_VARIABLES 600 CMAKE_C_COMPILER_TARGET 601 CMAKE_CXX_COMPILER_TARGET 602 CMAKE_SHARED_LINKER_FLAGS 603 CMAKE_MODULE_LINKER_FLAGS 604 CMAKE_EXE_LINKER_FLAGS 605 CMAKE_INSTALL_PREFIX 606 CMAKE_MAKE_PROGRAM 607 CMAKE_LINKER 608 CMAKE_AR 609 CMAKE_RANLIB 610 CMAKE_NM 611 CMAKE_OBJCOPY 612 CMAKE_OBJDUMP 613 CMAKE_STRIP 614 CMAKE_SYSROOT 615 CMAKE_SYSTEM_NAME) 616 foreach(variable ${PASSTHROUGH_VARIABLES}) 617 get_property(is_value_set CACHE ${variable} PROPERTY VALUE SET) 618 if(${is_value_set}) 619 get_property(value CACHE ${variable} PROPERTY VALUE) 620 list(APPEND CMAKE_PASSTHROUGH_VARIABLES -D${variable}=${value}) 621 endif() 622 endforeach() 623 624 string(REPLACE ";" " " LIBCXX_C_FLAGS "${LIBCXX_CFLAGS}") 625 get_property(C_FLAGS CACHE CMAKE_C_FLAGS PROPERTY VALUE) 626 set(LIBCXX_C_FLAGS "${LIBCXX_C_FLAGS} ${C_FLAGS}") 627 628 string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CFLAGS}") 629 get_property(CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE) 630 set(LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS} ${CXX_FLAGS}") 631 632 ExternalProject_Add(${name} 633 DEPENDS ${name}-clobber ${LIBCXX_DEPS} 634 PREFIX ${prefix} 635 SOURCE_DIR ${COMPILER_RT_SOURCE_DIR}/cmake/Modules/CustomLibcxx 636 STAMP_DIR ${STAMP_DIR} 637 BINARY_DIR ${BINARY_DIR} 638 CMAKE_ARGS ${CMAKE_PASSTHROUGH_VARIABLES} 639 ${compiler_args} 640 -DCMAKE_C_FLAGS=${LIBCXX_C_FLAGS} 641 -DCMAKE_CXX_FLAGS=${LIBCXX_CXX_FLAGS} 642 -DCMAKE_BUILD_TYPE=Release 643 -DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY 644 -DLLVM_PATH=${LLVM_MAIN_SRC_DIR} 645 -DLLVM_BINARY_DIR=${prefix} 646 -DLLVM_LIBRARY_OUTPUT_INTDIR=${prefix}/lib 647 -DCOMPILER_RT_LIBCXX_PATH=${COMPILER_RT_LIBCXX_PATH} 648 -DCOMPILER_RT_LIBCXXABI_PATH=${COMPILER_RT_LIBCXXABI_PATH} 649 ${LIBCXX_CMAKE_ARGS} 650 INSTALL_COMMAND "" 651 STEP_TARGETS configure build 652 BUILD_ALWAYS 1 653 USES_TERMINAL_CONFIGURE 1 654 USES_TERMINAL_BUILD 1 655 USES_TERMINAL_INSTALL 1 656 EXCLUDE_FROM_ALL TRUE 657 ) 658 659 if (CMAKE_GENERATOR MATCHES "Make") 660 set(run_clean "$(MAKE)" "-C" "${BINARY_DIR}" "clean") 661 else() 662 set(run_clean ${CMAKE_COMMAND} --build ${BINARY_DIR} --target clean 663 --config "$<CONFIG>") 664 endif() 665 666 ExternalProject_Add_Step(${name} clean 667 COMMAND ${run_clean} 668 COMMENT "Cleaning ${name}..." 669 DEPENDEES configure 670 ${force_deps} 671 WORKING_DIRECTORY ${BINARY_DIR} 672 EXCLUDE_FROM_MAIN 1 673 USES_TERMINAL 1 674 ) 675 ExternalProject_Add_StepTargets(${name} clean) 676 677 if(LIBCXX_USE_TOOLCHAIN) 678 add_dependencies(${name}-clean ${name}-clobber) 679 set_target_properties(${name}-clean PROPERTIES 680 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 681 endif() 682endmacro() 683 684function(rt_externalize_debuginfo name) 685 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO) 686 return() 687 endif() 688 689 if(NOT COMPILER_RT_EXTERNALIZE_DEBUGINFO_SKIP_STRIP) 690 set(strip_command COMMAND xcrun strip -Sl $<TARGET_FILE:${name}>) 691 endif() 692 693 if(APPLE) 694 if(CMAKE_CXX_FLAGS MATCHES "-flto" 695 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto") 696 697 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o) 698 set_property(TARGET ${name} APPEND_STRING PROPERTY 699 LINK_FLAGS " -Wl,-object_path_lto -Wl,${lto_object}") 700 endif() 701 add_custom_command(TARGET ${name} POST_BUILD 702 COMMAND xcrun dsymutil $<TARGET_FILE:${name}> 703 ${strip_command}) 704 else() 705 message(FATAL_ERROR "COMPILER_RT_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!") 706 endif() 707endfunction() 708 709 710# Configure lit configuration files, including compiler-rt specific variables. 711function(configure_compiler_rt_lit_site_cfg input output) 712 set_llvm_build_mode() 713 714 get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir) 715 716 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER}) 717 string(REPLACE ${CMAKE_CFG_INTDIR} ${LLVM_BUILD_MODE} COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR ${output_dir}) 718 719 configure_lit_site_cfg(${input} ${output}) 720endfunction() 721