1include(LLVMDistributionSupport) 2include(LLVMProcessSources) 3include(LLVM-Config) 4include(DetermineGCCCompatible) 5 6function(llvm_update_compile_flags name) 7 get_property(sources TARGET ${name} PROPERTY SOURCES) 8 if("${sources}" MATCHES "\\.c(;|$)") 9 set(update_src_props ON) 10 endif() 11 12 list(APPEND LLVM_COMPILE_CFLAGS " ${LLVM_COMPILE_FLAGS}") 13 14 # LLVM_REQUIRES_EH is an internal flag that individual targets can use to 15 # force EH 16 if(LLVM_REQUIRES_EH OR LLVM_ENABLE_EH) 17 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 18 message(AUTHOR_WARNING "Exception handling requires RTTI. Enabling RTTI for ${name}") 19 set(LLVM_REQUIRES_RTTI ON) 20 endif() 21 if(MSVC) 22 list(APPEND LLVM_COMPILE_FLAGS "/EHsc") 23 endif() 24 else() 25 if(LLVM_COMPILER_IS_GCC_COMPATIBLE) 26 list(APPEND LLVM_COMPILE_FLAGS "-fno-exceptions") 27 if(NOT LLVM_ENABLE_UNWIND_TABLES) 28 list(APPEND LLVM_COMPILE_FLAGS "-fno-unwind-tables") 29 list(APPEND LLVM_COMPILE_FLAGS "-fno-asynchronous-unwind-tables") 30 endif() 31 elseif(MSVC) 32 list(APPEND LLVM_COMPILE_DEFINITIONS _HAS_EXCEPTIONS=0) 33 list(APPEND LLVM_COMPILE_FLAGS "/EHs-c-") 34 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 35 list(APPEND LLVM_COMPILE_FLAGS "-qnoeh") 36 endif() 37 endif() 38 39 # LLVM_REQUIRES_RTTI is an internal flag that individual 40 # targets can use to force RTTI 41 set(LLVM_CONFIG_HAS_RTTI YES CACHE INTERNAL "") 42 if(NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) 43 set(LLVM_CONFIG_HAS_RTTI NO CACHE INTERNAL "") 44 list(APPEND LLVM_COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) 45 if (LLVM_COMPILER_IS_GCC_COMPATIBLE) 46 list(APPEND LLVM_COMPILE_FLAGS "-fno-rtti") 47 elseif (MSVC) 48 list(APPEND LLVM_COMPILE_FLAGS "/GR-") 49 elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") 50 list(APPEND LLVM_COMPILE_FLAGS "-qnortti") 51 endif () 52 elseif(MSVC) 53 list(APPEND LLVM_COMPILE_FLAGS "/GR") 54 endif() 55 56 # Assume that; 57 # - LLVM_COMPILE_FLAGS is list. 58 # - PROPERTY COMPILE_FLAGS is string. 59 string(REPLACE ";" " " target_compile_flags " ${LLVM_COMPILE_FLAGS}") 60 string(REPLACE ";" " " target_compile_cflags " ${LLVM_COMPILE_CFLAGS}") 61 62 if(update_src_props) 63 foreach(fn ${sources}) 64 get_filename_component(suf ${fn} EXT) 65 if("${suf}" STREQUAL ".cpp") 66 set_property(SOURCE ${fn} APPEND_STRING PROPERTY 67 COMPILE_FLAGS "${target_compile_flags}") 68 endif() 69 if("${suf}" STREQUAL ".c") 70 set_property(SOURCE ${fn} APPEND_STRING PROPERTY 71 COMPILE_FLAGS "${target_compile_cflags}") 72 endif() 73 endforeach() 74 else() 75 # Update target props, since all sources are C++. 76 set_property(TARGET ${name} APPEND_STRING PROPERTY 77 COMPILE_FLAGS "${target_compile_flags}") 78 endif() 79 80 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) 81endfunction() 82 83function(add_llvm_symbol_exports target_name export_file) 84 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 85 set(native_export_file "${target_name}.exports") 86 add_custom_command(OUTPUT ${native_export_file} 87 COMMAND sed -e "s/^/_/" < ${export_file} > ${native_export_file} 88 DEPENDS ${export_file} 89 VERBATIM 90 COMMENT "Creating export file for ${target_name}") 91 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 92 LINK_FLAGS " -Wl,-exported_symbols_list,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 93 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 94 # FIXME: `-Wl,-bE:` bypasses whatever handling there is in the build 95 # compiler driver to defer to the specified export list. 96 set(native_export_file "${export_file}") 97 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 98 LINK_FLAGS " -Wl,-bE:${export_file}") 99 elseif(LLVM_HAVE_LINK_VERSION_SCRIPT) 100 # Gold and BFD ld require a version script rather than a plain list. 101 set(native_export_file "${target_name}.exports") 102 # FIXME: Don't write the "local:" line on OpenBSD. 103 # in the export file, also add a linker script to version LLVM symbols (form: LLVM_N.M) 104 add_custom_command(OUTPUT ${native_export_file} 105 COMMAND echo "LLVM_${LLVM_VERSION_MAJOR} {" > ${native_export_file} 106 COMMAND grep -q "[[:alnum:]]" ${export_file} && echo " global:" >> ${native_export_file} || : 107 COMMAND sed -e "s/$/;/" -e "s/^/ /" < ${export_file} >> ${native_export_file} 108 COMMAND echo " local: *;" >> ${native_export_file} 109 COMMAND echo "};" >> ${native_export_file} 110 DEPENDS ${export_file} 111 VERBATIM 112 COMMENT "Creating export file for ${target_name}") 113 if (${LLVM_LINKER_IS_SOLARISLD}) 114 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 115 LINK_FLAGS " -Wl,-M,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 116 else() 117 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 118 LINK_FLAGS " -Wl,--version-script,\"${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}\"") 119 endif() 120 else() 121 set(native_export_file "${target_name}.def") 122 123 add_custom_command(OUTPUT ${native_export_file} 124 COMMAND "${Python3_EXECUTABLE}" -c "import sys;print(''.join(['EXPORTS\\n']+sys.stdin.readlines(),))" 125 < ${export_file} > ${native_export_file} 126 DEPENDS ${export_file} 127 VERBATIM 128 COMMENT "Creating export file for ${target_name}") 129 set(export_file_linker_flag "${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}") 130 if(MSVC) 131 set(export_file_linker_flag "/DEF:\"${export_file_linker_flag}\"") 132 endif() 133 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 134 LINK_FLAGS " ${export_file_linker_flag}") 135 endif() 136 137 add_custom_target(${target_name}_exports DEPENDS ${native_export_file}) 138 set_target_properties(${target_name}_exports PROPERTIES FOLDER "Misc") 139 140 get_property(srcs TARGET ${target_name} PROPERTY SOURCES) 141 foreach(src ${srcs}) 142 get_filename_component(extension ${src} EXT) 143 if(extension STREQUAL ".cpp") 144 set(first_source_file ${src}) 145 break() 146 endif() 147 endforeach() 148 149 # Force re-linking when the exports file changes. Actually, it 150 # forces recompilation of the source file. The LINK_DEPENDS target 151 # property only works for makefile-based generators. 152 # FIXME: This is not safe because this will create the same target 153 # ${native_export_file} in several different file: 154 # - One where we emitted ${target_name}_exports 155 # - One where we emitted the build command for the following object. 156 # set_property(SOURCE ${first_source_file} APPEND PROPERTY 157 # OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${native_export_file}) 158 159 set_property(DIRECTORY APPEND 160 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${native_export_file}) 161 162 add_dependencies(${target_name} ${target_name}_exports) 163 164 # Add dependency to *_exports later -- CMake issue 14747 165 list(APPEND LLVM_COMMON_DEPENDS ${target_name}_exports) 166 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE) 167endfunction(add_llvm_symbol_exports) 168 169if (NOT DEFINED LLVM_LINKER_DETECTED) 170 if(APPLE) 171 execute_process( 172 COMMAND "${CMAKE_LINKER}" -v 173 ERROR_VARIABLE stderr 174 ) 175 if("${stderr}" MATCHES "PROJECT:ld64") 176 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 177 set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "") 178 message(STATUS "Linker detection: ld64") 179 else() 180 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "") 181 message(STATUS "Linker detection: unknown") 182 endif() 183 elseif(NOT WIN32) 184 # Detect what linker we have here 185 if( LLVM_USE_LINKER ) 186 set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version) 187 else() 188 separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}") 189 set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version) 190 endif() 191 execute_process( 192 COMMAND ${command} 193 OUTPUT_VARIABLE stdout 194 ERROR_VARIABLE stderr 195 ) 196 if("${stdout}" MATCHES "GNU gold") 197 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 198 set(LLVM_LINKER_IS_GOLD YES CACHE INTERNAL "") 199 message(STATUS "Linker detection: GNU Gold") 200 elseif("${stdout}" MATCHES "^LLD") 201 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 202 set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "") 203 message(STATUS "Linker detection: LLD") 204 elseif("${stdout}" MATCHES "GNU ld") 205 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 206 set(LLVM_LINKER_IS_GNULD YES CACHE INTERNAL "") 207 message(STATUS "Linker detection: GNU ld") 208 elseif("${stderr}" MATCHES "Solaris Link Editors" OR 209 "${stdout}" MATCHES "Solaris Link Editors") 210 set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") 211 set(LLVM_LINKER_IS_SOLARISLD YES CACHE INTERNAL "") 212 message(STATUS "Linker detection: Solaris ld") 213 else() 214 set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "") 215 message(STATUS "Linker detection: unknown") 216 endif() 217 endif() 218endif() 219 220function(add_link_opts target_name) 221 # Don't use linker optimizations in debug builds since it slows down the 222 # linker in a context where the optimizations are not important. 223 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 224 225 # Pass -O3 to the linker. This enabled different optimizations on different 226 # linkers. 227 if(NOT (CMAKE_SYSTEM_NAME MATCHES "Darwin|SunOS|AIX|OS390" OR WIN32)) 228 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 229 LINK_FLAGS " -Wl,-O3") 230 endif() 231 232 if(NOT LLVM_NO_DEAD_STRIP) 233 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 234 # ld64's implementation of -dead_strip breaks tools that use plugins. 235 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 236 LINK_FLAGS " -Wl,-dead_strip") 237 elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS") 238 # Support for ld -z discard-unused=sections was only added in 239 # Solaris 11.4. 240 include(LLVMCheckLinkerFlag) 241 llvm_check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED) 242 if (LINKER_SUPPORTS_Z_DISCARD_UNUSED) 243 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 244 LINK_FLAGS " -Wl,-z,discard-unused=sections") 245 endif() 246 elseif(NOT MSVC AND NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD|AIX|OS390") 247 # TODO Revisit this later on z/OS. 248 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 249 LINK_FLAGS " -Wl,--gc-sections") 250 endif() 251 else() #LLVM_NO_DEAD_STRIP 252 if(${CMAKE_SYSTEM_NAME} MATCHES "AIX") 253 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 254 LINK_FLAGS " -Wl,-bnogc") 255 endif() 256 endif() 257 endif() 258 259 if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX") 260 set_property(TARGET ${target_name} APPEND_STRING PROPERTY 261 LINK_FLAGS " -Wl,-brtl") 262 endif() 263endfunction(add_link_opts) 264 265# Set each output directory according to ${CMAKE_CONFIGURATION_TYPES}. 266# Note: Don't set variables CMAKE_*_OUTPUT_DIRECTORY any more, 267# or a certain builder, for eaxample, msbuild.exe, would be confused. 268function(set_output_directory target) 269 cmake_parse_arguments(ARG "" "BINARY_DIR;LIBRARY_DIR" "" ${ARGN}) 270 271 # module_dir -- corresponding to LIBRARY_OUTPUT_DIRECTORY. 272 # It affects output of add_library(MODULE). 273 if(WIN32 OR CYGWIN) 274 # DLL platform 275 set(module_dir ${ARG_BINARY_DIR}) 276 else() 277 set(module_dir ${ARG_LIBRARY_DIR}) 278 endif() 279 if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".") 280 foreach(build_mode ${CMAKE_CONFIGURATION_TYPES}) 281 string(TOUPPER "${build_mode}" CONFIG_SUFFIX) 282 if(ARG_BINARY_DIR) 283 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINARY_DIR}) 284 set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi}) 285 endif() 286 if(ARG_LIBRARY_DIR) 287 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBRARY_DIR}) 288 set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li}) 289 endif() 290 if(module_dir) 291 string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${module_dir}) 292 set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi}) 293 endif() 294 endforeach() 295 else() 296 if(ARG_BINARY_DIR) 297 set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINARY_DIR}) 298 endif() 299 if(ARG_LIBRARY_DIR) 300 set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBRARY_DIR}) 301 endif() 302 if(module_dir) 303 set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${module_dir}) 304 endif() 305 endif() 306endfunction() 307 308# If on Windows and building with MSVC, add the resource script containing the 309# VERSIONINFO data to the project. This embeds version resource information 310# into the output .exe or .dll. 311# TODO: Enable for MinGW Windows builds too. 312# 313function(add_windows_version_resource_file OUT_VAR) 314 set(sources ${ARGN}) 315 if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 316 set(resource_file ${LLVM_SOURCE_DIR}/resources/windows_version_resource.rc) 317 if(EXISTS ${resource_file}) 318 set(sources ${sources} ${resource_file}) 319 source_group("Resource Files" ${resource_file}) 320 set(windows_resource_file ${resource_file} PARENT_SCOPE) 321 endif() 322 endif(MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") 323 324 set(${OUT_VAR} ${sources} PARENT_SCOPE) 325endfunction(add_windows_version_resource_file) 326 327# set_windows_version_resource_properties(name resource_file... 328# VERSION_MAJOR int 329# Optional major version number (defaults to LLVM_VERSION_MAJOR) 330# VERSION_MINOR int 331# Optional minor version number (defaults to LLVM_VERSION_MINOR) 332# VERSION_PATCHLEVEL int 333# Optional patchlevel version number (defaults to LLVM_VERSION_PATCH) 334# VERSION_STRING 335# Optional version string (defaults to PACKAGE_VERSION) 336# PRODUCT_NAME 337# Optional product name string (defaults to "LLVM") 338# ) 339function(set_windows_version_resource_properties name resource_file) 340 cmake_parse_arguments(ARG 341 "" 342 "VERSION_MAJOR;VERSION_MINOR;VERSION_PATCHLEVEL;VERSION_STRING;PRODUCT_NAME" 343 "" 344 ${ARGN}) 345 346 if (NOT DEFINED ARG_VERSION_MAJOR) 347 set(ARG_VERSION_MAJOR ${LLVM_VERSION_MAJOR}) 348 endif() 349 350 if (NOT DEFINED ARG_VERSION_MINOR) 351 set(ARG_VERSION_MINOR ${LLVM_VERSION_MINOR}) 352 endif() 353 354 if (NOT DEFINED ARG_VERSION_PATCHLEVEL) 355 set(ARG_VERSION_PATCHLEVEL ${LLVM_VERSION_PATCH}) 356 endif() 357 358 if (NOT DEFINED ARG_VERSION_STRING) 359 set(ARG_VERSION_STRING ${PACKAGE_VERSION}) 360 endif() 361 362 if (NOT DEFINED ARG_PRODUCT_NAME) 363 set(ARG_PRODUCT_NAME "LLVM") 364 endif() 365 366 set_property(SOURCE ${resource_file} 367 PROPERTY COMPILE_FLAGS /nologo) 368 set_property(SOURCE ${resource_file} 369 PROPERTY COMPILE_DEFINITIONS 370 "RC_VERSION_FIELD_1=${ARG_VERSION_MAJOR}" 371 "RC_VERSION_FIELD_2=${ARG_VERSION_MINOR}" 372 "RC_VERSION_FIELD_3=${ARG_VERSION_PATCHLEVEL}" 373 "RC_VERSION_FIELD_4=0" 374 "RC_FILE_VERSION=\"${ARG_VERSION_STRING}\"" 375 "RC_INTERNAL_NAME=\"${name}\"" 376 "RC_PRODUCT_NAME=\"${ARG_PRODUCT_NAME}\"" 377 "RC_PRODUCT_VERSION=\"${ARG_VERSION_STRING}\"") 378endfunction(set_windows_version_resource_properties) 379 380# llvm_add_library(name sources... 381# SHARED;STATIC 382# STATIC by default w/o BUILD_SHARED_LIBS. 383# SHARED by default w/ BUILD_SHARED_LIBS. 384# OBJECT 385# Also create an OBJECT library target. Default if STATIC && SHARED. 386# MODULE 387# Target ${name} might not be created on unsupported platforms. 388# Check with "if(TARGET ${name})". 389# DISABLE_LLVM_LINK_LLVM_DYLIB 390# Do not link this library to libLLVM, even if 391# LLVM_LINK_LLVM_DYLIB is enabled. 392# OUTPUT_NAME name 393# Corresponds to OUTPUT_NAME in target properties. 394# DEPENDS targets... 395# Same semantics as add_dependencies(). 396# LINK_COMPONENTS components... 397# Same as the variable LLVM_LINK_COMPONENTS. 398# LINK_LIBS lib_targets... 399# Same semantics as target_link_libraries(). 400# ADDITIONAL_HEADERS 401# May specify header files for IDE generators. 402# SONAME 403# Should set SONAME link flags and create symlinks 404# NO_INSTALL_RPATH 405# Suppress default RPATH settings in shared libraries. 406# PLUGIN_TOOL 407# The tool (i.e. cmake target) that this plugin will link against 408# COMPONENT_LIB 409# This is used to specify that this is a component library of 410# LLVM which means that the source resides in llvm/lib/ and it is a 411# candidate for inclusion into libLLVM.so. 412# ) 413function(llvm_add_library name) 414 cmake_parse_arguments(ARG 415 "MODULE;SHARED;STATIC;OBJECT;DISABLE_LLVM_LINK_LLVM_DYLIB;SONAME;NO_INSTALL_RPATH;COMPONENT_LIB" 416 "OUTPUT_NAME;PLUGIN_TOOL;ENTITLEMENTS;BUNDLE_PATH" 417 "ADDITIONAL_HEADERS;DEPENDS;LINK_COMPONENTS;LINK_LIBS;OBJLIBS" 418 ${ARGN}) 419 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 420 if(ARG_ADDITIONAL_HEADERS) 421 # Pass through ADDITIONAL_HEADERS. 422 set(ARG_ADDITIONAL_HEADERS ADDITIONAL_HEADERS ${ARG_ADDITIONAL_HEADERS}) 423 endif() 424 if(ARG_OBJLIBS) 425 set(ALL_FILES ${ARG_OBJLIBS}) 426 else() 427 llvm_process_sources(ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ${ARG_ADDITIONAL_HEADERS}) 428 endif() 429 430 if(ARG_MODULE) 431 if(ARG_SHARED OR ARG_STATIC) 432 message(WARNING "MODULE with SHARED|STATIC doesn't make sense.") 433 endif() 434 # Plugins that link against a tool are allowed even when plugins in general are not 435 if(NOT LLVM_ENABLE_PLUGINS AND NOT (ARG_PLUGIN_TOOL AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS)) 436 message(STATUS "${name} ignored -- Loadable modules not supported on this platform.") 437 return() 438 endif() 439 else() 440 if(ARG_PLUGIN_TOOL) 441 message(WARNING "PLUGIN_TOOL without MODULE doesn't make sense.") 442 endif() 443 if(BUILD_SHARED_LIBS AND NOT ARG_STATIC) 444 set(ARG_SHARED TRUE) 445 endif() 446 if(NOT ARG_SHARED) 447 set(ARG_STATIC TRUE) 448 endif() 449 endif() 450 451 # Generate objlib 452 if((ARG_SHARED AND ARG_STATIC) OR ARG_OBJECT) 453 # Generate an obj library for both targets. 454 set(obj_name "obj.${name}") 455 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 456 ${ALL_FILES} 457 ) 458 llvm_update_compile_flags(${obj_name}) 459 if(CMAKE_GENERATOR STREQUAL "Xcode") 460 set(DUMMY_FILE ${CMAKE_CURRENT_BINARY_DIR}/Dummy.c) 461 file(WRITE ${DUMMY_FILE} "// This file intentionally empty\n") 462 set_property(SOURCE ${DUMMY_FILE} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-empty-translation-unit") 463 endif() 464 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>" ${DUMMY_FILE}) 465 466 # Do add_dependencies(obj) later due to CMake issue 14747. 467 list(APPEND objlibs ${obj_name}) 468 469 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 470 if(ARG_DEPENDS) 471 add_dependencies(${obj_name} ${ARG_DEPENDS}) 472 endif() 473 # Treat link libraries like PUBLIC dependencies. LINK_LIBS might 474 # result in generating header files. Add a dependendency so that 475 # the generated header is created before this object library. 476 if(ARG_LINK_LIBS) 477 cmake_parse_arguments(LINK_LIBS_ARG 478 "" 479 "" 480 "PUBLIC;PRIVATE" 481 ${ARG_LINK_LIBS}) 482 foreach(link_lib ${LINK_LIBS_ARG_PUBLIC}) 483 if(LLVM_PTHREAD_LIB) 484 # Can't specify a dependence on -lpthread 485 if(NOT ${link_lib} STREQUAL ${LLVM_PTHREAD_LIB}) 486 add_dependencies(${obj_name} ${link_lib}) 487 endif() 488 else() 489 add_dependencies(${obj_name} ${link_lib}) 490 endif() 491 endforeach() 492 endif() 493 endif() 494 495 if(ARG_SHARED AND ARG_STATIC) 496 # static 497 set(name_static "${name}_static") 498 if(ARG_OUTPUT_NAME) 499 set(output_name OUTPUT_NAME "${ARG_OUTPUT_NAME}") 500 endif() 501 # DEPENDS has been appended to LLVM_COMMON_LIBS. 502 llvm_add_library(${name_static} STATIC 503 ${output_name} 504 OBJLIBS ${ALL_FILES} # objlib 505 LINK_LIBS ${ARG_LINK_LIBS} 506 LINK_COMPONENTS ${ARG_LINK_COMPONENTS} 507 ) 508 # FIXME: Add name_static to anywhere in TARGET ${name}'s PROPERTY. 509 set(ARG_STATIC) 510 endif() 511 512 if(ARG_MODULE) 513 add_library(${name} MODULE ${ALL_FILES}) 514 elseif(ARG_SHARED) 515 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 516 add_library(${name} SHARED ${ALL_FILES}) 517 else() 518 add_library(${name} STATIC ${ALL_FILES}) 519 endif() 520 521 if(ARG_COMPONENT_LIB) 522 set_target_properties(${name} PROPERTIES LLVM_COMPONENT TRUE) 523 set_property(GLOBAL APPEND PROPERTY LLVM_COMPONENT_LIBS ${name}) 524 endif() 525 526 if(NOT ARG_NO_INSTALL_RPATH) 527 if(ARG_MODULE OR ARG_SHARED) 528 llvm_setup_rpath(${name}) 529 endif() 530 endif() 531 532 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 533 534 if(DEFINED windows_resource_file) 535 set_windows_version_resource_properties(${name} ${windows_resource_file}) 536 set(windows_resource_file ${windows_resource_file} PARENT_SCOPE) 537 endif() 538 539 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 540 # $<TARGET_OBJECTS> doesn't require compile flags. 541 if(NOT obj_name) 542 llvm_update_compile_flags(${name}) 543 endif() 544 add_link_opts( ${name} ) 545 if(ARG_OUTPUT_NAME) 546 set_target_properties(${name} 547 PROPERTIES 548 OUTPUT_NAME ${ARG_OUTPUT_NAME} 549 ) 550 endif() 551 552 if(ARG_MODULE) 553 set_target_properties(${name} PROPERTIES 554 PREFIX "" 555 SUFFIX ${LLVM_PLUGIN_EXT} 556 ) 557 endif() 558 559 if(ARG_SHARED) 560 if(MSVC) 561 set_target_properties(${name} PROPERTIES 562 PREFIX "" 563 ) 564 endif() 565 566 # Set SOVERSION on shared libraries that lack explicit SONAME 567 # specifier, on *nix systems that are not Darwin. 568 if(UNIX AND NOT APPLE AND NOT ARG_SONAME) 569 set_target_properties(${name} 570 PROPERTIES 571 # Since 4.0.0, the ABI version is indicated by the major version 572 SOVERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX} 573 VERSION ${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 574 endif() 575 endif() 576 577 if(ARG_MODULE OR ARG_SHARED) 578 # Do not add -Dname_EXPORTS to the command-line when building files in this 579 # target. Doing so is actively harmful for the modules build because it 580 # creates extra module variants, and not useful because we don't use these 581 # macros. 582 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 583 584 if (LLVM_EXPORTED_SYMBOL_FILE) 585 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 586 endif() 587 endif() 588 589 if(ARG_SHARED AND UNIX) 590 if(NOT APPLE AND ARG_SONAME) 591 get_target_property(output_name ${name} OUTPUT_NAME) 592 if(${output_name} STREQUAL "output_name-NOTFOUND") 593 set(output_name ${name}) 594 endif() 595 set(library_name ${output_name}-${LLVM_VERSION_MAJOR}${LLVM_VERSION_SUFFIX}) 596 set(api_name ${output_name}-${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}) 597 set_target_properties(${name} PROPERTIES OUTPUT_NAME ${library_name}) 598 llvm_install_library_symlink(${api_name} ${library_name} SHARED 599 COMPONENT ${name}) 600 llvm_install_library_symlink(${output_name} ${library_name} SHARED 601 COMPONENT ${name}) 602 endif() 603 endif() 604 605 if(ARG_STATIC) 606 set(libtype PUBLIC) 607 else() 608 # We can use PRIVATE since SO knows its dependent libs. 609 set(libtype PRIVATE) 610 endif() 611 612 if(ARG_MODULE AND LLVM_EXPORT_SYMBOLS_FOR_PLUGINS AND ARG_PLUGIN_TOOL AND (WIN32 OR CYGWIN)) 613 # On DLL platforms symbols are imported from the tool by linking against it. 614 set(llvm_libs ${ARG_PLUGIN_TOOL}) 615 elseif (NOT ARG_COMPONENT_LIB) 616 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 617 set(llvm_libs LLVM) 618 else() 619 llvm_map_components_to_libnames(llvm_libs 620 ${ARG_LINK_COMPONENTS} 621 ${LLVM_LINK_COMPONENTS} 622 ) 623 endif() 624 else() 625 # Components have not been defined explicitly in CMake, so add the 626 # dependency information for this library through their name, and let 627 # LLVMBuildResolveComponentsLink resolve the mapping. 628 # 629 # It would be nice to verify that we have the dependencies for this library 630 # name, but using get_property(... SET) doesn't suffice to determine if a 631 # property has been set to an empty value. 632 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS} ${LLVM_LINK_COMPONENTS}) 633 634 # These two properties are internal properties only used to make sure the 635 # link step applied in LLVMBuildResolveComponentsLink uses the same 636 # properties as the target_link_libraries call below. 637 set_property(TARGET ${name} PROPERTY LLVM_LINK_LIBS ${ARG_LINK_LIBS}) 638 set_property(TARGET ${name} PROPERTY LLVM_LIBTYPE ${libtype}) 639 endif() 640 641 target_link_libraries(${name} ${libtype} 642 ${ARG_LINK_LIBS} 643 ${lib_deps} 644 ${llvm_libs} 645 ) 646 647 if(LLVM_COMMON_DEPENDS) 648 add_dependencies(${name} ${LLVM_COMMON_DEPENDS}) 649 # Add dependencies also to objlibs. 650 # CMake issue 14747 -- add_dependencies() might be ignored to objlib's user. 651 foreach(objlib ${objlibs}) 652 add_dependencies(${objlib} ${LLVM_COMMON_DEPENDS}) 653 endforeach() 654 endif() 655 656 if(ARG_SHARED OR ARG_MODULE) 657 llvm_externalize_debuginfo(${name}) 658 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH}) 659 endif() 660 # clang and newer versions of ninja use high-resolutions timestamps, 661 # but older versions of libtool on Darwin don't, so the archive will 662 # often get an older timestamp than the last object that was added 663 # or updated. To fix this, we add a custom command to touch archive 664 # after it's been built so that ninja won't rebuild it unnecessarily 665 # the next time it's run. 666 if(ARG_STATIC AND LLVM_TOUCH_STATIC_LIBRARIES) 667 add_custom_command(TARGET ${name} 668 POST_BUILD 669 COMMAND touch ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX} 670 ) 671 endif() 672endfunction() 673 674function(add_llvm_install_targets target) 675 cmake_parse_arguments(ARG "" "COMPONENT;PREFIX;SYMLINK" "DEPENDS" ${ARGN}) 676 if(ARG_COMPONENT) 677 set(component_option -DCMAKE_INSTALL_COMPONENT="${ARG_COMPONENT}") 678 endif() 679 if(ARG_PREFIX) 680 set(prefix_option -DCMAKE_INSTALL_PREFIX="${ARG_PREFIX}") 681 endif() 682 683 set(file_dependencies) 684 set(target_dependencies) 685 foreach(dependency ${ARG_DEPENDS}) 686 if(TARGET ${dependency}) 687 list(APPEND target_dependencies ${dependency}) 688 else() 689 list(APPEND file_dependencies ${dependency}) 690 endif() 691 endforeach() 692 693 add_custom_target(${target} 694 DEPENDS ${file_dependencies} 695 COMMAND "${CMAKE_COMMAND}" 696 ${component_option} 697 ${prefix_option} 698 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 699 USES_TERMINAL) 700 add_custom_target(${target}-stripped 701 DEPENDS ${file_dependencies} 702 COMMAND "${CMAKE_COMMAND}" 703 ${component_option} 704 ${prefix_option} 705 -DCMAKE_INSTALL_DO_STRIP=1 706 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake" 707 USES_TERMINAL) 708 if(target_dependencies) 709 add_dependencies(${target} ${target_dependencies}) 710 add_dependencies(${target}-stripped ${target_dependencies}) 711 endif() 712 713 if(ARG_SYMLINK) 714 add_dependencies(${target} install-${ARG_SYMLINK}) 715 add_dependencies(${target}-stripped install-${ARG_SYMLINK}-stripped) 716 endif() 717endfunction() 718 719# Define special targets that behave like a component group. They don't have any 720# source attached but other components can add themselves to them. If the 721# component supports is a Target and it supports JIT compilation, HAS_JIT must 722# be passed. One can use ADD_TO_COMPONENT option from add_llvm_component_library 723# to link extra component into an existing group. 724function(add_llvm_component_group name) 725 cmake_parse_arguments(ARG "HAS_JIT" "" "LINK_COMPONENTS" ${ARGN}) 726 add_custom_target(${name}) 727 if(ARG_HAS_JIT) 728 set_property(TARGET ${name} PROPERTY COMPONENT_HAS_JIT ON) 729 endif() 730 if(ARG_LINK_COMPONENTS) 731 set_property(TARGET ${name} PROPERTY LLVM_LINK_COMPONENTS ${ARG_LINK_COMPONENTS}) 732 endif() 733endfunction() 734 735# An LLVM component is a cmake target with the following cmake properties 736# eventually set: 737# - LLVM_COMPONENT_NAME: the name of the component, which can be the name of 738# the associated library or the one specified through COMPONENT_NAME 739# - LLVM_LINK_COMPONENTS: a list of component this component depends on 740# - COMPONENT_HAS_JIT: (only for group component) whether this target group 741# supports JIT compilation 742# Additionnaly, the ADD_TO_COMPONENT <component> option make it possible to add this 743# component to the LLVM_LINK_COMPONENTS of <component>. 744function(add_llvm_component_library name) 745 cmake_parse_arguments(ARG 746 "" 747 "COMPONENT_NAME;ADD_TO_COMPONENT" 748 "" 749 ${ARGN}) 750 add_llvm_library(${name} COMPONENT_LIB ${ARG_UNPARSED_ARGUMENTS}) 751 string(REGEX REPLACE "^LLVM" "" component_name ${name}) 752 set_property(TARGET ${name} PROPERTY LLVM_COMPONENT_NAME ${component_name}) 753 754 if(ARG_COMPONENT_NAME) 755 set_property(GLOBAL PROPERTY LLVM_COMPONENT_NAME_${ARG_COMPONENT_NAME} ${component_name}) 756 endif() 757 758 if(ARG_ADD_TO_COMPONENT) 759 set_property(TARGET ${ARG_ADD_TO_COMPONENT} APPEND PROPERTY LLVM_LINK_COMPONENTS ${component_name}) 760 endif() 761 762endfunction() 763 764macro(add_llvm_library name) 765 cmake_parse_arguments(ARG 766 "SHARED;BUILDTREE_ONLY;MODULE;INSTALL_WITH_TOOLCHAIN" 767 "" 768 "" 769 ${ARGN}) 770 if(ARG_MODULE) 771 llvm_add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS}) 772 elseif( BUILD_SHARED_LIBS OR ARG_SHARED ) 773 llvm_add_library(${name} SHARED ${ARG_UNPARSED_ARGUMENTS}) 774 else() 775 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS}) 776 endif() 777 778 # Libraries that are meant to only be exposed via the build tree only are 779 # never installed and are only exported as a target in the special build tree 780 # config file. 781 if (NOT ARG_BUILDTREE_ONLY AND NOT ARG_MODULE) 782 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) 783 set(in_llvm_libs YES) 784 endif() 785 786 if (ARG_MODULE AND NOT TARGET ${name}) 787 # Add empty "phony" target 788 add_custom_target(${name}) 789 elseif( EXCLUDE_FROM_ALL ) 790 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 791 elseif(ARG_BUILDTREE_ONLY) 792 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name}) 793 else() 794 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN) 795 if(in_llvm_libs) 796 set(umbrella UMBRELLA llvm-libraries) 797 else() 798 set(umbrella) 799 endif() 800 801 get_target_export_arg(${name} LLVM export_to_llvmexports ${umbrella}) 802 install(TARGETS ${name} 803 ${export_to_llvmexports} 804 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} 805 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT ${name} 806 RUNTIME DESTINATION bin COMPONENT ${name}) 807 808 if (NOT LLVM_ENABLE_IDE) 809 add_llvm_install_targets(install-${name} 810 DEPENDS ${name} 811 COMPONENT ${name}) 812 endif() 813 endif() 814 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 815 endif() 816 if (ARG_MODULE) 817 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") 818 else() 819 set_target_properties(${name} PROPERTIES FOLDER "Libraries") 820 endif() 821endmacro(add_llvm_library name) 822 823macro(add_llvm_executable name) 824 cmake_parse_arguments(ARG 825 "DISABLE_LLVM_LINK_LLVM_DYLIB;IGNORE_EXTERNALIZE_DEBUGINFO;NO_INSTALL_RPATH;SUPPORT_PLUGINS" 826 "ENTITLEMENTS;BUNDLE_PATH" 827 "DEPENDS" 828 ${ARGN}) 829 830 llvm_process_sources( ALL_FILES ${ARG_UNPARSED_ARGUMENTS} ) 831 832 list(APPEND LLVM_COMMON_DEPENDS ${ARG_DEPENDS}) 833 834 # Generate objlib 835 if(LLVM_ENABLE_OBJLIB) 836 # Generate an obj library for both targets. 837 set(obj_name "obj.${name}") 838 add_library(${obj_name} OBJECT EXCLUDE_FROM_ALL 839 ${ALL_FILES} 840 ) 841 llvm_update_compile_flags(${obj_name}) 842 set(ALL_FILES "$<TARGET_OBJECTS:${obj_name}>") 843 844 set_target_properties(${obj_name} PROPERTIES FOLDER "Object Libraries") 845 endif() 846 847 add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) 848 849 if(XCODE) 850 # Note: the dummy.cpp source file provides no definitions. However, 851 # it forces Xcode to properly link the static library. 852 list(APPEND ALL_FILES "${LLVM_MAIN_SRC_DIR}/cmake/dummy.cpp") 853 endif() 854 855 if( EXCLUDE_FROM_ALL ) 856 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) 857 else() 858 add_executable(${name} ${ALL_FILES}) 859 endif() 860 861 setup_dependency_debugging(${name} ${LLVM_COMMON_DEPENDS}) 862 863 if(NOT ARG_NO_INSTALL_RPATH) 864 llvm_setup_rpath(${name}) 865 elseif(NOT "${LLVM_LOCAL_RPATH}" STREQUAL "") 866 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set. 867 if("${CMAKE_BUILD_RPATH}" STREQUAL "") 868 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON) 869 endif() 870 871 set_property(TARGET ${name} PROPERTY INSTALL_RPATH "${LLVM_LOCAL_RPATH}") 872 endif() 873 874 if(DEFINED windows_resource_file) 875 set_windows_version_resource_properties(${name} ${windows_resource_file}) 876 endif() 877 878 # $<TARGET_OBJECTS> doesn't require compile flags. 879 if(NOT LLVM_ENABLE_OBJLIB) 880 llvm_update_compile_flags(${name}) 881 endif() 882 883 if (ARG_SUPPORT_PLUGINS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX") 884 set(LLVM_NO_DEAD_STRIP On) 885 endif() 886 887 add_link_opts( ${name} ) 888 889 # Do not add -Dname_EXPORTS to the command-line when building files in this 890 # target. Doing so is actively harmful for the modules build because it 891 # creates extra module variants, and not useful because we don't use these 892 # macros. 893 set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" ) 894 895 if (LLVM_EXPORTED_SYMBOL_FILE) 896 add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} ) 897 endif(LLVM_EXPORTED_SYMBOL_FILE) 898 899 if (LLVM_LINK_LLVM_DYLIB AND NOT ARG_DISABLE_LLVM_LINK_LLVM_DYLIB) 900 set(USE_SHARED USE_SHARED) 901 endif() 902 903 set(EXCLUDE_FROM_ALL OFF) 904 set_output_directory(${name} BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR} LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 905 llvm_config( ${name} ${USE_SHARED} ${LLVM_LINK_COMPONENTS} ) 906 if( LLVM_COMMON_DEPENDS ) 907 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) 908 endif( LLVM_COMMON_DEPENDS ) 909 910 if(NOT ARG_IGNORE_EXTERNALIZE_DEBUGINFO) 911 llvm_externalize_debuginfo(${name}) 912 endif() 913 if (LLVM_PTHREAD_LIB) 914 # libpthreads overrides some standard library symbols, so main 915 # executable must be linked with it in order to provide consistent 916 # API for all shared libaries loaded by this executable. 917 target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB}) 918 endif() 919 920 llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH}) 921endmacro(add_llvm_executable name) 922 923# add_llvm_pass_plugin(name [NO_MODULE] ...) 924# Add ${name} as an llvm plugin. 925# If option LLVM_${name_upper}_LINK_INTO_TOOLS is set to ON, the plugin is registered statically. 926# Otherwise a pluggable shared library is registered. 927# 928# If NO_MODULE is specified, when option LLVM_${name_upper}_LINK_INTO_TOOLS is set to OFF, 929# only an object library is built, and no module is built. This is specific to the Polly use case. 930# 931# The SUBPROJECT argument contains the LLVM project the plugin belongs 932# to. If set, the plugin will link statically by default it if the 933# project was enabled. 934function(add_llvm_pass_plugin name) 935 cmake_parse_arguments(ARG 936 "NO_MODULE" "SUBPROJECT" "" 937 ${ARGN}) 938 939 string(TOUPPER ${name} name_upper) 940 941 # Enable the plugin by default if it was explicitly enabled by the user. 942 # Note: If was set to "all", LLVM's CMakeLists.txt replaces it with a 943 # list of all projects, counting as explicitly enabled. 944 set(link_into_tools_default OFF) 945 if (ARG_SUBPROJECT AND LLVM_TOOL_${name_upper}_BUILD) 946 set(link_into_tools_default ON) 947 endif() 948 option(LLVM_${name_upper}_LINK_INTO_TOOLS "Statically link ${name} into tools (if available)" ${link_into_tools_default}) 949 950 # If we statically link the plugin, don't use llvm dylib because we're going 951 # to be part of it. 952 if(LLVM_${name_upper}_LINK_INTO_TOOLS) 953 list(APPEND ARG_UNPARSED_ARGUMENTS DISABLE_LLVM_LINK_LLVM_DYLIB) 954 endif() 955 956 if(LLVM_${name_upper}_LINK_INTO_TOOLS) 957 list(REMOVE_ITEM ARG_UNPARSED_ARGUMENTS BUILDTREE_ONLY) 958 # process_llvm_pass_plugins takes care of the actual linking, just create an 959 # object library as of now 960 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS}) 961 target_compile_definitions(${name} PRIVATE LLVM_${name_upper}_LINK_INTO_TOOLS) 962 set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS LLVM_LINK_INTO_TOOLS) 963 if (TARGET intrinsics_gen) 964 add_dependencies(obj.${name} intrinsics_gen) 965 endif() 966 if (TARGET omp_gen) 967 add_dependencies(obj.${name} omp_gen) 968 endif() 969 if (TARGET acc_gen) 970 add_dependencies(obj.${name} acc_gen) 971 endif() 972 set_property(GLOBAL APPEND PROPERTY LLVM_STATIC_EXTENSIONS ${name}) 973 elseif(NOT ARG_NO_MODULE) 974 add_llvm_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS}) 975 else() 976 add_llvm_library(${name} OBJECT ${ARG_UNPARSED_ARGUMENTS}) 977 endif() 978 message(STATUS "Registering ${name} as a pass plugin (static build: ${LLVM_${name_upper}_LINK_INTO_TOOLS})") 979 980endfunction(add_llvm_pass_plugin) 981 982# process_llvm_pass_plugins([GEN_CONFIG]) 983# 984# Correctly set lib dependencies between plugins and tools, based on tools 985# registered with the ENABLE_PLUGINS option. 986# 987# if GEN_CONFIG option is set, also generate X Macro file for extension 988# handling. It provides a HANDLE_EXTENSION(extension_namespace, ExtensionProject) 989# call for each extension allowing client code to define 990# HANDLE_EXTENSION to have a specific code be run for each extension. 991# 992function(process_llvm_pass_plugins) 993 cmake_parse_arguments(ARG 994 "GEN_CONFIG" "" "" 995 ${ARGN}) 996 997 if(ARG_GEN_CONFIG) 998 get_property(LLVM_STATIC_EXTENSIONS GLOBAL PROPERTY LLVM_STATIC_EXTENSIONS) 999 else() 1000 include(LLVMConfigExtensions) 1001 endif() 1002 1003 # Add static plugins to the Extension component 1004 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1005 set_property(TARGET LLVMExtensions APPEND PROPERTY LINK_LIBRARIES ${llvm_extension}) 1006 set_property(TARGET LLVMExtensions APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${llvm_extension}) 1007 endforeach() 1008 1009 # Eventually generate the extension headers, and store config to a cmake file 1010 # for usage in third-party configuration. 1011 if(ARG_GEN_CONFIG) 1012 1013 ## Part 1: Extension header to be included whenever we need extension 1014 # processing. 1015 set(LLVM_INSTALL_PACKAGE_DIR lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm) 1016 set(llvm_cmake_builddir "${LLVM_BINARY_DIR}/${LLVM_INSTALL_PACKAGE_DIR}") 1017 file(WRITE 1018 "${llvm_cmake_builddir}/LLVMConfigExtensions.cmake" 1019 "set(LLVM_STATIC_EXTENSIONS ${LLVM_STATIC_EXTENSIONS})") 1020 install(FILES 1021 ${llvm_cmake_builddir}/LLVMConfigExtensions.cmake 1022 DESTINATION ${LLVM_INSTALL_PACKAGE_DIR} 1023 COMPONENT cmake-exports) 1024 1025 set(ExtensionDef "${LLVM_BINARY_DIR}/include/llvm/Support/Extension.def") 1026 file(WRITE "${ExtensionDef}.tmp" "//extension handlers\n") 1027 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1028 file(APPEND "${ExtensionDef}.tmp" "HANDLE_EXTENSION(${llvm_extension})\n") 1029 endforeach() 1030 file(APPEND "${ExtensionDef}.tmp" "#undef HANDLE_EXTENSION\n") 1031 1032 # only replace if there's an actual change 1033 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different 1034 "${ExtensionDef}.tmp" 1035 "${ExtensionDef}") 1036 file(REMOVE "${ExtensionDef}.tmp") 1037 1038 ## Part 2: Extension header that captures each extension dependency, to be 1039 # used by llvm-config. 1040 set(ExtensionDeps "${LLVM_BINARY_DIR}/tools/llvm-config/ExtensionDependencies.inc") 1041 1042 # Max needed to correctly size the required library array. 1043 set(llvm_plugin_max_deps_length 0) 1044 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1045 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES) 1046 list(LENGTH llvm_plugin_deps llvm_plugin_deps_length) 1047 if(llvm_plugin_deps_length GREATER llvm_plugin_max_deps_length) 1048 set(llvm_plugin_max_deps_length ${llvm_plugin_deps_length}) 1049 endif() 1050 endforeach() 1051 1052 list(LENGTH LLVM_STATIC_EXTENSIONS llvm_static_extension_count) 1053 file(WRITE 1054 "${ExtensionDeps}.tmp" 1055 "#include <array>\n\ 1056 struct ExtensionDescriptor {\n\ 1057 const char* Name;\n\ 1058 const char* RequiredLibraries[1 + 1 + ${llvm_plugin_max_deps_length}];\n\ 1059 };\n\ 1060 std::array<ExtensionDescriptor, ${llvm_static_extension_count}> AvailableExtensions{\n") 1061 1062 foreach(llvm_extension ${LLVM_STATIC_EXTENSIONS}) 1063 get_property(llvm_plugin_deps TARGET ${llvm_extension} PROPERTY LINK_LIBRARIES) 1064 1065 file(APPEND "${ExtensionDeps}.tmp" "ExtensionDescriptor{\"${llvm_extension}\", {") 1066 foreach(llvm_plugin_dep ${llvm_plugin_deps}) 1067 # Turn library dependency back to component name, if possible. 1068 # That way llvm-config can avoid redundant dependencies. 1069 STRING(REGEX REPLACE "^-l" "" plugin_dep_name ${llvm_plugin_dep}) 1070 STRING(REGEX MATCH "^LLVM" is_llvm_library ${plugin_dep_name}) 1071 if(is_llvm_library) 1072 STRING(REGEX REPLACE "^LLVM" "" plugin_dep_name ${plugin_dep_name}) 1073 STRING(TOLOWER ${plugin_dep_name} plugin_dep_name) 1074 endif() 1075 file(APPEND "${ExtensionDeps}.tmp" "\"${plugin_dep_name}\", ") 1076 endforeach() 1077 1078 # Self + mandatory trailing null, because the number of RequiredLibraries differs between extensions. 1079 file(APPEND "${ExtensionDeps}.tmp" \"${llvm_extension}\", "nullptr}},\n") 1080 endforeach() 1081 file(APPEND "${ExtensionDeps}.tmp" "};\n") 1082 1083 # only replace if there's an actual change 1084 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different 1085 "${ExtensionDeps}.tmp" 1086 "${ExtensionDeps}") 1087 file(REMOVE "${ExtensionDeps}.tmp") 1088 endif() 1089endfunction() 1090 1091function(export_executable_symbols target) 1092 if (LLVM_EXPORTED_SYMBOL_FILE) 1093 # The symbol file should contain the symbols we want the executable to 1094 # export 1095 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1096 elseif (LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1097 # Extract the symbols to export from the static libraries that the 1098 # executable links against. 1099 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1100 set(exported_symbol_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${target}.symbols) 1101 # We need to consider not just the direct link dependencies, but also the 1102 # transitive link dependencies. Do this by starting with the set of direct 1103 # dependencies, then the dependencies of those dependencies, and so on. 1104 get_target_property(new_libs ${target} LINK_LIBRARIES) 1105 set(link_libs ${new_libs}) 1106 while(NOT "${new_libs}" STREQUAL "") 1107 foreach(lib ${new_libs}) 1108 if(TARGET ${lib}) 1109 get_target_property(lib_type ${lib} TYPE) 1110 if("${lib_type}" STREQUAL "STATIC_LIBRARY") 1111 list(APPEND static_libs ${lib}) 1112 else() 1113 list(APPEND other_libs ${lib}) 1114 endif() 1115 get_target_property(transitive_libs ${lib} INTERFACE_LINK_LIBRARIES) 1116 foreach(transitive_lib ${transitive_libs}) 1117 list(FIND link_libs ${transitive_lib} idx) 1118 if(TARGET ${transitive_lib} AND idx EQUAL -1) 1119 list(APPEND newer_libs ${transitive_lib}) 1120 list(APPEND link_libs ${transitive_lib}) 1121 endif() 1122 endforeach(transitive_lib) 1123 endif() 1124 endforeach(lib) 1125 set(new_libs ${newer_libs}) 1126 set(newer_libs "") 1127 endwhile() 1128 list(REMOVE_DUPLICATES static_libs) 1129 if (MSVC) 1130 set(mangling microsoft) 1131 else() 1132 set(mangling itanium) 1133 endif() 1134 add_custom_command(OUTPUT ${exported_symbol_file} 1135 COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py --mangling=${mangling} ${static_libs} -o ${exported_symbol_file} 1136 WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR} 1137 DEPENDS ${LLVM_MAIN_SRC_DIR}/utils/extract_symbols.py ${static_libs} 1138 VERBATIM 1139 COMMENT "Generating export list for ${target}") 1140 add_llvm_symbol_exports( ${target} ${exported_symbol_file} ) 1141 # If something links against this executable then we want a 1142 # transitive link against only the libraries whose symbols 1143 # we aren't exporting. 1144 set_target_properties(${target} PROPERTIES INTERFACE_LINK_LIBRARIES "${other_libs}") 1145 # The default import library suffix that cmake uses for cygwin/mingw is 1146 # ".dll.a", but for clang.exe that causes a collision with libclang.dll, 1147 # where the import libraries of both get named libclang.dll.a. Use a suffix 1148 # of ".exe.a" to avoid this. 1149 if(CYGWIN OR MINGW) 1150 set_target_properties(${target} PROPERTIES IMPORT_SUFFIX ".exe.a") 1151 endif() 1152 elseif(NOT (WIN32 OR CYGWIN)) 1153 # On Windows auto-exporting everything doesn't work because of the limit on 1154 # the size of the exported symbol table, but on other platforms we can do 1155 # it without any trouble. 1156 set_target_properties(${target} PROPERTIES ENABLE_EXPORTS 1) 1157 if (APPLE) 1158 set_property(TARGET ${target} APPEND_STRING PROPERTY 1159 LINK_FLAGS " -rdynamic") 1160 endif() 1161 endif() 1162endfunction() 1163 1164# Export symbols if LLVM plugins are enabled. 1165function(export_executable_symbols_for_plugins target) 1166 if(LLVM_ENABLE_PLUGINS OR LLVM_EXPORT_SYMBOLS_FOR_PLUGINS) 1167 export_executable_symbols(${target}) 1168 endif() 1169endfunction() 1170 1171if(NOT LLVM_TOOLCHAIN_TOOLS) 1172 set (LLVM_TOOLCHAIN_TOOLS 1173 llvm-ar 1174 llvm-cov 1175 llvm-cxxfilt 1176 llvm-ranlib 1177 llvm-lib 1178 llvm-nm 1179 llvm-objcopy 1180 llvm-objdump 1181 llvm-rc 1182 llvm-size 1183 llvm-strings 1184 llvm-strip 1185 llvm-profdata 1186 llvm-symbolizer 1187 # symlink version of some of above tools that are enabled by 1188 # LLVM_INSTALL_BINUTILS_SYMLINKS. 1189 addr2line 1190 ar 1191 c++filt 1192 ranlib 1193 nm 1194 objcopy 1195 objdump 1196 size 1197 strings 1198 strip 1199 ) 1200endif() 1201 1202macro(add_llvm_tool name) 1203 if( NOT LLVM_BUILD_TOOLS ) 1204 set(EXCLUDE_FROM_ALL ON) 1205 endif() 1206 add_llvm_executable(${name} ${ARGN}) 1207 1208 if ( ${name} IN_LIST LLVM_TOOLCHAIN_TOOLS OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 1209 if( LLVM_BUILD_TOOLS ) 1210 get_target_export_arg(${name} LLVM export_to_llvmexports) 1211 install(TARGETS ${name} 1212 ${export_to_llvmexports} 1213 RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR} 1214 COMPONENT ${name}) 1215 1216 if (NOT LLVM_ENABLE_IDE) 1217 add_llvm_install_targets(install-${name} 1218 DEPENDS ${name} 1219 COMPONENT ${name}) 1220 endif() 1221 endif() 1222 endif() 1223 if( LLVM_BUILD_TOOLS ) 1224 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 1225 endif() 1226 set_target_properties(${name} PROPERTIES FOLDER "Tools") 1227endmacro(add_llvm_tool name) 1228 1229 1230macro(add_llvm_example name) 1231 if( NOT LLVM_BUILD_EXAMPLES ) 1232 set(EXCLUDE_FROM_ALL ON) 1233 endif() 1234 add_llvm_executable(${name} ${ARGN}) 1235 if( LLVM_BUILD_EXAMPLES ) 1236 install(TARGETS ${name} RUNTIME DESTINATION examples) 1237 endif() 1238 set_target_properties(${name} PROPERTIES FOLDER "Examples") 1239endmacro(add_llvm_example name) 1240 1241macro(add_llvm_example_library name) 1242 if( NOT LLVM_BUILD_EXAMPLES ) 1243 set(EXCLUDE_FROM_ALL ON) 1244 add_llvm_library(${name} BUILDTREE_ONLY ${ARGN}) 1245 else() 1246 add_llvm_library(${name} ${ARGN}) 1247 endif() 1248 1249 set_target_properties(${name} PROPERTIES FOLDER "Examples") 1250endmacro(add_llvm_example_library name) 1251 1252# This is a macro that is used to create targets for executables that are needed 1253# for development, but that are not intended to be installed by default. 1254macro(add_llvm_utility name) 1255 if ( NOT LLVM_BUILD_UTILS ) 1256 set(EXCLUDE_FROM_ALL ON) 1257 endif() 1258 1259 add_llvm_executable(${name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN}) 1260 set_target_properties(${name} PROPERTIES FOLDER "Utils") 1261 if ( ${name} IN_LIST LLVM_TOOLCHAIN_UTILITIES OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 1262 if (LLVM_INSTALL_UTILS AND LLVM_BUILD_UTILS) 1263 get_target_export_arg(${name} LLVM export_to_llvmexports) 1264 install(TARGETS ${name} 1265 ${export_to_llvmexports} 1266 RUNTIME DESTINATION ${LLVM_UTILS_INSTALL_DIR} 1267 COMPONENT ${name}) 1268 1269 if (NOT LLVM_ENABLE_IDE) 1270 add_llvm_install_targets(install-${name} 1271 DEPENDS ${name} 1272 COMPONENT ${name}) 1273 endif() 1274 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) 1275 elseif(LLVM_BUILD_UTILS) 1276 set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS_BUILDTREE_ONLY ${name}) 1277 endif() 1278 endif() 1279endmacro(add_llvm_utility name) 1280 1281macro(add_llvm_fuzzer name) 1282 cmake_parse_arguments(ARG "" "DUMMY_MAIN" "" ${ARGN}) 1283 if( LLVM_LIB_FUZZING_ENGINE ) 1284 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 1285 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 1286 target_link_libraries(${name} PRIVATE ${LLVM_LIB_FUZZING_ENGINE}) 1287 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1288 elseif( LLVM_USE_SANITIZE_COVERAGE ) 1289 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=fuzzer") 1290 set(LLVM_OPTIONAL_SOURCES ${ARG_DUMMY_MAIN}) 1291 add_llvm_executable(${name} ${ARG_UNPARSED_ARGUMENTS}) 1292 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1293 elseif( ARG_DUMMY_MAIN ) 1294 add_llvm_executable(${name} ${ARG_DUMMY_MAIN} ${ARG_UNPARSED_ARGUMENTS}) 1295 set_target_properties(${name} PROPERTIES FOLDER "Fuzzers") 1296 endif() 1297endmacro() 1298 1299macro(add_llvm_target target_name) 1300 include_directories(BEFORE 1301 ${CMAKE_CURRENT_BINARY_DIR} 1302 ${CMAKE_CURRENT_SOURCE_DIR}) 1303 add_llvm_component_library(LLVM${target_name} ${ARGN}) 1304 set( CURRENT_LLVM_TARGET LLVM${target_name} ) 1305endmacro(add_llvm_target) 1306 1307function(canonicalize_tool_name name output) 1308 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" nameStrip ${name}) 1309 string(REPLACE "-" "_" nameUNDERSCORE ${nameStrip}) 1310 string(TOUPPER ${nameUNDERSCORE} nameUPPER) 1311 set(${output} "${nameUPPER}" PARENT_SCOPE) 1312endfunction(canonicalize_tool_name) 1313 1314# Custom add_subdirectory wrapper 1315# Takes in a project name (i.e. LLVM), the subdirectory name, and an optional 1316# path if it differs from the name. 1317function(add_llvm_subdirectory project type name) 1318 set(add_llvm_external_dir "${ARGN}") 1319 if("${add_llvm_external_dir}" STREQUAL "") 1320 set(add_llvm_external_dir ${name}) 1321 endif() 1322 canonicalize_tool_name(${name} nameUPPER) 1323 set(canonical_full_name ${project}_${type}_${nameUPPER}) 1324 get_property(already_processed GLOBAL PROPERTY ${canonical_full_name}_PROCESSED) 1325 if(already_processed) 1326 return() 1327 endif() 1328 set_property(GLOBAL PROPERTY ${canonical_full_name}_PROCESSED YES) 1329 1330 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}/CMakeLists.txt) 1331 # Treat it as in-tree subproject. 1332 option(${canonical_full_name}_BUILD 1333 "Whether to build ${name} as part of ${project}" On) 1334 mark_as_advanced(${project}_${type}_${name}_BUILD) 1335 if(${canonical_full_name}_BUILD) 1336 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir} ${add_llvm_external_dir}) 1337 endif() 1338 else() 1339 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR 1340 "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" 1341 CACHE PATH "Path to ${name} source directory") 1342 set(${canonical_full_name}_BUILD_DEFAULT ON) 1343 if(NOT LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR OR NOT EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1344 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1345 endif() 1346 if("${LLVM_EXTERNAL_${nameUPPER}_BUILD}" STREQUAL "OFF") 1347 set(${canonical_full_name}_BUILD_DEFAULT OFF) 1348 endif() 1349 option(${canonical_full_name}_BUILD 1350 "Whether to build ${name} as part of LLVM" 1351 ${${canonical_full_name}_BUILD_DEFAULT}) 1352 if (${canonical_full_name}_BUILD) 1353 if(EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}) 1354 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir}) 1355 elseif(NOT "${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}" STREQUAL "") 1356 message(WARNING "Nonexistent directory for ${name}: ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}") 1357 endif() 1358 endif() 1359 endif() 1360endfunction() 1361 1362# Add external project that may want to be built as part of llvm such as Clang, 1363# lld, and Polly. This adds two options. One for the source directory of the 1364# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to 1365# enable or disable building it with everything else. 1366# Additional parameter can be specified as the name of directory. 1367macro(add_llvm_external_project name) 1368 add_llvm_subdirectory(LLVM TOOL ${name} ${ARGN}) 1369endmacro() 1370 1371macro(add_llvm_tool_subdirectory name) 1372 add_llvm_external_project(${name}) 1373endmacro(add_llvm_tool_subdirectory) 1374 1375function(get_project_name_from_src_var var output) 1376 string(REGEX MATCH "LLVM_EXTERNAL_(.*)_SOURCE_DIR" 1377 MACHED_TOOL "${var}") 1378 if(MACHED_TOOL) 1379 set(${output} ${CMAKE_MATCH_1} PARENT_SCOPE) 1380 else() 1381 set(${output} PARENT_SCOPE) 1382 endif() 1383endfunction() 1384 1385function(create_subdirectory_options project type) 1386 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1387 foreach(dir ${sub-dirs}) 1388 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1389 canonicalize_tool_name(${dir} name) 1390 option(${project}_${type}_${name}_BUILD 1391 "Whether to build ${name} as part of ${project}" On) 1392 mark_as_advanced(${project}_${type}_${name}_BUILD) 1393 endif() 1394 endforeach() 1395endfunction(create_subdirectory_options) 1396 1397function(create_llvm_tool_options) 1398 create_subdirectory_options(LLVM TOOL) 1399endfunction(create_llvm_tool_options) 1400 1401function(llvm_add_implicit_projects project) 1402 set(list_of_implicit_subdirs "") 1403 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*") 1404 foreach(dir ${sub-dirs}) 1405 if(IS_DIRECTORY "${dir}" AND EXISTS "${dir}/CMakeLists.txt") 1406 canonicalize_tool_name(${dir} name) 1407 if (${project}_TOOL_${name}_BUILD) 1408 get_filename_component(fn "${dir}" NAME) 1409 list(APPEND list_of_implicit_subdirs "${fn}") 1410 endif() 1411 endif() 1412 endforeach() 1413 1414 foreach(external_proj ${list_of_implicit_subdirs}) 1415 add_llvm_subdirectory(${project} TOOL "${external_proj}" ${ARGN}) 1416 endforeach() 1417endfunction(llvm_add_implicit_projects) 1418 1419function(add_llvm_implicit_projects) 1420 llvm_add_implicit_projects(LLVM) 1421endfunction(add_llvm_implicit_projects) 1422 1423# Generic support for adding a unittest. 1424function(add_unittest test_suite test_name) 1425 if( NOT LLVM_BUILD_TESTS ) 1426 set(EXCLUDE_FROM_ALL ON) 1427 endif() 1428 1429 if (SUPPORTS_VARIADIC_MACROS_FLAG) 1430 list(APPEND LLVM_COMPILE_FLAGS "-Wno-variadic-macros") 1431 endif () 1432 # Some parts of gtest rely on this GNU extension, don't warn on it. 1433 if(SUPPORTS_GNU_ZERO_VARIADIC_MACRO_ARGUMENTS_FLAG) 1434 list(APPEND LLVM_COMPILE_FLAGS "-Wno-gnu-zero-variadic-macro-arguments") 1435 endif() 1436 1437 set(LLVM_REQUIRES_RTTI OFF) 1438 1439 list(APPEND LLVM_LINK_COMPONENTS Support) # gtest needs it for raw_ostream 1440 add_llvm_executable(${test_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1441 1442 # The runtime benefits of ThinLTO don't outweight the compile time costs for tests. 1443 if(uppercase_LLVM_ENABLE_LTO STREQUAL "THIN") 1444 if((UNIX OR MINGW) AND LLVM_USE_LINKER STREQUAL "lld") 1445 set_property(TARGET ${test_name} APPEND_STRING PROPERTY 1446 LINK_FLAGS " -Wl,--lto-O0") 1447 elseif(LINKER_IS_LLD_LINK) 1448 set_property(TARGET ${test_name} APPEND_STRING PROPERTY 1449 LINK_FLAGS " /opt:lldlto=0") 1450 endif() 1451 endif() 1452 1453 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1454 set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1455 # libpthreads overrides some standard library symbols, so main 1456 # executable must be linked with it in order to provide consistent 1457 # API for all shared libaries loaded by this executable. 1458 target_link_libraries(${test_name} PRIVATE gtest_main gtest ${LLVM_PTHREAD_LIB}) 1459 1460 add_dependencies(${test_suite} ${test_name}) 1461 get_target_property(test_suite_folder ${test_suite} FOLDER) 1462 if (test_suite_folder) 1463 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") 1464 endif () 1465endfunction() 1466 1467# Use for test binaries that call llvm::getInputFileDirectory(). Use of this 1468# is discouraged. 1469function(add_unittest_with_input_files test_suite test_name) 1470 set(LLVM_UNITTEST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 1471 configure_file( 1472 ${LLVM_MAIN_SRC_DIR}/unittests/unittest.cfg.in 1473 ${CMAKE_CURRENT_BINARY_DIR}/llvm.srcdir.txt) 1474 1475 add_unittest(${test_suite} ${test_name} ${ARGN}) 1476endfunction() 1477 1478# Generic support for adding a benchmark. 1479function(add_benchmark benchmark_name) 1480 if( NOT LLVM_BUILD_BENCHMARKS ) 1481 set(EXCLUDE_FROM_ALL ON) 1482 endif() 1483 1484 add_llvm_executable(${benchmark_name} IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH ${ARGN}) 1485 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) 1486 set_output_directory(${benchmark_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) 1487 set_property(TARGET ${benchmark_name} PROPERTY FOLDER "Utils") 1488 target_link_libraries(${benchmark_name} PRIVATE benchmark) 1489endfunction() 1490 1491# This function canonicalize the CMake variables passed by names 1492# from CMake boolean to 0/1 suitable for passing into Python or C++, 1493# in place. 1494function(llvm_canonicalize_cmake_booleans) 1495 foreach(var ${ARGN}) 1496 if(${var}) 1497 set(${var} 1 PARENT_SCOPE) 1498 else() 1499 set(${var} 0 PARENT_SCOPE) 1500 endif() 1501 endforeach() 1502endfunction(llvm_canonicalize_cmake_booleans) 1503 1504macro(set_llvm_build_mode) 1505 # Configuration-time: See Unit/lit.site.cfg.in 1506 if (CMAKE_CFG_INTDIR STREQUAL ".") 1507 set(LLVM_BUILD_MODE ".") 1508 else () 1509 set(LLVM_BUILD_MODE "%(build_mode)s") 1510 endif () 1511endmacro() 1512 1513# Takes a list of path names in pathlist and a base directory, and returns 1514# a list of paths relative to the base directory in out_pathlist. 1515# Paths that are on a different drive than the basedir (on Windows) or that 1516# contain symlinks are returned absolute. 1517# Use with LLVM_LIT_PATH_FUNCTION below. 1518function(make_paths_relative out_pathlist basedir pathlist) 1519 # Passing ARG_PATH_VALUES as-is to execute_process() makes cmake strip 1520 # empty list entries. So escape the ;s in the list and do the splitting 1521 # ourselves. cmake has no relpath function, so use Python for that. 1522 string(REPLACE ";" "\\;" pathlist_escaped "${pathlist}") 1523 execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "\n 1524import os, sys\n 1525base = sys.argv[1] 1526def haslink(p):\n 1527 if not p or p == os.path.dirname(p): return False\n 1528 return os.path.islink(p) or haslink(os.path.dirname(p))\n 1529def relpath(p):\n 1530 if not p: return ''\n 1531 if os.path.splitdrive(p)[0] != os.path.splitdrive(base)[0]: return p\n 1532 if haslink(p) or haslink(base): return p\n 1533 return os.path.relpath(p, base)\n 1534if len(sys.argv) < 3: sys.exit(0)\n 1535sys.stdout.write(';'.join(relpath(p) for p in sys.argv[2].split(';')))" 1536 ${basedir} 1537 ${pathlist_escaped} 1538 OUTPUT_VARIABLE pathlist_relative 1539 ERROR_VARIABLE error 1540 RESULT_VARIABLE result) 1541 if (NOT result EQUAL 0) 1542 message(FATAL_ERROR "make_paths_relative() failed due to error '${result}', with stderr\n${error}") 1543 endif() 1544 set(${out_pathlist} "${pathlist_relative}" PARENT_SCOPE) 1545endfunction() 1546 1547# Converts a file that's relative to the current python file to an absolute 1548# path. Since this uses __file__, it has to be emitted into python files that 1549# use it and can't be in a lit module. Use with make_paths_relative(). 1550string(CONCAT LLVM_LIT_PATH_FUNCTION 1551 "# Allow generated file to be relocatable.\n" 1552 "def path(p):\n" 1553 " if not p: return ''\n" 1554 " return os.path.join(os.path.dirname(os.path.abspath(__file__)), p)\n" 1555 ) 1556 1557# This function provides an automatic way to 'configure'-like generate a file 1558# based on a set of common and custom variables, specifically targeting the 1559# variables needed for the 'lit.site.cfg' files. This function bundles the 1560# common variables that any Lit instance is likely to need, and custom 1561# variables can be passed in. 1562# The keyword PATHS is followed by a list of cmake variable names that are 1563# mentioned as `path("@varname@")` in the lit.cfg.py.in file. Variables in that 1564# list are treated as paths that are relative to the directory the generated 1565# lit.cfg.py file is in, and the `path()` function converts the relative 1566# path back to absolute form. This makes it possible to move a build directory 1567# containing lit.cfg.py files from one machine to another. 1568function(configure_lit_site_cfg site_in site_out) 1569 cmake_parse_arguments(ARG "" "" "MAIN_CONFIG;OUTPUT_MAPPING;PATHS" ${ARGN}) 1570 1571 if ("${ARG_MAIN_CONFIG}" STREQUAL "") 1572 get_filename_component(INPUT_DIR ${site_in} DIRECTORY) 1573 set(ARG_MAIN_CONFIG "${INPUT_DIR}/lit.cfg") 1574 endif() 1575 if ("${ARG_OUTPUT_MAPPING}" STREQUAL "") 1576 set(ARG_OUTPUT_MAPPING "${site_out}") 1577 endif() 1578 1579 foreach(c ${LLVM_TARGETS_TO_BUILD}) 1580 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") 1581 endforeach(c) 1582 set(TARGETS_TO_BUILD ${TARGETS_BUILT}) 1583 1584 set(SHLIBEXT "${LTDL_SHLIB_EXT}") 1585 1586 set_llvm_build_mode() 1587 1588 # The below might not be the build tree but provided binary tree. 1589 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) 1590 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) 1591 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}") 1592 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" LLVM_LIBS_DIR "${LLVM_LIBRARY_DIR}") 1593 1594 # SHLIBDIR points the build tree. 1595 string(REPLACE "${CMAKE_CFG_INTDIR}" "${LLVM_BUILD_MODE}" SHLIBDIR "${LLVM_SHLIB_OUTPUT_INTDIR}") 1596 1597 # FIXME: "ENABLE_SHARED" doesn't make sense, since it is used just for 1598 # plugins. We may rename it. 1599 if(LLVM_ENABLE_PLUGINS) 1600 set(ENABLE_SHARED "1") 1601 else() 1602 set(ENABLE_SHARED "0") 1603 endif() 1604 1605 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) 1606 set(ENABLE_ASSERTIONS "1") 1607 else() 1608 set(ENABLE_ASSERTIONS "0") 1609 endif() 1610 1611 set(HOST_OS ${CMAKE_SYSTEM_NAME}) 1612 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR}) 1613 1614 set(HOST_CC "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}") 1615 set(HOST_CXX "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}") 1616 set(HOST_LDFLAGS "${CMAKE_EXE_LINKER_FLAGS}") 1617 1618 string(CONCAT LIT_SITE_CFG_IN_HEADER 1619 "# Autogenerated from ${site_in}\n# Do not edit!\n\n" 1620 "${LLVM_LIT_PATH_FUNCTION}" 1621 ) 1622 1623 # Override config_target_triple (and the env) 1624 if(LLVM_TARGET_TRIPLE_ENV) 1625 # This is expanded into the heading. 1626 string(CONCAT LIT_SITE_CFG_IN_HEADER "${LIT_SITE_CFG_IN_HEADER}" 1627 "import os\n" 1628 "target_env = \"${LLVM_TARGET_TRIPLE_ENV}\"\n" 1629 "config.target_triple = config.environment[target_env] = os.environ.get(target_env, \"${TARGET_TRIPLE}\")\n" 1630 ) 1631 1632 # This is expanded to; config.target_triple = ""+config.target_triple+"" 1633 set(TARGET_TRIPLE "\"+config.target_triple+\"") 1634 endif() 1635 1636 if (ARG_PATHS) 1637 # Walk ARG_PATHS and collect the current value of the variables in there. 1638 # list(APPEND) ignores empty elements exactly if the list is empty, 1639 # so start the list with a dummy element and drop it, to make sure that 1640 # even empty values make it into the values list. 1641 set(ARG_PATH_VALUES "dummy") 1642 foreach(path ${ARG_PATHS}) 1643 list(APPEND ARG_PATH_VALUES "${${path}}") 1644 endforeach() 1645 list(REMOVE_AT ARG_PATH_VALUES 0) 1646 1647 get_filename_component(OUTPUT_DIR ${site_out} DIRECTORY) 1648 make_paths_relative( 1649 ARG_PATH_VALUES_RELATIVE "${OUTPUT_DIR}" "${ARG_PATH_VALUES}") 1650 1651 list(LENGTH ARG_PATHS len_paths) 1652 list(LENGTH ARG_PATH_VALUES len_path_values) 1653 list(LENGTH ARG_PATH_VALUES_RELATIVE len_path_value_rels) 1654 if ((NOT ${len_paths} EQUAL ${len_path_values}) OR 1655 (NOT ${len_paths} EQUAL ${len_path_value_rels})) 1656 message(SEND_ERROR "PATHS lengths got confused") 1657 endif() 1658 1659 # Transform variables mentioned in ARG_PATHS to relative paths for 1660 # the configure_file() call. Variables are copied to subscopeds by cmake, 1661 # so this only modifies the local copy of the variables. 1662 math(EXPR arg_path_limit "${len_paths} - 1") 1663 foreach(i RANGE ${arg_path_limit}) 1664 list(GET ARG_PATHS ${i} val1) 1665 list(GET ARG_PATH_VALUES_RELATIVE ${i} val2) 1666 set(${val1} ${val2}) 1667 endforeach() 1668 endif() 1669 1670 configure_file(${site_in} ${site_out} @ONLY) 1671 1672 if (EXISTS "${ARG_MAIN_CONFIG}") 1673 # Remember main config / generated site config for llvm-lit.in. 1674 get_property(LLVM_LIT_CONFIG_FILES GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES) 1675 list(APPEND LLVM_LIT_CONFIG_FILES "${ARG_MAIN_CONFIG}" "${site_out}") 1676 set_property(GLOBAL PROPERTY LLVM_LIT_CONFIG_FILES ${LLVM_LIT_CONFIG_FILES}) 1677 endif() 1678endfunction() 1679 1680function(dump_all_cmake_variables) 1681 get_cmake_property(_variableNames VARIABLES) 1682 foreach (_variableName ${_variableNames}) 1683 message(STATUS "${_variableName}=${${_variableName}}") 1684 endforeach() 1685endfunction() 1686 1687function(get_llvm_lit_path base_dir file_name) 1688 cmake_parse_arguments(ARG "ALLOW_EXTERNAL" "" "" ${ARGN}) 1689 1690 if (ARG_ALLOW_EXTERNAL) 1691 set (LLVM_EXTERNAL_LIT "" CACHE STRING "Command used to spawn lit") 1692 if ("${LLVM_EXTERNAL_LIT}" STREQUAL "") 1693 set(LLVM_EXTERNAL_LIT "${LLVM_DEFAULT_EXTERNAL_LIT}") 1694 endif() 1695 1696 if (NOT "${LLVM_EXTERNAL_LIT}" STREQUAL "") 1697 if (EXISTS ${LLVM_EXTERNAL_LIT}) 1698 get_filename_component(LIT_FILE_NAME ${LLVM_EXTERNAL_LIT} NAME) 1699 get_filename_component(LIT_BASE_DIR ${LLVM_EXTERNAL_LIT} DIRECTORY) 1700 set(${file_name} ${LIT_FILE_NAME} PARENT_SCOPE) 1701 set(${base_dir} ${LIT_BASE_DIR} PARENT_SCOPE) 1702 return() 1703 elseif (NOT DEFINED CACHE{LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE}) 1704 message(WARNING "LLVM_EXTERNAL_LIT set to ${LLVM_EXTERNAL_LIT}, but the path does not exist.") 1705 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "") 1706 endif() 1707 endif() 1708 endif() 1709 1710 set(lit_file_name "llvm-lit") 1711 if (CMAKE_HOST_WIN32 AND NOT CYGWIN) 1712 # llvm-lit needs suffix.py for multiprocess to find a main module. 1713 set(lit_file_name "${lit_file_name}.py") 1714 endif () 1715 set(${file_name} ${lit_file_name} PARENT_SCOPE) 1716 1717 get_property(LLVM_LIT_BASE_DIR GLOBAL PROPERTY LLVM_LIT_BASE_DIR) 1718 if (NOT "${LLVM_LIT_BASE_DIR}" STREQUAL "") 1719 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1720 endif() 1721 1722 # Allow individual projects to provide an override 1723 if (NOT "${LLVM_LIT_OUTPUT_DIR}" STREQUAL "") 1724 set(LLVM_LIT_BASE_DIR ${LLVM_LIT_OUTPUT_DIR}) 1725 elseif(NOT "${LLVM_RUNTIME_OUTPUT_INTDIR}" STREQUAL "") 1726 set(LLVM_LIT_BASE_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) 1727 else() 1728 set(LLVM_LIT_BASE_DIR "") 1729 endif() 1730 1731 # Cache this so we don't have to do it again and have subsequent calls 1732 # potentially disagree on the value. 1733 set_property(GLOBAL PROPERTY LLVM_LIT_BASE_DIR ${LLVM_LIT_BASE_DIR}) 1734 set(${base_dir} ${LLVM_LIT_BASE_DIR} PARENT_SCOPE) 1735endfunction() 1736 1737# A raw function to create a lit target. This is used to implement the testuite 1738# management functions. 1739function(add_lit_target target comment) 1740 cmake_parse_arguments(ARG "" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1741 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}") 1742 separate_arguments(LIT_ARGS) 1743 if (NOT CMAKE_CFG_INTDIR STREQUAL ".") 1744 list(APPEND LIT_ARGS --param build_mode=${CMAKE_CFG_INTDIR}) 1745 endif () 1746 1747 # Get the path to the lit to *run* tests with. This can be overriden by 1748 # the user by specifying -DLLVM_EXTERNAL_LIT=<path-to-lit.py> 1749 get_llvm_lit_path( 1750 lit_base_dir 1751 lit_file_name 1752 ALLOW_EXTERNAL 1753 ) 1754 1755 set(LIT_COMMAND "${Python3_EXECUTABLE};${lit_base_dir}/${lit_file_name}") 1756 list(APPEND LIT_COMMAND ${LIT_ARGS}) 1757 foreach(param ${ARG_PARAMS}) 1758 list(APPEND LIT_COMMAND --param ${param}) 1759 endforeach() 1760 if (ARG_UNPARSED_ARGUMENTS) 1761 add_custom_target(${target} 1762 COMMAND ${LIT_COMMAND} ${ARG_UNPARSED_ARGUMENTS} 1763 COMMENT "${comment}" 1764 USES_TERMINAL 1765 ) 1766 else() 1767 add_custom_target(${target} 1768 COMMAND ${CMAKE_COMMAND} -E echo "${target} does nothing, no tools built.") 1769 message(STATUS "${target} does nothing.") 1770 endif() 1771 1772 if (ARG_DEPENDS) 1773 add_dependencies(${target} ${ARG_DEPENDS}) 1774 endif() 1775 1776 # Tests should be excluded from "Build Solution". 1777 set_target_properties(${target} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON) 1778endfunction() 1779 1780# A function to add a set of lit test suites to be driven through 'check-*' targets. 1781function(add_lit_testsuite target comment) 1782 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1783 1784 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all. 1785 if(NOT ARG_EXCLUDE_FROM_CHECK_ALL) 1786 # Register the testsuites, params and depends for the global check rule. 1787 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_UNPARSED_ARGUMENTS}) 1788 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS}) 1789 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS}) 1790 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS}) 1791 endif() 1792 1793 # Produce a specific suffixed check rule. 1794 add_lit_target(${target} ${comment} 1795 ${ARG_UNPARSED_ARGUMENTS} 1796 PARAMS ${ARG_PARAMS} 1797 DEPENDS ${ARG_DEPENDS} 1798 ARGS ${ARG_ARGS} 1799 ) 1800endfunction() 1801 1802function(add_lit_testsuites project directory) 1803 if (NOT LLVM_ENABLE_IDE) 1804 cmake_parse_arguments(ARG "EXCLUDE_FROM_CHECK_ALL" "" "PARAMS;DEPENDS;ARGS" ${ARGN}) 1805 1806 # Search recursively for test directories by assuming anything not 1807 # in a directory called Inputs contains tests. 1808 file(GLOB_RECURSE to_process LIST_DIRECTORIES true ${directory}/*) 1809 foreach(lit_suite ${to_process}) 1810 if(NOT IS_DIRECTORY ${lit_suite}) 1811 continue() 1812 endif() 1813 string(FIND ${lit_suite} Inputs is_inputs) 1814 string(FIND ${lit_suite} Output is_output) 1815 if (NOT (is_inputs EQUAL -1 AND is_output EQUAL -1)) 1816 continue() 1817 endif() 1818 1819 # Create a check- target for the directory. 1820 string(REPLACE ${directory} "" name_slash ${lit_suite}) 1821 if (name_slash) 1822 string(REPLACE "/" "-" name_slash ${name_slash}) 1823 string(REPLACE "\\" "-" name_dashes ${name_slash}) 1824 string(TOLOWER "${project}${name_dashes}" name_var) 1825 add_lit_target("check-${name_var}" "Running lit suite ${lit_suite}" 1826 ${lit_suite} 1827 ${EXCLUDE_FROM_CHECK_ALL} 1828 PARAMS ${ARG_PARAMS} 1829 DEPENDS ${ARG_DEPENDS} 1830 ARGS ${ARG_ARGS} 1831 ) 1832 endif() 1833 endforeach() 1834 endif() 1835endfunction() 1836 1837function(llvm_install_library_symlink name dest type) 1838 cmake_parse_arguments(ARG "" "COMPONENT" "" ${ARGN}) 1839 foreach(path ${CMAKE_MODULE_PATH}) 1840 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1841 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1842 break() 1843 endif() 1844 endforeach() 1845 1846 set(component ${ARG_COMPONENT}) 1847 if(NOT component) 1848 set(component ${name}) 1849 endif() 1850 1851 set(full_name ${CMAKE_${type}_LIBRARY_PREFIX}${name}${CMAKE_${type}_LIBRARY_SUFFIX}) 1852 set(full_dest ${CMAKE_${type}_LIBRARY_PREFIX}${dest}${CMAKE_${type}_LIBRARY_SUFFIX}) 1853 1854 set(output_dir lib${LLVM_LIBDIR_SUFFIX}) 1855 if(WIN32 AND "${type}" STREQUAL "SHARED") 1856 set(output_dir bin) 1857 endif() 1858 1859 install(SCRIPT ${INSTALL_SYMLINK} 1860 CODE "install_symlink(${full_name} ${full_dest} ${output_dir})" 1861 COMPONENT ${component}) 1862 1863endfunction() 1864 1865function(llvm_install_symlink name dest) 1866 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "COMPONENT" "" ${ARGN}) 1867 foreach(path ${CMAKE_MODULE_PATH}) 1868 if(EXISTS ${path}/LLVMInstallSymlink.cmake) 1869 set(INSTALL_SYMLINK ${path}/LLVMInstallSymlink.cmake) 1870 break() 1871 endif() 1872 endforeach() 1873 1874 if(ARG_COMPONENT) 1875 set(component ${ARG_COMPONENT}) 1876 else() 1877 if(ARG_ALWAYS_GENERATE) 1878 set(component ${dest}) 1879 else() 1880 set(component ${name}) 1881 endif() 1882 endif() 1883 1884 set(full_name ${name}${CMAKE_EXECUTABLE_SUFFIX}) 1885 set(full_dest ${dest}${CMAKE_EXECUTABLE_SUFFIX}) 1886 1887 install(SCRIPT ${INSTALL_SYMLINK} 1888 CODE "install_symlink(${full_name} ${full_dest} ${LLVM_TOOLS_INSTALL_DIR})" 1889 COMPONENT ${component}) 1890 1891 if (NOT LLVM_ENABLE_IDE AND NOT ARG_ALWAYS_GENERATE) 1892 add_llvm_install_targets(install-${name} 1893 DEPENDS ${name} ${dest} 1894 COMPONENT ${name} 1895 SYMLINK ${dest}) 1896 endif() 1897endfunction() 1898 1899function(add_llvm_tool_symlink link_name target) 1900 cmake_parse_arguments(ARG "ALWAYS_GENERATE" "OUTPUT_DIR" "" ${ARGN}) 1901 set(dest_binary "$<TARGET_FILE:${target}>") 1902 1903 # This got a bit gross... For multi-configuration generators the target 1904 # properties return the resolved value of the string, not the build system 1905 # expression. To reconstruct the platform-agnostic path we have to do some 1906 # magic. First we grab one of the types, and a type-specific path. Then from 1907 # the type-specific path we find the last occurrence of the type in the path, 1908 # and replace it with CMAKE_CFG_INTDIR. This allows the build step to be type 1909 # agnostic again. 1910 if(NOT ARG_OUTPUT_DIR) 1911 # If you're not overriding the OUTPUT_DIR, we can make the link relative in 1912 # the same directory. 1913 if(CMAKE_HOST_UNIX) 1914 set(dest_binary "$<TARGET_FILE_NAME:${target}>") 1915 endif() 1916 if(CMAKE_CONFIGURATION_TYPES) 1917 list(GET CMAKE_CONFIGURATION_TYPES 0 first_type) 1918 string(TOUPPER ${first_type} first_type_upper) 1919 set(first_type_suffix _${first_type_upper}) 1920 endif() 1921 get_target_property(target_type ${target} TYPE) 1922 if(${target_type} STREQUAL "STATIC_LIBRARY") 1923 get_target_property(ARG_OUTPUT_DIR ${target} ARCHIVE_OUTPUT_DIRECTORY${first_type_suffix}) 1924 elseif(UNIX AND ${target_type} STREQUAL "SHARED_LIBRARY") 1925 get_target_property(ARG_OUTPUT_DIR ${target} LIBRARY_OUTPUT_DIRECTORY${first_type_suffix}) 1926 else() 1927 get_target_property(ARG_OUTPUT_DIR ${target} RUNTIME_OUTPUT_DIRECTORY${first_type_suffix}) 1928 endif() 1929 if(CMAKE_CONFIGURATION_TYPES) 1930 string(FIND "${ARG_OUTPUT_DIR}" "/${first_type}/" type_start REVERSE) 1931 string(SUBSTRING "${ARG_OUTPUT_DIR}" 0 ${type_start} path_prefix) 1932 string(SUBSTRING "${ARG_OUTPUT_DIR}" ${type_start} -1 path_suffix) 1933 string(REPLACE "/${first_type}/" "/${CMAKE_CFG_INTDIR}/" 1934 path_suffix ${path_suffix}) 1935 set(ARG_OUTPUT_DIR ${path_prefix}${path_suffix}) 1936 endif() 1937 endif() 1938 1939 if(CMAKE_HOST_UNIX) 1940 set(LLVM_LINK_OR_COPY create_symlink) 1941 else() 1942 set(LLVM_LINK_OR_COPY copy) 1943 endif() 1944 1945 set(output_path "${ARG_OUTPUT_DIR}/${link_name}${CMAKE_EXECUTABLE_SUFFIX}") 1946 1947 set(target_name ${link_name}) 1948 if(TARGET ${link_name}) 1949 set(target_name ${link_name}-link) 1950 endif() 1951 1952 1953 if(ARG_ALWAYS_GENERATE) 1954 set_property(DIRECTORY APPEND PROPERTY 1955 ADDITIONAL_MAKE_CLEAN_FILES ${dest_binary}) 1956 add_custom_command(TARGET ${target} POST_BUILD 1957 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}") 1958 else() 1959 add_custom_command(OUTPUT ${output_path} 1960 COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${dest_binary}" "${output_path}" 1961 DEPENDS ${target}) 1962 add_custom_target(${target_name} ALL DEPENDS ${target} ${output_path}) 1963 set_target_properties(${target_name} PROPERTIES FOLDER Tools) 1964 1965 # Make sure both the link and target are toolchain tools 1966 if (${link_name} IN_LIST LLVM_TOOLCHAIN_TOOLS AND ${target} IN_LIST LLVM_TOOLCHAIN_TOOLS) 1967 set(TOOL_IS_TOOLCHAIN ON) 1968 endif() 1969 1970 if ((TOOL_IS_TOOLCHAIN OR NOT LLVM_INSTALL_TOOLCHAIN_ONLY) AND LLVM_BUILD_TOOLS) 1971 llvm_install_symlink(${link_name} ${target}) 1972 endif() 1973 endif() 1974endfunction() 1975 1976function(llvm_externalize_debuginfo name) 1977 if(NOT LLVM_EXTERNALIZE_DEBUGINFO) 1978 return() 1979 endif() 1980 1981 if(NOT LLVM_EXTERNALIZE_DEBUGINFO_SKIP_STRIP) 1982 if(APPLE) 1983 if(NOT CMAKE_STRIP) 1984 set(CMAKE_STRIP xcrun strip) 1985 endif() 1986 set(strip_command COMMAND ${CMAKE_STRIP} -Sxl $<TARGET_FILE:${name}>) 1987 else() 1988 set(strip_command COMMAND ${CMAKE_STRIP} -g -x $<TARGET_FILE:${name}>) 1989 endif() 1990 endif() 1991 1992 if(APPLE) 1993 if(LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION) 1994 set(file_ext ${LLVM_EXTERNALIZE_DEBUGINFO_EXTENSION}) 1995 else() 1996 set(file_ext dSYM) 1997 endif() 1998 1999 set(output_name "$<TARGET_FILE_NAME:${name}>.${file_ext}") 2000 2001 if(LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR) 2002 set(output_path "-o=${LLVM_EXTERNALIZE_DEBUGINFO_OUTPUT_DIR}/${output_name}") 2003 else() 2004 set(output_path "-o=${output_name}") 2005 endif() 2006 2007 if(CMAKE_CXX_FLAGS MATCHES "-flto" 2008 OR CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} MATCHES "-flto") 2009 2010 set(lto_object ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${name}-lto.o) 2011 set_property(TARGET ${name} APPEND_STRING PROPERTY 2012 LINK_FLAGS " -Wl,-object_path_lto,${lto_object}") 2013 endif() 2014 if(NOT CMAKE_DSYMUTIL) 2015 set(CMAKE_DSYMUTIL xcrun dsymutil) 2016 endif() 2017 add_custom_command(TARGET ${name} POST_BUILD 2018 COMMAND ${CMAKE_DSYMUTIL} ${output_path} $<TARGET_FILE:${name}> 2019 ${strip_command} 2020 ) 2021 else() 2022 add_custom_command(TARGET ${name} POST_BUILD 2023 COMMAND ${CMAKE_OBJCOPY} --only-keep-debug $<TARGET_FILE:${name}> $<TARGET_FILE:${name}>.debug 2024 ${strip_command} -R .gnu_debuglink 2025 COMMAND ${CMAKE_OBJCOPY} --add-gnu-debuglink=$<TARGET_FILE:${name}>.debug $<TARGET_FILE:${name}> 2026 ) 2027 endif() 2028endfunction() 2029 2030# Usage: llvm_codesign(name [FORCE] [ENTITLEMENTS file] [BUNDLE_PATH path]) 2031function(llvm_codesign name) 2032 cmake_parse_arguments(ARG "FORCE" "ENTITLEMENTS;BUNDLE_PATH" "" ${ARGN}) 2033 2034 if(NOT LLVM_CODESIGNING_IDENTITY) 2035 return() 2036 endif() 2037 2038 if(CMAKE_GENERATOR STREQUAL "Xcode") 2039 set_target_properties(${name} PROPERTIES 2040 XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY ${LLVM_CODESIGNING_IDENTITY} 2041 ) 2042 if(DEFINED ARG_ENTITLEMENTS) 2043 set_target_properties(${name} PROPERTIES 2044 XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS ${ARG_ENTITLEMENTS} 2045 ) 2046 endif() 2047 elseif(APPLE AND CMAKE_HOST_SYSTEM_NAME MATCHES Darwin) 2048 if(NOT CMAKE_CODESIGN) 2049 set(CMAKE_CODESIGN xcrun codesign) 2050 endif() 2051 if(NOT CMAKE_CODESIGN_ALLOCATE) 2052 execute_process( 2053 COMMAND xcrun -f codesign_allocate 2054 OUTPUT_STRIP_TRAILING_WHITESPACE 2055 OUTPUT_VARIABLE CMAKE_CODESIGN_ALLOCATE 2056 ) 2057 endif() 2058 if(DEFINED ARG_ENTITLEMENTS) 2059 set(pass_entitlements --entitlements ${ARG_ENTITLEMENTS}) 2060 endif() 2061 2062 if (NOT ARG_BUNDLE_PATH) 2063 set(ARG_BUNDLE_PATH $<TARGET_FILE:${name}>) 2064 endif() 2065 2066 # ld64 now always codesigns the binaries it creates. Apply the force arg 2067 # unconditionally so that we can - for example - add entitlements to the 2068 # targets that need it. 2069 set(force_flag "-f") 2070 2071 add_custom_command( 2072 TARGET ${name} POST_BUILD 2073 COMMAND ${CMAKE_COMMAND} -E 2074 env CODESIGN_ALLOCATE=${CMAKE_CODESIGN_ALLOCATE} 2075 ${CMAKE_CODESIGN} -s ${LLVM_CODESIGNING_IDENTITY} 2076 ${pass_entitlements} ${force_flag} ${ARG_BUNDLE_PATH} 2077 ) 2078 endif() 2079endfunction() 2080 2081function(llvm_setup_rpath name) 2082 if(CMAKE_INSTALL_RPATH) 2083 return() 2084 endif() 2085 2086 if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX)) 2087 set(extra_libdir ${LLVM_LIBRARY_DIR}) 2088 elseif(LLVM_BUILD_LIBRARY_DIR) 2089 set(extra_libdir ${LLVM_LIBRARY_DIR}) 2090 endif() 2091 2092 if (APPLE) 2093 set(_install_name_dir INSTALL_NAME_DIR "@rpath") 2094 set(_install_rpath "@loader_path/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 2095 elseif(${CMAKE_SYSTEM_NAME} MATCHES "AIX" AND BUILD_SHARED_LIBS) 2096 # $ORIGIN is not interpreted at link time by aix ld. 2097 # Since BUILD_SHARED_LIBS is only recommended for use by developers, 2098 # hardcode the rpath to build/install lib dir first in this mode. 2099 # FIXME: update this when there is better solution. 2100 set(_install_rpath "${LLVM_LIBRARY_OUTPUT_INTDIR}" "${CMAKE_INSTALL_PREFIX}/lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 2101 elseif(UNIX) 2102 set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) 2103 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)") 2104 set_property(TARGET ${name} APPEND_STRING PROPERTY 2105 LINK_FLAGS " -Wl,-z,origin ") 2106 endif() 2107 if(LLVM_LINKER_IS_GNULD) 2108 # $ORIGIN is not interpreted at link time by ld.bfd 2109 set_property(TARGET ${name} APPEND_STRING PROPERTY 2110 LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ") 2111 endif() 2112 else() 2113 return() 2114 endif() 2115 2116 # Enable BUILD_WITH_INSTALL_RPATH unless CMAKE_BUILD_RPATH is set. 2117 if("${CMAKE_BUILD_RPATH}" STREQUAL "") 2118 set_property(TARGET ${name} PROPERTY BUILD_WITH_INSTALL_RPATH ON) 2119 endif() 2120 2121 set_target_properties(${name} PROPERTIES 2122 INSTALL_RPATH "${_install_rpath}" 2123 ${_install_name_dir}) 2124endfunction() 2125 2126function(setup_dependency_debugging name) 2127 if(NOT LLVM_DEPENDENCY_DEBUGGING) 2128 return() 2129 endif() 2130 2131 if("intrinsics_gen" IN_LIST ARGN) 2132 return() 2133 endif() 2134 2135 set(deny_attributes_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Attributes.inc\"))") 2136 set(deny_intrinsics_inc "(deny file* (literal \"${LLVM_BINARY_DIR}/include/llvm/IR/Intrinsics.inc\"))") 2137 2138 set(sandbox_command "sandbox-exec -p '(version 1) (allow default) ${deny_attributes_inc} ${deny_intrinsics_inc}'") 2139 set_target_properties(${name} PROPERTIES RULE_LAUNCH_COMPILE ${sandbox_command}) 2140endfunction() 2141 2142# If the sources at the given `path` are under version control, set `out_var` 2143# to the the path of a file which will be modified when the VCS revision 2144# changes, attempting to create that file if it does not exist; if no such 2145# file exists and one cannot be created, instead set `out_var` to the 2146# empty string. 2147# 2148# If the sources are not under version control, do not define `out_var`. 2149function(find_first_existing_vc_file path out_var) 2150 if(NOT EXISTS "${path}") 2151 return() 2152 endif() 2153 find_package(Git) 2154 if(GIT_FOUND) 2155 execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --git-dir 2156 WORKING_DIRECTORY ${path} 2157 RESULT_VARIABLE git_result 2158 OUTPUT_VARIABLE git_output 2159 ERROR_QUIET) 2160 if(git_result EQUAL 0) 2161 string(STRIP "${git_output}" git_output) 2162 get_filename_component(git_dir ${git_output} ABSOLUTE BASE_DIR ${path}) 2163 # Some branchless cases (e.g. 'repo') may not yet have .git/logs/HEAD 2164 if (NOT EXISTS "${git_dir}/logs/HEAD") 2165 execute_process(COMMAND ${CMAKE_COMMAND} -E touch HEAD 2166 WORKING_DIRECTORY "${git_dir}/logs" 2167 RESULT_VARIABLE touch_head_result 2168 ERROR_QUIET) 2169 if (NOT touch_head_result EQUAL 0) 2170 set(${out_var} "" PARENT_SCOPE) 2171 return() 2172 endif() 2173 endif() 2174 set(${out_var} "${git_dir}/logs/HEAD" PARENT_SCOPE) 2175 endif() 2176 endif() 2177endfunction() 2178